blob: 013f116b3223a88a1b648a1857008c9466a612e5 [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
Stephen Wilson18f661b2011-03-13 15:49:22 -0400194static int __check_mem_permission(struct task_struct *task)
Roland McGrath638fa2022008-04-29 01:01:38 -0700195{
196 /*
197 * A task can always look at itself, in case it chooses
198 * to use system calls instead of load instructions.
199 */
200 if (task == current)
201 return 0;
202
203 /*
204 * If current is actively ptrace'ing, and would also be
205 * permitted to freshly attach with ptrace now, permit it.
206 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700207 if (task_is_stopped_or_traced(task)) {
208 int match;
209 rcu_read_lock();
210 match = (tracehook_tracer_task(task) == current);
211 rcu_read_unlock();
212 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
213 return 0;
214 }
Roland McGrath638fa2022008-04-29 01:01:38 -0700215
216 /*
217 * Noone else is allowed.
218 */
219 return -EPERM;
220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Stephen Wilson18f661b2011-03-13 15:49:22 -0400222/*
223 * Return zero if current may access user memory in @task, -error if not.
224 */
225static int check_mem_permission(struct task_struct *task)
226{
227 int err;
228
229 /*
230 * Avoid racing if task exec's as we might get a new mm but validate
231 * against old credentials.
232 */
233 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
234 if (err)
235 return err;
236
237 err = __check_mem_permission(task);
238 mutex_unlock(&task->signal->cred_guard_mutex);
239
240 return err;
241}
242
Al Viro831830b2008-01-02 14:09:57 +0000243struct mm_struct *mm_for_maps(struct task_struct *task)
244{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200245 struct mm_struct *mm;
Al Viroec6fd8a2011-02-15 22:22:54 -0500246 int err;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200247
Al Viroec6fd8a2011-02-15 22:22:54 -0500248 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
249 if (err)
250 return ERR_PTR(err);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200251
252 mm = get_task_mm(task);
253 if (mm && mm != current->mm &&
254 !ptrace_may_access(task, PTRACE_MODE_READ)) {
255 mmput(mm);
Al Viroec6fd8a2011-02-15 22:22:54 -0500256 mm = ERR_PTR(-EACCES);
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200257 }
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700258 mutex_unlock(&task->signal->cred_guard_mutex);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200259
Al Viro831830b2008-01-02 14:09:57 +0000260 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static int proc_pid_cmdline(struct task_struct *task, char * buffer)
264{
265 int res = 0;
266 unsigned int len;
267 struct mm_struct *mm = get_task_mm(task);
268 if (!mm)
269 goto out;
270 if (!mm->arg_end)
271 goto out_mm; /* Shh! No looking before we're done */
272
273 len = mm->arg_end - mm->arg_start;
274
275 if (len > PAGE_SIZE)
276 len = PAGE_SIZE;
277
278 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
279
280 // If the nul at the end of args has been overwritten, then
281 // assume application is using setproctitle(3).
282 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
283 len = strnlen(buffer, res);
284 if (len < res) {
285 res = len;
286 } else {
287 len = mm->env_end - mm->env_start;
288 if (len > PAGE_SIZE - res)
289 len = PAGE_SIZE - res;
290 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
291 res = strnlen(buffer, res);
292 }
293 }
294out_mm:
295 mmput(mm);
296out:
297 return res;
298}
299
300static int proc_pid_auxv(struct task_struct *task, char *buffer)
301{
Al Viro2fadaef2011-02-15 22:52:11 -0500302 struct mm_struct *mm = mm_for_maps(task);
303 int res = PTR_ERR(mm);
304 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300306 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300308 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 res = nwords * sizeof(mm->saved_auxv[0]);
310 if (res > PAGE_SIZE)
311 res = PAGE_SIZE;
312 memcpy(buffer, mm->saved_auxv, res);
313 mmput(mm);
314 }
315 return res;
316}
317
318
319#ifdef CONFIG_KALLSYMS
320/*
321 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
322 * Returns the resolved symbol. If that fails, simply return the address.
323 */
324static int proc_pid_wchan(struct task_struct *task, char *buffer)
325{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700326 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700327 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 wchan = get_wchan(task);
330
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700331 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600332 if (!ptrace_may_access(task, PTRACE_MODE_READ))
333 return 0;
334 else
335 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700336 else
337 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339#endif /* CONFIG_KALLSYMS */
340
Ken Chen2ec220e2008-11-10 11:26:08 +0300341#ifdef CONFIG_STACKTRACE
342
343#define MAX_STACK_TRACE_DEPTH 64
344
345static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
346 struct pid *pid, struct task_struct *task)
347{
348 struct stack_trace trace;
349 unsigned long *entries;
350 int i;
351
352 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
353 if (!entries)
354 return -ENOMEM;
355
356 trace.nr_entries = 0;
357 trace.max_entries = MAX_STACK_TRACE_DEPTH;
358 trace.entries = entries;
359 trace.skip = 0;
360 save_stack_trace_tsk(task, &trace);
361
362 for (i = 0; i < trace.nr_entries; i++) {
363 seq_printf(m, "[<%p>] %pS\n",
364 (void *)entries[i], (void *)entries[i]);
365 }
366 kfree(entries);
367
368 return 0;
369}
370#endif
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372#ifdef CONFIG_SCHEDSTATS
373/*
374 * Provides /proc/PID/schedstat
375 */
376static int proc_pid_schedstat(struct task_struct *task, char *buffer)
377{
Balbir Singh172ba842007-07-09 18:52:00 +0200378 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100379 (unsigned long long)task->se.sum_exec_runtime,
380 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200381 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383#endif
384
Arjan van de Ven97455122008-01-25 21:08:34 +0100385#ifdef CONFIG_LATENCYTOP
386static int lstats_show_proc(struct seq_file *m, void *v)
387{
388 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800389 struct inode *inode = m->private;
390 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100391
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800392 if (!task)
393 return -ESRCH;
394 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100395 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800396 struct latency_record *lr = &task->latency_record[i];
397 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100398 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800399 seq_printf(m, "%i %li %li",
400 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100401 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800402 unsigned long bt = lr->backtrace[q];
403 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100404 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800405 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100406 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800407 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100408 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800409 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100410 }
411
412 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800413 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100414 return 0;
415}
416
417static int lstats_open(struct inode *inode, struct file *file)
418{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800419 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800420}
421
Arjan van de Ven97455122008-01-25 21:08:34 +0100422static ssize_t lstats_write(struct file *file, const char __user *buf,
423 size_t count, loff_t *offs)
424{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800425 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100426
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800427 if (!task)
428 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100429 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800430 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100431
432 return count;
433}
434
435static const struct file_operations proc_lstats_operations = {
436 .open = lstats_open,
437 .read = seq_read,
438 .write = lstats_write,
439 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800440 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100441};
442
443#endif
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static int proc_oom_score(struct task_struct *task, char *buffer)
446{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200447 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700449 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200450 if (pid_alive(task))
David Rientjesa63d83f2010-08-09 17:19:46 -0700451 points = oom_badness(task, NULL, NULL,
452 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700453 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return sprintf(buffer, "%lu\n", points);
455}
456
Neil Hormand85f50d2007-10-18 23:40:37 -0700457struct limit_names {
458 char *name;
459 char *unit;
460};
461
462static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700463 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700464 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
465 [RLIMIT_DATA] = {"Max data size", "bytes"},
466 [RLIMIT_STACK] = {"Max stack size", "bytes"},
467 [RLIMIT_CORE] = {"Max core file size", "bytes"},
468 [RLIMIT_RSS] = {"Max resident set", "bytes"},
469 [RLIMIT_NPROC] = {"Max processes", "processes"},
470 [RLIMIT_NOFILE] = {"Max open files", "files"},
471 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
472 [RLIMIT_AS] = {"Max address space", "bytes"},
473 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
474 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
475 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
476 [RLIMIT_NICE] = {"Max nice priority", NULL},
477 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800478 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700479};
480
481/* Display limits for a process */
482static int proc_pid_limits(struct task_struct *task, char *buffer)
483{
484 unsigned int i;
485 int count = 0;
486 unsigned long flags;
487 char *bufptr = buffer;
488
489 struct rlimit rlim[RLIM_NLIMITS];
490
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400491 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700492 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700493 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
494 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700495
496 /*
497 * print the file header
498 */
499 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
500 "Limit", "Soft Limit", "Hard Limit", "Units");
501
502 for (i = 0; i < RLIM_NLIMITS; i++) {
503 if (rlim[i].rlim_cur == RLIM_INFINITY)
504 count += sprintf(&bufptr[count], "%-25s %-20s ",
505 lnames[i].name, "unlimited");
506 else
507 count += sprintf(&bufptr[count], "%-25s %-20lu ",
508 lnames[i].name, rlim[i].rlim_cur);
509
510 if (rlim[i].rlim_max == RLIM_INFINITY)
511 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
512 else
513 count += sprintf(&bufptr[count], "%-20lu ",
514 rlim[i].rlim_max);
515
516 if (lnames[i].unit)
517 count += sprintf(&bufptr[count], "%-10s\n",
518 lnames[i].unit);
519 else
520 count += sprintf(&bufptr[count], "\n");
521 }
522
523 return count;
524}
525
Roland McGrathebcb6732008-07-25 19:46:00 -0700526#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
527static int proc_pid_syscall(struct task_struct *task, char *buffer)
528{
529 long nr;
530 unsigned long args[6], sp, pc;
531
532 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
533 return sprintf(buffer, "running\n");
534
535 if (nr < 0)
536 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
537
538 return sprintf(buffer,
539 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
540 nr,
541 args[0], args[1], args[2], args[3], args[4], args[5],
542 sp, pc);
543}
544#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/************************************************************************/
547/* Here the fs part begins */
548/************************************************************************/
549
550/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700551static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700553 struct task_struct *task;
554 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700555 /* Allow access to a task's file descriptors if it is us or we
556 * may use ptrace attach to the process and find out that
557 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700558 */
559 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700560 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400561 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700562 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700563 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700564 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700567static int proc_setattr(struct dentry *dentry, struct iattr *attr)
568{
569 int error;
570 struct inode *inode = dentry->d_inode;
571
572 if (attr->ia_valid & ATTR_MODE)
573 return -EPERM;
574
575 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200576 if (error)
577 return error;
578
579 if ((attr->ia_valid & ATTR_SIZE) &&
580 attr->ia_size != i_size_read(inode)) {
581 error = vmtruncate(inode, attr->ia_size);
582 if (error)
583 return error;
584 }
585
586 setattr_copy(inode, attr);
587 mark_inode_dirty(inode);
588 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700589}
590
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800591static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700592 .setattr = proc_setattr,
593};
594
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100595static int mounts_open_common(struct inode *inode, struct file *file,
596 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700598 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700599 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800600 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100601 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500602 struct proc_mounts *p;
603 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Eric W. Biederman99f89552006-06-26 00:25:55 -0700605 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700606 rcu_read_lock();
607 nsp = task_nsproxy(task);
608 if (nsp) {
609 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800610 if (ns)
611 get_mnt_ns(ns);
612 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700613 rcu_read_unlock();
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200614 if (ns && get_task_root(task, &root) == 0)
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000615 ret = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700616 put_task_struct(task);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100619 if (!ns)
620 goto err;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000621 if (ret)
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100622 goto err_put_ns;
623
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100624 ret = -ENOMEM;
625 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
626 if (!p)
627 goto err_put_path;
628
629 file->private_data = &p->m;
630 ret = seq_open(file, op);
631 if (ret)
632 goto err_free;
633
634 p->m.private = p;
635 p->ns = ns;
636 p->root = root;
637 p->event = ns->event;
638
639 return 0;
640
641 err_free:
642 kfree(p);
643 err_put_path:
644 path_put(&root);
645 err_put_ns:
646 put_mnt_ns(ns);
647 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return ret;
649}
650
651static int mounts_release(struct inode *inode, struct file *file)
652{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100653 struct proc_mounts *p = file->private_data;
654 path_put(&p->root);
655 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return seq_release(inode, file);
657}
658
Al Viro5addc5d2005-11-07 17:15:49 -0500659static unsigned mounts_poll(struct file *file, poll_table *wait)
660{
661 struct proc_mounts *p = file->private_data;
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900662 unsigned res = POLLIN | POLLRDNORM;
Al Viro5addc5d2005-11-07 17:15:49 -0500663
Al Viro9f5596a2010-02-05 00:40:25 -0500664 poll_wait(file, &p->ns->poll, wait);
665 if (mnt_had_events(p))
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900666 res |= POLLERR | POLLPRI;
Al Viro5addc5d2005-11-07 17:15:49 -0500667
668 return res;
669}
670
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100671static int mounts_open(struct inode *inode, struct file *file)
672{
673 return mounts_open_common(inode, file, &mounts_op);
674}
675
Arjan van de Ven00977a52007-02-12 00:55:34 -0800676static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 .open = mounts_open,
678 .read = seq_read,
679 .llseek = seq_lseek,
680 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500681 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682};
683
Ram Pai2d4d4862008-03-27 13:06:25 +0100684static int mountinfo_open(struct inode *inode, struct file *file)
685{
686 return mounts_open_common(inode, file, &mountinfo_op);
687}
688
689static const struct file_operations proc_mountinfo_operations = {
690 .open = mountinfo_open,
691 .read = seq_read,
692 .llseek = seq_lseek,
693 .release = mounts_release,
694 .poll = mounts_poll,
695};
696
Chuck Leverb4629fe2006-03-20 13:44:12 -0500697static int mountstats_open(struct inode *inode, struct file *file)
698{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100699 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500700}
701
Arjan van de Ven00977a52007-02-12 00:55:34 -0800702static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500703 .open = mountstats_open,
704 .read = seq_read,
705 .llseek = seq_lseek,
706 .release = mounts_release,
707};
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
710
711static ssize_t proc_info_read(struct file * file, char __user * buf,
712 size_t count, loff_t *ppos)
713{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800714 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unsigned long page;
716 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700717 struct task_struct *task = get_proc_task(inode);
718
719 length = -ESRCH;
720 if (!task)
721 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (count > PROC_BLOCK_SIZE)
724 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700725
726 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700727 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700728 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 length = PROC_I(inode)->op.proc_read(task, (char*)page);
731
732 if (length >= 0)
733 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
734 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700735out:
736 put_task_struct(task);
737out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return length;
739}
740
Arjan van de Ven00977a52007-02-12 00:55:34 -0800741static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100743 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744};
745
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800746static int proc_single_show(struct seq_file *m, void *v)
747{
748 struct inode *inode = m->private;
749 struct pid_namespace *ns;
750 struct pid *pid;
751 struct task_struct *task;
752 int ret;
753
754 ns = inode->i_sb->s_fs_info;
755 pid = proc_pid(inode);
756 task = get_pid_task(pid, PIDTYPE_PID);
757 if (!task)
758 return -ESRCH;
759
760 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
761
762 put_task_struct(task);
763 return ret;
764}
765
766static int proc_single_open(struct inode *inode, struct file *filp)
767{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800768 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800769}
770
771static const struct file_operations proc_single_file_operations = {
772 .open = proc_single_open,
773 .read = seq_read,
774 .llseek = seq_lseek,
775 .release = single_release,
776};
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static int mem_open(struct inode* inode, struct file* file)
779{
780 file->private_data = (void*)((long)current->self_exec_id);
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700781 /* OK to pass negative loff_t, we can catch out-of-range */
782 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784}
785
786static ssize_t mem_read(struct file * file, char __user * buf,
787 size_t count, loff_t *ppos)
788{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800789 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 char *page;
791 unsigned long src = *ppos;
792 int ret = -ESRCH;
793 struct mm_struct *mm;
794
Eric W. Biederman99f89552006-06-26 00:25:55 -0700795 if (!task)
796 goto out_no_task;
797
Roland McGrath638fa2022008-04-29 01:01:38 -0700798 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto out;
800
801 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700802 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (!page)
804 goto out;
805
806 ret = 0;
807
808 mm = get_task_mm(task);
809 if (!mm)
810 goto out_free;
811
812 ret = -EIO;
813
814 if (file->private_data != (void*)((long)current->self_exec_id))
815 goto out_put;
816
817 ret = 0;
818
819 while (count > 0) {
820 int this_len, retval;
821
822 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
823 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa2022008-04-29 01:01:38 -0700824 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!ret)
826 ret = -EIO;
827 break;
828 }
829
830 if (copy_to_user(buf, page, retval)) {
831 ret = -EFAULT;
832 break;
833 }
834
835 ret += retval;
836 src += retval;
837 buf += retval;
838 count -= retval;
839 }
840 *ppos = src;
841
842out_put:
843 mmput(mm);
844out_free:
845 free_page((unsigned long) page);
846out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700847 put_task_struct(task);
848out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return ret;
850}
851
852#define mem_write NULL
853
854#ifndef mem_write
855/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800856static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 size_t count, loff_t *ppos)
858{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700859 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800861 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 unsigned long dst = *ppos;
863
Eric W. Biederman99f89552006-06-26 00:25:55 -0700864 copied = -ESRCH;
865 if (!task)
866 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Roland McGrath638fa2022008-04-29 01:01:38 -0700868 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700869 goto out;
870
Stephen Wilson26947f82011-03-13 15:49:21 -0400871 copied = -EIO;
872 if (file->private_data != (void *)((long)current->self_exec_id))
873 goto out;
874
Eric W. Biederman99f89552006-06-26 00:25:55 -0700875 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700876 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700878 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700880 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 while (count > 0) {
882 int this_len, retval;
883
884 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
885 if (copy_from_user(page, buf, this_len)) {
886 copied = -EFAULT;
887 break;
888 }
889 retval = access_process_vm(task, dst, page, this_len, 1);
890 if (!retval) {
891 if (!copied)
892 copied = -EIO;
893 break;
894 }
895 copied += retval;
896 buf += retval;
897 dst += retval;
898 count -= retval;
899 }
900 *ppos = dst;
901 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700902out:
903 put_task_struct(task);
904out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return copied;
906}
907#endif
908
Matt Mackall85863e42008-02-04 22:29:04 -0800909loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 switch (orig) {
912 case 0:
913 file->f_pos = offset;
914 break;
915 case 1:
916 file->f_pos += offset;
917 break;
918 default:
919 return -EINVAL;
920 }
921 force_successful_syscall_return();
922 return file->f_pos;
923}
924
Arjan van de Ven00977a52007-02-12 00:55:34 -0800925static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 .llseek = mem_lseek,
927 .read = mem_read,
928 .write = mem_write,
929 .open = mem_open,
930};
931
James Pearson315e28c2007-10-16 23:30:17 -0700932static ssize_t environ_read(struct file *file, char __user *buf,
933 size_t count, loff_t *ppos)
934{
935 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
936 char *page;
937 unsigned long src = *ppos;
938 int ret = -ESRCH;
939 struct mm_struct *mm;
940
941 if (!task)
942 goto out_no_task;
943
James Pearson315e28c2007-10-16 23:30:17 -0700944 ret = -ENOMEM;
945 page = (char *)__get_free_page(GFP_TEMPORARY);
946 if (!page)
947 goto out;
948
James Pearson315e28c2007-10-16 23:30:17 -0700949
Al Virod6f64b82011-02-15 22:26:01 -0500950 mm = mm_for_maps(task);
951 ret = PTR_ERR(mm);
952 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700953 goto out_free;
954
Al Virod6f64b82011-02-15 22:26:01 -0500955 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -0700956 while (count > 0) {
957 int this_len, retval, max_len;
958
959 this_len = mm->env_end - (mm->env_start + src);
960
961 if (this_len <= 0)
962 break;
963
964 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
965 this_len = (this_len > max_len) ? max_len : this_len;
966
967 retval = access_process_vm(task, (mm->env_start + src),
968 page, this_len, 0);
969
970 if (retval <= 0) {
971 ret = retval;
972 break;
973 }
974
975 if (copy_to_user(buf, page, retval)) {
976 ret = -EFAULT;
977 break;
978 }
979
980 ret += retval;
981 src += retval;
982 buf += retval;
983 count -= retval;
984 }
985 *ppos = src;
986
987 mmput(mm);
988out_free:
989 free_page((unsigned long) page);
990out:
991 put_task_struct(task);
992out_no_task:
993 return ret;
994}
995
996static const struct file_operations proc_environ_operations = {
997 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100998 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700999};
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1002 size_t count, loff_t *ppos)
1003{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001004 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001005 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001007 int oom_adjust = OOM_DISABLE;
1008 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Eric W. Biederman99f89552006-06-26 00:25:55 -07001010 if (!task)
1011 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001012
1013 if (lock_task_sighand(task, &flags)) {
1014 oom_adjust = task->signal->oom_adj;
1015 unlock_task_sighand(task, &flags);
1016 }
1017
Eric W. Biederman99f89552006-06-26 00:25:55 -07001018 put_task_struct(task);
1019
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001020 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001021
1022 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1026 size_t count, loff_t *ppos)
1027{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001028 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001029 char buffer[PROC_NUMBUF];
1030 long oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001031 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001032 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001034 memset(buffer, 0, sizeof(buffer));
1035 if (count > sizeof(buffer) - 1)
1036 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001037 if (copy_from_user(buffer, buf, count)) {
1038 err = -EFAULT;
1039 goto out;
1040 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001041
1042 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1043 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001044 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001045 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -07001046 oom_adjust != OOM_DISABLE) {
1047 err = -EINVAL;
1048 goto out;
1049 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001050
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001051 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001052 if (!task) {
1053 err = -ESRCH;
1054 goto out;
1055 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001056
Ying Han3d5992d2010-10-26 14:21:23 -07001057 task_lock(task);
1058 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001059 err = -EINVAL;
1060 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001061 }
1062
David Rientjesd19d5472010-10-26 14:21:26 -07001063 if (!lock_task_sighand(task, &flags)) {
1064 err = -ESRCH;
1065 goto err_task_lock;
1066 }
1067
1068 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1069 err = -EACCES;
1070 goto err_sighand;
1071 }
1072
Ying Han3d5992d2010-10-26 14:21:23 -07001073 if (oom_adjust != task->signal->oom_adj) {
1074 if (oom_adjust == OOM_DISABLE)
1075 atomic_inc(&task->mm->oom_disable_count);
1076 if (task->signal->oom_adj == OOM_DISABLE)
1077 atomic_dec(&task->mm->oom_disable_count);
1078 }
1079
David Rientjes51b1bd22010-08-09 17:19:47 -07001080 /*
1081 * Warn that /proc/pid/oom_adj is deprecated, see
1082 * Documentation/feature-removal-schedule.txt.
1083 */
1084 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1085 "please use /proc/%d/oom_score_adj instead.\n",
1086 current->comm, task_pid_nr(current),
1087 task_pid_nr(task), task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001088 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -07001089 /*
1090 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1091 * value is always attainable.
1092 */
1093 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1094 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1095 else
1096 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1097 -OOM_DISABLE;
David Rientjes723548b2010-10-26 14:21:25 -07001098err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001099 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001100err_task_lock:
1101 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001102 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001103out:
1104 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Arjan van de Ven00977a52007-02-12 00:55:34 -08001107static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 .read = oom_adjust_read,
1109 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001110 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111};
1112
David Rientjesa63d83f2010-08-09 17:19:46 -07001113static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1114 size_t count, loff_t *ppos)
1115{
1116 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1117 char buffer[PROC_NUMBUF];
1118 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1119 unsigned long flags;
1120 size_t len;
1121
1122 if (!task)
1123 return -ESRCH;
1124 if (lock_task_sighand(task, &flags)) {
1125 oom_score_adj = task->signal->oom_score_adj;
1126 unlock_task_sighand(task, &flags);
1127 }
1128 put_task_struct(task);
1129 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1130 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1131}
1132
1133static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1134 size_t count, loff_t *ppos)
1135{
1136 struct task_struct *task;
1137 char buffer[PROC_NUMBUF];
1138 unsigned long flags;
1139 long oom_score_adj;
1140 int err;
1141
1142 memset(buffer, 0, sizeof(buffer));
1143 if (count > sizeof(buffer) - 1)
1144 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001145 if (copy_from_user(buffer, buf, count)) {
1146 err = -EFAULT;
1147 goto out;
1148 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001149
1150 err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1151 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001152 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001153 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001154 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1155 err = -EINVAL;
1156 goto out;
1157 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001158
1159 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001160 if (!task) {
1161 err = -ESRCH;
1162 goto out;
1163 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001164
Ying Han3d5992d2010-10-26 14:21:23 -07001165 task_lock(task);
1166 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001167 err = -EINVAL;
1168 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001169 }
David Rientjesd19d5472010-10-26 14:21:26 -07001170
1171 if (!lock_task_sighand(task, &flags)) {
1172 err = -ESRCH;
1173 goto err_task_lock;
1174 }
1175
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001176 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001177 !capable(CAP_SYS_RESOURCE)) {
1178 err = -EACCES;
1179 goto err_sighand;
1180 }
1181
Ying Han3d5992d2010-10-26 14:21:23 -07001182 if (oom_score_adj != task->signal->oom_score_adj) {
1183 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1184 atomic_inc(&task->mm->oom_disable_count);
1185 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1186 atomic_dec(&task->mm->oom_disable_count);
1187 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001188 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001189 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1190 task->signal->oom_score_adj_min = oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001191 /*
1192 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1193 * always attainable.
1194 */
1195 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1196 task->signal->oom_adj = OOM_DISABLE;
1197 else
1198 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1199 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001200err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001201 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001202err_task_lock:
1203 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001204 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001205out:
1206 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001207}
1208
1209static const struct file_operations proc_oom_score_adj_operations = {
1210 .read = oom_score_adj_read,
1211 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001212 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001213};
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215#ifdef CONFIG_AUDITSYSCALL
1216#define TMPBUFLEN 21
1217static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1218 size_t count, loff_t *ppos)
1219{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001220 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001221 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 ssize_t length;
1223 char tmpbuf[TMPBUFLEN];
1224
Eric W. Biederman99f89552006-06-26 00:25:55 -07001225 if (!task)
1226 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001228 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001229 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1231}
1232
1233static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1234 size_t count, loff_t *ppos)
1235{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001236 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 char *page, *tmp;
1238 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 uid_t loginuid;
1240
1241 if (!capable(CAP_AUDIT_CONTROL))
1242 return -EPERM;
1243
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001244 rcu_read_lock();
1245 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1246 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001248 }
1249 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Al Viroe0182902006-05-18 08:28:02 -04001251 if (count >= PAGE_SIZE)
1252 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (*ppos != 0) {
1255 /* No partial writes. */
1256 return -EINVAL;
1257 }
Mel Gormane12ba742007-10-16 01:25:52 -07001258 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (!page)
1260 return -ENOMEM;
1261 length = -EFAULT;
1262 if (copy_from_user(page, buf, count))
1263 goto out_free_page;
1264
Al Viroe0182902006-05-18 08:28:02 -04001265 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 loginuid = simple_strtoul(page, &tmp, 10);
1267 if (tmp == page) {
1268 length = -EINVAL;
1269 goto out_free_page;
1270
1271 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001272 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (likely(length == 0))
1274 length = count;
1275
1276out_free_page:
1277 free_page((unsigned long) page);
1278 return length;
1279}
1280
Arjan van de Ven00977a52007-02-12 00:55:34 -08001281static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 .read = proc_loginuid_read,
1283 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001284 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285};
Eric Paris1e0bd752008-03-13 08:15:31 -04001286
1287static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1288 size_t count, loff_t *ppos)
1289{
1290 struct inode * inode = file->f_path.dentry->d_inode;
1291 struct task_struct *task = get_proc_task(inode);
1292 ssize_t length;
1293 char tmpbuf[TMPBUFLEN];
1294
1295 if (!task)
1296 return -ESRCH;
1297 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1298 audit_get_sessionid(task));
1299 put_task_struct(task);
1300 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1301}
1302
1303static const struct file_operations proc_sessionid_operations = {
1304 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001305 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001306};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307#endif
1308
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001309#ifdef CONFIG_FAULT_INJECTION
1310static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1311 size_t count, loff_t *ppos)
1312{
1313 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1314 char buffer[PROC_NUMBUF];
1315 size_t len;
1316 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001317
1318 if (!task)
1319 return -ESRCH;
1320 make_it_fail = task->make_it_fail;
1321 put_task_struct(task);
1322
1323 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001324
1325 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001326}
1327
1328static ssize_t proc_fault_inject_write(struct file * file,
1329 const char __user * buf, size_t count, loff_t *ppos)
1330{
1331 struct task_struct *task;
1332 char buffer[PROC_NUMBUF], *end;
1333 int make_it_fail;
1334
1335 if (!capable(CAP_SYS_RESOURCE))
1336 return -EPERM;
1337 memset(buffer, 0, sizeof(buffer));
1338 if (count > sizeof(buffer) - 1)
1339 count = sizeof(buffer) - 1;
1340 if (copy_from_user(buffer, buf, count))
1341 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001342 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1343 if (*end)
1344 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001345 task = get_proc_task(file->f_dentry->d_inode);
1346 if (!task)
1347 return -ESRCH;
1348 task->make_it_fail = make_it_fail;
1349 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001350
1351 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001352}
1353
Arjan van de Ven00977a52007-02-12 00:55:34 -08001354static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001355 .read = proc_fault_inject_read,
1356 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001357 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001358};
1359#endif
1360
Arjan van de Ven97455122008-01-25 21:08:34 +01001361
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001362#ifdef CONFIG_SCHED_DEBUG
1363/*
1364 * Print out various scheduling related per-task fields:
1365 */
1366static int sched_show(struct seq_file *m, void *v)
1367{
1368 struct inode *inode = m->private;
1369 struct task_struct *p;
1370
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001371 p = get_proc_task(inode);
1372 if (!p)
1373 return -ESRCH;
1374 proc_sched_show_task(p, m);
1375
1376 put_task_struct(p);
1377
1378 return 0;
1379}
1380
1381static ssize_t
1382sched_write(struct file *file, const char __user *buf,
1383 size_t count, loff_t *offset)
1384{
1385 struct inode *inode = file->f_path.dentry->d_inode;
1386 struct task_struct *p;
1387
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001388 p = get_proc_task(inode);
1389 if (!p)
1390 return -ESRCH;
1391 proc_sched_set_task(p);
1392
1393 put_task_struct(p);
1394
1395 return count;
1396}
1397
1398static int sched_open(struct inode *inode, struct file *filp)
1399{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001400 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001401}
1402
1403static const struct file_operations proc_pid_sched_operations = {
1404 .open = sched_open,
1405 .read = seq_read,
1406 .write = sched_write,
1407 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001408 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001409};
1410
1411#endif
1412
Mike Galbraith5091faa2010-11-30 14:18:03 +01001413#ifdef CONFIG_SCHED_AUTOGROUP
1414/*
1415 * Print out autogroup related information:
1416 */
1417static int sched_autogroup_show(struct seq_file *m, void *v)
1418{
1419 struct inode *inode = m->private;
1420 struct task_struct *p;
1421
1422 p = get_proc_task(inode);
1423 if (!p)
1424 return -ESRCH;
1425 proc_sched_autogroup_show_task(p, m);
1426
1427 put_task_struct(p);
1428
1429 return 0;
1430}
1431
1432static ssize_t
1433sched_autogroup_write(struct file *file, const char __user *buf,
1434 size_t count, loff_t *offset)
1435{
1436 struct inode *inode = file->f_path.dentry->d_inode;
1437 struct task_struct *p;
1438 char buffer[PROC_NUMBUF];
1439 long nice;
1440 int err;
1441
1442 memset(buffer, 0, sizeof(buffer));
1443 if (count > sizeof(buffer) - 1)
1444 count = sizeof(buffer) - 1;
1445 if (copy_from_user(buffer, buf, count))
1446 return -EFAULT;
1447
1448 err = strict_strtol(strstrip(buffer), 0, &nice);
1449 if (err)
1450 return -EINVAL;
1451
1452 p = get_proc_task(inode);
1453 if (!p)
1454 return -ESRCH;
1455
1456 err = nice;
1457 err = proc_sched_autogroup_set_nice(p, &err);
1458 if (err)
1459 count = err;
1460
1461 put_task_struct(p);
1462
1463 return count;
1464}
1465
1466static int sched_autogroup_open(struct inode *inode, struct file *filp)
1467{
1468 int ret;
1469
1470 ret = single_open(filp, sched_autogroup_show, NULL);
1471 if (!ret) {
1472 struct seq_file *m = filp->private_data;
1473
1474 m->private = inode;
1475 }
1476 return ret;
1477}
1478
1479static const struct file_operations proc_pid_sched_autogroup_operations = {
1480 .open = sched_autogroup_open,
1481 .read = seq_read,
1482 .write = sched_autogroup_write,
1483 .llseek = seq_lseek,
1484 .release = single_release,
1485};
1486
1487#endif /* CONFIG_SCHED_AUTOGROUP */
1488
john stultz4614a696b2009-12-14 18:00:05 -08001489static ssize_t comm_write(struct file *file, const char __user *buf,
1490 size_t count, loff_t *offset)
1491{
1492 struct inode *inode = file->f_path.dentry->d_inode;
1493 struct task_struct *p;
1494 char buffer[TASK_COMM_LEN];
1495
1496 memset(buffer, 0, sizeof(buffer));
1497 if (count > sizeof(buffer) - 1)
1498 count = sizeof(buffer) - 1;
1499 if (copy_from_user(buffer, buf, count))
1500 return -EFAULT;
1501
1502 p = get_proc_task(inode);
1503 if (!p)
1504 return -ESRCH;
1505
1506 if (same_thread_group(current, p))
1507 set_task_comm(p, buffer);
1508 else
1509 count = -EINVAL;
1510
1511 put_task_struct(p);
1512
1513 return count;
1514}
1515
1516static int comm_show(struct seq_file *m, void *v)
1517{
1518 struct inode *inode = m->private;
1519 struct task_struct *p;
1520
1521 p = get_proc_task(inode);
1522 if (!p)
1523 return -ESRCH;
1524
1525 task_lock(p);
1526 seq_printf(m, "%s\n", p->comm);
1527 task_unlock(p);
1528
1529 put_task_struct(p);
1530
1531 return 0;
1532}
1533
1534static int comm_open(struct inode *inode, struct file *filp)
1535{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001536 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001537}
1538
1539static const struct file_operations proc_pid_set_comm_operations = {
1540 .open = comm_open,
1541 .read = seq_read,
1542 .write = comm_write,
1543 .llseek = seq_lseek,
1544 .release = single_release,
1545};
1546
Matt Helsley925d1c42008-04-29 01:01:36 -07001547/*
1548 * We added or removed a vma mapping the executable. The vmas are only mapped
1549 * during exec and are not mapped with the mmap system call.
1550 * Callers must hold down_write() on the mm's mmap_sem for these
1551 */
1552void added_exe_file_vma(struct mm_struct *mm)
1553{
1554 mm->num_exe_file_vmas++;
1555}
1556
1557void removed_exe_file_vma(struct mm_struct *mm)
1558{
1559 mm->num_exe_file_vmas--;
1560 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1561 fput(mm->exe_file);
1562 mm->exe_file = NULL;
1563 }
1564
1565}
1566
1567void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1568{
1569 if (new_exe_file)
1570 get_file(new_exe_file);
1571 if (mm->exe_file)
1572 fput(mm->exe_file);
1573 mm->exe_file = new_exe_file;
1574 mm->num_exe_file_vmas = 0;
1575}
1576
1577struct file *get_mm_exe_file(struct mm_struct *mm)
1578{
1579 struct file *exe_file;
1580
1581 /* We need mmap_sem to protect against races with removal of
1582 * VM_EXECUTABLE vmas */
1583 down_read(&mm->mmap_sem);
1584 exe_file = mm->exe_file;
1585 if (exe_file)
1586 get_file(exe_file);
1587 up_read(&mm->mmap_sem);
1588 return exe_file;
1589}
1590
1591void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1592{
1593 /* It's safe to write the exe_file pointer without exe_file_lock because
1594 * this is called during fork when the task is not yet in /proc */
1595 newmm->exe_file = get_mm_exe_file(oldmm);
1596}
1597
1598static int proc_exe_link(struct inode *inode, struct path *exe_path)
1599{
1600 struct task_struct *task;
1601 struct mm_struct *mm;
1602 struct file *exe_file;
1603
1604 task = get_proc_task(inode);
1605 if (!task)
1606 return -ENOENT;
1607 mm = get_task_mm(task);
1608 put_task_struct(task);
1609 if (!mm)
1610 return -ENOENT;
1611 exe_file = get_mm_exe_file(mm);
1612 mmput(mm);
1613 if (exe_file) {
1614 *exe_path = exe_file->f_path;
1615 path_get(&exe_file->f_path);
1616 fput(exe_file);
1617 return 0;
1618 } else
1619 return -ENOENT;
1620}
1621
Al Viro008b1502005-08-20 00:17:39 +01001622static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 struct inode *inode = dentry->d_inode;
1625 int error = -EACCES;
1626
1627 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001628 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Eric W. Biederman778c1142006-06-26 00:25:58 -07001630 /* Are we allowed to snoop on the tasks file descriptors? */
1631 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001634 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635out:
Al Viro008b1502005-08-20 00:17:39 +01001636 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001639static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Mel Gormane12ba742007-10-16 01:25:52 -07001641 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001642 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int len;
1644
1645 if (!tmp)
1646 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001647
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001648 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001649 len = PTR_ERR(pathname);
1650 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001652 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 if (len > buflen)
1655 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001656 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 len = -EFAULT;
1658 out:
1659 free_page((unsigned long)tmp);
1660 return len;
1661}
1662
1663static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1664{
1665 int error = -EACCES;
1666 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001667 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Eric W. Biederman778c1142006-06-26 00:25:58 -07001669 /* Are we allowed to snoop on the tasks file descriptors? */
1670 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001673 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (error)
1675 goto out;
1676
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001677 error = do_proc_readlink(&path, buffer, buflen);
1678 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return error;
1681}
1682
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001683static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001685 .follow_link = proc_pid_follow_link,
1686 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687};
1688
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001689
1690/* building an inode */
1691
1692static int task_dumpable(struct task_struct *task)
1693{
1694 int dumpable = 0;
1695 struct mm_struct *mm;
1696
1697 task_lock(task);
1698 mm = task->mm;
1699 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001700 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001701 task_unlock(task);
1702 if(dumpable == 1)
1703 return 1;
1704 return 0;
1705}
1706
1707
Eric W. Biederman61a28782006-10-02 02:18:49 -07001708static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001709{
1710 struct inode * inode;
1711 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001712 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001713
1714 /* We need a new inode */
1715
1716 inode = new_inode(sb);
1717 if (!inode)
1718 goto out;
1719
1720 /* Common stuff */
1721 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001722 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001723 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001724 inode->i_op = &proc_def_inode_operations;
1725
1726 /*
1727 * grab the reference to task.
1728 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001729 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001730 if (!ei->pid)
1731 goto out_unlock;
1732
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001733 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001734 rcu_read_lock();
1735 cred = __task_cred(task);
1736 inode->i_uid = cred->euid;
1737 inode->i_gid = cred->egid;
1738 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001739 }
1740 security_task_to_inode(task, inode);
1741
1742out:
1743 return inode;
1744
1745out_unlock:
1746 iput(inode);
1747 return NULL;
1748}
1749
1750static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1751{
1752 struct inode *inode = dentry->d_inode;
1753 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001754 const struct cred *cred;
1755
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001756 generic_fillattr(inode, stat);
1757
1758 rcu_read_lock();
1759 stat->uid = 0;
1760 stat->gid = 0;
1761 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1762 if (task) {
1763 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1764 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001765 cred = __task_cred(task);
1766 stat->uid = cred->euid;
1767 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768 }
1769 }
1770 rcu_read_unlock();
1771 return 0;
1772}
1773
1774/* dentry stuff */
1775
1776/*
1777 * Exceptional case: normally we are not allowed to unhash a busy
1778 * directory. In this case, however, we can do it - no aliasing problems
1779 * due to the way we treat inodes.
1780 *
1781 * Rewrite the inode's ownerships here because the owning task may have
1782 * performed a setuid(), etc.
1783 *
1784 * Before the /proc/pid/status file was created the only way to read
1785 * the effective uid of a /process was to stat /proc/pid. Reading
1786 * /proc/pid/status is slow enough that procps and other packages
1787 * kept stating /proc/pid. To keep the rules in /proc simple I have
1788 * made this apply to all per process world readable and executable
1789 * directories.
1790 */
1791static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1792{
Nick Piggin34286d62011-01-07 17:49:57 +11001793 struct inode *inode;
1794 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001795 const struct cred *cred;
1796
Nick Piggin34286d62011-01-07 17:49:57 +11001797 if (nd && nd->flags & LOOKUP_RCU)
1798 return -ECHILD;
1799
1800 inode = dentry->d_inode;
1801 task = get_proc_task(inode);
1802
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001803 if (task) {
1804 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1805 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001806 rcu_read_lock();
1807 cred = __task_cred(task);
1808 inode->i_uid = cred->euid;
1809 inode->i_gid = cred->egid;
1810 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001811 } else {
1812 inode->i_uid = 0;
1813 inode->i_gid = 0;
1814 }
1815 inode->i_mode &= ~(S_ISUID | S_ISGID);
1816 security_task_to_inode(task, inode);
1817 put_task_struct(task);
1818 return 1;
1819 }
1820 d_drop(dentry);
1821 return 0;
1822}
1823
Nick Pigginfe15ce42011-01-07 17:49:23 +11001824static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001825{
1826 /* Is the task we represent dead?
1827 * If so, then don't put the dentry on the lru list,
1828 * kill it immediately.
1829 */
1830 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1831}
1832
Al Virod72f71e2009-02-20 05:58:47 +00001833static const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001834{
1835 .d_revalidate = pid_revalidate,
1836 .d_delete = pid_delete_dentry,
1837};
1838
1839/* Lookups */
1840
Eric Dumazetc5141e62007-05-08 00:26:15 -07001841typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1842 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001843
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001844/*
1845 * Fill a directory entry.
1846 *
1847 * If possible create the dcache entry and derive our inode number and
1848 * file type from dcache entry.
1849 *
1850 * Since all of the proc inode numbers are dynamically generated, the inode
1851 * numbers do not exist until the inode is cache. This means creating the
1852 * the dcache entry in readdir is necessary to keep the inode numbers
1853 * reported by readdir in sync with the inode numbers reported
1854 * by stat.
1855 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001856static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1857 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001858 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001859{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001860 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001861 struct inode *inode;
1862 struct qstr qname;
1863 ino_t ino = 0;
1864 unsigned type = DT_UNKNOWN;
1865
1866 qname.name = name;
1867 qname.len = len;
1868 qname.hash = full_name_hash(name, len);
1869
1870 child = d_lookup(dir, &qname);
1871 if (!child) {
1872 struct dentry *new;
1873 new = d_alloc(dir, &qname);
1874 if (new) {
1875 child = instantiate(dir->d_inode, new, task, ptr);
1876 if (child)
1877 dput(new);
1878 else
1879 child = new;
1880 }
1881 }
1882 if (!child || IS_ERR(child) || !child->d_inode)
1883 goto end_instantiate;
1884 inode = child->d_inode;
1885 if (inode) {
1886 ino = inode->i_ino;
1887 type = inode->i_mode >> 12;
1888 }
1889 dput(child);
1890end_instantiate:
1891 if (!ino)
1892 ino = find_inode_number(dir, &qname);
1893 if (!ino)
1894 ino = 1;
1895 return filldir(dirent, name, len, filp->f_pos, ino, type);
1896}
1897
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001898static unsigned name_to_int(struct dentry *dentry)
1899{
1900 const char *name = dentry->d_name.name;
1901 int len = dentry->d_name.len;
1902 unsigned n = 0;
1903
1904 if (len > 1 && *name == '0')
1905 goto out;
1906 while (len-- > 0) {
1907 unsigned c = *name++ - '0';
1908 if (c > 9)
1909 goto out;
1910 if (n >= (~0U-9)/10)
1911 goto out;
1912 n *= 10;
1913 n += c;
1914 }
1915 return n;
1916out:
1917 return ~0U;
1918}
1919
Miklos Szeredi27932742007-05-08 00:26:17 -07001920#define PROC_FDINFO_MAX 64
1921
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001922static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001923{
1924 struct task_struct *task = get_proc_task(inode);
1925 struct files_struct *files = NULL;
1926 struct file *file;
1927 int fd = proc_fd(inode);
1928
1929 if (task) {
1930 files = get_files_struct(task);
1931 put_task_struct(task);
1932 }
1933 if (files) {
1934 /*
1935 * We are not taking a ref to the file structure, so we must
1936 * hold ->file_lock.
1937 */
1938 spin_lock(&files->file_lock);
1939 file = fcheck_files(files, fd);
1940 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001941 if (path) {
1942 *path = file->f_path;
1943 path_get(&file->f_path);
1944 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001945 if (info)
1946 snprintf(info, PROC_FDINFO_MAX,
1947 "pos:\t%lli\n"
1948 "flags:\t0%o\n",
1949 (long long) file->f_pos,
1950 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001951 spin_unlock(&files->file_lock);
1952 put_files_struct(files);
1953 return 0;
1954 }
1955 spin_unlock(&files->file_lock);
1956 put_files_struct(files);
1957 }
1958 return -ENOENT;
1959}
1960
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001961static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001962{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001963 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001964}
1965
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001966static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1967{
Nick Piggin34286d62011-01-07 17:49:57 +11001968 struct inode *inode;
1969 struct task_struct *task;
1970 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001971 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001972 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001973
Nick Piggin34286d62011-01-07 17:49:57 +11001974 if (nd && nd->flags & LOOKUP_RCU)
1975 return -ECHILD;
1976
1977 inode = dentry->d_inode;
1978 task = get_proc_task(inode);
1979 fd = proc_fd(inode);
1980
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001981 if (task) {
1982 files = get_files_struct(task);
1983 if (files) {
1984 rcu_read_lock();
1985 if (fcheck_files(files, fd)) {
1986 rcu_read_unlock();
1987 put_files_struct(files);
1988 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001989 rcu_read_lock();
1990 cred = __task_cred(task);
1991 inode->i_uid = cred->euid;
1992 inode->i_gid = cred->egid;
1993 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001994 } else {
1995 inode->i_uid = 0;
1996 inode->i_gid = 0;
1997 }
1998 inode->i_mode &= ~(S_ISUID | S_ISGID);
1999 security_task_to_inode(task, inode);
2000 put_task_struct(task);
2001 return 1;
2002 }
2003 rcu_read_unlock();
2004 put_files_struct(files);
2005 }
2006 put_task_struct(task);
2007 }
2008 d_drop(dentry);
2009 return 0;
2010}
2011
Al Virod72f71e2009-02-20 05:58:47 +00002012static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002013{
2014 .d_revalidate = tid_fd_revalidate,
2015 .d_delete = pid_delete_dentry,
2016};
2017
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002018static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002019 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002020{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002021 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002022 struct file *file;
2023 struct files_struct *files;
2024 struct inode *inode;
2025 struct proc_inode *ei;
2026 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002027
Eric W. Biederman61a28782006-10-02 02:18:49 -07002028 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002029 if (!inode)
2030 goto out;
2031 ei = PROC_I(inode);
2032 ei->fd = fd;
2033 files = get_files_struct(task);
2034 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002035 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002036 inode->i_mode = S_IFLNK;
2037
2038 /*
2039 * We are not taking a ref to the file structure, so we must
2040 * hold ->file_lock.
2041 */
2042 spin_lock(&files->file_lock);
2043 file = fcheck_files(files, fd);
2044 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002045 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04002046 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002047 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04002048 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002049 inode->i_mode |= S_IWUSR | S_IXUSR;
2050 spin_unlock(&files->file_lock);
2051 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002052
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002053 inode->i_op = &proc_pid_link_inode_operations;
2054 inode->i_size = 64;
2055 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002056 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002057 d_add(dentry, inode);
2058 /* Close the race of the process dying before we return the dentry */
2059 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002060 error = NULL;
2061
2062 out:
2063 return error;
2064out_unlock:
2065 spin_unlock(&files->file_lock);
2066 put_files_struct(files);
2067out_iput:
2068 iput(inode);
2069 goto out;
2070}
2071
Miklos Szeredi27932742007-05-08 00:26:17 -07002072static struct dentry *proc_lookupfd_common(struct inode *dir,
2073 struct dentry *dentry,
2074 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002075{
2076 struct task_struct *task = get_proc_task(dir);
2077 unsigned fd = name_to_int(dentry);
2078 struct dentry *result = ERR_PTR(-ENOENT);
2079
2080 if (!task)
2081 goto out_no_task;
2082 if (fd == ~0U)
2083 goto out;
2084
Miklos Szeredi27932742007-05-08 00:26:17 -07002085 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002086out:
2087 put_task_struct(task);
2088out_no_task:
2089 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002090}
2091
Miklos Szeredi27932742007-05-08 00:26:17 -07002092static int proc_readfd_common(struct file * filp, void * dirent,
2093 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002095 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07002096 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002097 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07002098 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct files_struct * files;
2101
2102 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002103 if (!p)
2104 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 fd = filp->f_pos;
2108 switch (fd) {
2109 case 0:
2110 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2111 goto out;
2112 filp->f_pos++;
2113 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07002114 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2116 goto out;
2117 filp->f_pos++;
2118 default:
2119 files = get_files_struct(p);
2120 if (!files)
2121 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002122 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04002124 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07002126 char name[PROC_NUMBUF];
2127 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 if (!fcheck_files(files, fd))
2130 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002131 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Miklos Szeredi27932742007-05-08 00:26:17 -07002133 len = snprintf(name, sizeof(name), "%d", fd);
2134 if (proc_fill_cache(filp, dirent, filldir,
2135 name, len, instantiate,
2136 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07002137 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 break;
2139 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002140 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002142 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 put_files_struct(files);
2144 }
2145out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002146 put_task_struct(p);
2147out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return retval;
2149}
2150
Miklos Szeredi27932742007-05-08 00:26:17 -07002151static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2152 struct nameidata *nd)
2153{
2154 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2155}
2156
2157static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2158{
2159 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2160}
2161
2162static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2163 size_t len, loff_t *ppos)
2164{
2165 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002166 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002167 if (!err)
2168 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2169 return err;
2170}
2171
2172static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002173 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002174 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002175 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002176};
2177
Arjan van de Ven00977a52007-02-12 00:55:34 -08002178static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 .read = generic_read_dir,
2180 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002181 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182};
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002185 * /proc/pid/fd needs a special permission handler so that a process can still
2186 * access /proc/self/fd after it has executed a setuid().
2187 */
Nick Pigginb74c79e2011-01-07 17:49:58 +11002188static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002189{
2190 int rv;
2191
Nick Pigginb74c79e2011-01-07 17:49:58 +11002192 if (flags & IPERM_FLAG_RCU)
2193 return -ECHILD;
2194 rv = generic_permission(inode, mask, flags, NULL);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002195 if (rv == 0)
2196 return 0;
2197 if (task_pid(current) == proc_pid(inode))
2198 rv = 0;
2199 return rv;
2200}
2201
2202/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 * proc directories can do almost nothing..
2204 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002205static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002207 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002208 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209};
2210
Miklos Szeredi27932742007-05-08 00:26:17 -07002211static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2212 struct dentry *dentry, struct task_struct *task, const void *ptr)
2213{
2214 unsigned fd = *(unsigned *)ptr;
2215 struct inode *inode;
2216 struct proc_inode *ei;
2217 struct dentry *error = ERR_PTR(-ENOENT);
2218
2219 inode = proc_pid_make_inode(dir->i_sb, task);
2220 if (!inode)
2221 goto out;
2222 ei = PROC_I(inode);
2223 ei->fd = fd;
2224 inode->i_mode = S_IFREG | S_IRUSR;
2225 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002226 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002227 d_add(dentry, inode);
2228 /* Close the race of the process dying before we return the dentry */
2229 if (tid_fd_revalidate(dentry, NULL))
2230 error = NULL;
2231
2232 out:
2233 return error;
2234}
2235
2236static struct dentry *proc_lookupfdinfo(struct inode *dir,
2237 struct dentry *dentry,
2238 struct nameidata *nd)
2239{
2240 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2241}
2242
2243static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2244{
2245 return proc_readfd_common(filp, dirent, filldir,
2246 proc_fdinfo_instantiate);
2247}
2248
2249static const struct file_operations proc_fdinfo_operations = {
2250 .read = generic_read_dir,
2251 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002252 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002253};
2254
2255/*
2256 * proc directories can do almost nothing..
2257 */
2258static const struct inode_operations proc_fdinfo_inode_operations = {
2259 .lookup = proc_lookupfdinfo,
2260 .setattr = proc_setattr,
2261};
2262
2263
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002264static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002265 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002266{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002267 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002268 struct inode *inode;
2269 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002270 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002271
Eric W. Biederman61a28782006-10-02 02:18:49 -07002272 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002273 if (!inode)
2274 goto out;
2275
2276 ei = PROC_I(inode);
2277 inode->i_mode = p->mode;
2278 if (S_ISDIR(inode->i_mode))
2279 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2280 if (p->iop)
2281 inode->i_op = p->iop;
2282 if (p->fop)
2283 inode->i_fop = p->fop;
2284 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002285 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002286 d_add(dentry, inode);
2287 /* Close the race of the process dying before we return the dentry */
2288 if (pid_revalidate(dentry, NULL))
2289 error = NULL;
2290out:
2291 return error;
2292}
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294static struct dentry *proc_pident_lookup(struct inode *dir,
2295 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002296 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002297 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002299 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002300 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002301 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002303 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Eric W. Biederman99f89552006-06-26 00:25:55 -07002305 if (!task)
2306 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002308 /*
2309 * Yes, it does not scale. And it should not. Don't add
2310 * new entries into /proc/<tgid>/ without very good reasons.
2311 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002312 last = &ents[nents - 1];
2313 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (p->len != dentry->d_name.len)
2315 continue;
2316 if (!memcmp(dentry->d_name.name, p->name, p->len))
2317 break;
2318 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002319 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 goto out;
2321
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002322 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002324 put_task_struct(task);
2325out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002326 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Eric Dumazetc5141e62007-05-08 00:26:15 -07002329static int proc_pident_fill_cache(struct file *filp, void *dirent,
2330 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002331{
2332 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2333 proc_pident_instantiate, task, p);
2334}
2335
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002336static int proc_pident_readdir(struct file *filp,
2337 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002338 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002339{
2340 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002341 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002342 struct inode *inode = dentry->d_inode;
2343 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002344 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002345 ino_t ino;
2346 int ret;
2347
2348 ret = -ENOENT;
2349 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002350 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002351
2352 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002353 i = filp->f_pos;
2354 switch (i) {
2355 case 0:
2356 ino = inode->i_ino;
2357 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2358 goto out;
2359 i++;
2360 filp->f_pos++;
2361 /* fall through */
2362 case 1:
2363 ino = parent_ino(dentry);
2364 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2365 goto out;
2366 i++;
2367 filp->f_pos++;
2368 /* fall through */
2369 default:
2370 i -= 2;
2371 if (i >= nents) {
2372 ret = 1;
2373 goto out;
2374 }
2375 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002376 last = &ents[nents - 1];
2377 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002378 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002379 goto out;
2380 filp->f_pos++;
2381 p++;
2382 }
2383 }
2384
2385 ret = 1;
2386out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002387 put_task_struct(task);
2388out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002389 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002393static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2394 size_t count, loff_t *ppos)
2395{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002396 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002397 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002398 ssize_t length;
2399 struct task_struct *task = get_proc_task(inode);
2400
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002401 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002402 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002403
2404 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002405 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002406 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002407 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002408 if (length > 0)
2409 length = simple_read_from_buffer(buf, count, ppos, p, length);
2410 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002411 return length;
2412}
2413
2414static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2415 size_t count, loff_t *ppos)
2416{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002417 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002418 char *page;
2419 ssize_t length;
2420 struct task_struct *task = get_proc_task(inode);
2421
2422 length = -ESRCH;
2423 if (!task)
2424 goto out_no_task;
2425 if (count > PAGE_SIZE)
2426 count = PAGE_SIZE;
2427
2428 /* No partial writes. */
2429 length = -EINVAL;
2430 if (*ppos != 0)
2431 goto out;
2432
2433 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002434 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002435 if (!page)
2436 goto out;
2437
2438 length = -EFAULT;
2439 if (copy_from_user(page, buf, count))
2440 goto out_free;
2441
David Howells107db7c2009-05-08 13:55:27 +01002442 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002443 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002444 if (length < 0)
2445 goto out_free;
2446
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002447 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002448 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002449 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002450 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002451out_free:
2452 free_page((unsigned long) page);
2453out:
2454 put_task_struct(task);
2455out_no_task:
2456 return length;
2457}
2458
Arjan van de Ven00977a52007-02-12 00:55:34 -08002459static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002460 .read = proc_pid_attr_read,
2461 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002462 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002463};
2464
Eric Dumazetc5141e62007-05-08 00:26:15 -07002465static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002466 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2467 REG("prev", S_IRUGO, proc_pid_attr_operations),
2468 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2469 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2470 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2471 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002472};
2473
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002474static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 void * dirent, filldir_t filldir)
2476{
2477 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002478 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
Arjan van de Ven00977a52007-02-12 00:55:34 -08002481static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002483 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002484 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485};
2486
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002487static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 struct dentry *dentry, struct nameidata *nd)
2489{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002490 return proc_pident_lookup(dir, dentry,
2491 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002494static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002495 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002496 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002497 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498};
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500#endif
2501
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002502#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002503static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2504 size_t count, loff_t *ppos)
2505{
2506 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2507 struct mm_struct *mm;
2508 char buffer[PROC_NUMBUF];
2509 size_t len;
2510 int ret;
2511
2512 if (!task)
2513 return -ESRCH;
2514
2515 ret = 0;
2516 mm = get_task_mm(task);
2517 if (mm) {
2518 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2519 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2520 MMF_DUMP_FILTER_SHIFT));
2521 mmput(mm);
2522 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2523 }
2524
2525 put_task_struct(task);
2526
2527 return ret;
2528}
2529
2530static ssize_t proc_coredump_filter_write(struct file *file,
2531 const char __user *buf,
2532 size_t count,
2533 loff_t *ppos)
2534{
2535 struct task_struct *task;
2536 struct mm_struct *mm;
2537 char buffer[PROC_NUMBUF], *end;
2538 unsigned int val;
2539 int ret;
2540 int i;
2541 unsigned long mask;
2542
2543 ret = -EFAULT;
2544 memset(buffer, 0, sizeof(buffer));
2545 if (count > sizeof(buffer) - 1)
2546 count = sizeof(buffer) - 1;
2547 if (copy_from_user(buffer, buf, count))
2548 goto out_no_task;
2549
2550 ret = -EINVAL;
2551 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2552 if (*end == '\n')
2553 end++;
2554 if (end - buffer == 0)
2555 goto out_no_task;
2556
2557 ret = -ESRCH;
2558 task = get_proc_task(file->f_dentry->d_inode);
2559 if (!task)
2560 goto out_no_task;
2561
2562 ret = end - buffer;
2563 mm = get_task_mm(task);
2564 if (!mm)
2565 goto out_no_mm;
2566
2567 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2568 if (val & mask)
2569 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2570 else
2571 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2572 }
2573
2574 mmput(mm);
2575 out_no_mm:
2576 put_task_struct(task);
2577 out_no_task:
2578 return ret;
2579}
2580
2581static const struct file_operations proc_coredump_filter_operations = {
2582 .read = proc_coredump_filter_read,
2583 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002584 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002585};
2586#endif
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588/*
2589 * /proc/self:
2590 */
2591static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2592 int buflen)
2593{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002594 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002595 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002596 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002597 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002598 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002599 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return vfs_readlink(dentry,buffer,buflen,tmp);
2601}
2602
Al Viro008b1502005-08-20 00:17:39 +01002603static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002605 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002606 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002607 char *name = ERR_PTR(-ENOENT);
2608 if (tgid) {
2609 name = __getname();
2610 if (!name)
2611 name = ERR_PTR(-ENOMEM);
2612 else
2613 sprintf(name, "%d", tgid);
2614 }
2615 nd_set_link(nd, name);
2616 return NULL;
2617}
2618
2619static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2620 void *cookie)
2621{
2622 char *s = nd_get_link(nd);
2623 if (!IS_ERR(s))
2624 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002625}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002627static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 .readlink = proc_self_readlink,
2629 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002630 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631};
2632
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002633/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002634 * proc base
2635 *
2636 * These are the directory entries in the root directory of /proc
2637 * that properly belong to the /proc filesystem, as they describe
2638 * describe something that is process related.
2639 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002640static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002641 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002642 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002643};
2644
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002645static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002646 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002647{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002648 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002649 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002650 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002651 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002652
2653 /* Allocate the inode */
2654 error = ERR_PTR(-ENOMEM);
2655 inode = new_inode(dir->i_sb);
2656 if (!inode)
2657 goto out;
2658
2659 /* Initialize the inode */
2660 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002661 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002662 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002663
2664 /*
2665 * grab the reference to the task.
2666 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002667 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002668 if (!ei->pid)
2669 goto out_iput;
2670
Eric W. Biederman801199c2006-10-02 02:18:48 -07002671 inode->i_mode = p->mode;
2672 if (S_ISDIR(inode->i_mode))
2673 inode->i_nlink = 2;
2674 if (S_ISLNK(inode->i_mode))
2675 inode->i_size = 64;
2676 if (p->iop)
2677 inode->i_op = p->iop;
2678 if (p->fop)
2679 inode->i_fop = p->fop;
2680 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002681 d_add(dentry, inode);
2682 error = NULL;
2683out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002684 return error;
2685out_iput:
2686 iput(inode);
2687 goto out;
2688}
2689
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002690static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2691{
2692 struct dentry *error;
2693 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002694 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002695
2696 error = ERR_PTR(-ENOENT);
2697
2698 if (!task)
2699 goto out_no_task;
2700
2701 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002702 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2703 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002704 if (p->len != dentry->d_name.len)
2705 continue;
2706 if (!memcmp(dentry->d_name.name, p->name, p->len))
2707 break;
2708 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002709 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002710 goto out;
2711
2712 error = proc_base_instantiate(dir, dentry, task, p);
2713
2714out:
2715 put_task_struct(task);
2716out_no_task:
2717 return error;
2718}
2719
Eric Dumazetc5141e62007-05-08 00:26:15 -07002720static int proc_base_fill_cache(struct file *filp, void *dirent,
2721 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002722{
2723 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2724 proc_base_instantiate, task, p);
2725}
2726
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002727#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002728static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002729{
Andrea Righi940389b2008-07-28 00:48:12 +02002730 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002731 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002732
Andrea Righi59954772008-07-27 17:29:15 +02002733 if (whole && lock_task_sighand(task, &flags)) {
2734 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002735
Andrea Righi59954772008-07-27 17:29:15 +02002736 task_io_accounting_add(&acct, &task->signal->ioac);
2737 while_each_thread(task, t)
2738 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002739
Andrea Righi59954772008-07-27 17:29:15 +02002740 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002741 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002742 return sprintf(buffer,
2743 "rchar: %llu\n"
2744 "wchar: %llu\n"
2745 "syscr: %llu\n"
2746 "syscw: %llu\n"
2747 "read_bytes: %llu\n"
2748 "write_bytes: %llu\n"
2749 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002750 (unsigned long long)acct.rchar,
2751 (unsigned long long)acct.wchar,
2752 (unsigned long long)acct.syscr,
2753 (unsigned long long)acct.syscw,
2754 (unsigned long long)acct.read_bytes,
2755 (unsigned long long)acct.write_bytes,
2756 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002757}
Andrea Righi297c5d92008-07-25 01:48:49 -07002758
2759static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2760{
2761 return do_io_accounting(task, buffer, 0);
2762}
2763
2764static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2765{
2766 return do_io_accounting(task, buffer, 1);
2767}
2768#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002769
Kees Cook47830722008-10-06 03:11:58 +04002770static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2771 struct pid *pid, struct task_struct *task)
2772{
2773 seq_printf(m, "%08x\n", task->personality);
2774 return 0;
2775}
2776
Eric W. Biederman801199c2006-10-02 02:18:48 -07002777/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002778 * Thread groups
2779 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002780static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002781static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002782
Eric Dumazetc5141e62007-05-08 00:26:15 -07002783static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002784 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2785 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2786 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002787#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002788 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002789#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002790 REG("environ", S_IRUSR, proc_environ_operations),
2791 INF("auxv", S_IRUSR, proc_pid_auxv),
2792 ONE("status", S_IRUGO, proc_pid_status),
2793 ONE("personality", S_IRUSR, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002794 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002795#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002796 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002797#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002798#ifdef CONFIG_SCHED_AUTOGROUP
2799 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2800#endif
john stultz4614a696b2009-12-14 18:00:05 -08002801 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002802#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002803 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002804#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002805 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2806 ONE("stat", S_IRUGO, proc_tgid_stat),
2807 ONE("statm", S_IRUGO, proc_pid_statm),
2808 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002809#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002810 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002811#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002812 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2813 LNK("cwd", proc_cwd_link),
2814 LNK("root", proc_root_link),
2815 LNK("exe", proc_exe_link),
2816 REG("mounts", S_IRUGO, proc_mounts_operations),
2817 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2818 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002819#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002820 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2821 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002822 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002823#endif
2824#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002825 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002826#endif
2827#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002828 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002829#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002830#ifdef CONFIG_STACKTRACE
2831 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002832#endif
2833#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002834 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002835#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002836#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002837 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002838#endif
Paul Menage8793d852007-10-18 23:39:39 -07002839#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002840 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002841#endif
Paul Menagea4243162007-10-18 23:39:35 -07002842#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002843 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002844#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002845 INF("oom_score", S_IRUGO, proc_oom_score),
2846 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002847 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002848#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002849 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2850 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002851#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002852#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002853 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002854#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002855#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002856 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002857#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002858#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002859 INF("io", S_IRUGO, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002860#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002861};
2862
2863static int proc_tgid_base_readdir(struct file * filp,
2864 void * dirent, filldir_t filldir)
2865{
2866 return proc_pident_readdir(filp,dirent,filldir,
2867 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2868}
2869
Arjan van de Ven00977a52007-02-12 00:55:34 -08002870static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002871 .read = generic_read_dir,
2872 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002873 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002874};
2875
2876static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002877 return proc_pident_lookup(dir, dentry,
2878 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002879}
2880
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002881static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002882 .lookup = proc_tgid_base_lookup,
2883 .getattr = pid_getattr,
2884 .setattr = proc_setattr,
2885};
2886
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002887static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002889 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002890 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002891 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Eric W. Biederman48e64842006-06-26 00:25:48 -07002893 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002894 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2895 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002896 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002897 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002898 d_drop(dentry);
2899 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Eric W. Biederman48e64842006-06-26 00:25:48 -07002902 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002903 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2904 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002905 if (!leader)
2906 goto out;
2907
2908 name.name = "task";
2909 name.len = strlen(name.name);
2910 dir = d_hash_and_lookup(leader, &name);
2911 if (!dir)
2912 goto out_put_leader;
2913
2914 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002915 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002916 dentry = d_hash_and_lookup(dir, &name);
2917 if (dentry) {
2918 shrink_dcache_parent(dentry);
2919 d_drop(dentry);
2920 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002922
2923 dput(dir);
2924out_put_leader:
2925 dput(leader);
2926out:
2927 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
Randy Dunlap0895e912007-10-21 21:00:10 -07002930/**
2931 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2932 * @task: task that should be flushed.
2933 *
2934 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002935 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002936 * in. This call is supposed to do all of this job.
2937 *
2938 * Looks in the dcache for
2939 * /proc/@pid
2940 * /proc/@tgid/task/@pid
2941 * if either directory is present flushes it and all of it'ts children
2942 * from the dcache.
2943 *
2944 * It is safe and reasonable to cache /proc entries for a task until
2945 * that task exits. After that they just clog up the dcache with
2946 * useless entries, possibly causing useful dcache entries to be
2947 * flushed instead. This routine is proved to flush those useless
2948 * dcache entries at process exit time.
2949 *
2950 * NOTE: This routine is just an optimization so it does not guarantee
2951 * that no dcache entries will exist at process exit time it
2952 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002953 */
2954
2955void proc_flush_task(struct task_struct *task)
2956{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002957 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002958 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002959 struct upid *upid;
2960
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002961 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002962 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002963
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002964 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002965 upid = &pid->numbers[i];
2966 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002967 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002968 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002969
2970 upid = &pid->numbers[pid->level];
2971 if (upid->nr == 1)
2972 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002973}
2974
Adrian Bunk9711ef92006-12-06 20:38:31 -08002975static struct dentry *proc_pid_instantiate(struct inode *dir,
2976 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002977 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002978{
2979 struct dentry *error = ERR_PTR(-ENOENT);
2980 struct inode *inode;
2981
Eric W. Biederman61a28782006-10-02 02:18:49 -07002982 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002983 if (!inode)
2984 goto out;
2985
2986 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2987 inode->i_op = &proc_tgid_base_inode_operations;
2988 inode->i_fop = &proc_tgid_base_operations;
2989 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002990
2991 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2992 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002993
Nick Pigginfb045ad2011-01-07 17:49:55 +11002994 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002995
2996 d_add(dentry, inode);
2997 /* Close the race of the process dying before we return the dentry */
2998 if (pid_revalidate(dentry, NULL))
2999 error = NULL;
3000out:
3001 return error;
3002}
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3005{
Dan Carpenter73d36462010-05-26 14:43:25 -07003006 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003009 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Eric W. Biederman801199c2006-10-02 02:18:48 -07003011 result = proc_base_lookup(dir, dentry);
3012 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3013 goto out;
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 tgid = name_to_int(dentry);
3016 if (tgid == ~0U)
3017 goto out;
3018
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003019 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003020 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003021 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 if (task)
3023 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003024 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (!task)
3026 goto out;
3027
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003028 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003029 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07003031 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003035 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003036 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003038struct tgid_iter {
3039 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003040 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003041};
3042static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3043{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003044 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003046 if (iter.task)
3047 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003048 rcu_read_lock();
3049retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003050 iter.task = NULL;
3051 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003052 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003053 iter.tgid = pid_nr_ns(pid, ns);
3054 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003055 /* What we to know is if the pid we have find is the
3056 * pid of a thread_group_leader. Testing for task
3057 * being a thread_group_leader is the obvious thing
3058 * todo but there is a window when it fails, due to
3059 * the pid transfer logic in de_thread.
3060 *
3061 * So we perform the straight forward test of seeing
3062 * if the pid we have found is the pid of a thread
3063 * group leader, and don't worry if the task we have
3064 * found doesn't happen to be a thread group leader.
3065 * As we don't care in the case of readdir.
3066 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003067 if (!iter.task || !has_group_leader_pid(iter.task)) {
3068 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003069 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003070 }
3071 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003073 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003074 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075}
3076
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003077#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Eric W. Biederman61a28782006-10-02 02:18:49 -07003079static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003080 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003081{
3082 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003083 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003084 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003085 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003086}
3087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088/* for the /proc/ directory itself, after non-process stuff has been done */
3089int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3090{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003092 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003093 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003094 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Eric W. Biederman61a28782006-10-02 02:18:49 -07003096 if (!reaper)
3097 goto out_no_task;
3098
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003099 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003100 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003101 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003105 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003106 iter.task = NULL;
3107 iter.tgid = filp->f_pos - TGID_OFFSET;
3108 for (iter = next_tgid(ns, iter);
3109 iter.task;
3110 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3111 filp->f_pos = iter.tgid + TGID_OFFSET;
3112 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3113 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003114 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003117 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3118out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003119 put_task_struct(reaper);
3120out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return 0;
3122}
3123
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003124/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003125 * Tasks
3126 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003127static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003128 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003129 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003130 REG("environ", S_IRUSR, proc_environ_operations),
3131 INF("auxv", S_IRUSR, proc_pid_auxv),
3132 ONE("status", S_IRUGO, proc_pid_status),
3133 ONE("personality", S_IRUSR, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003134 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003135#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003136 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003137#endif
john stultz4614a696b2009-12-14 18:00:05 -08003138 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003139#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003140 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003141#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003142 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3143 ONE("stat", S_IRUGO, proc_tid_stat),
3144 ONE("statm", S_IRUGO, proc_pid_statm),
3145 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003146#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003147 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003148#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003149 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3150 LNK("cwd", proc_cwd_link),
3151 LNK("root", proc_root_link),
3152 LNK("exe", proc_exe_link),
3153 REG("mounts", S_IRUGO, proc_mounts_operations),
3154 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003155#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003156 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3157 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003158 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003159#endif
3160#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003161 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003162#endif
3163#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003164 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003165#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003166#ifdef CONFIG_STACKTRACE
3167 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003168#endif
3169#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003170 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003171#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003172#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003173 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003174#endif
Paul Menage8793d852007-10-18 23:39:39 -07003175#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003176 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003177#endif
Paul Menagea4243162007-10-18 23:39:35 -07003178#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003179 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003180#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003181 INF("oom_score", S_IRUGO, proc_oom_score),
3182 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003183 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003184#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003185 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003186 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003187#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003188#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003189 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003190#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003191#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003192 INF("io", S_IRUGO, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003193#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003194};
3195
3196static int proc_tid_base_readdir(struct file * filp,
3197 void * dirent, filldir_t filldir)
3198{
3199 return proc_pident_readdir(filp,dirent,filldir,
3200 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3201}
3202
3203static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003204 return proc_pident_lookup(dir, dentry,
3205 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003206}
3207
Arjan van de Ven00977a52007-02-12 00:55:34 -08003208static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003209 .read = generic_read_dir,
3210 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003211 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003212};
3213
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003214static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003215 .lookup = proc_tid_base_lookup,
3216 .getattr = pid_getattr,
3217 .setattr = proc_setattr,
3218};
3219
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003220static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003221 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003222{
3223 struct dentry *error = ERR_PTR(-ENOENT);
3224 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003225 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003226
3227 if (!inode)
3228 goto out;
3229 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3230 inode->i_op = &proc_tid_base_inode_operations;
3231 inode->i_fop = &proc_tid_base_operations;
3232 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003233
3234 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3235 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003236
Nick Pigginfb045ad2011-01-07 17:49:55 +11003237 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003238
3239 d_add(dentry, inode);
3240 /* Close the race of the process dying before we return the dentry */
3241 if (pid_revalidate(dentry, NULL))
3242 error = NULL;
3243out:
3244 return error;
3245}
3246
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003247static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3248{
3249 struct dentry *result = ERR_PTR(-ENOENT);
3250 struct task_struct *task;
3251 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003252 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003253 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003254
3255 if (!leader)
3256 goto out_no_task;
3257
3258 tid = name_to_int(dentry);
3259 if (tid == ~0U)
3260 goto out;
3261
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003262 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003263 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003264 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003265 if (task)
3266 get_task_struct(task);
3267 rcu_read_unlock();
3268 if (!task)
3269 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003270 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003271 goto out_drop_task;
3272
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003273 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003274out_drop_task:
3275 put_task_struct(task);
3276out:
3277 put_task_struct(leader);
3278out_no_task:
3279 return result;
3280}
3281
3282/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003283 * Find the first tid of a thread group to return to user space.
3284 *
3285 * Usually this is just the thread group leader, but if the users
3286 * buffer was too small or there was a seek into the middle of the
3287 * directory we have more work todo.
3288 *
3289 * In the case of a short read we start with find_task_by_pid.
3290 *
3291 * In the case of a seek we start with the leader and walk nr
3292 * threads past it.
3293 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003294static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003295 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003296{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003297 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003298
Eric W. Biedermancc288732006-06-26 00:26:01 -07003299 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003300 /* Attempt to start with the pid of a thread */
3301 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003302 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003303 if (pos && (pos->group_leader == leader))
3304 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003305 }
3306
3307 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003308 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003309 if (nr && nr >= get_nr_threads(leader))
3310 goto out;
3311
3312 /* If we haven't found our starting place yet start
3313 * with the leader and walk nr threads forward.
3314 */
3315 for (pos = leader; nr > 0; --nr) {
3316 pos = next_thread(pos);
3317 if (pos == leader) {
3318 pos = NULL;
3319 goto out;
3320 }
3321 }
3322found:
3323 get_task_struct(pos);
3324out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003325 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003326 return pos;
3327}
3328
3329/*
3330 * Find the next thread in the thread list.
3331 * Return NULL if there is an error or no next thread.
3332 *
3333 * The reference to the input task_struct is released.
3334 */
3335static struct task_struct *next_tid(struct task_struct *start)
3336{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003337 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003338 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003339 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003340 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003341 if (thread_group_leader(pos))
3342 pos = NULL;
3343 else
3344 get_task_struct(pos);
3345 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003346 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003347 put_task_struct(start);
3348 return pos;
3349}
3350
Eric W. Biederman61a28782006-10-02 02:18:49 -07003351static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3352 struct task_struct *task, int tid)
3353{
3354 char name[PROC_NUMBUF];
3355 int len = snprintf(name, sizeof(name), "%d", tid);
3356 return proc_fill_cache(filp, dirent, filldir, name, len,
3357 proc_task_instantiate, task, NULL);
3358}
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360/* for the /proc/TGID/task/ directories */
3361static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3362{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003363 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003365 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003366 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 int retval = -ENOENT;
3368 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003369 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003370 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003372 task = get_proc_task(inode);
3373 if (!task)
3374 goto out_no_task;
3375 rcu_read_lock();
3376 if (pid_alive(task)) {
3377 leader = task->group_leader;
3378 get_task_struct(leader);
3379 }
3380 rcu_read_unlock();
3381 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003382 if (!leader)
3383 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 retval = 0;
3385
Linus Torvaldsee568b22009-03-17 10:02:35 -07003386 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 case 0:
3388 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003389 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003391 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 /* fall through */
3393 case 1:
3394 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003395 if (filldir(dirent, "..", 2, 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 }
3400
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003401 /* f_version caches the tgid value that the last readdir call couldn't
3402 * return. lseek aka telldir automagically resets f_version to 0.
3403 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003404 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003405 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003406 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003407 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003408 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003409 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003410 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003411 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003412 /* returning this tgid failed, save it as the first
3413 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003414 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003415 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
3419out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003420 put_task_struct(leader);
3421out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return retval;
3423}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003424
3425static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3426{
3427 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003428 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003429 generic_fillattr(inode, stat);
3430
Eric W. Biederman99f89552006-06-26 00:25:55 -07003431 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003432 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003433 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003434 }
3435
3436 return 0;
3437}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003438
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003439static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003440 .lookup = proc_task_lookup,
3441 .getattr = proc_task_getattr,
3442 .setattr = proc_setattr,
3443};
3444
Arjan van de Ven00977a52007-02-12 00:55:34 -08003445static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003446 .read = generic_read_dir,
3447 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003448 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003449};