blob: b3e298f2942115c944b91188e6ecb434aed4fbee [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
Vegard Nossumaed54172008-06-05 22:46:53 -0700136/*
137 * Count the number of hardlinks for the pid_entry table, excluding the .
138 * and .. links.
139 */
140static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
141 unsigned int n)
142{
143 unsigned int i;
144 unsigned int count;
145
146 count = 0;
147 for (i = 0; i < n; ++i) {
148 if (S_ISDIR(entries[i].mode))
149 ++count;
150 }
151
152 return count;
153}
154
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200155static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000157 int result = -ENOENT;
158
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700159 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200160 if (task->fs) {
161 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000162 result = 0;
163 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700164 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000165 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700166}
167
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800168static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700169{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700170 struct task_struct *task = get_proc_task(inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700172
173 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200174 task_lock(task);
175 if (task->fs) {
176 get_fs_pwd(task->fs, path);
177 result = 0;
178 }
179 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700180 put_task_struct(task);
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return result;
183}
184
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800185static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700187 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700189
190 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200191 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700192 put_task_struct(task);
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return result;
195}
196
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800197static struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
Al Viro831830b2008-01-02 14:09:57 +0000198{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200199 struct mm_struct *mm;
Al Viroec6fd8a2011-02-15 22:22:54 -0500200 int err;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200201
Al Viroec6fd8a2011-02-15 22:22:54 -0500202 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
203 if (err)
204 return ERR_PTR(err);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200205
206 mm = get_task_mm(task);
207 if (mm && mm != current->mm &&
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800208 !ptrace_may_access(task, mode)) {
Oleg Nesterov704b8362009-07-10 03:27:40 +0200209 mmput(mm);
Al Viroec6fd8a2011-02-15 22:22:54 -0500210 mm = ERR_PTR(-EACCES);
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200211 }
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700212 mutex_unlock(&task->signal->cred_guard_mutex);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200213
Al Viro831830b2008-01-02 14:09:57 +0000214 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000215}
216
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800217struct mm_struct *mm_for_maps(struct task_struct *task)
218{
219 return mm_access(task, PTRACE_MODE_READ);
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static int proc_pid_cmdline(struct task_struct *task, char * buffer)
223{
224 int res = 0;
225 unsigned int len;
226 struct mm_struct *mm = get_task_mm(task);
227 if (!mm)
228 goto out;
229 if (!mm->arg_end)
230 goto out_mm; /* Shh! No looking before we're done */
231
232 len = mm->arg_end - mm->arg_start;
233
234 if (len > PAGE_SIZE)
235 len = PAGE_SIZE;
236
237 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
238
239 // If the nul at the end of args has been overwritten, then
240 // assume application is using setproctitle(3).
241 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
242 len = strnlen(buffer, res);
243 if (len < res) {
244 res = len;
245 } else {
246 len = mm->env_end - mm->env_start;
247 if (len > PAGE_SIZE - res)
248 len = PAGE_SIZE - res;
249 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
250 res = strnlen(buffer, res);
251 }
252 }
253out_mm:
254 mmput(mm);
255out:
256 return res;
257}
258
259static int proc_pid_auxv(struct task_struct *task, char *buffer)
260{
Al Viro2fadaef2011-02-15 22:52:11 -0500261 struct mm_struct *mm = mm_for_maps(task);
262 int res = PTR_ERR(mm);
263 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300265 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300267 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 res = nwords * sizeof(mm->saved_auxv[0]);
269 if (res > PAGE_SIZE)
270 res = PAGE_SIZE;
271 memcpy(buffer, mm->saved_auxv, res);
272 mmput(mm);
273 }
274 return res;
275}
276
277
278#ifdef CONFIG_KALLSYMS
279/*
280 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
281 * Returns the resolved symbol. If that fails, simply return the address.
282 */
283static int proc_pid_wchan(struct task_struct *task, char *buffer)
284{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700285 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700286 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 wchan = get_wchan(task);
289
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700290 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600291 if (!ptrace_may_access(task, PTRACE_MODE_READ))
292 return 0;
293 else
294 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700295 else
296 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298#endif /* CONFIG_KALLSYMS */
299
Al Viroa9712bc2011-03-23 15:52:50 -0400300static int lock_trace(struct task_struct *task)
301{
302 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
303 if (err)
304 return err;
305 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
306 mutex_unlock(&task->signal->cred_guard_mutex);
307 return -EPERM;
308 }
309 return 0;
310}
311
312static void unlock_trace(struct task_struct *task)
313{
314 mutex_unlock(&task->signal->cred_guard_mutex);
315}
316
Ken Chen2ec220e2008-11-10 11:26:08 +0300317#ifdef CONFIG_STACKTRACE
318
319#define MAX_STACK_TRACE_DEPTH 64
320
321static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
322 struct pid *pid, struct task_struct *task)
323{
324 struct stack_trace trace;
325 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400326 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300327 int i;
328
329 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
330 if (!entries)
331 return -ENOMEM;
332
333 trace.nr_entries = 0;
334 trace.max_entries = MAX_STACK_TRACE_DEPTH;
335 trace.entries = entries;
336 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300337
Al Viroa9712bc2011-03-23 15:52:50 -0400338 err = lock_trace(task);
339 if (!err) {
340 save_stack_trace_tsk(task, &trace);
341
342 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700343 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400344 (void *)entries[i], (void *)entries[i]);
345 }
346 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300347 }
348 kfree(entries);
349
Al Viroa9712bc2011-03-23 15:52:50 -0400350 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300351}
352#endif
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#ifdef CONFIG_SCHEDSTATS
355/*
356 * Provides /proc/PID/schedstat
357 */
358static int proc_pid_schedstat(struct task_struct *task, char *buffer)
359{
Balbir Singh172ba842007-07-09 18:52:00 +0200360 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100361 (unsigned long long)task->se.sum_exec_runtime,
362 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200363 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365#endif
366
Arjan van de Ven97455122008-01-25 21:08:34 +0100367#ifdef CONFIG_LATENCYTOP
368static int lstats_show_proc(struct seq_file *m, void *v)
369{
370 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800371 struct inode *inode = m->private;
372 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100373
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800374 if (!task)
375 return -ESRCH;
376 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100377 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800378 struct latency_record *lr = &task->latency_record[i];
379 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100380 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800381 seq_printf(m, "%i %li %li",
382 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100383 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800384 unsigned long bt = lr->backtrace[q];
385 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100386 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800387 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100388 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800389 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100390 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800391 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100392 }
393
394 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800395 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100396 return 0;
397}
398
399static int lstats_open(struct inode *inode, struct file *file)
400{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800401 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800402}
403
Arjan van de Ven97455122008-01-25 21:08:34 +0100404static ssize_t lstats_write(struct file *file, const char __user *buf,
405 size_t count, loff_t *offs)
406{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800407 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100408
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800409 if (!task)
410 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100411 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800412 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100413
414 return count;
415}
416
417static const struct file_operations proc_lstats_operations = {
418 .open = lstats_open,
419 .read = seq_read,
420 .write = lstats_write,
421 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800422 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100423};
424
425#endif
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static int proc_oom_score(struct task_struct *task, char *buffer)
428{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200429 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700431 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200432 if (pid_alive(task))
David Rientjesa63d83f2010-08-09 17:19:46 -0700433 points = oom_badness(task, NULL, NULL,
434 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700435 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return sprintf(buffer, "%lu\n", points);
437}
438
Neil Hormand85f50d2007-10-18 23:40:37 -0700439struct limit_names {
440 char *name;
441 char *unit;
442};
443
444static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700445 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700446 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
447 [RLIMIT_DATA] = {"Max data size", "bytes"},
448 [RLIMIT_STACK] = {"Max stack size", "bytes"},
449 [RLIMIT_CORE] = {"Max core file size", "bytes"},
450 [RLIMIT_RSS] = {"Max resident set", "bytes"},
451 [RLIMIT_NPROC] = {"Max processes", "processes"},
452 [RLIMIT_NOFILE] = {"Max open files", "files"},
453 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
454 [RLIMIT_AS] = {"Max address space", "bytes"},
455 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
456 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
457 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
458 [RLIMIT_NICE] = {"Max nice priority", NULL},
459 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800460 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700461};
462
463/* Display limits for a process */
464static int proc_pid_limits(struct task_struct *task, char *buffer)
465{
466 unsigned int i;
467 int count = 0;
468 unsigned long flags;
469 char *bufptr = buffer;
470
471 struct rlimit rlim[RLIM_NLIMITS];
472
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400473 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700474 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700475 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
476 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700477
478 /*
479 * print the file header
480 */
481 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
482 "Limit", "Soft Limit", "Hard Limit", "Units");
483
484 for (i = 0; i < RLIM_NLIMITS; i++) {
485 if (rlim[i].rlim_cur == RLIM_INFINITY)
486 count += sprintf(&bufptr[count], "%-25s %-20s ",
487 lnames[i].name, "unlimited");
488 else
489 count += sprintf(&bufptr[count], "%-25s %-20lu ",
490 lnames[i].name, rlim[i].rlim_cur);
491
492 if (rlim[i].rlim_max == RLIM_INFINITY)
493 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
494 else
495 count += sprintf(&bufptr[count], "%-20lu ",
496 rlim[i].rlim_max);
497
498 if (lnames[i].unit)
499 count += sprintf(&bufptr[count], "%-10s\n",
500 lnames[i].unit);
501 else
502 count += sprintf(&bufptr[count], "\n");
503 }
504
505 return count;
506}
507
Roland McGrathebcb6732008-07-25 19:46:00 -0700508#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
509static int proc_pid_syscall(struct task_struct *task, char *buffer)
510{
511 long nr;
512 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400513 int res = lock_trace(task);
514 if (res)
515 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700516
517 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400518 res = sprintf(buffer, "running\n");
519 else if (nr < 0)
520 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
521 else
522 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700523 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
524 nr,
525 args[0], args[1], args[2], args[3], args[4], args[5],
526 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400527 unlock_trace(task);
528 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700529}
530#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/************************************************************************/
533/* Here the fs part begins */
534/************************************************************************/
535
536/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700537static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700539 struct task_struct *task;
540 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700541 /* Allow access to a task's file descriptors if it is us or we
542 * may use ptrace attach to the process and find out that
543 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700544 */
545 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700546 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400547 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700548 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700549 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700550 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800553int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700554{
555 int error;
556 struct inode *inode = dentry->d_inode;
557
558 if (attr->ia_valid & ATTR_MODE)
559 return -EPERM;
560
561 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200562 if (error)
563 return error;
564
565 if ((attr->ia_valid & ATTR_SIZE) &&
566 attr->ia_size != i_size_read(inode)) {
567 error = vmtruncate(inode, attr->ia_size);
568 if (error)
569 return error;
570 }
571
572 setattr_copy(inode, attr);
573 mark_inode_dirty(inode);
574 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700575}
576
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800577static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700578 .setattr = proc_setattr,
579};
580
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100581static int mounts_open_common(struct inode *inode, struct file *file,
582 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700584 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700585 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800586 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100587 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500588 struct proc_mounts *p;
589 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Eric W. Biederman99f89552006-06-26 00:25:55 -0700591 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700592 rcu_read_lock();
593 nsp = task_nsproxy(task);
594 if (nsp) {
595 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800596 if (ns)
597 get_mnt_ns(ns);
598 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700599 rcu_read_unlock();
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200600 if (ns && get_task_root(task, &root) == 0)
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000601 ret = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700602 put_task_struct(task);
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100605 if (!ns)
606 goto err;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000607 if (ret)
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100608 goto err_put_ns;
609
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100610 ret = -ENOMEM;
611 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
612 if (!p)
613 goto err_put_path;
614
615 file->private_data = &p->m;
616 ret = seq_open(file, op);
617 if (ret)
618 goto err_free;
619
620 p->m.private = p;
621 p->ns = ns;
622 p->root = root;
623 p->event = ns->event;
624
625 return 0;
626
627 err_free:
628 kfree(p);
629 err_put_path:
630 path_put(&root);
631 err_put_ns:
632 put_mnt_ns(ns);
633 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return ret;
635}
636
637static int mounts_release(struct inode *inode, struct file *file)
638{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100639 struct proc_mounts *p = file->private_data;
640 path_put(&p->root);
641 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return seq_release(inode, file);
643}
644
Al Viro5addc5d2005-11-07 17:15:49 -0500645static unsigned mounts_poll(struct file *file, poll_table *wait)
646{
647 struct proc_mounts *p = file->private_data;
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900648 unsigned res = POLLIN | POLLRDNORM;
Al Viro5addc5d2005-11-07 17:15:49 -0500649
Al Viro9f5596a2010-02-05 00:40:25 -0500650 poll_wait(file, &p->ns->poll, wait);
651 if (mnt_had_events(p))
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900652 res |= POLLERR | POLLPRI;
Al Viro5addc5d2005-11-07 17:15:49 -0500653
654 return res;
655}
656
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100657static int mounts_open(struct inode *inode, struct file *file)
658{
659 return mounts_open_common(inode, file, &mounts_op);
660}
661
Arjan van de Ven00977a52007-02-12 00:55:34 -0800662static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .open = mounts_open,
664 .read = seq_read,
665 .llseek = seq_lseek,
666 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500667 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668};
669
Ram Pai2d4d4862008-03-27 13:06:25 +0100670static int mountinfo_open(struct inode *inode, struct file *file)
671{
672 return mounts_open_common(inode, file, &mountinfo_op);
673}
674
675static const struct file_operations proc_mountinfo_operations = {
676 .open = mountinfo_open,
677 .read = seq_read,
678 .llseek = seq_lseek,
679 .release = mounts_release,
680 .poll = mounts_poll,
681};
682
Chuck Leverb4629fe2006-03-20 13:44:12 -0500683static int mountstats_open(struct inode *inode, struct file *file)
684{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100685 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500686}
687
Arjan van de Ven00977a52007-02-12 00:55:34 -0800688static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500689 .open = mountstats_open,
690 .read = seq_read,
691 .llseek = seq_lseek,
692 .release = mounts_release,
693};
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
696
697static ssize_t proc_info_read(struct file * file, char __user * buf,
698 size_t count, loff_t *ppos)
699{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800700 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 unsigned long page;
702 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700703 struct task_struct *task = get_proc_task(inode);
704
705 length = -ESRCH;
706 if (!task)
707 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (count > PROC_BLOCK_SIZE)
710 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700711
712 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700713 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700714 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 length = PROC_I(inode)->op.proc_read(task, (char*)page);
717
718 if (length >= 0)
719 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
720 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700721out:
722 put_task_struct(task);
723out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return length;
725}
726
Arjan van de Ven00977a52007-02-12 00:55:34 -0800727static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100729 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730};
731
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800732static int proc_single_show(struct seq_file *m, void *v)
733{
734 struct inode *inode = m->private;
735 struct pid_namespace *ns;
736 struct pid *pid;
737 struct task_struct *task;
738 int ret;
739
740 ns = inode->i_sb->s_fs_info;
741 pid = proc_pid(inode);
742 task = get_pid_task(pid, PIDTYPE_PID);
743 if (!task)
744 return -ESRCH;
745
746 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
747
748 put_task_struct(task);
749 return ret;
750}
751
752static int proc_single_open(struct inode *inode, struct file *filp)
753{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800754 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800755}
756
757static const struct file_operations proc_single_file_operations = {
758 .open = proc_single_open,
759 .read = seq_read,
760 .llseek = seq_lseek,
761 .release = single_release,
762};
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764static int mem_open(struct inode* inode, struct file* file)
765{
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800766 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
767 struct mm_struct *mm;
768
769 if (!task)
770 return -ESRCH;
771
772 mm = mm_access(task, PTRACE_MODE_ATTACH);
773 put_task_struct(task);
774
775 if (IS_ERR(mm))
776 return PTR_ERR(mm);
777
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700778 /* OK to pass negative loff_t, we can catch out-of-range */
779 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800780 file->private_data = mm;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
783}
784
785static ssize_t mem_read(struct file * file, char __user * buf,
786 size_t count, loff_t *ppos)
787{
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800788 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 char *page;
790 unsigned long src = *ppos;
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800791 struct mm_struct *mm = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800793 if (!mm)
794 return 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700795
Mel Gormane12ba742007-10-16 01:25:52 -0700796 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!page)
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800798 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 ret = 0;
801
802 while (count > 0) {
803 int this_len, retval;
804
805 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400806 retval = access_remote_vm(mm, src, page, this_len, 0);
807 if (!retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!ret)
809 ret = -EIO;
810 break;
811 }
812
813 if (copy_to_user(buf, page, retval)) {
814 ret = -EFAULT;
815 break;
816 }
817
818 ret += retval;
819 src += retval;
820 buf += retval;
821 count -= retval;
822 }
823 *ppos = src;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return ret;
827}
828
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800829static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 size_t count, loff_t *ppos)
831{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700832 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 unsigned long dst = *ppos;
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800835 struct mm_struct *mm = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800837 if (!mm)
838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Mel Gormane12ba742007-10-16 01:25:52 -0700840 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (!page)
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800842 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700844 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 while (count > 0) {
846 int this_len, retval;
847
848 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
849 if (copy_from_user(page, buf, this_len)) {
850 copied = -EFAULT;
851 break;
852 }
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400853 retval = access_remote_vm(mm, dst, page, this_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!retval) {
855 if (!copied)
856 copied = -EIO;
857 break;
858 }
859 copied += retval;
860 buf += retval;
861 dst += retval;
862 count -= retval;
863 }
864 *ppos = dst;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700865
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700866 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return copied;
868}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Matt Mackall85863e42008-02-04 22:29:04 -0800870loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 switch (orig) {
873 case 0:
874 file->f_pos = offset;
875 break;
876 case 1:
877 file->f_pos += offset;
878 break;
879 default:
880 return -EINVAL;
881 }
882 force_successful_syscall_return();
883 return file->f_pos;
884}
885
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800886static int mem_release(struct inode *inode, struct file *file)
887{
888 struct mm_struct *mm = file->private_data;
Oleg Nesterov401f6372012-01-31 17:14:38 +0100889 if (mm)
890 mmput(mm);
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800891 return 0;
892}
893
Arjan van de Ven00977a52007-02-12 00:55:34 -0800894static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 .llseek = mem_lseek,
896 .read = mem_read,
897 .write = mem_write,
898 .open = mem_open,
Linus Torvaldsc8fec252012-01-17 15:21:19 -0800899 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900};
901
James Pearson315e28c2007-10-16 23:30:17 -0700902static ssize_t environ_read(struct file *file, char __user *buf,
903 size_t count, loff_t *ppos)
904{
905 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
906 char *page;
907 unsigned long src = *ppos;
908 int ret = -ESRCH;
909 struct mm_struct *mm;
910
911 if (!task)
912 goto out_no_task;
913
James Pearson315e28c2007-10-16 23:30:17 -0700914 ret = -ENOMEM;
915 page = (char *)__get_free_page(GFP_TEMPORARY);
916 if (!page)
917 goto out;
918
James Pearson315e28c2007-10-16 23:30:17 -0700919
Al Virod6f64b82011-02-15 22:26:01 -0500920 mm = mm_for_maps(task);
921 ret = PTR_ERR(mm);
922 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700923 goto out_free;
924
Al Virod6f64b82011-02-15 22:26:01 -0500925 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -0700926 while (count > 0) {
927 int this_len, retval, max_len;
928
929 this_len = mm->env_end - (mm->env_start + src);
930
931 if (this_len <= 0)
932 break;
933
934 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
935 this_len = (this_len > max_len) ? max_len : this_len;
936
937 retval = access_process_vm(task, (mm->env_start + src),
938 page, this_len, 0);
939
940 if (retval <= 0) {
941 ret = retval;
942 break;
943 }
944
945 if (copy_to_user(buf, page, retval)) {
946 ret = -EFAULT;
947 break;
948 }
949
950 ret += retval;
951 src += retval;
952 buf += retval;
953 count -= retval;
954 }
955 *ppos = src;
956
957 mmput(mm);
958out_free:
959 free_page((unsigned long) page);
960out:
961 put_task_struct(task);
962out_no_task:
963 return ret;
964}
965
966static const struct file_operations proc_environ_operations = {
967 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100968 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700969};
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971static ssize_t oom_adjust_read(struct file *file, char __user *buf,
972 size_t count, loff_t *ppos)
973{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800974 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700975 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700977 int oom_adjust = OOM_DISABLE;
978 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Eric W. Biederman99f89552006-06-26 00:25:55 -0700980 if (!task)
981 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700982
983 if (lock_task_sighand(task, &flags)) {
984 oom_adjust = task->signal->oom_adj;
985 unlock_task_sighand(task, &flags);
986 }
987
Eric W. Biederman99f89552006-06-26 00:25:55 -0700988 put_task_struct(task);
989
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700990 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700991
992 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
995static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
996 size_t count, loff_t *ppos)
997{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700998 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700999 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001000 int oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001001 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001002 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001004 memset(buffer, 0, sizeof(buffer));
1005 if (count > sizeof(buffer) - 1)
1006 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001007 if (copy_from_user(buffer, buf, count)) {
1008 err = -EFAULT;
1009 goto out;
1010 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001011
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001012 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001013 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001014 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001015 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -07001016 oom_adjust != OOM_DISABLE) {
1017 err = -EINVAL;
1018 goto out;
1019 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001020
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001021 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001022 if (!task) {
1023 err = -ESRCH;
1024 goto out;
1025 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001026
Ying Han3d5992d2010-10-26 14:21:23 -07001027 task_lock(task);
1028 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001029 err = -EINVAL;
1030 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001031 }
1032
David Rientjesd19d5472010-10-26 14:21:26 -07001033 if (!lock_task_sighand(task, &flags)) {
1034 err = -ESRCH;
1035 goto err_task_lock;
1036 }
1037
1038 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1039 err = -EACCES;
1040 goto err_sighand;
1041 }
1042
Ying Han3d5992d2010-10-26 14:21:23 -07001043 if (oom_adjust != task->signal->oom_adj) {
1044 if (oom_adjust == OOM_DISABLE)
1045 atomic_inc(&task->mm->oom_disable_count);
1046 if (task->signal->oom_adj == OOM_DISABLE)
1047 atomic_dec(&task->mm->oom_disable_count);
1048 }
1049
David Rientjes51b1bd22010-08-09 17:19:47 -07001050 /*
1051 * Warn that /proc/pid/oom_adj is deprecated, see
1052 * Documentation/feature-removal-schedule.txt.
1053 */
1054 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1055 "please use /proc/%d/oom_score_adj instead.\n",
1056 current->comm, task_pid_nr(current),
1057 task_pid_nr(task), task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001058 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -07001059 /*
1060 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1061 * value is always attainable.
1062 */
1063 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1064 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1065 else
1066 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1067 -OOM_DISABLE;
David Rientjes723548b2010-10-26 14:21:25 -07001068err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001069 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001070err_task_lock:
1071 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001072 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001073out:
1074 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Arjan van de Ven00977a52007-02-12 00:55:34 -08001077static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 .read = oom_adjust_read,
1079 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001080 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081};
1082
David Rientjesa63d83f2010-08-09 17:19:46 -07001083static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1084 size_t count, loff_t *ppos)
1085{
1086 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1087 char buffer[PROC_NUMBUF];
1088 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1089 unsigned long flags;
1090 size_t len;
1091
1092 if (!task)
1093 return -ESRCH;
1094 if (lock_task_sighand(task, &flags)) {
1095 oom_score_adj = task->signal->oom_score_adj;
1096 unlock_task_sighand(task, &flags);
1097 }
1098 put_task_struct(task);
1099 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1100 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1101}
1102
1103static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1104 size_t count, loff_t *ppos)
1105{
1106 struct task_struct *task;
1107 char buffer[PROC_NUMBUF];
1108 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001109 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001110 int err;
1111
1112 memset(buffer, 0, sizeof(buffer));
1113 if (count > sizeof(buffer) - 1)
1114 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001115 if (copy_from_user(buffer, buf, count)) {
1116 err = -EFAULT;
1117 goto out;
1118 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001119
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001120 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001121 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001122 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001123 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001124 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1125 err = -EINVAL;
1126 goto out;
1127 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001128
1129 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001130 if (!task) {
1131 err = -ESRCH;
1132 goto out;
1133 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001134
Ying Han3d5992d2010-10-26 14:21:23 -07001135 task_lock(task);
1136 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001137 err = -EINVAL;
1138 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001139 }
David Rientjesd19d5472010-10-26 14:21:26 -07001140
1141 if (!lock_task_sighand(task, &flags)) {
1142 err = -ESRCH;
1143 goto err_task_lock;
1144 }
1145
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001146 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001147 !capable(CAP_SYS_RESOURCE)) {
1148 err = -EACCES;
1149 goto err_sighand;
1150 }
1151
Ying Han3d5992d2010-10-26 14:21:23 -07001152 if (oom_score_adj != task->signal->oom_score_adj) {
1153 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1154 atomic_inc(&task->mm->oom_disable_count);
1155 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1156 atomic_dec(&task->mm->oom_disable_count);
1157 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001158 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001159 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1160 task->signal->oom_score_adj_min = oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001161 /*
1162 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1163 * always attainable.
1164 */
1165 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1166 task->signal->oom_adj = OOM_DISABLE;
1167 else
1168 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1169 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001170err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001171 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001172err_task_lock:
1173 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001174 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001175out:
1176 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001177}
1178
1179static const struct file_operations proc_oom_score_adj_operations = {
1180 .read = oom_score_adj_read,
1181 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001182 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001183};
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185#ifdef CONFIG_AUDITSYSCALL
1186#define TMPBUFLEN 21
1187static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1188 size_t count, loff_t *ppos)
1189{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001190 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001191 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ssize_t length;
1193 char tmpbuf[TMPBUFLEN];
1194
Eric W. Biederman99f89552006-06-26 00:25:55 -07001195 if (!task)
1196 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001198 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001199 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1201}
1202
1203static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1204 size_t count, loff_t *ppos)
1205{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001206 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 char *page, *tmp;
1208 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 uid_t loginuid;
1210
1211 if (!capable(CAP_AUDIT_CONTROL))
1212 return -EPERM;
1213
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001214 rcu_read_lock();
1215 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1216 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001218 }
1219 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Al Viroe0182902006-05-18 08:28:02 -04001221 if (count >= PAGE_SIZE)
1222 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (*ppos != 0) {
1225 /* No partial writes. */
1226 return -EINVAL;
1227 }
Mel Gormane12ba742007-10-16 01:25:52 -07001228 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!page)
1230 return -ENOMEM;
1231 length = -EFAULT;
1232 if (copy_from_user(page, buf, count))
1233 goto out_free_page;
1234
Al Viroe0182902006-05-18 08:28:02 -04001235 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 loginuid = simple_strtoul(page, &tmp, 10);
1237 if (tmp == page) {
1238 length = -EINVAL;
1239 goto out_free_page;
1240
1241 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001242 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (likely(length == 0))
1244 length = count;
1245
1246out_free_page:
1247 free_page((unsigned long) page);
1248 return length;
1249}
1250
Arjan van de Ven00977a52007-02-12 00:55:34 -08001251static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 .read = proc_loginuid_read,
1253 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001254 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255};
Eric Paris1e0bd752008-03-13 08:15:31 -04001256
1257static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1258 size_t count, loff_t *ppos)
1259{
1260 struct inode * inode = file->f_path.dentry->d_inode;
1261 struct task_struct *task = get_proc_task(inode);
1262 ssize_t length;
1263 char tmpbuf[TMPBUFLEN];
1264
1265 if (!task)
1266 return -ESRCH;
1267 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1268 audit_get_sessionid(task));
1269 put_task_struct(task);
1270 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1271}
1272
1273static const struct file_operations proc_sessionid_operations = {
1274 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001275 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001276};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277#endif
1278
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001279#ifdef CONFIG_FAULT_INJECTION
1280static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1281 size_t count, loff_t *ppos)
1282{
1283 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1284 char buffer[PROC_NUMBUF];
1285 size_t len;
1286 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001287
1288 if (!task)
1289 return -ESRCH;
1290 make_it_fail = task->make_it_fail;
1291 put_task_struct(task);
1292
1293 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001294
1295 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001296}
1297
1298static ssize_t proc_fault_inject_write(struct file * file,
1299 const char __user * buf, size_t count, loff_t *ppos)
1300{
1301 struct task_struct *task;
1302 char buffer[PROC_NUMBUF], *end;
1303 int make_it_fail;
1304
1305 if (!capable(CAP_SYS_RESOURCE))
1306 return -EPERM;
1307 memset(buffer, 0, sizeof(buffer));
1308 if (count > sizeof(buffer) - 1)
1309 count = sizeof(buffer) - 1;
1310 if (copy_from_user(buffer, buf, count))
1311 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001312 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1313 if (*end)
1314 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001315 task = get_proc_task(file->f_dentry->d_inode);
1316 if (!task)
1317 return -ESRCH;
1318 task->make_it_fail = make_it_fail;
1319 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001320
1321 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001322}
1323
Arjan van de Ven00977a52007-02-12 00:55:34 -08001324static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001325 .read = proc_fault_inject_read,
1326 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001327 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001328};
1329#endif
1330
Arjan van de Ven97455122008-01-25 21:08:34 +01001331
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001332#ifdef CONFIG_SCHED_DEBUG
1333/*
1334 * Print out various scheduling related per-task fields:
1335 */
1336static int sched_show(struct seq_file *m, void *v)
1337{
1338 struct inode *inode = m->private;
1339 struct task_struct *p;
1340
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001341 p = get_proc_task(inode);
1342 if (!p)
1343 return -ESRCH;
1344 proc_sched_show_task(p, m);
1345
1346 put_task_struct(p);
1347
1348 return 0;
1349}
1350
1351static ssize_t
1352sched_write(struct file *file, const char __user *buf,
1353 size_t count, loff_t *offset)
1354{
1355 struct inode *inode = file->f_path.dentry->d_inode;
1356 struct task_struct *p;
1357
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001358 p = get_proc_task(inode);
1359 if (!p)
1360 return -ESRCH;
1361 proc_sched_set_task(p);
1362
1363 put_task_struct(p);
1364
1365 return count;
1366}
1367
1368static int sched_open(struct inode *inode, struct file *filp)
1369{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001370 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001371}
1372
1373static const struct file_operations proc_pid_sched_operations = {
1374 .open = sched_open,
1375 .read = seq_read,
1376 .write = sched_write,
1377 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001378 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001379};
1380
1381#endif
1382
Mike Galbraith5091faa2010-11-30 14:18:03 +01001383#ifdef CONFIG_SCHED_AUTOGROUP
1384/*
1385 * Print out autogroup related information:
1386 */
1387static int sched_autogroup_show(struct seq_file *m, void *v)
1388{
1389 struct inode *inode = m->private;
1390 struct task_struct *p;
1391
1392 p = get_proc_task(inode);
1393 if (!p)
1394 return -ESRCH;
1395 proc_sched_autogroup_show_task(p, m);
1396
1397 put_task_struct(p);
1398
1399 return 0;
1400}
1401
1402static ssize_t
1403sched_autogroup_write(struct file *file, const char __user *buf,
1404 size_t count, loff_t *offset)
1405{
1406 struct inode *inode = file->f_path.dentry->d_inode;
1407 struct task_struct *p;
1408 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001409 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001410 int err;
1411
1412 memset(buffer, 0, sizeof(buffer));
1413 if (count > sizeof(buffer) - 1)
1414 count = sizeof(buffer) - 1;
1415 if (copy_from_user(buffer, buf, count))
1416 return -EFAULT;
1417
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001418 err = kstrtoint(strstrip(buffer), 0, &nice);
1419 if (err < 0)
1420 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001421
1422 p = get_proc_task(inode);
1423 if (!p)
1424 return -ESRCH;
1425
1426 err = nice;
1427 err = proc_sched_autogroup_set_nice(p, &err);
1428 if (err)
1429 count = err;
1430
1431 put_task_struct(p);
1432
1433 return count;
1434}
1435
1436static int sched_autogroup_open(struct inode *inode, struct file *filp)
1437{
1438 int ret;
1439
1440 ret = single_open(filp, sched_autogroup_show, NULL);
1441 if (!ret) {
1442 struct seq_file *m = filp->private_data;
1443
1444 m->private = inode;
1445 }
1446 return ret;
1447}
1448
1449static const struct file_operations proc_pid_sched_autogroup_operations = {
1450 .open = sched_autogroup_open,
1451 .read = seq_read,
1452 .write = sched_autogroup_write,
1453 .llseek = seq_lseek,
1454 .release = single_release,
1455};
1456
1457#endif /* CONFIG_SCHED_AUTOGROUP */
1458
john stultz4614a696b2009-12-14 18:00:05 -08001459static ssize_t comm_write(struct file *file, const char __user *buf,
1460 size_t count, loff_t *offset)
1461{
1462 struct inode *inode = file->f_path.dentry->d_inode;
1463 struct task_struct *p;
1464 char buffer[TASK_COMM_LEN];
1465
1466 memset(buffer, 0, sizeof(buffer));
1467 if (count > sizeof(buffer) - 1)
1468 count = sizeof(buffer) - 1;
1469 if (copy_from_user(buffer, buf, count))
1470 return -EFAULT;
1471
1472 p = get_proc_task(inode);
1473 if (!p)
1474 return -ESRCH;
1475
1476 if (same_thread_group(current, p))
1477 set_task_comm(p, buffer);
1478 else
1479 count = -EINVAL;
1480
1481 put_task_struct(p);
1482
1483 return count;
1484}
1485
1486static int comm_show(struct seq_file *m, void *v)
1487{
1488 struct inode *inode = m->private;
1489 struct task_struct *p;
1490
1491 p = get_proc_task(inode);
1492 if (!p)
1493 return -ESRCH;
1494
1495 task_lock(p);
1496 seq_printf(m, "%s\n", p->comm);
1497 task_unlock(p);
1498
1499 put_task_struct(p);
1500
1501 return 0;
1502}
1503
1504static int comm_open(struct inode *inode, struct file *filp)
1505{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001506 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001507}
1508
1509static const struct file_operations proc_pid_set_comm_operations = {
1510 .open = comm_open,
1511 .read = seq_read,
1512 .write = comm_write,
1513 .llseek = seq_lseek,
1514 .release = single_release,
1515};
1516
Matt Helsley925d1c42008-04-29 01:01:36 -07001517static int proc_exe_link(struct inode *inode, struct path *exe_path)
1518{
1519 struct task_struct *task;
1520 struct mm_struct *mm;
1521 struct file *exe_file;
1522
1523 task = get_proc_task(inode);
1524 if (!task)
1525 return -ENOENT;
1526 mm = get_task_mm(task);
1527 put_task_struct(task);
1528 if (!mm)
1529 return -ENOENT;
1530 exe_file = get_mm_exe_file(mm);
1531 mmput(mm);
1532 if (exe_file) {
1533 *exe_path = exe_file->f_path;
1534 path_get(&exe_file->f_path);
1535 fput(exe_file);
1536 return 0;
1537 } else
1538 return -ENOENT;
1539}
1540
Al Viro008b1502005-08-20 00:17:39 +01001541static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 struct inode *inode = dentry->d_inode;
1544 int error = -EACCES;
1545
1546 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001547 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Eric W. Biederman778c1142006-06-26 00:25:58 -07001549 /* Are we allowed to snoop on the tasks file descriptors? */
1550 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001553 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554out:
Al Viro008b1502005-08-20 00:17:39 +01001555 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001558static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Mel Gormane12ba742007-10-16 01:25:52 -07001560 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001561 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int len;
1563
1564 if (!tmp)
1565 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001566
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001567 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001568 len = PTR_ERR(pathname);
1569 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001571 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 if (len > buflen)
1574 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001575 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 len = -EFAULT;
1577 out:
1578 free_page((unsigned long)tmp);
1579 return len;
1580}
1581
1582static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1583{
1584 int error = -EACCES;
1585 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001586 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Eric W. Biederman778c1142006-06-26 00:25:58 -07001588 /* Are we allowed to snoop on the tasks file descriptors? */
1589 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001592 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (error)
1594 goto out;
1595
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001596 error = do_proc_readlink(&path, buffer, buflen);
1597 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return error;
1600}
1601
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001602static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001604 .follow_link = proc_pid_follow_link,
1605 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606};
1607
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001608
1609/* building an inode */
1610
1611static int task_dumpable(struct task_struct *task)
1612{
1613 int dumpable = 0;
1614 struct mm_struct *mm;
1615
1616 task_lock(task);
1617 mm = task->mm;
1618 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001619 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001620 task_unlock(task);
1621 if(dumpable == 1)
1622 return 1;
1623 return 0;
1624}
1625
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001626struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001627{
1628 struct inode * inode;
1629 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001630 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001631
1632 /* We need a new inode */
1633
1634 inode = new_inode(sb);
1635 if (!inode)
1636 goto out;
1637
1638 /* Common stuff */
1639 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001640 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001641 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001642 inode->i_op = &proc_def_inode_operations;
1643
1644 /*
1645 * grab the reference to task.
1646 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001647 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001648 if (!ei->pid)
1649 goto out_unlock;
1650
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001651 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001652 rcu_read_lock();
1653 cred = __task_cred(task);
1654 inode->i_uid = cred->euid;
1655 inode->i_gid = cred->egid;
1656 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001657 }
1658 security_task_to_inode(task, inode);
1659
1660out:
1661 return inode;
1662
1663out_unlock:
1664 iput(inode);
1665 return NULL;
1666}
1667
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001668int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001669{
1670 struct inode *inode = dentry->d_inode;
1671 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001672 const struct cred *cred;
1673
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001674 generic_fillattr(inode, stat);
1675
1676 rcu_read_lock();
1677 stat->uid = 0;
1678 stat->gid = 0;
1679 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1680 if (task) {
1681 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1682 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001683 cred = __task_cred(task);
1684 stat->uid = cred->euid;
1685 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001686 }
1687 }
1688 rcu_read_unlock();
1689 return 0;
1690}
1691
1692/* dentry stuff */
1693
1694/*
1695 * Exceptional case: normally we are not allowed to unhash a busy
1696 * directory. In this case, however, we can do it - no aliasing problems
1697 * due to the way we treat inodes.
1698 *
1699 * Rewrite the inode's ownerships here because the owning task may have
1700 * performed a setuid(), etc.
1701 *
1702 * Before the /proc/pid/status file was created the only way to read
1703 * the effective uid of a /process was to stat /proc/pid. Reading
1704 * /proc/pid/status is slow enough that procps and other packages
1705 * kept stating /proc/pid. To keep the rules in /proc simple I have
1706 * made this apply to all per process world readable and executable
1707 * directories.
1708 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001709int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001710{
Nick Piggin34286d62011-01-07 17:49:57 +11001711 struct inode *inode;
1712 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001713 const struct cred *cred;
1714
Nick Piggin34286d62011-01-07 17:49:57 +11001715 if (nd && nd->flags & LOOKUP_RCU)
1716 return -ECHILD;
1717
1718 inode = dentry->d_inode;
1719 task = get_proc_task(inode);
1720
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001721 if (task) {
1722 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1723 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001724 rcu_read_lock();
1725 cred = __task_cred(task);
1726 inode->i_uid = cred->euid;
1727 inode->i_gid = cred->egid;
1728 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001729 } else {
1730 inode->i_uid = 0;
1731 inode->i_gid = 0;
1732 }
1733 inode->i_mode &= ~(S_ISUID | S_ISGID);
1734 security_task_to_inode(task, inode);
1735 put_task_struct(task);
1736 return 1;
1737 }
1738 d_drop(dentry);
1739 return 0;
1740}
1741
Nick Pigginfe15ce42011-01-07 17:49:23 +11001742static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001743{
1744 /* Is the task we represent dead?
1745 * If so, then don't put the dentry on the lru list,
1746 * kill it immediately.
1747 */
1748 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1749}
1750
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001751const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001752{
1753 .d_revalidate = pid_revalidate,
1754 .d_delete = pid_delete_dentry,
1755};
1756
1757/* Lookups */
1758
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001759/*
1760 * Fill a directory entry.
1761 *
1762 * If possible create the dcache entry and derive our inode number and
1763 * file type from dcache entry.
1764 *
1765 * Since all of the proc inode numbers are dynamically generated, the inode
1766 * numbers do not exist until the inode is cache. This means creating the
1767 * the dcache entry in readdir is necessary to keep the inode numbers
1768 * reported by readdir in sync with the inode numbers reported
1769 * by stat.
1770 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001771int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1772 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001773 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001774{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001775 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001776 struct inode *inode;
1777 struct qstr qname;
1778 ino_t ino = 0;
1779 unsigned type = DT_UNKNOWN;
1780
1781 qname.name = name;
1782 qname.len = len;
1783 qname.hash = full_name_hash(name, len);
1784
1785 child = d_lookup(dir, &qname);
1786 if (!child) {
1787 struct dentry *new;
1788 new = d_alloc(dir, &qname);
1789 if (new) {
1790 child = instantiate(dir->d_inode, new, task, ptr);
1791 if (child)
1792 dput(new);
1793 else
1794 child = new;
1795 }
1796 }
1797 if (!child || IS_ERR(child) || !child->d_inode)
1798 goto end_instantiate;
1799 inode = child->d_inode;
1800 if (inode) {
1801 ino = inode->i_ino;
1802 type = inode->i_mode >> 12;
1803 }
1804 dput(child);
1805end_instantiate:
1806 if (!ino)
1807 ino = find_inode_number(dir, &qname);
1808 if (!ino)
1809 ino = 1;
1810 return filldir(dirent, name, len, filp->f_pos, ino, type);
1811}
1812
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001813static unsigned name_to_int(struct dentry *dentry)
1814{
1815 const char *name = dentry->d_name.name;
1816 int len = dentry->d_name.len;
1817 unsigned n = 0;
1818
1819 if (len > 1 && *name == '0')
1820 goto out;
1821 while (len-- > 0) {
1822 unsigned c = *name++ - '0';
1823 if (c > 9)
1824 goto out;
1825 if (n >= (~0U-9)/10)
1826 goto out;
1827 n *= 10;
1828 n += c;
1829 }
1830 return n;
1831out:
1832 return ~0U;
1833}
1834
Miklos Szeredi27932742007-05-08 00:26:17 -07001835#define PROC_FDINFO_MAX 64
1836
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001837static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001838{
1839 struct task_struct *task = get_proc_task(inode);
1840 struct files_struct *files = NULL;
1841 struct file *file;
1842 int fd = proc_fd(inode);
1843
1844 if (task) {
1845 files = get_files_struct(task);
1846 put_task_struct(task);
1847 }
1848 if (files) {
1849 /*
1850 * We are not taking a ref to the file structure, so we must
1851 * hold ->file_lock.
1852 */
1853 spin_lock(&files->file_lock);
1854 file = fcheck_files(files, fd);
1855 if (file) {
Linus Torvalds4f0bf012011-08-06 11:51:33 -07001856 unsigned int f_flags;
1857 struct fdtable *fdt;
1858
1859 fdt = files_fdtable(files);
1860 f_flags = file->f_flags & ~O_CLOEXEC;
1861 if (FD_ISSET(fd, fdt->close_on_exec))
1862 f_flags |= O_CLOEXEC;
1863
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001864 if (path) {
1865 *path = file->f_path;
1866 path_get(&file->f_path);
1867 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001868 if (info)
1869 snprintf(info, PROC_FDINFO_MAX,
1870 "pos:\t%lli\n"
1871 "flags:\t0%o\n",
1872 (long long) file->f_pos,
Linus Torvalds4f0bf012011-08-06 11:51:33 -07001873 f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001874 spin_unlock(&files->file_lock);
1875 put_files_struct(files);
1876 return 0;
1877 }
1878 spin_unlock(&files->file_lock);
1879 put_files_struct(files);
1880 }
1881 return -ENOENT;
1882}
1883
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001884static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001885{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001886 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001887}
1888
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001889static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1890{
Nick Piggin34286d62011-01-07 17:49:57 +11001891 struct inode *inode;
1892 struct task_struct *task;
1893 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001894 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001895 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001896
Nick Piggin34286d62011-01-07 17:49:57 +11001897 if (nd && nd->flags & LOOKUP_RCU)
1898 return -ECHILD;
1899
1900 inode = dentry->d_inode;
1901 task = get_proc_task(inode);
1902 fd = proc_fd(inode);
1903
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001904 if (task) {
1905 files = get_files_struct(task);
1906 if (files) {
1907 rcu_read_lock();
1908 if (fcheck_files(files, fd)) {
1909 rcu_read_unlock();
1910 put_files_struct(files);
1911 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001912 rcu_read_lock();
1913 cred = __task_cred(task);
1914 inode->i_uid = cred->euid;
1915 inode->i_gid = cred->egid;
1916 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001917 } else {
1918 inode->i_uid = 0;
1919 inode->i_gid = 0;
1920 }
1921 inode->i_mode &= ~(S_ISUID | S_ISGID);
1922 security_task_to_inode(task, inode);
1923 put_task_struct(task);
1924 return 1;
1925 }
1926 rcu_read_unlock();
1927 put_files_struct(files);
1928 }
1929 put_task_struct(task);
1930 }
1931 d_drop(dentry);
1932 return 0;
1933}
1934
Al Virod72f71e2009-02-20 05:58:47 +00001935static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001936{
1937 .d_revalidate = tid_fd_revalidate,
1938 .d_delete = pid_delete_dentry,
1939};
1940
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001941static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001942 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001943{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001944 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001945 struct file *file;
1946 struct files_struct *files;
1947 struct inode *inode;
1948 struct proc_inode *ei;
1949 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001950
Eric W. Biederman61a28782006-10-02 02:18:49 -07001951 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001952 if (!inode)
1953 goto out;
1954 ei = PROC_I(inode);
1955 ei->fd = fd;
1956 files = get_files_struct(task);
1957 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001958 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001959 inode->i_mode = S_IFLNK;
1960
1961 /*
1962 * We are not taking a ref to the file structure, so we must
1963 * hold ->file_lock.
1964 */
1965 spin_lock(&files->file_lock);
1966 file = fcheck_files(files, fd);
1967 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001968 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001969 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001970 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001971 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001972 inode->i_mode |= S_IWUSR | S_IXUSR;
1973 spin_unlock(&files->file_lock);
1974 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001975
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001976 inode->i_op = &proc_pid_link_inode_operations;
1977 inode->i_size = 64;
1978 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001979 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001980 d_add(dentry, inode);
1981 /* Close the race of the process dying before we return the dentry */
1982 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001983 error = NULL;
1984
1985 out:
1986 return error;
1987out_unlock:
1988 spin_unlock(&files->file_lock);
1989 put_files_struct(files);
1990out_iput:
1991 iput(inode);
1992 goto out;
1993}
1994
Miklos Szeredi27932742007-05-08 00:26:17 -07001995static struct dentry *proc_lookupfd_common(struct inode *dir,
1996 struct dentry *dentry,
1997 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001998{
1999 struct task_struct *task = get_proc_task(dir);
2000 unsigned fd = name_to_int(dentry);
2001 struct dentry *result = ERR_PTR(-ENOENT);
2002
2003 if (!task)
2004 goto out_no_task;
2005 if (fd == ~0U)
2006 goto out;
2007
Miklos Szeredi27932742007-05-08 00:26:17 -07002008 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002009out:
2010 put_task_struct(task);
2011out_no_task:
2012 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002013}
2014
Miklos Szeredi27932742007-05-08 00:26:17 -07002015static int proc_readfd_common(struct file * filp, void * dirent,
2016 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002018 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07002019 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002020 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07002021 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 struct files_struct * files;
2024
2025 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002026 if (!p)
2027 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 fd = filp->f_pos;
2031 switch (fd) {
2032 case 0:
2033 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2034 goto out;
2035 filp->f_pos++;
2036 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07002037 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2039 goto out;
2040 filp->f_pos++;
2041 default:
2042 files = get_files_struct(p);
2043 if (!files)
2044 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002045 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04002047 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07002049 char name[PROC_NUMBUF];
2050 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 if (!fcheck_files(files, fd))
2053 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002054 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Miklos Szeredi27932742007-05-08 00:26:17 -07002056 len = snprintf(name, sizeof(name), "%d", fd);
2057 if (proc_fill_cache(filp, dirent, filldir,
2058 name, len, instantiate,
2059 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07002060 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 break;
2062 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002063 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002065 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 put_files_struct(files);
2067 }
2068out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002069 put_task_struct(p);
2070out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return retval;
2072}
2073
Miklos Szeredi27932742007-05-08 00:26:17 -07002074static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2075 struct nameidata *nd)
2076{
2077 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2078}
2079
2080static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2081{
2082 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2083}
2084
2085static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2086 size_t len, loff_t *ppos)
2087{
2088 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002089 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002090 if (!err)
2091 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2092 return err;
2093}
2094
2095static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002096 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002097 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002098 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002099};
2100
Arjan van de Ven00977a52007-02-12 00:55:34 -08002101static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 .read = generic_read_dir,
2103 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002104 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105};
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002108 * /proc/pid/fd needs a special permission handler so that a process can still
2109 * access /proc/self/fd after it has executed a setuid().
2110 */
Nick Pigginb74c79e2011-01-07 17:49:58 +11002111static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002112{
Al Virocf127912011-06-18 20:35:23 -04002113 int rv = generic_permission(inode, mask, flags, NULL);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002114 if (rv == 0)
2115 return 0;
2116 if (task_pid(current) == proc_pid(inode))
2117 rv = 0;
2118 return rv;
2119}
2120
2121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 * proc directories can do almost nothing..
2123 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002124static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002126 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002127 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128};
2129
Miklos Szeredi27932742007-05-08 00:26:17 -07002130static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2131 struct dentry *dentry, struct task_struct *task, const void *ptr)
2132{
2133 unsigned fd = *(unsigned *)ptr;
2134 struct inode *inode;
2135 struct proc_inode *ei;
2136 struct dentry *error = ERR_PTR(-ENOENT);
2137
2138 inode = proc_pid_make_inode(dir->i_sb, task);
2139 if (!inode)
2140 goto out;
2141 ei = PROC_I(inode);
2142 ei->fd = fd;
2143 inode->i_mode = S_IFREG | S_IRUSR;
2144 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002145 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002146 d_add(dentry, inode);
2147 /* Close the race of the process dying before we return the dentry */
2148 if (tid_fd_revalidate(dentry, NULL))
2149 error = NULL;
2150
2151 out:
2152 return error;
2153}
2154
2155static struct dentry *proc_lookupfdinfo(struct inode *dir,
2156 struct dentry *dentry,
2157 struct nameidata *nd)
2158{
2159 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2160}
2161
2162static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2163{
2164 return proc_readfd_common(filp, dirent, filldir,
2165 proc_fdinfo_instantiate);
2166}
2167
2168static const struct file_operations proc_fdinfo_operations = {
2169 .read = generic_read_dir,
2170 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002171 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002172};
2173
2174/*
2175 * proc directories can do almost nothing..
2176 */
2177static const struct inode_operations proc_fdinfo_inode_operations = {
2178 .lookup = proc_lookupfdinfo,
2179 .setattr = proc_setattr,
2180};
2181
2182
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002183static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002184 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002185{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002186 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002187 struct inode *inode;
2188 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002189 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002190
Eric W. Biederman61a28782006-10-02 02:18:49 -07002191 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002192 if (!inode)
2193 goto out;
2194
2195 ei = PROC_I(inode);
2196 inode->i_mode = p->mode;
2197 if (S_ISDIR(inode->i_mode))
2198 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2199 if (p->iop)
2200 inode->i_op = p->iop;
2201 if (p->fop)
2202 inode->i_fop = p->fop;
2203 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002204 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002205 d_add(dentry, inode);
2206 /* Close the race of the process dying before we return the dentry */
2207 if (pid_revalidate(dentry, NULL))
2208 error = NULL;
2209out:
2210 return error;
2211}
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213static struct dentry *proc_pident_lookup(struct inode *dir,
2214 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002215 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002216 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002218 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002219 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002220 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002222 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Eric W. Biederman99f89552006-06-26 00:25:55 -07002224 if (!task)
2225 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002227 /*
2228 * Yes, it does not scale. And it should not. Don't add
2229 * new entries into /proc/<tgid>/ without very good reasons.
2230 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002231 last = &ents[nents - 1];
2232 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (p->len != dentry->d_name.len)
2234 continue;
2235 if (!memcmp(dentry->d_name.name, p->name, p->len))
2236 break;
2237 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002238 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 goto out;
2240
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002241 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002243 put_task_struct(task);
2244out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002245 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Eric Dumazetc5141e62007-05-08 00:26:15 -07002248static int proc_pident_fill_cache(struct file *filp, void *dirent,
2249 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002250{
2251 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2252 proc_pident_instantiate, task, p);
2253}
2254
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002255static int proc_pident_readdir(struct file *filp,
2256 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002257 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002258{
2259 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002260 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002261 struct inode *inode = dentry->d_inode;
2262 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002263 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002264 ino_t ino;
2265 int ret;
2266
2267 ret = -ENOENT;
2268 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002269 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002270
2271 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002272 i = filp->f_pos;
2273 switch (i) {
2274 case 0:
2275 ino = inode->i_ino;
2276 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2277 goto out;
2278 i++;
2279 filp->f_pos++;
2280 /* fall through */
2281 case 1:
2282 ino = parent_ino(dentry);
2283 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2284 goto out;
2285 i++;
2286 filp->f_pos++;
2287 /* fall through */
2288 default:
2289 i -= 2;
2290 if (i >= nents) {
2291 ret = 1;
2292 goto out;
2293 }
2294 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002295 last = &ents[nents - 1];
2296 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002297 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002298 goto out;
2299 filp->f_pos++;
2300 p++;
2301 }
2302 }
2303
2304 ret = 1;
2305out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002306 put_task_struct(task);
2307out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002308 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002312static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2313 size_t count, loff_t *ppos)
2314{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002315 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002316 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002317 ssize_t length;
2318 struct task_struct *task = get_proc_task(inode);
2319
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002320 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002321 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002322
2323 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002324 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002325 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002326 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002327 if (length > 0)
2328 length = simple_read_from_buffer(buf, count, ppos, p, length);
2329 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002330 return length;
2331}
2332
2333static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2334 size_t count, loff_t *ppos)
2335{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002336 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002337 char *page;
2338 ssize_t length;
2339 struct task_struct *task = get_proc_task(inode);
2340
2341 length = -ESRCH;
2342 if (!task)
2343 goto out_no_task;
2344 if (count > PAGE_SIZE)
2345 count = PAGE_SIZE;
2346
2347 /* No partial writes. */
2348 length = -EINVAL;
2349 if (*ppos != 0)
2350 goto out;
2351
2352 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002353 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002354 if (!page)
2355 goto out;
2356
2357 length = -EFAULT;
2358 if (copy_from_user(page, buf, count))
2359 goto out_free;
2360
David Howells107db7c2009-05-08 13:55:27 +01002361 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002362 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002363 if (length < 0)
2364 goto out_free;
2365
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002366 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002367 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002368 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002369 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002370out_free:
2371 free_page((unsigned long) page);
2372out:
2373 put_task_struct(task);
2374out_no_task:
2375 return length;
2376}
2377
Arjan van de Ven00977a52007-02-12 00:55:34 -08002378static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002379 .read = proc_pid_attr_read,
2380 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002381 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002382};
2383
Eric Dumazetc5141e62007-05-08 00:26:15 -07002384static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002385 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2386 REG("prev", S_IRUGO, proc_pid_attr_operations),
2387 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2388 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2389 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2390 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002391};
2392
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002393static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 void * dirent, filldir_t filldir)
2395{
2396 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002397 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Arjan van de Ven00977a52007-02-12 00:55:34 -08002400static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002402 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002403 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404};
2405
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002406static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 struct dentry *dentry, struct nameidata *nd)
2408{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002409 return proc_pident_lookup(dir, dentry,
2410 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002413static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002414 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002415 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002416 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417};
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419#endif
2420
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002421#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002422static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2423 size_t count, loff_t *ppos)
2424{
2425 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2426 struct mm_struct *mm;
2427 char buffer[PROC_NUMBUF];
2428 size_t len;
2429 int ret;
2430
2431 if (!task)
2432 return -ESRCH;
2433
2434 ret = 0;
2435 mm = get_task_mm(task);
2436 if (mm) {
2437 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2438 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2439 MMF_DUMP_FILTER_SHIFT));
2440 mmput(mm);
2441 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2442 }
2443
2444 put_task_struct(task);
2445
2446 return ret;
2447}
2448
2449static ssize_t proc_coredump_filter_write(struct file *file,
2450 const char __user *buf,
2451 size_t count,
2452 loff_t *ppos)
2453{
2454 struct task_struct *task;
2455 struct mm_struct *mm;
2456 char buffer[PROC_NUMBUF], *end;
2457 unsigned int val;
2458 int ret;
2459 int i;
2460 unsigned long mask;
2461
2462 ret = -EFAULT;
2463 memset(buffer, 0, sizeof(buffer));
2464 if (count > sizeof(buffer) - 1)
2465 count = sizeof(buffer) - 1;
2466 if (copy_from_user(buffer, buf, count))
2467 goto out_no_task;
2468
2469 ret = -EINVAL;
2470 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2471 if (*end == '\n')
2472 end++;
2473 if (end - buffer == 0)
2474 goto out_no_task;
2475
2476 ret = -ESRCH;
2477 task = get_proc_task(file->f_dentry->d_inode);
2478 if (!task)
2479 goto out_no_task;
2480
2481 ret = end - buffer;
2482 mm = get_task_mm(task);
2483 if (!mm)
2484 goto out_no_mm;
2485
2486 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2487 if (val & mask)
2488 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2489 else
2490 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2491 }
2492
2493 mmput(mm);
2494 out_no_mm:
2495 put_task_struct(task);
2496 out_no_task:
2497 return ret;
2498}
2499
2500static const struct file_operations proc_coredump_filter_operations = {
2501 .read = proc_coredump_filter_read,
2502 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002503 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002504};
2505#endif
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507/*
2508 * /proc/self:
2509 */
2510static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2511 int buflen)
2512{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002513 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002514 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002515 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002516 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002517 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002518 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return vfs_readlink(dentry,buffer,buflen,tmp);
2520}
2521
Al Viro008b1502005-08-20 00:17:39 +01002522static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002524 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002525 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002526 char *name = ERR_PTR(-ENOENT);
2527 if (tgid) {
2528 name = __getname();
2529 if (!name)
2530 name = ERR_PTR(-ENOMEM);
2531 else
2532 sprintf(name, "%d", tgid);
2533 }
2534 nd_set_link(nd, name);
2535 return NULL;
2536}
2537
2538static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2539 void *cookie)
2540{
2541 char *s = nd_get_link(nd);
2542 if (!IS_ERR(s))
2543 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002546static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 .readlink = proc_self_readlink,
2548 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002549 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550};
2551
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002552/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002553 * proc base
2554 *
2555 * These are the directory entries in the root directory of /proc
2556 * that properly belong to the /proc filesystem, as they describe
2557 * describe something that is process related.
2558 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002559static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002560 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002561 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002562};
2563
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002564static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002565 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002566{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002567 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002568 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002569 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002570 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002571
2572 /* Allocate the inode */
2573 error = ERR_PTR(-ENOMEM);
2574 inode = new_inode(dir->i_sb);
2575 if (!inode)
2576 goto out;
2577
2578 /* Initialize the inode */
2579 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002580 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002581 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002582
2583 /*
2584 * grab the reference to the task.
2585 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002586 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002587 if (!ei->pid)
2588 goto out_iput;
2589
Eric W. Biederman801199c2006-10-02 02:18:48 -07002590 inode->i_mode = p->mode;
2591 if (S_ISDIR(inode->i_mode))
2592 inode->i_nlink = 2;
2593 if (S_ISLNK(inode->i_mode))
2594 inode->i_size = 64;
2595 if (p->iop)
2596 inode->i_op = p->iop;
2597 if (p->fop)
2598 inode->i_fop = p->fop;
2599 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002600 d_add(dentry, inode);
2601 error = NULL;
2602out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002603 return error;
2604out_iput:
2605 iput(inode);
2606 goto out;
2607}
2608
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002609static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2610{
2611 struct dentry *error;
2612 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002613 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002614
2615 error = ERR_PTR(-ENOENT);
2616
2617 if (!task)
2618 goto out_no_task;
2619
2620 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002621 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2622 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002623 if (p->len != dentry->d_name.len)
2624 continue;
2625 if (!memcmp(dentry->d_name.name, p->name, p->len))
2626 break;
2627 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002628 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002629 goto out;
2630
2631 error = proc_base_instantiate(dir, dentry, task, p);
2632
2633out:
2634 put_task_struct(task);
2635out_no_task:
2636 return error;
2637}
2638
Eric Dumazetc5141e62007-05-08 00:26:15 -07002639static int proc_base_fill_cache(struct file *filp, void *dirent,
2640 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002641{
2642 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2643 proc_base_instantiate, task, p);
2644}
2645
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002646#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002647static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002648{
Andrea Righi940389b2008-07-28 00:48:12 +02002649 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002650 unsigned long flags;
Vasiliy Kulikov8cd3f192011-07-26 16:08:38 -07002651 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002652
Vasiliy Kulikov8cd3f192011-07-26 16:08:38 -07002653 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2654 if (result)
2655 return result;
2656
2657 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2658 result = -EACCES;
2659 goto out_unlock;
2660 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002661
Andrea Righi59954772008-07-27 17:29:15 +02002662 if (whole && lock_task_sighand(task, &flags)) {
2663 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002664
Andrea Righi59954772008-07-27 17:29:15 +02002665 task_io_accounting_add(&acct, &task->signal->ioac);
2666 while_each_thread(task, t)
2667 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002668
Andrea Righi59954772008-07-27 17:29:15 +02002669 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002670 }
Vasiliy Kulikov8cd3f192011-07-26 16:08:38 -07002671 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002672 "rchar: %llu\n"
2673 "wchar: %llu\n"
2674 "syscr: %llu\n"
2675 "syscw: %llu\n"
2676 "read_bytes: %llu\n"
2677 "write_bytes: %llu\n"
2678 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002679 (unsigned long long)acct.rchar,
2680 (unsigned long long)acct.wchar,
2681 (unsigned long long)acct.syscr,
2682 (unsigned long long)acct.syscw,
2683 (unsigned long long)acct.read_bytes,
2684 (unsigned long long)acct.write_bytes,
2685 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov8cd3f192011-07-26 16:08:38 -07002686out_unlock:
2687 mutex_unlock(&task->signal->cred_guard_mutex);
2688 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002689}
Andrea Righi297c5d92008-07-25 01:48:49 -07002690
2691static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2692{
2693 return do_io_accounting(task, buffer, 0);
2694}
2695
2696static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2697{
2698 return do_io_accounting(task, buffer, 1);
2699}
2700#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002701
Kees Cook47830722008-10-06 03:11:58 +04002702static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2703 struct pid *pid, struct task_struct *task)
2704{
Al Viroa9712bc2011-03-23 15:52:50 -04002705 int err = lock_trace(task);
2706 if (!err) {
2707 seq_printf(m, "%08x\n", task->personality);
2708 unlock_trace(task);
2709 }
2710 return err;
Kees Cook47830722008-10-06 03:11:58 +04002711}
2712
Eric W. Biederman801199c2006-10-02 02:18:48 -07002713/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002714 * Thread groups
2715 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002716static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002717static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002718
Eric Dumazetc5141e62007-05-08 00:26:15 -07002719static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002720 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2721 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2722 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002723 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002724#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002725 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002726#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002727 REG("environ", S_IRUSR, proc_environ_operations),
2728 INF("auxv", S_IRUSR, proc_pid_auxv),
2729 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002730 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002731 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002732#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002733 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002734#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002735#ifdef CONFIG_SCHED_AUTOGROUP
2736 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2737#endif
john stultz4614a696b2009-12-14 18:00:05 -08002738 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002739#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002740 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002741#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002742 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2743 ONE("stat", S_IRUGO, proc_tgid_stat),
2744 ONE("statm", S_IRUGO, proc_pid_statm),
2745 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002746#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002747 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002748#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002749 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2750 LNK("cwd", proc_cwd_link),
2751 LNK("root", proc_root_link),
2752 LNK("exe", proc_exe_link),
2753 REG("mounts", S_IRUGO, proc_mounts_operations),
2754 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2755 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002756#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002757 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2758 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002759 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002760#endif
2761#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002762 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002763#endif
2764#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002765 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002766#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002767#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002768 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002769#endif
2770#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002771 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002772#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002773#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002774 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002775#endif
Paul Menage8793d852007-10-18 23:39:39 -07002776#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002777 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002778#endif
Paul Menagea4243162007-10-18 23:39:35 -07002779#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002780 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002781#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002782 INF("oom_score", S_IRUGO, proc_oom_score),
2783 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002784 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002785#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002786 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2787 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002788#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002789#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002790 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002791#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002792#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002793 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002794#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002795#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002796 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002797#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002798#ifdef CONFIG_HARDWALL
2799 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2800#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002801};
2802
2803static int proc_tgid_base_readdir(struct file * filp,
2804 void * dirent, filldir_t filldir)
2805{
2806 return proc_pident_readdir(filp,dirent,filldir,
2807 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2808}
2809
Arjan van de Ven00977a52007-02-12 00:55:34 -08002810static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002811 .read = generic_read_dir,
2812 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002813 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002814};
2815
2816static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002817 return proc_pident_lookup(dir, dentry,
2818 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002819}
2820
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002821static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002822 .lookup = proc_tgid_base_lookup,
2823 .getattr = pid_getattr,
2824 .setattr = proc_setattr,
2825};
2826
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002827static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002829 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002830 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002831 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Eric W. Biederman48e64842006-06-26 00:25:48 -07002833 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002834 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2835 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002836 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002837 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002838 d_drop(dentry);
2839 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Eric W. Biederman48e64842006-06-26 00:25:48 -07002842 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002843 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2844 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002845 if (!leader)
2846 goto out;
2847
2848 name.name = "task";
2849 name.len = strlen(name.name);
2850 dir = d_hash_and_lookup(leader, &name);
2851 if (!dir)
2852 goto out_put_leader;
2853
2854 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002855 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002856 dentry = d_hash_and_lookup(dir, &name);
2857 if (dentry) {
2858 shrink_dcache_parent(dentry);
2859 d_drop(dentry);
2860 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002862
2863 dput(dir);
2864out_put_leader:
2865 dput(leader);
2866out:
2867 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
Randy Dunlap0895e912007-10-21 21:00:10 -07002870/**
2871 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2872 * @task: task that should be flushed.
2873 *
2874 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002875 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002876 * in. This call is supposed to do all of this job.
2877 *
2878 * Looks in the dcache for
2879 * /proc/@pid
2880 * /proc/@tgid/task/@pid
2881 * if either directory is present flushes it and all of it'ts children
2882 * from the dcache.
2883 *
2884 * It is safe and reasonable to cache /proc entries for a task until
2885 * that task exits. After that they just clog up the dcache with
2886 * useless entries, possibly causing useful dcache entries to be
2887 * flushed instead. This routine is proved to flush those useless
2888 * dcache entries at process exit time.
2889 *
2890 * NOTE: This routine is just an optimization so it does not guarantee
2891 * that no dcache entries will exist at process exit time it
2892 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002893 */
2894
2895void proc_flush_task(struct task_struct *task)
2896{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002897 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002898 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002899 struct upid *upid;
2900
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002901 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002902 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002903
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002904 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002905 upid = &pid->numbers[i];
2906 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002907 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002908 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002909
2910 upid = &pid->numbers[pid->level];
2911 if (upid->nr == 1)
2912 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002913}
2914
Adrian Bunk9711ef92006-12-06 20:38:31 -08002915static struct dentry *proc_pid_instantiate(struct inode *dir,
2916 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002917 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002918{
2919 struct dentry *error = ERR_PTR(-ENOENT);
2920 struct inode *inode;
2921
Eric W. Biederman61a28782006-10-02 02:18:49 -07002922 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002923 if (!inode)
2924 goto out;
2925
2926 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2927 inode->i_op = &proc_tgid_base_inode_operations;
2928 inode->i_fop = &proc_tgid_base_operations;
2929 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002930
2931 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2932 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002933
Nick Pigginfb045ad2011-01-07 17:49:55 +11002934 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002935
2936 d_add(dentry, inode);
2937 /* Close the race of the process dying before we return the dentry */
2938 if (pid_revalidate(dentry, NULL))
2939 error = NULL;
2940out:
2941 return error;
2942}
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2945{
Dan Carpenter73d36462010-05-26 14:43:25 -07002946 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002949 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Eric W. Biederman801199c2006-10-02 02:18:48 -07002951 result = proc_base_lookup(dir, dentry);
2952 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2953 goto out;
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 tgid = name_to_int(dentry);
2956 if (tgid == ~0U)
2957 goto out;
2958
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002959 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002960 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002961 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (task)
2963 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002964 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (!task)
2966 goto out;
2967
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002968 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002969 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002971 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002975 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002976 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002978struct tgid_iter {
2979 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002980 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002981};
2982static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2983{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002984 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002986 if (iter.task)
2987 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002988 rcu_read_lock();
2989retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002990 iter.task = NULL;
2991 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002992 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002993 iter.tgid = pid_nr_ns(pid, ns);
2994 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002995 /* What we to know is if the pid we have find is the
2996 * pid of a thread_group_leader. Testing for task
2997 * being a thread_group_leader is the obvious thing
2998 * todo but there is a window when it fails, due to
2999 * the pid transfer logic in de_thread.
3000 *
3001 * So we perform the straight forward test of seeing
3002 * if the pid we have found is the pid of a thread
3003 * group leader, and don't worry if the task we have
3004 * found doesn't happen to be a thread group leader.
3005 * As we don't care in the case of readdir.
3006 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003007 if (!iter.task || !has_group_leader_pid(iter.task)) {
3008 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003009 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003010 }
3011 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003013 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003014 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003017#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Eric W. Biederman61a28782006-10-02 02:18:49 -07003019static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003020 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003021{
3022 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003023 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003024 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003025 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003026}
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028/* for the /proc/ directory itself, after non-process stuff has been done */
3029int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3030{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003031 unsigned int nr;
3032 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003033 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003034 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003036 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3037 goto out_no_task;
3038 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3039
3040 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003041 if (!reaper)
3042 goto out_no_task;
3043
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003044 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003045 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003046 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003047 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003050 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003051 iter.task = NULL;
3052 iter.tgid = filp->f_pos - TGID_OFFSET;
3053 for (iter = next_tgid(ns, iter);
3054 iter.task;
3055 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3056 filp->f_pos = iter.tgid + TGID_OFFSET;
3057 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3058 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003059 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003062 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3063out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003064 put_task_struct(reaper);
3065out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return 0;
3067}
3068
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003069/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003070 * Tasks
3071 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003072static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003073 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003074 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003075 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003076 REG("environ", S_IRUSR, proc_environ_operations),
3077 INF("auxv", S_IRUSR, proc_pid_auxv),
3078 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003079 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003080 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003081#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003082 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003083#endif
john stultz4614a696b2009-12-14 18:00:05 -08003084 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003085#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003086 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003087#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003088 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3089 ONE("stat", S_IRUGO, proc_tid_stat),
3090 ONE("statm", S_IRUGO, proc_pid_statm),
3091 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003092#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003093 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003094#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003095 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3096 LNK("cwd", proc_cwd_link),
3097 LNK("root", proc_root_link),
3098 LNK("exe", proc_exe_link),
3099 REG("mounts", S_IRUGO, proc_mounts_operations),
3100 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003101#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003102 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3103 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003104 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003105#endif
3106#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003107 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003108#endif
3109#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003110 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003111#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003112#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003113 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003114#endif
3115#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003116 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003117#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003118#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003119 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003120#endif
Paul Menage8793d852007-10-18 23:39:39 -07003121#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003122 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003123#endif
Paul Menagea4243162007-10-18 23:39:35 -07003124#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003125 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003126#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003127 INF("oom_score", S_IRUGO, proc_oom_score),
3128 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003129 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003130#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003131 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003132 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003133#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003134#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003135 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003136#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003137#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003138 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003139#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003140#ifdef CONFIG_HARDWALL
3141 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3142#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003143};
3144
3145static int proc_tid_base_readdir(struct file * filp,
3146 void * dirent, filldir_t filldir)
3147{
3148 return proc_pident_readdir(filp,dirent,filldir,
3149 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3150}
3151
3152static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003153 return proc_pident_lookup(dir, dentry,
3154 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003155}
3156
Arjan van de Ven00977a52007-02-12 00:55:34 -08003157static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003158 .read = generic_read_dir,
3159 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003160 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003161};
3162
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003163static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003164 .lookup = proc_tid_base_lookup,
3165 .getattr = pid_getattr,
3166 .setattr = proc_setattr,
3167};
3168
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003169static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003170 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003171{
3172 struct dentry *error = ERR_PTR(-ENOENT);
3173 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003174 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003175
3176 if (!inode)
3177 goto out;
3178 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3179 inode->i_op = &proc_tid_base_inode_operations;
3180 inode->i_fop = &proc_tid_base_operations;
3181 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003182
3183 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3184 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003185
Nick Pigginfb045ad2011-01-07 17:49:55 +11003186 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003187
3188 d_add(dentry, inode);
3189 /* Close the race of the process dying before we return the dentry */
3190 if (pid_revalidate(dentry, NULL))
3191 error = NULL;
3192out:
3193 return error;
3194}
3195
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003196static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3197{
3198 struct dentry *result = ERR_PTR(-ENOENT);
3199 struct task_struct *task;
3200 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003201 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003202 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003203
3204 if (!leader)
3205 goto out_no_task;
3206
3207 tid = name_to_int(dentry);
3208 if (tid == ~0U)
3209 goto out;
3210
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003211 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003212 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003213 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003214 if (task)
3215 get_task_struct(task);
3216 rcu_read_unlock();
3217 if (!task)
3218 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003219 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003220 goto out_drop_task;
3221
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003222 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003223out_drop_task:
3224 put_task_struct(task);
3225out:
3226 put_task_struct(leader);
3227out_no_task:
3228 return result;
3229}
3230
3231/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003232 * Find the first tid of a thread group to return to user space.
3233 *
3234 * Usually this is just the thread group leader, but if the users
3235 * buffer was too small or there was a seek into the middle of the
3236 * directory we have more work todo.
3237 *
3238 * In the case of a short read we start with find_task_by_pid.
3239 *
3240 * In the case of a seek we start with the leader and walk nr
3241 * threads past it.
3242 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003243static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003244 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003245{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003246 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003247
Eric W. Biedermancc288732006-06-26 00:26:01 -07003248 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003249 /* Attempt to start with the pid of a thread */
3250 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003251 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003252 if (pos && (pos->group_leader == leader))
3253 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003254 }
3255
3256 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003257 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003258 if (nr && nr >= get_nr_threads(leader))
3259 goto out;
3260
3261 /* If we haven't found our starting place yet start
3262 * with the leader and walk nr threads forward.
3263 */
3264 for (pos = leader; nr > 0; --nr) {
3265 pos = next_thread(pos);
3266 if (pos == leader) {
3267 pos = NULL;
3268 goto out;
3269 }
3270 }
3271found:
3272 get_task_struct(pos);
3273out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003274 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003275 return pos;
3276}
3277
3278/*
3279 * Find the next thread in the thread list.
3280 * Return NULL if there is an error or no next thread.
3281 *
3282 * The reference to the input task_struct is released.
3283 */
3284static struct task_struct *next_tid(struct task_struct *start)
3285{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003286 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003287 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003288 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003289 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003290 if (thread_group_leader(pos))
3291 pos = NULL;
3292 else
3293 get_task_struct(pos);
3294 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003295 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003296 put_task_struct(start);
3297 return pos;
3298}
3299
Eric W. Biederman61a28782006-10-02 02:18:49 -07003300static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3301 struct task_struct *task, int tid)
3302{
3303 char name[PROC_NUMBUF];
3304 int len = snprintf(name, sizeof(name), "%d", tid);
3305 return proc_fill_cache(filp, dirent, filldir, name, len,
3306 proc_task_instantiate, task, NULL);
3307}
3308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309/* for the /proc/TGID/task/ directories */
3310static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3311{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003312 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003314 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003315 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 int retval = -ENOENT;
3317 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003318 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003319 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003321 task = get_proc_task(inode);
3322 if (!task)
3323 goto out_no_task;
3324 rcu_read_lock();
3325 if (pid_alive(task)) {
3326 leader = task->group_leader;
3327 get_task_struct(leader);
3328 }
3329 rcu_read_unlock();
3330 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003331 if (!leader)
3332 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 retval = 0;
3334
Linus Torvaldsee568b22009-03-17 10:02:35 -07003335 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 case 0:
3337 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003338 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003340 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 /* fall through */
3342 case 1:
3343 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003344 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003346 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 /* fall through */
3348 }
3349
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003350 /* f_version caches the tgid value that the last readdir call couldn't
3351 * return. lseek aka telldir automagically resets f_version to 0.
3352 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003353 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003354 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003355 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003356 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003357 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003358 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003359 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003360 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003361 /* returning this tgid failed, save it as the first
3362 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003363 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003364 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
3368out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003369 put_task_struct(leader);
3370out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 return retval;
3372}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003373
3374static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3375{
3376 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003377 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003378 generic_fillattr(inode, stat);
3379
Eric W. Biederman99f89552006-06-26 00:25:55 -07003380 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003381 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003382 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003383 }
3384
3385 return 0;
3386}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003387
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003388static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003389 .lookup = proc_task_lookup,
3390 .getattr = proc_task_getattr,
3391 .setattr = proc_setattr,
3392};
3393
Arjan van de Ven00977a52007-02-12 00:55:34 -08003394static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003395 .read = generic_read_dir,
3396 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003397 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003398};