blob: 9e28356a959a2f4f0aecddd6170ebacfa799a5b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070076#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/cpuset.h>
78#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050079#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070080#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070081#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070082#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070083#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080084#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040085#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090086#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080087#include <linux/flex_array.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040088#ifdef CONFIG_HARDWALL
89#include <asm/hardwall.h>
90#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080091#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040093#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070095/* NOTE:
96 * Implementing inode permission operations in /proc is almost
97 * certainly an error. Permission checks need to happen during
98 * each system call not at open time. The reason is that most of
99 * what we wish to check for permissions in /proc varies at runtime.
100 *
101 * The classic example of a problem is opening file descriptors
102 * in /proc for a task before it execs a suid executable.
103 */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700107 int len;
Al Virod161a132011-07-24 03:36:29 -0400108 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800109 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800110 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700111 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Eric W. Biederman61a28782006-10-02 02:18:49 -0700114#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700116 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 .mode = MODE, \
118 .iop = IOP, \
119 .fop = FOP, \
120 .op = OP, \
121}
122
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300123#define DIR(NAME, MODE, iops, fops) \
124 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
125#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700126 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700127 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300128 { .proc_get_link = get_link } )
129#define REG(NAME, MODE, fops) \
130 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
131#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700132 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700133 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300134 { .proc_read = read } )
135#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800136 NOD(NAME, (S_IFREG|(MODE)), \
137 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300138 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Vegard Nossumaed54172008-06-05 22:46:53 -0700140/*
141 * Count the number of hardlinks for the pid_entry table, excluding the .
142 * and .. links.
143 */
144static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145 unsigned int n)
146{
147 unsigned int i;
148 unsigned int count;
149
150 count = 0;
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
153 ++count;
154 }
155
156 return count;
157}
158
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200159static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000161 int result = -ENOENT;
162
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700163 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200164 if (task->fs) {
165 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000166 result = 0;
167 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000169 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170}
171
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800172static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700173{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800174 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176
177 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200178 task_lock(task);
179 if (task->fs) {
180 get_fs_pwd(task->fs, path);
181 result = 0;
182 }
183 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700184 put_task_struct(task);
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return result;
187}
188
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800189static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800191 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700193
194 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200195 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700196 put_task_struct(task);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return result;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int proc_pid_cmdline(struct task_struct *task, char * buffer)
202{
203 int res = 0;
204 unsigned int len;
205 struct mm_struct *mm = get_task_mm(task);
206 if (!mm)
207 goto out;
208 if (!mm->arg_end)
209 goto out_mm; /* Shh! No looking before we're done */
210
211 len = mm->arg_end - mm->arg_start;
212
213 if (len > PAGE_SIZE)
214 len = PAGE_SIZE;
215
216 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
217
218 // If the nul at the end of args has been overwritten, then
219 // assume application is using setproctitle(3).
220 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
221 len = strnlen(buffer, res);
222 if (len < res) {
223 res = len;
224 } else {
225 len = mm->env_end - mm->env_start;
226 if (len > PAGE_SIZE - res)
227 len = PAGE_SIZE - res;
228 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
229 res = strnlen(buffer, res);
230 }
231 }
232out_mm:
233 mmput(mm);
234out:
235 return res;
236}
237
238static int proc_pid_auxv(struct task_struct *task, char *buffer)
239{
Cong Wange7dcd992012-05-31 16:26:17 -0700240 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500241 int res = PTR_ERR(mm);
242 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300244 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300246 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 res = nwords * sizeof(mm->saved_auxv[0]);
248 if (res > PAGE_SIZE)
249 res = PAGE_SIZE;
250 memcpy(buffer, mm->saved_auxv, res);
251 mmput(mm);
252 }
253 return res;
254}
255
256
257#ifdef CONFIG_KALLSYMS
258/*
259 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
260 * Returns the resolved symbol. If that fails, simply return the address.
261 */
262static int proc_pid_wchan(struct task_struct *task, char *buffer)
263{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700264 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700265 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 wchan = get_wchan(task);
268
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700269 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600270 if (!ptrace_may_access(task, PTRACE_MODE_READ))
271 return 0;
272 else
273 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700274 else
275 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277#endif /* CONFIG_KALLSYMS */
278
Al Viroa9712bc2011-03-23 15:52:50 -0400279static int lock_trace(struct task_struct *task)
280{
281 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
282 if (err)
283 return err;
284 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
285 mutex_unlock(&task->signal->cred_guard_mutex);
286 return -EPERM;
287 }
288 return 0;
289}
290
291static void unlock_trace(struct task_struct *task)
292{
293 mutex_unlock(&task->signal->cred_guard_mutex);
294}
295
Ken Chen2ec220e2008-11-10 11:26:08 +0300296#ifdef CONFIG_STACKTRACE
297
298#define MAX_STACK_TRACE_DEPTH 64
299
300static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
301 struct pid *pid, struct task_struct *task)
302{
303 struct stack_trace trace;
304 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400305 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300306 int i;
307
308 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
309 if (!entries)
310 return -ENOMEM;
311
312 trace.nr_entries = 0;
313 trace.max_entries = MAX_STACK_TRACE_DEPTH;
314 trace.entries = entries;
315 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300316
Al Viroa9712bc2011-03-23 15:52:50 -0400317 err = lock_trace(task);
318 if (!err) {
319 save_stack_trace_tsk(task, &trace);
320
321 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700322 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400323 (void *)entries[i], (void *)entries[i]);
324 }
325 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300326 }
327 kfree(entries);
328
Al Viroa9712bc2011-03-23 15:52:50 -0400329 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300330}
331#endif
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#ifdef CONFIG_SCHEDSTATS
334/*
335 * Provides /proc/PID/schedstat
336 */
337static int proc_pid_schedstat(struct task_struct *task, char *buffer)
338{
Balbir Singh172ba842007-07-09 18:52:00 +0200339 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100340 (unsigned long long)task->se.sum_exec_runtime,
341 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200342 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344#endif
345
Arjan van de Ven97455122008-01-25 21:08:34 +0100346#ifdef CONFIG_LATENCYTOP
347static int lstats_show_proc(struct seq_file *m, void *v)
348{
349 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800350 struct inode *inode = m->private;
351 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100352
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800353 if (!task)
354 return -ESRCH;
355 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100356 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800357 struct latency_record *lr = &task->latency_record[i];
358 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100359 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800360 seq_printf(m, "%i %li %li",
361 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100362 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800363 unsigned long bt = lr->backtrace[q];
364 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100365 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800366 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100367 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800368 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100369 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800370 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100371 }
372
373 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800374 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100375 return 0;
376}
377
378static int lstats_open(struct inode *inode, struct file *file)
379{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800380 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800381}
382
Arjan van de Ven97455122008-01-25 21:08:34 +0100383static ssize_t lstats_write(struct file *file, const char __user *buf,
384 size_t count, loff_t *offs)
385{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800386 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100387
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800388 if (!task)
389 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100390 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800391 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100392
393 return count;
394}
395
396static const struct file_operations proc_lstats_operations = {
397 .open = lstats_open,
398 .read = seq_read,
399 .write = lstats_write,
400 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800401 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100402};
403
404#endif
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static int proc_oom_score(struct task_struct *task, char *buffer)
407{
David Rientjesa7f638f2012-05-29 15:06:47 -0700408 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200409 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700411 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200412 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700413 points = oom_badness(task, NULL, NULL, totalpages) *
414 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700415 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return sprintf(buffer, "%lu\n", points);
417}
418
Neil Hormand85f50d2007-10-18 23:40:37 -0700419struct limit_names {
420 char *name;
421 char *unit;
422};
423
424static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700425 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700426 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
427 [RLIMIT_DATA] = {"Max data size", "bytes"},
428 [RLIMIT_STACK] = {"Max stack size", "bytes"},
429 [RLIMIT_CORE] = {"Max core file size", "bytes"},
430 [RLIMIT_RSS] = {"Max resident set", "bytes"},
431 [RLIMIT_NPROC] = {"Max processes", "processes"},
432 [RLIMIT_NOFILE] = {"Max open files", "files"},
433 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
434 [RLIMIT_AS] = {"Max address space", "bytes"},
435 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
436 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
437 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
438 [RLIMIT_NICE] = {"Max nice priority", NULL},
439 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800440 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700441};
442
443/* Display limits for a process */
444static int proc_pid_limits(struct task_struct *task, char *buffer)
445{
446 unsigned int i;
447 int count = 0;
448 unsigned long flags;
449 char *bufptr = buffer;
450
451 struct rlimit rlim[RLIM_NLIMITS];
452
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400453 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700454 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700455 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
456 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700457
458 /*
459 * print the file header
460 */
461 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
462 "Limit", "Soft Limit", "Hard Limit", "Units");
463
464 for (i = 0; i < RLIM_NLIMITS; i++) {
465 if (rlim[i].rlim_cur == RLIM_INFINITY)
466 count += sprintf(&bufptr[count], "%-25s %-20s ",
467 lnames[i].name, "unlimited");
468 else
469 count += sprintf(&bufptr[count], "%-25s %-20lu ",
470 lnames[i].name, rlim[i].rlim_cur);
471
472 if (rlim[i].rlim_max == RLIM_INFINITY)
473 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
474 else
475 count += sprintf(&bufptr[count], "%-20lu ",
476 rlim[i].rlim_max);
477
478 if (lnames[i].unit)
479 count += sprintf(&bufptr[count], "%-10s\n",
480 lnames[i].unit);
481 else
482 count += sprintf(&bufptr[count], "\n");
483 }
484
485 return count;
486}
487
Roland McGrathebcb6732008-07-25 19:46:00 -0700488#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
489static int proc_pid_syscall(struct task_struct *task, char *buffer)
490{
491 long nr;
492 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400493 int res = lock_trace(task);
494 if (res)
495 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700496
497 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400498 res = sprintf(buffer, "running\n");
499 else if (nr < 0)
500 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
501 else
502 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700503 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
504 nr,
505 args[0], args[1], args[2], args[3], args[4], args[5],
506 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400507 unlock_trace(task);
508 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700509}
510#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/************************************************************************/
513/* Here the fs part begins */
514/************************************************************************/
515
516/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700517static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700519 struct task_struct *task;
520 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700521 /* Allow access to a task's file descriptors if it is us or we
522 * may use ptrace attach to the process and find out that
523 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700524 */
525 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700526 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400527 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700528 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700529 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700530 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800533int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700534{
535 int error;
536 struct inode *inode = dentry->d_inode;
537
538 if (attr->ia_valid & ATTR_MODE)
539 return -EPERM;
540
541 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200542 if (error)
543 return error;
544
545 if ((attr->ia_valid & ATTR_SIZE) &&
546 attr->ia_size != i_size_read(inode)) {
547 error = vmtruncate(inode, attr->ia_size);
548 if (error)
549 return error;
550 }
551
552 setattr_copy(inode, attr);
553 mark_inode_dirty(inode);
554 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700555}
556
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800557/*
558 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
559 * or euid/egid (for hide_pid_min=2)?
560 */
561static bool has_pid_permissions(struct pid_namespace *pid,
562 struct task_struct *task,
563 int hide_pid_min)
564{
565 if (pid->hide_pid < hide_pid_min)
566 return true;
567 if (in_group_p(pid->pid_gid))
568 return true;
569 return ptrace_may_access(task, PTRACE_MODE_READ);
570}
571
572
573static int proc_pid_permission(struct inode *inode, int mask)
574{
575 struct pid_namespace *pid = inode->i_sb->s_fs_info;
576 struct task_struct *task;
577 bool has_perms;
578
579 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800580 if (!task)
581 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800582 has_perms = has_pid_permissions(pid, task, 1);
583 put_task_struct(task);
584
585 if (!has_perms) {
586 if (pid->hide_pid == 2) {
587 /*
588 * Let's make getdents(), stat(), and open()
589 * consistent with each other. If a process
590 * may not stat() a file, it shouldn't be seen
591 * in procfs at all.
592 */
593 return -ENOENT;
594 }
595
596 return -EPERM;
597 }
598 return generic_permission(inode, mask);
599}
600
601
602
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800603static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700604 .setattr = proc_setattr,
605};
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
608
609static ssize_t proc_info_read(struct file * file, char __user * buf,
610 size_t count, loff_t *ppos)
611{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800612 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 unsigned long page;
614 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700615 struct task_struct *task = get_proc_task(inode);
616
617 length = -ESRCH;
618 if (!task)
619 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (count > PROC_BLOCK_SIZE)
622 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700623
624 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700625 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 length = PROC_I(inode)->op.proc_read(task, (char*)page);
629
630 if (length >= 0)
631 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
632 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700633out:
634 put_task_struct(task);
635out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return length;
637}
638
Arjan van de Ven00977a52007-02-12 00:55:34 -0800639static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100641 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642};
643
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800644static int proc_single_show(struct seq_file *m, void *v)
645{
646 struct inode *inode = m->private;
647 struct pid_namespace *ns;
648 struct pid *pid;
649 struct task_struct *task;
650 int ret;
651
652 ns = inode->i_sb->s_fs_info;
653 pid = proc_pid(inode);
654 task = get_pid_task(pid, PIDTYPE_PID);
655 if (!task)
656 return -ESRCH;
657
658 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
659
660 put_task_struct(task);
661 return ret;
662}
663
664static int proc_single_open(struct inode *inode, struct file *filp)
665{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800666 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800667}
668
669static const struct file_operations proc_single_file_operations = {
670 .open = proc_single_open,
671 .read = seq_read,
672 .llseek = seq_lseek,
673 .release = single_release,
674};
675
Cong Wangb409e572012-05-31 16:26:17 -0700676static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Linus Torvaldse2683372012-01-17 15:21:19 -0800678 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
679 struct mm_struct *mm;
680
681 if (!task)
682 return -ESRCH;
683
Cong Wangb409e572012-05-31 16:26:17 -0700684 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800685 put_task_struct(task);
686
687 if (IS_ERR(mm))
688 return PTR_ERR(mm);
689
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100690 if (mm) {
691 /* ensure this mm_struct can't be freed */
692 atomic_inc(&mm->mm_count);
693 /* but do not pin its memory */
694 mmput(mm);
695 }
696
Linus Torvaldse2683372012-01-17 15:21:19 -0800697 file->private_data = mm;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 0;
700}
701
Cong Wangb409e572012-05-31 16:26:17 -0700702static int mem_open(struct inode *inode, struct file *file)
703{
Djalal Harounibc452b42012-07-30 14:42:28 -0700704 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
705
706 /* OK to pass negative loff_t, we can catch out-of-range */
707 file->f_mode |= FMODE_UNSIGNED_OFFSET;
708
709 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700710}
711
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100712static ssize_t mem_rw(struct file *file, char __user *buf,
713 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Linus Torvaldse2683372012-01-17 15:21:19 -0800715 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100716 unsigned long addr = *ppos;
717 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvaldse2683372012-01-17 15:21:19 -0800720 if (!mm)
721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Mel Gormane12ba742007-10-16 01:25:52 -0700723 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800725 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700727 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100728 if (!atomic_inc_not_zero(&mm->mm_users))
729 goto free;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100732 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100734 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 copied = -EFAULT;
736 break;
737 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100738
739 this_len = access_remote_vm(mm, addr, page, this_len, write);
740 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!copied)
742 copied = -EIO;
743 break;
744 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100745
746 if (!write && copy_to_user(buf, page, this_len)) {
747 copied = -EFAULT;
748 break;
749 }
750
751 buf += this_len;
752 addr += this_len;
753 copied += this_len;
754 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100756 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700757
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100758 mmput(mm);
759free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700760 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return copied;
762}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100764static ssize_t mem_read(struct file *file, char __user *buf,
765 size_t count, loff_t *ppos)
766{
767 return mem_rw(file, buf, count, ppos, 0);
768}
769
770static ssize_t mem_write(struct file *file, const char __user *buf,
771 size_t count, loff_t *ppos)
772{
773 return mem_rw(file, (char __user*)buf, count, ppos, 1);
774}
775
Matt Mackall85863e42008-02-04 22:29:04 -0800776loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 switch (orig) {
779 case 0:
780 file->f_pos = offset;
781 break;
782 case 1:
783 file->f_pos += offset;
784 break;
785 default:
786 return -EINVAL;
787 }
788 force_successful_syscall_return();
789 return file->f_pos;
790}
791
Linus Torvaldse2683372012-01-17 15:21:19 -0800792static int mem_release(struct inode *inode, struct file *file)
793{
794 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100795 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100796 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800797 return 0;
798}
799
Arjan van de Ven00977a52007-02-12 00:55:34 -0800800static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .llseek = mem_lseek,
802 .read = mem_read,
803 .write = mem_write,
804 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800805 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806};
807
Cong Wangb409e572012-05-31 16:26:17 -0700808static int environ_open(struct inode *inode, struct file *file)
809{
810 return __mem_open(inode, file, PTRACE_MODE_READ);
811}
812
James Pearson315e28c2007-10-16 23:30:17 -0700813static ssize_t environ_read(struct file *file, char __user *buf,
814 size_t count, loff_t *ppos)
815{
James Pearson315e28c2007-10-16 23:30:17 -0700816 char *page;
817 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700818 int ret = 0;
819 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700820
Cong Wangb409e572012-05-31 16:26:17 -0700821 if (!mm)
822 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700823
James Pearson315e28c2007-10-16 23:30:17 -0700824 page = (char *)__get_free_page(GFP_TEMPORARY);
825 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700826 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700827
Al Virod6f64b82011-02-15 22:26:01 -0500828 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700829 if (!atomic_inc_not_zero(&mm->mm_users))
830 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700831 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700832 size_t this_len, max_len;
833 int retval;
834
835 if (src >= (mm->env_end - mm->env_start))
836 break;
James Pearson315e28c2007-10-16 23:30:17 -0700837
838 this_len = mm->env_end - (mm->env_start + src);
839
Djalal Harounie8905ec2012-07-30 14:42:26 -0700840 max_len = min_t(size_t, PAGE_SIZE, count);
841 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700842
Cong Wangb409e572012-05-31 16:26:17 -0700843 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700844 page, this_len, 0);
845
846 if (retval <= 0) {
847 ret = retval;
848 break;
849 }
850
851 if (copy_to_user(buf, page, retval)) {
852 ret = -EFAULT;
853 break;
854 }
855
856 ret += retval;
857 src += retval;
858 buf += retval;
859 count -= retval;
860 }
861 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700862 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700863
864free:
James Pearson315e28c2007-10-16 23:30:17 -0700865 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700866 return ret;
867}
868
869static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700870 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700871 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100872 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700873 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700874};
875
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800876static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
877 loff_t *ppos)
878{
879 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
880 char buffer[PROC_NUMBUF];
881 int oom_adj = OOM_ADJUST_MIN;
882 size_t len;
883 unsigned long flags;
884
885 if (!task)
886 return -ESRCH;
887 if (lock_task_sighand(task, &flags)) {
888 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
889 oom_adj = OOM_ADJUST_MAX;
890 else
891 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
892 OOM_SCORE_ADJ_MAX;
893 unlock_task_sighand(task, &flags);
894 }
895 put_task_struct(task);
896 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
897 return simple_read_from_buffer(buf, count, ppos, buffer, len);
898}
899
900static ssize_t oom_adj_write(struct file *file, const char __user *buf,
901 size_t count, loff_t *ppos)
902{
903 struct task_struct *task;
904 char buffer[PROC_NUMBUF];
905 int oom_adj;
906 unsigned long flags;
907 int err;
908
909 memset(buffer, 0, sizeof(buffer));
910 if (count > sizeof(buffer) - 1)
911 count = sizeof(buffer) - 1;
912 if (copy_from_user(buffer, buf, count)) {
913 err = -EFAULT;
914 goto out;
915 }
916
917 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
918 if (err)
919 goto out;
920 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
921 oom_adj != OOM_DISABLE) {
922 err = -EINVAL;
923 goto out;
924 }
925
926 task = get_proc_task(file->f_path.dentry->d_inode);
927 if (!task) {
928 err = -ESRCH;
929 goto out;
930 }
931
932 task_lock(task);
933 if (!task->mm) {
934 err = -EINVAL;
935 goto err_task_lock;
936 }
937
938 if (!lock_task_sighand(task, &flags)) {
939 err = -ESRCH;
940 goto err_task_lock;
941 }
942
943 /*
944 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
945 * value is always attainable.
946 */
947 if (oom_adj == OOM_ADJUST_MAX)
948 oom_adj = OOM_SCORE_ADJ_MAX;
949 else
950 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
951
952 if (oom_adj < task->signal->oom_score_adj &&
953 !capable(CAP_SYS_RESOURCE)) {
954 err = -EACCES;
955 goto err_sighand;
956 }
957
958 /*
959 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
960 * /proc/pid/oom_score_adj instead.
961 */
962 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
963 current->comm, task_pid_nr(current), task_pid_nr(task),
964 task_pid_nr(task));
965
966 task->signal->oom_score_adj = oom_adj;
967 trace_oom_score_adj_update(task);
968err_sighand:
969 unlock_task_sighand(task, &flags);
970err_task_lock:
971 task_unlock(task);
972 put_task_struct(task);
973out:
974 return err < 0 ? err : count;
975}
976
977static const struct file_operations proc_oom_adj_operations = {
978 .read = oom_adj_read,
979 .write = oom_adj_write,
980 .llseek = generic_file_llseek,
981};
982
David Rientjesa63d83f2010-08-09 17:19:46 -0700983static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
984 size_t count, loff_t *ppos)
985{
986 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
987 char buffer[PROC_NUMBUF];
988 int oom_score_adj = OOM_SCORE_ADJ_MIN;
989 unsigned long flags;
990 size_t len;
991
992 if (!task)
993 return -ESRCH;
994 if (lock_task_sighand(task, &flags)) {
995 oom_score_adj = task->signal->oom_score_adj;
996 unlock_task_sighand(task, &flags);
997 }
998 put_task_struct(task);
999 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1000 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1001}
1002
1003static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1004 size_t count, loff_t *ppos)
1005{
1006 struct task_struct *task;
1007 char buffer[PROC_NUMBUF];
1008 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001009 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001010 int err;
1011
1012 memset(buffer, 0, sizeof(buffer));
1013 if (count > sizeof(buffer) - 1)
1014 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001015 if (copy_from_user(buffer, buf, count)) {
1016 err = -EFAULT;
1017 goto out;
1018 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001019
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001020 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001021 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001022 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001023 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001024 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1025 err = -EINVAL;
1026 goto out;
1027 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001028
1029 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001030 if (!task) {
1031 err = -ESRCH;
1032 goto out;
1033 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001034
Ying Han3d5992d2010-10-26 14:21:23 -07001035 task_lock(task);
1036 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001037 err = -EINVAL;
1038 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001039 }
David Rientjesd19d5472010-10-26 14:21:26 -07001040
1041 if (!lock_task_sighand(task, &flags)) {
1042 err = -ESRCH;
1043 goto err_task_lock;
1044 }
1045
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001046 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001047 !capable(CAP_SYS_RESOURCE)) {
1048 err = -EACCES;
1049 goto err_sighand;
1050 }
1051
David Rientjesa63d83f2010-08-09 17:19:46 -07001052 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001053 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1054 task->signal->oom_score_adj_min = oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001055 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001056
David Rientjes723548b2010-10-26 14:21:25 -07001057err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001058 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001059err_task_lock:
1060 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001061 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001062out:
1063 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001064}
1065
1066static const struct file_operations proc_oom_score_adj_operations = {
1067 .read = oom_score_adj_read,
1068 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001069 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001070};
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072#ifdef CONFIG_AUDITSYSCALL
1073#define TMPBUFLEN 21
1074static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1075 size_t count, loff_t *ppos)
1076{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001077 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001078 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ssize_t length;
1080 char tmpbuf[TMPBUFLEN];
1081
Eric W. Biederman99f89552006-06-26 00:25:55 -07001082 if (!task)
1083 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001085 from_kuid(file->f_cred->user_ns,
1086 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001087 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1089}
1090
1091static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1092 size_t count, loff_t *ppos)
1093{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001094 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 char *page, *tmp;
1096 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001098 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001100 rcu_read_lock();
1101 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1102 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001104 }
1105 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Al Viroe0182902006-05-18 08:28:02 -04001107 if (count >= PAGE_SIZE)
1108 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (*ppos != 0) {
1111 /* No partial writes. */
1112 return -EINVAL;
1113 }
Mel Gormane12ba742007-10-16 01:25:52 -07001114 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (!page)
1116 return -ENOMEM;
1117 length = -EFAULT;
1118 if (copy_from_user(page, buf, count))
1119 goto out_free_page;
1120
Al Viroe0182902006-05-18 08:28:02 -04001121 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 loginuid = simple_strtoul(page, &tmp, 10);
1123 if (tmp == page) {
1124 length = -EINVAL;
1125 goto out_free_page;
1126
1127 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001128 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1129 if (!uid_valid(kloginuid)) {
1130 length = -EINVAL;
1131 goto out_free_page;
1132 }
1133
1134 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (likely(length == 0))
1136 length = count;
1137
1138out_free_page:
1139 free_page((unsigned long) page);
1140 return length;
1141}
1142
Arjan van de Ven00977a52007-02-12 00:55:34 -08001143static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 .read = proc_loginuid_read,
1145 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001146 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147};
Eric Paris1e0bd752008-03-13 08:15:31 -04001148
1149static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1150 size_t count, loff_t *ppos)
1151{
1152 struct inode * inode = file->f_path.dentry->d_inode;
1153 struct task_struct *task = get_proc_task(inode);
1154 ssize_t length;
1155 char tmpbuf[TMPBUFLEN];
1156
1157 if (!task)
1158 return -ESRCH;
1159 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1160 audit_get_sessionid(task));
1161 put_task_struct(task);
1162 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1163}
1164
1165static const struct file_operations proc_sessionid_operations = {
1166 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001167 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001168};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169#endif
1170
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001171#ifdef CONFIG_FAULT_INJECTION
1172static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1173 size_t count, loff_t *ppos)
1174{
1175 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1176 char buffer[PROC_NUMBUF];
1177 size_t len;
1178 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001179
1180 if (!task)
1181 return -ESRCH;
1182 make_it_fail = task->make_it_fail;
1183 put_task_struct(task);
1184
1185 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001186
1187 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001188}
1189
1190static ssize_t proc_fault_inject_write(struct file * file,
1191 const char __user * buf, size_t count, loff_t *ppos)
1192{
1193 struct task_struct *task;
1194 char buffer[PROC_NUMBUF], *end;
1195 int make_it_fail;
1196
1197 if (!capable(CAP_SYS_RESOURCE))
1198 return -EPERM;
1199 memset(buffer, 0, sizeof(buffer));
1200 if (count > sizeof(buffer) - 1)
1201 count = sizeof(buffer) - 1;
1202 if (copy_from_user(buffer, buf, count))
1203 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001204 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1205 if (*end)
1206 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001207 task = get_proc_task(file->f_dentry->d_inode);
1208 if (!task)
1209 return -ESRCH;
1210 task->make_it_fail = make_it_fail;
1211 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001212
1213 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001214}
1215
Arjan van de Ven00977a52007-02-12 00:55:34 -08001216static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001217 .read = proc_fault_inject_read,
1218 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001219 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001220};
1221#endif
1222
Arjan van de Ven97455122008-01-25 21:08:34 +01001223
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001224#ifdef CONFIG_SCHED_DEBUG
1225/*
1226 * Print out various scheduling related per-task fields:
1227 */
1228static int sched_show(struct seq_file *m, void *v)
1229{
1230 struct inode *inode = m->private;
1231 struct task_struct *p;
1232
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001233 p = get_proc_task(inode);
1234 if (!p)
1235 return -ESRCH;
1236 proc_sched_show_task(p, m);
1237
1238 put_task_struct(p);
1239
1240 return 0;
1241}
1242
1243static ssize_t
1244sched_write(struct file *file, const char __user *buf,
1245 size_t count, loff_t *offset)
1246{
1247 struct inode *inode = file->f_path.dentry->d_inode;
1248 struct task_struct *p;
1249
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001250 p = get_proc_task(inode);
1251 if (!p)
1252 return -ESRCH;
1253 proc_sched_set_task(p);
1254
1255 put_task_struct(p);
1256
1257 return count;
1258}
1259
1260static int sched_open(struct inode *inode, struct file *filp)
1261{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001262 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001263}
1264
1265static const struct file_operations proc_pid_sched_operations = {
1266 .open = sched_open,
1267 .read = seq_read,
1268 .write = sched_write,
1269 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001270 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001271};
1272
1273#endif
1274
Mike Galbraith5091faa2010-11-30 14:18:03 +01001275#ifdef CONFIG_SCHED_AUTOGROUP
1276/*
1277 * Print out autogroup related information:
1278 */
1279static int sched_autogroup_show(struct seq_file *m, void *v)
1280{
1281 struct inode *inode = m->private;
1282 struct task_struct *p;
1283
1284 p = get_proc_task(inode);
1285 if (!p)
1286 return -ESRCH;
1287 proc_sched_autogroup_show_task(p, m);
1288
1289 put_task_struct(p);
1290
1291 return 0;
1292}
1293
1294static ssize_t
1295sched_autogroup_write(struct file *file, const char __user *buf,
1296 size_t count, loff_t *offset)
1297{
1298 struct inode *inode = file->f_path.dentry->d_inode;
1299 struct task_struct *p;
1300 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001301 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001302 int err;
1303
1304 memset(buffer, 0, sizeof(buffer));
1305 if (count > sizeof(buffer) - 1)
1306 count = sizeof(buffer) - 1;
1307 if (copy_from_user(buffer, buf, count))
1308 return -EFAULT;
1309
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001310 err = kstrtoint(strstrip(buffer), 0, &nice);
1311 if (err < 0)
1312 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001313
1314 p = get_proc_task(inode);
1315 if (!p)
1316 return -ESRCH;
1317
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001318 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001319 if (err)
1320 count = err;
1321
1322 put_task_struct(p);
1323
1324 return count;
1325}
1326
1327static int sched_autogroup_open(struct inode *inode, struct file *filp)
1328{
1329 int ret;
1330
1331 ret = single_open(filp, sched_autogroup_show, NULL);
1332 if (!ret) {
1333 struct seq_file *m = filp->private_data;
1334
1335 m->private = inode;
1336 }
1337 return ret;
1338}
1339
1340static const struct file_operations proc_pid_sched_autogroup_operations = {
1341 .open = sched_autogroup_open,
1342 .read = seq_read,
1343 .write = sched_autogroup_write,
1344 .llseek = seq_lseek,
1345 .release = single_release,
1346};
1347
1348#endif /* CONFIG_SCHED_AUTOGROUP */
1349
john stultz4614a696b2009-12-14 18:00:05 -08001350static ssize_t comm_write(struct file *file, const char __user *buf,
1351 size_t count, loff_t *offset)
1352{
1353 struct inode *inode = file->f_path.dentry->d_inode;
1354 struct task_struct *p;
1355 char buffer[TASK_COMM_LEN];
1356
1357 memset(buffer, 0, sizeof(buffer));
1358 if (count > sizeof(buffer) - 1)
1359 count = sizeof(buffer) - 1;
1360 if (copy_from_user(buffer, buf, count))
1361 return -EFAULT;
1362
1363 p = get_proc_task(inode);
1364 if (!p)
1365 return -ESRCH;
1366
1367 if (same_thread_group(current, p))
1368 set_task_comm(p, buffer);
1369 else
1370 count = -EINVAL;
1371
1372 put_task_struct(p);
1373
1374 return count;
1375}
1376
1377static int comm_show(struct seq_file *m, void *v)
1378{
1379 struct inode *inode = m->private;
1380 struct task_struct *p;
1381
1382 p = get_proc_task(inode);
1383 if (!p)
1384 return -ESRCH;
1385
1386 task_lock(p);
1387 seq_printf(m, "%s\n", p->comm);
1388 task_unlock(p);
1389
1390 put_task_struct(p);
1391
1392 return 0;
1393}
1394
1395static int comm_open(struct inode *inode, struct file *filp)
1396{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001397 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001398}
1399
1400static const struct file_operations proc_pid_set_comm_operations = {
1401 .open = comm_open,
1402 .read = seq_read,
1403 .write = comm_write,
1404 .llseek = seq_lseek,
1405 .release = single_release,
1406};
1407
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001408static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001409{
1410 struct task_struct *task;
1411 struct mm_struct *mm;
1412 struct file *exe_file;
1413
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001414 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001415 if (!task)
1416 return -ENOENT;
1417 mm = get_task_mm(task);
1418 put_task_struct(task);
1419 if (!mm)
1420 return -ENOENT;
1421 exe_file = get_mm_exe_file(mm);
1422 mmput(mm);
1423 if (exe_file) {
1424 *exe_path = exe_file->f_path;
1425 path_get(&exe_file->f_path);
1426 fput(exe_file);
1427 return 0;
1428 } else
1429 return -ENOENT;
1430}
1431
Al Viro008b1502005-08-20 00:17:39 +01001432static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001435 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int error = -EACCES;
1437
Eric W. Biederman778c1142006-06-26 00:25:58 -07001438 /* Are we allowed to snoop on the tasks file descriptors? */
1439 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Christoph Hellwig408ef012012-06-18 10:47:03 -04001442 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1443 if (error)
1444 goto out;
1445
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001446 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001447 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448out:
Al Viro008b1502005-08-20 00:17:39 +01001449 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001452static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Mel Gormane12ba742007-10-16 01:25:52 -07001454 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001455 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int len;
1457
1458 if (!tmp)
1459 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001460
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001461 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001462 len = PTR_ERR(pathname);
1463 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001465 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (len > buflen)
1468 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001469 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 len = -EFAULT;
1471 out:
1472 free_page((unsigned long)tmp);
1473 return len;
1474}
1475
1476static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1477{
1478 int error = -EACCES;
1479 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001480 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Eric W. Biederman778c1142006-06-26 00:25:58 -07001482 /* Are we allowed to snoop on the tasks file descriptors? */
1483 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001486 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (error)
1488 goto out;
1489
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001490 error = do_proc_readlink(&path, buffer, buflen);
1491 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return error;
1494}
1495
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001496const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001498 .follow_link = proc_pid_follow_link,
1499 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500};
1501
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001502
1503/* building an inode */
1504
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001505struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001506{
1507 struct inode * inode;
1508 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001509 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001510
1511 /* We need a new inode */
1512
1513 inode = new_inode(sb);
1514 if (!inode)
1515 goto out;
1516
1517 /* Common stuff */
1518 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001519 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001520 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001521 inode->i_op = &proc_def_inode_operations;
1522
1523 /*
1524 * grab the reference to task.
1525 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001526 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001527 if (!ei->pid)
1528 goto out_unlock;
1529
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001530 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001531 rcu_read_lock();
1532 cred = __task_cred(task);
1533 inode->i_uid = cred->euid;
1534 inode->i_gid = cred->egid;
1535 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001536 }
1537 security_task_to_inode(task, inode);
1538
1539out:
1540 return inode;
1541
1542out_unlock:
1543 iput(inode);
1544 return NULL;
1545}
1546
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001547int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001548{
1549 struct inode *inode = dentry->d_inode;
1550 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001551 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001552 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001553
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001554 generic_fillattr(inode, stat);
1555
1556 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001557 stat->uid = GLOBAL_ROOT_UID;
1558 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001559 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1560 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001561 if (!has_pid_permissions(pid, task, 2)) {
1562 rcu_read_unlock();
1563 /*
1564 * This doesn't prevent learning whether PID exists,
1565 * it only makes getattr() consistent with readdir().
1566 */
1567 return -ENOENT;
1568 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001569 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1570 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001571 cred = __task_cred(task);
1572 stat->uid = cred->euid;
1573 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001574 }
1575 }
1576 rcu_read_unlock();
1577 return 0;
1578}
1579
1580/* dentry stuff */
1581
1582/*
1583 * Exceptional case: normally we are not allowed to unhash a busy
1584 * directory. In this case, however, we can do it - no aliasing problems
1585 * due to the way we treat inodes.
1586 *
1587 * Rewrite the inode's ownerships here because the owning task may have
1588 * performed a setuid(), etc.
1589 *
1590 * Before the /proc/pid/status file was created the only way to read
1591 * the effective uid of a /process was to stat /proc/pid. Reading
1592 * /proc/pid/status is slow enough that procps and other packages
1593 * kept stating /proc/pid. To keep the rules in /proc simple I have
1594 * made this apply to all per process world readable and executable
1595 * directories.
1596 */
Al Viro0b728e12012-06-10 16:03:43 -04001597int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001598{
Nick Piggin34286d62011-01-07 17:49:57 +11001599 struct inode *inode;
1600 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001601 const struct cred *cred;
1602
Al Viro0b728e12012-06-10 16:03:43 -04001603 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001604 return -ECHILD;
1605
1606 inode = dentry->d_inode;
1607 task = get_proc_task(inode);
1608
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001609 if (task) {
1610 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1611 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001612 rcu_read_lock();
1613 cred = __task_cred(task);
1614 inode->i_uid = cred->euid;
1615 inode->i_gid = cred->egid;
1616 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001617 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001618 inode->i_uid = GLOBAL_ROOT_UID;
1619 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001620 }
1621 inode->i_mode &= ~(S_ISUID | S_ISGID);
1622 security_task_to_inode(task, inode);
1623 put_task_struct(task);
1624 return 1;
1625 }
1626 d_drop(dentry);
1627 return 0;
1628}
1629
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001630const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001631{
1632 .d_revalidate = pid_revalidate,
1633 .d_delete = pid_delete_dentry,
1634};
1635
1636/* Lookups */
1637
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001638/*
1639 * Fill a directory entry.
1640 *
1641 * If possible create the dcache entry and derive our inode number and
1642 * file type from dcache entry.
1643 *
1644 * Since all of the proc inode numbers are dynamically generated, the inode
1645 * numbers do not exist until the inode is cache. This means creating the
1646 * the dcache entry in readdir is necessary to keep the inode numbers
1647 * reported by readdir in sync with the inode numbers reported
1648 * by stat.
1649 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001650int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1651 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001652 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001653{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001654 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001655 struct inode *inode;
1656 struct qstr qname;
1657 ino_t ino = 0;
1658 unsigned type = DT_UNKNOWN;
1659
1660 qname.name = name;
1661 qname.len = len;
1662 qname.hash = full_name_hash(name, len);
1663
1664 child = d_lookup(dir, &qname);
1665 if (!child) {
1666 struct dentry *new;
1667 new = d_alloc(dir, &qname);
1668 if (new) {
1669 child = instantiate(dir->d_inode, new, task, ptr);
1670 if (child)
1671 dput(new);
1672 else
1673 child = new;
1674 }
1675 }
1676 if (!child || IS_ERR(child) || !child->d_inode)
1677 goto end_instantiate;
1678 inode = child->d_inode;
1679 if (inode) {
1680 ino = inode->i_ino;
1681 type = inode->i_mode >> 12;
1682 }
1683 dput(child);
1684end_instantiate:
1685 if (!ino)
1686 ino = find_inode_number(dir, &qname);
1687 if (!ino)
1688 ino = 1;
1689 return filldir(dirent, name, len, filp->f_pos, ino, type);
1690}
1691
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001692#ifdef CONFIG_CHECKPOINT_RESTORE
1693
1694/*
1695 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1696 * which represent vma start and end addresses.
1697 */
1698static int dname_to_vma_addr(struct dentry *dentry,
1699 unsigned long *start, unsigned long *end)
1700{
1701 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1702 return -EINVAL;
1703
1704 return 0;
1705}
1706
Al Viro0b728e12012-06-10 16:03:43 -04001707static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001708{
1709 unsigned long vm_start, vm_end;
1710 bool exact_vma_exists = false;
1711 struct mm_struct *mm = NULL;
1712 struct task_struct *task;
1713 const struct cred *cred;
1714 struct inode *inode;
1715 int status = 0;
1716
Al Viro0b728e12012-06-10 16:03:43 -04001717 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001718 return -ECHILD;
1719
1720 if (!capable(CAP_SYS_ADMIN)) {
1721 status = -EACCES;
1722 goto out_notask;
1723 }
1724
1725 inode = dentry->d_inode;
1726 task = get_proc_task(inode);
1727 if (!task)
1728 goto out_notask;
1729
Cong Wang2344bec2012-05-31 16:26:18 -07001730 mm = mm_access(task, PTRACE_MODE_READ);
1731 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001732 goto out;
1733
1734 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1735 down_read(&mm->mmap_sem);
1736 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1737 up_read(&mm->mmap_sem);
1738 }
1739
1740 mmput(mm);
1741
1742 if (exact_vma_exists) {
1743 if (task_dumpable(task)) {
1744 rcu_read_lock();
1745 cred = __task_cred(task);
1746 inode->i_uid = cred->euid;
1747 inode->i_gid = cred->egid;
1748 rcu_read_unlock();
1749 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001750 inode->i_uid = GLOBAL_ROOT_UID;
1751 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001752 }
1753 security_task_to_inode(task, inode);
1754 status = 1;
1755 }
1756
1757out:
1758 put_task_struct(task);
1759
1760out_notask:
1761 if (status <= 0)
1762 d_drop(dentry);
1763
1764 return status;
1765}
1766
1767static const struct dentry_operations tid_map_files_dentry_operations = {
1768 .d_revalidate = map_files_d_revalidate,
1769 .d_delete = pid_delete_dentry,
1770};
1771
1772static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1773{
1774 unsigned long vm_start, vm_end;
1775 struct vm_area_struct *vma;
1776 struct task_struct *task;
1777 struct mm_struct *mm;
1778 int rc;
1779
1780 rc = -ENOENT;
1781 task = get_proc_task(dentry->d_inode);
1782 if (!task)
1783 goto out;
1784
1785 mm = get_task_mm(task);
1786 put_task_struct(task);
1787 if (!mm)
1788 goto out;
1789
1790 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1791 if (rc)
1792 goto out_mmput;
1793
1794 down_read(&mm->mmap_sem);
1795 vma = find_exact_vma(mm, vm_start, vm_end);
1796 if (vma && vma->vm_file) {
1797 *path = vma->vm_file->f_path;
1798 path_get(path);
1799 rc = 0;
1800 }
1801 up_read(&mm->mmap_sem);
1802
1803out_mmput:
1804 mmput(mm);
1805out:
1806 return rc;
1807}
1808
1809struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001810 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001811 unsigned long len;
1812 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1813};
1814
1815static struct dentry *
1816proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1817 struct task_struct *task, const void *ptr)
1818{
Al Viro7b540d02012-08-27 14:55:26 -04001819 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001820 struct proc_inode *ei;
1821 struct inode *inode;
1822
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001823 inode = proc_pid_make_inode(dir->i_sb, task);
1824 if (!inode)
1825 return ERR_PTR(-ENOENT);
1826
1827 ei = PROC_I(inode);
1828 ei->op.proc_get_link = proc_map_files_get_link;
1829
1830 inode->i_op = &proc_pid_link_inode_operations;
1831 inode->i_size = 64;
1832 inode->i_mode = S_IFLNK;
1833
Al Viro7b540d02012-08-27 14:55:26 -04001834 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001835 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001836 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001837 inode->i_mode |= S_IWUSR;
1838
1839 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1840 d_add(dentry, inode);
1841
1842 return NULL;
1843}
1844
1845static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001846 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001847{
1848 unsigned long vm_start, vm_end;
1849 struct vm_area_struct *vma;
1850 struct task_struct *task;
1851 struct dentry *result;
1852 struct mm_struct *mm;
1853
1854 result = ERR_PTR(-EACCES);
1855 if (!capable(CAP_SYS_ADMIN))
1856 goto out;
1857
1858 result = ERR_PTR(-ENOENT);
1859 task = get_proc_task(dir);
1860 if (!task)
1861 goto out;
1862
1863 result = ERR_PTR(-EACCES);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001864 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001865 goto out_put_task;
1866
1867 result = ERR_PTR(-ENOENT);
1868 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001869 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001870
1871 mm = get_task_mm(task);
1872 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001873 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001874
1875 down_read(&mm->mmap_sem);
1876 vma = find_exact_vma(mm, vm_start, vm_end);
1877 if (!vma)
1878 goto out_no_vma;
1879
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001880 if (vma->vm_file)
1881 result = proc_map_files_instantiate(dir, dentry, task,
1882 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001883
1884out_no_vma:
1885 up_read(&mm->mmap_sem);
1886 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001887out_put_task:
1888 put_task_struct(task);
1889out:
1890 return result;
1891}
1892
1893static const struct inode_operations proc_map_files_inode_operations = {
1894 .lookup = proc_map_files_lookup,
1895 .permission = proc_fd_permission,
1896 .setattr = proc_setattr,
1897};
1898
1899static int
1900proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1901{
1902 struct dentry *dentry = filp->f_path.dentry;
1903 struct inode *inode = dentry->d_inode;
1904 struct vm_area_struct *vma;
1905 struct task_struct *task;
1906 struct mm_struct *mm;
1907 ino_t ino;
1908 int ret;
1909
1910 ret = -EACCES;
1911 if (!capable(CAP_SYS_ADMIN))
1912 goto out;
1913
1914 ret = -ENOENT;
1915 task = get_proc_task(inode);
1916 if (!task)
1917 goto out;
1918
1919 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001920 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001921 goto out_put_task;
1922
1923 ret = 0;
1924 switch (filp->f_pos) {
1925 case 0:
1926 ino = inode->i_ino;
1927 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001928 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001929 filp->f_pos++;
1930 case 1:
1931 ino = parent_ino(dentry);
1932 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001933 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001934 filp->f_pos++;
1935 default:
1936 {
1937 unsigned long nr_files, pos, i;
1938 struct flex_array *fa = NULL;
1939 struct map_files_info info;
1940 struct map_files_info *p;
1941
1942 mm = get_task_mm(task);
1943 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001944 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001945 down_read(&mm->mmap_sem);
1946
1947 nr_files = 0;
1948
1949 /*
1950 * We need two passes here:
1951 *
1952 * 1) Collect vmas of mapped files with mmap_sem taken
1953 * 2) Release mmap_sem and instantiate entries
1954 *
1955 * otherwise we get lockdep complained, since filldir()
1956 * routine might require mmap_sem taken in might_fault().
1957 */
1958
1959 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1960 if (vma->vm_file && ++pos > filp->f_pos)
1961 nr_files++;
1962 }
1963
1964 if (nr_files) {
1965 fa = flex_array_alloc(sizeof(info), nr_files,
1966 GFP_KERNEL);
1967 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1968 GFP_KERNEL)) {
1969 ret = -ENOMEM;
1970 if (fa)
1971 flex_array_free(fa);
1972 up_read(&mm->mmap_sem);
1973 mmput(mm);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001974 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001975 }
1976 for (i = 0, vma = mm->mmap, pos = 2; vma;
1977 vma = vma->vm_next) {
1978 if (!vma->vm_file)
1979 continue;
1980 if (++pos <= filp->f_pos)
1981 continue;
1982
Al Viro7b540d02012-08-27 14:55:26 -04001983 info.mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001984 info.len = snprintf(info.name,
1985 sizeof(info.name), "%lx-%lx",
1986 vma->vm_start, vma->vm_end);
1987 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1988 BUG();
1989 }
1990 }
1991 up_read(&mm->mmap_sem);
1992
1993 for (i = 0; i < nr_files; i++) {
1994 p = flex_array_get(fa, i);
1995 ret = proc_fill_cache(filp, dirent, filldir,
1996 p->name, p->len,
1997 proc_map_files_instantiate,
Al Viro7b540d02012-08-27 14:55:26 -04001998 task,
1999 (void *)(unsigned long)p->mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002000 if (ret)
2001 break;
2002 filp->f_pos++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002003 }
2004 if (fa)
2005 flex_array_free(fa);
2006 mmput(mm);
2007 }
2008 }
2009
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002010out_put_task:
2011 put_task_struct(task);
2012out:
2013 return ret;
2014}
2015
2016static const struct file_operations proc_map_files_operations = {
2017 .read = generic_read_dir,
2018 .readdir = proc_map_files_readdir,
2019 .llseek = default_llseek,
2020};
2021
2022#endif /* CONFIG_CHECKPOINT_RESTORE */
2023
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002024static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002025 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002026{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002027 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002028 struct inode *inode;
2029 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002030 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002031
Eric W. Biederman61a28782006-10-02 02:18:49 -07002032 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002033 if (!inode)
2034 goto out;
2035
2036 ei = PROC_I(inode);
2037 inode->i_mode = p->mode;
2038 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002039 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002040 if (p->iop)
2041 inode->i_op = p->iop;
2042 if (p->fop)
2043 inode->i_fop = p->fop;
2044 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002045 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002046 d_add(dentry, inode);
2047 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002048 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002049 error = NULL;
2050out:
2051 return error;
2052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054static struct dentry *proc_pident_lookup(struct inode *dir,
2055 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002056 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002057 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002059 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002060 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002061 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002063 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Eric W. Biederman99f89552006-06-26 00:25:55 -07002065 if (!task)
2066 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002068 /*
2069 * Yes, it does not scale. And it should not. Don't add
2070 * new entries into /proc/<tgid>/ without very good reasons.
2071 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002072 last = &ents[nents - 1];
2073 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (p->len != dentry->d_name.len)
2075 continue;
2076 if (!memcmp(dentry->d_name.name, p->name, p->len))
2077 break;
2078 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002079 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 goto out;
2081
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002082 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002084 put_task_struct(task);
2085out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002086 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Eric Dumazetc5141e62007-05-08 00:26:15 -07002089static int proc_pident_fill_cache(struct file *filp, void *dirent,
2090 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002091{
2092 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2093 proc_pident_instantiate, task, p);
2094}
2095
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002096static int proc_pident_readdir(struct file *filp,
2097 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002098 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002099{
2100 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002101 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002102 struct inode *inode = dentry->d_inode;
2103 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002104 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002105 ino_t ino;
2106 int ret;
2107
2108 ret = -ENOENT;
2109 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002110 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002111
2112 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002113 i = filp->f_pos;
2114 switch (i) {
2115 case 0:
2116 ino = inode->i_ino;
2117 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2118 goto out;
2119 i++;
2120 filp->f_pos++;
2121 /* fall through */
2122 case 1:
2123 ino = parent_ino(dentry);
2124 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2125 goto out;
2126 i++;
2127 filp->f_pos++;
2128 /* fall through */
2129 default:
2130 i -= 2;
2131 if (i >= nents) {
2132 ret = 1;
2133 goto out;
2134 }
2135 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002136 last = &ents[nents - 1];
2137 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002138 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002139 goto out;
2140 filp->f_pos++;
2141 p++;
2142 }
2143 }
2144
2145 ret = 1;
2146out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002147 put_task_struct(task);
2148out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002149 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002153static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2154 size_t count, loff_t *ppos)
2155{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002156 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002157 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002158 ssize_t length;
2159 struct task_struct *task = get_proc_task(inode);
2160
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002161 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002162 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002163
2164 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002165 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002166 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002167 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002168 if (length > 0)
2169 length = simple_read_from_buffer(buf, count, ppos, p, length);
2170 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002171 return length;
2172}
2173
2174static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2175 size_t count, loff_t *ppos)
2176{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002177 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002178 char *page;
2179 ssize_t length;
2180 struct task_struct *task = get_proc_task(inode);
2181
2182 length = -ESRCH;
2183 if (!task)
2184 goto out_no_task;
2185 if (count > PAGE_SIZE)
2186 count = PAGE_SIZE;
2187
2188 /* No partial writes. */
2189 length = -EINVAL;
2190 if (*ppos != 0)
2191 goto out;
2192
2193 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002194 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002195 if (!page)
2196 goto out;
2197
2198 length = -EFAULT;
2199 if (copy_from_user(page, buf, count))
2200 goto out_free;
2201
David Howells107db7c2009-05-08 13:55:27 +01002202 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002203 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002204 if (length < 0)
2205 goto out_free;
2206
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002207 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002208 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002209 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002210 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002211out_free:
2212 free_page((unsigned long) page);
2213out:
2214 put_task_struct(task);
2215out_no_task:
2216 return length;
2217}
2218
Arjan van de Ven00977a52007-02-12 00:55:34 -08002219static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002220 .read = proc_pid_attr_read,
2221 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002222 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002223};
2224
Eric Dumazetc5141e62007-05-08 00:26:15 -07002225static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002226 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2227 REG("prev", S_IRUGO, proc_pid_attr_operations),
2228 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2229 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2230 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2231 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002232};
2233
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002234static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 void * dirent, filldir_t filldir)
2236{
2237 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002238 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Arjan van de Ven00977a52007-02-12 00:55:34 -08002241static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002243 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002244 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245};
2246
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002247static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002248 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002250 return proc_pident_lookup(dir, dentry,
2251 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002254static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002255 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002256 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002257 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258};
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260#endif
2261
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002262#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002263static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2264 size_t count, loff_t *ppos)
2265{
2266 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2267 struct mm_struct *mm;
2268 char buffer[PROC_NUMBUF];
2269 size_t len;
2270 int ret;
2271
2272 if (!task)
2273 return -ESRCH;
2274
2275 ret = 0;
2276 mm = get_task_mm(task);
2277 if (mm) {
2278 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2279 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2280 MMF_DUMP_FILTER_SHIFT));
2281 mmput(mm);
2282 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2283 }
2284
2285 put_task_struct(task);
2286
2287 return ret;
2288}
2289
2290static ssize_t proc_coredump_filter_write(struct file *file,
2291 const char __user *buf,
2292 size_t count,
2293 loff_t *ppos)
2294{
2295 struct task_struct *task;
2296 struct mm_struct *mm;
2297 char buffer[PROC_NUMBUF], *end;
2298 unsigned int val;
2299 int ret;
2300 int i;
2301 unsigned long mask;
2302
2303 ret = -EFAULT;
2304 memset(buffer, 0, sizeof(buffer));
2305 if (count > sizeof(buffer) - 1)
2306 count = sizeof(buffer) - 1;
2307 if (copy_from_user(buffer, buf, count))
2308 goto out_no_task;
2309
2310 ret = -EINVAL;
2311 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2312 if (*end == '\n')
2313 end++;
2314 if (end - buffer == 0)
2315 goto out_no_task;
2316
2317 ret = -ESRCH;
2318 task = get_proc_task(file->f_dentry->d_inode);
2319 if (!task)
2320 goto out_no_task;
2321
2322 ret = end - buffer;
2323 mm = get_task_mm(task);
2324 if (!mm)
2325 goto out_no_mm;
2326
2327 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2328 if (val & mask)
2329 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2330 else
2331 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2332 }
2333
2334 mmput(mm);
2335 out_no_mm:
2336 put_task_struct(task);
2337 out_no_task:
2338 return ret;
2339}
2340
2341static const struct file_operations proc_coredump_filter_operations = {
2342 .read = proc_coredump_filter_read,
2343 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002344 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002345};
2346#endif
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/*
2349 * /proc/self:
2350 */
2351static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2352 int buflen)
2353{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002354 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002355 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002356 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002357 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002358 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002359 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return vfs_readlink(dentry,buffer,buflen,tmp);
2361}
2362
Al Viro008b1502005-08-20 00:17:39 +01002363static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002365 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002366 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002367 char *name = ERR_PTR(-ENOENT);
2368 if (tgid) {
Jeff Laytonf81700b2012-10-10 16:43:21 -04002369 /* 11 for max length of signed int in decimal + NULL term */
2370 name = kmalloc(12, GFP_KERNEL);
Al Viro7fee4862010-01-14 01:03:28 -05002371 if (!name)
2372 name = ERR_PTR(-ENOMEM);
2373 else
2374 sprintf(name, "%d", tgid);
2375 }
2376 nd_set_link(nd, name);
2377 return NULL;
2378}
2379
2380static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2381 void *cookie)
2382{
2383 char *s = nd_get_link(nd);
2384 if (!IS_ERR(s))
Jeff Laytonf81700b2012-10-10 16:43:21 -04002385 kfree(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002386}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002388static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 .readlink = proc_self_readlink,
2390 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002391 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392};
2393
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002394/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002395 * proc base
2396 *
2397 * These are the directory entries in the root directory of /proc
2398 * that properly belong to the /proc filesystem, as they describe
2399 * describe something that is process related.
2400 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002401static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002402 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002403 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002404};
2405
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002406static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002407 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002408{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002409 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002410 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002411 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002412 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002413
2414 /* Allocate the inode */
2415 error = ERR_PTR(-ENOMEM);
2416 inode = new_inode(dir->i_sb);
2417 if (!inode)
2418 goto out;
2419
2420 /* Initialize the inode */
2421 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002422 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002423 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002424
2425 /*
2426 * grab the reference to the task.
2427 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002428 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002429 if (!ei->pid)
2430 goto out_iput;
2431
Eric W. Biederman801199c2006-10-02 02:18:48 -07002432 inode->i_mode = p->mode;
2433 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002434 set_nlink(inode, 2);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002435 if (S_ISLNK(inode->i_mode))
2436 inode->i_size = 64;
2437 if (p->iop)
2438 inode->i_op = p->iop;
2439 if (p->fop)
2440 inode->i_fop = p->fop;
2441 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002442 d_add(dentry, inode);
2443 error = NULL;
2444out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002445 return error;
2446out_iput:
2447 iput(inode);
2448 goto out;
2449}
2450
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002451static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2452{
2453 struct dentry *error;
2454 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002455 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002456
2457 error = ERR_PTR(-ENOENT);
2458
2459 if (!task)
2460 goto out_no_task;
2461
2462 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002463 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2464 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002465 if (p->len != dentry->d_name.len)
2466 continue;
2467 if (!memcmp(dentry->d_name.name, p->name, p->len))
2468 break;
2469 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002470 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002471 goto out;
2472
2473 error = proc_base_instantiate(dir, dentry, task, p);
2474
2475out:
2476 put_task_struct(task);
2477out_no_task:
2478 return error;
2479}
2480
Eric Dumazetc5141e62007-05-08 00:26:15 -07002481static int proc_base_fill_cache(struct file *filp, void *dirent,
2482 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002483{
2484 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2485 proc_base_instantiate, task, p);
2486}
2487
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002488#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002489static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002490{
Andrea Righi940389b2008-07-28 00:48:12 +02002491 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002492 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002493 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002494
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002495 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2496 if (result)
2497 return result;
2498
2499 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2500 result = -EACCES;
2501 goto out_unlock;
2502 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002503
Andrea Righi59954772008-07-27 17:29:15 +02002504 if (whole && lock_task_sighand(task, &flags)) {
2505 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002506
Andrea Righi59954772008-07-27 17:29:15 +02002507 task_io_accounting_add(&acct, &task->signal->ioac);
2508 while_each_thread(task, t)
2509 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002510
Andrea Righi59954772008-07-27 17:29:15 +02002511 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002512 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002513 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002514 "rchar: %llu\n"
2515 "wchar: %llu\n"
2516 "syscr: %llu\n"
2517 "syscw: %llu\n"
2518 "read_bytes: %llu\n"
2519 "write_bytes: %llu\n"
2520 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002521 (unsigned long long)acct.rchar,
2522 (unsigned long long)acct.wchar,
2523 (unsigned long long)acct.syscr,
2524 (unsigned long long)acct.syscw,
2525 (unsigned long long)acct.read_bytes,
2526 (unsigned long long)acct.write_bytes,
2527 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002528out_unlock:
2529 mutex_unlock(&task->signal->cred_guard_mutex);
2530 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002531}
Andrea Righi297c5d92008-07-25 01:48:49 -07002532
2533static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2534{
2535 return do_io_accounting(task, buffer, 0);
2536}
2537
2538static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2539{
2540 return do_io_accounting(task, buffer, 1);
2541}
2542#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002543
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002544#ifdef CONFIG_USER_NS
2545static int proc_id_map_open(struct inode *inode, struct file *file,
2546 struct seq_operations *seq_ops)
2547{
2548 struct user_namespace *ns = NULL;
2549 struct task_struct *task;
2550 struct seq_file *seq;
2551 int ret = -EINVAL;
2552
2553 task = get_proc_task(inode);
2554 if (task) {
2555 rcu_read_lock();
2556 ns = get_user_ns(task_cred_xxx(task, user_ns));
2557 rcu_read_unlock();
2558 put_task_struct(task);
2559 }
2560 if (!ns)
2561 goto err;
2562
2563 ret = seq_open(file, seq_ops);
2564 if (ret)
2565 goto err_put_ns;
2566
2567 seq = file->private_data;
2568 seq->private = ns;
2569
2570 return 0;
2571err_put_ns:
2572 put_user_ns(ns);
2573err:
2574 return ret;
2575}
2576
2577static int proc_id_map_release(struct inode *inode, struct file *file)
2578{
2579 struct seq_file *seq = file->private_data;
2580 struct user_namespace *ns = seq->private;
2581 put_user_ns(ns);
2582 return seq_release(inode, file);
2583}
2584
2585static int proc_uid_map_open(struct inode *inode, struct file *file)
2586{
2587 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2588}
2589
2590static int proc_gid_map_open(struct inode *inode, struct file *file)
2591{
2592 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2593}
2594
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002595static int proc_projid_map_open(struct inode *inode, struct file *file)
2596{
2597 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2598}
2599
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002600static const struct file_operations proc_uid_map_operations = {
2601 .open = proc_uid_map_open,
2602 .write = proc_uid_map_write,
2603 .read = seq_read,
2604 .llseek = seq_lseek,
2605 .release = proc_id_map_release,
2606};
2607
2608static const struct file_operations proc_gid_map_operations = {
2609 .open = proc_gid_map_open,
2610 .write = proc_gid_map_write,
2611 .read = seq_read,
2612 .llseek = seq_lseek,
2613 .release = proc_id_map_release,
2614};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002615
2616static const struct file_operations proc_projid_map_operations = {
2617 .open = proc_projid_map_open,
2618 .write = proc_projid_map_write,
2619 .read = seq_read,
2620 .llseek = seq_lseek,
2621 .release = proc_id_map_release,
2622};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002623#endif /* CONFIG_USER_NS */
2624
Kees Cook47830722008-10-06 03:11:58 +04002625static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2626 struct pid *pid, struct task_struct *task)
2627{
Al Viroa9712bc2011-03-23 15:52:50 -04002628 int err = lock_trace(task);
2629 if (!err) {
2630 seq_printf(m, "%08x\n", task->personality);
2631 unlock_trace(task);
2632 }
2633 return err;
Kees Cook47830722008-10-06 03:11:58 +04002634}
2635
Eric W. Biederman801199c2006-10-02 02:18:48 -07002636/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002637 * Thread groups
2638 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002639static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002640static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002641
Eric Dumazetc5141e62007-05-08 00:26:15 -07002642static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002643 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2644 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002645#ifdef CONFIG_CHECKPOINT_RESTORE
2646 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2647#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002648 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002649 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002650#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002651 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002652#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002653 REG("environ", S_IRUSR, proc_environ_operations),
2654 INF("auxv", S_IRUSR, proc_pid_auxv),
2655 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002656 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002657 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002658#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002659 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002660#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002661#ifdef CONFIG_SCHED_AUTOGROUP
2662 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2663#endif
john stultz4614a696b2009-12-14 18:00:05 -08002664 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002665#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002666 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002667#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002668 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2669 ONE("stat", S_IRUGO, proc_tgid_stat),
2670 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002671 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002672#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002673 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002674#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002675 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2676 LNK("cwd", proc_cwd_link),
2677 LNK("root", proc_root_link),
2678 LNK("exe", proc_exe_link),
2679 REG("mounts", S_IRUGO, proc_mounts_operations),
2680 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2681 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002682#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002683 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002684 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002685 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002686#endif
2687#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002688 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002689#endif
2690#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002691 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002692#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002693#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002694 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002695#endif
2696#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002697 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002698#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002699#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002700 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002701#endif
Paul Menage8793d852007-10-18 23:39:39 -07002702#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002703 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002704#endif
Paul Menagea4243162007-10-18 23:39:35 -07002705#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002706 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002707#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002708 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002709 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002710 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002711#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002712 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2713 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002714#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002715#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002716 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002717#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002718#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002719 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002720#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002721#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002722 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002723#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002724#ifdef CONFIG_HARDWALL
2725 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2726#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002727#ifdef CONFIG_USER_NS
2728 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2729 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002730 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002731#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002732};
2733
2734static int proc_tgid_base_readdir(struct file * filp,
2735 void * dirent, filldir_t filldir)
2736{
2737 return proc_pident_readdir(filp,dirent,filldir,
2738 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2739}
2740
Arjan van de Ven00977a52007-02-12 00:55:34 -08002741static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002742 .read = generic_read_dir,
2743 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002744 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002745};
2746
Al Viro00cd8dd2012-06-10 17:13:09 -04002747static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2748{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002749 return proc_pident_lookup(dir, dentry,
2750 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002751}
2752
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002753static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002754 .lookup = proc_tgid_base_lookup,
2755 .getattr = pid_getattr,
2756 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002757 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002758};
2759
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002760static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002762 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002763 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002764 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Eric W. Biederman48e64842006-06-26 00:25:48 -07002766 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002767 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2768 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002769 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002770 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002771 d_drop(dentry);
2772 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Eric W. Biederman48e64842006-06-26 00:25:48 -07002775 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002776 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2777 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002778 if (!leader)
2779 goto out;
2780
2781 name.name = "task";
2782 name.len = strlen(name.name);
2783 dir = d_hash_and_lookup(leader, &name);
2784 if (!dir)
2785 goto out_put_leader;
2786
2787 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002788 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002789 dentry = d_hash_and_lookup(dir, &name);
2790 if (dentry) {
2791 shrink_dcache_parent(dentry);
2792 d_drop(dentry);
2793 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002795
2796 dput(dir);
2797out_put_leader:
2798 dput(leader);
2799out:
2800 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
Randy Dunlap0895e912007-10-21 21:00:10 -07002803/**
2804 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2805 * @task: task that should be flushed.
2806 *
2807 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002808 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002809 * in. This call is supposed to do all of this job.
2810 *
2811 * Looks in the dcache for
2812 * /proc/@pid
2813 * /proc/@tgid/task/@pid
2814 * if either directory is present flushes it and all of it'ts children
2815 * from the dcache.
2816 *
2817 * It is safe and reasonable to cache /proc entries for a task until
2818 * that task exits. After that they just clog up the dcache with
2819 * useless entries, possibly causing useful dcache entries to be
2820 * flushed instead. This routine is proved to flush those useless
2821 * dcache entries at process exit time.
2822 *
2823 * NOTE: This routine is just an optimization so it does not guarantee
2824 * that no dcache entries will exist at process exit time it
2825 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002826 */
2827
2828void proc_flush_task(struct task_struct *task)
2829{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002830 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002831 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002832 struct upid *upid;
2833
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002834 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002835 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002836
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002837 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002838 upid = &pid->numbers[i];
2839 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002840 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002841 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002842
2843 upid = &pid->numbers[pid->level];
2844 if (upid->nr == 1)
2845 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002846}
2847
Adrian Bunk9711ef92006-12-06 20:38:31 -08002848static struct dentry *proc_pid_instantiate(struct inode *dir,
2849 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002850 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002851{
2852 struct dentry *error = ERR_PTR(-ENOENT);
2853 struct inode *inode;
2854
Eric W. Biederman61a28782006-10-02 02:18:49 -07002855 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002856 if (!inode)
2857 goto out;
2858
2859 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2860 inode->i_op = &proc_tgid_base_inode_operations;
2861 inode->i_fop = &proc_tgid_base_operations;
2862 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002863
Miklos Szeredibfe86842011-10-28 14:13:29 +02002864 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2865 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002866
Nick Pigginfb045ad2011-01-07 17:49:55 +11002867 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002868
2869 d_add(dentry, inode);
2870 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002871 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002872 error = NULL;
2873out:
2874 return error;
2875}
2876
Al Viro00cd8dd2012-06-10 17:13:09 -04002877struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Dan Carpenter73d36462010-05-26 14:43:25 -07002879 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002882 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Eric W. Biederman801199c2006-10-02 02:18:48 -07002884 result = proc_base_lookup(dir, dentry);
2885 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2886 goto out;
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 tgid = name_to_int(dentry);
2889 if (tgid == ~0U)
2890 goto out;
2891
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002892 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002893 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002894 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (task)
2896 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002897 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (!task)
2899 goto out;
2900
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002901 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002902 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002904 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905}
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002908 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002909 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002911struct tgid_iter {
2912 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002913 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002914};
2915static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2916{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002917 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002919 if (iter.task)
2920 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002921 rcu_read_lock();
2922retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002923 iter.task = NULL;
2924 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002925 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002926 iter.tgid = pid_nr_ns(pid, ns);
2927 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002928 /* What we to know is if the pid we have find is the
2929 * pid of a thread_group_leader. Testing for task
2930 * being a thread_group_leader is the obvious thing
2931 * todo but there is a window when it fails, due to
2932 * the pid transfer logic in de_thread.
2933 *
2934 * So we perform the straight forward test of seeing
2935 * if the pid we have found is the pid of a thread
2936 * group leader, and don't worry if the task we have
2937 * found doesn't happen to be a thread group leader.
2938 * As we don't care in the case of readdir.
2939 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002940 if (!iter.task || !has_group_leader_pid(iter.task)) {
2941 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002942 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002943 }
2944 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002946 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002947 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
2949
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002950#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Eric W. Biederman61a28782006-10-02 02:18:49 -07002952static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002953 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002954{
2955 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002956 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002957 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002958 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002959}
2960
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002961static int fake_filldir(void *buf, const char *name, int namelen,
2962 loff_t offset, u64 ino, unsigned d_type)
2963{
2964 return 0;
2965}
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967/* for the /proc/ directory itself, after non-process stuff has been done */
2968int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2969{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07002970 unsigned int nr;
2971 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002972 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002973 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002974 filldir_t __filldir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07002976 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
2977 goto out_no_task;
2978 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2979
2980 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002981 if (!reaper)
2982 goto out_no_task;
2983
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002984 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002985 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002986 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002990 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002991 iter.task = NULL;
2992 iter.tgid = filp->f_pos - TGID_OFFSET;
2993 for (iter = next_tgid(ns, iter);
2994 iter.task;
2995 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002996 if (has_pid_permissions(ns, iter.task, 2))
2997 __filldir = filldir;
2998 else
2999 __filldir = fake_filldir;
3000
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003001 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003002 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003003 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003007 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3008out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003009 put_task_struct(reaper);
3010out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return 0;
3012}
3013
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003014/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015 * Tasks
3016 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003017static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003018 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003019 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003020 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003021 REG("environ", S_IRUSR, proc_environ_operations),
3022 INF("auxv", S_IRUSR, proc_pid_auxv),
3023 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003024 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003025 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003026#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003027 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003028#endif
john stultz4614a696b2009-12-14 18:00:05 -08003029 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003030#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003031 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003032#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003033 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3034 ONE("stat", S_IRUGO, proc_tid_stat),
3035 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003036 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003037#ifdef CONFIG_CHECKPOINT_RESTORE
3038 REG("children", S_IRUGO, proc_tid_children_operations),
3039#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003040#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003041 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003042#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003043 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3044 LNK("cwd", proc_cwd_link),
3045 LNK("root", proc_root_link),
3046 LNK("exe", proc_exe_link),
3047 REG("mounts", S_IRUGO, proc_mounts_operations),
3048 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003049#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003050 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003051 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003052 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003053#endif
3054#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003055 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003056#endif
3057#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003058 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003059#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003060#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003061 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003062#endif
3063#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003064 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003065#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003066#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003067 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003068#endif
Paul Menage8793d852007-10-18 23:39:39 -07003069#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003070 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003071#endif
Paul Menagea4243162007-10-18 23:39:35 -07003072#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003073 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003074#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003075 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003076 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003077 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003078#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003079 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003080 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003081#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003082#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003083 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003084#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003085#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003086 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003087#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003088#ifdef CONFIG_HARDWALL
3089 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3090#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003091#ifdef CONFIG_USER_NS
3092 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3093 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003094 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003095#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003096};
3097
3098static int proc_tid_base_readdir(struct file * filp,
3099 void * dirent, filldir_t filldir)
3100{
3101 return proc_pident_readdir(filp,dirent,filldir,
3102 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3103}
3104
Al Viro00cd8dd2012-06-10 17:13:09 -04003105static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3106{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003107 return proc_pident_lookup(dir, dentry,
3108 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003109}
3110
Arjan van de Ven00977a52007-02-12 00:55:34 -08003111static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003112 .read = generic_read_dir,
3113 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003114 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003115};
3116
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003117static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003118 .lookup = proc_tid_base_lookup,
3119 .getattr = pid_getattr,
3120 .setattr = proc_setattr,
3121};
3122
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003123static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003124 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003125{
3126 struct dentry *error = ERR_PTR(-ENOENT);
3127 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003128 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003129
3130 if (!inode)
3131 goto out;
3132 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3133 inode->i_op = &proc_tid_base_inode_operations;
3134 inode->i_fop = &proc_tid_base_operations;
3135 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003136
Miklos Szeredibfe86842011-10-28 14:13:29 +02003137 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3138 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003139
Nick Pigginfb045ad2011-01-07 17:49:55 +11003140 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003141
3142 d_add(dentry, inode);
3143 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003144 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003145 error = NULL;
3146out:
3147 return error;
3148}
3149
Al Viro00cd8dd2012-06-10 17:13:09 -04003150static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003151{
3152 struct dentry *result = ERR_PTR(-ENOENT);
3153 struct task_struct *task;
3154 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003155 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003156 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003157
3158 if (!leader)
3159 goto out_no_task;
3160
3161 tid = name_to_int(dentry);
3162 if (tid == ~0U)
3163 goto out;
3164
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003165 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003166 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003167 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003168 if (task)
3169 get_task_struct(task);
3170 rcu_read_unlock();
3171 if (!task)
3172 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003173 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003174 goto out_drop_task;
3175
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003176 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003177out_drop_task:
3178 put_task_struct(task);
3179out:
3180 put_task_struct(leader);
3181out_no_task:
3182 return result;
3183}
3184
3185/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003186 * Find the first tid of a thread group to return to user space.
3187 *
3188 * Usually this is just the thread group leader, but if the users
3189 * buffer was too small or there was a seek into the middle of the
3190 * directory we have more work todo.
3191 *
3192 * In the case of a short read we start with find_task_by_pid.
3193 *
3194 * In the case of a seek we start with the leader and walk nr
3195 * threads past it.
3196 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003197static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003198 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003199{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003200 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003201
Eric W. Biedermancc288732006-06-26 00:26:01 -07003202 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003203 /* Attempt to start with the pid of a thread */
3204 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003205 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003206 if (pos && (pos->group_leader == leader))
3207 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003208 }
3209
3210 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003211 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003212 if (nr && nr >= get_nr_threads(leader))
3213 goto out;
3214
3215 /* If we haven't found our starting place yet start
3216 * with the leader and walk nr threads forward.
3217 */
3218 for (pos = leader; nr > 0; --nr) {
3219 pos = next_thread(pos);
3220 if (pos == leader) {
3221 pos = NULL;
3222 goto out;
3223 }
3224 }
3225found:
3226 get_task_struct(pos);
3227out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003228 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003229 return pos;
3230}
3231
3232/*
3233 * Find the next thread in the thread list.
3234 * Return NULL if there is an error or no next thread.
3235 *
3236 * The reference to the input task_struct is released.
3237 */
3238static struct task_struct *next_tid(struct task_struct *start)
3239{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003240 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003241 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003242 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003243 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003244 if (thread_group_leader(pos))
3245 pos = NULL;
3246 else
3247 get_task_struct(pos);
3248 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003249 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003250 put_task_struct(start);
3251 return pos;
3252}
3253
Eric W. Biederman61a28782006-10-02 02:18:49 -07003254static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3255 struct task_struct *task, int tid)
3256{
3257 char name[PROC_NUMBUF];
3258 int len = snprintf(name, sizeof(name), "%d", tid);
3259 return proc_fill_cache(filp, dirent, filldir, name, len,
3260 proc_task_instantiate, task, NULL);
3261}
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263/* for the /proc/TGID/task/ directories */
3264static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3265{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003266 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003268 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003269 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 int retval = -ENOENT;
3271 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003272 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003273 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003275 task = get_proc_task(inode);
3276 if (!task)
3277 goto out_no_task;
3278 rcu_read_lock();
3279 if (pid_alive(task)) {
3280 leader = task->group_leader;
3281 get_task_struct(leader);
3282 }
3283 rcu_read_unlock();
3284 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003285 if (!leader)
3286 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 retval = 0;
3288
Linus Torvaldsee568b22009-03-17 10:02:35 -07003289 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 case 0:
3291 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003292 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003294 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 /* fall through */
3296 case 1:
3297 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003298 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003300 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 /* fall through */
3302 }
3303
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003304 /* f_version caches the tgid value that the last readdir call couldn't
3305 * return. lseek aka telldir automagically resets f_version to 0.
3306 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003307 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003308 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003309 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003310 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003311 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003312 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003313 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003314 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003315 /* returning this tgid failed, save it as the first
3316 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003317 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003318 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
3322out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003323 put_task_struct(leader);
3324out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return retval;
3326}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003327
3328static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3329{
3330 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003331 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003332 generic_fillattr(inode, stat);
3333
Eric W. Biederman99f89552006-06-26 00:25:55 -07003334 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003335 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003336 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003337 }
3338
3339 return 0;
3340}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003341
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003342static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003343 .lookup = proc_task_lookup,
3344 .getattr = proc_task_getattr,
3345 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003346 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003347};
3348
Arjan van de Ven00977a52007-02-12 00:55:34 -08003349static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003350 .read = generic_read_dir,
3351 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003352 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003353};