blob: cbe454e94af844e72760b2f5fdbfd99f4c5b9d32 [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>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080084#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040085#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090086#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080087#include <linux/flex_array.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040088#ifdef CONFIG_HARDWALL
89#include <asm/hardwall.h>
90#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080091#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040093#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070095/* NOTE:
96 * Implementing inode permission operations in /proc is almost
97 * certainly an error. Permission checks need to happen during
98 * each system call not at open time. The reason is that most of
99 * what we wish to check for permissions in /proc varies at runtime.
100 *
101 * The classic example of a problem is opening file descriptors
102 * in /proc for a task before it execs a suid executable.
103 */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700107 int len;
Al Virod161a132011-07-24 03:36:29 -0400108 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800109 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800110 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700111 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Eric W. Biederman61a28782006-10-02 02:18:49 -0700114#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700116 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 .mode = MODE, \
118 .iop = IOP, \
119 .fop = FOP, \
120 .op = OP, \
121}
122
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300123#define DIR(NAME, MODE, iops, fops) \
124 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
125#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700126 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700127 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300128 { .proc_get_link = get_link } )
129#define REG(NAME, MODE, fops) \
130 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
131#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700132 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700133 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300134 { .proc_read = read } )
135#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800136 NOD(NAME, (S_IFREG|(MODE)), \
137 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300138 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Vegard Nossumaed54172008-06-05 22:46:53 -0700140/*
141 * Count the number of hardlinks for the pid_entry table, excluding the .
142 * and .. links.
143 */
144static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145 unsigned int n)
146{
147 unsigned int i;
148 unsigned int count;
149
150 count = 0;
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
153 ++count;
154 }
155
156 return count;
157}
158
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200159static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000161 int result = -ENOENT;
162
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700163 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200164 if (task->fs) {
165 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000166 result = 0;
167 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000169 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170}
171
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800172static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700173{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800174 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176
177 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200178 task_lock(task);
179 if (task->fs) {
180 get_fs_pwd(task->fs, path);
181 result = 0;
182 }
183 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700184 put_task_struct(task);
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return result;
187}
188
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800189static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800191 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700193
194 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200195 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700196 put_task_struct(task);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return result;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int proc_pid_cmdline(struct task_struct *task, char * buffer)
202{
203 int res = 0;
204 unsigned int len;
205 struct mm_struct *mm = get_task_mm(task);
206 if (!mm)
207 goto out;
208 if (!mm->arg_end)
209 goto out_mm; /* Shh! No looking before we're done */
210
211 len = mm->arg_end - mm->arg_start;
212
213 if (len > PAGE_SIZE)
214 len = PAGE_SIZE;
215
216 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
217
218 // If the nul at the end of args has been overwritten, then
219 // assume application is using setproctitle(3).
220 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
221 len = strnlen(buffer, res);
222 if (len < res) {
223 res = len;
224 } else {
225 len = mm->env_end - mm->env_start;
226 if (len > PAGE_SIZE - res)
227 len = PAGE_SIZE - res;
228 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
229 res = strnlen(buffer, res);
230 }
231 }
232out_mm:
233 mmput(mm);
234out:
235 return res;
236}
237
238static int proc_pid_auxv(struct task_struct *task, char *buffer)
239{
Cong Wange7dcd992012-05-31 16:26:17 -0700240 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500241 int res = PTR_ERR(mm);
242 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300244 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300246 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 res = nwords * sizeof(mm->saved_auxv[0]);
248 if (res > PAGE_SIZE)
249 res = PAGE_SIZE;
250 memcpy(buffer, mm->saved_auxv, res);
251 mmput(mm);
252 }
253 return res;
254}
255
256
257#ifdef CONFIG_KALLSYMS
258/*
259 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
260 * Returns the resolved symbol. If that fails, simply return the address.
261 */
262static int proc_pid_wchan(struct task_struct *task, char *buffer)
263{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700264 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700265 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 wchan = get_wchan(task);
268
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700269 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600270 if (!ptrace_may_access(task, PTRACE_MODE_READ))
271 return 0;
272 else
273 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700274 else
275 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277#endif /* CONFIG_KALLSYMS */
278
Al Viroa9712bc2011-03-23 15:52:50 -0400279static int lock_trace(struct task_struct *task)
280{
281 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
282 if (err)
283 return err;
284 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
285 mutex_unlock(&task->signal->cred_guard_mutex);
286 return -EPERM;
287 }
288 return 0;
289}
290
291static void unlock_trace(struct task_struct *task)
292{
293 mutex_unlock(&task->signal->cred_guard_mutex);
294}
295
Ken Chen2ec220e2008-11-10 11:26:08 +0300296#ifdef CONFIG_STACKTRACE
297
298#define MAX_STACK_TRACE_DEPTH 64
299
300static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
301 struct pid *pid, struct task_struct *task)
302{
303 struct stack_trace trace;
304 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400305 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300306 int i;
307
308 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
309 if (!entries)
310 return -ENOMEM;
311
312 trace.nr_entries = 0;
313 trace.max_entries = MAX_STACK_TRACE_DEPTH;
314 trace.entries = entries;
315 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300316
Al Viroa9712bc2011-03-23 15:52:50 -0400317 err = lock_trace(task);
318 if (!err) {
319 save_stack_trace_tsk(task, &trace);
320
321 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700322 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400323 (void *)entries[i], (void *)entries[i]);
324 }
325 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300326 }
327 kfree(entries);
328
Al Viroa9712bc2011-03-23 15:52:50 -0400329 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300330}
331#endif
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#ifdef CONFIG_SCHEDSTATS
334/*
335 * Provides /proc/PID/schedstat
336 */
337static int proc_pid_schedstat(struct task_struct *task, char *buffer)
338{
Balbir Singh172ba842007-07-09 18:52:00 +0200339 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100340 (unsigned long long)task->se.sum_exec_runtime,
341 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200342 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344#endif
345
Arjan van de Ven97455122008-01-25 21:08:34 +0100346#ifdef CONFIG_LATENCYTOP
347static int lstats_show_proc(struct seq_file *m, void *v)
348{
349 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800350 struct inode *inode = m->private;
351 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100352
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800353 if (!task)
354 return -ESRCH;
355 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100356 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800357 struct latency_record *lr = &task->latency_record[i];
358 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100359 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800360 seq_printf(m, "%i %li %li",
361 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100362 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800363 unsigned long bt = lr->backtrace[q];
364 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100365 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800366 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100367 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800368 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100369 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800370 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100371 }
372
373 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800374 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100375 return 0;
376}
377
378static int lstats_open(struct inode *inode, struct file *file)
379{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800380 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800381}
382
Arjan van de Ven97455122008-01-25 21:08:34 +0100383static ssize_t lstats_write(struct file *file, const char __user *buf,
384 size_t count, loff_t *offs)
385{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800386 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100387
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800388 if (!task)
389 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100390 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800391 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100392
393 return count;
394}
395
396static const struct file_operations proc_lstats_operations = {
397 .open = lstats_open,
398 .read = seq_read,
399 .write = lstats_write,
400 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800401 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100402};
403
404#endif
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static int proc_oom_score(struct task_struct *task, char *buffer)
407{
David Rientjesa7f638f2012-05-29 15:06:47 -0700408 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200409 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700411 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200412 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700413 points = oom_badness(task, NULL, NULL, totalpages) *
414 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700415 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return sprintf(buffer, "%lu\n", points);
417}
418
Neil Hormand85f50d2007-10-18 23:40:37 -0700419struct limit_names {
420 char *name;
421 char *unit;
422};
423
424static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700425 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700426 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
427 [RLIMIT_DATA] = {"Max data size", "bytes"},
428 [RLIMIT_STACK] = {"Max stack size", "bytes"},
429 [RLIMIT_CORE] = {"Max core file size", "bytes"},
430 [RLIMIT_RSS] = {"Max resident set", "bytes"},
431 [RLIMIT_NPROC] = {"Max processes", "processes"},
432 [RLIMIT_NOFILE] = {"Max open files", "files"},
433 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
434 [RLIMIT_AS] = {"Max address space", "bytes"},
435 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
436 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
437 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
438 [RLIMIT_NICE] = {"Max nice priority", NULL},
439 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800440 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700441};
442
443/* Display limits for a process */
444static int proc_pid_limits(struct task_struct *task, char *buffer)
445{
446 unsigned int i;
447 int count = 0;
448 unsigned long flags;
449 char *bufptr = buffer;
450
451 struct rlimit rlim[RLIM_NLIMITS];
452
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400453 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700454 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700455 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
456 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700457
458 /*
459 * print the file header
460 */
461 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
462 "Limit", "Soft Limit", "Hard Limit", "Units");
463
464 for (i = 0; i < RLIM_NLIMITS; i++) {
465 if (rlim[i].rlim_cur == RLIM_INFINITY)
466 count += sprintf(&bufptr[count], "%-25s %-20s ",
467 lnames[i].name, "unlimited");
468 else
469 count += sprintf(&bufptr[count], "%-25s %-20lu ",
470 lnames[i].name, rlim[i].rlim_cur);
471
472 if (rlim[i].rlim_max == RLIM_INFINITY)
473 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
474 else
475 count += sprintf(&bufptr[count], "%-20lu ",
476 rlim[i].rlim_max);
477
478 if (lnames[i].unit)
479 count += sprintf(&bufptr[count], "%-10s\n",
480 lnames[i].unit);
481 else
482 count += sprintf(&bufptr[count], "\n");
483 }
484
485 return count;
486}
487
Roland McGrathebcb6732008-07-25 19:46:00 -0700488#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
489static int proc_pid_syscall(struct task_struct *task, char *buffer)
490{
491 long nr;
492 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400493 int res = lock_trace(task);
494 if (res)
495 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700496
497 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400498 res = sprintf(buffer, "running\n");
499 else if (nr < 0)
500 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
501 else
502 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700503 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
504 nr,
505 args[0], args[1], args[2], args[3], args[4], args[5],
506 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400507 unlock_trace(task);
508 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700509}
510#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/************************************************************************/
513/* Here the fs part begins */
514/************************************************************************/
515
516/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700517static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700519 struct task_struct *task;
520 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700521 /* Allow access to a task's file descriptors if it is us or we
522 * may use ptrace attach to the process and find out that
523 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700524 */
525 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700526 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400527 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700528 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700529 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700530 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800533int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700534{
535 int error;
536 struct inode *inode = dentry->d_inode;
537
538 if (attr->ia_valid & ATTR_MODE)
539 return -EPERM;
540
541 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200542 if (error)
543 return error;
544
545 if ((attr->ia_valid & ATTR_SIZE) &&
546 attr->ia_size != i_size_read(inode)) {
547 error = vmtruncate(inode, attr->ia_size);
548 if (error)
549 return error;
550 }
551
552 setattr_copy(inode, attr);
553 mark_inode_dirty(inode);
554 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700555}
556
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800557/*
558 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
559 * or euid/egid (for hide_pid_min=2)?
560 */
561static bool has_pid_permissions(struct pid_namespace *pid,
562 struct task_struct *task,
563 int hide_pid_min)
564{
565 if (pid->hide_pid < hide_pid_min)
566 return true;
567 if (in_group_p(pid->pid_gid))
568 return true;
569 return ptrace_may_access(task, PTRACE_MODE_READ);
570}
571
572
573static int proc_pid_permission(struct inode *inode, int mask)
574{
575 struct pid_namespace *pid = inode->i_sb->s_fs_info;
576 struct task_struct *task;
577 bool has_perms;
578
579 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800580 if (!task)
581 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800582 has_perms = has_pid_permissions(pid, task, 1);
583 put_task_struct(task);
584
585 if (!has_perms) {
586 if (pid->hide_pid == 2) {
587 /*
588 * Let's make getdents(), stat(), and open()
589 * consistent with each other. If a process
590 * may not stat() a file, it shouldn't be seen
591 * in procfs at all.
592 */
593 return -ENOENT;
594 }
595
596 return -EPERM;
597 }
598 return generic_permission(inode, mask);
599}
600
601
602
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800603static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700604 .setattr = proc_setattr,
605};
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
608
609static ssize_t proc_info_read(struct file * file, char __user * buf,
610 size_t count, loff_t *ppos)
611{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800612 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 unsigned long page;
614 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700615 struct task_struct *task = get_proc_task(inode);
616
617 length = -ESRCH;
618 if (!task)
619 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (count > PROC_BLOCK_SIZE)
622 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700623
624 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700625 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 length = PROC_I(inode)->op.proc_read(task, (char*)page);
629
630 if (length >= 0)
631 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
632 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700633out:
634 put_task_struct(task);
635out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return length;
637}
638
Arjan van de Ven00977a52007-02-12 00:55:34 -0800639static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100641 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642};
643
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800644static int proc_single_show(struct seq_file *m, void *v)
645{
646 struct inode *inode = m->private;
647 struct pid_namespace *ns;
648 struct pid *pid;
649 struct task_struct *task;
650 int ret;
651
652 ns = inode->i_sb->s_fs_info;
653 pid = proc_pid(inode);
654 task = get_pid_task(pid, PIDTYPE_PID);
655 if (!task)
656 return -ESRCH;
657
658 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
659
660 put_task_struct(task);
661 return ret;
662}
663
664static int proc_single_open(struct inode *inode, struct file *filp)
665{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800666 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800667}
668
669static const struct file_operations proc_single_file_operations = {
670 .open = proc_single_open,
671 .read = seq_read,
672 .llseek = seq_lseek,
673 .release = single_release,
674};
675
Cong Wangb409e572012-05-31 16:26:17 -0700676static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Linus Torvaldse2683372012-01-17 15:21:19 -0800678 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
679 struct mm_struct *mm;
680
681 if (!task)
682 return -ESRCH;
683
Cong Wangb409e572012-05-31 16:26:17 -0700684 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800685 put_task_struct(task);
686
687 if (IS_ERR(mm))
688 return PTR_ERR(mm);
689
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100690 if (mm) {
691 /* ensure this mm_struct can't be freed */
692 atomic_inc(&mm->mm_count);
693 /* but do not pin its memory */
694 mmput(mm);
695 }
696
Linus Torvaldse2683372012-01-17 15:21:19 -0800697 file->private_data = mm;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 0;
700}
701
Cong Wangb409e572012-05-31 16:26:17 -0700702static int mem_open(struct inode *inode, struct file *file)
703{
Djalal Harounibc452b42012-07-30 14:42:28 -0700704 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
705
706 /* OK to pass negative loff_t, we can catch out-of-range */
707 file->f_mode |= FMODE_UNSIGNED_OFFSET;
708
709 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700710}
711
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100712static ssize_t mem_rw(struct file *file, char __user *buf,
713 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Linus Torvaldse2683372012-01-17 15:21:19 -0800715 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100716 unsigned long addr = *ppos;
717 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvaldse2683372012-01-17 15:21:19 -0800720 if (!mm)
721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Mel Gormane12ba742007-10-16 01:25:52 -0700723 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800725 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700727 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100728 if (!atomic_inc_not_zero(&mm->mm_users))
729 goto free;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100732 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100734 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 copied = -EFAULT;
736 break;
737 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100738
739 this_len = access_remote_vm(mm, addr, page, this_len, write);
740 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!copied)
742 copied = -EIO;
743 break;
744 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100745
746 if (!write && copy_to_user(buf, page, this_len)) {
747 copied = -EFAULT;
748 break;
749 }
750
751 buf += this_len;
752 addr += this_len;
753 copied += this_len;
754 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100756 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700757
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100758 mmput(mm);
759free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700760 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return copied;
762}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100764static ssize_t mem_read(struct file *file, char __user *buf,
765 size_t count, loff_t *ppos)
766{
767 return mem_rw(file, buf, count, ppos, 0);
768}
769
770static ssize_t mem_write(struct file *file, const char __user *buf,
771 size_t count, loff_t *ppos)
772{
773 return mem_rw(file, (char __user*)buf, count, ppos, 1);
774}
775
Matt Mackall85863e42008-02-04 22:29:04 -0800776loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 switch (orig) {
779 case 0:
780 file->f_pos = offset;
781 break;
782 case 1:
783 file->f_pos += offset;
784 break;
785 default:
786 return -EINVAL;
787 }
788 force_successful_syscall_return();
789 return file->f_pos;
790}
791
Linus Torvaldse2683372012-01-17 15:21:19 -0800792static int mem_release(struct inode *inode, struct file *file)
793{
794 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100795 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100796 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800797 return 0;
798}
799
Arjan van de Ven00977a52007-02-12 00:55:34 -0800800static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .llseek = mem_lseek,
802 .read = mem_read,
803 .write = mem_write,
804 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800805 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806};
807
Cong Wangb409e572012-05-31 16:26:17 -0700808static int environ_open(struct inode *inode, struct file *file)
809{
810 return __mem_open(inode, file, PTRACE_MODE_READ);
811}
812
James Pearson315e28c2007-10-16 23:30:17 -0700813static ssize_t environ_read(struct file *file, char __user *buf,
814 size_t count, loff_t *ppos)
815{
James Pearson315e28c2007-10-16 23:30:17 -0700816 char *page;
817 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700818 int ret = 0;
819 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700820
Cong Wangb409e572012-05-31 16:26:17 -0700821 if (!mm)
822 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700823
James Pearson315e28c2007-10-16 23:30:17 -0700824 page = (char *)__get_free_page(GFP_TEMPORARY);
825 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700826 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700827
Al Virod6f64b82011-02-15 22:26:01 -0500828 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700829 if (!atomic_inc_not_zero(&mm->mm_users))
830 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700831 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700832 size_t this_len, max_len;
833 int retval;
834
835 if (src >= (mm->env_end - mm->env_start))
836 break;
James Pearson315e28c2007-10-16 23:30:17 -0700837
838 this_len = mm->env_end - (mm->env_start + src);
839
Djalal Harounie8905ec2012-07-30 14:42:26 -0700840 max_len = min_t(size_t, PAGE_SIZE, count);
841 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700842
Cong Wangb409e572012-05-31 16:26:17 -0700843 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700844 page, this_len, 0);
845
846 if (retval <= 0) {
847 ret = retval;
848 break;
849 }
850
851 if (copy_to_user(buf, page, retval)) {
852 ret = -EFAULT;
853 break;
854 }
855
856 ret += retval;
857 src += retval;
858 buf += retval;
859 count -= retval;
860 }
861 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700862 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700863
864free:
James Pearson315e28c2007-10-16 23:30:17 -0700865 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700866 return ret;
867}
868
869static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700870 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700871 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100872 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700873 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700874};
875
David Rientjesa63d83f2010-08-09 17:19:46 -0700876static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
877 size_t count, loff_t *ppos)
878{
879 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
880 char buffer[PROC_NUMBUF];
881 int oom_score_adj = OOM_SCORE_ADJ_MIN;
882 unsigned long flags;
883 size_t len;
884
885 if (!task)
886 return -ESRCH;
887 if (lock_task_sighand(task, &flags)) {
888 oom_score_adj = task->signal->oom_score_adj;
889 unlock_task_sighand(task, &flags);
890 }
891 put_task_struct(task);
892 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
893 return simple_read_from_buffer(buf, count, ppos, buffer, len);
894}
895
896static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
897 size_t count, loff_t *ppos)
898{
899 struct task_struct *task;
900 char buffer[PROC_NUMBUF];
901 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700902 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -0700903 int err;
904
905 memset(buffer, 0, sizeof(buffer));
906 if (count > sizeof(buffer) - 1)
907 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700908 if (copy_from_user(buffer, buf, count)) {
909 err = -EFAULT;
910 goto out;
911 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700912
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700913 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700914 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700915 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -0700916 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -0700917 oom_score_adj > OOM_SCORE_ADJ_MAX) {
918 err = -EINVAL;
919 goto out;
920 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700921
922 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -0700923 if (!task) {
924 err = -ESRCH;
925 goto out;
926 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700927
Ying Han3d5992d2010-10-26 14:21:23 -0700928 task_lock(task);
929 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -0700930 err = -EINVAL;
931 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -0700932 }
David Rientjesd19d5472010-10-26 14:21:26 -0700933
934 if (!lock_task_sighand(task, &flags)) {
935 err = -ESRCH;
936 goto err_task_lock;
937 }
938
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -0800939 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -0700940 !capable(CAP_SYS_RESOURCE)) {
941 err = -EACCES;
942 goto err_sighand;
943 }
944
David Rientjesa63d83f2010-08-09 17:19:46 -0700945 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -0800946 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
947 task->signal->oom_score_adj_min = oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800948 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -0700949
David Rientjes723548b2010-10-26 14:21:25 -0700950err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -0700951 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -0700952err_task_lock:
953 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -0700954 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -0700955out:
956 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -0700957}
958
959static const struct file_operations proc_oom_score_adj_operations = {
960 .read = oom_score_adj_read,
961 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200962 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -0700963};
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965#ifdef CONFIG_AUDITSYSCALL
966#define TMPBUFLEN 21
967static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
968 size_t count, loff_t *ppos)
969{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800970 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700971 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ssize_t length;
973 char tmpbuf[TMPBUFLEN];
974
Eric W. Biederman99f89552006-06-26 00:25:55 -0700975 if (!task)
976 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700978 from_kuid(file->f_cred->user_ns,
979 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700980 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
982}
983
984static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
985 size_t count, loff_t *ppos)
986{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800987 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 char *page, *tmp;
989 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700991 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Paul E. McKenney7dc52152010-02-22 17:04:52 -0800993 rcu_read_lock();
994 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
995 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -0800997 }
998 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Al Viroe0182902006-05-18 08:28:02 -04001000 if (count >= PAGE_SIZE)
1001 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (*ppos != 0) {
1004 /* No partial writes. */
1005 return -EINVAL;
1006 }
Mel Gormane12ba742007-10-16 01:25:52 -07001007 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!page)
1009 return -ENOMEM;
1010 length = -EFAULT;
1011 if (copy_from_user(page, buf, count))
1012 goto out_free_page;
1013
Al Viroe0182902006-05-18 08:28:02 -04001014 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 loginuid = simple_strtoul(page, &tmp, 10);
1016 if (tmp == page) {
1017 length = -EINVAL;
1018 goto out_free_page;
1019
1020 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001021 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1022 if (!uid_valid(kloginuid)) {
1023 length = -EINVAL;
1024 goto out_free_page;
1025 }
1026
1027 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (likely(length == 0))
1029 length = count;
1030
1031out_free_page:
1032 free_page((unsigned long) page);
1033 return length;
1034}
1035
Arjan van de Ven00977a52007-02-12 00:55:34 -08001036static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 .read = proc_loginuid_read,
1038 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001039 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040};
Eric Paris1e0bd752008-03-13 08:15:31 -04001041
1042static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1043 size_t count, loff_t *ppos)
1044{
1045 struct inode * inode = file->f_path.dentry->d_inode;
1046 struct task_struct *task = get_proc_task(inode);
1047 ssize_t length;
1048 char tmpbuf[TMPBUFLEN];
1049
1050 if (!task)
1051 return -ESRCH;
1052 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1053 audit_get_sessionid(task));
1054 put_task_struct(task);
1055 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1056}
1057
1058static const struct file_operations proc_sessionid_operations = {
1059 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001060 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001061};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#endif
1063
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001064#ifdef CONFIG_FAULT_INJECTION
1065static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1066 size_t count, loff_t *ppos)
1067{
1068 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1069 char buffer[PROC_NUMBUF];
1070 size_t len;
1071 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001072
1073 if (!task)
1074 return -ESRCH;
1075 make_it_fail = task->make_it_fail;
1076 put_task_struct(task);
1077
1078 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001079
1080 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001081}
1082
1083static ssize_t proc_fault_inject_write(struct file * file,
1084 const char __user * buf, size_t count, loff_t *ppos)
1085{
1086 struct task_struct *task;
1087 char buffer[PROC_NUMBUF], *end;
1088 int make_it_fail;
1089
1090 if (!capable(CAP_SYS_RESOURCE))
1091 return -EPERM;
1092 memset(buffer, 0, sizeof(buffer));
1093 if (count > sizeof(buffer) - 1)
1094 count = sizeof(buffer) - 1;
1095 if (copy_from_user(buffer, buf, count))
1096 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001097 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1098 if (*end)
1099 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001100 task = get_proc_task(file->f_dentry->d_inode);
1101 if (!task)
1102 return -ESRCH;
1103 task->make_it_fail = make_it_fail;
1104 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001105
1106 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001107}
1108
Arjan van de Ven00977a52007-02-12 00:55:34 -08001109static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001110 .read = proc_fault_inject_read,
1111 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001112 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001113};
1114#endif
1115
Arjan van de Ven97455122008-01-25 21:08:34 +01001116
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001117#ifdef CONFIG_SCHED_DEBUG
1118/*
1119 * Print out various scheduling related per-task fields:
1120 */
1121static int sched_show(struct seq_file *m, void *v)
1122{
1123 struct inode *inode = m->private;
1124 struct task_struct *p;
1125
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001126 p = get_proc_task(inode);
1127 if (!p)
1128 return -ESRCH;
1129 proc_sched_show_task(p, m);
1130
1131 put_task_struct(p);
1132
1133 return 0;
1134}
1135
1136static ssize_t
1137sched_write(struct file *file, const char __user *buf,
1138 size_t count, loff_t *offset)
1139{
1140 struct inode *inode = file->f_path.dentry->d_inode;
1141 struct task_struct *p;
1142
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001143 p = get_proc_task(inode);
1144 if (!p)
1145 return -ESRCH;
1146 proc_sched_set_task(p);
1147
1148 put_task_struct(p);
1149
1150 return count;
1151}
1152
1153static int sched_open(struct inode *inode, struct file *filp)
1154{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001155 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001156}
1157
1158static const struct file_operations proc_pid_sched_operations = {
1159 .open = sched_open,
1160 .read = seq_read,
1161 .write = sched_write,
1162 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001163 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001164};
1165
1166#endif
1167
Mike Galbraith5091faa2010-11-30 14:18:03 +01001168#ifdef CONFIG_SCHED_AUTOGROUP
1169/*
1170 * Print out autogroup related information:
1171 */
1172static int sched_autogroup_show(struct seq_file *m, void *v)
1173{
1174 struct inode *inode = m->private;
1175 struct task_struct *p;
1176
1177 p = get_proc_task(inode);
1178 if (!p)
1179 return -ESRCH;
1180 proc_sched_autogroup_show_task(p, m);
1181
1182 put_task_struct(p);
1183
1184 return 0;
1185}
1186
1187static ssize_t
1188sched_autogroup_write(struct file *file, const char __user *buf,
1189 size_t count, loff_t *offset)
1190{
1191 struct inode *inode = file->f_path.dentry->d_inode;
1192 struct task_struct *p;
1193 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001194 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001195 int err;
1196
1197 memset(buffer, 0, sizeof(buffer));
1198 if (count > sizeof(buffer) - 1)
1199 count = sizeof(buffer) - 1;
1200 if (copy_from_user(buffer, buf, count))
1201 return -EFAULT;
1202
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001203 err = kstrtoint(strstrip(buffer), 0, &nice);
1204 if (err < 0)
1205 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001206
1207 p = get_proc_task(inode);
1208 if (!p)
1209 return -ESRCH;
1210
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001211 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001212 if (err)
1213 count = err;
1214
1215 put_task_struct(p);
1216
1217 return count;
1218}
1219
1220static int sched_autogroup_open(struct inode *inode, struct file *filp)
1221{
1222 int ret;
1223
1224 ret = single_open(filp, sched_autogroup_show, NULL);
1225 if (!ret) {
1226 struct seq_file *m = filp->private_data;
1227
1228 m->private = inode;
1229 }
1230 return ret;
1231}
1232
1233static const struct file_operations proc_pid_sched_autogroup_operations = {
1234 .open = sched_autogroup_open,
1235 .read = seq_read,
1236 .write = sched_autogroup_write,
1237 .llseek = seq_lseek,
1238 .release = single_release,
1239};
1240
1241#endif /* CONFIG_SCHED_AUTOGROUP */
1242
john stultz4614a696b2009-12-14 18:00:05 -08001243static ssize_t comm_write(struct file *file, const char __user *buf,
1244 size_t count, loff_t *offset)
1245{
1246 struct inode *inode = file->f_path.dentry->d_inode;
1247 struct task_struct *p;
1248 char buffer[TASK_COMM_LEN];
1249
1250 memset(buffer, 0, sizeof(buffer));
1251 if (count > sizeof(buffer) - 1)
1252 count = sizeof(buffer) - 1;
1253 if (copy_from_user(buffer, buf, count))
1254 return -EFAULT;
1255
1256 p = get_proc_task(inode);
1257 if (!p)
1258 return -ESRCH;
1259
1260 if (same_thread_group(current, p))
1261 set_task_comm(p, buffer);
1262 else
1263 count = -EINVAL;
1264
1265 put_task_struct(p);
1266
1267 return count;
1268}
1269
1270static int comm_show(struct seq_file *m, void *v)
1271{
1272 struct inode *inode = m->private;
1273 struct task_struct *p;
1274
1275 p = get_proc_task(inode);
1276 if (!p)
1277 return -ESRCH;
1278
1279 task_lock(p);
1280 seq_printf(m, "%s\n", p->comm);
1281 task_unlock(p);
1282
1283 put_task_struct(p);
1284
1285 return 0;
1286}
1287
1288static int comm_open(struct inode *inode, struct file *filp)
1289{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001290 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001291}
1292
1293static const struct file_operations proc_pid_set_comm_operations = {
1294 .open = comm_open,
1295 .read = seq_read,
1296 .write = comm_write,
1297 .llseek = seq_lseek,
1298 .release = single_release,
1299};
1300
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001301static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001302{
1303 struct task_struct *task;
1304 struct mm_struct *mm;
1305 struct file *exe_file;
1306
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001307 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001308 if (!task)
1309 return -ENOENT;
1310 mm = get_task_mm(task);
1311 put_task_struct(task);
1312 if (!mm)
1313 return -ENOENT;
1314 exe_file = get_mm_exe_file(mm);
1315 mmput(mm);
1316 if (exe_file) {
1317 *exe_path = exe_file->f_path;
1318 path_get(&exe_file->f_path);
1319 fput(exe_file);
1320 return 0;
1321 } else
1322 return -ENOENT;
1323}
1324
Al Viro008b1502005-08-20 00:17:39 +01001325static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001328 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int error = -EACCES;
1330
Eric W. Biederman778c1142006-06-26 00:25:58 -07001331 /* Are we allowed to snoop on the tasks file descriptors? */
1332 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Christoph Hellwig408ef012012-06-18 10:47:03 -04001335 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1336 if (error)
1337 goto out;
1338
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001339 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001340 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341out:
Al Viro008b1502005-08-20 00:17:39 +01001342 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001345static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Mel Gormane12ba742007-10-16 01:25:52 -07001347 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001348 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int len;
1350
1351 if (!tmp)
1352 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001353
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001354 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001355 len = PTR_ERR(pathname);
1356 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001358 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 if (len > buflen)
1361 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001362 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 len = -EFAULT;
1364 out:
1365 free_page((unsigned long)tmp);
1366 return len;
1367}
1368
1369static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1370{
1371 int error = -EACCES;
1372 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001373 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Eric W. Biederman778c1142006-06-26 00:25:58 -07001375 /* Are we allowed to snoop on the tasks file descriptors? */
1376 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001379 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (error)
1381 goto out;
1382
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001383 error = do_proc_readlink(&path, buffer, buflen);
1384 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return error;
1387}
1388
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001389const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001391 .follow_link = proc_pid_follow_link,
1392 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393};
1394
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001395
1396/* building an inode */
1397
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001398struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001399{
1400 struct inode * inode;
1401 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001402 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001403
1404 /* We need a new inode */
1405
1406 inode = new_inode(sb);
1407 if (!inode)
1408 goto out;
1409
1410 /* Common stuff */
1411 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001412 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001413 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001414 inode->i_op = &proc_def_inode_operations;
1415
1416 /*
1417 * grab the reference to task.
1418 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001419 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001420 if (!ei->pid)
1421 goto out_unlock;
1422
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001423 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001424 rcu_read_lock();
1425 cred = __task_cred(task);
1426 inode->i_uid = cred->euid;
1427 inode->i_gid = cred->egid;
1428 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001429 }
1430 security_task_to_inode(task, inode);
1431
1432out:
1433 return inode;
1434
1435out_unlock:
1436 iput(inode);
1437 return NULL;
1438}
1439
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001440int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001441{
1442 struct inode *inode = dentry->d_inode;
1443 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001444 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001445 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001446
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001447 generic_fillattr(inode, stat);
1448
1449 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001450 stat->uid = GLOBAL_ROOT_UID;
1451 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001452 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1453 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001454 if (!has_pid_permissions(pid, task, 2)) {
1455 rcu_read_unlock();
1456 /*
1457 * This doesn't prevent learning whether PID exists,
1458 * it only makes getattr() consistent with readdir().
1459 */
1460 return -ENOENT;
1461 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001462 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1463 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001464 cred = __task_cred(task);
1465 stat->uid = cred->euid;
1466 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001467 }
1468 }
1469 rcu_read_unlock();
1470 return 0;
1471}
1472
1473/* dentry stuff */
1474
1475/*
1476 * Exceptional case: normally we are not allowed to unhash a busy
1477 * directory. In this case, however, we can do it - no aliasing problems
1478 * due to the way we treat inodes.
1479 *
1480 * Rewrite the inode's ownerships here because the owning task may have
1481 * performed a setuid(), etc.
1482 *
1483 * Before the /proc/pid/status file was created the only way to read
1484 * the effective uid of a /process was to stat /proc/pid. Reading
1485 * /proc/pid/status is slow enough that procps and other packages
1486 * kept stating /proc/pid. To keep the rules in /proc simple I have
1487 * made this apply to all per process world readable and executable
1488 * directories.
1489 */
Al Viro0b728e12012-06-10 16:03:43 -04001490int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001491{
Nick Piggin34286d62011-01-07 17:49:57 +11001492 struct inode *inode;
1493 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001494 const struct cred *cred;
1495
Al Viro0b728e12012-06-10 16:03:43 -04001496 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001497 return -ECHILD;
1498
1499 inode = dentry->d_inode;
1500 task = get_proc_task(inode);
1501
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001502 if (task) {
1503 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1504 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001505 rcu_read_lock();
1506 cred = __task_cred(task);
1507 inode->i_uid = cred->euid;
1508 inode->i_gid = cred->egid;
1509 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001510 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001511 inode->i_uid = GLOBAL_ROOT_UID;
1512 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001513 }
1514 inode->i_mode &= ~(S_ISUID | S_ISGID);
1515 security_task_to_inode(task, inode);
1516 put_task_struct(task);
1517 return 1;
1518 }
1519 d_drop(dentry);
1520 return 0;
1521}
1522
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001523const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001524{
1525 .d_revalidate = pid_revalidate,
1526 .d_delete = pid_delete_dentry,
1527};
1528
1529/* Lookups */
1530
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001531/*
1532 * Fill a directory entry.
1533 *
1534 * If possible create the dcache entry and derive our inode number and
1535 * file type from dcache entry.
1536 *
1537 * Since all of the proc inode numbers are dynamically generated, the inode
1538 * numbers do not exist until the inode is cache. This means creating the
1539 * the dcache entry in readdir is necessary to keep the inode numbers
1540 * reported by readdir in sync with the inode numbers reported
1541 * by stat.
1542 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001543int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1544 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001545 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001546{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001547 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001548 struct inode *inode;
1549 struct qstr qname;
1550 ino_t ino = 0;
1551 unsigned type = DT_UNKNOWN;
1552
1553 qname.name = name;
1554 qname.len = len;
1555 qname.hash = full_name_hash(name, len);
1556
1557 child = d_lookup(dir, &qname);
1558 if (!child) {
1559 struct dentry *new;
1560 new = d_alloc(dir, &qname);
1561 if (new) {
1562 child = instantiate(dir->d_inode, new, task, ptr);
1563 if (child)
1564 dput(new);
1565 else
1566 child = new;
1567 }
1568 }
1569 if (!child || IS_ERR(child) || !child->d_inode)
1570 goto end_instantiate;
1571 inode = child->d_inode;
1572 if (inode) {
1573 ino = inode->i_ino;
1574 type = inode->i_mode >> 12;
1575 }
1576 dput(child);
1577end_instantiate:
1578 if (!ino)
1579 ino = find_inode_number(dir, &qname);
1580 if (!ino)
1581 ino = 1;
1582 return filldir(dirent, name, len, filp->f_pos, ino, type);
1583}
1584
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001585#ifdef CONFIG_CHECKPOINT_RESTORE
1586
1587/*
1588 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1589 * which represent vma start and end addresses.
1590 */
1591static int dname_to_vma_addr(struct dentry *dentry,
1592 unsigned long *start, unsigned long *end)
1593{
1594 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1595 return -EINVAL;
1596
1597 return 0;
1598}
1599
Al Viro0b728e12012-06-10 16:03:43 -04001600static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001601{
1602 unsigned long vm_start, vm_end;
1603 bool exact_vma_exists = false;
1604 struct mm_struct *mm = NULL;
1605 struct task_struct *task;
1606 const struct cred *cred;
1607 struct inode *inode;
1608 int status = 0;
1609
Al Viro0b728e12012-06-10 16:03:43 -04001610 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001611 return -ECHILD;
1612
1613 if (!capable(CAP_SYS_ADMIN)) {
1614 status = -EACCES;
1615 goto out_notask;
1616 }
1617
1618 inode = dentry->d_inode;
1619 task = get_proc_task(inode);
1620 if (!task)
1621 goto out_notask;
1622
Cong Wang2344bec2012-05-31 16:26:18 -07001623 mm = mm_access(task, PTRACE_MODE_READ);
1624 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001625 goto out;
1626
1627 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1628 down_read(&mm->mmap_sem);
1629 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1630 up_read(&mm->mmap_sem);
1631 }
1632
1633 mmput(mm);
1634
1635 if (exact_vma_exists) {
1636 if (task_dumpable(task)) {
1637 rcu_read_lock();
1638 cred = __task_cred(task);
1639 inode->i_uid = cred->euid;
1640 inode->i_gid = cred->egid;
1641 rcu_read_unlock();
1642 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001643 inode->i_uid = GLOBAL_ROOT_UID;
1644 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001645 }
1646 security_task_to_inode(task, inode);
1647 status = 1;
1648 }
1649
1650out:
1651 put_task_struct(task);
1652
1653out_notask:
1654 if (status <= 0)
1655 d_drop(dentry);
1656
1657 return status;
1658}
1659
1660static const struct dentry_operations tid_map_files_dentry_operations = {
1661 .d_revalidate = map_files_d_revalidate,
1662 .d_delete = pid_delete_dentry,
1663};
1664
1665static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1666{
1667 unsigned long vm_start, vm_end;
1668 struct vm_area_struct *vma;
1669 struct task_struct *task;
1670 struct mm_struct *mm;
1671 int rc;
1672
1673 rc = -ENOENT;
1674 task = get_proc_task(dentry->d_inode);
1675 if (!task)
1676 goto out;
1677
1678 mm = get_task_mm(task);
1679 put_task_struct(task);
1680 if (!mm)
1681 goto out;
1682
1683 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1684 if (rc)
1685 goto out_mmput;
1686
1687 down_read(&mm->mmap_sem);
1688 vma = find_exact_vma(mm, vm_start, vm_end);
1689 if (vma && vma->vm_file) {
1690 *path = vma->vm_file->f_path;
1691 path_get(path);
1692 rc = 0;
1693 }
1694 up_read(&mm->mmap_sem);
1695
1696out_mmput:
1697 mmput(mm);
1698out:
1699 return rc;
1700}
1701
1702struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001703 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001704 unsigned long len;
1705 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1706};
1707
1708static struct dentry *
1709proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1710 struct task_struct *task, const void *ptr)
1711{
Al Viro7b540d02012-08-27 14:55:26 -04001712 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001713 struct proc_inode *ei;
1714 struct inode *inode;
1715
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001716 inode = proc_pid_make_inode(dir->i_sb, task);
1717 if (!inode)
1718 return ERR_PTR(-ENOENT);
1719
1720 ei = PROC_I(inode);
1721 ei->op.proc_get_link = proc_map_files_get_link;
1722
1723 inode->i_op = &proc_pid_link_inode_operations;
1724 inode->i_size = 64;
1725 inode->i_mode = S_IFLNK;
1726
Al Viro7b540d02012-08-27 14:55:26 -04001727 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001728 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001729 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001730 inode->i_mode |= S_IWUSR;
1731
1732 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1733 d_add(dentry, inode);
1734
1735 return NULL;
1736}
1737
1738static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001739 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001740{
1741 unsigned long vm_start, vm_end;
1742 struct vm_area_struct *vma;
1743 struct task_struct *task;
1744 struct dentry *result;
1745 struct mm_struct *mm;
1746
1747 result = ERR_PTR(-EACCES);
1748 if (!capable(CAP_SYS_ADMIN))
1749 goto out;
1750
1751 result = ERR_PTR(-ENOENT);
1752 task = get_proc_task(dir);
1753 if (!task)
1754 goto out;
1755
1756 result = ERR_PTR(-EACCES);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001757 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001758 goto out_put_task;
1759
1760 result = ERR_PTR(-ENOENT);
1761 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001762 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001763
1764 mm = get_task_mm(task);
1765 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001766 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001767
1768 down_read(&mm->mmap_sem);
1769 vma = find_exact_vma(mm, vm_start, vm_end);
1770 if (!vma)
1771 goto out_no_vma;
1772
Al Viro7b540d02012-08-27 14:55:26 -04001773 result = proc_map_files_instantiate(dir, dentry, task,
1774 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001775
1776out_no_vma:
1777 up_read(&mm->mmap_sem);
1778 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001779out_put_task:
1780 put_task_struct(task);
1781out:
1782 return result;
1783}
1784
1785static const struct inode_operations proc_map_files_inode_operations = {
1786 .lookup = proc_map_files_lookup,
1787 .permission = proc_fd_permission,
1788 .setattr = proc_setattr,
1789};
1790
1791static int
1792proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1793{
1794 struct dentry *dentry = filp->f_path.dentry;
1795 struct inode *inode = dentry->d_inode;
1796 struct vm_area_struct *vma;
1797 struct task_struct *task;
1798 struct mm_struct *mm;
1799 ino_t ino;
1800 int ret;
1801
1802 ret = -EACCES;
1803 if (!capable(CAP_SYS_ADMIN))
1804 goto out;
1805
1806 ret = -ENOENT;
1807 task = get_proc_task(inode);
1808 if (!task)
1809 goto out;
1810
1811 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001812 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001813 goto out_put_task;
1814
1815 ret = 0;
1816 switch (filp->f_pos) {
1817 case 0:
1818 ino = inode->i_ino;
1819 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001820 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001821 filp->f_pos++;
1822 case 1:
1823 ino = parent_ino(dentry);
1824 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001825 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001826 filp->f_pos++;
1827 default:
1828 {
1829 unsigned long nr_files, pos, i;
1830 struct flex_array *fa = NULL;
1831 struct map_files_info info;
1832 struct map_files_info *p;
1833
1834 mm = get_task_mm(task);
1835 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001836 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001837 down_read(&mm->mmap_sem);
1838
1839 nr_files = 0;
1840
1841 /*
1842 * We need two passes here:
1843 *
1844 * 1) Collect vmas of mapped files with mmap_sem taken
1845 * 2) Release mmap_sem and instantiate entries
1846 *
1847 * otherwise we get lockdep complained, since filldir()
1848 * routine might require mmap_sem taken in might_fault().
1849 */
1850
1851 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1852 if (vma->vm_file && ++pos > filp->f_pos)
1853 nr_files++;
1854 }
1855
1856 if (nr_files) {
1857 fa = flex_array_alloc(sizeof(info), nr_files,
1858 GFP_KERNEL);
1859 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1860 GFP_KERNEL)) {
1861 ret = -ENOMEM;
1862 if (fa)
1863 flex_array_free(fa);
1864 up_read(&mm->mmap_sem);
1865 mmput(mm);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001866 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001867 }
1868 for (i = 0, vma = mm->mmap, pos = 2; vma;
1869 vma = vma->vm_next) {
1870 if (!vma->vm_file)
1871 continue;
1872 if (++pos <= filp->f_pos)
1873 continue;
1874
Al Viro7b540d02012-08-27 14:55:26 -04001875 info.mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001876 info.len = snprintf(info.name,
1877 sizeof(info.name), "%lx-%lx",
1878 vma->vm_start, vma->vm_end);
1879 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1880 BUG();
1881 }
1882 }
1883 up_read(&mm->mmap_sem);
1884
1885 for (i = 0; i < nr_files; i++) {
1886 p = flex_array_get(fa, i);
1887 ret = proc_fill_cache(filp, dirent, filldir,
1888 p->name, p->len,
1889 proc_map_files_instantiate,
Al Viro7b540d02012-08-27 14:55:26 -04001890 task,
1891 (void *)(unsigned long)p->mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001892 if (ret)
1893 break;
1894 filp->f_pos++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001895 }
1896 if (fa)
1897 flex_array_free(fa);
1898 mmput(mm);
1899 }
1900 }
1901
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001902out_put_task:
1903 put_task_struct(task);
1904out:
1905 return ret;
1906}
1907
1908static const struct file_operations proc_map_files_operations = {
1909 .read = generic_read_dir,
1910 .readdir = proc_map_files_readdir,
1911 .llseek = default_llseek,
1912};
1913
1914#endif /* CONFIG_CHECKPOINT_RESTORE */
1915
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001916static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001917 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001918{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001919 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001920 struct inode *inode;
1921 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07001922 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001923
Eric W. Biederman61a28782006-10-02 02:18:49 -07001924 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001925 if (!inode)
1926 goto out;
1927
1928 ei = PROC_I(inode);
1929 inode->i_mode = p->mode;
1930 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02001931 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001932 if (p->iop)
1933 inode->i_op = p->iop;
1934 if (p->fop)
1935 inode->i_fop = p->fop;
1936 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001937 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001938 d_add(dentry, inode);
1939 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04001940 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001941 error = NULL;
1942out:
1943 return error;
1944}
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946static struct dentry *proc_pident_lookup(struct inode *dir,
1947 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001948 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001949 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001951 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001952 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001953 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001955 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Eric W. Biederman99f89552006-06-26 00:25:55 -07001957 if (!task)
1958 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001960 /*
1961 * Yes, it does not scale. And it should not. Don't add
1962 * new entries into /proc/<tgid>/ without very good reasons.
1963 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001964 last = &ents[nents - 1];
1965 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (p->len != dentry->d_name.len)
1967 continue;
1968 if (!memcmp(dentry->d_name.name, p->name, p->len))
1969 break;
1970 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001971 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 goto out;
1973
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001974 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001976 put_task_struct(task);
1977out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001978 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Eric Dumazetc5141e62007-05-08 00:26:15 -07001981static int proc_pident_fill_cache(struct file *filp, void *dirent,
1982 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001983{
1984 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1985 proc_pident_instantiate, task, p);
1986}
1987
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001988static int proc_pident_readdir(struct file *filp,
1989 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001990 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001991{
1992 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001993 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001994 struct inode *inode = dentry->d_inode;
1995 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001996 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001997 ino_t ino;
1998 int ret;
1999
2000 ret = -ENOENT;
2001 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002002 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002003
2004 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002005 i = filp->f_pos;
2006 switch (i) {
2007 case 0:
2008 ino = inode->i_ino;
2009 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2010 goto out;
2011 i++;
2012 filp->f_pos++;
2013 /* fall through */
2014 case 1:
2015 ino = parent_ino(dentry);
2016 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2017 goto out;
2018 i++;
2019 filp->f_pos++;
2020 /* fall through */
2021 default:
2022 i -= 2;
2023 if (i >= nents) {
2024 ret = 1;
2025 goto out;
2026 }
2027 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002028 last = &ents[nents - 1];
2029 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002030 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002031 goto out;
2032 filp->f_pos++;
2033 p++;
2034 }
2035 }
2036
2037 ret = 1;
2038out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002039 put_task_struct(task);
2040out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002041 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002045static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2046 size_t count, loff_t *ppos)
2047{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002048 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002049 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002050 ssize_t length;
2051 struct task_struct *task = get_proc_task(inode);
2052
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002053 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002054 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002055
2056 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002057 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002058 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002059 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002060 if (length > 0)
2061 length = simple_read_from_buffer(buf, count, ppos, p, length);
2062 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002063 return length;
2064}
2065
2066static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2067 size_t count, loff_t *ppos)
2068{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002069 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002070 char *page;
2071 ssize_t length;
2072 struct task_struct *task = get_proc_task(inode);
2073
2074 length = -ESRCH;
2075 if (!task)
2076 goto out_no_task;
2077 if (count > PAGE_SIZE)
2078 count = PAGE_SIZE;
2079
2080 /* No partial writes. */
2081 length = -EINVAL;
2082 if (*ppos != 0)
2083 goto out;
2084
2085 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002086 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002087 if (!page)
2088 goto out;
2089
2090 length = -EFAULT;
2091 if (copy_from_user(page, buf, count))
2092 goto out_free;
2093
David Howells107db7c2009-05-08 13:55:27 +01002094 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002095 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002096 if (length < 0)
2097 goto out_free;
2098
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002099 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002100 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002101 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002102 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002103out_free:
2104 free_page((unsigned long) page);
2105out:
2106 put_task_struct(task);
2107out_no_task:
2108 return length;
2109}
2110
Arjan van de Ven00977a52007-02-12 00:55:34 -08002111static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002112 .read = proc_pid_attr_read,
2113 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002114 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002115};
2116
Eric Dumazetc5141e62007-05-08 00:26:15 -07002117static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002118 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2119 REG("prev", S_IRUGO, proc_pid_attr_operations),
2120 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2121 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2122 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2123 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002124};
2125
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002126static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 void * dirent, filldir_t filldir)
2128{
2129 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002130 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
Arjan van de Ven00977a52007-02-12 00:55:34 -08002133static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002135 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002136 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137};
2138
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002139static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002140 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002142 return proc_pident_lookup(dir, dentry,
2143 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002146static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002147 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002148 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002149 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150};
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152#endif
2153
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002154#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002155static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2156 size_t count, loff_t *ppos)
2157{
2158 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2159 struct mm_struct *mm;
2160 char buffer[PROC_NUMBUF];
2161 size_t len;
2162 int ret;
2163
2164 if (!task)
2165 return -ESRCH;
2166
2167 ret = 0;
2168 mm = get_task_mm(task);
2169 if (mm) {
2170 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2171 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2172 MMF_DUMP_FILTER_SHIFT));
2173 mmput(mm);
2174 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2175 }
2176
2177 put_task_struct(task);
2178
2179 return ret;
2180}
2181
2182static ssize_t proc_coredump_filter_write(struct file *file,
2183 const char __user *buf,
2184 size_t count,
2185 loff_t *ppos)
2186{
2187 struct task_struct *task;
2188 struct mm_struct *mm;
2189 char buffer[PROC_NUMBUF], *end;
2190 unsigned int val;
2191 int ret;
2192 int i;
2193 unsigned long mask;
2194
2195 ret = -EFAULT;
2196 memset(buffer, 0, sizeof(buffer));
2197 if (count > sizeof(buffer) - 1)
2198 count = sizeof(buffer) - 1;
2199 if (copy_from_user(buffer, buf, count))
2200 goto out_no_task;
2201
2202 ret = -EINVAL;
2203 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2204 if (*end == '\n')
2205 end++;
2206 if (end - buffer == 0)
2207 goto out_no_task;
2208
2209 ret = -ESRCH;
2210 task = get_proc_task(file->f_dentry->d_inode);
2211 if (!task)
2212 goto out_no_task;
2213
2214 ret = end - buffer;
2215 mm = get_task_mm(task);
2216 if (!mm)
2217 goto out_no_mm;
2218
2219 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2220 if (val & mask)
2221 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2222 else
2223 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2224 }
2225
2226 mmput(mm);
2227 out_no_mm:
2228 put_task_struct(task);
2229 out_no_task:
2230 return ret;
2231}
2232
2233static const struct file_operations proc_coredump_filter_operations = {
2234 .read = proc_coredump_filter_read,
2235 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002236 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002237};
2238#endif
2239
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002240#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002241static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002242{
Andrea Righi940389b2008-07-28 00:48:12 +02002243 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002244 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002245 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002246
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002247 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2248 if (result)
2249 return result;
2250
2251 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2252 result = -EACCES;
2253 goto out_unlock;
2254 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002255
Andrea Righi59954772008-07-27 17:29:15 +02002256 if (whole && lock_task_sighand(task, &flags)) {
2257 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002258
Andrea Righi59954772008-07-27 17:29:15 +02002259 task_io_accounting_add(&acct, &task->signal->ioac);
2260 while_each_thread(task, t)
2261 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002262
Andrea Righi59954772008-07-27 17:29:15 +02002263 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002264 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002265 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002266 "rchar: %llu\n"
2267 "wchar: %llu\n"
2268 "syscr: %llu\n"
2269 "syscw: %llu\n"
2270 "read_bytes: %llu\n"
2271 "write_bytes: %llu\n"
2272 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002273 (unsigned long long)acct.rchar,
2274 (unsigned long long)acct.wchar,
2275 (unsigned long long)acct.syscr,
2276 (unsigned long long)acct.syscw,
2277 (unsigned long long)acct.read_bytes,
2278 (unsigned long long)acct.write_bytes,
2279 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002280out_unlock:
2281 mutex_unlock(&task->signal->cred_guard_mutex);
2282 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002283}
Andrea Righi297c5d92008-07-25 01:48:49 -07002284
2285static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2286{
2287 return do_io_accounting(task, buffer, 0);
2288}
2289
2290static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2291{
2292 return do_io_accounting(task, buffer, 1);
2293}
2294#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002295
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002296#ifdef CONFIG_USER_NS
2297static int proc_id_map_open(struct inode *inode, struct file *file,
2298 struct seq_operations *seq_ops)
2299{
2300 struct user_namespace *ns = NULL;
2301 struct task_struct *task;
2302 struct seq_file *seq;
2303 int ret = -EINVAL;
2304
2305 task = get_proc_task(inode);
2306 if (task) {
2307 rcu_read_lock();
2308 ns = get_user_ns(task_cred_xxx(task, user_ns));
2309 rcu_read_unlock();
2310 put_task_struct(task);
2311 }
2312 if (!ns)
2313 goto err;
2314
2315 ret = seq_open(file, seq_ops);
2316 if (ret)
2317 goto err_put_ns;
2318
2319 seq = file->private_data;
2320 seq->private = ns;
2321
2322 return 0;
2323err_put_ns:
2324 put_user_ns(ns);
2325err:
2326 return ret;
2327}
2328
2329static int proc_id_map_release(struct inode *inode, struct file *file)
2330{
2331 struct seq_file *seq = file->private_data;
2332 struct user_namespace *ns = seq->private;
2333 put_user_ns(ns);
2334 return seq_release(inode, file);
2335}
2336
2337static int proc_uid_map_open(struct inode *inode, struct file *file)
2338{
2339 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2340}
2341
2342static int proc_gid_map_open(struct inode *inode, struct file *file)
2343{
2344 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2345}
2346
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002347static int proc_projid_map_open(struct inode *inode, struct file *file)
2348{
2349 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2350}
2351
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002352static const struct file_operations proc_uid_map_operations = {
2353 .open = proc_uid_map_open,
2354 .write = proc_uid_map_write,
2355 .read = seq_read,
2356 .llseek = seq_lseek,
2357 .release = proc_id_map_release,
2358};
2359
2360static const struct file_operations proc_gid_map_operations = {
2361 .open = proc_gid_map_open,
2362 .write = proc_gid_map_write,
2363 .read = seq_read,
2364 .llseek = seq_lseek,
2365 .release = proc_id_map_release,
2366};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002367
2368static const struct file_operations proc_projid_map_operations = {
2369 .open = proc_projid_map_open,
2370 .write = proc_projid_map_write,
2371 .read = seq_read,
2372 .llseek = seq_lseek,
2373 .release = proc_id_map_release,
2374};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002375#endif /* CONFIG_USER_NS */
2376
Kees Cook47830722008-10-06 03:11:58 +04002377static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2378 struct pid *pid, struct task_struct *task)
2379{
Al Viroa9712bc2011-03-23 15:52:50 -04002380 int err = lock_trace(task);
2381 if (!err) {
2382 seq_printf(m, "%08x\n", task->personality);
2383 unlock_trace(task);
2384 }
2385 return err;
Kees Cook47830722008-10-06 03:11:58 +04002386}
2387
Eric W. Biederman801199c2006-10-02 02:18:48 -07002388/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002389 * Thread groups
2390 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002391static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002392static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002393
Eric Dumazetc5141e62007-05-08 00:26:15 -07002394static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002395 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2396 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002397#ifdef CONFIG_CHECKPOINT_RESTORE
2398 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2399#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002400 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002401 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002402#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002403 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002404#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002405 REG("environ", S_IRUSR, proc_environ_operations),
2406 INF("auxv", S_IRUSR, proc_pid_auxv),
2407 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002408 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002409 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002410#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002411 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002412#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002413#ifdef CONFIG_SCHED_AUTOGROUP
2414 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2415#endif
john stultz4614a696b2009-12-14 18:00:05 -08002416 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002417#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002418 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002419#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002420 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2421 ONE("stat", S_IRUGO, proc_tgid_stat),
2422 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002423 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002424#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002425 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002426#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002427 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2428 LNK("cwd", proc_cwd_link),
2429 LNK("root", proc_root_link),
2430 LNK("exe", proc_exe_link),
2431 REG("mounts", S_IRUGO, proc_mounts_operations),
2432 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2433 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002434#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002435 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002436 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002437 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002438#endif
2439#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002440 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002441#endif
2442#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002443 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002444#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002445#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002446 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002447#endif
2448#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002449 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002450#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002451#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002452 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002453#endif
Paul Menage8793d852007-10-18 23:39:39 -07002454#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002455 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002456#endif
Paul Menagea4243162007-10-18 23:39:35 -07002457#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002458 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002459#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002460 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesa63d83f2010-08-09 17:19:46 -07002461 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002462#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002463 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2464 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002465#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002466#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002467 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002468#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002469#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002470 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002471#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002472#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002473 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002474#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002475#ifdef CONFIG_HARDWALL
2476 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2477#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002478#ifdef CONFIG_USER_NS
2479 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2480 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002481 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002482#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002483};
2484
2485static int proc_tgid_base_readdir(struct file * filp,
2486 void * dirent, filldir_t filldir)
2487{
2488 return proc_pident_readdir(filp,dirent,filldir,
2489 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2490}
2491
Arjan van de Ven00977a52007-02-12 00:55:34 -08002492static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002493 .read = generic_read_dir,
2494 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002495 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496};
2497
Al Viro00cd8dd2012-06-10 17:13:09 -04002498static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2499{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002500 return proc_pident_lookup(dir, dentry,
2501 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502}
2503
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002504static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505 .lookup = proc_tgid_base_lookup,
2506 .getattr = pid_getattr,
2507 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002508 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002509};
2510
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002511static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002513 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002514 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002515 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Eric W. Biederman48e64842006-06-26 00:25:48 -07002517 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002518 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2519 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002520 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002521 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002522 d_drop(dentry);
2523 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Eric W. Biederman48e64842006-06-26 00:25:48 -07002526 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002527 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2528 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002529 if (!leader)
2530 goto out;
2531
2532 name.name = "task";
2533 name.len = strlen(name.name);
2534 dir = d_hash_and_lookup(leader, &name);
2535 if (!dir)
2536 goto out_put_leader;
2537
2538 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002539 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002540 dentry = d_hash_and_lookup(dir, &name);
2541 if (dentry) {
2542 shrink_dcache_parent(dentry);
2543 d_drop(dentry);
2544 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002546
2547 dput(dir);
2548out_put_leader:
2549 dput(leader);
2550out:
2551 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
Randy Dunlap0895e912007-10-21 21:00:10 -07002554/**
2555 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2556 * @task: task that should be flushed.
2557 *
2558 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002559 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002560 * in. This call is supposed to do all of this job.
2561 *
2562 * Looks in the dcache for
2563 * /proc/@pid
2564 * /proc/@tgid/task/@pid
2565 * if either directory is present flushes it and all of it'ts children
2566 * from the dcache.
2567 *
2568 * It is safe and reasonable to cache /proc entries for a task until
2569 * that task exits. After that they just clog up the dcache with
2570 * useless entries, possibly causing useful dcache entries to be
2571 * flushed instead. This routine is proved to flush those useless
2572 * dcache entries at process exit time.
2573 *
2574 * NOTE: This routine is just an optimization so it does not guarantee
2575 * that no dcache entries will exist at process exit time it
2576 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002577 */
2578
2579void proc_flush_task(struct task_struct *task)
2580{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002581 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002582 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002583 struct upid *upid;
2584
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002585 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002586 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002587
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002588 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002589 upid = &pid->numbers[i];
2590 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002591 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002592 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002593
2594 upid = &pid->numbers[pid->level];
2595 if (upid->nr == 1)
2596 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002597}
2598
Adrian Bunk9711ef92006-12-06 20:38:31 -08002599static struct dentry *proc_pid_instantiate(struct inode *dir,
2600 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002601 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002602{
2603 struct dentry *error = ERR_PTR(-ENOENT);
2604 struct inode *inode;
2605
Eric W. Biederman61a28782006-10-02 02:18:49 -07002606 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002607 if (!inode)
2608 goto out;
2609
2610 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2611 inode->i_op = &proc_tgid_base_inode_operations;
2612 inode->i_fop = &proc_tgid_base_operations;
2613 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002614
Miklos Szeredibfe86842011-10-28 14:13:29 +02002615 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2616 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002617
Nick Pigginfb045ad2011-01-07 17:49:55 +11002618 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002619
2620 d_add(dentry, inode);
2621 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002622 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002623 error = NULL;
2624out:
2625 return error;
2626}
2627
Al Viro00cd8dd2012-06-10 17:13:09 -04002628struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Eric W. Biedermane656d8a2010-07-10 14:52:49 -07002630 struct dentry *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002633 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 tgid = name_to_int(dentry);
2636 if (tgid == ~0U)
2637 goto out;
2638
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002639 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002640 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002641 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (task)
2643 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002644 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (!task)
2646 goto out;
2647
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002648 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002649 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002651 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002655 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002656 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002658struct tgid_iter {
2659 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002660 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002661};
2662static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2663{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002664 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002666 if (iter.task)
2667 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002668 rcu_read_lock();
2669retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002670 iter.task = NULL;
2671 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002672 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002673 iter.tgid = pid_nr_ns(pid, ns);
2674 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002675 /* What we to know is if the pid we have find is the
2676 * pid of a thread_group_leader. Testing for task
2677 * being a thread_group_leader is the obvious thing
2678 * todo but there is a window when it fails, due to
2679 * the pid transfer logic in de_thread.
2680 *
2681 * So we perform the straight forward test of seeing
2682 * if the pid we have found is the pid of a thread
2683 * group leader, and don't worry if the task we have
2684 * found doesn't happen to be a thread group leader.
2685 * As we don't care in the case of readdir.
2686 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002687 if (!iter.task || !has_group_leader_pid(iter.task)) {
2688 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002689 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002690 }
2691 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002693 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002694 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
Eric W. Biedermane656d8a2010-07-10 14:52:49 -07002697#define TGID_OFFSET (FIRST_PROCESS_ENTRY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Eric W. Biederman61a28782006-10-02 02:18:49 -07002699static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002700 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002701{
2702 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002703 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002704 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002705 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002706}
2707
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002708static int fake_filldir(void *buf, const char *name, int namelen,
2709 loff_t offset, u64 ino, unsigned d_type)
2710{
2711 return 0;
2712}
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714/* for the /proc/ directory itself, after non-process stuff has been done */
2715int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2716{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07002717 unsigned int nr;
2718 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002719 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002720 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002721 filldir_t __filldir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07002723 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
2724 goto out_no_task;
2725 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2726
2727 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002728 if (!reaper)
2729 goto out_no_task;
2730
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002731 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002732 iter.task = NULL;
2733 iter.tgid = filp->f_pos - TGID_OFFSET;
2734 for (iter = next_tgid(ns, iter);
2735 iter.task;
2736 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002737 if (has_pid_permissions(ns, iter.task, 2))
2738 __filldir = filldir;
2739 else
2740 __filldir = fake_filldir;
2741
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002742 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002743 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002744 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002745 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002748 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2749out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002750 put_task_struct(reaper);
2751out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return 0;
2753}
2754
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002755/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002756 * Tasks
2757 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002758static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002759 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002760 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002761 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002762 REG("environ", S_IRUSR, proc_environ_operations),
2763 INF("auxv", S_IRUSR, proc_pid_auxv),
2764 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002765 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002766 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002767#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002768 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002769#endif
john stultz4614a696b2009-12-14 18:00:05 -08002770 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002771#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002772 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002773#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002774 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2775 ONE("stat", S_IRUGO, proc_tid_stat),
2776 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002777 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002778#ifdef CONFIG_CHECKPOINT_RESTORE
2779 REG("children", S_IRUGO, proc_tid_children_operations),
2780#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002781#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002782 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002783#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002784 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2785 LNK("cwd", proc_cwd_link),
2786 LNK("root", proc_root_link),
2787 LNK("exe", proc_exe_link),
2788 REG("mounts", S_IRUGO, proc_mounts_operations),
2789 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002790#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002791 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002792 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002793 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002794#endif
2795#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002796 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002797#endif
2798#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002799 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002800#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002801#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002802 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002803#endif
2804#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002805 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002806#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002807#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002808 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002809#endif
Paul Menage8793d852007-10-18 23:39:39 -07002810#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002811 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002812#endif
Paul Menagea4243162007-10-18 23:39:35 -07002813#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002814 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002815#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002816 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesa63d83f2010-08-09 17:19:46 -07002817 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002818#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002819 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002820 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002821#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002822#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002823 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002824#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002825#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002826 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002827#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002828#ifdef CONFIG_HARDWALL
2829 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2830#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002831#ifdef CONFIG_USER_NS
2832 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2833 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002834 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002835#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002836};
2837
2838static int proc_tid_base_readdir(struct file * filp,
2839 void * dirent, filldir_t filldir)
2840{
2841 return proc_pident_readdir(filp,dirent,filldir,
2842 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2843}
2844
Al Viro00cd8dd2012-06-10 17:13:09 -04002845static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2846{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002847 return proc_pident_lookup(dir, dentry,
2848 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002849}
2850
Arjan van de Ven00977a52007-02-12 00:55:34 -08002851static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002852 .read = generic_read_dir,
2853 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002854 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002855};
2856
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002857static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002858 .lookup = proc_tid_base_lookup,
2859 .getattr = pid_getattr,
2860 .setattr = proc_setattr,
2861};
2862
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002863static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002864 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002865{
2866 struct dentry *error = ERR_PTR(-ENOENT);
2867 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002868 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002869
2870 if (!inode)
2871 goto out;
2872 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2873 inode->i_op = &proc_tid_base_inode_operations;
2874 inode->i_fop = &proc_tid_base_operations;
2875 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002876
Miklos Szeredibfe86842011-10-28 14:13:29 +02002877 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
2878 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002879
Nick Pigginfb045ad2011-01-07 17:49:55 +11002880 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002881
2882 d_add(dentry, inode);
2883 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002884 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002885 error = NULL;
2886out:
2887 return error;
2888}
2889
Al Viro00cd8dd2012-06-10 17:13:09 -04002890static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002891{
2892 struct dentry *result = ERR_PTR(-ENOENT);
2893 struct task_struct *task;
2894 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002895 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002896 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002897
2898 if (!leader)
2899 goto out_no_task;
2900
2901 tid = name_to_int(dentry);
2902 if (tid == ~0U)
2903 goto out;
2904
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002905 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002906 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002907 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002908 if (task)
2909 get_task_struct(task);
2910 rcu_read_unlock();
2911 if (!task)
2912 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002913 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002914 goto out_drop_task;
2915
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002916 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002917out_drop_task:
2918 put_task_struct(task);
2919out:
2920 put_task_struct(leader);
2921out_no_task:
2922 return result;
2923}
2924
2925/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002926 * Find the first tid of a thread group to return to user space.
2927 *
2928 * Usually this is just the thread group leader, but if the users
2929 * buffer was too small or there was a seek into the middle of the
2930 * directory we have more work todo.
2931 *
2932 * In the case of a short read we start with find_task_by_pid.
2933 *
2934 * In the case of a seek we start with the leader and walk nr
2935 * threads past it.
2936 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002937static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002938 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002939{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002940 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002941
Eric W. Biedermancc288732006-06-26 00:26:01 -07002942 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002943 /* Attempt to start with the pid of a thread */
2944 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002945 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002946 if (pos && (pos->group_leader == leader))
2947 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002948 }
2949
2950 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002951 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002952 if (nr && nr >= get_nr_threads(leader))
2953 goto out;
2954
2955 /* If we haven't found our starting place yet start
2956 * with the leader and walk nr threads forward.
2957 */
2958 for (pos = leader; nr > 0; --nr) {
2959 pos = next_thread(pos);
2960 if (pos == leader) {
2961 pos = NULL;
2962 goto out;
2963 }
2964 }
2965found:
2966 get_task_struct(pos);
2967out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002968 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002969 return pos;
2970}
2971
2972/*
2973 * Find the next thread in the thread list.
2974 * Return NULL if there is an error or no next thread.
2975 *
2976 * The reference to the input task_struct is released.
2977 */
2978static struct task_struct *next_tid(struct task_struct *start)
2979{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002980 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002981 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002982 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002983 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002984 if (thread_group_leader(pos))
2985 pos = NULL;
2986 else
2987 get_task_struct(pos);
2988 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002989 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002990 put_task_struct(start);
2991 return pos;
2992}
2993
Eric W. Biederman61a28782006-10-02 02:18:49 -07002994static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2995 struct task_struct *task, int tid)
2996{
2997 char name[PROC_NUMBUF];
2998 int len = snprintf(name, sizeof(name), "%d", tid);
2999 return proc_fill_cache(filp, dirent, filldir, name, len,
3000 proc_task_instantiate, task, NULL);
3001}
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003/* for the /proc/TGID/task/ directories */
3004static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3005{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003006 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003008 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003009 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 int retval = -ENOENT;
3011 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003012 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003013 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003015 task = get_proc_task(inode);
3016 if (!task)
3017 goto out_no_task;
3018 rcu_read_lock();
3019 if (pid_alive(task)) {
3020 leader = task->group_leader;
3021 get_task_struct(leader);
3022 }
3023 rcu_read_unlock();
3024 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003025 if (!leader)
3026 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 retval = 0;
3028
Linus Torvaldsee568b22009-03-17 10:02:35 -07003029 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 case 0:
3031 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003032 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003034 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 /* fall through */
3036 case 1:
3037 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003038 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003040 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 /* fall through */
3042 }
3043
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003044 /* f_version caches the tgid value that the last readdir call couldn't
3045 * return. lseek aka telldir automagically resets f_version to 0.
3046 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003047 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003048 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003049 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003050 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003051 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003052 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003053 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003054 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003055 /* returning this tgid failed, save it as the first
3056 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003057 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003058 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003063 put_task_struct(leader);
3064out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return retval;
3066}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003067
3068static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3069{
3070 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003071 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003072 generic_fillattr(inode, stat);
3073
Eric W. Biederman99f89552006-06-26 00:25:55 -07003074 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003075 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003076 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003077 }
3078
3079 return 0;
3080}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003081
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003082static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003083 .lookup = proc_task_lookup,
3084 .getattr = proc_task_getattr,
3085 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003086 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003087};
3088
Arjan van de Ven00977a52007-02-12 00:55:34 -08003089static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003090 .read = generic_read_dir,
3091 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003092 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003093};