blob: 5949d0ac30f2d8b6f0b3a3027a0872c4244f49c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070066#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030068#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070069#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070070#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/mount.h>
72#include <linux/security.h>
73#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070074#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070075#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/cpuset.h>
77#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050078#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070079#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070080#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070081#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070082#include <linux/pid_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040083#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090084#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include "internal.h"
86
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070087/* NOTE:
88 * Implementing inode permission operations in /proc is almost
89 * certainly an error. Permission checks need to happen during
90 * each system call not at open time. The reason is that most of
91 * what we wish to check for permissions in /proc varies at runtime.
92 *
93 * The classic example of a problem is opening file descriptors
94 * in /proc for a task before it execs a suid executable.
95 */
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070099 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800101 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800102 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Eric W. Biederman61a28782006-10-02 02:18:49 -0700106#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700107 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700108 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700109 .mode = MODE, \
110 .iop = IOP, \
111 .fop = FOP, \
112 .op = OP, \
113}
114
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300115#define DIR(NAME, MODE, iops, fops) \
116 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
117#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700118 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300120 { .proc_get_link = get_link } )
121#define REG(NAME, MODE, fops) \
122 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
123#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700124 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700125 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300126 { .proc_read = read } )
127#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300130 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Vegard Nossumaed54172008-06-05 22:46:53 -0700132/*
133 * Count the number of hardlinks for the pid_entry table, excluding the .
134 * and .. links.
135 */
136static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
137 unsigned int n)
138{
139 unsigned int i;
140 unsigned int count;
141
142 count = 0;
143 for (i = 0; i < n; ++i) {
144 if (S_ISDIR(entries[i].mode))
145 ++count;
146 }
147
148 return count;
149}
150
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000151static int get_fs_path(struct task_struct *task, struct path *path, bool root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct fs_struct *fs;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000154 int result = -ENOENT;
155
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700156 task_lock(task);
157 fs = task->fs;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000158 if (fs) {
159 read_lock(&fs->lock);
160 *path = root ? fs->root : fs->pwd;
161 path_get(path);
162 read_unlock(&fs->lock);
163 result = 0;
164 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700165 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000166 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700167}
168
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800169static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700171 struct task_struct *task = get_proc_task(inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700172 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700173
174 if (task) {
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000175 result = get_fs_path(task, path, 0);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176 put_task_struct(task);
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return result;
179}
180
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800181static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700183 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700185
186 if (task) {
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000187 result = get_fs_path(task, path, 1);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700188 put_task_struct(task);
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return result;
191}
192
Roland McGrath638fa202008-04-29 01:01:38 -0700193/*
194 * Return zero if current may access user memory in @task, -error if not.
195 */
196static int check_mem_permission(struct task_struct *task)
197{
198 /*
199 * A task can always look at itself, in case it chooses
200 * to use system calls instead of load instructions.
201 */
202 if (task == current)
203 return 0;
204
205 /*
206 * If current is actively ptrace'ing, and would also be
207 * permitted to freshly attach with ptrace now, permit it.
208 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700209 if (task_is_stopped_or_traced(task)) {
210 int match;
211 rcu_read_lock();
212 match = (tracehook_tracer_task(task) == current);
213 rcu_read_unlock();
214 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
215 return 0;
216 }
Roland McGrath638fa202008-04-29 01:01:38 -0700217
218 /*
219 * Noone else is allowed.
220 */
221 return -EPERM;
222}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Al Viro831830b2008-01-02 14:09:57 +0000224struct mm_struct *mm_for_maps(struct task_struct *task)
225{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200226 struct mm_struct *mm;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200227
Oleg Nesterov704b8362009-07-10 03:27:40 +0200228 if (mutex_lock_killable(&task->cred_guard_mutex))
229 return NULL;
230
231 mm = get_task_mm(task);
232 if (mm && mm != current->mm &&
233 !ptrace_may_access(task, PTRACE_MODE_READ)) {
234 mmput(mm);
235 mm = NULL;
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200236 }
Oleg Nesterov704b8362009-07-10 03:27:40 +0200237 mutex_unlock(&task->cred_guard_mutex);
238
Al Viro831830b2008-01-02 14:09:57 +0000239 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static int proc_pid_cmdline(struct task_struct *task, char * buffer)
243{
244 int res = 0;
245 unsigned int len;
246 struct mm_struct *mm = get_task_mm(task);
247 if (!mm)
248 goto out;
249 if (!mm->arg_end)
250 goto out_mm; /* Shh! No looking before we're done */
251
252 len = mm->arg_end - mm->arg_start;
253
254 if (len > PAGE_SIZE)
255 len = PAGE_SIZE;
256
257 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
258
259 // If the nul at the end of args has been overwritten, then
260 // assume application is using setproctitle(3).
261 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
262 len = strnlen(buffer, res);
263 if (len < res) {
264 res = len;
265 } else {
266 len = mm->env_end - mm->env_start;
267 if (len > PAGE_SIZE - res)
268 len = PAGE_SIZE - res;
269 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
270 res = strnlen(buffer, res);
271 }
272 }
273out_mm:
274 mmput(mm);
275out:
276 return res;
277}
278
279static int proc_pid_auxv(struct task_struct *task, char *buffer)
280{
281 int res = 0;
282 struct mm_struct *mm = get_task_mm(task);
283 if (mm) {
284 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300285 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300287 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 res = nwords * sizeof(mm->saved_auxv[0]);
289 if (res > PAGE_SIZE)
290 res = PAGE_SIZE;
291 memcpy(buffer, mm->saved_auxv, res);
292 mmput(mm);
293 }
294 return res;
295}
296
297
298#ifdef CONFIG_KALLSYMS
299/*
300 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
301 * Returns the resolved symbol. If that fails, simply return the address.
302 */
303static int proc_pid_wchan(struct task_struct *task, char *buffer)
304{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700305 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700306 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 wchan = get_wchan(task);
309
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700310 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600311 if (!ptrace_may_access(task, PTRACE_MODE_READ))
312 return 0;
313 else
314 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700315 else
316 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318#endif /* CONFIG_KALLSYMS */
319
Ken Chen2ec220e2008-11-10 11:26:08 +0300320#ifdef CONFIG_STACKTRACE
321
322#define MAX_STACK_TRACE_DEPTH 64
323
324static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
325 struct pid *pid, struct task_struct *task)
326{
327 struct stack_trace trace;
328 unsigned long *entries;
329 int i;
330
331 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
332 if (!entries)
333 return -ENOMEM;
334
335 trace.nr_entries = 0;
336 trace.max_entries = MAX_STACK_TRACE_DEPTH;
337 trace.entries = entries;
338 trace.skip = 0;
339 save_stack_trace_tsk(task, &trace);
340
341 for (i = 0; i < trace.nr_entries; i++) {
342 seq_printf(m, "[<%p>] %pS\n",
343 (void *)entries[i], (void *)entries[i]);
344 }
345 kfree(entries);
346
347 return 0;
348}
349#endif
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#ifdef CONFIG_SCHEDSTATS
352/*
353 * Provides /proc/PID/schedstat
354 */
355static int proc_pid_schedstat(struct task_struct *task, char *buffer)
356{
Balbir Singh172ba842007-07-09 18:52:00 +0200357 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100358 (unsigned long long)task->se.sum_exec_runtime,
359 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200360 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362#endif
363
Arjan van de Ven97455122008-01-25 21:08:34 +0100364#ifdef CONFIG_LATENCYTOP
365static int lstats_show_proc(struct seq_file *m, void *v)
366{
367 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800368 struct inode *inode = m->private;
369 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100370
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800371 if (!task)
372 return -ESRCH;
373 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100374 for (i = 0; i < 32; i++) {
375 if (task->latency_record[i].backtrace[0]) {
376 int q;
377 seq_printf(m, "%i %li %li ",
378 task->latency_record[i].count,
379 task->latency_record[i].time,
380 task->latency_record[i].max);
381 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Hugh Dickins9c246242008-12-09 13:14:27 -0800382 char sym[KSYM_SYMBOL_LEN];
Arjan van de Ven97455122008-01-25 21:08:34 +0100383 char *c;
384 if (!task->latency_record[i].backtrace[q])
385 break;
386 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
387 break;
388 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
389 c = strchr(sym, '+');
390 if (c)
391 *c = 0;
392 seq_printf(m, "%s ", sym);
393 }
394 seq_printf(m, "\n");
395 }
396
397 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800398 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100399 return 0;
400}
401
402static int lstats_open(struct inode *inode, struct file *file)
403{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800404 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800405}
406
Arjan van de Ven97455122008-01-25 21:08:34 +0100407static ssize_t lstats_write(struct file *file, const char __user *buf,
408 size_t count, loff_t *offs)
409{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800410 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100411
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800412 if (!task)
413 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100414 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800415 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100416
417 return count;
418}
419
420static const struct file_operations proc_lstats_operations = {
421 .open = lstats_open,
422 .read = seq_read,
423 .write = lstats_write,
424 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800425 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100426};
427
428#endif
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static int proc_oom_score(struct task_struct *task, char *buffer)
431{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200432 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct timespec uptime;
434
435 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700436 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200437 if (pid_alive(task))
KOSAKI Motohiro26ebc982010-08-09 17:19:37 -0700438 points = badness(task, NULL, NULL, uptime.tv_sec);
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 McGrath638fa202008-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 McGrath638fa202008-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 McGrath638fa202008-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
1040 task->signal->oom_adj = oom_adjust;
1041
1042 unlock_task_sighand(task, &flags);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001043 put_task_struct(task);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001044
1045 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Arjan van de Ven00977a52007-02-12 00:55:34 -08001048static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 .read = oom_adjust_read,
1050 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001051 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052};
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054#ifdef CONFIG_AUDITSYSCALL
1055#define TMPBUFLEN 21
1056static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1057 size_t count, loff_t *ppos)
1058{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001059 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001060 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 ssize_t length;
1062 char tmpbuf[TMPBUFLEN];
1063
Eric W. Biederman99f89552006-06-26 00:25:55 -07001064 if (!task)
1065 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001067 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001068 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1070}
1071
1072static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1073 size_t count, loff_t *ppos)
1074{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001075 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 char *page, *tmp;
1077 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 uid_t loginuid;
1079
1080 if (!capable(CAP_AUDIT_CONTROL))
1081 return -EPERM;
1082
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001083 rcu_read_lock();
1084 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1085 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001087 }
1088 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Al Viroe0182902006-05-18 08:28:02 -04001090 if (count >= PAGE_SIZE)
1091 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (*ppos != 0) {
1094 /* No partial writes. */
1095 return -EINVAL;
1096 }
Mel Gormane12ba742007-10-16 01:25:52 -07001097 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!page)
1099 return -ENOMEM;
1100 length = -EFAULT;
1101 if (copy_from_user(page, buf, count))
1102 goto out_free_page;
1103
Al Viroe0182902006-05-18 08:28:02 -04001104 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 loginuid = simple_strtoul(page, &tmp, 10);
1106 if (tmp == page) {
1107 length = -EINVAL;
1108 goto out_free_page;
1109
1110 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001111 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (likely(length == 0))
1113 length = count;
1114
1115out_free_page:
1116 free_page((unsigned long) page);
1117 return length;
1118}
1119
Arjan van de Ven00977a52007-02-12 00:55:34 -08001120static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 .read = proc_loginuid_read,
1122 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001123 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124};
Eric Paris1e0bd752008-03-13 08:15:31 -04001125
1126static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1127 size_t count, loff_t *ppos)
1128{
1129 struct inode * inode = file->f_path.dentry->d_inode;
1130 struct task_struct *task = get_proc_task(inode);
1131 ssize_t length;
1132 char tmpbuf[TMPBUFLEN];
1133
1134 if (!task)
1135 return -ESRCH;
1136 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1137 audit_get_sessionid(task));
1138 put_task_struct(task);
1139 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1140}
1141
1142static const struct file_operations proc_sessionid_operations = {
1143 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001144 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001145};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146#endif
1147
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001148#ifdef CONFIG_FAULT_INJECTION
1149static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1150 size_t count, loff_t *ppos)
1151{
1152 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1153 char buffer[PROC_NUMBUF];
1154 size_t len;
1155 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001156
1157 if (!task)
1158 return -ESRCH;
1159 make_it_fail = task->make_it_fail;
1160 put_task_struct(task);
1161
1162 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001163
1164 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001165}
1166
1167static ssize_t proc_fault_inject_write(struct file * file,
1168 const char __user * buf, size_t count, loff_t *ppos)
1169{
1170 struct task_struct *task;
1171 char buffer[PROC_NUMBUF], *end;
1172 int make_it_fail;
1173
1174 if (!capable(CAP_SYS_RESOURCE))
1175 return -EPERM;
1176 memset(buffer, 0, sizeof(buffer));
1177 if (count > sizeof(buffer) - 1)
1178 count = sizeof(buffer) - 1;
1179 if (copy_from_user(buffer, buf, count))
1180 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001181 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1182 if (*end)
1183 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001184 task = get_proc_task(file->f_dentry->d_inode);
1185 if (!task)
1186 return -ESRCH;
1187 task->make_it_fail = make_it_fail;
1188 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001189
1190 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001191}
1192
Arjan van de Ven00977a52007-02-12 00:55:34 -08001193static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001194 .read = proc_fault_inject_read,
1195 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001196 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001197};
1198#endif
1199
Arjan van de Ven97455122008-01-25 21:08:34 +01001200
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001201#ifdef CONFIG_SCHED_DEBUG
1202/*
1203 * Print out various scheduling related per-task fields:
1204 */
1205static int sched_show(struct seq_file *m, void *v)
1206{
1207 struct inode *inode = m->private;
1208 struct task_struct *p;
1209
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001210 p = get_proc_task(inode);
1211 if (!p)
1212 return -ESRCH;
1213 proc_sched_show_task(p, m);
1214
1215 put_task_struct(p);
1216
1217 return 0;
1218}
1219
1220static ssize_t
1221sched_write(struct file *file, const char __user *buf,
1222 size_t count, loff_t *offset)
1223{
1224 struct inode *inode = file->f_path.dentry->d_inode;
1225 struct task_struct *p;
1226
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001227 p = get_proc_task(inode);
1228 if (!p)
1229 return -ESRCH;
1230 proc_sched_set_task(p);
1231
1232 put_task_struct(p);
1233
1234 return count;
1235}
1236
1237static int sched_open(struct inode *inode, struct file *filp)
1238{
1239 int ret;
1240
1241 ret = single_open(filp, sched_show, NULL);
1242 if (!ret) {
1243 struct seq_file *m = filp->private_data;
1244
1245 m->private = inode;
1246 }
1247 return ret;
1248}
1249
1250static const struct file_operations proc_pid_sched_operations = {
1251 .open = sched_open,
1252 .read = seq_read,
1253 .write = sched_write,
1254 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001255 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001256};
1257
1258#endif
1259
john stultz4614a696b2009-12-14 18:00:05 -08001260static ssize_t comm_write(struct file *file, const char __user *buf,
1261 size_t count, loff_t *offset)
1262{
1263 struct inode *inode = file->f_path.dentry->d_inode;
1264 struct task_struct *p;
1265 char buffer[TASK_COMM_LEN];
1266
1267 memset(buffer, 0, sizeof(buffer));
1268 if (count > sizeof(buffer) - 1)
1269 count = sizeof(buffer) - 1;
1270 if (copy_from_user(buffer, buf, count))
1271 return -EFAULT;
1272
1273 p = get_proc_task(inode);
1274 if (!p)
1275 return -ESRCH;
1276
1277 if (same_thread_group(current, p))
1278 set_task_comm(p, buffer);
1279 else
1280 count = -EINVAL;
1281
1282 put_task_struct(p);
1283
1284 return count;
1285}
1286
1287static int comm_show(struct seq_file *m, void *v)
1288{
1289 struct inode *inode = m->private;
1290 struct task_struct *p;
1291
1292 p = get_proc_task(inode);
1293 if (!p)
1294 return -ESRCH;
1295
1296 task_lock(p);
1297 seq_printf(m, "%s\n", p->comm);
1298 task_unlock(p);
1299
1300 put_task_struct(p);
1301
1302 return 0;
1303}
1304
1305static int comm_open(struct inode *inode, struct file *filp)
1306{
1307 int ret;
1308
1309 ret = single_open(filp, comm_show, NULL);
1310 if (!ret) {
1311 struct seq_file *m = filp->private_data;
1312
1313 m->private = inode;
1314 }
1315 return ret;
1316}
1317
1318static const struct file_operations proc_pid_set_comm_operations = {
1319 .open = comm_open,
1320 .read = seq_read,
1321 .write = comm_write,
1322 .llseek = seq_lseek,
1323 .release = single_release,
1324};
1325
Matt Helsley925d1c42008-04-29 01:01:36 -07001326/*
1327 * We added or removed a vma mapping the executable. The vmas are only mapped
1328 * during exec and are not mapped with the mmap system call.
1329 * Callers must hold down_write() on the mm's mmap_sem for these
1330 */
1331void added_exe_file_vma(struct mm_struct *mm)
1332{
1333 mm->num_exe_file_vmas++;
1334}
1335
1336void removed_exe_file_vma(struct mm_struct *mm)
1337{
1338 mm->num_exe_file_vmas--;
1339 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1340 fput(mm->exe_file);
1341 mm->exe_file = NULL;
1342 }
1343
1344}
1345
1346void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1347{
1348 if (new_exe_file)
1349 get_file(new_exe_file);
1350 if (mm->exe_file)
1351 fput(mm->exe_file);
1352 mm->exe_file = new_exe_file;
1353 mm->num_exe_file_vmas = 0;
1354}
1355
1356struct file *get_mm_exe_file(struct mm_struct *mm)
1357{
1358 struct file *exe_file;
1359
1360 /* We need mmap_sem to protect against races with removal of
1361 * VM_EXECUTABLE vmas */
1362 down_read(&mm->mmap_sem);
1363 exe_file = mm->exe_file;
1364 if (exe_file)
1365 get_file(exe_file);
1366 up_read(&mm->mmap_sem);
1367 return exe_file;
1368}
1369
1370void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1371{
1372 /* It's safe to write the exe_file pointer without exe_file_lock because
1373 * this is called during fork when the task is not yet in /proc */
1374 newmm->exe_file = get_mm_exe_file(oldmm);
1375}
1376
1377static int proc_exe_link(struct inode *inode, struct path *exe_path)
1378{
1379 struct task_struct *task;
1380 struct mm_struct *mm;
1381 struct file *exe_file;
1382
1383 task = get_proc_task(inode);
1384 if (!task)
1385 return -ENOENT;
1386 mm = get_task_mm(task);
1387 put_task_struct(task);
1388 if (!mm)
1389 return -ENOENT;
1390 exe_file = get_mm_exe_file(mm);
1391 mmput(mm);
1392 if (exe_file) {
1393 *exe_path = exe_file->f_path;
1394 path_get(&exe_file->f_path);
1395 fput(exe_file);
1396 return 0;
1397 } else
1398 return -ENOENT;
1399}
1400
Al Viro008b1502005-08-20 00:17:39 +01001401static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct inode *inode = dentry->d_inode;
1404 int error = -EACCES;
1405
1406 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001407 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Eric W. Biederman778c1142006-06-26 00:25:58 -07001409 /* Are we allowed to snoop on the tasks file descriptors? */
1410 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001413 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414out:
Al Viro008b1502005-08-20 00:17:39 +01001415 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001418static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Mel Gormane12ba742007-10-16 01:25:52 -07001420 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001421 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int len;
1423
1424 if (!tmp)
1425 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001426
Jan Blunckcf28b482008-02-14 19:38:44 -08001427 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001428 len = PTR_ERR(pathname);
1429 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001431 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (len > buflen)
1434 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001435 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 len = -EFAULT;
1437 out:
1438 free_page((unsigned long)tmp);
1439 return len;
1440}
1441
1442static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1443{
1444 int error = -EACCES;
1445 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001446 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Eric W. Biederman778c1142006-06-26 00:25:58 -07001448 /* Are we allowed to snoop on the tasks file descriptors? */
1449 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001452 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (error)
1454 goto out;
1455
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001456 error = do_proc_readlink(&path, buffer, buflen);
1457 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return error;
1460}
1461
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001462static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001464 .follow_link = proc_pid_follow_link,
1465 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466};
1467
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001468
1469/* building an inode */
1470
1471static int task_dumpable(struct task_struct *task)
1472{
1473 int dumpable = 0;
1474 struct mm_struct *mm;
1475
1476 task_lock(task);
1477 mm = task->mm;
1478 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001479 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001480 task_unlock(task);
1481 if(dumpable == 1)
1482 return 1;
1483 return 0;
1484}
1485
1486
Eric W. Biederman61a28782006-10-02 02:18:49 -07001487static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001488{
1489 struct inode * inode;
1490 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001491 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001492
1493 /* We need a new inode */
1494
1495 inode = new_inode(sb);
1496 if (!inode)
1497 goto out;
1498
1499 /* Common stuff */
1500 ei = PROC_I(inode);
1501 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001502 inode->i_op = &proc_def_inode_operations;
1503
1504 /*
1505 * grab the reference to task.
1506 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001507 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001508 if (!ei->pid)
1509 goto out_unlock;
1510
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001511 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001512 rcu_read_lock();
1513 cred = __task_cred(task);
1514 inode->i_uid = cred->euid;
1515 inode->i_gid = cred->egid;
1516 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001517 }
1518 security_task_to_inode(task, inode);
1519
1520out:
1521 return inode;
1522
1523out_unlock:
1524 iput(inode);
1525 return NULL;
1526}
1527
1528static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1529{
1530 struct inode *inode = dentry->d_inode;
1531 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001532 const struct cred *cred;
1533
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001534 generic_fillattr(inode, stat);
1535
1536 rcu_read_lock();
1537 stat->uid = 0;
1538 stat->gid = 0;
1539 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1540 if (task) {
1541 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1542 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001543 cred = __task_cred(task);
1544 stat->uid = cred->euid;
1545 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001546 }
1547 }
1548 rcu_read_unlock();
1549 return 0;
1550}
1551
1552/* dentry stuff */
1553
1554/*
1555 * Exceptional case: normally we are not allowed to unhash a busy
1556 * directory. In this case, however, we can do it - no aliasing problems
1557 * due to the way we treat inodes.
1558 *
1559 * Rewrite the inode's ownerships here because the owning task may have
1560 * performed a setuid(), etc.
1561 *
1562 * Before the /proc/pid/status file was created the only way to read
1563 * the effective uid of a /process was to stat /proc/pid. Reading
1564 * /proc/pid/status is slow enough that procps and other packages
1565 * kept stating /proc/pid. To keep the rules in /proc simple I have
1566 * made this apply to all per process world readable and executable
1567 * directories.
1568 */
1569static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1570{
1571 struct inode *inode = dentry->d_inode;
1572 struct task_struct *task = get_proc_task(inode);
David Howellsc69e8d92008-11-14 10:39:19 +11001573 const struct cred *cred;
1574
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001575 if (task) {
1576 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1577 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001578 rcu_read_lock();
1579 cred = __task_cred(task);
1580 inode->i_uid = cred->euid;
1581 inode->i_gid = cred->egid;
1582 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001583 } else {
1584 inode->i_uid = 0;
1585 inode->i_gid = 0;
1586 }
1587 inode->i_mode &= ~(S_ISUID | S_ISGID);
1588 security_task_to_inode(task, inode);
1589 put_task_struct(task);
1590 return 1;
1591 }
1592 d_drop(dentry);
1593 return 0;
1594}
1595
1596static int pid_delete_dentry(struct dentry * dentry)
1597{
1598 /* Is the task we represent dead?
1599 * If so, then don't put the dentry on the lru list,
1600 * kill it immediately.
1601 */
1602 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1603}
1604
Al Virod72f71e2009-02-20 05:58:47 +00001605static const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001606{
1607 .d_revalidate = pid_revalidate,
1608 .d_delete = pid_delete_dentry,
1609};
1610
1611/* Lookups */
1612
Eric Dumazetc5141e62007-05-08 00:26:15 -07001613typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1614 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001615
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001616/*
1617 * Fill a directory entry.
1618 *
1619 * If possible create the dcache entry and derive our inode number and
1620 * file type from dcache entry.
1621 *
1622 * Since all of the proc inode numbers are dynamically generated, the inode
1623 * numbers do not exist until the inode is cache. This means creating the
1624 * the dcache entry in readdir is necessary to keep the inode numbers
1625 * reported by readdir in sync with the inode numbers reported
1626 * by stat.
1627 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001628static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1629 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001630 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001631{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001632 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001633 struct inode *inode;
1634 struct qstr qname;
1635 ino_t ino = 0;
1636 unsigned type = DT_UNKNOWN;
1637
1638 qname.name = name;
1639 qname.len = len;
1640 qname.hash = full_name_hash(name, len);
1641
1642 child = d_lookup(dir, &qname);
1643 if (!child) {
1644 struct dentry *new;
1645 new = d_alloc(dir, &qname);
1646 if (new) {
1647 child = instantiate(dir->d_inode, new, task, ptr);
1648 if (child)
1649 dput(new);
1650 else
1651 child = new;
1652 }
1653 }
1654 if (!child || IS_ERR(child) || !child->d_inode)
1655 goto end_instantiate;
1656 inode = child->d_inode;
1657 if (inode) {
1658 ino = inode->i_ino;
1659 type = inode->i_mode >> 12;
1660 }
1661 dput(child);
1662end_instantiate:
1663 if (!ino)
1664 ino = find_inode_number(dir, &qname);
1665 if (!ino)
1666 ino = 1;
1667 return filldir(dirent, name, len, filp->f_pos, ino, type);
1668}
1669
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001670static unsigned name_to_int(struct dentry *dentry)
1671{
1672 const char *name = dentry->d_name.name;
1673 int len = dentry->d_name.len;
1674 unsigned n = 0;
1675
1676 if (len > 1 && *name == '0')
1677 goto out;
1678 while (len-- > 0) {
1679 unsigned c = *name++ - '0';
1680 if (c > 9)
1681 goto out;
1682 if (n >= (~0U-9)/10)
1683 goto out;
1684 n *= 10;
1685 n += c;
1686 }
1687 return n;
1688out:
1689 return ~0U;
1690}
1691
Miklos Szeredi27932742007-05-08 00:26:17 -07001692#define PROC_FDINFO_MAX 64
1693
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001694static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001695{
1696 struct task_struct *task = get_proc_task(inode);
1697 struct files_struct *files = NULL;
1698 struct file *file;
1699 int fd = proc_fd(inode);
1700
1701 if (task) {
1702 files = get_files_struct(task);
1703 put_task_struct(task);
1704 }
1705 if (files) {
1706 /*
1707 * We are not taking a ref to the file structure, so we must
1708 * hold ->file_lock.
1709 */
1710 spin_lock(&files->file_lock);
1711 file = fcheck_files(files, fd);
1712 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001713 if (path) {
1714 *path = file->f_path;
1715 path_get(&file->f_path);
1716 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001717 if (info)
1718 snprintf(info, PROC_FDINFO_MAX,
1719 "pos:\t%lli\n"
1720 "flags:\t0%o\n",
1721 (long long) file->f_pos,
1722 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001723 spin_unlock(&files->file_lock);
1724 put_files_struct(files);
1725 return 0;
1726 }
1727 spin_unlock(&files->file_lock);
1728 put_files_struct(files);
1729 }
1730 return -ENOENT;
1731}
1732
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001733static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001734{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001735 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001736}
1737
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001738static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1739{
1740 struct inode *inode = dentry->d_inode;
1741 struct task_struct *task = get_proc_task(inode);
1742 int fd = proc_fd(inode);
1743 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001744 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001745
1746 if (task) {
1747 files = get_files_struct(task);
1748 if (files) {
1749 rcu_read_lock();
1750 if (fcheck_files(files, fd)) {
1751 rcu_read_unlock();
1752 put_files_struct(files);
1753 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001754 rcu_read_lock();
1755 cred = __task_cred(task);
1756 inode->i_uid = cred->euid;
1757 inode->i_gid = cred->egid;
1758 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001759 } else {
1760 inode->i_uid = 0;
1761 inode->i_gid = 0;
1762 }
1763 inode->i_mode &= ~(S_ISUID | S_ISGID);
1764 security_task_to_inode(task, inode);
1765 put_task_struct(task);
1766 return 1;
1767 }
1768 rcu_read_unlock();
1769 put_files_struct(files);
1770 }
1771 put_task_struct(task);
1772 }
1773 d_drop(dentry);
1774 return 0;
1775}
1776
Al Virod72f71e2009-02-20 05:58:47 +00001777static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778{
1779 .d_revalidate = tid_fd_revalidate,
1780 .d_delete = pid_delete_dentry,
1781};
1782
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001783static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001784 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001785{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001786 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001787 struct file *file;
1788 struct files_struct *files;
1789 struct inode *inode;
1790 struct proc_inode *ei;
1791 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001792
Eric W. Biederman61a28782006-10-02 02:18:49 -07001793 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001794 if (!inode)
1795 goto out;
1796 ei = PROC_I(inode);
1797 ei->fd = fd;
1798 files = get_files_struct(task);
1799 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001800 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001801 inode->i_mode = S_IFLNK;
1802
1803 /*
1804 * We are not taking a ref to the file structure, so we must
1805 * hold ->file_lock.
1806 */
1807 spin_lock(&files->file_lock);
1808 file = fcheck_files(files, fd);
1809 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001810 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001811 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001812 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001813 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001814 inode->i_mode |= S_IWUSR | S_IXUSR;
1815 spin_unlock(&files->file_lock);
1816 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001817
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001818 inode->i_op = &proc_pid_link_inode_operations;
1819 inode->i_size = 64;
1820 ei->op.proc_get_link = proc_fd_link;
1821 dentry->d_op = &tid_fd_dentry_operations;
1822 d_add(dentry, inode);
1823 /* Close the race of the process dying before we return the dentry */
1824 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001825 error = NULL;
1826
1827 out:
1828 return error;
1829out_unlock:
1830 spin_unlock(&files->file_lock);
1831 put_files_struct(files);
1832out_iput:
1833 iput(inode);
1834 goto out;
1835}
1836
Miklos Szeredi27932742007-05-08 00:26:17 -07001837static struct dentry *proc_lookupfd_common(struct inode *dir,
1838 struct dentry *dentry,
1839 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001840{
1841 struct task_struct *task = get_proc_task(dir);
1842 unsigned fd = name_to_int(dentry);
1843 struct dentry *result = ERR_PTR(-ENOENT);
1844
1845 if (!task)
1846 goto out_no_task;
1847 if (fd == ~0U)
1848 goto out;
1849
Miklos Szeredi27932742007-05-08 00:26:17 -07001850 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001851out:
1852 put_task_struct(task);
1853out_no_task:
1854 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001855}
1856
Miklos Szeredi27932742007-05-08 00:26:17 -07001857static int proc_readfd_common(struct file * filp, void * dirent,
1858 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001860 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001861 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001862 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001863 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 struct files_struct * files;
1866
1867 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001868 if (!p)
1869 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 fd = filp->f_pos;
1873 switch (fd) {
1874 case 0:
1875 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1876 goto out;
1877 filp->f_pos++;
1878 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001879 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1881 goto out;
1882 filp->f_pos++;
1883 default:
1884 files = get_files_struct(p);
1885 if (!files)
1886 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001887 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001889 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001891 char name[PROC_NUMBUF];
1892 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 if (!fcheck_files(files, fd))
1895 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001896 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Miklos Szeredi27932742007-05-08 00:26:17 -07001898 len = snprintf(name, sizeof(name), "%d", fd);
1899 if (proc_fill_cache(filp, dirent, filldir,
1900 name, len, instantiate,
1901 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001902 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001905 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001907 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 put_files_struct(files);
1909 }
1910out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001911 put_task_struct(p);
1912out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return retval;
1914}
1915
Miklos Szeredi27932742007-05-08 00:26:17 -07001916static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1917 struct nameidata *nd)
1918{
1919 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1920}
1921
1922static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1923{
1924 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1925}
1926
1927static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1928 size_t len, loff_t *ppos)
1929{
1930 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001931 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001932 if (!err)
1933 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1934 return err;
1935}
1936
1937static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01001938 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07001939 .read = proc_fdinfo_read,
1940};
1941
Arjan van de Ven00977a52007-02-12 00:55:34 -08001942static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 .read = generic_read_dir,
1944 .readdir = proc_readfd,
1945};
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001948 * /proc/pid/fd needs a special permission handler so that a process can still
1949 * access /proc/self/fd after it has executed a setuid().
1950 */
Al Viroe6305c42008-07-15 21:03:57 -04001951static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001952{
1953 int rv;
1954
1955 rv = generic_permission(inode, mask, NULL);
1956 if (rv == 0)
1957 return 0;
1958 if (task_pid(current) == proc_pid(inode))
1959 rv = 0;
1960 return rv;
1961}
1962
1963/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 * proc directories can do almost nothing..
1965 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001966static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001968 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001969 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970};
1971
Miklos Szeredi27932742007-05-08 00:26:17 -07001972static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1973 struct dentry *dentry, struct task_struct *task, const void *ptr)
1974{
1975 unsigned fd = *(unsigned *)ptr;
1976 struct inode *inode;
1977 struct proc_inode *ei;
1978 struct dentry *error = ERR_PTR(-ENOENT);
1979
1980 inode = proc_pid_make_inode(dir->i_sb, task);
1981 if (!inode)
1982 goto out;
1983 ei = PROC_I(inode);
1984 ei->fd = fd;
1985 inode->i_mode = S_IFREG | S_IRUSR;
1986 inode->i_fop = &proc_fdinfo_file_operations;
1987 dentry->d_op = &tid_fd_dentry_operations;
1988 d_add(dentry, inode);
1989 /* Close the race of the process dying before we return the dentry */
1990 if (tid_fd_revalidate(dentry, NULL))
1991 error = NULL;
1992
1993 out:
1994 return error;
1995}
1996
1997static struct dentry *proc_lookupfdinfo(struct inode *dir,
1998 struct dentry *dentry,
1999 struct nameidata *nd)
2000{
2001 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2002}
2003
2004static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2005{
2006 return proc_readfd_common(filp, dirent, filldir,
2007 proc_fdinfo_instantiate);
2008}
2009
2010static const struct file_operations proc_fdinfo_operations = {
2011 .read = generic_read_dir,
2012 .readdir = proc_readfdinfo,
2013};
2014
2015/*
2016 * proc directories can do almost nothing..
2017 */
2018static const struct inode_operations proc_fdinfo_inode_operations = {
2019 .lookup = proc_lookupfdinfo,
2020 .setattr = proc_setattr,
2021};
2022
2023
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002024static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002025 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002026{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002027 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002028 struct inode *inode;
2029 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002030 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002031
Eric W. Biederman61a28782006-10-02 02:18:49 -07002032 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002033 if (!inode)
2034 goto out;
2035
2036 ei = PROC_I(inode);
2037 inode->i_mode = p->mode;
2038 if (S_ISDIR(inode->i_mode))
2039 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2040 if (p->iop)
2041 inode->i_op = p->iop;
2042 if (p->fop)
2043 inode->i_fop = p->fop;
2044 ei->op = p->op;
2045 dentry->d_op = &pid_dentry_operations;
2046 d_add(dentry, inode);
2047 /* Close the race of the process dying before we return the dentry */
2048 if (pid_revalidate(dentry, NULL))
2049 error = NULL;
2050out:
2051 return error;
2052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054static struct dentry *proc_pident_lookup(struct inode *dir,
2055 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002056 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002057 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002059 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002060 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002061 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002063 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Eric W. Biederman99f89552006-06-26 00:25:55 -07002065 if (!task)
2066 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002068 /*
2069 * Yes, it does not scale. And it should not. Don't add
2070 * new entries into /proc/<tgid>/ without very good reasons.
2071 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002072 last = &ents[nents - 1];
2073 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (p->len != dentry->d_name.len)
2075 continue;
2076 if (!memcmp(dentry->d_name.name, p->name, p->len))
2077 break;
2078 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002079 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 goto out;
2081
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002082 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002084 put_task_struct(task);
2085out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002086 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Eric Dumazetc5141e62007-05-08 00:26:15 -07002089static int proc_pident_fill_cache(struct file *filp, void *dirent,
2090 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002091{
2092 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2093 proc_pident_instantiate, task, p);
2094}
2095
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002096static int proc_pident_readdir(struct file *filp,
2097 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002098 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002099{
2100 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002101 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002102 struct inode *inode = dentry->d_inode;
2103 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002104 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002105 ino_t ino;
2106 int ret;
2107
2108 ret = -ENOENT;
2109 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002110 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002111
2112 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002113 i = filp->f_pos;
2114 switch (i) {
2115 case 0:
2116 ino = inode->i_ino;
2117 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2118 goto out;
2119 i++;
2120 filp->f_pos++;
2121 /* fall through */
2122 case 1:
2123 ino = parent_ino(dentry);
2124 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2125 goto out;
2126 i++;
2127 filp->f_pos++;
2128 /* fall through */
2129 default:
2130 i -= 2;
2131 if (i >= nents) {
2132 ret = 1;
2133 goto out;
2134 }
2135 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002136 last = &ents[nents - 1];
2137 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002138 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002139 goto out;
2140 filp->f_pos++;
2141 p++;
2142 }
2143 }
2144
2145 ret = 1;
2146out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002147 put_task_struct(task);
2148out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002149 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002153static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2154 size_t count, loff_t *ppos)
2155{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002156 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002157 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002158 ssize_t length;
2159 struct task_struct *task = get_proc_task(inode);
2160
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002161 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002162 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002163
2164 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002165 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002166 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002167 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002168 if (length > 0)
2169 length = simple_read_from_buffer(buf, count, ppos, p, length);
2170 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002171 return length;
2172}
2173
2174static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2175 size_t count, loff_t *ppos)
2176{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002177 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002178 char *page;
2179 ssize_t length;
2180 struct task_struct *task = get_proc_task(inode);
2181
2182 length = -ESRCH;
2183 if (!task)
2184 goto out_no_task;
2185 if (count > PAGE_SIZE)
2186 count = PAGE_SIZE;
2187
2188 /* No partial writes. */
2189 length = -EINVAL;
2190 if (*ppos != 0)
2191 goto out;
2192
2193 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002194 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002195 if (!page)
2196 goto out;
2197
2198 length = -EFAULT;
2199 if (copy_from_user(page, buf, count))
2200 goto out_free;
2201
David Howells107db7c2009-05-08 13:55:27 +01002202 /* Guard against adverse ptrace interaction */
2203 length = mutex_lock_interruptible(&task->cred_guard_mutex);
2204 if (length < 0)
2205 goto out_free;
2206
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002207 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002208 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002209 (void*)page, count);
David Howells107db7c2009-05-08 13:55:27 +01002210 mutex_unlock(&task->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002211out_free:
2212 free_page((unsigned long) page);
2213out:
2214 put_task_struct(task);
2215out_no_task:
2216 return length;
2217}
2218
Arjan van de Ven00977a52007-02-12 00:55:34 -08002219static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002220 .read = proc_pid_attr_read,
2221 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002222 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002223};
2224
Eric Dumazetc5141e62007-05-08 00:26:15 -07002225static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002226 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2227 REG("prev", S_IRUGO, proc_pid_attr_operations),
2228 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2229 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2230 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2231 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002232};
2233
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002234static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 void * dirent, filldir_t filldir)
2236{
2237 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002238 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Arjan van de Ven00977a52007-02-12 00:55:34 -08002241static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002243 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244};
2245
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002246static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 struct dentry *dentry, struct nameidata *nd)
2248{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002249 return proc_pident_lookup(dir, dentry,
2250 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002253static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002254 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002255 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002256 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257};
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259#endif
2260
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002261#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002262static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2263 size_t count, loff_t *ppos)
2264{
2265 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2266 struct mm_struct *mm;
2267 char buffer[PROC_NUMBUF];
2268 size_t len;
2269 int ret;
2270
2271 if (!task)
2272 return -ESRCH;
2273
2274 ret = 0;
2275 mm = get_task_mm(task);
2276 if (mm) {
2277 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2278 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2279 MMF_DUMP_FILTER_SHIFT));
2280 mmput(mm);
2281 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2282 }
2283
2284 put_task_struct(task);
2285
2286 return ret;
2287}
2288
2289static ssize_t proc_coredump_filter_write(struct file *file,
2290 const char __user *buf,
2291 size_t count,
2292 loff_t *ppos)
2293{
2294 struct task_struct *task;
2295 struct mm_struct *mm;
2296 char buffer[PROC_NUMBUF], *end;
2297 unsigned int val;
2298 int ret;
2299 int i;
2300 unsigned long mask;
2301
2302 ret = -EFAULT;
2303 memset(buffer, 0, sizeof(buffer));
2304 if (count > sizeof(buffer) - 1)
2305 count = sizeof(buffer) - 1;
2306 if (copy_from_user(buffer, buf, count))
2307 goto out_no_task;
2308
2309 ret = -EINVAL;
2310 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2311 if (*end == '\n')
2312 end++;
2313 if (end - buffer == 0)
2314 goto out_no_task;
2315
2316 ret = -ESRCH;
2317 task = get_proc_task(file->f_dentry->d_inode);
2318 if (!task)
2319 goto out_no_task;
2320
2321 ret = end - buffer;
2322 mm = get_task_mm(task);
2323 if (!mm)
2324 goto out_no_mm;
2325
2326 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2327 if (val & mask)
2328 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2329 else
2330 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2331 }
2332
2333 mmput(mm);
2334 out_no_mm:
2335 put_task_struct(task);
2336 out_no_task:
2337 return ret;
2338}
2339
2340static const struct file_operations proc_coredump_filter_operations = {
2341 .read = proc_coredump_filter_read,
2342 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002343 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002344};
2345#endif
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347/*
2348 * /proc/self:
2349 */
2350static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2351 int buflen)
2352{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002353 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002354 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002355 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002356 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002357 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002358 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return vfs_readlink(dentry,buffer,buflen,tmp);
2360}
2361
Al Viro008b1502005-08-20 00:17:39 +01002362static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002364 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002365 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002366 char *name = ERR_PTR(-ENOENT);
2367 if (tgid) {
2368 name = __getname();
2369 if (!name)
2370 name = ERR_PTR(-ENOMEM);
2371 else
2372 sprintf(name, "%d", tgid);
2373 }
2374 nd_set_link(nd, name);
2375 return NULL;
2376}
2377
2378static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2379 void *cookie)
2380{
2381 char *s = nd_get_link(nd);
2382 if (!IS_ERR(s))
2383 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002386static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 .readlink = proc_self_readlink,
2388 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002389 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390};
2391
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002392/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002393 * proc base
2394 *
2395 * These are the directory entries in the root directory of /proc
2396 * that properly belong to the /proc filesystem, as they describe
2397 * describe something that is process related.
2398 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002399static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002400 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002401 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002402};
2403
2404/*
2405 * Exceptional case: normally we are not allowed to unhash a busy
2406 * directory. In this case, however, we can do it - no aliasing problems
2407 * due to the way we treat inodes.
2408 */
2409static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2410{
2411 struct inode *inode = dentry->d_inode;
2412 struct task_struct *task = get_proc_task(inode);
2413 if (task) {
2414 put_task_struct(task);
2415 return 1;
2416 }
2417 d_drop(dentry);
2418 return 0;
2419}
2420
Al Virod72f71e2009-02-20 05:58:47 +00002421static const struct dentry_operations proc_base_dentry_operations =
Eric W. Biederman801199c2006-10-02 02:18:48 -07002422{
2423 .d_revalidate = proc_base_revalidate,
2424 .d_delete = pid_delete_dentry,
2425};
2426
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002427static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002428 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002429{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002430 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002431 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002432 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002433 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002434
2435 /* Allocate the inode */
2436 error = ERR_PTR(-ENOMEM);
2437 inode = new_inode(dir->i_sb);
2438 if (!inode)
2439 goto out;
2440
2441 /* Initialize the inode */
2442 ei = PROC_I(inode);
2443 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002444
2445 /*
2446 * grab the reference to the task.
2447 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002448 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002449 if (!ei->pid)
2450 goto out_iput;
2451
Eric W. Biederman801199c2006-10-02 02:18:48 -07002452 inode->i_mode = p->mode;
2453 if (S_ISDIR(inode->i_mode))
2454 inode->i_nlink = 2;
2455 if (S_ISLNK(inode->i_mode))
2456 inode->i_size = 64;
2457 if (p->iop)
2458 inode->i_op = p->iop;
2459 if (p->fop)
2460 inode->i_fop = p->fop;
2461 ei->op = p->op;
2462 dentry->d_op = &proc_base_dentry_operations;
2463 d_add(dentry, inode);
2464 error = NULL;
2465out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002466 return error;
2467out_iput:
2468 iput(inode);
2469 goto out;
2470}
2471
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002472static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2473{
2474 struct dentry *error;
2475 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002476 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002477
2478 error = ERR_PTR(-ENOENT);
2479
2480 if (!task)
2481 goto out_no_task;
2482
2483 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002484 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2485 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002486 if (p->len != dentry->d_name.len)
2487 continue;
2488 if (!memcmp(dentry->d_name.name, p->name, p->len))
2489 break;
2490 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002491 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002492 goto out;
2493
2494 error = proc_base_instantiate(dir, dentry, task, p);
2495
2496out:
2497 put_task_struct(task);
2498out_no_task:
2499 return error;
2500}
2501
Eric Dumazetc5141e62007-05-08 00:26:15 -07002502static int proc_base_fill_cache(struct file *filp, void *dirent,
2503 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002504{
2505 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2506 proc_base_instantiate, task, p);
2507}
2508
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002509#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002510static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002511{
Andrea Righi940389b2008-07-28 00:48:12 +02002512 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002513 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002514
Andrea Righi59954772008-07-27 17:29:15 +02002515 if (whole && lock_task_sighand(task, &flags)) {
2516 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002517
Andrea Righi59954772008-07-27 17:29:15 +02002518 task_io_accounting_add(&acct, &task->signal->ioac);
2519 while_each_thread(task, t)
2520 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002521
Andrea Righi59954772008-07-27 17:29:15 +02002522 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002523 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002524 return sprintf(buffer,
2525 "rchar: %llu\n"
2526 "wchar: %llu\n"
2527 "syscr: %llu\n"
2528 "syscw: %llu\n"
2529 "read_bytes: %llu\n"
2530 "write_bytes: %llu\n"
2531 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002532 (unsigned long long)acct.rchar,
2533 (unsigned long long)acct.wchar,
2534 (unsigned long long)acct.syscr,
2535 (unsigned long long)acct.syscw,
2536 (unsigned long long)acct.read_bytes,
2537 (unsigned long long)acct.write_bytes,
2538 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002539}
Andrea Righi297c5d92008-07-25 01:48:49 -07002540
2541static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2542{
2543 return do_io_accounting(task, buffer, 0);
2544}
2545
2546static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2547{
2548 return do_io_accounting(task, buffer, 1);
2549}
2550#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002551
Kees Cook47830722008-10-06 03:11:58 +04002552static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2553 struct pid *pid, struct task_struct *task)
2554{
2555 seq_printf(m, "%08x\n", task->personality);
2556 return 0;
2557}
2558
Eric W. Biederman801199c2006-10-02 02:18:48 -07002559/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002560 * Thread groups
2561 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002562static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002563static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002564
Eric Dumazetc5141e62007-05-08 00:26:15 -07002565static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002566 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2567 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2568 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002569#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002570 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002571#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002572 REG("environ", S_IRUSR, proc_environ_operations),
2573 INF("auxv", S_IRUSR, proc_pid_auxv),
2574 ONE("status", S_IRUGO, proc_pid_status),
2575 ONE("personality", S_IRUSR, proc_pid_personality),
2576 INF("limits", S_IRUSR, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002577#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002578 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002579#endif
john stultz4614a696b2009-12-14 18:00:05 -08002580 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002581#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002582 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002583#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002584 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2585 ONE("stat", S_IRUGO, proc_tgid_stat),
2586 ONE("statm", S_IRUGO, proc_pid_statm),
2587 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002588#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002589 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002590#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002591 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2592 LNK("cwd", proc_cwd_link),
2593 LNK("root", proc_root_link),
2594 LNK("exe", proc_exe_link),
2595 REG("mounts", S_IRUGO, proc_mounts_operations),
2596 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2597 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002598#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002599 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2600 REG("smaps", S_IRUGO, proc_smaps_operations),
2601 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002602#endif
2603#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002604 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002605#endif
2606#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002607 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002608#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002609#ifdef CONFIG_STACKTRACE
2610 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002611#endif
2612#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002613 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002614#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002615#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002616 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002617#endif
Paul Menage8793d852007-10-18 23:39:39 -07002618#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002619 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002620#endif
Paul Menagea4243162007-10-18 23:39:35 -07002621#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002622 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002623#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002624 INF("oom_score", S_IRUGO, proc_oom_score),
2625 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002626#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002627 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2628 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002629#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002630#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002631 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002632#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002633#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002634 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002635#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002636#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002637 INF("io", S_IRUGO, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002638#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002639};
2640
2641static int proc_tgid_base_readdir(struct file * filp,
2642 void * dirent, filldir_t filldir)
2643{
2644 return proc_pident_readdir(filp,dirent,filldir,
2645 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2646}
2647
Arjan van de Ven00977a52007-02-12 00:55:34 -08002648static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002649 .read = generic_read_dir,
2650 .readdir = proc_tgid_base_readdir,
2651};
2652
2653static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002654 return proc_pident_lookup(dir, dentry,
2655 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002656}
2657
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002658static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002659 .lookup = proc_tgid_base_lookup,
2660 .getattr = pid_getattr,
2661 .setattr = proc_setattr,
2662};
2663
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002664static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002666 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002667 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002668 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Eric W. Biederman48e64842006-06-26 00:25:48 -07002670 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002671 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2672 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002673 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002674 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002675 d_drop(dentry);
2676 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Eric W. Biederman48e64842006-06-26 00:25:48 -07002679 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002680 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2681 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002682 if (!leader)
2683 goto out;
2684
2685 name.name = "task";
2686 name.len = strlen(name.name);
2687 dir = d_hash_and_lookup(leader, &name);
2688 if (!dir)
2689 goto out_put_leader;
2690
2691 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002692 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002693 dentry = d_hash_and_lookup(dir, &name);
2694 if (dentry) {
2695 shrink_dcache_parent(dentry);
2696 d_drop(dentry);
2697 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002699
2700 dput(dir);
2701out_put_leader:
2702 dput(leader);
2703out:
2704 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
Randy Dunlap0895e912007-10-21 21:00:10 -07002707/**
2708 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2709 * @task: task that should be flushed.
2710 *
2711 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002712 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002713 * in. This call is supposed to do all of this job.
2714 *
2715 * Looks in the dcache for
2716 * /proc/@pid
2717 * /proc/@tgid/task/@pid
2718 * if either directory is present flushes it and all of it'ts children
2719 * from the dcache.
2720 *
2721 * It is safe and reasonable to cache /proc entries for a task until
2722 * that task exits. After that they just clog up the dcache with
2723 * useless entries, possibly causing useful dcache entries to be
2724 * flushed instead. This routine is proved to flush those useless
2725 * dcache entries at process exit time.
2726 *
2727 * NOTE: This routine is just an optimization so it does not guarantee
2728 * that no dcache entries will exist at process exit time it
2729 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002730 */
2731
2732void proc_flush_task(struct task_struct *task)
2733{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002734 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002735 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002736 struct upid *upid;
2737
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002738 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002739 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002740
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002741 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002742 upid = &pid->numbers[i];
2743 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002744 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002745 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002746
2747 upid = &pid->numbers[pid->level];
2748 if (upid->nr == 1)
2749 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002750}
2751
Adrian Bunk9711ef92006-12-06 20:38:31 -08002752static struct dentry *proc_pid_instantiate(struct inode *dir,
2753 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002754 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002755{
2756 struct dentry *error = ERR_PTR(-ENOENT);
2757 struct inode *inode;
2758
Eric W. Biederman61a28782006-10-02 02:18:49 -07002759 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002760 if (!inode)
2761 goto out;
2762
2763 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2764 inode->i_op = &proc_tgid_base_inode_operations;
2765 inode->i_fop = &proc_tgid_base_operations;
2766 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002767
2768 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2769 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002770
2771 dentry->d_op = &pid_dentry_operations;
2772
2773 d_add(dentry, inode);
2774 /* Close the race of the process dying before we return the dentry */
2775 if (pid_revalidate(dentry, NULL))
2776 error = NULL;
2777out:
2778 return error;
2779}
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2782{
Dan Carpenter73d36462010-05-26 14:43:25 -07002783 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002786 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Eric W. Biederman801199c2006-10-02 02:18:48 -07002788 result = proc_base_lookup(dir, dentry);
2789 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2790 goto out;
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 tgid = name_to_int(dentry);
2793 if (tgid == ~0U)
2794 goto out;
2795
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002796 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002797 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002798 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (task)
2800 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002801 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (!task)
2803 goto out;
2804
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002805 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002806 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002808 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809}
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002812 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002813 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002815struct tgid_iter {
2816 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002817 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002818};
2819static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2820{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002821 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002823 if (iter.task)
2824 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002825 rcu_read_lock();
2826retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002827 iter.task = NULL;
2828 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002829 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002830 iter.tgid = pid_nr_ns(pid, ns);
2831 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002832 /* What we to know is if the pid we have find is the
2833 * pid of a thread_group_leader. Testing for task
2834 * being a thread_group_leader is the obvious thing
2835 * todo but there is a window when it fails, due to
2836 * the pid transfer logic in de_thread.
2837 *
2838 * So we perform the straight forward test of seeing
2839 * if the pid we have found is the pid of a thread
2840 * group leader, and don't worry if the task we have
2841 * found doesn't happen to be a thread group leader.
2842 * As we don't care in the case of readdir.
2843 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002844 if (!iter.task || !has_group_leader_pid(iter.task)) {
2845 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002846 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002847 }
2848 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002850 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002851 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002854#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Eric W. Biederman61a28782006-10-02 02:18:49 -07002856static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002857 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002858{
2859 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002860 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002861 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002862 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002863}
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865/* for the /proc/ directory itself, after non-process stuff has been done */
2866int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2867{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002869 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002870 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002871 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Eric W. Biederman61a28782006-10-02 02:18:49 -07002873 if (!reaper)
2874 goto out_no_task;
2875
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002876 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002877 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002878 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002879 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
2881
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002882 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002883 iter.task = NULL;
2884 iter.tgid = filp->f_pos - TGID_OFFSET;
2885 for (iter = next_tgid(ns, iter);
2886 iter.task;
2887 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2888 filp->f_pos = iter.tgid + TGID_OFFSET;
2889 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2890 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002894 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2895out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002896 put_task_struct(reaper);
2897out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return 0;
2899}
2900
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002901/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002902 * Tasks
2903 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002904static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002905 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002906 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002907 REG("environ", S_IRUSR, proc_environ_operations),
2908 INF("auxv", S_IRUSR, proc_pid_auxv),
2909 ONE("status", S_IRUGO, proc_pid_status),
2910 ONE("personality", S_IRUSR, proc_pid_personality),
2911 INF("limits", S_IRUSR, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002912#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002913 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002914#endif
john stultz4614a696b2009-12-14 18:00:05 -08002915 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002916#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002917 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002918#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002919 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2920 ONE("stat", S_IRUGO, proc_tid_stat),
2921 ONE("statm", S_IRUGO, proc_pid_statm),
2922 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002923#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002924 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002925#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002926 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2927 LNK("cwd", proc_cwd_link),
2928 LNK("root", proc_root_link),
2929 LNK("exe", proc_exe_link),
2930 REG("mounts", S_IRUGO, proc_mounts_operations),
2931 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002932#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002933 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2934 REG("smaps", S_IRUGO, proc_smaps_operations),
2935 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002936#endif
2937#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002938 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939#endif
2940#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002941 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002942#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002943#ifdef CONFIG_STACKTRACE
2944 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002945#endif
2946#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002948#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002949#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002950 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002951#endif
Paul Menage8793d852007-10-18 23:39:39 -07002952#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002953 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954#endif
Paul Menagea4243162007-10-18 23:39:35 -07002955#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002956 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002957#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002958 INF("oom_score", S_IRUGO, proc_oom_score),
2959 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002960#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002961 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2962 REG("sessionid", S_IRUSR, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002963#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002964#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002965 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002966#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002967#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002968 INF("io", S_IRUGO, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002969#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002970};
2971
2972static int proc_tid_base_readdir(struct file * filp,
2973 void * dirent, filldir_t filldir)
2974{
2975 return proc_pident_readdir(filp,dirent,filldir,
2976 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2977}
2978
2979static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002980 return proc_pident_lookup(dir, dentry,
2981 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002982}
2983
Arjan van de Ven00977a52007-02-12 00:55:34 -08002984static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002985 .read = generic_read_dir,
2986 .readdir = proc_tid_base_readdir,
2987};
2988
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002989static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002990 .lookup = proc_tid_base_lookup,
2991 .getattr = pid_getattr,
2992 .setattr = proc_setattr,
2993};
2994
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002995static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002996 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002997{
2998 struct dentry *error = ERR_PTR(-ENOENT);
2999 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003000 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003001
3002 if (!inode)
3003 goto out;
3004 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3005 inode->i_op = &proc_tid_base_inode_operations;
3006 inode->i_fop = &proc_tid_base_operations;
3007 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003008
3009 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3010 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003011
3012 dentry->d_op = &pid_dentry_operations;
3013
3014 d_add(dentry, inode);
3015 /* Close the race of the process dying before we return the dentry */
3016 if (pid_revalidate(dentry, NULL))
3017 error = NULL;
3018out:
3019 return error;
3020}
3021
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003022static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3023{
3024 struct dentry *result = ERR_PTR(-ENOENT);
3025 struct task_struct *task;
3026 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003027 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003028 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003029
3030 if (!leader)
3031 goto out_no_task;
3032
3033 tid = name_to_int(dentry);
3034 if (tid == ~0U)
3035 goto out;
3036
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003037 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003038 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003039 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003040 if (task)
3041 get_task_struct(task);
3042 rcu_read_unlock();
3043 if (!task)
3044 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003045 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003046 goto out_drop_task;
3047
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003048 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003049out_drop_task:
3050 put_task_struct(task);
3051out:
3052 put_task_struct(leader);
3053out_no_task:
3054 return result;
3055}
3056
3057/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003058 * Find the first tid of a thread group to return to user space.
3059 *
3060 * Usually this is just the thread group leader, but if the users
3061 * buffer was too small or there was a seek into the middle of the
3062 * directory we have more work todo.
3063 *
3064 * In the case of a short read we start with find_task_by_pid.
3065 *
3066 * In the case of a seek we start with the leader and walk nr
3067 * threads past it.
3068 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003069static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003070 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003071{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003072 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003073
Eric W. Biedermancc288732006-06-26 00:26:01 -07003074 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003075 /* Attempt to start with the pid of a thread */
3076 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003077 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003078 if (pos && (pos->group_leader == leader))
3079 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003080 }
3081
3082 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003083 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003084 if (nr && nr >= get_nr_threads(leader))
3085 goto out;
3086
3087 /* If we haven't found our starting place yet start
3088 * with the leader and walk nr threads forward.
3089 */
3090 for (pos = leader; nr > 0; --nr) {
3091 pos = next_thread(pos);
3092 if (pos == leader) {
3093 pos = NULL;
3094 goto out;
3095 }
3096 }
3097found:
3098 get_task_struct(pos);
3099out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003100 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003101 return pos;
3102}
3103
3104/*
3105 * Find the next thread in the thread list.
3106 * Return NULL if there is an error or no next thread.
3107 *
3108 * The reference to the input task_struct is released.
3109 */
3110static struct task_struct *next_tid(struct task_struct *start)
3111{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003112 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003113 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003114 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003115 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003116 if (thread_group_leader(pos))
3117 pos = NULL;
3118 else
3119 get_task_struct(pos);
3120 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003121 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003122 put_task_struct(start);
3123 return pos;
3124}
3125
Eric W. Biederman61a28782006-10-02 02:18:49 -07003126static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3127 struct task_struct *task, int tid)
3128{
3129 char name[PROC_NUMBUF];
3130 int len = snprintf(name, sizeof(name), "%d", tid);
3131 return proc_fill_cache(filp, dirent, filldir, name, len,
3132 proc_task_instantiate, task, NULL);
3133}
3134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135/* for the /proc/TGID/task/ directories */
3136static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3137{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003138 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003140 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003141 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 int retval = -ENOENT;
3143 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003144 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003145 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003147 task = get_proc_task(inode);
3148 if (!task)
3149 goto out_no_task;
3150 rcu_read_lock();
3151 if (pid_alive(task)) {
3152 leader = task->group_leader;
3153 get_task_struct(leader);
3154 }
3155 rcu_read_unlock();
3156 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003157 if (!leader)
3158 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 retval = 0;
3160
Linus Torvaldsee568b22009-03-17 10:02:35 -07003161 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 case 0:
3163 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003164 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003166 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /* fall through */
3168 case 1:
3169 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003170 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003172 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /* fall through */
3174 }
3175
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003176 /* f_version caches the tgid value that the last readdir call couldn't
3177 * return. lseek aka telldir automagically resets f_version to 0.
3178 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003179 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003180 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003181 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003182 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003183 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003184 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003185 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003186 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003187 /* returning this tgid failed, save it as the first
3188 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003189 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003190 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
3194out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003195 put_task_struct(leader);
3196out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return retval;
3198}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003199
3200static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3201{
3202 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003203 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003204 generic_fillattr(inode, stat);
3205
Eric W. Biederman99f89552006-06-26 00:25:55 -07003206 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003207 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003208 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003209 }
3210
3211 return 0;
3212}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003213
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003214static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003215 .lookup = proc_task_lookup,
3216 .getattr = proc_task_getattr,
3217 .setattr = proc_setattr,
3218};
3219
Arjan van de Ven00977a52007-02-12 00:55:34 -08003220static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003221 .read = generic_read_dir,
3222 .readdir = proc_task_readdir,
3223};