blob: 93f1cdd5d3d7156b02a42359fb5999232020765c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070076#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/cpuset.h>
78#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050079#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070080#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070081#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070082#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070083#include <linux/pid_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040084#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090085#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include "internal.h"
87
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070088/* NOTE:
89 * Implementing inode permission operations in /proc is almost
90 * certainly an error. Permission checks need to happen during
91 * each system call not at open time. The reason is that most of
92 * what we wish to check for permissions in /proc varies at runtime.
93 *
94 * The classic example of a problem is opening file descriptors
95 * in /proc for a task before it execs a suid executable.
96 */
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700100 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800102 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800103 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Eric W. Biederman61a28782006-10-02 02:18:49 -0700107#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700108 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700109 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700110 .mode = MODE, \
111 .iop = IOP, \
112 .fop = FOP, \
113 .op = OP, \
114}
115
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300116#define DIR(NAME, MODE, iops, fops) \
117 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
118#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300121 { .proc_get_link = get_link } )
122#define REG(NAME, MODE, fops) \
123 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
124#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700125 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700126 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300127 { .proc_read = read } )
128#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800129 NOD(NAME, (S_IFREG|(MODE)), \
130 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300131 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Vegard Nossumaed54172008-06-05 22:46:53 -0700133/*
134 * Count the number of hardlinks for the pid_entry table, excluding the .
135 * and .. links.
136 */
137static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
138 unsigned int n)
139{
140 unsigned int i;
141 unsigned int count;
142
143 count = 0;
144 for (i = 0; i < n; ++i) {
145 if (S_ISDIR(entries[i].mode))
146 ++count;
147 }
148
149 return count;
150}
151
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200152static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000154 int result = -ENOENT;
155
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700156 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200157 if (task->fs) {
158 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000159 result = 0;
160 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700161 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000162 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700163}
164
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800165static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700166{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700167 struct task_struct *task = get_proc_task(inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700169
170 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200171 task_lock(task);
172 if (task->fs) {
173 get_fs_pwd(task->fs, path);
174 result = 0;
175 }
176 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177 put_task_struct(task);
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return result;
180}
181
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800182static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700184 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700186
187 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200188 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700189 put_task_struct(task);
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return result;
192}
193
Roland McGrath638fa202008-04-29 01:01:38 -0700194/*
195 * Return zero if current may access user memory in @task, -error if not.
196 */
197static int check_mem_permission(struct task_struct *task)
198{
199 /*
200 * A task can always look at itself, in case it chooses
201 * to use system calls instead of load instructions.
202 */
203 if (task == current)
204 return 0;
205
206 /*
207 * If current is actively ptrace'ing, and would also be
208 * permitted to freshly attach with ptrace now, permit it.
209 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700210 if (task_is_stopped_or_traced(task)) {
211 int match;
212 rcu_read_lock();
213 match = (tracehook_tracer_task(task) == current);
214 rcu_read_unlock();
215 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
216 return 0;
217 }
Roland McGrath638fa202008-04-29 01:01:38 -0700218
219 /*
220 * Noone else is allowed.
221 */
222 return -EPERM;
223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Al Viro831830b2008-01-02 14:09:57 +0000225struct mm_struct *mm_for_maps(struct task_struct *task)
226{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200227 struct mm_struct *mm;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200228
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700229 if (mutex_lock_killable(&task->signal->cred_guard_mutex))
Oleg Nesterov704b8362009-07-10 03:27:40 +0200230 return NULL;
231
232 mm = get_task_mm(task);
233 if (mm && mm != current->mm &&
234 !ptrace_may_access(task, PTRACE_MODE_READ)) {
235 mmput(mm);
236 mm = NULL;
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200237 }
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700238 mutex_unlock(&task->signal->cred_guard_mutex);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200239
Al Viro831830b2008-01-02 14:09:57 +0000240 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static int proc_pid_cmdline(struct task_struct *task, char * buffer)
244{
245 int res = 0;
246 unsigned int len;
247 struct mm_struct *mm = get_task_mm(task);
248 if (!mm)
249 goto out;
250 if (!mm->arg_end)
251 goto out_mm; /* Shh! No looking before we're done */
252
253 len = mm->arg_end - mm->arg_start;
254
255 if (len > PAGE_SIZE)
256 len = PAGE_SIZE;
257
258 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
259
260 // If the nul at the end of args has been overwritten, then
261 // assume application is using setproctitle(3).
262 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
263 len = strnlen(buffer, res);
264 if (len < res) {
265 res = len;
266 } else {
267 len = mm->env_end - mm->env_start;
268 if (len > PAGE_SIZE - res)
269 len = PAGE_SIZE - res;
270 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
271 res = strnlen(buffer, res);
272 }
273 }
274out_mm:
275 mmput(mm);
276out:
277 return res;
278}
279
280static int proc_pid_auxv(struct task_struct *task, char *buffer)
281{
282 int res = 0;
283 struct mm_struct *mm = get_task_mm(task);
284 if (mm) {
285 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300286 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300288 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 res = nwords * sizeof(mm->saved_auxv[0]);
290 if (res > PAGE_SIZE)
291 res = PAGE_SIZE;
292 memcpy(buffer, mm->saved_auxv, res);
293 mmput(mm);
294 }
295 return res;
296}
297
298
299#ifdef CONFIG_KALLSYMS
300/*
301 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
302 * Returns the resolved symbol. If that fails, simply return the address.
303 */
304static int proc_pid_wchan(struct task_struct *task, char *buffer)
305{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700306 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700307 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 wchan = get_wchan(task);
310
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700311 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600312 if (!ptrace_may_access(task, PTRACE_MODE_READ))
313 return 0;
314 else
315 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700316 else
317 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319#endif /* CONFIG_KALLSYMS */
320
Ken Chen2ec220e2008-11-10 11:26:08 +0300321#ifdef CONFIG_STACKTRACE
322
323#define MAX_STACK_TRACE_DEPTH 64
324
325static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
326 struct pid *pid, struct task_struct *task)
327{
328 struct stack_trace trace;
329 unsigned long *entries;
330 int i;
331
332 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
333 if (!entries)
334 return -ENOMEM;
335
336 trace.nr_entries = 0;
337 trace.max_entries = MAX_STACK_TRACE_DEPTH;
338 trace.entries = entries;
339 trace.skip = 0;
340 save_stack_trace_tsk(task, &trace);
341
342 for (i = 0; i < trace.nr_entries; i++) {
343 seq_printf(m, "[<%p>] %pS\n",
344 (void *)entries[i], (void *)entries[i]);
345 }
346 kfree(entries);
347
348 return 0;
349}
350#endif
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#ifdef CONFIG_SCHEDSTATS
353/*
354 * Provides /proc/PID/schedstat
355 */
356static int proc_pid_schedstat(struct task_struct *task, char *buffer)
357{
Balbir Singh172ba842007-07-09 18:52:00 +0200358 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100359 (unsigned long long)task->se.sum_exec_runtime,
360 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200361 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363#endif
364
Arjan van de Ven97455122008-01-25 21:08:34 +0100365#ifdef CONFIG_LATENCYTOP
366static int lstats_show_proc(struct seq_file *m, void *v)
367{
368 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800369 struct inode *inode = m->private;
370 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100371
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800372 if (!task)
373 return -ESRCH;
374 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100375 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800376 struct latency_record *lr = &task->latency_record[i];
377 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100378 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800379 seq_printf(m, "%i %li %li",
380 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100381 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800382 unsigned long bt = lr->backtrace[q];
383 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100384 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800385 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100386 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800387 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100388 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800389 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100390 }
391
392 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800393 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100394 return 0;
395}
396
397static int lstats_open(struct inode *inode, struct file *file)
398{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800399 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800400}
401
Arjan van de Ven97455122008-01-25 21:08:34 +0100402static ssize_t lstats_write(struct file *file, const char __user *buf,
403 size_t count, loff_t *offs)
404{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800405 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100406
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800407 if (!task)
408 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100409 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800410 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100411
412 return count;
413}
414
415static const struct file_operations proc_lstats_operations = {
416 .open = lstats_open,
417 .read = seq_read,
418 .write = lstats_write,
419 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800420 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100421};
422
423#endif
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static int proc_oom_score(struct task_struct *task, char *buffer)
426{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200427 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700429 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200430 if (pid_alive(task))
David Rientjesa63d83f2010-08-09 17:19:46 -0700431 points = oom_badness(task, NULL, NULL,
432 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700433 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return sprintf(buffer, "%lu\n", points);
435}
436
Neil Hormand85f50d2007-10-18 23:40:37 -0700437struct limit_names {
438 char *name;
439 char *unit;
440};
441
442static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700443 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700444 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
445 [RLIMIT_DATA] = {"Max data size", "bytes"},
446 [RLIMIT_STACK] = {"Max stack size", "bytes"},
447 [RLIMIT_CORE] = {"Max core file size", "bytes"},
448 [RLIMIT_RSS] = {"Max resident set", "bytes"},
449 [RLIMIT_NPROC] = {"Max processes", "processes"},
450 [RLIMIT_NOFILE] = {"Max open files", "files"},
451 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
452 [RLIMIT_AS] = {"Max address space", "bytes"},
453 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
454 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
455 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
456 [RLIMIT_NICE] = {"Max nice priority", NULL},
457 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800458 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700459};
460
461/* Display limits for a process */
462static int proc_pid_limits(struct task_struct *task, char *buffer)
463{
464 unsigned int i;
465 int count = 0;
466 unsigned long flags;
467 char *bufptr = buffer;
468
469 struct rlimit rlim[RLIM_NLIMITS];
470
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400471 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700472 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700473 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
474 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700475
476 /*
477 * print the file header
478 */
479 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
480 "Limit", "Soft Limit", "Hard Limit", "Units");
481
482 for (i = 0; i < RLIM_NLIMITS; i++) {
483 if (rlim[i].rlim_cur == RLIM_INFINITY)
484 count += sprintf(&bufptr[count], "%-25s %-20s ",
485 lnames[i].name, "unlimited");
486 else
487 count += sprintf(&bufptr[count], "%-25s %-20lu ",
488 lnames[i].name, rlim[i].rlim_cur);
489
490 if (rlim[i].rlim_max == RLIM_INFINITY)
491 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
492 else
493 count += sprintf(&bufptr[count], "%-20lu ",
494 rlim[i].rlim_max);
495
496 if (lnames[i].unit)
497 count += sprintf(&bufptr[count], "%-10s\n",
498 lnames[i].unit);
499 else
500 count += sprintf(&bufptr[count], "\n");
501 }
502
503 return count;
504}
505
Roland McGrathebcb6732008-07-25 19:46:00 -0700506#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
507static int proc_pid_syscall(struct task_struct *task, char *buffer)
508{
509 long nr;
510 unsigned long args[6], sp, pc;
511
512 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
513 return sprintf(buffer, "running\n");
514
515 if (nr < 0)
516 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
517
518 return sprintf(buffer,
519 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
520 nr,
521 args[0], args[1], args[2], args[3], args[4], args[5],
522 sp, pc);
523}
524#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/************************************************************************/
527/* Here the fs part begins */
528/************************************************************************/
529
530/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700531static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700533 struct task_struct *task;
534 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700535 /* Allow access to a task's file descriptors if it is us or we
536 * may use ptrace attach to the process and find out that
537 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700538 */
539 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700540 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400541 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700542 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700543 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700544 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700547static int proc_setattr(struct dentry *dentry, struct iattr *attr)
548{
549 int error;
550 struct inode *inode = dentry->d_inode;
551
552 if (attr->ia_valid & ATTR_MODE)
553 return -EPERM;
554
555 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200556 if (error)
557 return error;
558
559 if ((attr->ia_valid & ATTR_SIZE) &&
560 attr->ia_size != i_size_read(inode)) {
561 error = vmtruncate(inode, attr->ia_size);
562 if (error)
563 return error;
564 }
565
566 setattr_copy(inode, attr);
567 mark_inode_dirty(inode);
568 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700569}
570
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800571static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700572 .setattr = proc_setattr,
573};
574
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100575static int mounts_open_common(struct inode *inode, struct file *file,
576 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700578 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700579 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800580 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100581 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500582 struct proc_mounts *p;
583 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Eric W. Biederman99f89552006-06-26 00:25:55 -0700585 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700586 rcu_read_lock();
587 nsp = task_nsproxy(task);
588 if (nsp) {
589 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800590 if (ns)
591 get_mnt_ns(ns);
592 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700593 rcu_read_unlock();
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200594 if (ns && get_task_root(task, &root) == 0)
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000595 ret = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700596 put_task_struct(task);
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100599 if (!ns)
600 goto err;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000601 if (ret)
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100602 goto err_put_ns;
603
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100604 ret = -ENOMEM;
605 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
606 if (!p)
607 goto err_put_path;
608
609 file->private_data = &p->m;
610 ret = seq_open(file, op);
611 if (ret)
612 goto err_free;
613
614 p->m.private = p;
615 p->ns = ns;
616 p->root = root;
617 p->event = ns->event;
618
619 return 0;
620
621 err_free:
622 kfree(p);
623 err_put_path:
624 path_put(&root);
625 err_put_ns:
626 put_mnt_ns(ns);
627 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return ret;
629}
630
631static int mounts_release(struct inode *inode, struct file *file)
632{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100633 struct proc_mounts *p = file->private_data;
634 path_put(&p->root);
635 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return seq_release(inode, file);
637}
638
Al Viro5addc5d2005-11-07 17:15:49 -0500639static unsigned mounts_poll(struct file *file, poll_table *wait)
640{
641 struct proc_mounts *p = file->private_data;
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900642 unsigned res = POLLIN | POLLRDNORM;
Al Viro5addc5d2005-11-07 17:15:49 -0500643
Al Viro9f5596a2010-02-05 00:40:25 -0500644 poll_wait(file, &p->ns->poll, wait);
645 if (mnt_had_events(p))
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900646 res |= POLLERR | POLLPRI;
Al Viro5addc5d2005-11-07 17:15:49 -0500647
648 return res;
649}
650
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100651static int mounts_open(struct inode *inode, struct file *file)
652{
653 return mounts_open_common(inode, file, &mounts_op);
654}
655
Arjan van de Ven00977a52007-02-12 00:55:34 -0800656static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 .open = mounts_open,
658 .read = seq_read,
659 .llseek = seq_lseek,
660 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500661 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662};
663
Ram Pai2d4d4862008-03-27 13:06:25 +0100664static int mountinfo_open(struct inode *inode, struct file *file)
665{
666 return mounts_open_common(inode, file, &mountinfo_op);
667}
668
669static const struct file_operations proc_mountinfo_operations = {
670 .open = mountinfo_open,
671 .read = seq_read,
672 .llseek = seq_lseek,
673 .release = mounts_release,
674 .poll = mounts_poll,
675};
676
Chuck Leverb4629fe2006-03-20 13:44:12 -0500677static int mountstats_open(struct inode *inode, struct file *file)
678{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100679 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500680}
681
Arjan van de Ven00977a52007-02-12 00:55:34 -0800682static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500683 .open = mountstats_open,
684 .read = seq_read,
685 .llseek = seq_lseek,
686 .release = mounts_release,
687};
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
690
691static ssize_t proc_info_read(struct file * file, char __user * buf,
692 size_t count, loff_t *ppos)
693{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800694 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 unsigned long page;
696 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700697 struct task_struct *task = get_proc_task(inode);
698
699 length = -ESRCH;
700 if (!task)
701 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (count > PROC_BLOCK_SIZE)
704 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700705
706 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700707 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 length = PROC_I(inode)->op.proc_read(task, (char*)page);
711
712 if (length >= 0)
713 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
714 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700715out:
716 put_task_struct(task);
717out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return length;
719}
720
Arjan van de Ven00977a52007-02-12 00:55:34 -0800721static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100723 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724};
725
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800726static int proc_single_show(struct seq_file *m, void *v)
727{
728 struct inode *inode = m->private;
729 struct pid_namespace *ns;
730 struct pid *pid;
731 struct task_struct *task;
732 int ret;
733
734 ns = inode->i_sb->s_fs_info;
735 pid = proc_pid(inode);
736 task = get_pid_task(pid, PIDTYPE_PID);
737 if (!task)
738 return -ESRCH;
739
740 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
741
742 put_task_struct(task);
743 return ret;
744}
745
746static int proc_single_open(struct inode *inode, struct file *filp)
747{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800748 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800749}
750
751static const struct file_operations proc_single_file_operations = {
752 .open = proc_single_open,
753 .read = seq_read,
754 .llseek = seq_lseek,
755 .release = single_release,
756};
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758static int mem_open(struct inode* inode, struct file* file)
759{
760 file->private_data = (void*)((long)current->self_exec_id);
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700761 /* OK to pass negative loff_t, we can catch out-of-range */
762 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 0;
764}
765
766static ssize_t mem_read(struct file * file, char __user * buf,
767 size_t count, loff_t *ppos)
768{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800769 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 char *page;
771 unsigned long src = *ppos;
772 int ret = -ESRCH;
773 struct mm_struct *mm;
774
Eric W. Biederman99f89552006-06-26 00:25:55 -0700775 if (!task)
776 goto out_no_task;
777
Roland McGrath638fa202008-04-29 01:01:38 -0700778 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto out;
780
781 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700782 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!page)
784 goto out;
785
786 ret = 0;
787
788 mm = get_task_mm(task);
789 if (!mm)
790 goto out_free;
791
792 ret = -EIO;
793
794 if (file->private_data != (void*)((long)current->self_exec_id))
795 goto out_put;
796
797 ret = 0;
798
799 while (count > 0) {
800 int this_len, retval;
801
802 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
803 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa202008-04-29 01:01:38 -0700804 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!ret)
806 ret = -EIO;
807 break;
808 }
809
810 if (copy_to_user(buf, page, retval)) {
811 ret = -EFAULT;
812 break;
813 }
814
815 ret += retval;
816 src += retval;
817 buf += retval;
818 count -= retval;
819 }
820 *ppos = src;
821
822out_put:
823 mmput(mm);
824out_free:
825 free_page((unsigned long) page);
826out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700827 put_task_struct(task);
828out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return ret;
830}
831
832#define mem_write NULL
833
834#ifndef mem_write
835/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800836static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 size_t count, loff_t *ppos)
838{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700839 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800841 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 unsigned long dst = *ppos;
843
Eric W. Biederman99f89552006-06-26 00:25:55 -0700844 copied = -ESRCH;
845 if (!task)
846 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Roland McGrath638fa202008-04-29 01:01:38 -0700848 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700849 goto out;
850
851 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700852 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700856 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 while (count > 0) {
858 int this_len, retval;
859
860 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
861 if (copy_from_user(page, buf, this_len)) {
862 copied = -EFAULT;
863 break;
864 }
865 retval = access_process_vm(task, dst, page, this_len, 1);
866 if (!retval) {
867 if (!copied)
868 copied = -EIO;
869 break;
870 }
871 copied += retval;
872 buf += retval;
873 dst += retval;
874 count -= retval;
875 }
876 *ppos = dst;
877 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700878out:
879 put_task_struct(task);
880out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return copied;
882}
883#endif
884
Matt Mackall85863e42008-02-04 22:29:04 -0800885loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 switch (orig) {
888 case 0:
889 file->f_pos = offset;
890 break;
891 case 1:
892 file->f_pos += offset;
893 break;
894 default:
895 return -EINVAL;
896 }
897 force_successful_syscall_return();
898 return file->f_pos;
899}
900
Arjan van de Ven00977a52007-02-12 00:55:34 -0800901static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 .llseek = mem_lseek,
903 .read = mem_read,
904 .write = mem_write,
905 .open = mem_open,
906};
907
James Pearson315e28c2007-10-16 23:30:17 -0700908static ssize_t environ_read(struct file *file, char __user *buf,
909 size_t count, loff_t *ppos)
910{
911 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
912 char *page;
913 unsigned long src = *ppos;
914 int ret = -ESRCH;
915 struct mm_struct *mm;
916
917 if (!task)
918 goto out_no_task;
919
Stephen Smalley006ebb42008-05-19 08:32:49 -0400920 if (!ptrace_may_access(task, PTRACE_MODE_READ))
James Pearson315e28c2007-10-16 23:30:17 -0700921 goto out;
922
923 ret = -ENOMEM;
924 page = (char *)__get_free_page(GFP_TEMPORARY);
925 if (!page)
926 goto out;
927
928 ret = 0;
929
930 mm = get_task_mm(task);
931 if (!mm)
932 goto out_free;
933
934 while (count > 0) {
935 int this_len, retval, max_len;
936
937 this_len = mm->env_end - (mm->env_start + src);
938
939 if (this_len <= 0)
940 break;
941
942 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
943 this_len = (this_len > max_len) ? max_len : this_len;
944
945 retval = access_process_vm(task, (mm->env_start + src),
946 page, this_len, 0);
947
948 if (retval <= 0) {
949 ret = retval;
950 break;
951 }
952
953 if (copy_to_user(buf, page, retval)) {
954 ret = -EFAULT;
955 break;
956 }
957
958 ret += retval;
959 src += retval;
960 buf += retval;
961 count -= retval;
962 }
963 *ppos = src;
964
965 mmput(mm);
966out_free:
967 free_page((unsigned long) page);
968out:
969 put_task_struct(task);
970out_no_task:
971 return ret;
972}
973
974static const struct file_operations proc_environ_operations = {
975 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100976 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700977};
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979static ssize_t oom_adjust_read(struct file *file, char __user *buf,
980 size_t count, loff_t *ppos)
981{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800982 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700983 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700985 int oom_adjust = OOM_DISABLE;
986 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Eric W. Biederman99f89552006-06-26 00:25:55 -0700988 if (!task)
989 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700990
991 if (lock_task_sighand(task, &flags)) {
992 oom_adjust = task->signal->oom_adj;
993 unlock_task_sighand(task, &flags);
994 }
995
Eric W. Biederman99f89552006-06-26 00:25:55 -0700996 put_task_struct(task);
997
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700998 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700999
1000 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1004 size_t count, loff_t *ppos)
1005{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001006 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001007 char buffer[PROC_NUMBUF];
1008 long oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001009 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001010 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001012 memset(buffer, 0, sizeof(buffer));
1013 if (count > sizeof(buffer) - 1)
1014 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001015 if (copy_from_user(buffer, buf, count)) {
1016 err = -EFAULT;
1017 goto out;
1018 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001019
1020 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1021 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001022 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001023 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -07001024 oom_adjust != OOM_DISABLE) {
1025 err = -EINVAL;
1026 goto out;
1027 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001028
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001029 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001030 if (!task) {
1031 err = -ESRCH;
1032 goto out;
1033 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001034
Ying Han3d5992d2010-10-26 14:21:23 -07001035 task_lock(task);
1036 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001037 err = -EINVAL;
1038 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001039 }
1040
David Rientjesd19d5472010-10-26 14:21:26 -07001041 if (!lock_task_sighand(task, &flags)) {
1042 err = -ESRCH;
1043 goto err_task_lock;
1044 }
1045
1046 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1047 err = -EACCES;
1048 goto err_sighand;
1049 }
1050
Ying Han3d5992d2010-10-26 14:21:23 -07001051 if (oom_adjust != task->signal->oom_adj) {
1052 if (oom_adjust == OOM_DISABLE)
1053 atomic_inc(&task->mm->oom_disable_count);
1054 if (task->signal->oom_adj == OOM_DISABLE)
1055 atomic_dec(&task->mm->oom_disable_count);
1056 }
1057
David Rientjes51b1bd22010-08-09 17:19:47 -07001058 /*
1059 * Warn that /proc/pid/oom_adj is deprecated, see
1060 * Documentation/feature-removal-schedule.txt.
1061 */
1062 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1063 "please use /proc/%d/oom_score_adj instead.\n",
1064 current->comm, task_pid_nr(current),
1065 task_pid_nr(task), task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001066 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -07001067 /*
1068 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1069 * value is always attainable.
1070 */
1071 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1072 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1073 else
1074 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1075 -OOM_DISABLE;
David Rientjes723548b2010-10-26 14:21:25 -07001076err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001077 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001078err_task_lock:
1079 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001080 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001081out:
1082 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Arjan van de Ven00977a52007-02-12 00:55:34 -08001085static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 .read = oom_adjust_read,
1087 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001088 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089};
1090
David Rientjesa63d83f2010-08-09 17:19:46 -07001091static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1092 size_t count, loff_t *ppos)
1093{
1094 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1095 char buffer[PROC_NUMBUF];
1096 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1097 unsigned long flags;
1098 size_t len;
1099
1100 if (!task)
1101 return -ESRCH;
1102 if (lock_task_sighand(task, &flags)) {
1103 oom_score_adj = task->signal->oom_score_adj;
1104 unlock_task_sighand(task, &flags);
1105 }
1106 put_task_struct(task);
1107 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1108 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1109}
1110
1111static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1112 size_t count, loff_t *ppos)
1113{
1114 struct task_struct *task;
1115 char buffer[PROC_NUMBUF];
1116 unsigned long flags;
1117 long oom_score_adj;
1118 int err;
1119
1120 memset(buffer, 0, sizeof(buffer));
1121 if (count > sizeof(buffer) - 1)
1122 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001123 if (copy_from_user(buffer, buf, count)) {
1124 err = -EFAULT;
1125 goto out;
1126 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001127
1128 err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1129 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001130 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001131 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001132 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1133 err = -EINVAL;
1134 goto out;
1135 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001136
1137 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001138 if (!task) {
1139 err = -ESRCH;
1140 goto out;
1141 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001142
Ying Han3d5992d2010-10-26 14:21:23 -07001143 task_lock(task);
1144 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001145 err = -EINVAL;
1146 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001147 }
David Rientjesd19d5472010-10-26 14:21:26 -07001148
1149 if (!lock_task_sighand(task, &flags)) {
1150 err = -ESRCH;
1151 goto err_task_lock;
1152 }
1153
1154 if (oom_score_adj < task->signal->oom_score_adj &&
1155 !capable(CAP_SYS_RESOURCE)) {
1156 err = -EACCES;
1157 goto err_sighand;
1158 }
1159
Ying Han3d5992d2010-10-26 14:21:23 -07001160 if (oom_score_adj != task->signal->oom_score_adj) {
1161 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1162 atomic_inc(&task->mm->oom_disable_count);
1163 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1164 atomic_dec(&task->mm->oom_disable_count);
1165 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001166 task->signal->oom_score_adj = oom_score_adj;
1167 /*
1168 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1169 * always attainable.
1170 */
1171 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1172 task->signal->oom_adj = OOM_DISABLE;
1173 else
1174 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1175 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001176err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001177 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001178err_task_lock:
1179 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001180 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001181out:
1182 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001183}
1184
1185static const struct file_operations proc_oom_score_adj_operations = {
1186 .read = oom_score_adj_read,
1187 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001188 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001189};
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191#ifdef CONFIG_AUDITSYSCALL
1192#define TMPBUFLEN 21
1193static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1194 size_t count, loff_t *ppos)
1195{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001196 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001197 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ssize_t length;
1199 char tmpbuf[TMPBUFLEN];
1200
Eric W. Biederman99f89552006-06-26 00:25:55 -07001201 if (!task)
1202 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001204 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001205 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1207}
1208
1209static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1210 size_t count, loff_t *ppos)
1211{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001212 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 char *page, *tmp;
1214 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 uid_t loginuid;
1216
1217 if (!capable(CAP_AUDIT_CONTROL))
1218 return -EPERM;
1219
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001220 rcu_read_lock();
1221 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1222 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001224 }
1225 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Al Viroe0182902006-05-18 08:28:02 -04001227 if (count >= PAGE_SIZE)
1228 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 if (*ppos != 0) {
1231 /* No partial writes. */
1232 return -EINVAL;
1233 }
Mel Gormane12ba742007-10-16 01:25:52 -07001234 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (!page)
1236 return -ENOMEM;
1237 length = -EFAULT;
1238 if (copy_from_user(page, buf, count))
1239 goto out_free_page;
1240
Al Viroe0182902006-05-18 08:28:02 -04001241 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 loginuid = simple_strtoul(page, &tmp, 10);
1243 if (tmp == page) {
1244 length = -EINVAL;
1245 goto out_free_page;
1246
1247 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001248 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (likely(length == 0))
1250 length = count;
1251
1252out_free_page:
1253 free_page((unsigned long) page);
1254 return length;
1255}
1256
Arjan van de Ven00977a52007-02-12 00:55:34 -08001257static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 .read = proc_loginuid_read,
1259 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001260 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261};
Eric Paris1e0bd752008-03-13 08:15:31 -04001262
1263static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1264 size_t count, loff_t *ppos)
1265{
1266 struct inode * inode = file->f_path.dentry->d_inode;
1267 struct task_struct *task = get_proc_task(inode);
1268 ssize_t length;
1269 char tmpbuf[TMPBUFLEN];
1270
1271 if (!task)
1272 return -ESRCH;
1273 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1274 audit_get_sessionid(task));
1275 put_task_struct(task);
1276 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1277}
1278
1279static const struct file_operations proc_sessionid_operations = {
1280 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001281 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001282};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283#endif
1284
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001285#ifdef CONFIG_FAULT_INJECTION
1286static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1287 size_t count, loff_t *ppos)
1288{
1289 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1290 char buffer[PROC_NUMBUF];
1291 size_t len;
1292 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001293
1294 if (!task)
1295 return -ESRCH;
1296 make_it_fail = task->make_it_fail;
1297 put_task_struct(task);
1298
1299 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001300
1301 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001302}
1303
1304static ssize_t proc_fault_inject_write(struct file * file,
1305 const char __user * buf, size_t count, loff_t *ppos)
1306{
1307 struct task_struct *task;
1308 char buffer[PROC_NUMBUF], *end;
1309 int make_it_fail;
1310
1311 if (!capable(CAP_SYS_RESOURCE))
1312 return -EPERM;
1313 memset(buffer, 0, sizeof(buffer));
1314 if (count > sizeof(buffer) - 1)
1315 count = sizeof(buffer) - 1;
1316 if (copy_from_user(buffer, buf, count))
1317 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001318 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1319 if (*end)
1320 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001321 task = get_proc_task(file->f_dentry->d_inode);
1322 if (!task)
1323 return -ESRCH;
1324 task->make_it_fail = make_it_fail;
1325 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001326
1327 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001328}
1329
Arjan van de Ven00977a52007-02-12 00:55:34 -08001330static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001331 .read = proc_fault_inject_read,
1332 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001333 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001334};
1335#endif
1336
Arjan van de Ven97455122008-01-25 21:08:34 +01001337
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001338#ifdef CONFIG_SCHED_DEBUG
1339/*
1340 * Print out various scheduling related per-task fields:
1341 */
1342static int sched_show(struct seq_file *m, void *v)
1343{
1344 struct inode *inode = m->private;
1345 struct task_struct *p;
1346
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001347 p = get_proc_task(inode);
1348 if (!p)
1349 return -ESRCH;
1350 proc_sched_show_task(p, m);
1351
1352 put_task_struct(p);
1353
1354 return 0;
1355}
1356
1357static ssize_t
1358sched_write(struct file *file, const char __user *buf,
1359 size_t count, loff_t *offset)
1360{
1361 struct inode *inode = file->f_path.dentry->d_inode;
1362 struct task_struct *p;
1363
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001364 p = get_proc_task(inode);
1365 if (!p)
1366 return -ESRCH;
1367 proc_sched_set_task(p);
1368
1369 put_task_struct(p);
1370
1371 return count;
1372}
1373
1374static int sched_open(struct inode *inode, struct file *filp)
1375{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001376 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001377}
1378
1379static const struct file_operations proc_pid_sched_operations = {
1380 .open = sched_open,
1381 .read = seq_read,
1382 .write = sched_write,
1383 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001384 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001385};
1386
1387#endif
1388
Mike Galbraith5091faa2010-11-30 14:18:03 +01001389#ifdef CONFIG_SCHED_AUTOGROUP
1390/*
1391 * Print out autogroup related information:
1392 */
1393static int sched_autogroup_show(struct seq_file *m, void *v)
1394{
1395 struct inode *inode = m->private;
1396 struct task_struct *p;
1397
1398 p = get_proc_task(inode);
1399 if (!p)
1400 return -ESRCH;
1401 proc_sched_autogroup_show_task(p, m);
1402
1403 put_task_struct(p);
1404
1405 return 0;
1406}
1407
1408static ssize_t
1409sched_autogroup_write(struct file *file, const char __user *buf,
1410 size_t count, loff_t *offset)
1411{
1412 struct inode *inode = file->f_path.dentry->d_inode;
1413 struct task_struct *p;
1414 char buffer[PROC_NUMBUF];
1415 long nice;
1416 int err;
1417
1418 memset(buffer, 0, sizeof(buffer));
1419 if (count > sizeof(buffer) - 1)
1420 count = sizeof(buffer) - 1;
1421 if (copy_from_user(buffer, buf, count))
1422 return -EFAULT;
1423
1424 err = strict_strtol(strstrip(buffer), 0, &nice);
1425 if (err)
1426 return -EINVAL;
1427
1428 p = get_proc_task(inode);
1429 if (!p)
1430 return -ESRCH;
1431
1432 err = nice;
1433 err = proc_sched_autogroup_set_nice(p, &err);
1434 if (err)
1435 count = err;
1436
1437 put_task_struct(p);
1438
1439 return count;
1440}
1441
1442static int sched_autogroup_open(struct inode *inode, struct file *filp)
1443{
1444 int ret;
1445
1446 ret = single_open(filp, sched_autogroup_show, NULL);
1447 if (!ret) {
1448 struct seq_file *m = filp->private_data;
1449
1450 m->private = inode;
1451 }
1452 return ret;
1453}
1454
1455static const struct file_operations proc_pid_sched_autogroup_operations = {
1456 .open = sched_autogroup_open,
1457 .read = seq_read,
1458 .write = sched_autogroup_write,
1459 .llseek = seq_lseek,
1460 .release = single_release,
1461};
1462
1463#endif /* CONFIG_SCHED_AUTOGROUP */
1464
john stultz4614a696b2009-12-14 18:00:05 -08001465static ssize_t comm_write(struct file *file, const char __user *buf,
1466 size_t count, loff_t *offset)
1467{
1468 struct inode *inode = file->f_path.dentry->d_inode;
1469 struct task_struct *p;
1470 char buffer[TASK_COMM_LEN];
1471
1472 memset(buffer, 0, sizeof(buffer));
1473 if (count > sizeof(buffer) - 1)
1474 count = sizeof(buffer) - 1;
1475 if (copy_from_user(buffer, buf, count))
1476 return -EFAULT;
1477
1478 p = get_proc_task(inode);
1479 if (!p)
1480 return -ESRCH;
1481
1482 if (same_thread_group(current, p))
1483 set_task_comm(p, buffer);
1484 else
1485 count = -EINVAL;
1486
1487 put_task_struct(p);
1488
1489 return count;
1490}
1491
1492static int comm_show(struct seq_file *m, void *v)
1493{
1494 struct inode *inode = m->private;
1495 struct task_struct *p;
1496
1497 p = get_proc_task(inode);
1498 if (!p)
1499 return -ESRCH;
1500
1501 task_lock(p);
1502 seq_printf(m, "%s\n", p->comm);
1503 task_unlock(p);
1504
1505 put_task_struct(p);
1506
1507 return 0;
1508}
1509
1510static int comm_open(struct inode *inode, struct file *filp)
1511{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001512 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001513}
1514
1515static const struct file_operations proc_pid_set_comm_operations = {
1516 .open = comm_open,
1517 .read = seq_read,
1518 .write = comm_write,
1519 .llseek = seq_lseek,
1520 .release = single_release,
1521};
1522
Matt Helsley925d1c42008-04-29 01:01:36 -07001523/*
1524 * We added or removed a vma mapping the executable. The vmas are only mapped
1525 * during exec and are not mapped with the mmap system call.
1526 * Callers must hold down_write() on the mm's mmap_sem for these
1527 */
1528void added_exe_file_vma(struct mm_struct *mm)
1529{
1530 mm->num_exe_file_vmas++;
1531}
1532
1533void removed_exe_file_vma(struct mm_struct *mm)
1534{
1535 mm->num_exe_file_vmas--;
1536 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1537 fput(mm->exe_file);
1538 mm->exe_file = NULL;
1539 }
1540
1541}
1542
1543void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1544{
1545 if (new_exe_file)
1546 get_file(new_exe_file);
1547 if (mm->exe_file)
1548 fput(mm->exe_file);
1549 mm->exe_file = new_exe_file;
1550 mm->num_exe_file_vmas = 0;
1551}
1552
1553struct file *get_mm_exe_file(struct mm_struct *mm)
1554{
1555 struct file *exe_file;
1556
1557 /* We need mmap_sem to protect against races with removal of
1558 * VM_EXECUTABLE vmas */
1559 down_read(&mm->mmap_sem);
1560 exe_file = mm->exe_file;
1561 if (exe_file)
1562 get_file(exe_file);
1563 up_read(&mm->mmap_sem);
1564 return exe_file;
1565}
1566
1567void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1568{
1569 /* It's safe to write the exe_file pointer without exe_file_lock because
1570 * this is called during fork when the task is not yet in /proc */
1571 newmm->exe_file = get_mm_exe_file(oldmm);
1572}
1573
1574static int proc_exe_link(struct inode *inode, struct path *exe_path)
1575{
1576 struct task_struct *task;
1577 struct mm_struct *mm;
1578 struct file *exe_file;
1579
1580 task = get_proc_task(inode);
1581 if (!task)
1582 return -ENOENT;
1583 mm = get_task_mm(task);
1584 put_task_struct(task);
1585 if (!mm)
1586 return -ENOENT;
1587 exe_file = get_mm_exe_file(mm);
1588 mmput(mm);
1589 if (exe_file) {
1590 *exe_path = exe_file->f_path;
1591 path_get(&exe_file->f_path);
1592 fput(exe_file);
1593 return 0;
1594 } else
1595 return -ENOENT;
1596}
1597
Al Viro008b1502005-08-20 00:17:39 +01001598static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 struct inode *inode = dentry->d_inode;
1601 int error = -EACCES;
1602
1603 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001604 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Eric W. Biederman778c1142006-06-26 00:25:58 -07001606 /* Are we allowed to snoop on the tasks file descriptors? */
1607 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001610 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611out:
Al Viro008b1502005-08-20 00:17:39 +01001612 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001615static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Mel Gormane12ba742007-10-16 01:25:52 -07001617 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001618 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 int len;
1620
1621 if (!tmp)
1622 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001623
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001624 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001625 len = PTR_ERR(pathname);
1626 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001628 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 if (len > buflen)
1631 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001632 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 len = -EFAULT;
1634 out:
1635 free_page((unsigned long)tmp);
1636 return len;
1637}
1638
1639static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1640{
1641 int error = -EACCES;
1642 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001643 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Eric W. Biederman778c1142006-06-26 00:25:58 -07001645 /* Are we allowed to snoop on the tasks file descriptors? */
1646 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001649 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (error)
1651 goto out;
1652
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001653 error = do_proc_readlink(&path, buffer, buflen);
1654 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return error;
1657}
1658
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001659static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001661 .follow_link = proc_pid_follow_link,
1662 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663};
1664
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001665
1666/* building an inode */
1667
1668static int task_dumpable(struct task_struct *task)
1669{
1670 int dumpable = 0;
1671 struct mm_struct *mm;
1672
1673 task_lock(task);
1674 mm = task->mm;
1675 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001676 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001677 task_unlock(task);
1678 if(dumpable == 1)
1679 return 1;
1680 return 0;
1681}
1682
1683
Eric W. Biederman61a28782006-10-02 02:18:49 -07001684static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001685{
1686 struct inode * inode;
1687 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001688 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001689
1690 /* We need a new inode */
1691
1692 inode = new_inode(sb);
1693 if (!inode)
1694 goto out;
1695
1696 /* Common stuff */
1697 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001698 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001699 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001700 inode->i_op = &proc_def_inode_operations;
1701
1702 /*
1703 * grab the reference to task.
1704 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001705 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001706 if (!ei->pid)
1707 goto out_unlock;
1708
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001709 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001710 rcu_read_lock();
1711 cred = __task_cred(task);
1712 inode->i_uid = cred->euid;
1713 inode->i_gid = cred->egid;
1714 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001715 }
1716 security_task_to_inode(task, inode);
1717
1718out:
1719 return inode;
1720
1721out_unlock:
1722 iput(inode);
1723 return NULL;
1724}
1725
1726static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1727{
1728 struct inode *inode = dentry->d_inode;
1729 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001730 const struct cred *cred;
1731
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001732 generic_fillattr(inode, stat);
1733
1734 rcu_read_lock();
1735 stat->uid = 0;
1736 stat->gid = 0;
1737 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1738 if (task) {
1739 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1740 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001741 cred = __task_cred(task);
1742 stat->uid = cred->euid;
1743 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001744 }
1745 }
1746 rcu_read_unlock();
1747 return 0;
1748}
1749
1750/* dentry stuff */
1751
1752/*
1753 * Exceptional case: normally we are not allowed to unhash a busy
1754 * directory. In this case, however, we can do it - no aliasing problems
1755 * due to the way we treat inodes.
1756 *
1757 * Rewrite the inode's ownerships here because the owning task may have
1758 * performed a setuid(), etc.
1759 *
1760 * Before the /proc/pid/status file was created the only way to read
1761 * the effective uid of a /process was to stat /proc/pid. Reading
1762 * /proc/pid/status is slow enough that procps and other packages
1763 * kept stating /proc/pid. To keep the rules in /proc simple I have
1764 * made this apply to all per process world readable and executable
1765 * directories.
1766 */
1767static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1768{
Nick Piggin34286d62011-01-07 17:49:57 +11001769 struct inode *inode;
1770 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001771 const struct cred *cred;
1772
Nick Piggin34286d62011-01-07 17:49:57 +11001773 if (nd && nd->flags & LOOKUP_RCU)
1774 return -ECHILD;
1775
1776 inode = dentry->d_inode;
1777 task = get_proc_task(inode);
1778
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001779 if (task) {
1780 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1781 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001782 rcu_read_lock();
1783 cred = __task_cred(task);
1784 inode->i_uid = cred->euid;
1785 inode->i_gid = cred->egid;
1786 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001787 } else {
1788 inode->i_uid = 0;
1789 inode->i_gid = 0;
1790 }
1791 inode->i_mode &= ~(S_ISUID | S_ISGID);
1792 security_task_to_inode(task, inode);
1793 put_task_struct(task);
1794 return 1;
1795 }
1796 d_drop(dentry);
1797 return 0;
1798}
1799
Nick Pigginfe15ce42011-01-07 17:49:23 +11001800static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001801{
1802 /* Is the task we represent dead?
1803 * If so, then don't put the dentry on the lru list,
1804 * kill it immediately.
1805 */
1806 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1807}
1808
Al Virod72f71e2009-02-20 05:58:47 +00001809static const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001810{
1811 .d_revalidate = pid_revalidate,
1812 .d_delete = pid_delete_dentry,
1813};
1814
1815/* Lookups */
1816
Eric Dumazetc5141e62007-05-08 00:26:15 -07001817typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1818 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001819
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001820/*
1821 * Fill a directory entry.
1822 *
1823 * If possible create the dcache entry and derive our inode number and
1824 * file type from dcache entry.
1825 *
1826 * Since all of the proc inode numbers are dynamically generated, the inode
1827 * numbers do not exist until the inode is cache. This means creating the
1828 * the dcache entry in readdir is necessary to keep the inode numbers
1829 * reported by readdir in sync with the inode numbers reported
1830 * by stat.
1831 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001832static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1833 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001834 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001835{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001836 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001837 struct inode *inode;
1838 struct qstr qname;
1839 ino_t ino = 0;
1840 unsigned type = DT_UNKNOWN;
1841
1842 qname.name = name;
1843 qname.len = len;
1844 qname.hash = full_name_hash(name, len);
1845
1846 child = d_lookup(dir, &qname);
1847 if (!child) {
1848 struct dentry *new;
1849 new = d_alloc(dir, &qname);
1850 if (new) {
1851 child = instantiate(dir->d_inode, new, task, ptr);
1852 if (child)
1853 dput(new);
1854 else
1855 child = new;
1856 }
1857 }
1858 if (!child || IS_ERR(child) || !child->d_inode)
1859 goto end_instantiate;
1860 inode = child->d_inode;
1861 if (inode) {
1862 ino = inode->i_ino;
1863 type = inode->i_mode >> 12;
1864 }
1865 dput(child);
1866end_instantiate:
1867 if (!ino)
1868 ino = find_inode_number(dir, &qname);
1869 if (!ino)
1870 ino = 1;
1871 return filldir(dirent, name, len, filp->f_pos, ino, type);
1872}
1873
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001874static unsigned name_to_int(struct dentry *dentry)
1875{
1876 const char *name = dentry->d_name.name;
1877 int len = dentry->d_name.len;
1878 unsigned n = 0;
1879
1880 if (len > 1 && *name == '0')
1881 goto out;
1882 while (len-- > 0) {
1883 unsigned c = *name++ - '0';
1884 if (c > 9)
1885 goto out;
1886 if (n >= (~0U-9)/10)
1887 goto out;
1888 n *= 10;
1889 n += c;
1890 }
1891 return n;
1892out:
1893 return ~0U;
1894}
1895
Miklos Szeredi27932742007-05-08 00:26:17 -07001896#define PROC_FDINFO_MAX 64
1897
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001898static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001899{
1900 struct task_struct *task = get_proc_task(inode);
1901 struct files_struct *files = NULL;
1902 struct file *file;
1903 int fd = proc_fd(inode);
1904
1905 if (task) {
1906 files = get_files_struct(task);
1907 put_task_struct(task);
1908 }
1909 if (files) {
1910 /*
1911 * We are not taking a ref to the file structure, so we must
1912 * hold ->file_lock.
1913 */
1914 spin_lock(&files->file_lock);
1915 file = fcheck_files(files, fd);
1916 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001917 if (path) {
1918 *path = file->f_path;
1919 path_get(&file->f_path);
1920 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001921 if (info)
1922 snprintf(info, PROC_FDINFO_MAX,
1923 "pos:\t%lli\n"
1924 "flags:\t0%o\n",
1925 (long long) file->f_pos,
1926 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001927 spin_unlock(&files->file_lock);
1928 put_files_struct(files);
1929 return 0;
1930 }
1931 spin_unlock(&files->file_lock);
1932 put_files_struct(files);
1933 }
1934 return -ENOENT;
1935}
1936
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001937static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001938{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001939 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001940}
1941
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001942static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1943{
Nick Piggin34286d62011-01-07 17:49:57 +11001944 struct inode *inode;
1945 struct task_struct *task;
1946 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001947 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001948 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001949
Nick Piggin34286d62011-01-07 17:49:57 +11001950 if (nd && nd->flags & LOOKUP_RCU)
1951 return -ECHILD;
1952
1953 inode = dentry->d_inode;
1954 task = get_proc_task(inode);
1955 fd = proc_fd(inode);
1956
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001957 if (task) {
1958 files = get_files_struct(task);
1959 if (files) {
1960 rcu_read_lock();
1961 if (fcheck_files(files, fd)) {
1962 rcu_read_unlock();
1963 put_files_struct(files);
1964 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001965 rcu_read_lock();
1966 cred = __task_cred(task);
1967 inode->i_uid = cred->euid;
1968 inode->i_gid = cred->egid;
1969 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001970 } else {
1971 inode->i_uid = 0;
1972 inode->i_gid = 0;
1973 }
1974 inode->i_mode &= ~(S_ISUID | S_ISGID);
1975 security_task_to_inode(task, inode);
1976 put_task_struct(task);
1977 return 1;
1978 }
1979 rcu_read_unlock();
1980 put_files_struct(files);
1981 }
1982 put_task_struct(task);
1983 }
1984 d_drop(dentry);
1985 return 0;
1986}
1987
Al Virod72f71e2009-02-20 05:58:47 +00001988static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001989{
1990 .d_revalidate = tid_fd_revalidate,
1991 .d_delete = pid_delete_dentry,
1992};
1993
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001994static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001995 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001996{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001997 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001998 struct file *file;
1999 struct files_struct *files;
2000 struct inode *inode;
2001 struct proc_inode *ei;
2002 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002003
Eric W. Biederman61a28782006-10-02 02:18:49 -07002004 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002005 if (!inode)
2006 goto out;
2007 ei = PROC_I(inode);
2008 ei->fd = fd;
2009 files = get_files_struct(task);
2010 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002011 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002012 inode->i_mode = S_IFLNK;
2013
2014 /*
2015 * We are not taking a ref to the file structure, so we must
2016 * hold ->file_lock.
2017 */
2018 spin_lock(&files->file_lock);
2019 file = fcheck_files(files, fd);
2020 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002021 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04002022 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002023 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04002024 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002025 inode->i_mode |= S_IWUSR | S_IXUSR;
2026 spin_unlock(&files->file_lock);
2027 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002028
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002029 inode->i_op = &proc_pid_link_inode_operations;
2030 inode->i_size = 64;
2031 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002032 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002033 d_add(dentry, inode);
2034 /* Close the race of the process dying before we return the dentry */
2035 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002036 error = NULL;
2037
2038 out:
2039 return error;
2040out_unlock:
2041 spin_unlock(&files->file_lock);
2042 put_files_struct(files);
2043out_iput:
2044 iput(inode);
2045 goto out;
2046}
2047
Miklos Szeredi27932742007-05-08 00:26:17 -07002048static struct dentry *proc_lookupfd_common(struct inode *dir,
2049 struct dentry *dentry,
2050 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002051{
2052 struct task_struct *task = get_proc_task(dir);
2053 unsigned fd = name_to_int(dentry);
2054 struct dentry *result = ERR_PTR(-ENOENT);
2055
2056 if (!task)
2057 goto out_no_task;
2058 if (fd == ~0U)
2059 goto out;
2060
Miklos Szeredi27932742007-05-08 00:26:17 -07002061 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002062out:
2063 put_task_struct(task);
2064out_no_task:
2065 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002066}
2067
Miklos Szeredi27932742007-05-08 00:26:17 -07002068static int proc_readfd_common(struct file * filp, void * dirent,
2069 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002071 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07002072 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002073 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07002074 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 struct files_struct * files;
2077
2078 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002079 if (!p)
2080 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 fd = filp->f_pos;
2084 switch (fd) {
2085 case 0:
2086 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2087 goto out;
2088 filp->f_pos++;
2089 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07002090 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2092 goto out;
2093 filp->f_pos++;
2094 default:
2095 files = get_files_struct(p);
2096 if (!files)
2097 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002098 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04002100 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07002102 char name[PROC_NUMBUF];
2103 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 if (!fcheck_files(files, fd))
2106 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002107 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Miklos Szeredi27932742007-05-08 00:26:17 -07002109 len = snprintf(name, sizeof(name), "%d", fd);
2110 if (proc_fill_cache(filp, dirent, filldir,
2111 name, len, instantiate,
2112 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07002113 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 break;
2115 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002116 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002118 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 put_files_struct(files);
2120 }
2121out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002122 put_task_struct(p);
2123out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return retval;
2125}
2126
Miklos Szeredi27932742007-05-08 00:26:17 -07002127static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2128 struct nameidata *nd)
2129{
2130 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2131}
2132
2133static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2134{
2135 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2136}
2137
2138static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2139 size_t len, loff_t *ppos)
2140{
2141 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002142 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002143 if (!err)
2144 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2145 return err;
2146}
2147
2148static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002149 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002150 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002151 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002152};
2153
Arjan van de Ven00977a52007-02-12 00:55:34 -08002154static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 .read = generic_read_dir,
2156 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002157 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158};
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002161 * /proc/pid/fd needs a special permission handler so that a process can still
2162 * access /proc/self/fd after it has executed a setuid().
2163 */
Nick Pigginb74c79e2011-01-07 17:49:58 +11002164static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002165{
2166 int rv;
2167
Nick Pigginb74c79e2011-01-07 17:49:58 +11002168 if (flags & IPERM_FLAG_RCU)
2169 return -ECHILD;
2170 rv = generic_permission(inode, mask, flags, NULL);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002171 if (rv == 0)
2172 return 0;
2173 if (task_pid(current) == proc_pid(inode))
2174 rv = 0;
2175 return rv;
2176}
2177
2178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 * proc directories can do almost nothing..
2180 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002181static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002183 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002184 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185};
2186
Miklos Szeredi27932742007-05-08 00:26:17 -07002187static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2188 struct dentry *dentry, struct task_struct *task, const void *ptr)
2189{
2190 unsigned fd = *(unsigned *)ptr;
2191 struct inode *inode;
2192 struct proc_inode *ei;
2193 struct dentry *error = ERR_PTR(-ENOENT);
2194
2195 inode = proc_pid_make_inode(dir->i_sb, task);
2196 if (!inode)
2197 goto out;
2198 ei = PROC_I(inode);
2199 ei->fd = fd;
2200 inode->i_mode = S_IFREG | S_IRUSR;
2201 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002202 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002203 d_add(dentry, inode);
2204 /* Close the race of the process dying before we return the dentry */
2205 if (tid_fd_revalidate(dentry, NULL))
2206 error = NULL;
2207
2208 out:
2209 return error;
2210}
2211
2212static struct dentry *proc_lookupfdinfo(struct inode *dir,
2213 struct dentry *dentry,
2214 struct nameidata *nd)
2215{
2216 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2217}
2218
2219static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2220{
2221 return proc_readfd_common(filp, dirent, filldir,
2222 proc_fdinfo_instantiate);
2223}
2224
2225static const struct file_operations proc_fdinfo_operations = {
2226 .read = generic_read_dir,
2227 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002228 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002229};
2230
2231/*
2232 * proc directories can do almost nothing..
2233 */
2234static const struct inode_operations proc_fdinfo_inode_operations = {
2235 .lookup = proc_lookupfdinfo,
2236 .setattr = proc_setattr,
2237};
2238
2239
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002240static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002241 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002242{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002243 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002244 struct inode *inode;
2245 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002246 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002247
Eric W. Biederman61a28782006-10-02 02:18:49 -07002248 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002249 if (!inode)
2250 goto out;
2251
2252 ei = PROC_I(inode);
2253 inode->i_mode = p->mode;
2254 if (S_ISDIR(inode->i_mode))
2255 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2256 if (p->iop)
2257 inode->i_op = p->iop;
2258 if (p->fop)
2259 inode->i_fop = p->fop;
2260 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002261 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002262 d_add(dentry, inode);
2263 /* Close the race of the process dying before we return the dentry */
2264 if (pid_revalidate(dentry, NULL))
2265 error = NULL;
2266out:
2267 return error;
2268}
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270static struct dentry *proc_pident_lookup(struct inode *dir,
2271 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002272 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002273 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002275 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002276 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002277 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002279 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Eric W. Biederman99f89552006-06-26 00:25:55 -07002281 if (!task)
2282 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002284 /*
2285 * Yes, it does not scale. And it should not. Don't add
2286 * new entries into /proc/<tgid>/ without very good reasons.
2287 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002288 last = &ents[nents - 1];
2289 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (p->len != dentry->d_name.len)
2291 continue;
2292 if (!memcmp(dentry->d_name.name, p->name, p->len))
2293 break;
2294 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002295 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto out;
2297
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002298 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002300 put_task_struct(task);
2301out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002302 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Eric Dumazetc5141e62007-05-08 00:26:15 -07002305static int proc_pident_fill_cache(struct file *filp, void *dirent,
2306 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002307{
2308 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2309 proc_pident_instantiate, task, p);
2310}
2311
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002312static int proc_pident_readdir(struct file *filp,
2313 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002314 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002315{
2316 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002317 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002318 struct inode *inode = dentry->d_inode;
2319 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002320 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002321 ino_t ino;
2322 int ret;
2323
2324 ret = -ENOENT;
2325 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002326 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002327
2328 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002329 i = filp->f_pos;
2330 switch (i) {
2331 case 0:
2332 ino = inode->i_ino;
2333 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2334 goto out;
2335 i++;
2336 filp->f_pos++;
2337 /* fall through */
2338 case 1:
2339 ino = parent_ino(dentry);
2340 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2341 goto out;
2342 i++;
2343 filp->f_pos++;
2344 /* fall through */
2345 default:
2346 i -= 2;
2347 if (i >= nents) {
2348 ret = 1;
2349 goto out;
2350 }
2351 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002352 last = &ents[nents - 1];
2353 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002354 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002355 goto out;
2356 filp->f_pos++;
2357 p++;
2358 }
2359 }
2360
2361 ret = 1;
2362out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002363 put_task_struct(task);
2364out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002369static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2370 size_t count, loff_t *ppos)
2371{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002372 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002373 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002374 ssize_t length;
2375 struct task_struct *task = get_proc_task(inode);
2376
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002377 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002378 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002379
2380 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002381 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002382 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002383 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002384 if (length > 0)
2385 length = simple_read_from_buffer(buf, count, ppos, p, length);
2386 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002387 return length;
2388}
2389
2390static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2391 size_t count, loff_t *ppos)
2392{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002393 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002394 char *page;
2395 ssize_t length;
2396 struct task_struct *task = get_proc_task(inode);
2397
2398 length = -ESRCH;
2399 if (!task)
2400 goto out_no_task;
2401 if (count > PAGE_SIZE)
2402 count = PAGE_SIZE;
2403
2404 /* No partial writes. */
2405 length = -EINVAL;
2406 if (*ppos != 0)
2407 goto out;
2408
2409 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002410 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002411 if (!page)
2412 goto out;
2413
2414 length = -EFAULT;
2415 if (copy_from_user(page, buf, count))
2416 goto out_free;
2417
David Howells107db7c2009-05-08 13:55:27 +01002418 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002419 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002420 if (length < 0)
2421 goto out_free;
2422
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002423 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002424 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002425 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002426 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002427out_free:
2428 free_page((unsigned long) page);
2429out:
2430 put_task_struct(task);
2431out_no_task:
2432 return length;
2433}
2434
Arjan van de Ven00977a52007-02-12 00:55:34 -08002435static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002436 .read = proc_pid_attr_read,
2437 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002438 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002439};
2440
Eric Dumazetc5141e62007-05-08 00:26:15 -07002441static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002442 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2443 REG("prev", S_IRUGO, proc_pid_attr_operations),
2444 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2445 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2446 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2447 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002448};
2449
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002450static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 void * dirent, filldir_t filldir)
2452{
2453 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002454 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Arjan van de Ven00977a52007-02-12 00:55:34 -08002457static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002459 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002460 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461};
2462
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002463static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct dentry *dentry, struct nameidata *nd)
2465{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002466 return proc_pident_lookup(dir, dentry,
2467 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002470static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002471 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002472 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002473 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474};
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476#endif
2477
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002478#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002479static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2480 size_t count, loff_t *ppos)
2481{
2482 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2483 struct mm_struct *mm;
2484 char buffer[PROC_NUMBUF];
2485 size_t len;
2486 int ret;
2487
2488 if (!task)
2489 return -ESRCH;
2490
2491 ret = 0;
2492 mm = get_task_mm(task);
2493 if (mm) {
2494 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2495 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2496 MMF_DUMP_FILTER_SHIFT));
2497 mmput(mm);
2498 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2499 }
2500
2501 put_task_struct(task);
2502
2503 return ret;
2504}
2505
2506static ssize_t proc_coredump_filter_write(struct file *file,
2507 const char __user *buf,
2508 size_t count,
2509 loff_t *ppos)
2510{
2511 struct task_struct *task;
2512 struct mm_struct *mm;
2513 char buffer[PROC_NUMBUF], *end;
2514 unsigned int val;
2515 int ret;
2516 int i;
2517 unsigned long mask;
2518
2519 ret = -EFAULT;
2520 memset(buffer, 0, sizeof(buffer));
2521 if (count > sizeof(buffer) - 1)
2522 count = sizeof(buffer) - 1;
2523 if (copy_from_user(buffer, buf, count))
2524 goto out_no_task;
2525
2526 ret = -EINVAL;
2527 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2528 if (*end == '\n')
2529 end++;
2530 if (end - buffer == 0)
2531 goto out_no_task;
2532
2533 ret = -ESRCH;
2534 task = get_proc_task(file->f_dentry->d_inode);
2535 if (!task)
2536 goto out_no_task;
2537
2538 ret = end - buffer;
2539 mm = get_task_mm(task);
2540 if (!mm)
2541 goto out_no_mm;
2542
2543 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2544 if (val & mask)
2545 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2546 else
2547 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2548 }
2549
2550 mmput(mm);
2551 out_no_mm:
2552 put_task_struct(task);
2553 out_no_task:
2554 return ret;
2555}
2556
2557static const struct file_operations proc_coredump_filter_operations = {
2558 .read = proc_coredump_filter_read,
2559 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002560 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002561};
2562#endif
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564/*
2565 * /proc/self:
2566 */
2567static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2568 int buflen)
2569{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002570 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002571 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002572 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002573 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002574 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002575 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return vfs_readlink(dentry,buffer,buflen,tmp);
2577}
2578
Al Viro008b1502005-08-20 00:17:39 +01002579static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002581 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002582 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002583 char *name = ERR_PTR(-ENOENT);
2584 if (tgid) {
2585 name = __getname();
2586 if (!name)
2587 name = ERR_PTR(-ENOMEM);
2588 else
2589 sprintf(name, "%d", tgid);
2590 }
2591 nd_set_link(nd, name);
2592 return NULL;
2593}
2594
2595static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2596 void *cookie)
2597{
2598 char *s = nd_get_link(nd);
2599 if (!IS_ERR(s))
2600 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002603static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 .readlink = proc_self_readlink,
2605 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002606 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607};
2608
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002609/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002610 * proc base
2611 *
2612 * These are the directory entries in the root directory of /proc
2613 * that properly belong to the /proc filesystem, as they describe
2614 * describe something that is process related.
2615 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002616static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002617 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002618 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002619};
2620
2621/*
2622 * Exceptional case: normally we are not allowed to unhash a busy
2623 * directory. In this case, however, we can do it - no aliasing problems
2624 * due to the way we treat inodes.
2625 */
2626static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2627{
Nick Piggin34286d62011-01-07 17:49:57 +11002628 struct inode *inode;
2629 struct task_struct *task;
2630
2631 if (nd->flags & LOOKUP_RCU)
2632 return -ECHILD;
2633
2634 inode = dentry->d_inode;
2635 task = get_proc_task(inode);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002636 if (task) {
2637 put_task_struct(task);
2638 return 1;
2639 }
2640 d_drop(dentry);
2641 return 0;
2642}
2643
Al Virod72f71e2009-02-20 05:58:47 +00002644static const struct dentry_operations proc_base_dentry_operations =
Eric W. Biederman801199c2006-10-02 02:18:48 -07002645{
2646 .d_revalidate = proc_base_revalidate,
2647 .d_delete = pid_delete_dentry,
2648};
2649
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002650static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002651 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002652{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002653 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002654 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002655 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002656 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002657
2658 /* Allocate the inode */
2659 error = ERR_PTR(-ENOMEM);
2660 inode = new_inode(dir->i_sb);
2661 if (!inode)
2662 goto out;
2663
2664 /* Initialize the inode */
2665 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002666 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002667 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002668
2669 /*
2670 * grab the reference to the task.
2671 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002672 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002673 if (!ei->pid)
2674 goto out_iput;
2675
Eric W. Biederman801199c2006-10-02 02:18:48 -07002676 inode->i_mode = p->mode;
2677 if (S_ISDIR(inode->i_mode))
2678 inode->i_nlink = 2;
2679 if (S_ISLNK(inode->i_mode))
2680 inode->i_size = 64;
2681 if (p->iop)
2682 inode->i_op = p->iop;
2683 if (p->fop)
2684 inode->i_fop = p->fop;
2685 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002686 d_set_d_op(dentry, &proc_base_dentry_operations);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002687 d_add(dentry, inode);
2688 error = NULL;
2689out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002690 return error;
2691out_iput:
2692 iput(inode);
2693 goto out;
2694}
2695
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002696static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2697{
2698 struct dentry *error;
2699 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002700 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002701
2702 error = ERR_PTR(-ENOENT);
2703
2704 if (!task)
2705 goto out_no_task;
2706
2707 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002708 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2709 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002710 if (p->len != dentry->d_name.len)
2711 continue;
2712 if (!memcmp(dentry->d_name.name, p->name, p->len))
2713 break;
2714 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002715 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002716 goto out;
2717
2718 error = proc_base_instantiate(dir, dentry, task, p);
2719
2720out:
2721 put_task_struct(task);
2722out_no_task:
2723 return error;
2724}
2725
Eric Dumazetc5141e62007-05-08 00:26:15 -07002726static int proc_base_fill_cache(struct file *filp, void *dirent,
2727 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002728{
2729 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2730 proc_base_instantiate, task, p);
2731}
2732
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002733#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002734static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002735{
Andrea Righi940389b2008-07-28 00:48:12 +02002736 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002737 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002738
Andrea Righi59954772008-07-27 17:29:15 +02002739 if (whole && lock_task_sighand(task, &flags)) {
2740 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002741
Andrea Righi59954772008-07-27 17:29:15 +02002742 task_io_accounting_add(&acct, &task->signal->ioac);
2743 while_each_thread(task, t)
2744 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002745
Andrea Righi59954772008-07-27 17:29:15 +02002746 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002747 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002748 return sprintf(buffer,
2749 "rchar: %llu\n"
2750 "wchar: %llu\n"
2751 "syscr: %llu\n"
2752 "syscw: %llu\n"
2753 "read_bytes: %llu\n"
2754 "write_bytes: %llu\n"
2755 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002756 (unsigned long long)acct.rchar,
2757 (unsigned long long)acct.wchar,
2758 (unsigned long long)acct.syscr,
2759 (unsigned long long)acct.syscw,
2760 (unsigned long long)acct.read_bytes,
2761 (unsigned long long)acct.write_bytes,
2762 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002763}
Andrea Righi297c5d92008-07-25 01:48:49 -07002764
2765static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2766{
2767 return do_io_accounting(task, buffer, 0);
2768}
2769
2770static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2771{
2772 return do_io_accounting(task, buffer, 1);
2773}
2774#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002775
Kees Cook47830722008-10-06 03:11:58 +04002776static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2777 struct pid *pid, struct task_struct *task)
2778{
2779 seq_printf(m, "%08x\n", task->personality);
2780 return 0;
2781}
2782
Eric W. Biederman801199c2006-10-02 02:18:48 -07002783/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002784 * Thread groups
2785 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002786static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002787static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002788
Eric Dumazetc5141e62007-05-08 00:26:15 -07002789static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002790 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2791 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2792 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002793#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002794 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002795#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002796 REG("environ", S_IRUSR, proc_environ_operations),
2797 INF("auxv", S_IRUSR, proc_pid_auxv),
2798 ONE("status", S_IRUGO, proc_pid_status),
2799 ONE("personality", S_IRUSR, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002800 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002801#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002802 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002803#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002804#ifdef CONFIG_SCHED_AUTOGROUP
2805 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2806#endif
john stultz4614a696b2009-12-14 18:00:05 -08002807 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002808#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002809 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002810#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002811 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2812 ONE("stat", S_IRUGO, proc_tgid_stat),
2813 ONE("statm", S_IRUGO, proc_pid_statm),
2814 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002815#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002816 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002817#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002818 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2819 LNK("cwd", proc_cwd_link),
2820 LNK("root", proc_root_link),
2821 LNK("exe", proc_exe_link),
2822 REG("mounts", S_IRUGO, proc_mounts_operations),
2823 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2824 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002825#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002826 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2827 REG("smaps", S_IRUGO, proc_smaps_operations),
2828 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002829#endif
2830#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002831 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002832#endif
2833#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002834 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002835#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002836#ifdef CONFIG_STACKTRACE
2837 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002838#endif
2839#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002840 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002841#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002842#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002843 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002844#endif
Paul Menage8793d852007-10-18 23:39:39 -07002845#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002846 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002847#endif
Paul Menagea4243162007-10-18 23:39:35 -07002848#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002849 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002850#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002851 INF("oom_score", S_IRUGO, proc_oom_score),
2852 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002853 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002854#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002855 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2856 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002857#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002858#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002859 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002860#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002861#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002862 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002863#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002864#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002865 INF("io", S_IRUGO, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002866#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002867};
2868
2869static int proc_tgid_base_readdir(struct file * filp,
2870 void * dirent, filldir_t filldir)
2871{
2872 return proc_pident_readdir(filp,dirent,filldir,
2873 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2874}
2875
Arjan van de Ven00977a52007-02-12 00:55:34 -08002876static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002877 .read = generic_read_dir,
2878 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002879 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002880};
2881
2882static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002883 return proc_pident_lookup(dir, dentry,
2884 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002885}
2886
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002887static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002888 .lookup = proc_tgid_base_lookup,
2889 .getattr = pid_getattr,
2890 .setattr = proc_setattr,
2891};
2892
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002893static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002895 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002896 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002897 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Eric W. Biederman48e64842006-06-26 00:25:48 -07002899 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002900 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2901 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002902 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002903 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002904 d_drop(dentry);
2905 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Eric W. Biederman48e64842006-06-26 00:25:48 -07002908 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002909 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2910 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002911 if (!leader)
2912 goto out;
2913
2914 name.name = "task";
2915 name.len = strlen(name.name);
2916 dir = d_hash_and_lookup(leader, &name);
2917 if (!dir)
2918 goto out_put_leader;
2919
2920 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002921 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002922 dentry = d_hash_and_lookup(dir, &name);
2923 if (dentry) {
2924 shrink_dcache_parent(dentry);
2925 d_drop(dentry);
2926 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002928
2929 dput(dir);
2930out_put_leader:
2931 dput(leader);
2932out:
2933 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
Randy Dunlap0895e912007-10-21 21:00:10 -07002936/**
2937 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2938 * @task: task that should be flushed.
2939 *
2940 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002941 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002942 * in. This call is supposed to do all of this job.
2943 *
2944 * Looks in the dcache for
2945 * /proc/@pid
2946 * /proc/@tgid/task/@pid
2947 * if either directory is present flushes it and all of it'ts children
2948 * from the dcache.
2949 *
2950 * It is safe and reasonable to cache /proc entries for a task until
2951 * that task exits. After that they just clog up the dcache with
2952 * useless entries, possibly causing useful dcache entries to be
2953 * flushed instead. This routine is proved to flush those useless
2954 * dcache entries at process exit time.
2955 *
2956 * NOTE: This routine is just an optimization so it does not guarantee
2957 * that no dcache entries will exist at process exit time it
2958 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002959 */
2960
2961void proc_flush_task(struct task_struct *task)
2962{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002963 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002964 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002965 struct upid *upid;
2966
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002967 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002968 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002969
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002970 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002971 upid = &pid->numbers[i];
2972 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002973 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002974 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002975
2976 upid = &pid->numbers[pid->level];
2977 if (upid->nr == 1)
2978 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002979}
2980
Adrian Bunk9711ef92006-12-06 20:38:31 -08002981static struct dentry *proc_pid_instantiate(struct inode *dir,
2982 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002983 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002984{
2985 struct dentry *error = ERR_PTR(-ENOENT);
2986 struct inode *inode;
2987
Eric W. Biederman61a28782006-10-02 02:18:49 -07002988 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002989 if (!inode)
2990 goto out;
2991
2992 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2993 inode->i_op = &proc_tgid_base_inode_operations;
2994 inode->i_fop = &proc_tgid_base_operations;
2995 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002996
2997 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2998 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002999
Nick Pigginfb045ad2011-01-07 17:49:55 +11003000 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003001
3002 d_add(dentry, inode);
3003 /* Close the race of the process dying before we return the dentry */
3004 if (pid_revalidate(dentry, NULL))
3005 error = NULL;
3006out:
3007 return error;
3008}
3009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3011{
Dan Carpenter73d36462010-05-26 14:43:25 -07003012 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003015 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Eric W. Biederman801199c2006-10-02 02:18:48 -07003017 result = proc_base_lookup(dir, dentry);
3018 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3019 goto out;
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 tgid = name_to_int(dentry);
3022 if (tgid == ~0U)
3023 goto out;
3024
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003025 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003026 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003027 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 if (task)
3029 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003030 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (!task)
3032 goto out;
3033
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003034 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003035 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07003037 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003041 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003042 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003044struct tgid_iter {
3045 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003046 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003047};
3048static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3049{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003050 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003052 if (iter.task)
3053 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003054 rcu_read_lock();
3055retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003056 iter.task = NULL;
3057 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003058 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003059 iter.tgid = pid_nr_ns(pid, ns);
3060 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003061 /* What we to know is if the pid we have find is the
3062 * pid of a thread_group_leader. Testing for task
3063 * being a thread_group_leader is the obvious thing
3064 * todo but there is a window when it fails, due to
3065 * the pid transfer logic in de_thread.
3066 *
3067 * So we perform the straight forward test of seeing
3068 * if the pid we have found is the pid of a thread
3069 * group leader, and don't worry if the task we have
3070 * found doesn't happen to be a thread group leader.
3071 * As we don't care in the case of readdir.
3072 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003073 if (!iter.task || !has_group_leader_pid(iter.task)) {
3074 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003075 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003076 }
3077 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003079 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003080 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003083#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Eric W. Biederman61a28782006-10-02 02:18:49 -07003085static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003086 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003087{
3088 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003089 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003090 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003091 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003092}
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094/* for the /proc/ directory itself, after non-process stuff has been done */
3095int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003098 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003099 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003100 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Eric W. Biederman61a28782006-10-02 02:18:49 -07003102 if (!reaper)
3103 goto out_no_task;
3104
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003105 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003106 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003107 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003111 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003112 iter.task = NULL;
3113 iter.tgid = filp->f_pos - TGID_OFFSET;
3114 for (iter = next_tgid(ns, iter);
3115 iter.task;
3116 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3117 filp->f_pos = iter.tgid + TGID_OFFSET;
3118 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3119 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003123 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3124out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003125 put_task_struct(reaper);
3126out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return 0;
3128}
3129
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003130/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003131 * Tasks
3132 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003133static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003134 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003135 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003136 REG("environ", S_IRUSR, proc_environ_operations),
3137 INF("auxv", S_IRUSR, proc_pid_auxv),
3138 ONE("status", S_IRUGO, proc_pid_status),
3139 ONE("personality", S_IRUSR, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003140 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003141#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003142 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003143#endif
john stultz4614a696b2009-12-14 18:00:05 -08003144 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003145#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003146 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003147#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003148 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3149 ONE("stat", S_IRUGO, proc_tid_stat),
3150 ONE("statm", S_IRUGO, proc_pid_statm),
3151 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003152#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003153 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003154#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003155 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3156 LNK("cwd", proc_cwd_link),
3157 LNK("root", proc_root_link),
3158 LNK("exe", proc_exe_link),
3159 REG("mounts", S_IRUGO, proc_mounts_operations),
3160 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003161#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003162 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3163 REG("smaps", S_IRUGO, proc_smaps_operations),
3164 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003165#endif
3166#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003167 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003168#endif
3169#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003170 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003171#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003172#ifdef CONFIG_STACKTRACE
3173 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003174#endif
3175#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003176 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003177#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003178#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003179 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003180#endif
Paul Menage8793d852007-10-18 23:39:39 -07003181#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003182 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003183#endif
Paul Menagea4243162007-10-18 23:39:35 -07003184#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003185 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003186#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003187 INF("oom_score", S_IRUGO, proc_oom_score),
3188 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003189 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003190#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003191 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3192 REG("sessionid", S_IRUSR, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003193#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003194#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003195 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003196#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003197#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003198 INF("io", S_IRUGO, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003199#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003200};
3201
3202static int proc_tid_base_readdir(struct file * filp,
3203 void * dirent, filldir_t filldir)
3204{
3205 return proc_pident_readdir(filp,dirent,filldir,
3206 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3207}
3208
3209static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003210 return proc_pident_lookup(dir, dentry,
3211 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003212}
3213
Arjan van de Ven00977a52007-02-12 00:55:34 -08003214static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003215 .read = generic_read_dir,
3216 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003217 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003218};
3219
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003220static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003221 .lookup = proc_tid_base_lookup,
3222 .getattr = pid_getattr,
3223 .setattr = proc_setattr,
3224};
3225
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003226static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003227 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003228{
3229 struct dentry *error = ERR_PTR(-ENOENT);
3230 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003231 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003232
3233 if (!inode)
3234 goto out;
3235 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3236 inode->i_op = &proc_tid_base_inode_operations;
3237 inode->i_fop = &proc_tid_base_operations;
3238 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003239
3240 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3241 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003242
Nick Pigginfb045ad2011-01-07 17:49:55 +11003243 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003244
3245 d_add(dentry, inode);
3246 /* Close the race of the process dying before we return the dentry */
3247 if (pid_revalidate(dentry, NULL))
3248 error = NULL;
3249out:
3250 return error;
3251}
3252
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003253static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3254{
3255 struct dentry *result = ERR_PTR(-ENOENT);
3256 struct task_struct *task;
3257 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003258 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003259 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003260
3261 if (!leader)
3262 goto out_no_task;
3263
3264 tid = name_to_int(dentry);
3265 if (tid == ~0U)
3266 goto out;
3267
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003268 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003269 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003270 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003271 if (task)
3272 get_task_struct(task);
3273 rcu_read_unlock();
3274 if (!task)
3275 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003276 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003277 goto out_drop_task;
3278
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003279 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003280out_drop_task:
3281 put_task_struct(task);
3282out:
3283 put_task_struct(leader);
3284out_no_task:
3285 return result;
3286}
3287
3288/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003289 * Find the first tid of a thread group to return to user space.
3290 *
3291 * Usually this is just the thread group leader, but if the users
3292 * buffer was too small or there was a seek into the middle of the
3293 * directory we have more work todo.
3294 *
3295 * In the case of a short read we start with find_task_by_pid.
3296 *
3297 * In the case of a seek we start with the leader and walk nr
3298 * threads past it.
3299 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003300static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003301 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003302{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003303 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003304
Eric W. Biedermancc288732006-06-26 00:26:01 -07003305 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003306 /* Attempt to start with the pid of a thread */
3307 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003308 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003309 if (pos && (pos->group_leader == leader))
3310 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003311 }
3312
3313 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003314 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003315 if (nr && nr >= get_nr_threads(leader))
3316 goto out;
3317
3318 /* If we haven't found our starting place yet start
3319 * with the leader and walk nr threads forward.
3320 */
3321 for (pos = leader; nr > 0; --nr) {
3322 pos = next_thread(pos);
3323 if (pos == leader) {
3324 pos = NULL;
3325 goto out;
3326 }
3327 }
3328found:
3329 get_task_struct(pos);
3330out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003331 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003332 return pos;
3333}
3334
3335/*
3336 * Find the next thread in the thread list.
3337 * Return NULL if there is an error or no next thread.
3338 *
3339 * The reference to the input task_struct is released.
3340 */
3341static struct task_struct *next_tid(struct task_struct *start)
3342{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003343 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003344 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003345 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003346 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003347 if (thread_group_leader(pos))
3348 pos = NULL;
3349 else
3350 get_task_struct(pos);
3351 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003352 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003353 put_task_struct(start);
3354 return pos;
3355}
3356
Eric W. Biederman61a28782006-10-02 02:18:49 -07003357static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3358 struct task_struct *task, int tid)
3359{
3360 char name[PROC_NUMBUF];
3361 int len = snprintf(name, sizeof(name), "%d", tid);
3362 return proc_fill_cache(filp, dirent, filldir, name, len,
3363 proc_task_instantiate, task, NULL);
3364}
3365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366/* for the /proc/TGID/task/ directories */
3367static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3368{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003369 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003371 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003372 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 int retval = -ENOENT;
3374 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003375 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003376 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003378 task = get_proc_task(inode);
3379 if (!task)
3380 goto out_no_task;
3381 rcu_read_lock();
3382 if (pid_alive(task)) {
3383 leader = task->group_leader;
3384 get_task_struct(leader);
3385 }
3386 rcu_read_unlock();
3387 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003388 if (!leader)
3389 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 retval = 0;
3391
Linus Torvaldsee568b22009-03-17 10:02:35 -07003392 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 case 0:
3394 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003395 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003397 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 /* fall through */
3399 case 1:
3400 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003401 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003403 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 /* fall through */
3405 }
3406
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003407 /* f_version caches the tgid value that the last readdir call couldn't
3408 * return. lseek aka telldir automagically resets f_version to 0.
3409 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003410 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003411 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003412 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003413 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003414 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003415 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003416 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003417 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003418 /* returning this tgid failed, save it as the first
3419 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003420 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003421 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
3425out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003426 put_task_struct(leader);
3427out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return retval;
3429}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003430
3431static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3432{
3433 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003434 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003435 generic_fillattr(inode, stat);
3436
Eric W. Biederman99f89552006-06-26 00:25:55 -07003437 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003438 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003439 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003440 }
3441
3442 return 0;
3443}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003444
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003445static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003446 .lookup = proc_task_lookup,
3447 .getattr = proc_task_getattr,
3448 .setattr = proc_setattr,
3449};
3450
Arjan van de Ven00977a52007-02-12 00:55:34 -08003451static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003452 .read = generic_read_dir,
3453 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003454 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003455};