blob: 0da237920270edc297c8257e4c28e93cbf81255e [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
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800203static struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
Al Viro831830b2008-01-02 14:09:57 +0000204{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200205 struct mm_struct *mm;
Al Viroec6fd8a2011-02-15 22:22:54 -0500206 int err;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200207
Al Viroec6fd8a2011-02-15 22:22:54 -0500208 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
209 if (err)
210 return ERR_PTR(err);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200211
212 mm = get_task_mm(task);
213 if (mm && mm != current->mm &&
Rohit Vaswania76e99a2012-03-30 00:09:34 -0700214 !ptrace_may_access(task, mode) &&
San Mehata97e6c52009-09-16 12:39:10 -0700215 !capable(CAP_SYS_RESOURCE)) {
Oleg Nesterov704b8362009-07-10 03:27:40 +0200216 mmput(mm);
Al Viroec6fd8a2011-02-15 22:22:54 -0500217 mm = ERR_PTR(-EACCES);
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200218 }
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700219 mutex_unlock(&task->signal->cred_guard_mutex);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200220
Al Viro831830b2008-01-02 14:09:57 +0000221 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000222}
223
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800224struct mm_struct *mm_for_maps(struct task_struct *task)
225{
226 return mm_access(task, PTRACE_MODE_READ);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int proc_pid_cmdline(struct task_struct *task, char * buffer)
230{
231 int res = 0;
232 unsigned int len;
233 struct mm_struct *mm = get_task_mm(task);
234 if (!mm)
235 goto out;
236 if (!mm->arg_end)
237 goto out_mm; /* Shh! No looking before we're done */
238
239 len = mm->arg_end - mm->arg_start;
240
241 if (len > PAGE_SIZE)
242 len = PAGE_SIZE;
243
244 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
245
246 // If the nul at the end of args has been overwritten, then
247 // assume application is using setproctitle(3).
248 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
249 len = strnlen(buffer, res);
250 if (len < res) {
251 res = len;
252 } else {
253 len = mm->env_end - mm->env_start;
254 if (len > PAGE_SIZE - res)
255 len = PAGE_SIZE - res;
256 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
257 res = strnlen(buffer, res);
258 }
259 }
260out_mm:
261 mmput(mm);
262out:
263 return res;
264}
265
266static int proc_pid_auxv(struct task_struct *task, char *buffer)
267{
Al Viro2fadaef2011-02-15 22:52:11 -0500268 struct mm_struct *mm = mm_for_maps(task);
269 int res = PTR_ERR(mm);
270 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300272 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300274 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 res = nwords * sizeof(mm->saved_auxv[0]);
276 if (res > PAGE_SIZE)
277 res = PAGE_SIZE;
278 memcpy(buffer, mm->saved_auxv, res);
279 mmput(mm);
280 }
281 return res;
282}
283
284
285#ifdef CONFIG_KALLSYMS
286/*
287 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
288 * Returns the resolved symbol. If that fails, simply return the address.
289 */
290static int proc_pid_wchan(struct task_struct *task, char *buffer)
291{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700292 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700293 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 wchan = get_wchan(task);
296
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700297 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600298 if (!ptrace_may_access(task, PTRACE_MODE_READ))
299 return 0;
300 else
301 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700302 else
303 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305#endif /* CONFIG_KALLSYMS */
306
Al Viroa9712bc2011-03-23 15:52:50 -0400307static int lock_trace(struct task_struct *task)
308{
309 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
310 if (err)
311 return err;
312 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
313 mutex_unlock(&task->signal->cred_guard_mutex);
314 return -EPERM;
315 }
316 return 0;
317}
318
319static void unlock_trace(struct task_struct *task)
320{
321 mutex_unlock(&task->signal->cred_guard_mutex);
322}
323
Ken Chen2ec220e2008-11-10 11:26:08 +0300324#ifdef CONFIG_STACKTRACE
325
326#define MAX_STACK_TRACE_DEPTH 64
327
328static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
329 struct pid *pid, struct task_struct *task)
330{
331 struct stack_trace trace;
332 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400333 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300334 int i;
335
336 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
337 if (!entries)
338 return -ENOMEM;
339
340 trace.nr_entries = 0;
341 trace.max_entries = MAX_STACK_TRACE_DEPTH;
342 trace.entries = entries;
343 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300344
Al Viroa9712bc2011-03-23 15:52:50 -0400345 err = lock_trace(task);
346 if (!err) {
347 save_stack_trace_tsk(task, &trace);
348
349 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700350 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400351 (void *)entries[i], (void *)entries[i]);
352 }
353 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300354 }
355 kfree(entries);
356
Al Viroa9712bc2011-03-23 15:52:50 -0400357 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300358}
359#endif
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#ifdef CONFIG_SCHEDSTATS
362/*
363 * Provides /proc/PID/schedstat
364 */
365static int proc_pid_schedstat(struct task_struct *task, char *buffer)
366{
Balbir Singh172ba842007-07-09 18:52:00 +0200367 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100368 (unsigned long long)task->se.sum_exec_runtime,
369 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200370 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372#endif
373
Arjan van de Ven97455122008-01-25 21:08:34 +0100374#ifdef CONFIG_LATENCYTOP
375static int lstats_show_proc(struct seq_file *m, void *v)
376{
377 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800378 struct inode *inode = m->private;
379 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100380
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800381 if (!task)
382 return -ESRCH;
383 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100384 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800385 struct latency_record *lr = &task->latency_record[i];
386 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100387 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800388 seq_printf(m, "%i %li %li",
389 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100390 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800391 unsigned long bt = lr->backtrace[q];
392 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100393 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800394 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100395 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800396 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100397 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800398 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100399 }
400
401 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800402 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100403 return 0;
404}
405
406static int lstats_open(struct inode *inode, struct file *file)
407{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800408 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800409}
410
Arjan van de Ven97455122008-01-25 21:08:34 +0100411static ssize_t lstats_write(struct file *file, const char __user *buf,
412 size_t count, loff_t *offs)
413{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800414 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100415
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800416 if (!task)
417 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100418 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800419 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100420
421 return count;
422}
423
424static const struct file_operations proc_lstats_operations = {
425 .open = lstats_open,
426 .read = seq_read,
427 .write = lstats_write,
428 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800429 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100430};
431
432#endif
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434static int proc_oom_score(struct task_struct *task, char *buffer)
435{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200436 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700438 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200439 if (pid_alive(task))
David Rientjesa63d83f2010-08-09 17:19:46 -0700440 points = oom_badness(task, NULL, NULL,
441 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700442 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return sprintf(buffer, "%lu\n", points);
444}
445
Neil Hormand85f50d2007-10-18 23:40:37 -0700446struct limit_names {
447 char *name;
448 char *unit;
449};
450
451static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700452 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700453 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
454 [RLIMIT_DATA] = {"Max data size", "bytes"},
455 [RLIMIT_STACK] = {"Max stack size", "bytes"},
456 [RLIMIT_CORE] = {"Max core file size", "bytes"},
457 [RLIMIT_RSS] = {"Max resident set", "bytes"},
458 [RLIMIT_NPROC] = {"Max processes", "processes"},
459 [RLIMIT_NOFILE] = {"Max open files", "files"},
460 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
461 [RLIMIT_AS] = {"Max address space", "bytes"},
462 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
463 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
464 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
465 [RLIMIT_NICE] = {"Max nice priority", NULL},
466 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800467 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700468};
469
470/* Display limits for a process */
471static int proc_pid_limits(struct task_struct *task, char *buffer)
472{
473 unsigned int i;
474 int count = 0;
475 unsigned long flags;
476 char *bufptr = buffer;
477
478 struct rlimit rlim[RLIM_NLIMITS];
479
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400480 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700481 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700482 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
483 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700484
485 /*
486 * print the file header
487 */
488 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
489 "Limit", "Soft Limit", "Hard Limit", "Units");
490
491 for (i = 0; i < RLIM_NLIMITS; i++) {
492 if (rlim[i].rlim_cur == RLIM_INFINITY)
493 count += sprintf(&bufptr[count], "%-25s %-20s ",
494 lnames[i].name, "unlimited");
495 else
496 count += sprintf(&bufptr[count], "%-25s %-20lu ",
497 lnames[i].name, rlim[i].rlim_cur);
498
499 if (rlim[i].rlim_max == RLIM_INFINITY)
500 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
501 else
502 count += sprintf(&bufptr[count], "%-20lu ",
503 rlim[i].rlim_max);
504
505 if (lnames[i].unit)
506 count += sprintf(&bufptr[count], "%-10s\n",
507 lnames[i].unit);
508 else
509 count += sprintf(&bufptr[count], "\n");
510 }
511
512 return count;
513}
514
Roland McGrathebcb6732008-07-25 19:46:00 -0700515#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
516static int proc_pid_syscall(struct task_struct *task, char *buffer)
517{
518 long nr;
519 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400520 int res = lock_trace(task);
521 if (res)
522 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700523
524 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400525 res = sprintf(buffer, "running\n");
526 else if (nr < 0)
527 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
528 else
529 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700530 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
531 nr,
532 args[0], args[1], args[2], args[3], args[4], args[5],
533 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400534 unlock_trace(task);
535 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700536}
537#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/************************************************************************/
540/* Here the fs part begins */
541/************************************************************************/
542
543/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700544static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700546 struct task_struct *task;
547 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700548 /* Allow access to a task's file descriptors if it is us or we
549 * may use ptrace attach to the process and find out that
550 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700551 */
552 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700553 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400554 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700555 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700556 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700557 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800560int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700561{
562 int error;
563 struct inode *inode = dentry->d_inode;
564
565 if (attr->ia_valid & ATTR_MODE)
566 return -EPERM;
567
568 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200569 if (error)
570 return error;
571
572 if ((attr->ia_valid & ATTR_SIZE) &&
573 attr->ia_size != i_size_read(inode)) {
574 error = vmtruncate(inode, attr->ia_size);
575 if (error)
576 return error;
577 }
578
579 setattr_copy(inode, attr);
580 mark_inode_dirty(inode);
581 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700582}
583
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800584static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700585 .setattr = proc_setattr,
586};
587
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100588static int mounts_open_common(struct inode *inode, struct file *file,
589 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700591 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700592 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800593 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100594 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500595 struct proc_mounts *p;
596 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Eric W. Biederman99f89552006-06-26 00:25:55 -0700598 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700599 rcu_read_lock();
600 nsp = task_nsproxy(task);
601 if (nsp) {
602 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800603 if (ns)
604 get_mnt_ns(ns);
605 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700606 rcu_read_unlock();
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200607 if (ns && get_task_root(task, &root) == 0)
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000608 ret = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700609 put_task_struct(task);
610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100612 if (!ns)
613 goto err;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000614 if (ret)
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100615 goto err_put_ns;
616
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100617 ret = -ENOMEM;
618 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
619 if (!p)
620 goto err_put_path;
621
622 file->private_data = &p->m;
623 ret = seq_open(file, op);
624 if (ret)
625 goto err_free;
626
627 p->m.private = p;
628 p->ns = ns;
629 p->root = root;
630 p->event = ns->event;
631
632 return 0;
633
634 err_free:
635 kfree(p);
636 err_put_path:
637 path_put(&root);
638 err_put_ns:
639 put_mnt_ns(ns);
640 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return ret;
642}
643
644static int mounts_release(struct inode *inode, struct file *file)
645{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100646 struct proc_mounts *p = file->private_data;
647 path_put(&p->root);
648 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return seq_release(inode, file);
650}
651
Al Viro5addc5d2005-11-07 17:15:49 -0500652static unsigned mounts_poll(struct file *file, poll_table *wait)
653{
654 struct proc_mounts *p = file->private_data;
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900655 unsigned res = POLLIN | POLLRDNORM;
Al Viro5addc5d2005-11-07 17:15:49 -0500656
Al Viro9f5596a2010-02-05 00:40:25 -0500657 poll_wait(file, &p->ns->poll, wait);
658 if (mnt_had_events(p))
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900659 res |= POLLERR | POLLPRI;
Al Viro5addc5d2005-11-07 17:15:49 -0500660
661 return res;
662}
663
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100664static int mounts_open(struct inode *inode, struct file *file)
665{
666 return mounts_open_common(inode, file, &mounts_op);
667}
668
Arjan van de Ven00977a52007-02-12 00:55:34 -0800669static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 .open = mounts_open,
671 .read = seq_read,
672 .llseek = seq_lseek,
673 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500674 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675};
676
Ram Pai2d4d4862008-03-27 13:06:25 +0100677static int mountinfo_open(struct inode *inode, struct file *file)
678{
679 return mounts_open_common(inode, file, &mountinfo_op);
680}
681
682static const struct file_operations proc_mountinfo_operations = {
683 .open = mountinfo_open,
684 .read = seq_read,
685 .llseek = seq_lseek,
686 .release = mounts_release,
687 .poll = mounts_poll,
688};
689
Chuck Leverb4629fe2006-03-20 13:44:12 -0500690static int mountstats_open(struct inode *inode, struct file *file)
691{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100692 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500693}
694
Arjan van de Ven00977a52007-02-12 00:55:34 -0800695static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500696 .open = mountstats_open,
697 .read = seq_read,
698 .llseek = seq_lseek,
699 .release = mounts_release,
700};
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
703
704static ssize_t proc_info_read(struct file * file, char __user * buf,
705 size_t count, loff_t *ppos)
706{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800707 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unsigned long page;
709 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700710 struct task_struct *task = get_proc_task(inode);
711
712 length = -ESRCH;
713 if (!task)
714 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (count > PROC_BLOCK_SIZE)
717 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700718
719 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700720 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700721 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 length = PROC_I(inode)->op.proc_read(task, (char*)page);
724
725 if (length >= 0)
726 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
727 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700728out:
729 put_task_struct(task);
730out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return length;
732}
733
Arjan van de Ven00977a52007-02-12 00:55:34 -0800734static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100736 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737};
738
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800739static int proc_single_show(struct seq_file *m, void *v)
740{
741 struct inode *inode = m->private;
742 struct pid_namespace *ns;
743 struct pid *pid;
744 struct task_struct *task;
745 int ret;
746
747 ns = inode->i_sb->s_fs_info;
748 pid = proc_pid(inode);
749 task = get_pid_task(pid, PIDTYPE_PID);
750 if (!task)
751 return -ESRCH;
752
753 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
754
755 put_task_struct(task);
756 return ret;
757}
758
759static int proc_single_open(struct inode *inode, struct file *filp)
760{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800761 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800762}
763
764static const struct file_operations proc_single_file_operations = {
765 .open = proc_single_open,
766 .read = seq_read,
767 .llseek = seq_lseek,
768 .release = single_release,
769};
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static int mem_open(struct inode* inode, struct file* file)
772{
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800773 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
774 struct mm_struct *mm;
775
776 if (!task)
777 return -ESRCH;
778
779 mm = mm_access(task, PTRACE_MODE_ATTACH);
780 put_task_struct(task);
781
782 if (IS_ERR(mm))
783 return PTR_ERR(mm);
784
Oleg Nesterov00537792012-01-31 17:15:11 +0100785 if (mm) {
786 /* ensure this mm_struct can't be freed */
787 atomic_inc(&mm->mm_count);
788 /* but do not pin its memory */
789 mmput(mm);
790 }
791
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700792 /* OK to pass negative loff_t, we can catch out-of-range */
793 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800794 file->private_data = mm;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
797}
798
Oleg Nesterovb49767a2012-01-31 17:14:54 +0100799static ssize_t mem_rw(struct file *file, char __user *buf,
800 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800802 struct mm_struct *mm = file->private_data;
Oleg Nesterovb49767a2012-01-31 17:14:54 +0100803 unsigned long addr = *ppos;
804 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800807 if (!mm)
808 return 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700809
Mel Gormane12ba742007-10-16 01:25:52 -0700810 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!page)
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800812 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700814 copied = 0;
Oleg Nesterov00537792012-01-31 17:15:11 +0100815 if (!atomic_inc_not_zero(&mm->mm_users))
816 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 while (count > 0) {
Oleg Nesterovb49767a2012-01-31 17:14:54 +0100819 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Oleg Nesterovb49767a2012-01-31 17:14:54 +0100821 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 copied = -EFAULT;
823 break;
824 }
Oleg Nesterovb49767a2012-01-31 17:14:54 +0100825
826 this_len = access_remote_vm(mm, addr, page, this_len, write);
827 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!copied)
829 copied = -EIO;
830 break;
831 }
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700832
Oleg Nesterovb49767a2012-01-31 17:14:54 +0100833 if (!write && copy_to_user(buf, page, this_len)) {
834 copied = -EFAULT;
835 break;
836 }
837
838 buf += this_len;
839 addr += this_len;
840 copied += this_len;
841 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Oleg Nesterovb49767a2012-01-31 17:14:54 +0100843 *ppos = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400845 mmput(mm);
Oleg Nesterov00537792012-01-31 17:15:11 +0100846free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700847 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return copied;
849}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Oleg Nesterovb49767a2012-01-31 17:14:54 +0100851static ssize_t mem_read(struct file *file, char __user *buf,
852 size_t count, loff_t *ppos)
853{
854 return mem_rw(file, buf, count, ppos, 0);
855}
856
857static ssize_t mem_write(struct file *file, const char __user *buf,
858 size_t count, loff_t *ppos)
859{
860 return mem_rw(file, (char __user*)buf, count, ppos, 1);
861}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Matt Mackall85863e42008-02-04 22:29:04 -0800863loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 switch (orig) {
866 case 0:
867 file->f_pos = offset;
868 break;
869 case 1:
870 file->f_pos += offset;
871 break;
872 default:
873 return -EINVAL;
874 }
875 force_successful_syscall_return();
876 return file->f_pos;
877}
878
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800879static int mem_release(struct inode *inode, struct file *file)
880{
881 struct mm_struct *mm = file->private_data;
Oleg Nesterov401f6372012-01-31 17:14:38 +0100882 if (mm)
Oleg Nesterov00537792012-01-31 17:15:11 +0100883 mmdrop(mm);
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800884 return 0;
885}
886
Arjan van de Ven00977a52007-02-12 00:55:34 -0800887static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 .llseek = mem_lseek,
889 .read = mem_read,
890 .write = mem_write,
891 .open = mem_open,
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800892 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893};
894
James Pearson315e28c2007-10-16 23:30:17 -0700895static ssize_t environ_read(struct file *file, char __user *buf,
896 size_t count, loff_t *ppos)
897{
898 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
899 char *page;
900 unsigned long src = *ppos;
901 int ret = -ESRCH;
902 struct mm_struct *mm;
903
904 if (!task)
905 goto out_no_task;
906
James Pearson315e28c2007-10-16 23:30:17 -0700907 ret = -ENOMEM;
908 page = (char *)__get_free_page(GFP_TEMPORARY);
909 if (!page)
910 goto out;
911
James Pearson315e28c2007-10-16 23:30:17 -0700912
Al Virod6f64b82011-02-15 22:26:01 -0500913 mm = mm_for_maps(task);
914 ret = PTR_ERR(mm);
915 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700916 goto out_free;
917
Al Virod6f64b82011-02-15 22:26:01 -0500918 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -0700919 while (count > 0) {
920 int this_len, retval, max_len;
921
922 this_len = mm->env_end - (mm->env_start + src);
923
924 if (this_len <= 0)
925 break;
926
927 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
928 this_len = (this_len > max_len) ? max_len : this_len;
929
930 retval = access_process_vm(task, (mm->env_start + src),
931 page, this_len, 0);
932
933 if (retval <= 0) {
934 ret = retval;
935 break;
936 }
937
938 if (copy_to_user(buf, page, retval)) {
939 ret = -EFAULT;
940 break;
941 }
942
943 ret += retval;
944 src += retval;
945 buf += retval;
946 count -= retval;
947 }
948 *ppos = src;
949
950 mmput(mm);
951out_free:
952 free_page((unsigned long) page);
953out:
954 put_task_struct(task);
955out_no_task:
956 return ret;
957}
958
959static const struct file_operations proc_environ_operations = {
960 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100961 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700962};
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964static ssize_t oom_adjust_read(struct file *file, char __user *buf,
965 size_t count, loff_t *ppos)
966{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800967 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700968 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700970 int oom_adjust = OOM_DISABLE;
971 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Eric W. Biederman99f89552006-06-26 00:25:55 -0700973 if (!task)
974 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700975
976 if (lock_task_sighand(task, &flags)) {
977 oom_adjust = task->signal->oom_adj;
978 unlock_task_sighand(task, &flags);
979 }
980
Eric W. Biederman99f89552006-06-26 00:25:55 -0700981 put_task_struct(task);
982
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700983 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700984
985 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
989 size_t count, loff_t *ppos)
990{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700991 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700992 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700993 int oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700994 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700995 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700997 memset(buffer, 0, sizeof(buffer));
998 if (count > sizeof(buffer) - 1)
999 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001000 if (copy_from_user(buffer, buf, count)) {
1001 err = -EFAULT;
1002 goto out;
1003 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001004
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001005 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001006 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001007 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001008 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -07001009 oom_adjust != OOM_DISABLE) {
1010 err = -EINVAL;
1011 goto out;
1012 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001013
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001014 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001015 if (!task) {
1016 err = -ESRCH;
1017 goto out;
1018 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001019
Ying Han3d5992d2010-10-26 14:21:23 -07001020 task_lock(task);
1021 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001022 err = -EINVAL;
1023 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001024 }
1025
David Rientjesd19d5472010-10-26 14:21:26 -07001026 if (!lock_task_sighand(task, &flags)) {
1027 err = -ESRCH;
1028 goto err_task_lock;
1029 }
1030
1031 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1032 err = -EACCES;
1033 goto err_sighand;
1034 }
1035
Ying Han3d5992d2010-10-26 14:21:23 -07001036 if (oom_adjust != task->signal->oom_adj) {
1037 if (oom_adjust == OOM_DISABLE)
1038 atomic_inc(&task->mm->oom_disable_count);
1039 if (task->signal->oom_adj == OOM_DISABLE)
1040 atomic_dec(&task->mm->oom_disable_count);
1041 }
1042
David Rientjes51b1bd22010-08-09 17:19:47 -07001043 /*
1044 * Warn that /proc/pid/oom_adj is deprecated, see
1045 * Documentation/feature-removal-schedule.txt.
1046 */
1047 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1048 "please use /proc/%d/oom_score_adj instead.\n",
1049 current->comm, task_pid_nr(current),
1050 task_pid_nr(task), task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001051 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -07001052 /*
1053 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1054 * value is always attainable.
1055 */
1056 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1057 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1058 else
1059 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1060 -OOM_DISABLE;
David Rientjes723548b2010-10-26 14:21:25 -07001061err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001062 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001063err_task_lock:
1064 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001065 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001066out:
1067 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Mike Chan766e9932008-04-24 10:22:26 -07001070static int oom_adjust_permission(struct inode *inode, int mask,
1071 unsigned int flags)
1072{
1073 uid_t uid;
1074 struct task_struct *p;
1075
1076 if (flags & IPERM_FLAG_RCU)
1077 return -ECHILD;
1078
1079 p = get_proc_task(inode);
1080 if(p) {
1081 uid = task_uid(p);
1082 put_task_struct(p);
1083 }
1084
1085 /*
1086 * System Server (uid == 1000) is granted access to oom_adj of all
1087 * android applications (uid > 10000) as and services (uid >= 1000)
1088 */
1089 if (p && (current_fsuid() == 1000) && (uid >= 1000)) {
1090 if (inode->i_mode >> 6 & mask) {
1091 return 0;
1092 }
1093 }
1094
1095 /* Fall back to default. */
1096 return generic_permission(inode, mask, flags, NULL);
1097}
1098
1099static const struct inode_operations proc_oom_adjust_inode_operations = {
1100 .permission = oom_adjust_permission,
1101};
1102
Arjan van de Ven00977a52007-02-12 00:55:34 -08001103static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 .read = oom_adjust_read,
1105 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001106 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107};
1108
David Rientjesa63d83f2010-08-09 17:19:46 -07001109static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1110 size_t count, loff_t *ppos)
1111{
1112 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1113 char buffer[PROC_NUMBUF];
1114 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1115 unsigned long flags;
1116 size_t len;
1117
1118 if (!task)
1119 return -ESRCH;
1120 if (lock_task_sighand(task, &flags)) {
1121 oom_score_adj = task->signal->oom_score_adj;
1122 unlock_task_sighand(task, &flags);
1123 }
1124 put_task_struct(task);
1125 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1126 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1127}
1128
1129static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1130 size_t count, loff_t *ppos)
1131{
1132 struct task_struct *task;
1133 char buffer[PROC_NUMBUF];
1134 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001135 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001136 int err;
1137
1138 memset(buffer, 0, sizeof(buffer));
1139 if (count > sizeof(buffer) - 1)
1140 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001141 if (copy_from_user(buffer, buf, count)) {
1142 err = -EFAULT;
1143 goto out;
1144 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001145
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001146 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001147 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001148 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001149 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001150 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1151 err = -EINVAL;
1152 goto out;
1153 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001154
1155 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001156 if (!task) {
1157 err = -ESRCH;
1158 goto out;
1159 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001160
Ying Han3d5992d2010-10-26 14:21:23 -07001161 task_lock(task);
1162 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001163 err = -EINVAL;
1164 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001165 }
David Rientjesd19d5472010-10-26 14:21:26 -07001166
1167 if (!lock_task_sighand(task, &flags)) {
1168 err = -ESRCH;
1169 goto err_task_lock;
1170 }
1171
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001172 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001173 !capable(CAP_SYS_RESOURCE)) {
1174 err = -EACCES;
1175 goto err_sighand;
1176 }
1177
Ying Han3d5992d2010-10-26 14:21:23 -07001178 if (oom_score_adj != task->signal->oom_score_adj) {
1179 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1180 atomic_inc(&task->mm->oom_disable_count);
1181 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1182 atomic_dec(&task->mm->oom_disable_count);
1183 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001184 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001185 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1186 task->signal->oom_score_adj_min = oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001187 /*
1188 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1189 * always attainable.
1190 */
1191 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1192 task->signal->oom_adj = OOM_DISABLE;
1193 else
1194 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1195 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001196err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001197 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001198err_task_lock:
1199 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001200 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001201out:
1202 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001203}
1204
1205static const struct file_operations proc_oom_score_adj_operations = {
1206 .read = oom_score_adj_read,
1207 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001208 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001209};
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211#ifdef CONFIG_AUDITSYSCALL
1212#define TMPBUFLEN 21
1213static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1214 size_t count, loff_t *ppos)
1215{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001216 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001217 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 ssize_t length;
1219 char tmpbuf[TMPBUFLEN];
1220
Eric W. Biederman99f89552006-06-26 00:25:55 -07001221 if (!task)
1222 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001224 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001225 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1227}
1228
1229static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1230 size_t count, loff_t *ppos)
1231{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001232 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 char *page, *tmp;
1234 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 uid_t loginuid;
1236
1237 if (!capable(CAP_AUDIT_CONTROL))
1238 return -EPERM;
1239
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001240 rcu_read_lock();
1241 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1242 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001244 }
1245 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Al Viroe0182902006-05-18 08:28:02 -04001247 if (count >= PAGE_SIZE)
1248 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 if (*ppos != 0) {
1251 /* No partial writes. */
1252 return -EINVAL;
1253 }
Mel Gormane12ba742007-10-16 01:25:52 -07001254 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (!page)
1256 return -ENOMEM;
1257 length = -EFAULT;
1258 if (copy_from_user(page, buf, count))
1259 goto out_free_page;
1260
Al Viroe0182902006-05-18 08:28:02 -04001261 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 loginuid = simple_strtoul(page, &tmp, 10);
1263 if (tmp == page) {
1264 length = -EINVAL;
1265 goto out_free_page;
1266
1267 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001268 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (likely(length == 0))
1270 length = count;
1271
1272out_free_page:
1273 free_page((unsigned long) page);
1274 return length;
1275}
1276
Arjan van de Ven00977a52007-02-12 00:55:34 -08001277static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 .read = proc_loginuid_read,
1279 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001280 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281};
Eric Paris1e0bd752008-03-13 08:15:31 -04001282
1283static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1284 size_t count, loff_t *ppos)
1285{
1286 struct inode * inode = file->f_path.dentry->d_inode;
1287 struct task_struct *task = get_proc_task(inode);
1288 ssize_t length;
1289 char tmpbuf[TMPBUFLEN];
1290
1291 if (!task)
1292 return -ESRCH;
1293 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1294 audit_get_sessionid(task));
1295 put_task_struct(task);
1296 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1297}
1298
1299static const struct file_operations proc_sessionid_operations = {
1300 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001301 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001302};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303#endif
1304
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001305#ifdef CONFIG_FAULT_INJECTION
1306static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1307 size_t count, loff_t *ppos)
1308{
1309 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1310 char buffer[PROC_NUMBUF];
1311 size_t len;
1312 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001313
1314 if (!task)
1315 return -ESRCH;
1316 make_it_fail = task->make_it_fail;
1317 put_task_struct(task);
1318
1319 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001320
1321 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001322}
1323
1324static ssize_t proc_fault_inject_write(struct file * file,
1325 const char __user * buf, size_t count, loff_t *ppos)
1326{
1327 struct task_struct *task;
1328 char buffer[PROC_NUMBUF], *end;
1329 int make_it_fail;
1330
1331 if (!capable(CAP_SYS_RESOURCE))
1332 return -EPERM;
1333 memset(buffer, 0, sizeof(buffer));
1334 if (count > sizeof(buffer) - 1)
1335 count = sizeof(buffer) - 1;
1336 if (copy_from_user(buffer, buf, count))
1337 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001338 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1339 if (*end)
1340 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001341 task = get_proc_task(file->f_dentry->d_inode);
1342 if (!task)
1343 return -ESRCH;
1344 task->make_it_fail = make_it_fail;
1345 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001346
1347 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001348}
1349
Arjan van de Ven00977a52007-02-12 00:55:34 -08001350static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001351 .read = proc_fault_inject_read,
1352 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001353 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001354};
1355#endif
1356
Arjan van de Ven97455122008-01-25 21:08:34 +01001357
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001358#ifdef CONFIG_SCHED_DEBUG
1359/*
1360 * Print out various scheduling related per-task fields:
1361 */
1362static int sched_show(struct seq_file *m, void *v)
1363{
1364 struct inode *inode = m->private;
1365 struct task_struct *p;
1366
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001367 p = get_proc_task(inode);
1368 if (!p)
1369 return -ESRCH;
1370 proc_sched_show_task(p, m);
1371
1372 put_task_struct(p);
1373
1374 return 0;
1375}
1376
1377static ssize_t
1378sched_write(struct file *file, const char __user *buf,
1379 size_t count, loff_t *offset)
1380{
1381 struct inode *inode = file->f_path.dentry->d_inode;
1382 struct task_struct *p;
1383
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001384 p = get_proc_task(inode);
1385 if (!p)
1386 return -ESRCH;
1387 proc_sched_set_task(p);
1388
1389 put_task_struct(p);
1390
1391 return count;
1392}
1393
1394static int sched_open(struct inode *inode, struct file *filp)
1395{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001396 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001397}
1398
1399static const struct file_operations proc_pid_sched_operations = {
1400 .open = sched_open,
1401 .read = seq_read,
1402 .write = sched_write,
1403 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001404 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001405};
1406
1407#endif
1408
Mike Galbraith5091faa2010-11-30 14:18:03 +01001409#ifdef CONFIG_SCHED_AUTOGROUP
1410/*
1411 * Print out autogroup related information:
1412 */
1413static int sched_autogroup_show(struct seq_file *m, void *v)
1414{
1415 struct inode *inode = m->private;
1416 struct task_struct *p;
1417
1418 p = get_proc_task(inode);
1419 if (!p)
1420 return -ESRCH;
1421 proc_sched_autogroup_show_task(p, m);
1422
1423 put_task_struct(p);
1424
1425 return 0;
1426}
1427
1428static ssize_t
1429sched_autogroup_write(struct file *file, const char __user *buf,
1430 size_t count, loff_t *offset)
1431{
1432 struct inode *inode = file->f_path.dentry->d_inode;
1433 struct task_struct *p;
1434 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001435 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001436 int err;
1437
1438 memset(buffer, 0, sizeof(buffer));
1439 if (count > sizeof(buffer) - 1)
1440 count = sizeof(buffer) - 1;
1441 if (copy_from_user(buffer, buf, count))
1442 return -EFAULT;
1443
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001444 err = kstrtoint(strstrip(buffer), 0, &nice);
1445 if (err < 0)
1446 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001447
1448 p = get_proc_task(inode);
1449 if (!p)
1450 return -ESRCH;
1451
1452 err = nice;
1453 err = proc_sched_autogroup_set_nice(p, &err);
1454 if (err)
1455 count = err;
1456
1457 put_task_struct(p);
1458
1459 return count;
1460}
1461
1462static int sched_autogroup_open(struct inode *inode, struct file *filp)
1463{
1464 int ret;
1465
1466 ret = single_open(filp, sched_autogroup_show, NULL);
1467 if (!ret) {
1468 struct seq_file *m = filp->private_data;
1469
1470 m->private = inode;
1471 }
1472 return ret;
1473}
1474
1475static const struct file_operations proc_pid_sched_autogroup_operations = {
1476 .open = sched_autogroup_open,
1477 .read = seq_read,
1478 .write = sched_autogroup_write,
1479 .llseek = seq_lseek,
1480 .release = single_release,
1481};
1482
1483#endif /* CONFIG_SCHED_AUTOGROUP */
1484
john stultz4614a696b2009-12-14 18:00:05 -08001485static ssize_t comm_write(struct file *file, const char __user *buf,
1486 size_t count, loff_t *offset)
1487{
1488 struct inode *inode = file->f_path.dentry->d_inode;
1489 struct task_struct *p;
1490 char buffer[TASK_COMM_LEN];
1491
1492 memset(buffer, 0, sizeof(buffer));
1493 if (count > sizeof(buffer) - 1)
1494 count = sizeof(buffer) - 1;
1495 if (copy_from_user(buffer, buf, count))
1496 return -EFAULT;
1497
1498 p = get_proc_task(inode);
1499 if (!p)
1500 return -ESRCH;
1501
1502 if (same_thread_group(current, p))
1503 set_task_comm(p, buffer);
1504 else
1505 count = -EINVAL;
1506
1507 put_task_struct(p);
1508
1509 return count;
1510}
1511
1512static int comm_show(struct seq_file *m, void *v)
1513{
1514 struct inode *inode = m->private;
1515 struct task_struct *p;
1516
1517 p = get_proc_task(inode);
1518 if (!p)
1519 return -ESRCH;
1520
1521 task_lock(p);
1522 seq_printf(m, "%s\n", p->comm);
1523 task_unlock(p);
1524
1525 put_task_struct(p);
1526
1527 return 0;
1528}
1529
1530static int comm_open(struct inode *inode, struct file *filp)
1531{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001532 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001533}
1534
1535static const struct file_operations proc_pid_set_comm_operations = {
1536 .open = comm_open,
1537 .read = seq_read,
1538 .write = comm_write,
1539 .llseek = seq_lseek,
1540 .release = single_release,
1541};
1542
Matt Helsley925d1c42008-04-29 01:01:36 -07001543static int proc_exe_link(struct inode *inode, struct path *exe_path)
1544{
1545 struct task_struct *task;
1546 struct mm_struct *mm;
1547 struct file *exe_file;
1548
1549 task = get_proc_task(inode);
1550 if (!task)
1551 return -ENOENT;
1552 mm = get_task_mm(task);
1553 put_task_struct(task);
1554 if (!mm)
1555 return -ENOENT;
1556 exe_file = get_mm_exe_file(mm);
1557 mmput(mm);
1558 if (exe_file) {
1559 *exe_path = exe_file->f_path;
1560 path_get(&exe_file->f_path);
1561 fput(exe_file);
1562 return 0;
1563 } else
1564 return -ENOENT;
1565}
1566
Al Viro008b1502005-08-20 00:17:39 +01001567static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 struct inode *inode = dentry->d_inode;
1570 int error = -EACCES;
1571
1572 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001573 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Eric W. Biederman778c1142006-06-26 00:25:58 -07001575 /* Are we allowed to snoop on the tasks file descriptors? */
1576 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001579 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580out:
Al Viro008b1502005-08-20 00:17:39 +01001581 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001584static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Mel Gormane12ba742007-10-16 01:25:52 -07001586 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001587 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int len;
1589
1590 if (!tmp)
1591 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001592
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001593 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001594 len = PTR_ERR(pathname);
1595 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001597 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (len > buflen)
1600 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001601 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 len = -EFAULT;
1603 out:
1604 free_page((unsigned long)tmp);
1605 return len;
1606}
1607
1608static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1609{
1610 int error = -EACCES;
1611 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001612 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Eric W. Biederman778c1142006-06-26 00:25:58 -07001614 /* Are we allowed to snoop on the tasks file descriptors? */
1615 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001618 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (error)
1620 goto out;
1621
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001622 error = do_proc_readlink(&path, buffer, buflen);
1623 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return error;
1626}
1627
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001628static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001630 .follow_link = proc_pid_follow_link,
1631 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632};
1633
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001634
1635/* building an inode */
1636
1637static int task_dumpable(struct task_struct *task)
1638{
1639 int dumpable = 0;
1640 struct mm_struct *mm;
1641
1642 task_lock(task);
1643 mm = task->mm;
1644 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001645 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001646 task_unlock(task);
1647 if(dumpable == 1)
1648 return 1;
1649 return 0;
1650}
1651
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001652struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001653{
1654 struct inode * inode;
1655 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001656 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001657
1658 /* We need a new inode */
1659
1660 inode = new_inode(sb);
1661 if (!inode)
1662 goto out;
1663
1664 /* Common stuff */
1665 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001666 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001667 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001668 inode->i_op = &proc_def_inode_operations;
1669
1670 /*
1671 * grab the reference to task.
1672 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001673 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001674 if (!ei->pid)
1675 goto out_unlock;
1676
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001677 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001678 rcu_read_lock();
1679 cred = __task_cred(task);
1680 inode->i_uid = cred->euid;
1681 inode->i_gid = cred->egid;
1682 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001683 }
1684 security_task_to_inode(task, inode);
1685
1686out:
1687 return inode;
1688
1689out_unlock:
1690 iput(inode);
1691 return NULL;
1692}
1693
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001694int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001695{
1696 struct inode *inode = dentry->d_inode;
1697 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001698 const struct cred *cred;
1699
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001700 generic_fillattr(inode, stat);
1701
1702 rcu_read_lock();
1703 stat->uid = 0;
1704 stat->gid = 0;
1705 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1706 if (task) {
1707 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1708 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001709 cred = __task_cred(task);
1710 stat->uid = cred->euid;
1711 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001712 }
1713 }
1714 rcu_read_unlock();
1715 return 0;
1716}
1717
1718/* dentry stuff */
1719
1720/*
1721 * Exceptional case: normally we are not allowed to unhash a busy
1722 * directory. In this case, however, we can do it - no aliasing problems
1723 * due to the way we treat inodes.
1724 *
1725 * Rewrite the inode's ownerships here because the owning task may have
1726 * performed a setuid(), etc.
1727 *
1728 * Before the /proc/pid/status file was created the only way to read
1729 * the effective uid of a /process was to stat /proc/pid. Reading
1730 * /proc/pid/status is slow enough that procps and other packages
1731 * kept stating /proc/pid. To keep the rules in /proc simple I have
1732 * made this apply to all per process world readable and executable
1733 * directories.
1734 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001735int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001736{
Nick Piggin34286d62011-01-07 17:49:57 +11001737 struct inode *inode;
1738 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001739 const struct cred *cred;
1740
Nick Piggin34286d62011-01-07 17:49:57 +11001741 if (nd && nd->flags & LOOKUP_RCU)
1742 return -ECHILD;
1743
1744 inode = dentry->d_inode;
1745 task = get_proc_task(inode);
1746
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001747 if (task) {
1748 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1749 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001750 rcu_read_lock();
1751 cred = __task_cred(task);
1752 inode->i_uid = cred->euid;
1753 inode->i_gid = cred->egid;
1754 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001755 } else {
1756 inode->i_uid = 0;
1757 inode->i_gid = 0;
1758 }
1759 inode->i_mode &= ~(S_ISUID | S_ISGID);
1760 security_task_to_inode(task, inode);
1761 put_task_struct(task);
1762 return 1;
1763 }
1764 d_drop(dentry);
1765 return 0;
1766}
1767
Nick Pigginfe15ce42011-01-07 17:49:23 +11001768static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001769{
1770 /* Is the task we represent dead?
1771 * If so, then don't put the dentry on the lru list,
1772 * kill it immediately.
1773 */
1774 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1775}
1776
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001777const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778{
1779 .d_revalidate = pid_revalidate,
1780 .d_delete = pid_delete_dentry,
1781};
1782
1783/* Lookups */
1784
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001785/*
1786 * Fill a directory entry.
1787 *
1788 * If possible create the dcache entry and derive our inode number and
1789 * file type from dcache entry.
1790 *
1791 * Since all of the proc inode numbers are dynamically generated, the inode
1792 * numbers do not exist until the inode is cache. This means creating the
1793 * the dcache entry in readdir is necessary to keep the inode numbers
1794 * reported by readdir in sync with the inode numbers reported
1795 * by stat.
1796 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001797int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1798 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001799 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001800{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001801 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001802 struct inode *inode;
1803 struct qstr qname;
1804 ino_t ino = 0;
1805 unsigned type = DT_UNKNOWN;
1806
1807 qname.name = name;
1808 qname.len = len;
1809 qname.hash = full_name_hash(name, len);
1810
1811 child = d_lookup(dir, &qname);
1812 if (!child) {
1813 struct dentry *new;
1814 new = d_alloc(dir, &qname);
1815 if (new) {
1816 child = instantiate(dir->d_inode, new, task, ptr);
1817 if (child)
1818 dput(new);
1819 else
1820 child = new;
1821 }
1822 }
1823 if (!child || IS_ERR(child) || !child->d_inode)
1824 goto end_instantiate;
1825 inode = child->d_inode;
1826 if (inode) {
1827 ino = inode->i_ino;
1828 type = inode->i_mode >> 12;
1829 }
1830 dput(child);
1831end_instantiate:
1832 if (!ino)
1833 ino = find_inode_number(dir, &qname);
1834 if (!ino)
1835 ino = 1;
1836 return filldir(dirent, name, len, filp->f_pos, ino, type);
1837}
1838
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001839static unsigned name_to_int(struct dentry *dentry)
1840{
1841 const char *name = dentry->d_name.name;
1842 int len = dentry->d_name.len;
1843 unsigned n = 0;
1844
1845 if (len > 1 && *name == '0')
1846 goto out;
1847 while (len-- > 0) {
1848 unsigned c = *name++ - '0';
1849 if (c > 9)
1850 goto out;
1851 if (n >= (~0U-9)/10)
1852 goto out;
1853 n *= 10;
1854 n += c;
1855 }
1856 return n;
1857out:
1858 return ~0U;
1859}
1860
Miklos Szeredi27932742007-05-08 00:26:17 -07001861#define PROC_FDINFO_MAX 64
1862
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001863static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001864{
1865 struct task_struct *task = get_proc_task(inode);
1866 struct files_struct *files = NULL;
1867 struct file *file;
1868 int fd = proc_fd(inode);
1869
1870 if (task) {
1871 files = get_files_struct(task);
1872 put_task_struct(task);
1873 }
1874 if (files) {
1875 /*
1876 * We are not taking a ref to the file structure, so we must
1877 * hold ->file_lock.
1878 */
1879 spin_lock(&files->file_lock);
1880 file = fcheck_files(files, fd);
1881 if (file) {
Linus Torvalds4f0bf012011-08-06 11:51:33 -07001882 unsigned int f_flags;
1883 struct fdtable *fdt;
1884
1885 fdt = files_fdtable(files);
1886 f_flags = file->f_flags & ~O_CLOEXEC;
1887 if (FD_ISSET(fd, fdt->close_on_exec))
1888 f_flags |= O_CLOEXEC;
1889
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001890 if (path) {
1891 *path = file->f_path;
1892 path_get(&file->f_path);
1893 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001894 if (info)
1895 snprintf(info, PROC_FDINFO_MAX,
1896 "pos:\t%lli\n"
1897 "flags:\t0%o\n",
1898 (long long) file->f_pos,
Linus Torvalds4f0bf012011-08-06 11:51:33 -07001899 f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001900 spin_unlock(&files->file_lock);
1901 put_files_struct(files);
1902 return 0;
1903 }
1904 spin_unlock(&files->file_lock);
1905 put_files_struct(files);
1906 }
1907 return -ENOENT;
1908}
1909
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001910static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001911{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001912 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001913}
1914
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001915static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1916{
Nick Piggin34286d62011-01-07 17:49:57 +11001917 struct inode *inode;
1918 struct task_struct *task;
1919 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001920 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001921 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001922
Nick Piggin34286d62011-01-07 17:49:57 +11001923 if (nd && nd->flags & LOOKUP_RCU)
1924 return -ECHILD;
1925
1926 inode = dentry->d_inode;
1927 task = get_proc_task(inode);
1928 fd = proc_fd(inode);
1929
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001930 if (task) {
1931 files = get_files_struct(task);
1932 if (files) {
1933 rcu_read_lock();
1934 if (fcheck_files(files, fd)) {
1935 rcu_read_unlock();
1936 put_files_struct(files);
1937 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001938 rcu_read_lock();
1939 cred = __task_cred(task);
1940 inode->i_uid = cred->euid;
1941 inode->i_gid = cred->egid;
1942 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001943 } else {
1944 inode->i_uid = 0;
1945 inode->i_gid = 0;
1946 }
1947 inode->i_mode &= ~(S_ISUID | S_ISGID);
1948 security_task_to_inode(task, inode);
1949 put_task_struct(task);
1950 return 1;
1951 }
1952 rcu_read_unlock();
1953 put_files_struct(files);
1954 }
1955 put_task_struct(task);
1956 }
1957 d_drop(dentry);
1958 return 0;
1959}
1960
Al Virod72f71e2009-02-20 05:58:47 +00001961static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001962{
1963 .d_revalidate = tid_fd_revalidate,
1964 .d_delete = pid_delete_dentry,
1965};
1966
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001967static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001968 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001969{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001970 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001971 struct file *file;
1972 struct files_struct *files;
1973 struct inode *inode;
1974 struct proc_inode *ei;
1975 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001976
Eric W. Biederman61a28782006-10-02 02:18:49 -07001977 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001978 if (!inode)
1979 goto out;
1980 ei = PROC_I(inode);
1981 ei->fd = fd;
1982 files = get_files_struct(task);
1983 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001984 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001985 inode->i_mode = S_IFLNK;
1986
1987 /*
1988 * We are not taking a ref to the file structure, so we must
1989 * hold ->file_lock.
1990 */
1991 spin_lock(&files->file_lock);
1992 file = fcheck_files(files, fd);
1993 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001994 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001995 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001996 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001997 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001998 inode->i_mode |= S_IWUSR | S_IXUSR;
1999 spin_unlock(&files->file_lock);
2000 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002001
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002002 inode->i_op = &proc_pid_link_inode_operations;
2003 inode->i_size = 64;
2004 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002005 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002006 d_add(dentry, inode);
2007 /* Close the race of the process dying before we return the dentry */
2008 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002009 error = NULL;
2010
2011 out:
2012 return error;
2013out_unlock:
2014 spin_unlock(&files->file_lock);
2015 put_files_struct(files);
2016out_iput:
2017 iput(inode);
2018 goto out;
2019}
2020
Miklos Szeredi27932742007-05-08 00:26:17 -07002021static struct dentry *proc_lookupfd_common(struct inode *dir,
2022 struct dentry *dentry,
2023 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002024{
2025 struct task_struct *task = get_proc_task(dir);
2026 unsigned fd = name_to_int(dentry);
2027 struct dentry *result = ERR_PTR(-ENOENT);
2028
2029 if (!task)
2030 goto out_no_task;
2031 if (fd == ~0U)
2032 goto out;
2033
Miklos Szeredi27932742007-05-08 00:26:17 -07002034 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002035out:
2036 put_task_struct(task);
2037out_no_task:
2038 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002039}
2040
Miklos Szeredi27932742007-05-08 00:26:17 -07002041static int proc_readfd_common(struct file * filp, void * dirent,
2042 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002044 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07002045 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002046 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07002047 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 struct files_struct * files;
2050
2051 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002052 if (!p)
2053 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 fd = filp->f_pos;
2057 switch (fd) {
2058 case 0:
2059 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2060 goto out;
2061 filp->f_pos++;
2062 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07002063 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2065 goto out;
2066 filp->f_pos++;
2067 default:
2068 files = get_files_struct(p);
2069 if (!files)
2070 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002071 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04002073 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07002075 char name[PROC_NUMBUF];
2076 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 if (!fcheck_files(files, fd))
2079 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002080 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Miklos Szeredi27932742007-05-08 00:26:17 -07002082 len = snprintf(name, sizeof(name), "%d", fd);
2083 if (proc_fill_cache(filp, dirent, filldir,
2084 name, len, instantiate,
2085 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07002086 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 break;
2088 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002089 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002091 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 put_files_struct(files);
2093 }
2094out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002095 put_task_struct(p);
2096out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return retval;
2098}
2099
Miklos Szeredi27932742007-05-08 00:26:17 -07002100static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2101 struct nameidata *nd)
2102{
2103 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2104}
2105
2106static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2107{
2108 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2109}
2110
2111static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2112 size_t len, loff_t *ppos)
2113{
2114 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002115 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002116 if (!err)
2117 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2118 return err;
2119}
2120
2121static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002122 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002123 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002124 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002125};
2126
Arjan van de Ven00977a52007-02-12 00:55:34 -08002127static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 .read = generic_read_dir,
2129 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002130 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131};
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002134 * /proc/pid/fd needs a special permission handler so that a process can still
2135 * access /proc/self/fd after it has executed a setuid().
2136 */
Nick Pigginb74c79e2011-01-07 17:49:58 +11002137static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002138{
Al Virocf127912011-06-18 20:35:23 -04002139 int rv = generic_permission(inode, mask, flags, NULL);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002140 if (rv == 0)
2141 return 0;
2142 if (task_pid(current) == proc_pid(inode))
2143 rv = 0;
2144 return rv;
2145}
2146
2147/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 * proc directories can do almost nothing..
2149 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002150static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002152 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002153 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154};
2155
Miklos Szeredi27932742007-05-08 00:26:17 -07002156static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2157 struct dentry *dentry, struct task_struct *task, const void *ptr)
2158{
2159 unsigned fd = *(unsigned *)ptr;
2160 struct inode *inode;
2161 struct proc_inode *ei;
2162 struct dentry *error = ERR_PTR(-ENOENT);
2163
2164 inode = proc_pid_make_inode(dir->i_sb, task);
2165 if (!inode)
2166 goto out;
2167 ei = PROC_I(inode);
2168 ei->fd = fd;
2169 inode->i_mode = S_IFREG | S_IRUSR;
2170 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002171 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002172 d_add(dentry, inode);
2173 /* Close the race of the process dying before we return the dentry */
2174 if (tid_fd_revalidate(dentry, NULL))
2175 error = NULL;
2176
2177 out:
2178 return error;
2179}
2180
2181static struct dentry *proc_lookupfdinfo(struct inode *dir,
2182 struct dentry *dentry,
2183 struct nameidata *nd)
2184{
2185 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2186}
2187
2188static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2189{
2190 return proc_readfd_common(filp, dirent, filldir,
2191 proc_fdinfo_instantiate);
2192}
2193
2194static const struct file_operations proc_fdinfo_operations = {
2195 .read = generic_read_dir,
2196 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002197 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002198};
2199
2200/*
2201 * proc directories can do almost nothing..
2202 */
2203static const struct inode_operations proc_fdinfo_inode_operations = {
2204 .lookup = proc_lookupfdinfo,
2205 .setattr = proc_setattr,
2206};
2207
2208
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002209static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002210 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002211{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002212 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002213 struct inode *inode;
2214 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002215 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002216
Eric W. Biederman61a28782006-10-02 02:18:49 -07002217 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002218 if (!inode)
2219 goto out;
2220
2221 ei = PROC_I(inode);
2222 inode->i_mode = p->mode;
2223 if (S_ISDIR(inode->i_mode))
2224 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2225 if (p->iop)
2226 inode->i_op = p->iop;
2227 if (p->fop)
2228 inode->i_fop = p->fop;
2229 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002230 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002231 d_add(dentry, inode);
2232 /* Close the race of the process dying before we return the dentry */
2233 if (pid_revalidate(dentry, NULL))
2234 error = NULL;
2235out:
2236 return error;
2237}
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239static struct dentry *proc_pident_lookup(struct inode *dir,
2240 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002241 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002242 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002244 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002245 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002246 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002248 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Eric W. Biederman99f89552006-06-26 00:25:55 -07002250 if (!task)
2251 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002253 /*
2254 * Yes, it does not scale. And it should not. Don't add
2255 * new entries into /proc/<tgid>/ without very good reasons.
2256 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002257 last = &ents[nents - 1];
2258 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (p->len != dentry->d_name.len)
2260 continue;
2261 if (!memcmp(dentry->d_name.name, p->name, p->len))
2262 break;
2263 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002264 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 goto out;
2266
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002267 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002269 put_task_struct(task);
2270out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002271 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
Eric Dumazetc5141e62007-05-08 00:26:15 -07002274static int proc_pident_fill_cache(struct file *filp, void *dirent,
2275 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002276{
2277 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2278 proc_pident_instantiate, task, p);
2279}
2280
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002281static int proc_pident_readdir(struct file *filp,
2282 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002283 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002284{
2285 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002286 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002287 struct inode *inode = dentry->d_inode;
2288 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002289 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002290 ino_t ino;
2291 int ret;
2292
2293 ret = -ENOENT;
2294 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002295 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002296
2297 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002298 i = filp->f_pos;
2299 switch (i) {
2300 case 0:
2301 ino = inode->i_ino;
2302 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2303 goto out;
2304 i++;
2305 filp->f_pos++;
2306 /* fall through */
2307 case 1:
2308 ino = parent_ino(dentry);
2309 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2310 goto out;
2311 i++;
2312 filp->f_pos++;
2313 /* fall through */
2314 default:
2315 i -= 2;
2316 if (i >= nents) {
2317 ret = 1;
2318 goto out;
2319 }
2320 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002321 last = &ents[nents - 1];
2322 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002323 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002324 goto out;
2325 filp->f_pos++;
2326 p++;
2327 }
2328 }
2329
2330 ret = 1;
2331out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002332 put_task_struct(task);
2333out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002334 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002338static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2339 size_t count, loff_t *ppos)
2340{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002341 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002342 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002343 ssize_t length;
2344 struct task_struct *task = get_proc_task(inode);
2345
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002346 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002347 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002348
2349 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002350 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002351 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002352 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002353 if (length > 0)
2354 length = simple_read_from_buffer(buf, count, ppos, p, length);
2355 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002356 return length;
2357}
2358
2359static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2360 size_t count, loff_t *ppos)
2361{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002362 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002363 char *page;
2364 ssize_t length;
2365 struct task_struct *task = get_proc_task(inode);
2366
2367 length = -ESRCH;
2368 if (!task)
2369 goto out_no_task;
2370 if (count > PAGE_SIZE)
2371 count = PAGE_SIZE;
2372
2373 /* No partial writes. */
2374 length = -EINVAL;
2375 if (*ppos != 0)
2376 goto out;
2377
2378 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002379 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002380 if (!page)
2381 goto out;
2382
2383 length = -EFAULT;
2384 if (copy_from_user(page, buf, count))
2385 goto out_free;
2386
David Howells107db7c2009-05-08 13:55:27 +01002387 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002388 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002389 if (length < 0)
2390 goto out_free;
2391
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002392 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002393 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002394 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002395 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002396out_free:
2397 free_page((unsigned long) page);
2398out:
2399 put_task_struct(task);
2400out_no_task:
2401 return length;
2402}
2403
Arjan van de Ven00977a52007-02-12 00:55:34 -08002404static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002405 .read = proc_pid_attr_read,
2406 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002407 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002408};
2409
Eric Dumazetc5141e62007-05-08 00:26:15 -07002410static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002411 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2412 REG("prev", S_IRUGO, proc_pid_attr_operations),
2413 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2414 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2415 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2416 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002417};
2418
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002419static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 void * dirent, filldir_t filldir)
2421{
2422 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002423 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Arjan van de Ven00977a52007-02-12 00:55:34 -08002426static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002428 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002429 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430};
2431
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002432static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 struct dentry *dentry, struct nameidata *nd)
2434{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002435 return proc_pident_lookup(dir, dentry,
2436 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002439static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002440 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002441 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002442 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443};
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445#endif
2446
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002447#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002448static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2449 size_t count, loff_t *ppos)
2450{
2451 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2452 struct mm_struct *mm;
2453 char buffer[PROC_NUMBUF];
2454 size_t len;
2455 int ret;
2456
2457 if (!task)
2458 return -ESRCH;
2459
2460 ret = 0;
2461 mm = get_task_mm(task);
2462 if (mm) {
2463 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2464 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2465 MMF_DUMP_FILTER_SHIFT));
2466 mmput(mm);
2467 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2468 }
2469
2470 put_task_struct(task);
2471
2472 return ret;
2473}
2474
2475static ssize_t proc_coredump_filter_write(struct file *file,
2476 const char __user *buf,
2477 size_t count,
2478 loff_t *ppos)
2479{
2480 struct task_struct *task;
2481 struct mm_struct *mm;
2482 char buffer[PROC_NUMBUF], *end;
2483 unsigned int val;
2484 int ret;
2485 int i;
2486 unsigned long mask;
2487
2488 ret = -EFAULT;
2489 memset(buffer, 0, sizeof(buffer));
2490 if (count > sizeof(buffer) - 1)
2491 count = sizeof(buffer) - 1;
2492 if (copy_from_user(buffer, buf, count))
2493 goto out_no_task;
2494
2495 ret = -EINVAL;
2496 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2497 if (*end == '\n')
2498 end++;
2499 if (end - buffer == 0)
2500 goto out_no_task;
2501
2502 ret = -ESRCH;
2503 task = get_proc_task(file->f_dentry->d_inode);
2504 if (!task)
2505 goto out_no_task;
2506
2507 ret = end - buffer;
2508 mm = get_task_mm(task);
2509 if (!mm)
2510 goto out_no_mm;
2511
2512 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2513 if (val & mask)
2514 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2515 else
2516 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2517 }
2518
2519 mmput(mm);
2520 out_no_mm:
2521 put_task_struct(task);
2522 out_no_task:
2523 return ret;
2524}
2525
2526static const struct file_operations proc_coredump_filter_operations = {
2527 .read = proc_coredump_filter_read,
2528 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002529 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002530};
2531#endif
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533/*
2534 * /proc/self:
2535 */
2536static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2537 int buflen)
2538{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002539 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002540 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002541 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002542 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002543 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002544 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return vfs_readlink(dentry,buffer,buflen,tmp);
2546}
2547
Al Viro008b1502005-08-20 00:17:39 +01002548static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002550 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002551 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002552 char *name = ERR_PTR(-ENOENT);
2553 if (tgid) {
2554 name = __getname();
2555 if (!name)
2556 name = ERR_PTR(-ENOMEM);
2557 else
2558 sprintf(name, "%d", tgid);
2559 }
2560 nd_set_link(nd, name);
2561 return NULL;
2562}
2563
2564static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2565 void *cookie)
2566{
2567 char *s = nd_get_link(nd);
2568 if (!IS_ERR(s))
2569 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002570}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002572static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 .readlink = proc_self_readlink,
2574 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002575 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576};
2577
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002578/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002579 * proc base
2580 *
2581 * These are the directory entries in the root directory of /proc
2582 * that properly belong to the /proc filesystem, as they describe
2583 * describe something that is process related.
2584 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002585static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002586 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002587 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002588};
2589
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002590static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002591 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002592{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002593 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002594 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002595 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002596 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002597
2598 /* Allocate the inode */
2599 error = ERR_PTR(-ENOMEM);
2600 inode = new_inode(dir->i_sb);
2601 if (!inode)
2602 goto out;
2603
2604 /* Initialize the inode */
2605 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002606 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002607 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002608
2609 /*
2610 * grab the reference to the task.
2611 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002612 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002613 if (!ei->pid)
2614 goto out_iput;
2615
Eric W. Biederman801199c2006-10-02 02:18:48 -07002616 inode->i_mode = p->mode;
2617 if (S_ISDIR(inode->i_mode))
2618 inode->i_nlink = 2;
2619 if (S_ISLNK(inode->i_mode))
2620 inode->i_size = 64;
2621 if (p->iop)
2622 inode->i_op = p->iop;
2623 if (p->fop)
2624 inode->i_fop = p->fop;
2625 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002626 d_add(dentry, inode);
2627 error = NULL;
2628out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002629 return error;
2630out_iput:
2631 iput(inode);
2632 goto out;
2633}
2634
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002635static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2636{
2637 struct dentry *error;
2638 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002639 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002640
2641 error = ERR_PTR(-ENOENT);
2642
2643 if (!task)
2644 goto out_no_task;
2645
2646 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002647 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2648 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002649 if (p->len != dentry->d_name.len)
2650 continue;
2651 if (!memcmp(dentry->d_name.name, p->name, p->len))
2652 break;
2653 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002654 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002655 goto out;
2656
2657 error = proc_base_instantiate(dir, dentry, task, p);
2658
2659out:
2660 put_task_struct(task);
2661out_no_task:
2662 return error;
2663}
2664
Eric Dumazetc5141e62007-05-08 00:26:15 -07002665static int proc_base_fill_cache(struct file *filp, void *dirent,
2666 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002667{
2668 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2669 proc_base_instantiate, task, p);
2670}
2671
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002672#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002673static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002674{
Andrea Righi940389b2008-07-28 00:48:12 +02002675 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002676 unsigned long flags;
Vasiliy Kulikov8cd3f192011-07-26 16:08:38 -07002677 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002678
Vasiliy Kulikov8cd3f192011-07-26 16:08:38 -07002679 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2680 if (result)
2681 return result;
2682
2683 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2684 result = -EACCES;
2685 goto out_unlock;
2686 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002687
Andrea Righi59954772008-07-27 17:29:15 +02002688 if (whole && lock_task_sighand(task, &flags)) {
2689 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002690
Andrea Righi59954772008-07-27 17:29:15 +02002691 task_io_accounting_add(&acct, &task->signal->ioac);
2692 while_each_thread(task, t)
2693 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002694
Andrea Righi59954772008-07-27 17:29:15 +02002695 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002696 }
Vasiliy Kulikov8cd3f192011-07-26 16:08:38 -07002697 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002698 "rchar: %llu\n"
2699 "wchar: %llu\n"
2700 "syscr: %llu\n"
2701 "syscw: %llu\n"
2702 "read_bytes: %llu\n"
2703 "write_bytes: %llu\n"
2704 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002705 (unsigned long long)acct.rchar,
2706 (unsigned long long)acct.wchar,
2707 (unsigned long long)acct.syscr,
2708 (unsigned long long)acct.syscw,
2709 (unsigned long long)acct.read_bytes,
2710 (unsigned long long)acct.write_bytes,
2711 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov8cd3f192011-07-26 16:08:38 -07002712out_unlock:
2713 mutex_unlock(&task->signal->cred_guard_mutex);
2714 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002715}
Andrea Righi297c5d92008-07-25 01:48:49 -07002716
2717static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2718{
2719 return do_io_accounting(task, buffer, 0);
2720}
2721
2722static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2723{
2724 return do_io_accounting(task, buffer, 1);
2725}
2726#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002727
Kees Cook47830722008-10-06 03:11:58 +04002728static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2729 struct pid *pid, struct task_struct *task)
2730{
Al Viroa9712bc2011-03-23 15:52:50 -04002731 int err = lock_trace(task);
2732 if (!err) {
2733 seq_printf(m, "%08x\n", task->personality);
2734 unlock_trace(task);
2735 }
2736 return err;
Kees Cook47830722008-10-06 03:11:58 +04002737}
2738
Eric W. Biederman801199c2006-10-02 02:18:48 -07002739/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002740 * Thread groups
2741 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002742static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002743static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002744
Eric Dumazetc5141e62007-05-08 00:26:15 -07002745static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002746 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2747 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2748 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002749 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002750#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002751 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002752#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002753 REG("environ", S_IRUSR, proc_environ_operations),
2754 INF("auxv", S_IRUSR, proc_pid_auxv),
2755 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002756 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002757 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002758#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002759 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002760#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002761#ifdef CONFIG_SCHED_AUTOGROUP
2762 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2763#endif
john stultz4614a696b2009-12-14 18:00:05 -08002764 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002765#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002766 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002767#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002768 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2769 ONE("stat", S_IRUGO, proc_tgid_stat),
2770 ONE("statm", S_IRUGO, proc_pid_statm),
2771 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002772#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002773 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002774#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002775 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2776 LNK("cwd", proc_cwd_link),
2777 LNK("root", proc_root_link),
2778 LNK("exe", proc_exe_link),
2779 REG("mounts", S_IRUGO, proc_mounts_operations),
2780 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2781 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002782#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002783 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2784 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002785 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002786#endif
2787#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002788 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002789#endif
2790#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002791 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002792#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002793#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002794 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002795#endif
2796#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002797 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002798#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002799#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002800 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002801#endif
Paul Menage8793d852007-10-18 23:39:39 -07002802#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002803 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002804#endif
Paul Menagea4243162007-10-18 23:39:35 -07002805#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002806 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002807#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002808 INF("oom_score", S_IRUGO, proc_oom_score),
Mike Chan766e9932008-04-24 10:22:26 -07002809 ANDROID("oom_adj",S_IRUGO|S_IWUSR, oom_adjust),
David Rientjesa63d83f2010-08-09 17:19:46 -07002810 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002811#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002812 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2813 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002814#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002815#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002816 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002817#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002818#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002819 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002820#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002821#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002822 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002823#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002824#ifdef CONFIG_HARDWALL
2825 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2826#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002827};
2828
2829static int proc_tgid_base_readdir(struct file * filp,
2830 void * dirent, filldir_t filldir)
2831{
2832 return proc_pident_readdir(filp,dirent,filldir,
2833 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2834}
2835
Arjan van de Ven00977a52007-02-12 00:55:34 -08002836static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002837 .read = generic_read_dir,
2838 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002839 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002840};
2841
2842static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002843 return proc_pident_lookup(dir, dentry,
2844 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002845}
2846
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002847static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002848 .lookup = proc_tgid_base_lookup,
2849 .getattr = pid_getattr,
2850 .setattr = proc_setattr,
2851};
2852
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002853static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002855 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002856 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002857 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Eric W. Biederman48e64842006-06-26 00:25:48 -07002859 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002860 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2861 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002862 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002863 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002864 d_drop(dentry);
2865 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Eric W. Biederman48e64842006-06-26 00:25:48 -07002868 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002869 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2870 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002871 if (!leader)
2872 goto out;
2873
2874 name.name = "task";
2875 name.len = strlen(name.name);
2876 dir = d_hash_and_lookup(leader, &name);
2877 if (!dir)
2878 goto out_put_leader;
2879
2880 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002881 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002882 dentry = d_hash_and_lookup(dir, &name);
2883 if (dentry) {
2884 shrink_dcache_parent(dentry);
2885 d_drop(dentry);
2886 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002888
2889 dput(dir);
2890out_put_leader:
2891 dput(leader);
2892out:
2893 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
Randy Dunlap0895e912007-10-21 21:00:10 -07002896/**
2897 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2898 * @task: task that should be flushed.
2899 *
2900 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002901 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002902 * in. This call is supposed to do all of this job.
2903 *
2904 * Looks in the dcache for
2905 * /proc/@pid
2906 * /proc/@tgid/task/@pid
2907 * if either directory is present flushes it and all of it'ts children
2908 * from the dcache.
2909 *
2910 * It is safe and reasonable to cache /proc entries for a task until
2911 * that task exits. After that they just clog up the dcache with
2912 * useless entries, possibly causing useful dcache entries to be
2913 * flushed instead. This routine is proved to flush those useless
2914 * dcache entries at process exit time.
2915 *
2916 * NOTE: This routine is just an optimization so it does not guarantee
2917 * that no dcache entries will exist at process exit time it
2918 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002919 */
2920
2921void proc_flush_task(struct task_struct *task)
2922{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002923 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002924 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002925 struct upid *upid;
2926
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002927 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002928 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002929
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002930 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002931 upid = &pid->numbers[i];
2932 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002933 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002934 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002935
2936 upid = &pid->numbers[pid->level];
2937 if (upid->nr == 1)
2938 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002939}
2940
Adrian Bunk9711ef92006-12-06 20:38:31 -08002941static struct dentry *proc_pid_instantiate(struct inode *dir,
2942 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002943 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002944{
2945 struct dentry *error = ERR_PTR(-ENOENT);
2946 struct inode *inode;
2947
Eric W. Biederman61a28782006-10-02 02:18:49 -07002948 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002949 if (!inode)
2950 goto out;
2951
2952 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2953 inode->i_op = &proc_tgid_base_inode_operations;
2954 inode->i_fop = &proc_tgid_base_operations;
2955 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002956
2957 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2958 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002959
Nick Pigginfb045ad2011-01-07 17:49:55 +11002960 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002961
2962 d_add(dentry, inode);
2963 /* Close the race of the process dying before we return the dentry */
2964 if (pid_revalidate(dentry, NULL))
2965 error = NULL;
2966out:
2967 return error;
2968}
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2971{
Dan Carpenter73d36462010-05-26 14:43:25 -07002972 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002975 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Eric W. Biederman801199c2006-10-02 02:18:48 -07002977 result = proc_base_lookup(dir, dentry);
2978 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2979 goto out;
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 tgid = name_to_int(dentry);
2982 if (tgid == ~0U)
2983 goto out;
2984
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002985 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002986 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002987 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (task)
2989 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002990 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (!task)
2992 goto out;
2993
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002994 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002995 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002997 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003001 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003004struct tgid_iter {
3005 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003006 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003007};
3008static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3009{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003010 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003012 if (iter.task)
3013 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003014 rcu_read_lock();
3015retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003016 iter.task = NULL;
3017 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003018 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003019 iter.tgid = pid_nr_ns(pid, ns);
3020 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003021 /* What we to know is if the pid we have find is the
3022 * pid of a thread_group_leader. Testing for task
3023 * being a thread_group_leader is the obvious thing
3024 * todo but there is a window when it fails, due to
3025 * the pid transfer logic in de_thread.
3026 *
3027 * So we perform the straight forward test of seeing
3028 * if the pid we have found is the pid of a thread
3029 * group leader, and don't worry if the task we have
3030 * found doesn't happen to be a thread group leader.
3031 * As we don't care in the case of readdir.
3032 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003033 if (!iter.task || !has_group_leader_pid(iter.task)) {
3034 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003035 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003036 }
3037 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003039 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003040 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003043#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Eric W. Biederman61a28782006-10-02 02:18:49 -07003045static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003046 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003047{
3048 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003049 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003050 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003051 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003052}
3053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054/* for the /proc/ directory itself, after non-process stuff has been done */
3055int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3056{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003057 unsigned int nr;
3058 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003059 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003060 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003062 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3063 goto out_no_task;
3064 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3065
3066 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003067 if (!reaper)
3068 goto out_no_task;
3069
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003070 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003071 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003072 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
3075
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003076 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003077 iter.task = NULL;
3078 iter.tgid = filp->f_pos - TGID_OFFSET;
3079 for (iter = next_tgid(ns, iter);
3080 iter.task;
3081 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3082 filp->f_pos = iter.tgid + TGID_OFFSET;
3083 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3084 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003085 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003088 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3089out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003090 put_task_struct(reaper);
3091out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return 0;
3093}
3094
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003095/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003096 * Tasks
3097 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003098static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003099 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003100 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003101 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003102 REG("environ", S_IRUSR, proc_environ_operations),
3103 INF("auxv", S_IRUSR, proc_pid_auxv),
3104 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003105 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003106 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003107#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003108 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003109#endif
john stultz4614a696b2009-12-14 18:00:05 -08003110 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003111#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003112 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003113#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003114 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3115 ONE("stat", S_IRUGO, proc_tid_stat),
3116 ONE("statm", S_IRUGO, proc_pid_statm),
3117 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003118#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003119 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003120#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003121 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3122 LNK("cwd", proc_cwd_link),
3123 LNK("root", proc_root_link),
3124 LNK("exe", proc_exe_link),
3125 REG("mounts", S_IRUGO, proc_mounts_operations),
3126 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003127#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003128 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3129 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003130 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003131#endif
3132#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003133 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003134#endif
3135#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003136 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003137#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003138#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003139 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003140#endif
3141#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003142 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003143#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003144#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003145 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003146#endif
Paul Menage8793d852007-10-18 23:39:39 -07003147#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003148 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003149#endif
Paul Menagea4243162007-10-18 23:39:35 -07003150#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003151 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003152#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003153 INF("oom_score", S_IRUGO, proc_oom_score),
3154 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003155 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003156#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003157 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003158 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003159#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003160#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003161 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003162#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003163#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003164 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003165#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003166#ifdef CONFIG_HARDWALL
3167 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3168#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003169};
3170
3171static int proc_tid_base_readdir(struct file * filp,
3172 void * dirent, filldir_t filldir)
3173{
3174 return proc_pident_readdir(filp,dirent,filldir,
3175 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3176}
3177
3178static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003179 return proc_pident_lookup(dir, dentry,
3180 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003181}
3182
Arjan van de Ven00977a52007-02-12 00:55:34 -08003183static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003184 .read = generic_read_dir,
3185 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003186 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003187};
3188
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003189static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003190 .lookup = proc_tid_base_lookup,
3191 .getattr = pid_getattr,
3192 .setattr = proc_setattr,
3193};
3194
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003195static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003196 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003197{
3198 struct dentry *error = ERR_PTR(-ENOENT);
3199 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003200 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003201
3202 if (!inode)
3203 goto out;
3204 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3205 inode->i_op = &proc_tid_base_inode_operations;
3206 inode->i_fop = &proc_tid_base_operations;
3207 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003208
3209 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3210 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003211
Nick Pigginfb045ad2011-01-07 17:49:55 +11003212 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003213
3214 d_add(dentry, inode);
3215 /* Close the race of the process dying before we return the dentry */
3216 if (pid_revalidate(dentry, NULL))
3217 error = NULL;
3218out:
3219 return error;
3220}
3221
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003222static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3223{
3224 struct dentry *result = ERR_PTR(-ENOENT);
3225 struct task_struct *task;
3226 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003227 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003228 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003229
3230 if (!leader)
3231 goto out_no_task;
3232
3233 tid = name_to_int(dentry);
3234 if (tid == ~0U)
3235 goto out;
3236
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003237 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003238 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003239 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003240 if (task)
3241 get_task_struct(task);
3242 rcu_read_unlock();
3243 if (!task)
3244 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003245 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003246 goto out_drop_task;
3247
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003248 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003249out_drop_task:
3250 put_task_struct(task);
3251out:
3252 put_task_struct(leader);
3253out_no_task:
3254 return result;
3255}
3256
3257/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003258 * Find the first tid of a thread group to return to user space.
3259 *
3260 * Usually this is just the thread group leader, but if the users
3261 * buffer was too small or there was a seek into the middle of the
3262 * directory we have more work todo.
3263 *
3264 * In the case of a short read we start with find_task_by_pid.
3265 *
3266 * In the case of a seek we start with the leader and walk nr
3267 * threads past it.
3268 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003269static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003270 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003271{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003272 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003273
Eric W. Biedermancc288732006-06-26 00:26:01 -07003274 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003275 /* Attempt to start with the pid of a thread */
3276 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003277 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003278 if (pos && (pos->group_leader == leader))
3279 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003280 }
3281
3282 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003283 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003284 if (nr && nr >= get_nr_threads(leader))
3285 goto out;
3286
3287 /* If we haven't found our starting place yet start
3288 * with the leader and walk nr threads forward.
3289 */
3290 for (pos = leader; nr > 0; --nr) {
3291 pos = next_thread(pos);
3292 if (pos == leader) {
3293 pos = NULL;
3294 goto out;
3295 }
3296 }
3297found:
3298 get_task_struct(pos);
3299out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003300 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003301 return pos;
3302}
3303
3304/*
3305 * Find the next thread in the thread list.
3306 * Return NULL if there is an error or no next thread.
3307 *
3308 * The reference to the input task_struct is released.
3309 */
3310static struct task_struct *next_tid(struct task_struct *start)
3311{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003312 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003313 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003314 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003315 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003316 if (thread_group_leader(pos))
3317 pos = NULL;
3318 else
3319 get_task_struct(pos);
3320 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003321 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003322 put_task_struct(start);
3323 return pos;
3324}
3325
Eric W. Biederman61a28782006-10-02 02:18:49 -07003326static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3327 struct task_struct *task, int tid)
3328{
3329 char name[PROC_NUMBUF];
3330 int len = snprintf(name, sizeof(name), "%d", tid);
3331 return proc_fill_cache(filp, dirent, filldir, name, len,
3332 proc_task_instantiate, task, NULL);
3333}
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335/* for the /proc/TGID/task/ directories */
3336static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3337{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003338 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003340 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003341 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 int retval = -ENOENT;
3343 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003344 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003345 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003347 task = get_proc_task(inode);
3348 if (!task)
3349 goto out_no_task;
3350 rcu_read_lock();
3351 if (pid_alive(task)) {
3352 leader = task->group_leader;
3353 get_task_struct(leader);
3354 }
3355 rcu_read_unlock();
3356 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003357 if (!leader)
3358 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 retval = 0;
3360
Linus Torvaldsee568b22009-03-17 10:02:35 -07003361 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 case 0:
3363 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003364 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003366 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /* fall through */
3368 case 1:
3369 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003370 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003372 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 /* fall through */
3374 }
3375
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003376 /* f_version caches the tgid value that the last readdir call couldn't
3377 * return. lseek aka telldir automagically resets f_version to 0.
3378 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003379 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003380 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003381 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003382 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003383 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003384 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003385 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003386 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003387 /* returning this tgid failed, save it as the first
3388 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003389 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003390 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 }
3394out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003395 put_task_struct(leader);
3396out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return retval;
3398}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003399
3400static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3401{
3402 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003403 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003404 generic_fillattr(inode, stat);
3405
Eric W. Biederman99f89552006-06-26 00:25:55 -07003406 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003407 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003408 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003409 }
3410
3411 return 0;
3412}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003413
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003414static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003415 .lookup = proc_task_lookup,
3416 .getattr = proc_task_getattr,
3417 .setattr = proc_setattr,
3418};
3419
Arjan van de Ven00977a52007-02-12 00:55:34 -08003420static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003421 .read = generic_read_dir,
3422 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003423 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003424};