blob: df18db61d6d87108aea12587f3dc69cc9dfeae4c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876static ssize_t oom_adjust_read(struct file *file, char __user *buf,
877 size_t count, loff_t *ppos)
878{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800879 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700880 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700882 int oom_adjust = OOM_DISABLE;
883 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Eric W. Biederman99f89552006-06-26 00:25:55 -0700885 if (!task)
886 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700887
888 if (lock_task_sighand(task, &flags)) {
889 oom_adjust = task->signal->oom_adj;
890 unlock_task_sighand(task, &flags);
891 }
892
Eric W. Biederman99f89552006-06-26 00:25:55 -0700893 put_task_struct(task);
894
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700895 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700896
897 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
901 size_t count, loff_t *ppos)
902{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700903 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700904 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700905 int oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700906 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700907 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700909 memset(buffer, 0, sizeof(buffer));
910 if (count > sizeof(buffer) - 1)
911 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700912 if (copy_from_user(buffer, buf, count)) {
913 err = -EFAULT;
914 goto out;
915 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700916
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700917 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700918 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700919 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700920 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -0700921 oom_adjust != OOM_DISABLE) {
922 err = -EINVAL;
923 goto out;
924 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700925
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800926 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -0700927 if (!task) {
928 err = -ESRCH;
929 goto out;
930 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700931
Ying Han3d5992d2010-10-26 14:21:23 -0700932 task_lock(task);
933 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -0700934 err = -EINVAL;
935 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -0700936 }
937
David Rientjesd19d5472010-10-26 14:21:26 -0700938 if (!lock_task_sighand(task, &flags)) {
939 err = -ESRCH;
940 goto err_task_lock;
941 }
942
943 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
944 err = -EACCES;
945 goto err_sighand;
946 }
947
David Rientjes51b1bd22010-08-09 17:19:47 -0700948 /*
949 * Warn that /proc/pid/oom_adj is deprecated, see
950 * Documentation/feature-removal-schedule.txt.
951 */
Linus Torvaldsc2142702011-08-06 11:43:08 -0700952 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
David Rientjesbe8f6842011-07-25 17:12:18 -0700953 current->comm, task_pid_nr(current), task_pid_nr(task),
954 task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700955 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -0700956 /*
957 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
958 * value is always attainable.
959 */
960 if (task->signal->oom_adj == OOM_ADJUST_MAX)
961 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
962 else
963 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
964 -OOM_DISABLE;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800965 trace_oom_score_adj_update(task);
David Rientjes723548b2010-10-26 14:21:25 -0700966err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700967 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -0700968err_task_lock:
969 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700970 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -0700971out:
972 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Arjan van de Ven00977a52007-02-12 00:55:34 -0800975static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 .read = oom_adjust_read,
977 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100978 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979};
980
David Rientjesa63d83f2010-08-09 17:19:46 -0700981static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
982 size_t count, loff_t *ppos)
983{
984 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
985 char buffer[PROC_NUMBUF];
986 int oom_score_adj = OOM_SCORE_ADJ_MIN;
987 unsigned long flags;
988 size_t len;
989
990 if (!task)
991 return -ESRCH;
992 if (lock_task_sighand(task, &flags)) {
993 oom_score_adj = task->signal->oom_score_adj;
994 unlock_task_sighand(task, &flags);
995 }
996 put_task_struct(task);
997 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
998 return simple_read_from_buffer(buf, count, ppos, buffer, len);
999}
1000
1001static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1002 size_t count, loff_t *ppos)
1003{
1004 struct task_struct *task;
1005 char buffer[PROC_NUMBUF];
1006 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001007 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001008 int err;
1009
1010 memset(buffer, 0, sizeof(buffer));
1011 if (count > sizeof(buffer) - 1)
1012 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001013 if (copy_from_user(buffer, buf, count)) {
1014 err = -EFAULT;
1015 goto out;
1016 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001017
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001018 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001019 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001020 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001021 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001022 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1023 err = -EINVAL;
1024 goto out;
1025 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001026
1027 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001028 if (!task) {
1029 err = -ESRCH;
1030 goto out;
1031 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001032
Ying Han3d5992d2010-10-26 14:21:23 -07001033 task_lock(task);
1034 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001035 err = -EINVAL;
1036 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001037 }
David Rientjesd19d5472010-10-26 14:21:26 -07001038
1039 if (!lock_task_sighand(task, &flags)) {
1040 err = -ESRCH;
1041 goto err_task_lock;
1042 }
1043
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001044 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001045 !capable(CAP_SYS_RESOURCE)) {
1046 err = -EACCES;
1047 goto err_sighand;
1048 }
1049
David Rientjesa63d83f2010-08-09 17:19:46 -07001050 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001051 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1052 task->signal->oom_score_adj_min = oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001053 trace_oom_score_adj_update(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001054 /*
1055 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1056 * always attainable.
1057 */
1058 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1059 task->signal->oom_adj = OOM_DISABLE;
1060 else
1061 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1062 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001063err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001064 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001065err_task_lock:
1066 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001067 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001068out:
1069 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001070}
1071
1072static const struct file_operations proc_oom_score_adj_operations = {
1073 .read = oom_score_adj_read,
1074 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001075 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001076};
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078#ifdef CONFIG_AUDITSYSCALL
1079#define TMPBUFLEN 21
1080static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1081 size_t count, loff_t *ppos)
1082{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001083 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001084 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ssize_t length;
1086 char tmpbuf[TMPBUFLEN];
1087
Eric W. Biederman99f89552006-06-26 00:25:55 -07001088 if (!task)
1089 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001091 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001092 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1094}
1095
1096static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1097 size_t count, loff_t *ppos)
1098{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001099 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 char *page, *tmp;
1101 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 uid_t loginuid;
1103
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001104 rcu_read_lock();
1105 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1106 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001108 }
1109 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Al Viroe0182902006-05-18 08:28:02 -04001111 if (count >= PAGE_SIZE)
1112 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (*ppos != 0) {
1115 /* No partial writes. */
1116 return -EINVAL;
1117 }
Mel Gormane12ba742007-10-16 01:25:52 -07001118 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (!page)
1120 return -ENOMEM;
1121 length = -EFAULT;
1122 if (copy_from_user(page, buf, count))
1123 goto out_free_page;
1124
Al Viroe0182902006-05-18 08:28:02 -04001125 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 loginuid = simple_strtoul(page, &tmp, 10);
1127 if (tmp == page) {
1128 length = -EINVAL;
1129 goto out_free_page;
1130
1131 }
Eric Paris0a300be2012-01-03 14:23:08 -05001132 length = audit_set_loginuid(loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (likely(length == 0))
1134 length = count;
1135
1136out_free_page:
1137 free_page((unsigned long) page);
1138 return length;
1139}
1140
Arjan van de Ven00977a52007-02-12 00:55:34 -08001141static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 .read = proc_loginuid_read,
1143 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001144 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145};
Eric Paris1e0bd752008-03-13 08:15:31 -04001146
1147static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1148 size_t count, loff_t *ppos)
1149{
1150 struct inode * inode = file->f_path.dentry->d_inode;
1151 struct task_struct *task = get_proc_task(inode);
1152 ssize_t length;
1153 char tmpbuf[TMPBUFLEN];
1154
1155 if (!task)
1156 return -ESRCH;
1157 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1158 audit_get_sessionid(task));
1159 put_task_struct(task);
1160 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1161}
1162
1163static const struct file_operations proc_sessionid_operations = {
1164 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001165 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001166};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167#endif
1168
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001169#ifdef CONFIG_FAULT_INJECTION
1170static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1171 size_t count, loff_t *ppos)
1172{
1173 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1174 char buffer[PROC_NUMBUF];
1175 size_t len;
1176 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001177
1178 if (!task)
1179 return -ESRCH;
1180 make_it_fail = task->make_it_fail;
1181 put_task_struct(task);
1182
1183 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001184
1185 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001186}
1187
1188static ssize_t proc_fault_inject_write(struct file * file,
1189 const char __user * buf, size_t count, loff_t *ppos)
1190{
1191 struct task_struct *task;
1192 char buffer[PROC_NUMBUF], *end;
1193 int make_it_fail;
1194
1195 if (!capable(CAP_SYS_RESOURCE))
1196 return -EPERM;
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;
Vincent Licba8aaf2009-09-22 16:45:38 -07001202 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1203 if (*end)
1204 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001205 task = get_proc_task(file->f_dentry->d_inode);
1206 if (!task)
1207 return -ESRCH;
1208 task->make_it_fail = make_it_fail;
1209 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001210
1211 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001212}
1213
Arjan van de Ven00977a52007-02-12 00:55:34 -08001214static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001215 .read = proc_fault_inject_read,
1216 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001217 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001218};
1219#endif
1220
Arjan van de Ven97455122008-01-25 21:08:34 +01001221
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001222#ifdef CONFIG_SCHED_DEBUG
1223/*
1224 * Print out various scheduling related per-task fields:
1225 */
1226static int sched_show(struct seq_file *m, void *v)
1227{
1228 struct inode *inode = m->private;
1229 struct task_struct *p;
1230
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001231 p = get_proc_task(inode);
1232 if (!p)
1233 return -ESRCH;
1234 proc_sched_show_task(p, m);
1235
1236 put_task_struct(p);
1237
1238 return 0;
1239}
1240
1241static ssize_t
1242sched_write(struct file *file, const char __user *buf,
1243 size_t count, loff_t *offset)
1244{
1245 struct inode *inode = file->f_path.dentry->d_inode;
1246 struct task_struct *p;
1247
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001248 p = get_proc_task(inode);
1249 if (!p)
1250 return -ESRCH;
1251 proc_sched_set_task(p);
1252
1253 put_task_struct(p);
1254
1255 return count;
1256}
1257
1258static int sched_open(struct inode *inode, struct file *filp)
1259{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001260 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001261}
1262
1263static const struct file_operations proc_pid_sched_operations = {
1264 .open = sched_open,
1265 .read = seq_read,
1266 .write = sched_write,
1267 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001268 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001269};
1270
1271#endif
1272
Mike Galbraith5091faa2010-11-30 14:18:03 +01001273#ifdef CONFIG_SCHED_AUTOGROUP
1274/*
1275 * Print out autogroup related information:
1276 */
1277static int sched_autogroup_show(struct seq_file *m, void *v)
1278{
1279 struct inode *inode = m->private;
1280 struct task_struct *p;
1281
1282 p = get_proc_task(inode);
1283 if (!p)
1284 return -ESRCH;
1285 proc_sched_autogroup_show_task(p, m);
1286
1287 put_task_struct(p);
1288
1289 return 0;
1290}
1291
1292static ssize_t
1293sched_autogroup_write(struct file *file, const char __user *buf,
1294 size_t count, loff_t *offset)
1295{
1296 struct inode *inode = file->f_path.dentry->d_inode;
1297 struct task_struct *p;
1298 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001299 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001300 int err;
1301
1302 memset(buffer, 0, sizeof(buffer));
1303 if (count > sizeof(buffer) - 1)
1304 count = sizeof(buffer) - 1;
1305 if (copy_from_user(buffer, buf, count))
1306 return -EFAULT;
1307
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001308 err = kstrtoint(strstrip(buffer), 0, &nice);
1309 if (err < 0)
1310 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001311
1312 p = get_proc_task(inode);
1313 if (!p)
1314 return -ESRCH;
1315
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001316 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001317 if (err)
1318 count = err;
1319
1320 put_task_struct(p);
1321
1322 return count;
1323}
1324
1325static int sched_autogroup_open(struct inode *inode, struct file *filp)
1326{
1327 int ret;
1328
1329 ret = single_open(filp, sched_autogroup_show, NULL);
1330 if (!ret) {
1331 struct seq_file *m = filp->private_data;
1332
1333 m->private = inode;
1334 }
1335 return ret;
1336}
1337
1338static const struct file_operations proc_pid_sched_autogroup_operations = {
1339 .open = sched_autogroup_open,
1340 .read = seq_read,
1341 .write = sched_autogroup_write,
1342 .llseek = seq_lseek,
1343 .release = single_release,
1344};
1345
1346#endif /* CONFIG_SCHED_AUTOGROUP */
1347
john stultz4614a696b2009-12-14 18:00:05 -08001348static ssize_t comm_write(struct file *file, const char __user *buf,
1349 size_t count, loff_t *offset)
1350{
1351 struct inode *inode = file->f_path.dentry->d_inode;
1352 struct task_struct *p;
1353 char buffer[TASK_COMM_LEN];
1354
1355 memset(buffer, 0, sizeof(buffer));
1356 if (count > sizeof(buffer) - 1)
1357 count = sizeof(buffer) - 1;
1358 if (copy_from_user(buffer, buf, count))
1359 return -EFAULT;
1360
1361 p = get_proc_task(inode);
1362 if (!p)
1363 return -ESRCH;
1364
1365 if (same_thread_group(current, p))
1366 set_task_comm(p, buffer);
1367 else
1368 count = -EINVAL;
1369
1370 put_task_struct(p);
1371
1372 return count;
1373}
1374
1375static int comm_show(struct seq_file *m, void *v)
1376{
1377 struct inode *inode = m->private;
1378 struct task_struct *p;
1379
1380 p = get_proc_task(inode);
1381 if (!p)
1382 return -ESRCH;
1383
1384 task_lock(p);
1385 seq_printf(m, "%s\n", p->comm);
1386 task_unlock(p);
1387
1388 put_task_struct(p);
1389
1390 return 0;
1391}
1392
1393static int comm_open(struct inode *inode, struct file *filp)
1394{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001395 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001396}
1397
1398static const struct file_operations proc_pid_set_comm_operations = {
1399 .open = comm_open,
1400 .read = seq_read,
1401 .write = comm_write,
1402 .llseek = seq_lseek,
1403 .release = single_release,
1404};
1405
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001406static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001407{
1408 struct task_struct *task;
1409 struct mm_struct *mm;
1410 struct file *exe_file;
1411
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001412 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001413 if (!task)
1414 return -ENOENT;
1415 mm = get_task_mm(task);
1416 put_task_struct(task);
1417 if (!mm)
1418 return -ENOENT;
1419 exe_file = get_mm_exe_file(mm);
1420 mmput(mm);
1421 if (exe_file) {
1422 *exe_path = exe_file->f_path;
1423 path_get(&exe_file->f_path);
1424 fput(exe_file);
1425 return 0;
1426 } else
1427 return -ENOENT;
1428}
1429
Al Viro008b1502005-08-20 00:17:39 +01001430static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001433 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int error = -EACCES;
1435
Eric W. Biederman778c1142006-06-26 00:25:58 -07001436 /* Are we allowed to snoop on the tasks file descriptors? */
1437 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Christoph Hellwig408ef012012-06-18 10:47:03 -04001440 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1441 if (error)
1442 goto out;
1443
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001444 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001445 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446out:
Al Viro008b1502005-08-20 00:17:39 +01001447 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001450static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Mel Gormane12ba742007-10-16 01:25:52 -07001452 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001453 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 int len;
1455
1456 if (!tmp)
1457 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001458
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001459 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001460 len = PTR_ERR(pathname);
1461 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001463 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 if (len > buflen)
1466 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001467 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 len = -EFAULT;
1469 out:
1470 free_page((unsigned long)tmp);
1471 return len;
1472}
1473
1474static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1475{
1476 int error = -EACCES;
1477 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001478 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Eric W. Biederman778c1142006-06-26 00:25:58 -07001480 /* Are we allowed to snoop on the tasks file descriptors? */
1481 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001484 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (error)
1486 goto out;
1487
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001488 error = do_proc_readlink(&path, buffer, buflen);
1489 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return error;
1492}
1493
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001494const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001496 .follow_link = proc_pid_follow_link,
1497 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498};
1499
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001500
1501/* building an inode */
1502
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001503struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001504{
1505 struct inode * inode;
1506 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001507 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001508
1509 /* We need a new inode */
1510
1511 inode = new_inode(sb);
1512 if (!inode)
1513 goto out;
1514
1515 /* Common stuff */
1516 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001517 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001518 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001519 inode->i_op = &proc_def_inode_operations;
1520
1521 /*
1522 * grab the reference to task.
1523 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001524 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001525 if (!ei->pid)
1526 goto out_unlock;
1527
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001528 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001529 rcu_read_lock();
1530 cred = __task_cred(task);
1531 inode->i_uid = cred->euid;
1532 inode->i_gid = cred->egid;
1533 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001534 }
1535 security_task_to_inode(task, inode);
1536
1537out:
1538 return inode;
1539
1540out_unlock:
1541 iput(inode);
1542 return NULL;
1543}
1544
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001545int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001546{
1547 struct inode *inode = dentry->d_inode;
1548 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001549 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001550 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001551
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001552 generic_fillattr(inode, stat);
1553
1554 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001555 stat->uid = GLOBAL_ROOT_UID;
1556 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001557 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1558 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001559 if (!has_pid_permissions(pid, task, 2)) {
1560 rcu_read_unlock();
1561 /*
1562 * This doesn't prevent learning whether PID exists,
1563 * it only makes getattr() consistent with readdir().
1564 */
1565 return -ENOENT;
1566 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001567 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1568 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001569 cred = __task_cred(task);
1570 stat->uid = cred->euid;
1571 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001572 }
1573 }
1574 rcu_read_unlock();
1575 return 0;
1576}
1577
1578/* dentry stuff */
1579
1580/*
1581 * Exceptional case: normally we are not allowed to unhash a busy
1582 * directory. In this case, however, we can do it - no aliasing problems
1583 * due to the way we treat inodes.
1584 *
1585 * Rewrite the inode's ownerships here because the owning task may have
1586 * performed a setuid(), etc.
1587 *
1588 * Before the /proc/pid/status file was created the only way to read
1589 * the effective uid of a /process was to stat /proc/pid. Reading
1590 * /proc/pid/status is slow enough that procps and other packages
1591 * kept stating /proc/pid. To keep the rules in /proc simple I have
1592 * made this apply to all per process world readable and executable
1593 * directories.
1594 */
Al Viro0b728e12012-06-10 16:03:43 -04001595int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001596{
Nick Piggin34286d62011-01-07 17:49:57 +11001597 struct inode *inode;
1598 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001599 const struct cred *cred;
1600
Al Viro0b728e12012-06-10 16:03:43 -04001601 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001602 return -ECHILD;
1603
1604 inode = dentry->d_inode;
1605 task = get_proc_task(inode);
1606
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001607 if (task) {
1608 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1609 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001610 rcu_read_lock();
1611 cred = __task_cred(task);
1612 inode->i_uid = cred->euid;
1613 inode->i_gid = cred->egid;
1614 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001615 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001616 inode->i_uid = GLOBAL_ROOT_UID;
1617 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001618 }
1619 inode->i_mode &= ~(S_ISUID | S_ISGID);
1620 security_task_to_inode(task, inode);
1621 put_task_struct(task);
1622 return 1;
1623 }
1624 d_drop(dentry);
1625 return 0;
1626}
1627
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001628const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001629{
1630 .d_revalidate = pid_revalidate,
1631 .d_delete = pid_delete_dentry,
1632};
1633
1634/* Lookups */
1635
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001636/*
1637 * Fill a directory entry.
1638 *
1639 * If possible create the dcache entry and derive our inode number and
1640 * file type from dcache entry.
1641 *
1642 * Since all of the proc inode numbers are dynamically generated, the inode
1643 * numbers do not exist until the inode is cache. This means creating the
1644 * the dcache entry in readdir is necessary to keep the inode numbers
1645 * reported by readdir in sync with the inode numbers reported
1646 * by stat.
1647 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001648int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1649 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001650 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001651{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001652 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001653 struct inode *inode;
1654 struct qstr qname;
1655 ino_t ino = 0;
1656 unsigned type = DT_UNKNOWN;
1657
1658 qname.name = name;
1659 qname.len = len;
1660 qname.hash = full_name_hash(name, len);
1661
1662 child = d_lookup(dir, &qname);
1663 if (!child) {
1664 struct dentry *new;
1665 new = d_alloc(dir, &qname);
1666 if (new) {
1667 child = instantiate(dir->d_inode, new, task, ptr);
1668 if (child)
1669 dput(new);
1670 else
1671 child = new;
1672 }
1673 }
1674 if (!child || IS_ERR(child) || !child->d_inode)
1675 goto end_instantiate;
1676 inode = child->d_inode;
1677 if (inode) {
1678 ino = inode->i_ino;
1679 type = inode->i_mode >> 12;
1680 }
1681 dput(child);
1682end_instantiate:
1683 if (!ino)
1684 ino = find_inode_number(dir, &qname);
1685 if (!ino)
1686 ino = 1;
1687 return filldir(dirent, name, len, filp->f_pos, ino, type);
1688}
1689
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001690#ifdef CONFIG_CHECKPOINT_RESTORE
1691
1692/*
1693 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1694 * which represent vma start and end addresses.
1695 */
1696static int dname_to_vma_addr(struct dentry *dentry,
1697 unsigned long *start, unsigned long *end)
1698{
1699 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1700 return -EINVAL;
1701
1702 return 0;
1703}
1704
Al Viro0b728e12012-06-10 16:03:43 -04001705static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001706{
1707 unsigned long vm_start, vm_end;
1708 bool exact_vma_exists = false;
1709 struct mm_struct *mm = NULL;
1710 struct task_struct *task;
1711 const struct cred *cred;
1712 struct inode *inode;
1713 int status = 0;
1714
Al Viro0b728e12012-06-10 16:03:43 -04001715 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001716 return -ECHILD;
1717
1718 if (!capable(CAP_SYS_ADMIN)) {
1719 status = -EACCES;
1720 goto out_notask;
1721 }
1722
1723 inode = dentry->d_inode;
1724 task = get_proc_task(inode);
1725 if (!task)
1726 goto out_notask;
1727
Cong Wang2344bec2012-05-31 16:26:18 -07001728 mm = mm_access(task, PTRACE_MODE_READ);
1729 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001730 goto out;
1731
1732 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1733 down_read(&mm->mmap_sem);
1734 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1735 up_read(&mm->mmap_sem);
1736 }
1737
1738 mmput(mm);
1739
1740 if (exact_vma_exists) {
1741 if (task_dumpable(task)) {
1742 rcu_read_lock();
1743 cred = __task_cred(task);
1744 inode->i_uid = cred->euid;
1745 inode->i_gid = cred->egid;
1746 rcu_read_unlock();
1747 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001748 inode->i_uid = GLOBAL_ROOT_UID;
1749 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001750 }
1751 security_task_to_inode(task, inode);
1752 status = 1;
1753 }
1754
1755out:
1756 put_task_struct(task);
1757
1758out_notask:
1759 if (status <= 0)
1760 d_drop(dentry);
1761
1762 return status;
1763}
1764
1765static const struct dentry_operations tid_map_files_dentry_operations = {
1766 .d_revalidate = map_files_d_revalidate,
1767 .d_delete = pid_delete_dentry,
1768};
1769
1770static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1771{
1772 unsigned long vm_start, vm_end;
1773 struct vm_area_struct *vma;
1774 struct task_struct *task;
1775 struct mm_struct *mm;
1776 int rc;
1777
1778 rc = -ENOENT;
1779 task = get_proc_task(dentry->d_inode);
1780 if (!task)
1781 goto out;
1782
1783 mm = get_task_mm(task);
1784 put_task_struct(task);
1785 if (!mm)
1786 goto out;
1787
1788 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1789 if (rc)
1790 goto out_mmput;
1791
1792 down_read(&mm->mmap_sem);
1793 vma = find_exact_vma(mm, vm_start, vm_end);
1794 if (vma && vma->vm_file) {
1795 *path = vma->vm_file->f_path;
1796 path_get(path);
1797 rc = 0;
1798 }
1799 up_read(&mm->mmap_sem);
1800
1801out_mmput:
1802 mmput(mm);
1803out:
1804 return rc;
1805}
1806
1807struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001808 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001809 unsigned long len;
1810 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1811};
1812
1813static struct dentry *
1814proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1815 struct task_struct *task, const void *ptr)
1816{
Al Viro7b540d02012-08-27 14:55:26 -04001817 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001818 struct proc_inode *ei;
1819 struct inode *inode;
1820
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001821 inode = proc_pid_make_inode(dir->i_sb, task);
1822 if (!inode)
1823 return ERR_PTR(-ENOENT);
1824
1825 ei = PROC_I(inode);
1826 ei->op.proc_get_link = proc_map_files_get_link;
1827
1828 inode->i_op = &proc_pid_link_inode_operations;
1829 inode->i_size = 64;
1830 inode->i_mode = S_IFLNK;
1831
Al Viro7b540d02012-08-27 14:55:26 -04001832 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001833 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001834 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001835 inode->i_mode |= S_IWUSR;
1836
1837 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1838 d_add(dentry, inode);
1839
1840 return NULL;
1841}
1842
1843static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001844 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001845{
1846 unsigned long vm_start, vm_end;
1847 struct vm_area_struct *vma;
1848 struct task_struct *task;
1849 struct dentry *result;
1850 struct mm_struct *mm;
1851
1852 result = ERR_PTR(-EACCES);
1853 if (!capable(CAP_SYS_ADMIN))
1854 goto out;
1855
1856 result = ERR_PTR(-ENOENT);
1857 task = get_proc_task(dir);
1858 if (!task)
1859 goto out;
1860
1861 result = ERR_PTR(-EACCES);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001862 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001863 goto out_put_task;
1864
1865 result = ERR_PTR(-ENOENT);
1866 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001867 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001868
1869 mm = get_task_mm(task);
1870 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001871 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001872
1873 down_read(&mm->mmap_sem);
1874 vma = find_exact_vma(mm, vm_start, vm_end);
1875 if (!vma)
1876 goto out_no_vma;
1877
Al Viro7b540d02012-08-27 14:55:26 -04001878 result = proc_map_files_instantiate(dir, dentry, task,
1879 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001880
1881out_no_vma:
1882 up_read(&mm->mmap_sem);
1883 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001884out_put_task:
1885 put_task_struct(task);
1886out:
1887 return result;
1888}
1889
1890static const struct inode_operations proc_map_files_inode_operations = {
1891 .lookup = proc_map_files_lookup,
1892 .permission = proc_fd_permission,
1893 .setattr = proc_setattr,
1894};
1895
1896static int
1897proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1898{
1899 struct dentry *dentry = filp->f_path.dentry;
1900 struct inode *inode = dentry->d_inode;
1901 struct vm_area_struct *vma;
1902 struct task_struct *task;
1903 struct mm_struct *mm;
1904 ino_t ino;
1905 int ret;
1906
1907 ret = -EACCES;
1908 if (!capable(CAP_SYS_ADMIN))
1909 goto out;
1910
1911 ret = -ENOENT;
1912 task = get_proc_task(inode);
1913 if (!task)
1914 goto out;
1915
1916 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001917 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001918 goto out_put_task;
1919
1920 ret = 0;
1921 switch (filp->f_pos) {
1922 case 0:
1923 ino = inode->i_ino;
1924 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001925 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001926 filp->f_pos++;
1927 case 1:
1928 ino = parent_ino(dentry);
1929 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001930 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001931 filp->f_pos++;
1932 default:
1933 {
1934 unsigned long nr_files, pos, i;
1935 struct flex_array *fa = NULL;
1936 struct map_files_info info;
1937 struct map_files_info *p;
1938
1939 mm = get_task_mm(task);
1940 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001941 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001942 down_read(&mm->mmap_sem);
1943
1944 nr_files = 0;
1945
1946 /*
1947 * We need two passes here:
1948 *
1949 * 1) Collect vmas of mapped files with mmap_sem taken
1950 * 2) Release mmap_sem and instantiate entries
1951 *
1952 * otherwise we get lockdep complained, since filldir()
1953 * routine might require mmap_sem taken in might_fault().
1954 */
1955
1956 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1957 if (vma->vm_file && ++pos > filp->f_pos)
1958 nr_files++;
1959 }
1960
1961 if (nr_files) {
1962 fa = flex_array_alloc(sizeof(info), nr_files,
1963 GFP_KERNEL);
1964 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1965 GFP_KERNEL)) {
1966 ret = -ENOMEM;
1967 if (fa)
1968 flex_array_free(fa);
1969 up_read(&mm->mmap_sem);
1970 mmput(mm);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001971 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001972 }
1973 for (i = 0, vma = mm->mmap, pos = 2; vma;
1974 vma = vma->vm_next) {
1975 if (!vma->vm_file)
1976 continue;
1977 if (++pos <= filp->f_pos)
1978 continue;
1979
Al Viro7b540d02012-08-27 14:55:26 -04001980 info.mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001981 info.len = snprintf(info.name,
1982 sizeof(info.name), "%lx-%lx",
1983 vma->vm_start, vma->vm_end);
1984 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1985 BUG();
1986 }
1987 }
1988 up_read(&mm->mmap_sem);
1989
1990 for (i = 0; i < nr_files; i++) {
1991 p = flex_array_get(fa, i);
1992 ret = proc_fill_cache(filp, dirent, filldir,
1993 p->name, p->len,
1994 proc_map_files_instantiate,
Al Viro7b540d02012-08-27 14:55:26 -04001995 task,
1996 (void *)(unsigned long)p->mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001997 if (ret)
1998 break;
1999 filp->f_pos++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002000 }
2001 if (fa)
2002 flex_array_free(fa);
2003 mmput(mm);
2004 }
2005 }
2006
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002007out_put_task:
2008 put_task_struct(task);
2009out:
2010 return ret;
2011}
2012
2013static const struct file_operations proc_map_files_operations = {
2014 .read = generic_read_dir,
2015 .readdir = proc_map_files_readdir,
2016 .llseek = default_llseek,
2017};
2018
2019#endif /* CONFIG_CHECKPOINT_RESTORE */
2020
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002021static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002022 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002023{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002024 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002025 struct inode *inode;
2026 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002027 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002028
Eric W. Biederman61a28782006-10-02 02:18:49 -07002029 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002030 if (!inode)
2031 goto out;
2032
2033 ei = PROC_I(inode);
2034 inode->i_mode = p->mode;
2035 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002036 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002037 if (p->iop)
2038 inode->i_op = p->iop;
2039 if (p->fop)
2040 inode->i_fop = p->fop;
2041 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002042 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002043 d_add(dentry, inode);
2044 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002045 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002046 error = NULL;
2047out:
2048 return error;
2049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051static struct dentry *proc_pident_lookup(struct inode *dir,
2052 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002053 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002054 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002056 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002057 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002058 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002060 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Eric W. Biederman99f89552006-06-26 00:25:55 -07002062 if (!task)
2063 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002065 /*
2066 * Yes, it does not scale. And it should not. Don't add
2067 * new entries into /proc/<tgid>/ without very good reasons.
2068 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002069 last = &ents[nents - 1];
2070 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (p->len != dentry->d_name.len)
2072 continue;
2073 if (!memcmp(dentry->d_name.name, p->name, p->len))
2074 break;
2075 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002076 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 goto out;
2078
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002079 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002081 put_task_struct(task);
2082out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002083 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Eric Dumazetc5141e62007-05-08 00:26:15 -07002086static int proc_pident_fill_cache(struct file *filp, void *dirent,
2087 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002088{
2089 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2090 proc_pident_instantiate, task, p);
2091}
2092
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002093static int proc_pident_readdir(struct file *filp,
2094 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002095 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002096{
2097 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002098 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002099 struct inode *inode = dentry->d_inode;
2100 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002101 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002102 ino_t ino;
2103 int ret;
2104
2105 ret = -ENOENT;
2106 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002107 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002108
2109 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002110 i = filp->f_pos;
2111 switch (i) {
2112 case 0:
2113 ino = inode->i_ino;
2114 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2115 goto out;
2116 i++;
2117 filp->f_pos++;
2118 /* fall through */
2119 case 1:
2120 ino = parent_ino(dentry);
2121 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2122 goto out;
2123 i++;
2124 filp->f_pos++;
2125 /* fall through */
2126 default:
2127 i -= 2;
2128 if (i >= nents) {
2129 ret = 1;
2130 goto out;
2131 }
2132 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002133 last = &ents[nents - 1];
2134 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002135 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002136 goto out;
2137 filp->f_pos++;
2138 p++;
2139 }
2140 }
2141
2142 ret = 1;
2143out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002144 put_task_struct(task);
2145out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002150static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2151 size_t count, loff_t *ppos)
2152{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002153 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002154 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002155 ssize_t length;
2156 struct task_struct *task = get_proc_task(inode);
2157
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002158 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002159 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002160
2161 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002162 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002163 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002164 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002165 if (length > 0)
2166 length = simple_read_from_buffer(buf, count, ppos, p, length);
2167 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002168 return length;
2169}
2170
2171static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2172 size_t count, loff_t *ppos)
2173{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002174 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002175 char *page;
2176 ssize_t length;
2177 struct task_struct *task = get_proc_task(inode);
2178
2179 length = -ESRCH;
2180 if (!task)
2181 goto out_no_task;
2182 if (count > PAGE_SIZE)
2183 count = PAGE_SIZE;
2184
2185 /* No partial writes. */
2186 length = -EINVAL;
2187 if (*ppos != 0)
2188 goto out;
2189
2190 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002191 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002192 if (!page)
2193 goto out;
2194
2195 length = -EFAULT;
2196 if (copy_from_user(page, buf, count))
2197 goto out_free;
2198
David Howells107db7c2009-05-08 13:55:27 +01002199 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002200 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002201 if (length < 0)
2202 goto out_free;
2203
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002204 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002205 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002206 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002207 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002208out_free:
2209 free_page((unsigned long) page);
2210out:
2211 put_task_struct(task);
2212out_no_task:
2213 return length;
2214}
2215
Arjan van de Ven00977a52007-02-12 00:55:34 -08002216static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002217 .read = proc_pid_attr_read,
2218 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002219 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002220};
2221
Eric Dumazetc5141e62007-05-08 00:26:15 -07002222static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002223 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2224 REG("prev", S_IRUGO, proc_pid_attr_operations),
2225 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2226 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2227 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2228 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002229};
2230
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002231static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 void * dirent, filldir_t filldir)
2233{
2234 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002235 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Arjan van de Ven00977a52007-02-12 00:55:34 -08002238static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002240 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002241 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242};
2243
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002244static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002245 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002247 return proc_pident_lookup(dir, dentry,
2248 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002251static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002252 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002253 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002254 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255};
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257#endif
2258
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002259#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002260static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2261 size_t count, loff_t *ppos)
2262{
2263 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2264 struct mm_struct *mm;
2265 char buffer[PROC_NUMBUF];
2266 size_t len;
2267 int ret;
2268
2269 if (!task)
2270 return -ESRCH;
2271
2272 ret = 0;
2273 mm = get_task_mm(task);
2274 if (mm) {
2275 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2276 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2277 MMF_DUMP_FILTER_SHIFT));
2278 mmput(mm);
2279 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2280 }
2281
2282 put_task_struct(task);
2283
2284 return ret;
2285}
2286
2287static ssize_t proc_coredump_filter_write(struct file *file,
2288 const char __user *buf,
2289 size_t count,
2290 loff_t *ppos)
2291{
2292 struct task_struct *task;
2293 struct mm_struct *mm;
2294 char buffer[PROC_NUMBUF], *end;
2295 unsigned int val;
2296 int ret;
2297 int i;
2298 unsigned long mask;
2299
2300 ret = -EFAULT;
2301 memset(buffer, 0, sizeof(buffer));
2302 if (count > sizeof(buffer) - 1)
2303 count = sizeof(buffer) - 1;
2304 if (copy_from_user(buffer, buf, count))
2305 goto out_no_task;
2306
2307 ret = -EINVAL;
2308 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2309 if (*end == '\n')
2310 end++;
2311 if (end - buffer == 0)
2312 goto out_no_task;
2313
2314 ret = -ESRCH;
2315 task = get_proc_task(file->f_dentry->d_inode);
2316 if (!task)
2317 goto out_no_task;
2318
2319 ret = end - buffer;
2320 mm = get_task_mm(task);
2321 if (!mm)
2322 goto out_no_mm;
2323
2324 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2325 if (val & mask)
2326 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2327 else
2328 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2329 }
2330
2331 mmput(mm);
2332 out_no_mm:
2333 put_task_struct(task);
2334 out_no_task:
2335 return ret;
2336}
2337
2338static const struct file_operations proc_coredump_filter_operations = {
2339 .read = proc_coredump_filter_read,
2340 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002341 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002342};
2343#endif
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345/*
2346 * /proc/self:
2347 */
2348static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2349 int buflen)
2350{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002351 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002352 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002353 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002354 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002355 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002356 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return vfs_readlink(dentry,buffer,buflen,tmp);
2358}
2359
Al Viro008b1502005-08-20 00:17:39 +01002360static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002362 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002363 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002364 char *name = ERR_PTR(-ENOENT);
2365 if (tgid) {
2366 name = __getname();
2367 if (!name)
2368 name = ERR_PTR(-ENOMEM);
2369 else
2370 sprintf(name, "%d", tgid);
2371 }
2372 nd_set_link(nd, name);
2373 return NULL;
2374}
2375
2376static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2377 void *cookie)
2378{
2379 char *s = nd_get_link(nd);
2380 if (!IS_ERR(s))
2381 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002384static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 .readlink = proc_self_readlink,
2386 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002387 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388};
2389
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002390/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002391 * proc base
2392 *
2393 * These are the directory entries in the root directory of /proc
2394 * that properly belong to the /proc filesystem, as they describe
2395 * describe something that is process related.
2396 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002397static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002398 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002399 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002400};
2401
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002402static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002403 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002404{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002405 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002406 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002407 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002408 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002409
2410 /* Allocate the inode */
2411 error = ERR_PTR(-ENOMEM);
2412 inode = new_inode(dir->i_sb);
2413 if (!inode)
2414 goto out;
2415
2416 /* Initialize the inode */
2417 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002418 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002419 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002420
2421 /*
2422 * grab the reference to the task.
2423 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002424 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002425 if (!ei->pid)
2426 goto out_iput;
2427
Eric W. Biederman801199c2006-10-02 02:18:48 -07002428 inode->i_mode = p->mode;
2429 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002430 set_nlink(inode, 2);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002431 if (S_ISLNK(inode->i_mode))
2432 inode->i_size = 64;
2433 if (p->iop)
2434 inode->i_op = p->iop;
2435 if (p->fop)
2436 inode->i_fop = p->fop;
2437 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002438 d_add(dentry, inode);
2439 error = NULL;
2440out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002441 return error;
2442out_iput:
2443 iput(inode);
2444 goto out;
2445}
2446
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002447static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2448{
2449 struct dentry *error;
2450 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002451 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002452
2453 error = ERR_PTR(-ENOENT);
2454
2455 if (!task)
2456 goto out_no_task;
2457
2458 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002459 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2460 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002461 if (p->len != dentry->d_name.len)
2462 continue;
2463 if (!memcmp(dentry->d_name.name, p->name, p->len))
2464 break;
2465 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002466 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002467 goto out;
2468
2469 error = proc_base_instantiate(dir, dentry, task, p);
2470
2471out:
2472 put_task_struct(task);
2473out_no_task:
2474 return error;
2475}
2476
Eric Dumazetc5141e62007-05-08 00:26:15 -07002477static int proc_base_fill_cache(struct file *filp, void *dirent,
2478 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002479{
2480 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2481 proc_base_instantiate, task, p);
2482}
2483
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002484#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002485static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002486{
Andrea Righi940389b2008-07-28 00:48:12 +02002487 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002488 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002489 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002490
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002491 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2492 if (result)
2493 return result;
2494
2495 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2496 result = -EACCES;
2497 goto out_unlock;
2498 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002499
Andrea Righi59954772008-07-27 17:29:15 +02002500 if (whole && lock_task_sighand(task, &flags)) {
2501 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002502
Andrea Righi59954772008-07-27 17:29:15 +02002503 task_io_accounting_add(&acct, &task->signal->ioac);
2504 while_each_thread(task, t)
2505 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002506
Andrea Righi59954772008-07-27 17:29:15 +02002507 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002508 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002509 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002510 "rchar: %llu\n"
2511 "wchar: %llu\n"
2512 "syscr: %llu\n"
2513 "syscw: %llu\n"
2514 "read_bytes: %llu\n"
2515 "write_bytes: %llu\n"
2516 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002517 (unsigned long long)acct.rchar,
2518 (unsigned long long)acct.wchar,
2519 (unsigned long long)acct.syscr,
2520 (unsigned long long)acct.syscw,
2521 (unsigned long long)acct.read_bytes,
2522 (unsigned long long)acct.write_bytes,
2523 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002524out_unlock:
2525 mutex_unlock(&task->signal->cred_guard_mutex);
2526 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002527}
Andrea Righi297c5d92008-07-25 01:48:49 -07002528
2529static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2530{
2531 return do_io_accounting(task, buffer, 0);
2532}
2533
2534static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2535{
2536 return do_io_accounting(task, buffer, 1);
2537}
2538#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002539
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002540#ifdef CONFIG_USER_NS
2541static int proc_id_map_open(struct inode *inode, struct file *file,
2542 struct seq_operations *seq_ops)
2543{
2544 struct user_namespace *ns = NULL;
2545 struct task_struct *task;
2546 struct seq_file *seq;
2547 int ret = -EINVAL;
2548
2549 task = get_proc_task(inode);
2550 if (task) {
2551 rcu_read_lock();
2552 ns = get_user_ns(task_cred_xxx(task, user_ns));
2553 rcu_read_unlock();
2554 put_task_struct(task);
2555 }
2556 if (!ns)
2557 goto err;
2558
2559 ret = seq_open(file, seq_ops);
2560 if (ret)
2561 goto err_put_ns;
2562
2563 seq = file->private_data;
2564 seq->private = ns;
2565
2566 return 0;
2567err_put_ns:
2568 put_user_ns(ns);
2569err:
2570 return ret;
2571}
2572
2573static int proc_id_map_release(struct inode *inode, struct file *file)
2574{
2575 struct seq_file *seq = file->private_data;
2576 struct user_namespace *ns = seq->private;
2577 put_user_ns(ns);
2578 return seq_release(inode, file);
2579}
2580
2581static int proc_uid_map_open(struct inode *inode, struct file *file)
2582{
2583 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2584}
2585
2586static int proc_gid_map_open(struct inode *inode, struct file *file)
2587{
2588 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2589}
2590
2591static const struct file_operations proc_uid_map_operations = {
2592 .open = proc_uid_map_open,
2593 .write = proc_uid_map_write,
2594 .read = seq_read,
2595 .llseek = seq_lseek,
2596 .release = proc_id_map_release,
2597};
2598
2599static const struct file_operations proc_gid_map_operations = {
2600 .open = proc_gid_map_open,
2601 .write = proc_gid_map_write,
2602 .read = seq_read,
2603 .llseek = seq_lseek,
2604 .release = proc_id_map_release,
2605};
2606#endif /* CONFIG_USER_NS */
2607
Kees Cook47830722008-10-06 03:11:58 +04002608static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2609 struct pid *pid, struct task_struct *task)
2610{
Al Viroa9712bc2011-03-23 15:52:50 -04002611 int err = lock_trace(task);
2612 if (!err) {
2613 seq_printf(m, "%08x\n", task->personality);
2614 unlock_trace(task);
2615 }
2616 return err;
Kees Cook47830722008-10-06 03:11:58 +04002617}
2618
Eric W. Biederman801199c2006-10-02 02:18:48 -07002619/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002620 * Thread groups
2621 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002622static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002623static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002624
Eric Dumazetc5141e62007-05-08 00:26:15 -07002625static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002626 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2627 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002628#ifdef CONFIG_CHECKPOINT_RESTORE
2629 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2630#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002631 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002632 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002633#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002634 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002635#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002636 REG("environ", S_IRUSR, proc_environ_operations),
2637 INF("auxv", S_IRUSR, proc_pid_auxv),
2638 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002639 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002640 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002641#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002642 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002643#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002644#ifdef CONFIG_SCHED_AUTOGROUP
2645 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2646#endif
john stultz4614a696b2009-12-14 18:00:05 -08002647 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002648#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002649 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002650#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002651 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2652 ONE("stat", S_IRUGO, proc_tgid_stat),
2653 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002654 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002655#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002656 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002658 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2659 LNK("cwd", proc_cwd_link),
2660 LNK("root", proc_root_link),
2661 LNK("exe", proc_exe_link),
2662 REG("mounts", S_IRUGO, proc_mounts_operations),
2663 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2664 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002665#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002666 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002667 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002668 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002669#endif
2670#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002671 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002672#endif
2673#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002674 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002675#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002676#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002677 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002678#endif
2679#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002680 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002681#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002682#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002683 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002684#endif
Paul Menage8793d852007-10-18 23:39:39 -07002685#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002686 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002687#endif
Paul Menagea4243162007-10-18 23:39:35 -07002688#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002689 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002690#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002691 INF("oom_score", S_IRUGO, proc_oom_score),
2692 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002693 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002694#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002695 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2696 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002697#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002698#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002699 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002700#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002701#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002702 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002703#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002704#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002705 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002706#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002707#ifdef CONFIG_HARDWALL
2708 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2709#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002710#ifdef CONFIG_USER_NS
2711 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2712 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2713#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002714};
2715
2716static int proc_tgid_base_readdir(struct file * filp,
2717 void * dirent, filldir_t filldir)
2718{
2719 return proc_pident_readdir(filp,dirent,filldir,
2720 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2721}
2722
Arjan van de Ven00977a52007-02-12 00:55:34 -08002723static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002724 .read = generic_read_dir,
2725 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002726 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002727};
2728
Al Viro00cd8dd2012-06-10 17:13:09 -04002729static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2730{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002731 return proc_pident_lookup(dir, dentry,
2732 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002733}
2734
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002735static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002736 .lookup = proc_tgid_base_lookup,
2737 .getattr = pid_getattr,
2738 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002739 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002740};
2741
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002742static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002744 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002745 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002746 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Eric W. Biederman48e64842006-06-26 00:25:48 -07002748 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002749 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2750 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002751 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002752 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002753 d_drop(dentry);
2754 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Eric W. Biederman48e64842006-06-26 00:25:48 -07002757 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002758 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2759 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002760 if (!leader)
2761 goto out;
2762
2763 name.name = "task";
2764 name.len = strlen(name.name);
2765 dir = d_hash_and_lookup(leader, &name);
2766 if (!dir)
2767 goto out_put_leader;
2768
2769 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002770 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002771 dentry = d_hash_and_lookup(dir, &name);
2772 if (dentry) {
2773 shrink_dcache_parent(dentry);
2774 d_drop(dentry);
2775 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002777
2778 dput(dir);
2779out_put_leader:
2780 dput(leader);
2781out:
2782 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
2784
Randy Dunlap0895e912007-10-21 21:00:10 -07002785/**
2786 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2787 * @task: task that should be flushed.
2788 *
2789 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002790 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002791 * in. This call is supposed to do all of this job.
2792 *
2793 * Looks in the dcache for
2794 * /proc/@pid
2795 * /proc/@tgid/task/@pid
2796 * if either directory is present flushes it and all of it'ts children
2797 * from the dcache.
2798 *
2799 * It is safe and reasonable to cache /proc entries for a task until
2800 * that task exits. After that they just clog up the dcache with
2801 * useless entries, possibly causing useful dcache entries to be
2802 * flushed instead. This routine is proved to flush those useless
2803 * dcache entries at process exit time.
2804 *
2805 * NOTE: This routine is just an optimization so it does not guarantee
2806 * that no dcache entries will exist at process exit time it
2807 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002808 */
2809
2810void proc_flush_task(struct task_struct *task)
2811{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002812 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002813 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002814 struct upid *upid;
2815
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002816 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002817 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002818
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002819 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002820 upid = &pid->numbers[i];
2821 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002822 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002823 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002824
2825 upid = &pid->numbers[pid->level];
2826 if (upid->nr == 1)
2827 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002828}
2829
Adrian Bunk9711ef92006-12-06 20:38:31 -08002830static struct dentry *proc_pid_instantiate(struct inode *dir,
2831 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002832 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002833{
2834 struct dentry *error = ERR_PTR(-ENOENT);
2835 struct inode *inode;
2836
Eric W. Biederman61a28782006-10-02 02:18:49 -07002837 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002838 if (!inode)
2839 goto out;
2840
2841 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2842 inode->i_op = &proc_tgid_base_inode_operations;
2843 inode->i_fop = &proc_tgid_base_operations;
2844 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002845
Miklos Szeredibfe86842011-10-28 14:13:29 +02002846 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2847 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002848
Nick Pigginfb045ad2011-01-07 17:49:55 +11002849 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002850
2851 d_add(dentry, inode);
2852 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002853 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002854 error = NULL;
2855out:
2856 return error;
2857}
2858
Al Viro00cd8dd2012-06-10 17:13:09 -04002859struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Dan Carpenter73d36462010-05-26 14:43:25 -07002861 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002864 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Eric W. Biederman801199c2006-10-02 02:18:48 -07002866 result = proc_base_lookup(dir, dentry);
2867 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2868 goto out;
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 tgid = name_to_int(dentry);
2871 if (tgid == ~0U)
2872 goto out;
2873
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002874 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002875 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002876 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (task)
2878 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002879 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (!task)
2881 goto out;
2882
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002883 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002884 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002886 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002890 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002891 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002893struct tgid_iter {
2894 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002895 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002896};
2897static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2898{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002899 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002901 if (iter.task)
2902 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002903 rcu_read_lock();
2904retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002905 iter.task = NULL;
2906 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002907 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002908 iter.tgid = pid_nr_ns(pid, ns);
2909 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002910 /* What we to know is if the pid we have find is the
2911 * pid of a thread_group_leader. Testing for task
2912 * being a thread_group_leader is the obvious thing
2913 * todo but there is a window when it fails, due to
2914 * the pid transfer logic in de_thread.
2915 *
2916 * So we perform the straight forward test of seeing
2917 * if the pid we have found is the pid of a thread
2918 * group leader, and don't worry if the task we have
2919 * found doesn't happen to be a thread group leader.
2920 * As we don't care in the case of readdir.
2921 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002922 if (!iter.task || !has_group_leader_pid(iter.task)) {
2923 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002924 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002925 }
2926 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002928 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002929 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002932#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Eric W. Biederman61a28782006-10-02 02:18:49 -07002934static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002935 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002936{
2937 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002938 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002939 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002940 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002941}
2942
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002943static int fake_filldir(void *buf, const char *name, int namelen,
2944 loff_t offset, u64 ino, unsigned d_type)
2945{
2946 return 0;
2947}
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949/* for the /proc/ directory itself, after non-process stuff has been done */
2950int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2951{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07002952 unsigned int nr;
2953 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002954 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002955 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002956 filldir_t __filldir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07002958 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
2959 goto out_no_task;
2960 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2961
2962 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002963 if (!reaper)
2964 goto out_no_task;
2965
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002966 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002967 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002968 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002969 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
2971
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002972 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002973 iter.task = NULL;
2974 iter.tgid = filp->f_pos - TGID_OFFSET;
2975 for (iter = next_tgid(ns, iter);
2976 iter.task;
2977 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002978 if (has_pid_permissions(ns, iter.task, 2))
2979 __filldir = filldir;
2980 else
2981 __filldir = fake_filldir;
2982
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002983 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002984 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002985 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002986 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002989 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2990out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002991 put_task_struct(reaper);
2992out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 return 0;
2994}
2995
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002996/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002997 * Tasks
2998 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002999static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003000 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003001 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003002 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003003 REG("environ", S_IRUSR, proc_environ_operations),
3004 INF("auxv", S_IRUSR, proc_pid_auxv),
3005 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003006 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003007 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003008#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003009 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003010#endif
john stultz4614a696b2009-12-14 18:00:05 -08003011 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003012#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003013 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003014#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003015 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3016 ONE("stat", S_IRUGO, proc_tid_stat),
3017 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003018 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003019#ifdef CONFIG_CHECKPOINT_RESTORE
3020 REG("children", S_IRUGO, proc_tid_children_operations),
3021#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003022#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003023 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003024#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003025 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3026 LNK("cwd", proc_cwd_link),
3027 LNK("root", proc_root_link),
3028 LNK("exe", proc_exe_link),
3029 REG("mounts", S_IRUGO, proc_mounts_operations),
3030 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003031#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003032 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003033 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003034 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003035#endif
3036#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003037 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003038#endif
3039#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003040 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003041#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003042#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003043 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003044#endif
3045#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003046 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003047#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003048#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003049 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003050#endif
Paul Menage8793d852007-10-18 23:39:39 -07003051#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003052 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003053#endif
Paul Menagea4243162007-10-18 23:39:35 -07003054#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003055 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003056#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003057 INF("oom_score", S_IRUGO, proc_oom_score),
3058 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003059 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003060#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003061 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003062 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003063#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003064#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003065 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003066#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003067#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003068 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003069#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003070#ifdef CONFIG_HARDWALL
3071 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3072#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003073#ifdef CONFIG_USER_NS
3074 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3075 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3076#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003077};
3078
3079static int proc_tid_base_readdir(struct file * filp,
3080 void * dirent, filldir_t filldir)
3081{
3082 return proc_pident_readdir(filp,dirent,filldir,
3083 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3084}
3085
Al Viro00cd8dd2012-06-10 17:13:09 -04003086static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3087{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003088 return proc_pident_lookup(dir, dentry,
3089 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003090}
3091
Arjan van de Ven00977a52007-02-12 00:55:34 -08003092static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003093 .read = generic_read_dir,
3094 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003095 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003096};
3097
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003098static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003099 .lookup = proc_tid_base_lookup,
3100 .getattr = pid_getattr,
3101 .setattr = proc_setattr,
3102};
3103
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003104static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003105 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003106{
3107 struct dentry *error = ERR_PTR(-ENOENT);
3108 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003109 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003110
3111 if (!inode)
3112 goto out;
3113 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3114 inode->i_op = &proc_tid_base_inode_operations;
3115 inode->i_fop = &proc_tid_base_operations;
3116 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003117
Miklos Szeredibfe86842011-10-28 14:13:29 +02003118 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3119 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003120
Nick Pigginfb045ad2011-01-07 17:49:55 +11003121 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003122
3123 d_add(dentry, inode);
3124 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003125 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003126 error = NULL;
3127out:
3128 return error;
3129}
3130
Al Viro00cd8dd2012-06-10 17:13:09 -04003131static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003132{
3133 struct dentry *result = ERR_PTR(-ENOENT);
3134 struct task_struct *task;
3135 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003136 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003137 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003138
3139 if (!leader)
3140 goto out_no_task;
3141
3142 tid = name_to_int(dentry);
3143 if (tid == ~0U)
3144 goto out;
3145
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003146 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003147 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003148 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003149 if (task)
3150 get_task_struct(task);
3151 rcu_read_unlock();
3152 if (!task)
3153 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003154 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003155 goto out_drop_task;
3156
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003157 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003158out_drop_task:
3159 put_task_struct(task);
3160out:
3161 put_task_struct(leader);
3162out_no_task:
3163 return result;
3164}
3165
3166/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003167 * Find the first tid of a thread group to return to user space.
3168 *
3169 * Usually this is just the thread group leader, but if the users
3170 * buffer was too small or there was a seek into the middle of the
3171 * directory we have more work todo.
3172 *
3173 * In the case of a short read we start with find_task_by_pid.
3174 *
3175 * In the case of a seek we start with the leader and walk nr
3176 * threads past it.
3177 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003178static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003179 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003180{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003181 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003182
Eric W. Biedermancc288732006-06-26 00:26:01 -07003183 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003184 /* Attempt to start with the pid of a thread */
3185 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003186 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003187 if (pos && (pos->group_leader == leader))
3188 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003189 }
3190
3191 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003192 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003193 if (nr && nr >= get_nr_threads(leader))
3194 goto out;
3195
3196 /* If we haven't found our starting place yet start
3197 * with the leader and walk nr threads forward.
3198 */
3199 for (pos = leader; nr > 0; --nr) {
3200 pos = next_thread(pos);
3201 if (pos == leader) {
3202 pos = NULL;
3203 goto out;
3204 }
3205 }
3206found:
3207 get_task_struct(pos);
3208out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003209 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003210 return pos;
3211}
3212
3213/*
3214 * Find the next thread in the thread list.
3215 * Return NULL if there is an error or no next thread.
3216 *
3217 * The reference to the input task_struct is released.
3218 */
3219static struct task_struct *next_tid(struct task_struct *start)
3220{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003221 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003222 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003223 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003224 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003225 if (thread_group_leader(pos))
3226 pos = NULL;
3227 else
3228 get_task_struct(pos);
3229 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003230 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003231 put_task_struct(start);
3232 return pos;
3233}
3234
Eric W. Biederman61a28782006-10-02 02:18:49 -07003235static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3236 struct task_struct *task, int tid)
3237{
3238 char name[PROC_NUMBUF];
3239 int len = snprintf(name, sizeof(name), "%d", tid);
3240 return proc_fill_cache(filp, dirent, filldir, name, len,
3241 proc_task_instantiate, task, NULL);
3242}
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244/* for the /proc/TGID/task/ directories */
3245static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3246{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003247 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003249 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003250 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 int retval = -ENOENT;
3252 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003253 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003254 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003256 task = get_proc_task(inode);
3257 if (!task)
3258 goto out_no_task;
3259 rcu_read_lock();
3260 if (pid_alive(task)) {
3261 leader = task->group_leader;
3262 get_task_struct(leader);
3263 }
3264 rcu_read_unlock();
3265 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003266 if (!leader)
3267 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 retval = 0;
3269
Linus Torvaldsee568b22009-03-17 10:02:35 -07003270 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 case 0:
3272 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003273 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003275 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 /* fall through */
3277 case 1:
3278 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003279 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003281 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 /* fall through */
3283 }
3284
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003285 /* f_version caches the tgid value that the last readdir call couldn't
3286 * return. lseek aka telldir automagically resets f_version to 0.
3287 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003288 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003289 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003290 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003291 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003292 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003293 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003294 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003295 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003296 /* returning this tgid failed, save it as the first
3297 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003298 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003299 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003304 put_task_struct(leader);
3305out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return retval;
3307}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003308
3309static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3310{
3311 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003312 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003313 generic_fillattr(inode, stat);
3314
Eric W. Biederman99f89552006-06-26 00:25:55 -07003315 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003316 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003317 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003318 }
3319
3320 return 0;
3321}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003322
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003323static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003324 .lookup = proc_task_lookup,
3325 .getattr = proc_task_getattr,
3326 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003327 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003328};
3329
Arjan van de Ven00977a52007-02-12 00:55:34 -08003330static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003331 .read = generic_read_dir,
3332 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003333 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003334};