blob: bc71f120100d4332aaaf1799dca7379e59afa4c1 [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>
Chris Metcalff133ecc2011-05-26 12:40:09 -040086#ifdef CONFIG_HARDWALL
87#include <asm/hardwall.h>
88#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include "internal.h"
90
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070091/* NOTE:
92 * Implementing inode permission operations in /proc is almost
93 * certainly an error. Permission checks need to happen during
94 * each system call not at open time. The reason is that most of
95 * what we wish to check for permissions in /proc varies at runtime.
96 *
97 * The classic example of a problem is opening file descriptors
98 * in /proc for a task before it execs a suid executable.
99 */
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700103 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800105 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800106 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700107 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Eric W. Biederman61a28782006-10-02 02:18:49 -0700110#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700111 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700112 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 .mode = MODE, \
114 .iop = IOP, \
115 .fop = FOP, \
116 .op = OP, \
117}
118
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300119#define DIR(NAME, MODE, iops, fops) \
120 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
121#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700122 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700123 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300124 { .proc_get_link = get_link } )
125#define REG(NAME, MODE, fops) \
126 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
127#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700128 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700129 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300130 { .proc_read = read } )
131#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800132 NOD(NAME, (S_IFREG|(MODE)), \
133 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300134 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Mike Chan766e9932008-04-24 10:22:26 -0700136/* ANDROID is for special files in /proc. */
137#define ANDROID(NAME, MODE, OTYPE) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 &proc_##OTYPE##_inode_operations, \
140 &proc_##OTYPE##_operations, {})
141
Vegard Nossumaed54172008-06-05 22:46:53 -0700142/*
143 * Count the number of hardlinks for the pid_entry table, excluding the .
144 * and .. links.
145 */
146static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147 unsigned int n)
148{
149 unsigned int i;
150 unsigned int count;
151
152 count = 0;
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
155 ++count;
156 }
157
158 return count;
159}
160
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200161static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000163 int result = -ENOENT;
164
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700165 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200166 if (task->fs) {
167 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000168 result = 0;
169 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000171 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700172}
173
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800174static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176 struct task_struct *task = get_proc_task(inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700177 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700178
179 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200180 task_lock(task);
181 if (task->fs) {
182 get_fs_pwd(task->fs, path);
183 result = 0;
184 }
185 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700186 put_task_struct(task);
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return result;
189}
190
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800191static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700193 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700195
196 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200197 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700198 put_task_struct(task);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return result;
201}
202
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400203static struct mm_struct *__check_mem_permission(struct task_struct *task)
Roland McGrath638fa202008-04-29 01:01:38 -0700204{
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400205 struct mm_struct *mm;
206
207 mm = get_task_mm(task);
208 if (!mm)
209 return ERR_PTR(-EINVAL);
210
Roland McGrath638fa202008-04-29 01:01:38 -0700211 /*
212 * A task can always look at itself, in case it chooses
213 * to use system calls instead of load instructions.
214 */
215 if (task == current)
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400216 return mm;
Roland McGrath638fa202008-04-29 01:01:38 -0700217
218 /*
219 * If current is actively ptrace'ing, and would also be
220 * permitted to freshly attach with ptrace now, permit it.
221 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700222 if (task_is_stopped_or_traced(task)) {
223 int match;
224 rcu_read_lock();
225 match = (tracehook_tracer_task(task) == current);
226 rcu_read_unlock();
227 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400228 return mm;
Roland McGrath0d094ef2008-07-25 19:45:49 -0700229 }
Roland McGrath638fa202008-04-29 01:01:38 -0700230
231 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300232 * No one else is allowed.
Roland McGrath638fa202008-04-29 01:01:38 -0700233 */
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400234 mmput(mm);
235 return ERR_PTR(-EPERM);
Roland McGrath638fa202008-04-29 01:01:38 -0700236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Stephen Wilson18f661b2011-03-13 15:49:22 -0400238/*
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400239 * If current may access user memory in @task return a reference to the
240 * corresponding mm, otherwise ERR_PTR.
Stephen Wilson18f661b2011-03-13 15:49:22 -0400241 */
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400242static struct mm_struct *check_mem_permission(struct task_struct *task)
Stephen Wilson18f661b2011-03-13 15:49:22 -0400243{
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400244 struct mm_struct *mm;
Stephen Wilson18f661b2011-03-13 15:49:22 -0400245 int err;
246
247 /*
248 * Avoid racing if task exec's as we might get a new mm but validate
249 * against old credentials.
250 */
251 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
252 if (err)
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400253 return ERR_PTR(err);
Stephen Wilson18f661b2011-03-13 15:49:22 -0400254
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400255 mm = __check_mem_permission(task);
Stephen Wilson18f661b2011-03-13 15:49:22 -0400256 mutex_unlock(&task->signal->cred_guard_mutex);
257
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400258 return mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Al Viro831830b2008-01-02 14:09:57 +0000261struct mm_struct *mm_for_maps(struct task_struct *task)
262{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200263 struct mm_struct *mm;
Al Viroec6fd8a2011-02-15 22:22:54 -0500264 int err;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200265
Al Viroec6fd8a2011-02-15 22:22:54 -0500266 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
267 if (err)
268 return ERR_PTR(err);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200269
270 mm = get_task_mm(task);
271 if (mm && mm != current->mm &&
San Mehata97e6c52009-09-16 12:39:10 -0700272 !ptrace_may_access(task, PTRACE_MODE_READ) &&
273 !capable(CAP_SYS_RESOURCE)) {
Oleg Nesterov704b8362009-07-10 03:27:40 +0200274 mmput(mm);
Al Viroec6fd8a2011-02-15 22:22:54 -0500275 mm = ERR_PTR(-EACCES);
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200276 }
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700277 mutex_unlock(&task->signal->cred_guard_mutex);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200278
Al Viro831830b2008-01-02 14:09:57 +0000279 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static int proc_pid_cmdline(struct task_struct *task, char * buffer)
283{
284 int res = 0;
285 unsigned int len;
286 struct mm_struct *mm = get_task_mm(task);
287 if (!mm)
288 goto out;
289 if (!mm->arg_end)
290 goto out_mm; /* Shh! No looking before we're done */
291
292 len = mm->arg_end - mm->arg_start;
293
294 if (len > PAGE_SIZE)
295 len = PAGE_SIZE;
296
297 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
298
299 // If the nul at the end of args has been overwritten, then
300 // assume application is using setproctitle(3).
301 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
302 len = strnlen(buffer, res);
303 if (len < res) {
304 res = len;
305 } else {
306 len = mm->env_end - mm->env_start;
307 if (len > PAGE_SIZE - res)
308 len = PAGE_SIZE - res;
309 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
310 res = strnlen(buffer, res);
311 }
312 }
313out_mm:
314 mmput(mm);
315out:
316 return res;
317}
318
319static int proc_pid_auxv(struct task_struct *task, char *buffer)
320{
Al Viro2fadaef2011-02-15 22:52:11 -0500321 struct mm_struct *mm = mm_for_maps(task);
322 int res = PTR_ERR(mm);
323 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300325 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300327 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 res = nwords * sizeof(mm->saved_auxv[0]);
329 if (res > PAGE_SIZE)
330 res = PAGE_SIZE;
331 memcpy(buffer, mm->saved_auxv, res);
332 mmput(mm);
333 }
334 return res;
335}
336
337
338#ifdef CONFIG_KALLSYMS
339/*
340 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
341 * Returns the resolved symbol. If that fails, simply return the address.
342 */
343static int proc_pid_wchan(struct task_struct *task, char *buffer)
344{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700345 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700346 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 wchan = get_wchan(task);
349
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700350 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600351 if (!ptrace_may_access(task, PTRACE_MODE_READ))
352 return 0;
353 else
354 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700355 else
356 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358#endif /* CONFIG_KALLSYMS */
359
Al Viroa9712bc2011-03-23 15:52:50 -0400360static int lock_trace(struct task_struct *task)
361{
362 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
363 if (err)
364 return err;
365 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
366 mutex_unlock(&task->signal->cred_guard_mutex);
367 return -EPERM;
368 }
369 return 0;
370}
371
372static void unlock_trace(struct task_struct *task)
373{
374 mutex_unlock(&task->signal->cred_guard_mutex);
375}
376
Ken Chen2ec220e2008-11-10 11:26:08 +0300377#ifdef CONFIG_STACKTRACE
378
379#define MAX_STACK_TRACE_DEPTH 64
380
381static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
382 struct pid *pid, struct task_struct *task)
383{
384 struct stack_trace trace;
385 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400386 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300387 int i;
388
389 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
390 if (!entries)
391 return -ENOMEM;
392
393 trace.nr_entries = 0;
394 trace.max_entries = MAX_STACK_TRACE_DEPTH;
395 trace.entries = entries;
396 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300397
Al Viroa9712bc2011-03-23 15:52:50 -0400398 err = lock_trace(task);
399 if (!err) {
400 save_stack_trace_tsk(task, &trace);
401
402 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700403 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400404 (void *)entries[i], (void *)entries[i]);
405 }
406 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300407 }
408 kfree(entries);
409
Al Viroa9712bc2011-03-23 15:52:50 -0400410 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300411}
412#endif
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef CONFIG_SCHEDSTATS
415/*
416 * Provides /proc/PID/schedstat
417 */
418static int proc_pid_schedstat(struct task_struct *task, char *buffer)
419{
Balbir Singh172ba842007-07-09 18:52:00 +0200420 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100421 (unsigned long long)task->se.sum_exec_runtime,
422 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200423 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425#endif
426
Arjan van de Ven97455122008-01-25 21:08:34 +0100427#ifdef CONFIG_LATENCYTOP
428static int lstats_show_proc(struct seq_file *m, void *v)
429{
430 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800431 struct inode *inode = m->private;
432 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100433
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800434 if (!task)
435 return -ESRCH;
436 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100437 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800438 struct latency_record *lr = &task->latency_record[i];
439 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100440 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800441 seq_printf(m, "%i %li %li",
442 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100443 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800444 unsigned long bt = lr->backtrace[q];
445 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100446 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800447 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100448 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800449 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100450 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800451 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100452 }
453
454 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800455 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100456 return 0;
457}
458
459static int lstats_open(struct inode *inode, struct file *file)
460{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800461 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800462}
463
Arjan van de Ven97455122008-01-25 21:08:34 +0100464static ssize_t lstats_write(struct file *file, const char __user *buf,
465 size_t count, loff_t *offs)
466{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800467 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100468
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800469 if (!task)
470 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100471 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800472 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100473
474 return count;
475}
476
477static const struct file_operations proc_lstats_operations = {
478 .open = lstats_open,
479 .read = seq_read,
480 .write = lstats_write,
481 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800482 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100483};
484
485#endif
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static int proc_oom_score(struct task_struct *task, char *buffer)
488{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200489 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700491 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200492 if (pid_alive(task))
David Rientjesa63d83f2010-08-09 17:19:46 -0700493 points = oom_badness(task, NULL, NULL,
494 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700495 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return sprintf(buffer, "%lu\n", points);
497}
498
Neil Hormand85f50d2007-10-18 23:40:37 -0700499struct limit_names {
500 char *name;
501 char *unit;
502};
503
504static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700505 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700506 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
507 [RLIMIT_DATA] = {"Max data size", "bytes"},
508 [RLIMIT_STACK] = {"Max stack size", "bytes"},
509 [RLIMIT_CORE] = {"Max core file size", "bytes"},
510 [RLIMIT_RSS] = {"Max resident set", "bytes"},
511 [RLIMIT_NPROC] = {"Max processes", "processes"},
512 [RLIMIT_NOFILE] = {"Max open files", "files"},
513 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
514 [RLIMIT_AS] = {"Max address space", "bytes"},
515 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
516 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
517 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
518 [RLIMIT_NICE] = {"Max nice priority", NULL},
519 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800520 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700521};
522
523/* Display limits for a process */
524static int proc_pid_limits(struct task_struct *task, char *buffer)
525{
526 unsigned int i;
527 int count = 0;
528 unsigned long flags;
529 char *bufptr = buffer;
530
531 struct rlimit rlim[RLIM_NLIMITS];
532
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400533 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700534 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700535 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
536 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700537
538 /*
539 * print the file header
540 */
541 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
542 "Limit", "Soft Limit", "Hard Limit", "Units");
543
544 for (i = 0; i < RLIM_NLIMITS; i++) {
545 if (rlim[i].rlim_cur == RLIM_INFINITY)
546 count += sprintf(&bufptr[count], "%-25s %-20s ",
547 lnames[i].name, "unlimited");
548 else
549 count += sprintf(&bufptr[count], "%-25s %-20lu ",
550 lnames[i].name, rlim[i].rlim_cur);
551
552 if (rlim[i].rlim_max == RLIM_INFINITY)
553 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
554 else
555 count += sprintf(&bufptr[count], "%-20lu ",
556 rlim[i].rlim_max);
557
558 if (lnames[i].unit)
559 count += sprintf(&bufptr[count], "%-10s\n",
560 lnames[i].unit);
561 else
562 count += sprintf(&bufptr[count], "\n");
563 }
564
565 return count;
566}
567
Roland McGrathebcb6732008-07-25 19:46:00 -0700568#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
569static int proc_pid_syscall(struct task_struct *task, char *buffer)
570{
571 long nr;
572 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400573 int res = lock_trace(task);
574 if (res)
575 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700576
577 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400578 res = sprintf(buffer, "running\n");
579 else if (nr < 0)
580 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
581 else
582 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700583 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
584 nr,
585 args[0], args[1], args[2], args[3], args[4], args[5],
586 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400587 unlock_trace(task);
588 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700589}
590#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/************************************************************************/
593/* Here the fs part begins */
594/************************************************************************/
595
596/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700597static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700599 struct task_struct *task;
600 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700601 /* Allow access to a task's file descriptors if it is us or we
602 * may use ptrace attach to the process and find out that
603 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700604 */
605 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700606 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400607 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700608 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700609 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700610 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800613int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700614{
615 int error;
616 struct inode *inode = dentry->d_inode;
617
618 if (attr->ia_valid & ATTR_MODE)
619 return -EPERM;
620
621 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200622 if (error)
623 return error;
624
625 if ((attr->ia_valid & ATTR_SIZE) &&
626 attr->ia_size != i_size_read(inode)) {
627 error = vmtruncate(inode, attr->ia_size);
628 if (error)
629 return error;
630 }
631
632 setattr_copy(inode, attr);
633 mark_inode_dirty(inode);
634 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700635}
636
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800637static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700638 .setattr = proc_setattr,
639};
640
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100641static int mounts_open_common(struct inode *inode, struct file *file,
642 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700644 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700645 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800646 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100647 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500648 struct proc_mounts *p;
649 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Eric W. Biederman99f89552006-06-26 00:25:55 -0700651 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700652 rcu_read_lock();
653 nsp = task_nsproxy(task);
654 if (nsp) {
655 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800656 if (ns)
657 get_mnt_ns(ns);
658 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700659 rcu_read_unlock();
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200660 if (ns && get_task_root(task, &root) == 0)
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000661 ret = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700662 put_task_struct(task);
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100665 if (!ns)
666 goto err;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000667 if (ret)
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100668 goto err_put_ns;
669
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100670 ret = -ENOMEM;
671 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
672 if (!p)
673 goto err_put_path;
674
675 file->private_data = &p->m;
676 ret = seq_open(file, op);
677 if (ret)
678 goto err_free;
679
680 p->m.private = p;
681 p->ns = ns;
682 p->root = root;
683 p->event = ns->event;
684
685 return 0;
686
687 err_free:
688 kfree(p);
689 err_put_path:
690 path_put(&root);
691 err_put_ns:
692 put_mnt_ns(ns);
693 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return ret;
695}
696
697static int mounts_release(struct inode *inode, struct file *file)
698{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100699 struct proc_mounts *p = file->private_data;
700 path_put(&p->root);
701 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return seq_release(inode, file);
703}
704
Al Viro5addc5d2005-11-07 17:15:49 -0500705static unsigned mounts_poll(struct file *file, poll_table *wait)
706{
707 struct proc_mounts *p = file->private_data;
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900708 unsigned res = POLLIN | POLLRDNORM;
Al Viro5addc5d2005-11-07 17:15:49 -0500709
Al Viro9f5596a2010-02-05 00:40:25 -0500710 poll_wait(file, &p->ns->poll, wait);
711 if (mnt_had_events(p))
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900712 res |= POLLERR | POLLPRI;
Al Viro5addc5d2005-11-07 17:15:49 -0500713
714 return res;
715}
716
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100717static int mounts_open(struct inode *inode, struct file *file)
718{
719 return mounts_open_common(inode, file, &mounts_op);
720}
721
Arjan van de Ven00977a52007-02-12 00:55:34 -0800722static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 .open = mounts_open,
724 .read = seq_read,
725 .llseek = seq_lseek,
726 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500727 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728};
729
Ram Pai2d4d4862008-03-27 13:06:25 +0100730static int mountinfo_open(struct inode *inode, struct file *file)
731{
732 return mounts_open_common(inode, file, &mountinfo_op);
733}
734
735static const struct file_operations proc_mountinfo_operations = {
736 .open = mountinfo_open,
737 .read = seq_read,
738 .llseek = seq_lseek,
739 .release = mounts_release,
740 .poll = mounts_poll,
741};
742
Chuck Leverb4629fe2006-03-20 13:44:12 -0500743static int mountstats_open(struct inode *inode, struct file *file)
744{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100745 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500746}
747
Arjan van de Ven00977a52007-02-12 00:55:34 -0800748static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500749 .open = mountstats_open,
750 .read = seq_read,
751 .llseek = seq_lseek,
752 .release = mounts_release,
753};
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
756
757static ssize_t proc_info_read(struct file * file, char __user * buf,
758 size_t count, loff_t *ppos)
759{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800760 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 unsigned long page;
762 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700763 struct task_struct *task = get_proc_task(inode);
764
765 length = -ESRCH;
766 if (!task)
767 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (count > PROC_BLOCK_SIZE)
770 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700771
772 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700773 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700774 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 length = PROC_I(inode)->op.proc_read(task, (char*)page);
777
778 if (length >= 0)
779 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
780 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700781out:
782 put_task_struct(task);
783out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return length;
785}
786
Arjan van de Ven00977a52007-02-12 00:55:34 -0800787static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100789 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790};
791
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800792static int proc_single_show(struct seq_file *m, void *v)
793{
794 struct inode *inode = m->private;
795 struct pid_namespace *ns;
796 struct pid *pid;
797 struct task_struct *task;
798 int ret;
799
800 ns = inode->i_sb->s_fs_info;
801 pid = proc_pid(inode);
802 task = get_pid_task(pid, PIDTYPE_PID);
803 if (!task)
804 return -ESRCH;
805
806 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
807
808 put_task_struct(task);
809 return ret;
810}
811
812static int proc_single_open(struct inode *inode, struct file *filp)
813{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800814 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800815}
816
817static const struct file_operations proc_single_file_operations = {
818 .open = proc_single_open,
819 .read = seq_read,
820 .llseek = seq_lseek,
821 .release = single_release,
822};
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824static int mem_open(struct inode* inode, struct file* file)
825{
826 file->private_data = (void*)((long)current->self_exec_id);
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700827 /* OK to pass negative loff_t, we can catch out-of-range */
828 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830}
831
832static ssize_t mem_read(struct file * file, char __user * buf,
833 size_t count, loff_t *ppos)
834{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800835 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 char *page;
837 unsigned long src = *ppos;
838 int ret = -ESRCH;
839 struct mm_struct *mm;
840
Eric W. Biederman99f89552006-06-26 00:25:55 -0700841 if (!task)
842 goto out_no_task;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700845 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!page)
847 goto out;
848
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400849 mm = check_mem_permission(task);
850 ret = PTR_ERR(mm);
851 if (IS_ERR(mm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 goto out_free;
853
854 ret = -EIO;
855
856 if (file->private_data != (void*)((long)current->self_exec_id))
857 goto out_put;
858
859 ret = 0;
860
861 while (count > 0) {
862 int this_len, retval;
863
864 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400865 retval = access_remote_vm(mm, src, page, this_len, 0);
866 if (!retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (!ret)
868 ret = -EIO;
869 break;
870 }
871
872 if (copy_to_user(buf, page, retval)) {
873 ret = -EFAULT;
874 break;
875 }
876
877 ret += retval;
878 src += retval;
879 buf += retval;
880 count -= retval;
881 }
882 *ppos = src;
883
884out_put:
885 mmput(mm);
886out_free:
887 free_page((unsigned long) page);
888out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700889 put_task_struct(task);
890out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return ret;
892}
893
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800894static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 size_t count, loff_t *ppos)
896{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700897 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800899 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 unsigned long dst = *ppos;
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400901 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Eric W. Biederman99f89552006-06-26 00:25:55 -0700903 copied = -ESRCH;
904 if (!task)
905 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Eric W. Biederman99f89552006-06-26 00:25:55 -0700907 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700908 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!page)
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700910 goto out_task;
911
912 mm = check_mem_permission(task);
913 copied = PTR_ERR(mm);
914 if (IS_ERR(mm))
915 goto out_free;
916
917 copied = -EIO;
918 if (file->private_data != (void *)((long)current->self_exec_id))
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400919 goto out_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700921 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 while (count > 0) {
923 int this_len, retval;
924
925 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
926 if (copy_from_user(page, buf, this_len)) {
927 copied = -EFAULT;
928 break;
929 }
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400930 retval = access_remote_vm(mm, dst, page, this_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (!retval) {
932 if (!copied)
933 copied = -EIO;
934 break;
935 }
936 copied += retval;
937 buf += retval;
938 dst += retval;
939 count -= retval;
940 }
941 *ppos = dst;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700942
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400943out_mm:
944 mmput(mm);
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700945out_free:
946 free_page((unsigned long) page);
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400947out_task:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700948 put_task_struct(task);
949out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return copied;
951}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Matt Mackall85863e42008-02-04 22:29:04 -0800953loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 switch (orig) {
956 case 0:
957 file->f_pos = offset;
958 break;
959 case 1:
960 file->f_pos += offset;
961 break;
962 default:
963 return -EINVAL;
964 }
965 force_successful_syscall_return();
966 return file->f_pos;
967}
968
Arjan van de Ven00977a52007-02-12 00:55:34 -0800969static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 .llseek = mem_lseek,
971 .read = mem_read,
972 .write = mem_write,
973 .open = mem_open,
974};
975
James Pearson315e28c2007-10-16 23:30:17 -0700976static ssize_t environ_read(struct file *file, char __user *buf,
977 size_t count, loff_t *ppos)
978{
979 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
980 char *page;
981 unsigned long src = *ppos;
982 int ret = -ESRCH;
983 struct mm_struct *mm;
984
985 if (!task)
986 goto out_no_task;
987
James Pearson315e28c2007-10-16 23:30:17 -0700988 ret = -ENOMEM;
989 page = (char *)__get_free_page(GFP_TEMPORARY);
990 if (!page)
991 goto out;
992
James Pearson315e28c2007-10-16 23:30:17 -0700993
Al Virod6f64b82011-02-15 22:26:01 -0500994 mm = mm_for_maps(task);
995 ret = PTR_ERR(mm);
996 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700997 goto out_free;
998
Al Virod6f64b82011-02-15 22:26:01 -0500999 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -07001000 while (count > 0) {
1001 int this_len, retval, max_len;
1002
1003 this_len = mm->env_end - (mm->env_start + src);
1004
1005 if (this_len <= 0)
1006 break;
1007
1008 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
1009 this_len = (this_len > max_len) ? max_len : this_len;
1010
1011 retval = access_process_vm(task, (mm->env_start + src),
1012 page, this_len, 0);
1013
1014 if (retval <= 0) {
1015 ret = retval;
1016 break;
1017 }
1018
1019 if (copy_to_user(buf, page, retval)) {
1020 ret = -EFAULT;
1021 break;
1022 }
1023
1024 ret += retval;
1025 src += retval;
1026 buf += retval;
1027 count -= retval;
1028 }
1029 *ppos = src;
1030
1031 mmput(mm);
1032out_free:
1033 free_page((unsigned long) page);
1034out:
1035 put_task_struct(task);
1036out_no_task:
1037 return ret;
1038}
1039
1040static const struct file_operations proc_environ_operations = {
1041 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001042 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -07001043};
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1046 size_t count, loff_t *ppos)
1047{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001048 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001049 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001051 int oom_adjust = OOM_DISABLE;
1052 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Eric W. Biederman99f89552006-06-26 00:25:55 -07001054 if (!task)
1055 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001056
1057 if (lock_task_sighand(task, &flags)) {
1058 oom_adjust = task->signal->oom_adj;
1059 unlock_task_sighand(task, &flags);
1060 }
1061
Eric W. Biederman99f89552006-06-26 00:25:55 -07001062 put_task_struct(task);
1063
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001064 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001065
1066 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1070 size_t count, loff_t *ppos)
1071{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001072 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001073 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001074 int oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001075 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001076 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001078 memset(buffer, 0, sizeof(buffer));
1079 if (count > sizeof(buffer) - 1)
1080 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001081 if (copy_from_user(buffer, buf, count)) {
1082 err = -EFAULT;
1083 goto out;
1084 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001085
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001086 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001087 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001088 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001089 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -07001090 oom_adjust != OOM_DISABLE) {
1091 err = -EINVAL;
1092 goto out;
1093 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001094
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001095 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001096 if (!task) {
1097 err = -ESRCH;
1098 goto out;
1099 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001100
Ying Han3d5992d2010-10-26 14:21:23 -07001101 task_lock(task);
1102 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001103 err = -EINVAL;
1104 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001105 }
1106
David Rientjesd19d5472010-10-26 14:21:26 -07001107 if (!lock_task_sighand(task, &flags)) {
1108 err = -ESRCH;
1109 goto err_task_lock;
1110 }
1111
1112 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1113 err = -EACCES;
1114 goto err_sighand;
1115 }
1116
Ying Han3d5992d2010-10-26 14:21:23 -07001117 if (oom_adjust != task->signal->oom_adj) {
1118 if (oom_adjust == OOM_DISABLE)
1119 atomic_inc(&task->mm->oom_disable_count);
1120 if (task->signal->oom_adj == OOM_DISABLE)
1121 atomic_dec(&task->mm->oom_disable_count);
1122 }
1123
David Rientjes51b1bd22010-08-09 17:19:47 -07001124 /*
1125 * Warn that /proc/pid/oom_adj is deprecated, see
1126 * Documentation/feature-removal-schedule.txt.
1127 */
1128 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1129 "please use /proc/%d/oom_score_adj instead.\n",
1130 current->comm, task_pid_nr(current),
1131 task_pid_nr(task), task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001132 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -07001133 /*
1134 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1135 * value is always attainable.
1136 */
1137 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1138 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1139 else
1140 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1141 -OOM_DISABLE;
David Rientjes723548b2010-10-26 14:21:25 -07001142err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001143 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001144err_task_lock:
1145 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001146 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001147out:
1148 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Mike Chan766e9932008-04-24 10:22:26 -07001151static int oom_adjust_permission(struct inode *inode, int mask,
1152 unsigned int flags)
1153{
1154 uid_t uid;
1155 struct task_struct *p;
1156
1157 if (flags & IPERM_FLAG_RCU)
1158 return -ECHILD;
1159
1160 p = get_proc_task(inode);
1161 if(p) {
1162 uid = task_uid(p);
1163 put_task_struct(p);
1164 }
1165
1166 /*
1167 * System Server (uid == 1000) is granted access to oom_adj of all
1168 * android applications (uid > 10000) as and services (uid >= 1000)
1169 */
1170 if (p && (current_fsuid() == 1000) && (uid >= 1000)) {
1171 if (inode->i_mode >> 6 & mask) {
1172 return 0;
1173 }
1174 }
1175
1176 /* Fall back to default. */
1177 return generic_permission(inode, mask, flags, NULL);
1178}
1179
1180static const struct inode_operations proc_oom_adjust_inode_operations = {
1181 .permission = oom_adjust_permission,
1182};
1183
Arjan van de Ven00977a52007-02-12 00:55:34 -08001184static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 .read = oom_adjust_read,
1186 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001187 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188};
1189
David Rientjesa63d83f2010-08-09 17:19:46 -07001190static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1191 size_t count, loff_t *ppos)
1192{
1193 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1194 char buffer[PROC_NUMBUF];
1195 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1196 unsigned long flags;
1197 size_t len;
1198
1199 if (!task)
1200 return -ESRCH;
1201 if (lock_task_sighand(task, &flags)) {
1202 oom_score_adj = task->signal->oom_score_adj;
1203 unlock_task_sighand(task, &flags);
1204 }
1205 put_task_struct(task);
1206 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1207 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1208}
1209
1210static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1211 size_t count, loff_t *ppos)
1212{
1213 struct task_struct *task;
1214 char buffer[PROC_NUMBUF];
1215 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001216 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001217 int err;
1218
1219 memset(buffer, 0, sizeof(buffer));
1220 if (count > sizeof(buffer) - 1)
1221 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001222 if (copy_from_user(buffer, buf, count)) {
1223 err = -EFAULT;
1224 goto out;
1225 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001226
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001227 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001228 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001229 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001230 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001231 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1232 err = -EINVAL;
1233 goto out;
1234 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001235
1236 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001237 if (!task) {
1238 err = -ESRCH;
1239 goto out;
1240 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001241
Ying Han3d5992d2010-10-26 14:21:23 -07001242 task_lock(task);
1243 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001244 err = -EINVAL;
1245 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001246 }
David Rientjesd19d5472010-10-26 14:21:26 -07001247
1248 if (!lock_task_sighand(task, &flags)) {
1249 err = -ESRCH;
1250 goto err_task_lock;
1251 }
1252
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001253 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001254 !capable(CAP_SYS_RESOURCE)) {
1255 err = -EACCES;
1256 goto err_sighand;
1257 }
1258
Ying Han3d5992d2010-10-26 14:21:23 -07001259 if (oom_score_adj != task->signal->oom_score_adj) {
1260 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1261 atomic_inc(&task->mm->oom_disable_count);
1262 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1263 atomic_dec(&task->mm->oom_disable_count);
1264 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001265 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001266 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1267 task->signal->oom_score_adj_min = oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001268 /*
1269 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1270 * always attainable.
1271 */
1272 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1273 task->signal->oom_adj = OOM_DISABLE;
1274 else
1275 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1276 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001277err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001278 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001279err_task_lock:
1280 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001281 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001282out:
1283 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001284}
1285
1286static const struct file_operations proc_oom_score_adj_operations = {
1287 .read = oom_score_adj_read,
1288 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001289 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001290};
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292#ifdef CONFIG_AUDITSYSCALL
1293#define TMPBUFLEN 21
1294static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1295 size_t count, loff_t *ppos)
1296{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001297 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001298 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ssize_t length;
1300 char tmpbuf[TMPBUFLEN];
1301
Eric W. Biederman99f89552006-06-26 00:25:55 -07001302 if (!task)
1303 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001305 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001306 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1308}
1309
1310static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1311 size_t count, loff_t *ppos)
1312{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001313 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 char *page, *tmp;
1315 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 uid_t loginuid;
1317
1318 if (!capable(CAP_AUDIT_CONTROL))
1319 return -EPERM;
1320
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001321 rcu_read_lock();
1322 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1323 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001325 }
1326 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Al Viroe0182902006-05-18 08:28:02 -04001328 if (count >= PAGE_SIZE)
1329 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (*ppos != 0) {
1332 /* No partial writes. */
1333 return -EINVAL;
1334 }
Mel Gormane12ba742007-10-16 01:25:52 -07001335 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (!page)
1337 return -ENOMEM;
1338 length = -EFAULT;
1339 if (copy_from_user(page, buf, count))
1340 goto out_free_page;
1341
Al Viroe0182902006-05-18 08:28:02 -04001342 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 loginuid = simple_strtoul(page, &tmp, 10);
1344 if (tmp == page) {
1345 length = -EINVAL;
1346 goto out_free_page;
1347
1348 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001349 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (likely(length == 0))
1351 length = count;
1352
1353out_free_page:
1354 free_page((unsigned long) page);
1355 return length;
1356}
1357
Arjan van de Ven00977a52007-02-12 00:55:34 -08001358static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 .read = proc_loginuid_read,
1360 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001361 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362};
Eric Paris1e0bd752008-03-13 08:15:31 -04001363
1364static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1365 size_t count, loff_t *ppos)
1366{
1367 struct inode * inode = file->f_path.dentry->d_inode;
1368 struct task_struct *task = get_proc_task(inode);
1369 ssize_t length;
1370 char tmpbuf[TMPBUFLEN];
1371
1372 if (!task)
1373 return -ESRCH;
1374 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1375 audit_get_sessionid(task));
1376 put_task_struct(task);
1377 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1378}
1379
1380static const struct file_operations proc_sessionid_operations = {
1381 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001382 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001383};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#endif
1385
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001386#ifdef CONFIG_FAULT_INJECTION
1387static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1388 size_t count, loff_t *ppos)
1389{
1390 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1391 char buffer[PROC_NUMBUF];
1392 size_t len;
1393 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001394
1395 if (!task)
1396 return -ESRCH;
1397 make_it_fail = task->make_it_fail;
1398 put_task_struct(task);
1399
1400 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001401
1402 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001403}
1404
1405static ssize_t proc_fault_inject_write(struct file * file,
1406 const char __user * buf, size_t count, loff_t *ppos)
1407{
1408 struct task_struct *task;
1409 char buffer[PROC_NUMBUF], *end;
1410 int make_it_fail;
1411
1412 if (!capable(CAP_SYS_RESOURCE))
1413 return -EPERM;
1414 memset(buffer, 0, sizeof(buffer));
1415 if (count > sizeof(buffer) - 1)
1416 count = sizeof(buffer) - 1;
1417 if (copy_from_user(buffer, buf, count))
1418 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001419 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1420 if (*end)
1421 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001422 task = get_proc_task(file->f_dentry->d_inode);
1423 if (!task)
1424 return -ESRCH;
1425 task->make_it_fail = make_it_fail;
1426 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001427
1428 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001429}
1430
Arjan van de Ven00977a52007-02-12 00:55:34 -08001431static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001432 .read = proc_fault_inject_read,
1433 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001434 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001435};
1436#endif
1437
Arjan van de Ven97455122008-01-25 21:08:34 +01001438
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001439#ifdef CONFIG_SCHED_DEBUG
1440/*
1441 * Print out various scheduling related per-task fields:
1442 */
1443static int sched_show(struct seq_file *m, void *v)
1444{
1445 struct inode *inode = m->private;
1446 struct task_struct *p;
1447
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001448 p = get_proc_task(inode);
1449 if (!p)
1450 return -ESRCH;
1451 proc_sched_show_task(p, m);
1452
1453 put_task_struct(p);
1454
1455 return 0;
1456}
1457
1458static ssize_t
1459sched_write(struct file *file, const char __user *buf,
1460 size_t count, loff_t *offset)
1461{
1462 struct inode *inode = file->f_path.dentry->d_inode;
1463 struct task_struct *p;
1464
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001465 p = get_proc_task(inode);
1466 if (!p)
1467 return -ESRCH;
1468 proc_sched_set_task(p);
1469
1470 put_task_struct(p);
1471
1472 return count;
1473}
1474
1475static int sched_open(struct inode *inode, struct file *filp)
1476{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001477 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001478}
1479
1480static const struct file_operations proc_pid_sched_operations = {
1481 .open = sched_open,
1482 .read = seq_read,
1483 .write = sched_write,
1484 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001485 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001486};
1487
1488#endif
1489
Mike Galbraith5091faa2010-11-30 14:18:03 +01001490#ifdef CONFIG_SCHED_AUTOGROUP
1491/*
1492 * Print out autogroup related information:
1493 */
1494static int sched_autogroup_show(struct seq_file *m, void *v)
1495{
1496 struct inode *inode = m->private;
1497 struct task_struct *p;
1498
1499 p = get_proc_task(inode);
1500 if (!p)
1501 return -ESRCH;
1502 proc_sched_autogroup_show_task(p, m);
1503
1504 put_task_struct(p);
1505
1506 return 0;
1507}
1508
1509static ssize_t
1510sched_autogroup_write(struct file *file, const char __user *buf,
1511 size_t count, loff_t *offset)
1512{
1513 struct inode *inode = file->f_path.dentry->d_inode;
1514 struct task_struct *p;
1515 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001516 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001517 int err;
1518
1519 memset(buffer, 0, sizeof(buffer));
1520 if (count > sizeof(buffer) - 1)
1521 count = sizeof(buffer) - 1;
1522 if (copy_from_user(buffer, buf, count))
1523 return -EFAULT;
1524
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001525 err = kstrtoint(strstrip(buffer), 0, &nice);
1526 if (err < 0)
1527 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001528
1529 p = get_proc_task(inode);
1530 if (!p)
1531 return -ESRCH;
1532
1533 err = nice;
1534 err = proc_sched_autogroup_set_nice(p, &err);
1535 if (err)
1536 count = err;
1537
1538 put_task_struct(p);
1539
1540 return count;
1541}
1542
1543static int sched_autogroup_open(struct inode *inode, struct file *filp)
1544{
1545 int ret;
1546
1547 ret = single_open(filp, sched_autogroup_show, NULL);
1548 if (!ret) {
1549 struct seq_file *m = filp->private_data;
1550
1551 m->private = inode;
1552 }
1553 return ret;
1554}
1555
1556static const struct file_operations proc_pid_sched_autogroup_operations = {
1557 .open = sched_autogroup_open,
1558 .read = seq_read,
1559 .write = sched_autogroup_write,
1560 .llseek = seq_lseek,
1561 .release = single_release,
1562};
1563
1564#endif /* CONFIG_SCHED_AUTOGROUP */
1565
john stultz4614a696b2009-12-14 18:00:05 -08001566static ssize_t comm_write(struct file *file, const char __user *buf,
1567 size_t count, loff_t *offset)
1568{
1569 struct inode *inode = file->f_path.dentry->d_inode;
1570 struct task_struct *p;
1571 char buffer[TASK_COMM_LEN];
1572
1573 memset(buffer, 0, sizeof(buffer));
1574 if (count > sizeof(buffer) - 1)
1575 count = sizeof(buffer) - 1;
1576 if (copy_from_user(buffer, buf, count))
1577 return -EFAULT;
1578
1579 p = get_proc_task(inode);
1580 if (!p)
1581 return -ESRCH;
1582
1583 if (same_thread_group(current, p))
1584 set_task_comm(p, buffer);
1585 else
1586 count = -EINVAL;
1587
1588 put_task_struct(p);
1589
1590 return count;
1591}
1592
1593static int comm_show(struct seq_file *m, void *v)
1594{
1595 struct inode *inode = m->private;
1596 struct task_struct *p;
1597
1598 p = get_proc_task(inode);
1599 if (!p)
1600 return -ESRCH;
1601
1602 task_lock(p);
1603 seq_printf(m, "%s\n", p->comm);
1604 task_unlock(p);
1605
1606 put_task_struct(p);
1607
1608 return 0;
1609}
1610
1611static int comm_open(struct inode *inode, struct file *filp)
1612{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001613 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001614}
1615
1616static const struct file_operations proc_pid_set_comm_operations = {
1617 .open = comm_open,
1618 .read = seq_read,
1619 .write = comm_write,
1620 .llseek = seq_lseek,
1621 .release = single_release,
1622};
1623
Matt Helsley925d1c42008-04-29 01:01:36 -07001624static int proc_exe_link(struct inode *inode, struct path *exe_path)
1625{
1626 struct task_struct *task;
1627 struct mm_struct *mm;
1628 struct file *exe_file;
1629
1630 task = get_proc_task(inode);
1631 if (!task)
1632 return -ENOENT;
1633 mm = get_task_mm(task);
1634 put_task_struct(task);
1635 if (!mm)
1636 return -ENOENT;
1637 exe_file = get_mm_exe_file(mm);
1638 mmput(mm);
1639 if (exe_file) {
1640 *exe_path = exe_file->f_path;
1641 path_get(&exe_file->f_path);
1642 fput(exe_file);
1643 return 0;
1644 } else
1645 return -ENOENT;
1646}
1647
Al Viro008b1502005-08-20 00:17:39 +01001648static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 struct inode *inode = dentry->d_inode;
1651 int error = -EACCES;
1652
1653 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001654 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Eric W. Biederman778c1142006-06-26 00:25:58 -07001656 /* Are we allowed to snoop on the tasks file descriptors? */
1657 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001660 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661out:
Al Viro008b1502005-08-20 00:17:39 +01001662 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001665static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Mel Gormane12ba742007-10-16 01:25:52 -07001667 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001668 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int len;
1670
1671 if (!tmp)
1672 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001673
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001674 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001675 len = PTR_ERR(pathname);
1676 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001678 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (len > buflen)
1681 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001682 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 len = -EFAULT;
1684 out:
1685 free_page((unsigned long)tmp);
1686 return len;
1687}
1688
1689static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1690{
1691 int error = -EACCES;
1692 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001693 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Eric W. Biederman778c1142006-06-26 00:25:58 -07001695 /* Are we allowed to snoop on the tasks file descriptors? */
1696 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001699 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (error)
1701 goto out;
1702
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001703 error = do_proc_readlink(&path, buffer, buflen);
1704 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return error;
1707}
1708
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001709static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001711 .follow_link = proc_pid_follow_link,
1712 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713};
1714
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001715
1716/* building an inode */
1717
1718static int task_dumpable(struct task_struct *task)
1719{
1720 int dumpable = 0;
1721 struct mm_struct *mm;
1722
1723 task_lock(task);
1724 mm = task->mm;
1725 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001726 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001727 task_unlock(task);
1728 if(dumpable == 1)
1729 return 1;
1730 return 0;
1731}
1732
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001733struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001734{
1735 struct inode * inode;
1736 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001737 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001738
1739 /* We need a new inode */
1740
1741 inode = new_inode(sb);
1742 if (!inode)
1743 goto out;
1744
1745 /* Common stuff */
1746 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001747 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001748 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001749 inode->i_op = &proc_def_inode_operations;
1750
1751 /*
1752 * grab the reference to task.
1753 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001754 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001755 if (!ei->pid)
1756 goto out_unlock;
1757
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001758 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001759 rcu_read_lock();
1760 cred = __task_cred(task);
1761 inode->i_uid = cred->euid;
1762 inode->i_gid = cred->egid;
1763 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001764 }
1765 security_task_to_inode(task, inode);
1766
1767out:
1768 return inode;
1769
1770out_unlock:
1771 iput(inode);
1772 return NULL;
1773}
1774
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001775int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001776{
1777 struct inode *inode = dentry->d_inode;
1778 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001779 const struct cred *cred;
1780
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781 generic_fillattr(inode, stat);
1782
1783 rcu_read_lock();
1784 stat->uid = 0;
1785 stat->gid = 0;
1786 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1787 if (task) {
1788 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1789 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001790 cred = __task_cred(task);
1791 stat->uid = cred->euid;
1792 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001793 }
1794 }
1795 rcu_read_unlock();
1796 return 0;
1797}
1798
1799/* dentry stuff */
1800
1801/*
1802 * Exceptional case: normally we are not allowed to unhash a busy
1803 * directory. In this case, however, we can do it - no aliasing problems
1804 * due to the way we treat inodes.
1805 *
1806 * Rewrite the inode's ownerships here because the owning task may have
1807 * performed a setuid(), etc.
1808 *
1809 * Before the /proc/pid/status file was created the only way to read
1810 * the effective uid of a /process was to stat /proc/pid. Reading
1811 * /proc/pid/status is slow enough that procps and other packages
1812 * kept stating /proc/pid. To keep the rules in /proc simple I have
1813 * made this apply to all per process world readable and executable
1814 * directories.
1815 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001816int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001817{
Nick Piggin34286d62011-01-07 17:49:57 +11001818 struct inode *inode;
1819 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001820 const struct cred *cred;
1821
Nick Piggin34286d62011-01-07 17:49:57 +11001822 if (nd && nd->flags & LOOKUP_RCU)
1823 return -ECHILD;
1824
1825 inode = dentry->d_inode;
1826 task = get_proc_task(inode);
1827
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001828 if (task) {
1829 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1830 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001831 rcu_read_lock();
1832 cred = __task_cred(task);
1833 inode->i_uid = cred->euid;
1834 inode->i_gid = cred->egid;
1835 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001836 } else {
1837 inode->i_uid = 0;
1838 inode->i_gid = 0;
1839 }
1840 inode->i_mode &= ~(S_ISUID | S_ISGID);
1841 security_task_to_inode(task, inode);
1842 put_task_struct(task);
1843 return 1;
1844 }
1845 d_drop(dentry);
1846 return 0;
1847}
1848
Nick Pigginfe15ce42011-01-07 17:49:23 +11001849static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001850{
1851 /* Is the task we represent dead?
1852 * If so, then don't put the dentry on the lru list,
1853 * kill it immediately.
1854 */
1855 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1856}
1857
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001858const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001859{
1860 .d_revalidate = pid_revalidate,
1861 .d_delete = pid_delete_dentry,
1862};
1863
1864/* Lookups */
1865
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001866/*
1867 * Fill a directory entry.
1868 *
1869 * If possible create the dcache entry and derive our inode number and
1870 * file type from dcache entry.
1871 *
1872 * Since all of the proc inode numbers are dynamically generated, the inode
1873 * numbers do not exist until the inode is cache. This means creating the
1874 * the dcache entry in readdir is necessary to keep the inode numbers
1875 * reported by readdir in sync with the inode numbers reported
1876 * by stat.
1877 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001878int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1879 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001880 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001881{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001882 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001883 struct inode *inode;
1884 struct qstr qname;
1885 ino_t ino = 0;
1886 unsigned type = DT_UNKNOWN;
1887
1888 qname.name = name;
1889 qname.len = len;
1890 qname.hash = full_name_hash(name, len);
1891
1892 child = d_lookup(dir, &qname);
1893 if (!child) {
1894 struct dentry *new;
1895 new = d_alloc(dir, &qname);
1896 if (new) {
1897 child = instantiate(dir->d_inode, new, task, ptr);
1898 if (child)
1899 dput(new);
1900 else
1901 child = new;
1902 }
1903 }
1904 if (!child || IS_ERR(child) || !child->d_inode)
1905 goto end_instantiate;
1906 inode = child->d_inode;
1907 if (inode) {
1908 ino = inode->i_ino;
1909 type = inode->i_mode >> 12;
1910 }
1911 dput(child);
1912end_instantiate:
1913 if (!ino)
1914 ino = find_inode_number(dir, &qname);
1915 if (!ino)
1916 ino = 1;
1917 return filldir(dirent, name, len, filp->f_pos, ino, type);
1918}
1919
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001920static unsigned name_to_int(struct dentry *dentry)
1921{
1922 const char *name = dentry->d_name.name;
1923 int len = dentry->d_name.len;
1924 unsigned n = 0;
1925
1926 if (len > 1 && *name == '0')
1927 goto out;
1928 while (len-- > 0) {
1929 unsigned c = *name++ - '0';
1930 if (c > 9)
1931 goto out;
1932 if (n >= (~0U-9)/10)
1933 goto out;
1934 n *= 10;
1935 n += c;
1936 }
1937 return n;
1938out:
1939 return ~0U;
1940}
1941
Miklos Szeredi27932742007-05-08 00:26:17 -07001942#define PROC_FDINFO_MAX 64
1943
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001944static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001945{
1946 struct task_struct *task = get_proc_task(inode);
1947 struct files_struct *files = NULL;
1948 struct file *file;
1949 int fd = proc_fd(inode);
1950
1951 if (task) {
1952 files = get_files_struct(task);
1953 put_task_struct(task);
1954 }
1955 if (files) {
1956 /*
1957 * We are not taking a ref to the file structure, so we must
1958 * hold ->file_lock.
1959 */
1960 spin_lock(&files->file_lock);
1961 file = fcheck_files(files, fd);
1962 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001963 if (path) {
1964 *path = file->f_path;
1965 path_get(&file->f_path);
1966 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001967 if (info)
1968 snprintf(info, PROC_FDINFO_MAX,
1969 "pos:\t%lli\n"
1970 "flags:\t0%o\n",
1971 (long long) file->f_pos,
1972 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001973 spin_unlock(&files->file_lock);
1974 put_files_struct(files);
1975 return 0;
1976 }
1977 spin_unlock(&files->file_lock);
1978 put_files_struct(files);
1979 }
1980 return -ENOENT;
1981}
1982
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001983static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001984{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001985 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001986}
1987
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001988static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1989{
Nick Piggin34286d62011-01-07 17:49:57 +11001990 struct inode *inode;
1991 struct task_struct *task;
1992 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001993 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001994 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001995
Nick Piggin34286d62011-01-07 17:49:57 +11001996 if (nd && nd->flags & LOOKUP_RCU)
1997 return -ECHILD;
1998
1999 inode = dentry->d_inode;
2000 task = get_proc_task(inode);
2001 fd = proc_fd(inode);
2002
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002003 if (task) {
2004 files = get_files_struct(task);
2005 if (files) {
2006 rcu_read_lock();
2007 if (fcheck_files(files, fd)) {
2008 rcu_read_unlock();
2009 put_files_struct(files);
2010 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002011 rcu_read_lock();
2012 cred = __task_cred(task);
2013 inode->i_uid = cred->euid;
2014 inode->i_gid = cred->egid;
2015 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002016 } else {
2017 inode->i_uid = 0;
2018 inode->i_gid = 0;
2019 }
2020 inode->i_mode &= ~(S_ISUID | S_ISGID);
2021 security_task_to_inode(task, inode);
2022 put_task_struct(task);
2023 return 1;
2024 }
2025 rcu_read_unlock();
2026 put_files_struct(files);
2027 }
2028 put_task_struct(task);
2029 }
2030 d_drop(dentry);
2031 return 0;
2032}
2033
Al Virod72f71e2009-02-20 05:58:47 +00002034static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002035{
2036 .d_revalidate = tid_fd_revalidate,
2037 .d_delete = pid_delete_dentry,
2038};
2039
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002040static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002041 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002042{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002043 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002044 struct file *file;
2045 struct files_struct *files;
2046 struct inode *inode;
2047 struct proc_inode *ei;
2048 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002049
Eric W. Biederman61a28782006-10-02 02:18:49 -07002050 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002051 if (!inode)
2052 goto out;
2053 ei = PROC_I(inode);
2054 ei->fd = fd;
2055 files = get_files_struct(task);
2056 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002057 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002058 inode->i_mode = S_IFLNK;
2059
2060 /*
2061 * We are not taking a ref to the file structure, so we must
2062 * hold ->file_lock.
2063 */
2064 spin_lock(&files->file_lock);
2065 file = fcheck_files(files, fd);
2066 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002067 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04002068 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002069 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04002070 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002071 inode->i_mode |= S_IWUSR | S_IXUSR;
2072 spin_unlock(&files->file_lock);
2073 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002074
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002075 inode->i_op = &proc_pid_link_inode_operations;
2076 inode->i_size = 64;
2077 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002078 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002079 d_add(dentry, inode);
2080 /* Close the race of the process dying before we return the dentry */
2081 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002082 error = NULL;
2083
2084 out:
2085 return error;
2086out_unlock:
2087 spin_unlock(&files->file_lock);
2088 put_files_struct(files);
2089out_iput:
2090 iput(inode);
2091 goto out;
2092}
2093
Miklos Szeredi27932742007-05-08 00:26:17 -07002094static struct dentry *proc_lookupfd_common(struct inode *dir,
2095 struct dentry *dentry,
2096 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002097{
2098 struct task_struct *task = get_proc_task(dir);
2099 unsigned fd = name_to_int(dentry);
2100 struct dentry *result = ERR_PTR(-ENOENT);
2101
2102 if (!task)
2103 goto out_no_task;
2104 if (fd == ~0U)
2105 goto out;
2106
Miklos Szeredi27932742007-05-08 00:26:17 -07002107 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002108out:
2109 put_task_struct(task);
2110out_no_task:
2111 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002112}
2113
Miklos Szeredi27932742007-05-08 00:26:17 -07002114static int proc_readfd_common(struct file * filp, void * dirent,
2115 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002117 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07002118 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002119 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07002120 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 struct files_struct * files;
2123
2124 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002125 if (!p)
2126 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 fd = filp->f_pos;
2130 switch (fd) {
2131 case 0:
2132 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2133 goto out;
2134 filp->f_pos++;
2135 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07002136 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2138 goto out;
2139 filp->f_pos++;
2140 default:
2141 files = get_files_struct(p);
2142 if (!files)
2143 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002144 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04002146 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07002148 char name[PROC_NUMBUF];
2149 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 if (!fcheck_files(files, fd))
2152 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002153 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Miklos Szeredi27932742007-05-08 00:26:17 -07002155 len = snprintf(name, sizeof(name), "%d", fd);
2156 if (proc_fill_cache(filp, dirent, filldir,
2157 name, len, instantiate,
2158 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07002159 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 break;
2161 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002162 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002164 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 put_files_struct(files);
2166 }
2167out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002168 put_task_struct(p);
2169out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return retval;
2171}
2172
Miklos Szeredi27932742007-05-08 00:26:17 -07002173static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2174 struct nameidata *nd)
2175{
2176 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2177}
2178
2179static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2180{
2181 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2182}
2183
2184static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2185 size_t len, loff_t *ppos)
2186{
2187 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002188 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002189 if (!err)
2190 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2191 return err;
2192}
2193
2194static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002195 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002196 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002197 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002198};
2199
Arjan van de Ven00977a52007-02-12 00:55:34 -08002200static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 .read = generic_read_dir,
2202 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002203 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204};
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002207 * /proc/pid/fd needs a special permission handler so that a process can still
2208 * access /proc/self/fd after it has executed a setuid().
2209 */
Nick Pigginb74c79e2011-01-07 17:49:58 +11002210static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002211{
2212 int rv;
2213
Nick Pigginb74c79e2011-01-07 17:49:58 +11002214 if (flags & IPERM_FLAG_RCU)
2215 return -ECHILD;
2216 rv = generic_permission(inode, mask, flags, NULL);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002217 if (rv == 0)
2218 return 0;
2219 if (task_pid(current) == proc_pid(inode))
2220 rv = 0;
2221 return rv;
2222}
2223
2224/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 * proc directories can do almost nothing..
2226 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002227static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002229 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002230 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231};
2232
Miklos Szeredi27932742007-05-08 00:26:17 -07002233static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2234 struct dentry *dentry, struct task_struct *task, const void *ptr)
2235{
2236 unsigned fd = *(unsigned *)ptr;
2237 struct inode *inode;
2238 struct proc_inode *ei;
2239 struct dentry *error = ERR_PTR(-ENOENT);
2240
2241 inode = proc_pid_make_inode(dir->i_sb, task);
2242 if (!inode)
2243 goto out;
2244 ei = PROC_I(inode);
2245 ei->fd = fd;
2246 inode->i_mode = S_IFREG | S_IRUSR;
2247 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002248 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002249 d_add(dentry, inode);
2250 /* Close the race of the process dying before we return the dentry */
2251 if (tid_fd_revalidate(dentry, NULL))
2252 error = NULL;
2253
2254 out:
2255 return error;
2256}
2257
2258static struct dentry *proc_lookupfdinfo(struct inode *dir,
2259 struct dentry *dentry,
2260 struct nameidata *nd)
2261{
2262 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2263}
2264
2265static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2266{
2267 return proc_readfd_common(filp, dirent, filldir,
2268 proc_fdinfo_instantiate);
2269}
2270
2271static const struct file_operations proc_fdinfo_operations = {
2272 .read = generic_read_dir,
2273 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002274 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002275};
2276
2277/*
2278 * proc directories can do almost nothing..
2279 */
2280static const struct inode_operations proc_fdinfo_inode_operations = {
2281 .lookup = proc_lookupfdinfo,
2282 .setattr = proc_setattr,
2283};
2284
2285
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002286static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002287 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002288{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002289 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002290 struct inode *inode;
2291 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002292 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002293
Eric W. Biederman61a28782006-10-02 02:18:49 -07002294 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002295 if (!inode)
2296 goto out;
2297
2298 ei = PROC_I(inode);
2299 inode->i_mode = p->mode;
2300 if (S_ISDIR(inode->i_mode))
2301 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2302 if (p->iop)
2303 inode->i_op = p->iop;
2304 if (p->fop)
2305 inode->i_fop = p->fop;
2306 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002307 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002308 d_add(dentry, inode);
2309 /* Close the race of the process dying before we return the dentry */
2310 if (pid_revalidate(dentry, NULL))
2311 error = NULL;
2312out:
2313 return error;
2314}
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316static struct dentry *proc_pident_lookup(struct inode *dir,
2317 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002318 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002319 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002321 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002322 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002323 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002325 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Eric W. Biederman99f89552006-06-26 00:25:55 -07002327 if (!task)
2328 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002330 /*
2331 * Yes, it does not scale. And it should not. Don't add
2332 * new entries into /proc/<tgid>/ without very good reasons.
2333 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002334 last = &ents[nents - 1];
2335 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (p->len != dentry->d_name.len)
2337 continue;
2338 if (!memcmp(dentry->d_name.name, p->name, p->len))
2339 break;
2340 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002341 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 goto out;
2343
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002344 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002346 put_task_struct(task);
2347out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002348 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Eric Dumazetc5141e62007-05-08 00:26:15 -07002351static int proc_pident_fill_cache(struct file *filp, void *dirent,
2352 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002353{
2354 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2355 proc_pident_instantiate, task, p);
2356}
2357
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002358static int proc_pident_readdir(struct file *filp,
2359 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002360 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002361{
2362 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002363 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002364 struct inode *inode = dentry->d_inode;
2365 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002366 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002367 ino_t ino;
2368 int ret;
2369
2370 ret = -ENOENT;
2371 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002372 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002373
2374 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002375 i = filp->f_pos;
2376 switch (i) {
2377 case 0:
2378 ino = inode->i_ino;
2379 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2380 goto out;
2381 i++;
2382 filp->f_pos++;
2383 /* fall through */
2384 case 1:
2385 ino = parent_ino(dentry);
2386 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2387 goto out;
2388 i++;
2389 filp->f_pos++;
2390 /* fall through */
2391 default:
2392 i -= 2;
2393 if (i >= nents) {
2394 ret = 1;
2395 goto out;
2396 }
2397 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002398 last = &ents[nents - 1];
2399 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002400 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002401 goto out;
2402 filp->f_pos++;
2403 p++;
2404 }
2405 }
2406
2407 ret = 1;
2408out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002409 put_task_struct(task);
2410out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002411 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002415static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2416 size_t count, loff_t *ppos)
2417{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002418 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002419 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002420 ssize_t length;
2421 struct task_struct *task = get_proc_task(inode);
2422
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002423 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002424 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002425
2426 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002427 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002428 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002429 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002430 if (length > 0)
2431 length = simple_read_from_buffer(buf, count, ppos, p, length);
2432 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002433 return length;
2434}
2435
2436static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2437 size_t count, loff_t *ppos)
2438{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002439 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002440 char *page;
2441 ssize_t length;
2442 struct task_struct *task = get_proc_task(inode);
2443
2444 length = -ESRCH;
2445 if (!task)
2446 goto out_no_task;
2447 if (count > PAGE_SIZE)
2448 count = PAGE_SIZE;
2449
2450 /* No partial writes. */
2451 length = -EINVAL;
2452 if (*ppos != 0)
2453 goto out;
2454
2455 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002456 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002457 if (!page)
2458 goto out;
2459
2460 length = -EFAULT;
2461 if (copy_from_user(page, buf, count))
2462 goto out_free;
2463
David Howells107db7c2009-05-08 13:55:27 +01002464 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002465 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002466 if (length < 0)
2467 goto out_free;
2468
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002469 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002470 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002471 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002472 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002473out_free:
2474 free_page((unsigned long) page);
2475out:
2476 put_task_struct(task);
2477out_no_task:
2478 return length;
2479}
2480
Arjan van de Ven00977a52007-02-12 00:55:34 -08002481static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002482 .read = proc_pid_attr_read,
2483 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002484 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002485};
2486
Eric Dumazetc5141e62007-05-08 00:26:15 -07002487static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002488 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2489 REG("prev", S_IRUGO, proc_pid_attr_operations),
2490 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2491 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2492 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2493 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002494};
2495
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002496static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 void * dirent, filldir_t filldir)
2498{
2499 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002500 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Arjan van de Ven00977a52007-02-12 00:55:34 -08002503static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002505 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002506 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507};
2508
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002509static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 struct dentry *dentry, struct nameidata *nd)
2511{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002512 return proc_pident_lookup(dir, dentry,
2513 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002516static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002517 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002518 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002519 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520};
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522#endif
2523
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002524#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002525static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2526 size_t count, loff_t *ppos)
2527{
2528 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2529 struct mm_struct *mm;
2530 char buffer[PROC_NUMBUF];
2531 size_t len;
2532 int ret;
2533
2534 if (!task)
2535 return -ESRCH;
2536
2537 ret = 0;
2538 mm = get_task_mm(task);
2539 if (mm) {
2540 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2541 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2542 MMF_DUMP_FILTER_SHIFT));
2543 mmput(mm);
2544 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2545 }
2546
2547 put_task_struct(task);
2548
2549 return ret;
2550}
2551
2552static ssize_t proc_coredump_filter_write(struct file *file,
2553 const char __user *buf,
2554 size_t count,
2555 loff_t *ppos)
2556{
2557 struct task_struct *task;
2558 struct mm_struct *mm;
2559 char buffer[PROC_NUMBUF], *end;
2560 unsigned int val;
2561 int ret;
2562 int i;
2563 unsigned long mask;
2564
2565 ret = -EFAULT;
2566 memset(buffer, 0, sizeof(buffer));
2567 if (count > sizeof(buffer) - 1)
2568 count = sizeof(buffer) - 1;
2569 if (copy_from_user(buffer, buf, count))
2570 goto out_no_task;
2571
2572 ret = -EINVAL;
2573 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2574 if (*end == '\n')
2575 end++;
2576 if (end - buffer == 0)
2577 goto out_no_task;
2578
2579 ret = -ESRCH;
2580 task = get_proc_task(file->f_dentry->d_inode);
2581 if (!task)
2582 goto out_no_task;
2583
2584 ret = end - buffer;
2585 mm = get_task_mm(task);
2586 if (!mm)
2587 goto out_no_mm;
2588
2589 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2590 if (val & mask)
2591 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2592 else
2593 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2594 }
2595
2596 mmput(mm);
2597 out_no_mm:
2598 put_task_struct(task);
2599 out_no_task:
2600 return ret;
2601}
2602
2603static const struct file_operations proc_coredump_filter_operations = {
2604 .read = proc_coredump_filter_read,
2605 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002606 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002607};
2608#endif
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610/*
2611 * /proc/self:
2612 */
2613static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2614 int buflen)
2615{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002616 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002617 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002618 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002619 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002620 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002621 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return vfs_readlink(dentry,buffer,buflen,tmp);
2623}
2624
Al Viro008b1502005-08-20 00:17:39 +01002625static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002627 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002628 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002629 char *name = ERR_PTR(-ENOENT);
2630 if (tgid) {
2631 name = __getname();
2632 if (!name)
2633 name = ERR_PTR(-ENOMEM);
2634 else
2635 sprintf(name, "%d", tgid);
2636 }
2637 nd_set_link(nd, name);
2638 return NULL;
2639}
2640
2641static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2642 void *cookie)
2643{
2644 char *s = nd_get_link(nd);
2645 if (!IS_ERR(s))
2646 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002649static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 .readlink = proc_self_readlink,
2651 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002652 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653};
2654
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002655/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002656 * proc base
2657 *
2658 * These are the directory entries in the root directory of /proc
2659 * that properly belong to the /proc filesystem, as they describe
2660 * describe something that is process related.
2661 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002662static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002663 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002664 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002665};
2666
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002667static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002668 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002669{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002670 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002671 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002672 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002673 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002674
2675 /* Allocate the inode */
2676 error = ERR_PTR(-ENOMEM);
2677 inode = new_inode(dir->i_sb);
2678 if (!inode)
2679 goto out;
2680
2681 /* Initialize the inode */
2682 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002683 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002684 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002685
2686 /*
2687 * grab the reference to the task.
2688 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002689 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002690 if (!ei->pid)
2691 goto out_iput;
2692
Eric W. Biederman801199c2006-10-02 02:18:48 -07002693 inode->i_mode = p->mode;
2694 if (S_ISDIR(inode->i_mode))
2695 inode->i_nlink = 2;
2696 if (S_ISLNK(inode->i_mode))
2697 inode->i_size = 64;
2698 if (p->iop)
2699 inode->i_op = p->iop;
2700 if (p->fop)
2701 inode->i_fop = p->fop;
2702 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002703 d_add(dentry, inode);
2704 error = NULL;
2705out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002706 return error;
2707out_iput:
2708 iput(inode);
2709 goto out;
2710}
2711
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002712static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2713{
2714 struct dentry *error;
2715 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002716 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002717
2718 error = ERR_PTR(-ENOENT);
2719
2720 if (!task)
2721 goto out_no_task;
2722
2723 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002724 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2725 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002726 if (p->len != dentry->d_name.len)
2727 continue;
2728 if (!memcmp(dentry->d_name.name, p->name, p->len))
2729 break;
2730 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002731 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002732 goto out;
2733
2734 error = proc_base_instantiate(dir, dentry, task, p);
2735
2736out:
2737 put_task_struct(task);
2738out_no_task:
2739 return error;
2740}
2741
Eric Dumazetc5141e62007-05-08 00:26:15 -07002742static int proc_base_fill_cache(struct file *filp, void *dirent,
2743 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002744{
2745 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2746 proc_base_instantiate, task, p);
2747}
2748
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002749#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002750static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002751{
Andrea Righi940389b2008-07-28 00:48:12 +02002752 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002753 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002754
Andrea Righi59954772008-07-27 17:29:15 +02002755 if (whole && lock_task_sighand(task, &flags)) {
2756 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002757
Andrea Righi59954772008-07-27 17:29:15 +02002758 task_io_accounting_add(&acct, &task->signal->ioac);
2759 while_each_thread(task, t)
2760 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002761
Andrea Righi59954772008-07-27 17:29:15 +02002762 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002763 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002764 return sprintf(buffer,
2765 "rchar: %llu\n"
2766 "wchar: %llu\n"
2767 "syscr: %llu\n"
2768 "syscw: %llu\n"
2769 "read_bytes: %llu\n"
2770 "write_bytes: %llu\n"
2771 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002772 (unsigned long long)acct.rchar,
2773 (unsigned long long)acct.wchar,
2774 (unsigned long long)acct.syscr,
2775 (unsigned long long)acct.syscw,
2776 (unsigned long long)acct.read_bytes,
2777 (unsigned long long)acct.write_bytes,
2778 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002779}
Andrea Righi297c5d92008-07-25 01:48:49 -07002780
2781static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2782{
2783 return do_io_accounting(task, buffer, 0);
2784}
2785
2786static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2787{
2788 return do_io_accounting(task, buffer, 1);
2789}
2790#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002791
Kees Cook47830722008-10-06 03:11:58 +04002792static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2793 struct pid *pid, struct task_struct *task)
2794{
Al Viroa9712bc2011-03-23 15:52:50 -04002795 int err = lock_trace(task);
2796 if (!err) {
2797 seq_printf(m, "%08x\n", task->personality);
2798 unlock_trace(task);
2799 }
2800 return err;
Kees Cook47830722008-10-06 03:11:58 +04002801}
2802
Eric W. Biederman801199c2006-10-02 02:18:48 -07002803/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002804 * Thread groups
2805 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002806static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002807static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002808
Eric Dumazetc5141e62007-05-08 00:26:15 -07002809static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002810 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2811 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2812 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002813 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002814#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002815 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002816#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002817 REG("environ", S_IRUSR, proc_environ_operations),
2818 INF("auxv", S_IRUSR, proc_pid_auxv),
2819 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002820 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002821 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002822#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002823 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002824#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002825#ifdef CONFIG_SCHED_AUTOGROUP
2826 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2827#endif
john stultz4614a696b2009-12-14 18:00:05 -08002828 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002829#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002830 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002831#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002832 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2833 ONE("stat", S_IRUGO, proc_tgid_stat),
2834 ONE("statm", S_IRUGO, proc_pid_statm),
2835 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002836#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002837 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002838#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002839 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2840 LNK("cwd", proc_cwd_link),
2841 LNK("root", proc_root_link),
2842 LNK("exe", proc_exe_link),
2843 REG("mounts", S_IRUGO, proc_mounts_operations),
2844 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2845 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002846#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002847 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2848 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002849 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002850#endif
2851#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002852 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002853#endif
2854#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002855 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002856#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002857#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002858 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002859#endif
2860#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002861 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002862#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002863#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002864 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002865#endif
Paul Menage8793d852007-10-18 23:39:39 -07002866#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002867 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002868#endif
Paul Menagea4243162007-10-18 23:39:35 -07002869#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002870 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002871#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002872 INF("oom_score", S_IRUGO, proc_oom_score),
Mike Chan766e9932008-04-24 10:22:26 -07002873 ANDROID("oom_adj",S_IRUGO|S_IWUSR, oom_adjust),
David Rientjesa63d83f2010-08-09 17:19:46 -07002874 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002875#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002876 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2877 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002878#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002879#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002880 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002881#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002882#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002883 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002884#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002885#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002886 INF("io", S_IRUGO, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002887#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002888#ifdef CONFIG_HARDWALL
2889 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2890#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002891};
2892
2893static int proc_tgid_base_readdir(struct file * filp,
2894 void * dirent, filldir_t filldir)
2895{
2896 return proc_pident_readdir(filp,dirent,filldir,
2897 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2898}
2899
Arjan van de Ven00977a52007-02-12 00:55:34 -08002900static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002901 .read = generic_read_dir,
2902 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002903 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002904};
2905
2906static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002907 return proc_pident_lookup(dir, dentry,
2908 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002909}
2910
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002911static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002912 .lookup = proc_tgid_base_lookup,
2913 .getattr = pid_getattr,
2914 .setattr = proc_setattr,
2915};
2916
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002917static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002919 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002920 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002921 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Eric W. Biederman48e64842006-06-26 00:25:48 -07002923 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002924 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2925 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002926 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002927 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002928 d_drop(dentry);
2929 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Eric W. Biederman48e64842006-06-26 00:25:48 -07002932 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002933 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2934 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002935 if (!leader)
2936 goto out;
2937
2938 name.name = "task";
2939 name.len = strlen(name.name);
2940 dir = d_hash_and_lookup(leader, &name);
2941 if (!dir)
2942 goto out_put_leader;
2943
2944 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002945 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002946 dentry = d_hash_and_lookup(dir, &name);
2947 if (dentry) {
2948 shrink_dcache_parent(dentry);
2949 d_drop(dentry);
2950 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002952
2953 dput(dir);
2954out_put_leader:
2955 dput(leader);
2956out:
2957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958}
2959
Randy Dunlap0895e912007-10-21 21:00:10 -07002960/**
2961 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2962 * @task: task that should be flushed.
2963 *
2964 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002965 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002966 * in. This call is supposed to do all of this job.
2967 *
2968 * Looks in the dcache for
2969 * /proc/@pid
2970 * /proc/@tgid/task/@pid
2971 * if either directory is present flushes it and all of it'ts children
2972 * from the dcache.
2973 *
2974 * It is safe and reasonable to cache /proc entries for a task until
2975 * that task exits. After that they just clog up the dcache with
2976 * useless entries, possibly causing useful dcache entries to be
2977 * flushed instead. This routine is proved to flush those useless
2978 * dcache entries at process exit time.
2979 *
2980 * NOTE: This routine is just an optimization so it does not guarantee
2981 * that no dcache entries will exist at process exit time it
2982 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002983 */
2984
2985void proc_flush_task(struct task_struct *task)
2986{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002987 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002988 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002989 struct upid *upid;
2990
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002991 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002992 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002993
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002994 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002995 upid = &pid->numbers[i];
2996 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002997 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002998 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002999
3000 upid = &pid->numbers[pid->level];
3001 if (upid->nr == 1)
3002 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003003}
3004
Adrian Bunk9711ef92006-12-06 20:38:31 -08003005static struct dentry *proc_pid_instantiate(struct inode *dir,
3006 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003007 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003008{
3009 struct dentry *error = ERR_PTR(-ENOENT);
3010 struct inode *inode;
3011
Eric W. Biederman61a28782006-10-02 02:18:49 -07003012 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003013 if (!inode)
3014 goto out;
3015
3016 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3017 inode->i_op = &proc_tgid_base_inode_operations;
3018 inode->i_fop = &proc_tgid_base_operations;
3019 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003020
3021 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
3022 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003023
Nick Pigginfb045ad2011-01-07 17:49:55 +11003024 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003025
3026 d_add(dentry, inode);
3027 /* Close the race of the process dying before we return the dentry */
3028 if (pid_revalidate(dentry, NULL))
3029 error = NULL;
3030out:
3031 return error;
3032}
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3035{
Dan Carpenter73d36462010-05-26 14:43:25 -07003036 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003039 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Eric W. Biederman801199c2006-10-02 02:18:48 -07003041 result = proc_base_lookup(dir, dentry);
3042 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3043 goto out;
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 tgid = name_to_int(dentry);
3046 if (tgid == ~0U)
3047 goto out;
3048
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003049 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003050 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003051 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 if (task)
3053 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003054 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 if (!task)
3056 goto out;
3057
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003058 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003059 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07003061 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003065 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003066 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003068struct tgid_iter {
3069 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003070 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003071};
3072static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3073{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003074 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003076 if (iter.task)
3077 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003078 rcu_read_lock();
3079retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003080 iter.task = NULL;
3081 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003082 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003083 iter.tgid = pid_nr_ns(pid, ns);
3084 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003085 /* What we to know is if the pid we have find is the
3086 * pid of a thread_group_leader. Testing for task
3087 * being a thread_group_leader is the obvious thing
3088 * todo but there is a window when it fails, due to
3089 * the pid transfer logic in de_thread.
3090 *
3091 * So we perform the straight forward test of seeing
3092 * if the pid we have found is the pid of a thread
3093 * group leader, and don't worry if the task we have
3094 * found doesn't happen to be a thread group leader.
3095 * As we don't care in the case of readdir.
3096 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003097 if (!iter.task || !has_group_leader_pid(iter.task)) {
3098 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003099 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003100 }
3101 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003103 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003104 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105}
3106
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003107#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Eric W. Biederman61a28782006-10-02 02:18:49 -07003109static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003110 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003111{
3112 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003113 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003114 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003115 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003116}
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118/* for the /proc/ directory itself, after non-process stuff has been done */
3119int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3120{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003121 unsigned int nr;
3122 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003123 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003124 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003126 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3127 goto out_no_task;
3128 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3129
3130 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003131 if (!reaper)
3132 goto out_no_task;
3133
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003134 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003135 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003136 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
3139
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003140 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003141 iter.task = NULL;
3142 iter.tgid = filp->f_pos - TGID_OFFSET;
3143 for (iter = next_tgid(ns, iter);
3144 iter.task;
3145 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3146 filp->f_pos = iter.tgid + TGID_OFFSET;
3147 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3148 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003152 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3153out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003154 put_task_struct(reaper);
3155out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return 0;
3157}
3158
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003159/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003160 * Tasks
3161 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003162static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003163 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003164 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003165 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003166 REG("environ", S_IRUSR, proc_environ_operations),
3167 INF("auxv", S_IRUSR, proc_pid_auxv),
3168 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003169 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003170 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003171#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003172 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003173#endif
john stultz4614a696b2009-12-14 18:00:05 -08003174 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003175#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003176 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003177#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003178 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3179 ONE("stat", S_IRUGO, proc_tid_stat),
3180 ONE("statm", S_IRUGO, proc_pid_statm),
3181 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003182#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003183 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003184#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003185 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3186 LNK("cwd", proc_cwd_link),
3187 LNK("root", proc_root_link),
3188 LNK("exe", proc_exe_link),
3189 REG("mounts", S_IRUGO, proc_mounts_operations),
3190 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003191#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003192 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3193 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003194 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003195#endif
3196#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003197 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003198#endif
3199#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003200 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003201#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003202#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003203 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003204#endif
3205#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003206 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003207#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003208#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003209 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003210#endif
Paul Menage8793d852007-10-18 23:39:39 -07003211#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003212 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003213#endif
Paul Menagea4243162007-10-18 23:39:35 -07003214#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003215 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003216#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003217 INF("oom_score", S_IRUGO, proc_oom_score),
3218 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003219 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003220#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003221 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003222 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003223#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003224#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003225 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003226#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003227#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003228 INF("io", S_IRUGO, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003229#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003230#ifdef CONFIG_HARDWALL
3231 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3232#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003233};
3234
3235static int proc_tid_base_readdir(struct file * filp,
3236 void * dirent, filldir_t filldir)
3237{
3238 return proc_pident_readdir(filp,dirent,filldir,
3239 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3240}
3241
3242static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003243 return proc_pident_lookup(dir, dentry,
3244 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003245}
3246
Arjan van de Ven00977a52007-02-12 00:55:34 -08003247static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003248 .read = generic_read_dir,
3249 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003250 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003251};
3252
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003253static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003254 .lookup = proc_tid_base_lookup,
3255 .getattr = pid_getattr,
3256 .setattr = proc_setattr,
3257};
3258
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003259static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003260 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003261{
3262 struct dentry *error = ERR_PTR(-ENOENT);
3263 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003264 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003265
3266 if (!inode)
3267 goto out;
3268 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3269 inode->i_op = &proc_tid_base_inode_operations;
3270 inode->i_fop = &proc_tid_base_operations;
3271 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003272
3273 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3274 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003275
Nick Pigginfb045ad2011-01-07 17:49:55 +11003276 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003277
3278 d_add(dentry, inode);
3279 /* Close the race of the process dying before we return the dentry */
3280 if (pid_revalidate(dentry, NULL))
3281 error = NULL;
3282out:
3283 return error;
3284}
3285
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003286static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3287{
3288 struct dentry *result = ERR_PTR(-ENOENT);
3289 struct task_struct *task;
3290 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003291 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003292 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003293
3294 if (!leader)
3295 goto out_no_task;
3296
3297 tid = name_to_int(dentry);
3298 if (tid == ~0U)
3299 goto out;
3300
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003301 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003302 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003303 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003304 if (task)
3305 get_task_struct(task);
3306 rcu_read_unlock();
3307 if (!task)
3308 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003309 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003310 goto out_drop_task;
3311
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003312 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003313out_drop_task:
3314 put_task_struct(task);
3315out:
3316 put_task_struct(leader);
3317out_no_task:
3318 return result;
3319}
3320
3321/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003322 * Find the first tid of a thread group to return to user space.
3323 *
3324 * Usually this is just the thread group leader, but if the users
3325 * buffer was too small or there was a seek into the middle of the
3326 * directory we have more work todo.
3327 *
3328 * In the case of a short read we start with find_task_by_pid.
3329 *
3330 * In the case of a seek we start with the leader and walk nr
3331 * threads past it.
3332 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003333static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003334 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003335{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003336 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003337
Eric W. Biedermancc288732006-06-26 00:26:01 -07003338 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003339 /* Attempt to start with the pid of a thread */
3340 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003341 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003342 if (pos && (pos->group_leader == leader))
3343 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003344 }
3345
3346 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003347 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003348 if (nr && nr >= get_nr_threads(leader))
3349 goto out;
3350
3351 /* If we haven't found our starting place yet start
3352 * with the leader and walk nr threads forward.
3353 */
3354 for (pos = leader; nr > 0; --nr) {
3355 pos = next_thread(pos);
3356 if (pos == leader) {
3357 pos = NULL;
3358 goto out;
3359 }
3360 }
3361found:
3362 get_task_struct(pos);
3363out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003364 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003365 return pos;
3366}
3367
3368/*
3369 * Find the next thread in the thread list.
3370 * Return NULL if there is an error or no next thread.
3371 *
3372 * The reference to the input task_struct is released.
3373 */
3374static struct task_struct *next_tid(struct task_struct *start)
3375{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003376 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003377 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003378 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003379 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003380 if (thread_group_leader(pos))
3381 pos = NULL;
3382 else
3383 get_task_struct(pos);
3384 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003385 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003386 put_task_struct(start);
3387 return pos;
3388}
3389
Eric W. Biederman61a28782006-10-02 02:18:49 -07003390static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3391 struct task_struct *task, int tid)
3392{
3393 char name[PROC_NUMBUF];
3394 int len = snprintf(name, sizeof(name), "%d", tid);
3395 return proc_fill_cache(filp, dirent, filldir, name, len,
3396 proc_task_instantiate, task, NULL);
3397}
3398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399/* for the /proc/TGID/task/ directories */
3400static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3401{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003402 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003404 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003405 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 int retval = -ENOENT;
3407 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003408 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003409 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003411 task = get_proc_task(inode);
3412 if (!task)
3413 goto out_no_task;
3414 rcu_read_lock();
3415 if (pid_alive(task)) {
3416 leader = task->group_leader;
3417 get_task_struct(leader);
3418 }
3419 rcu_read_unlock();
3420 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003421 if (!leader)
3422 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 retval = 0;
3424
Linus Torvaldsee568b22009-03-17 10:02:35 -07003425 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 case 0:
3427 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003428 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003430 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 /* fall through */
3432 case 1:
3433 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003434 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003436 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /* fall through */
3438 }
3439
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003440 /* f_version caches the tgid value that the last readdir call couldn't
3441 * return. lseek aka telldir automagically resets f_version to 0.
3442 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003443 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003444 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003445 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003446 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003447 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003448 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003449 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003450 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003451 /* returning this tgid failed, save it as the first
3452 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003453 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003454 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
3458out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003459 put_task_struct(leader);
3460out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return retval;
3462}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003463
3464static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3465{
3466 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003467 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003468 generic_fillattr(inode, stat);
3469
Eric W. Biederman99f89552006-06-26 00:25:55 -07003470 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003471 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003472 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003473 }
3474
3475 return 0;
3476}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003477
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003478static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003479 .lookup = proc_task_lookup,
3480 .getattr = proc_task_getattr,
3481 .setattr = proc_setattr,
3482};
3483
Arjan van de Ven00977a52007-02-12 00:55:34 -08003484static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003485 .read = generic_read_dir,
3486 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003487 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003488};