blob: 5485a5388ecb2919ecb5033cbf99d1d22916a72b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070076#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/cpuset.h>
78#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050079#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070080#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070081#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070082#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070083#include <linux/pid_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040084#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090085#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080086#include <linux/flex_array.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040087#ifdef CONFIG_HARDWALL
88#include <asm/hardwall.h>
89#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080090#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include "internal.h"
92
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070093/* NOTE:
94 * Implementing inode permission operations in /proc is almost
95 * certainly an error. Permission checks need to happen during
96 * each system call not at open time. The reason is that most of
97 * what we wish to check for permissions in /proc varies at runtime.
98 *
99 * The classic example of a problem is opening file descriptors
100 * in /proc for a task before it execs a suid executable.
101 */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700105 int len;
Al Virod161a132011-07-24 03:36:29 -0400106 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800107 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800108 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700109 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
Eric W. Biederman61a28782006-10-02 02:18:49 -0700112#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700114 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 .mode = MODE, \
116 .iop = IOP, \
117 .fop = FOP, \
118 .op = OP, \
119}
120
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300121#define DIR(NAME, MODE, iops, fops) \
122 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
123#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700124 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700125 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300126 { .proc_get_link = get_link } )
127#define REG(NAME, MODE, fops) \
128 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
129#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700130 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700131 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300132 { .proc_read = read } )
133#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Pavel Emelyanov640708a2012-01-10 15:11:23 -0800138static int proc_fd_permission(struct inode *inode, int mask);
139
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
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400201static struct mm_struct *__check_mem_permission(struct task_struct *task)
Roland McGrath638fa2022008-04-29 01:01:38 -0700202{
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400203 struct mm_struct *mm;
204
205 mm = get_task_mm(task);
206 if (!mm)
207 return ERR_PTR(-EINVAL);
208
Roland McGrath638fa2022008-04-29 01:01:38 -0700209 /*
210 * A task can always look at itself, in case it chooses
211 * to use system calls instead of load instructions.
212 */
213 if (task == current)
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400214 return mm;
Roland McGrath638fa2022008-04-29 01:01:38 -0700215
216 /*
217 * If current is actively ptrace'ing, and would also be
218 * permitted to freshly attach with ptrace now, permit it.
219 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700220 if (task_is_stopped_or_traced(task)) {
221 int match;
222 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +0200223 match = (ptrace_parent(task) == current);
Roland McGrath0d094ef2008-07-25 19:45:49 -0700224 rcu_read_unlock();
225 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400226 return mm;
Roland McGrath0d094ef2008-07-25 19:45:49 -0700227 }
Roland McGrath638fa2022008-04-29 01:01:38 -0700228
229 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300230 * No one else is allowed.
Roland McGrath638fa2022008-04-29 01:01:38 -0700231 */
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400232 mmput(mm);
233 return ERR_PTR(-EPERM);
Roland McGrath638fa2022008-04-29 01:01:38 -0700234}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Stephen Wilson18f661b2011-03-13 15:49:22 -0400236/*
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400237 * If current may access user memory in @task return a reference to the
238 * corresponding mm, otherwise ERR_PTR.
Stephen Wilson18f661b2011-03-13 15:49:22 -0400239 */
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400240static struct mm_struct *check_mem_permission(struct task_struct *task)
Stephen Wilson18f661b2011-03-13 15:49:22 -0400241{
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400242 struct mm_struct *mm;
Stephen Wilson18f661b2011-03-13 15:49:22 -0400243 int err;
244
245 /*
246 * Avoid racing if task exec's as we might get a new mm but validate
247 * against old credentials.
248 */
249 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
250 if (err)
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400251 return ERR_PTR(err);
Stephen Wilson18f661b2011-03-13 15:49:22 -0400252
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400253 mm = __check_mem_permission(task);
Stephen Wilson18f661b2011-03-13 15:49:22 -0400254 mutex_unlock(&task->signal->cred_guard_mutex);
255
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400256 return mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Al Viro831830b2008-01-02 14:09:57 +0000259struct mm_struct *mm_for_maps(struct task_struct *task)
260{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200261 struct mm_struct *mm;
Al Viroec6fd8a2011-02-15 22:22:54 -0500262 int err;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200263
Al Viroec6fd8a2011-02-15 22:22:54 -0500264 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
265 if (err)
266 return ERR_PTR(err);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200267
268 mm = get_task_mm(task);
269 if (mm && mm != current->mm &&
270 !ptrace_may_access(task, PTRACE_MODE_READ)) {
271 mmput(mm);
Al Viroec6fd8a2011-02-15 22:22:54 -0500272 mm = ERR_PTR(-EACCES);
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200273 }
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700274 mutex_unlock(&task->signal->cred_guard_mutex);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200275
Al Viro831830b2008-01-02 14:09:57 +0000276 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static int proc_pid_cmdline(struct task_struct *task, char * buffer)
280{
281 int res = 0;
282 unsigned int len;
283 struct mm_struct *mm = get_task_mm(task);
284 if (!mm)
285 goto out;
286 if (!mm->arg_end)
287 goto out_mm; /* Shh! No looking before we're done */
288
289 len = mm->arg_end - mm->arg_start;
290
291 if (len > PAGE_SIZE)
292 len = PAGE_SIZE;
293
294 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
295
296 // If the nul at the end of args has been overwritten, then
297 // assume application is using setproctitle(3).
298 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
299 len = strnlen(buffer, res);
300 if (len < res) {
301 res = len;
302 } else {
303 len = mm->env_end - mm->env_start;
304 if (len > PAGE_SIZE - res)
305 len = PAGE_SIZE - res;
306 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
307 res = strnlen(buffer, res);
308 }
309 }
310out_mm:
311 mmput(mm);
312out:
313 return res;
314}
315
316static int proc_pid_auxv(struct task_struct *task, char *buffer)
317{
Al Viro2fadaef2011-02-15 22:52:11 -0500318 struct mm_struct *mm = mm_for_maps(task);
319 int res = PTR_ERR(mm);
320 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300322 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300324 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 res = nwords * sizeof(mm->saved_auxv[0]);
326 if (res > PAGE_SIZE)
327 res = PAGE_SIZE;
328 memcpy(buffer, mm->saved_auxv, res);
329 mmput(mm);
330 }
331 return res;
332}
333
334
335#ifdef CONFIG_KALLSYMS
336/*
337 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
338 * Returns the resolved symbol. If that fails, simply return the address.
339 */
340static int proc_pid_wchan(struct task_struct *task, char *buffer)
341{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700342 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700343 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 wchan = get_wchan(task);
346
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700347 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600348 if (!ptrace_may_access(task, PTRACE_MODE_READ))
349 return 0;
350 else
351 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700352 else
353 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355#endif /* CONFIG_KALLSYMS */
356
Al Viroa9712bc2011-03-23 15:52:50 -0400357static int lock_trace(struct task_struct *task)
358{
359 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
360 if (err)
361 return err;
362 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
363 mutex_unlock(&task->signal->cred_guard_mutex);
364 return -EPERM;
365 }
366 return 0;
367}
368
369static void unlock_trace(struct task_struct *task)
370{
371 mutex_unlock(&task->signal->cred_guard_mutex);
372}
373
Ken Chen2ec220e2008-11-10 11:26:08 +0300374#ifdef CONFIG_STACKTRACE
375
376#define MAX_STACK_TRACE_DEPTH 64
377
378static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
379 struct pid *pid, struct task_struct *task)
380{
381 struct stack_trace trace;
382 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400383 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300384 int i;
385
386 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
387 if (!entries)
388 return -ENOMEM;
389
390 trace.nr_entries = 0;
391 trace.max_entries = MAX_STACK_TRACE_DEPTH;
392 trace.entries = entries;
393 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300394
Al Viroa9712bc2011-03-23 15:52:50 -0400395 err = lock_trace(task);
396 if (!err) {
397 save_stack_trace_tsk(task, &trace);
398
399 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700400 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400401 (void *)entries[i], (void *)entries[i]);
402 }
403 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300404 }
405 kfree(entries);
406
Al Viroa9712bc2011-03-23 15:52:50 -0400407 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300408}
409#endif
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#ifdef CONFIG_SCHEDSTATS
412/*
413 * Provides /proc/PID/schedstat
414 */
415static int proc_pid_schedstat(struct task_struct *task, char *buffer)
416{
Balbir Singh172ba842007-07-09 18:52:00 +0200417 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100418 (unsigned long long)task->se.sum_exec_runtime,
419 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200420 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422#endif
423
Arjan van de Ven97455122008-01-25 21:08:34 +0100424#ifdef CONFIG_LATENCYTOP
425static int lstats_show_proc(struct seq_file *m, void *v)
426{
427 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800428 struct inode *inode = m->private;
429 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100430
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800431 if (!task)
432 return -ESRCH;
433 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100434 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800435 struct latency_record *lr = &task->latency_record[i];
436 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100437 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800438 seq_printf(m, "%i %li %li",
439 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100440 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800441 unsigned long bt = lr->backtrace[q];
442 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100443 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800444 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100445 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800446 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100447 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800448 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100449 }
450
451 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800452 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100453 return 0;
454}
455
456static int lstats_open(struct inode *inode, struct file *file)
457{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800458 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800459}
460
Arjan van de Ven97455122008-01-25 21:08:34 +0100461static ssize_t lstats_write(struct file *file, const char __user *buf,
462 size_t count, loff_t *offs)
463{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800464 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100465
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800466 if (!task)
467 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100468 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800469 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100470
471 return count;
472}
473
474static const struct file_operations proc_lstats_operations = {
475 .open = lstats_open,
476 .read = seq_read,
477 .write = lstats_write,
478 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800479 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100480};
481
482#endif
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static int proc_oom_score(struct task_struct *task, char *buffer)
485{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200486 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700488 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200489 if (pid_alive(task))
David Rientjesa63d83f2010-08-09 17:19:46 -0700490 points = oom_badness(task, NULL, NULL,
491 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700492 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return sprintf(buffer, "%lu\n", points);
494}
495
Neil Hormand85f50d2007-10-18 23:40:37 -0700496struct limit_names {
497 char *name;
498 char *unit;
499};
500
501static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700502 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700503 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
504 [RLIMIT_DATA] = {"Max data size", "bytes"},
505 [RLIMIT_STACK] = {"Max stack size", "bytes"},
506 [RLIMIT_CORE] = {"Max core file size", "bytes"},
507 [RLIMIT_RSS] = {"Max resident set", "bytes"},
508 [RLIMIT_NPROC] = {"Max processes", "processes"},
509 [RLIMIT_NOFILE] = {"Max open files", "files"},
510 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
511 [RLIMIT_AS] = {"Max address space", "bytes"},
512 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
513 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
514 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
515 [RLIMIT_NICE] = {"Max nice priority", NULL},
516 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800517 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700518};
519
520/* Display limits for a process */
521static int proc_pid_limits(struct task_struct *task, char *buffer)
522{
523 unsigned int i;
524 int count = 0;
525 unsigned long flags;
526 char *bufptr = buffer;
527
528 struct rlimit rlim[RLIM_NLIMITS];
529
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400530 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700531 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700532 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
533 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700534
535 /*
536 * print the file header
537 */
538 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
539 "Limit", "Soft Limit", "Hard Limit", "Units");
540
541 for (i = 0; i < RLIM_NLIMITS; i++) {
542 if (rlim[i].rlim_cur == RLIM_INFINITY)
543 count += sprintf(&bufptr[count], "%-25s %-20s ",
544 lnames[i].name, "unlimited");
545 else
546 count += sprintf(&bufptr[count], "%-25s %-20lu ",
547 lnames[i].name, rlim[i].rlim_cur);
548
549 if (rlim[i].rlim_max == RLIM_INFINITY)
550 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
551 else
552 count += sprintf(&bufptr[count], "%-20lu ",
553 rlim[i].rlim_max);
554
555 if (lnames[i].unit)
556 count += sprintf(&bufptr[count], "%-10s\n",
557 lnames[i].unit);
558 else
559 count += sprintf(&bufptr[count], "\n");
560 }
561
562 return count;
563}
564
Roland McGrathebcb6732008-07-25 19:46:00 -0700565#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
566static int proc_pid_syscall(struct task_struct *task, char *buffer)
567{
568 long nr;
569 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400570 int res = lock_trace(task);
571 if (res)
572 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700573
574 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400575 res = sprintf(buffer, "running\n");
576 else if (nr < 0)
577 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
578 else
579 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700580 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
581 nr,
582 args[0], args[1], args[2], args[3], args[4], args[5],
583 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400584 unlock_trace(task);
585 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700586}
587#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/************************************************************************/
590/* Here the fs part begins */
591/************************************************************************/
592
593/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700594static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700596 struct task_struct *task;
597 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700598 /* Allow access to a task's file descriptors if it is us or we
599 * may use ptrace attach to the process and find out that
600 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700601 */
602 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700603 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400604 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700605 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700606 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700607 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800610int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700611{
612 int error;
613 struct inode *inode = dentry->d_inode;
614
615 if (attr->ia_valid & ATTR_MODE)
616 return -EPERM;
617
618 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200619 if (error)
620 return error;
621
622 if ((attr->ia_valid & ATTR_SIZE) &&
623 attr->ia_size != i_size_read(inode)) {
624 error = vmtruncate(inode, attr->ia_size);
625 if (error)
626 return error;
627 }
628
629 setattr_copy(inode, attr);
630 mark_inode_dirty(inode);
631 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700632}
633
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800634/*
635 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
636 * or euid/egid (for hide_pid_min=2)?
637 */
638static bool has_pid_permissions(struct pid_namespace *pid,
639 struct task_struct *task,
640 int hide_pid_min)
641{
642 if (pid->hide_pid < hide_pid_min)
643 return true;
644 if (in_group_p(pid->pid_gid))
645 return true;
646 return ptrace_may_access(task, PTRACE_MODE_READ);
647}
648
649
650static int proc_pid_permission(struct inode *inode, int mask)
651{
652 struct pid_namespace *pid = inode->i_sb->s_fs_info;
653 struct task_struct *task;
654 bool has_perms;
655
656 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800657 if (!task)
658 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800659 has_perms = has_pid_permissions(pid, task, 1);
660 put_task_struct(task);
661
662 if (!has_perms) {
663 if (pid->hide_pid == 2) {
664 /*
665 * Let's make getdents(), stat(), and open()
666 * consistent with each other. If a process
667 * may not stat() a file, it shouldn't be seen
668 * in procfs at all.
669 */
670 return -ENOENT;
671 }
672
673 return -EPERM;
674 }
675 return generic_permission(inode, mask);
676}
677
678
679
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800680static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700681 .setattr = proc_setattr,
682};
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
685
686static ssize_t proc_info_read(struct file * file, char __user * buf,
687 size_t count, loff_t *ppos)
688{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800689 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 unsigned long page;
691 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700692 struct task_struct *task = get_proc_task(inode);
693
694 length = -ESRCH;
695 if (!task)
696 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (count > PROC_BLOCK_SIZE)
699 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700700
701 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700702 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700703 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 length = PROC_I(inode)->op.proc_read(task, (char*)page);
706
707 if (length >= 0)
708 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
709 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700710out:
711 put_task_struct(task);
712out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return length;
714}
715
Arjan van de Ven00977a52007-02-12 00:55:34 -0800716static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100718 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719};
720
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800721static int proc_single_show(struct seq_file *m, void *v)
722{
723 struct inode *inode = m->private;
724 struct pid_namespace *ns;
725 struct pid *pid;
726 struct task_struct *task;
727 int ret;
728
729 ns = inode->i_sb->s_fs_info;
730 pid = proc_pid(inode);
731 task = get_pid_task(pid, PIDTYPE_PID);
732 if (!task)
733 return -ESRCH;
734
735 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
736
737 put_task_struct(task);
738 return ret;
739}
740
741static int proc_single_open(struct inode *inode, struct file *filp)
742{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800743 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800744}
745
746static const struct file_operations proc_single_file_operations = {
747 .open = proc_single_open,
748 .read = seq_read,
749 .llseek = seq_lseek,
750 .release = single_release,
751};
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753static int mem_open(struct inode* inode, struct file* file)
754{
755 file->private_data = (void*)((long)current->self_exec_id);
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700756 /* OK to pass negative loff_t, we can catch out-of-range */
757 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759}
760
761static ssize_t mem_read(struct file * file, char __user * buf,
762 size_t count, loff_t *ppos)
763{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800764 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 char *page;
766 unsigned long src = *ppos;
767 int ret = -ESRCH;
768 struct mm_struct *mm;
769
Eric W. Biederman99f89552006-06-26 00:25:55 -0700770 if (!task)
771 goto out_no_task;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700774 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!page)
776 goto out;
777
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400778 mm = check_mem_permission(task);
779 ret = PTR_ERR(mm);
780 if (IS_ERR(mm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto out_free;
782
783 ret = -EIO;
784
785 if (file->private_data != (void*)((long)current->self_exec_id))
786 goto out_put;
787
788 ret = 0;
789
790 while (count > 0) {
791 int this_len, retval;
792
793 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400794 retval = access_remote_vm(mm, src, page, this_len, 0);
795 if (!retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!ret)
797 ret = -EIO;
798 break;
799 }
800
801 if (copy_to_user(buf, page, retval)) {
802 ret = -EFAULT;
803 break;
804 }
805
806 ret += retval;
807 src += retval;
808 buf += retval;
809 count -= retval;
810 }
811 *ppos = src;
812
813out_put:
814 mmput(mm);
815out_free:
816 free_page((unsigned long) page);
817out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700818 put_task_struct(task);
819out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return ret;
821}
822
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800823static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 size_t count, loff_t *ppos)
825{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700826 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800828 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 unsigned long dst = *ppos;
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400830 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Eric W. Biederman99f89552006-06-26 00:25:55 -0700832 copied = -ESRCH;
833 if (!task)
834 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Eric W. Biederman99f89552006-06-26 00:25:55 -0700836 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700837 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (!page)
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700839 goto out_task;
840
841 mm = check_mem_permission(task);
842 copied = PTR_ERR(mm);
843 if (IS_ERR(mm))
844 goto out_free;
845
846 copied = -EIO;
847 if (file->private_data != (void *)((long)current->self_exec_id))
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400848 goto out_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700850 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 while (count > 0) {
852 int this_len, retval;
853
854 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
855 if (copy_from_user(page, buf, this_len)) {
856 copied = -EFAULT;
857 break;
858 }
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400859 retval = access_remote_vm(mm, dst, page, this_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (!retval) {
861 if (!copied)
862 copied = -EIO;
863 break;
864 }
865 copied += retval;
866 buf += retval;
867 dst += retval;
868 count -= retval;
869 }
870 *ppos = dst;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700871
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400872out_mm:
873 mmput(mm);
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700874out_free:
875 free_page((unsigned long) page);
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400876out_task:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700877 put_task_struct(task);
878out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return copied;
880}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Matt Mackall85863e42008-02-04 22:29:04 -0800882loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 switch (orig) {
885 case 0:
886 file->f_pos = offset;
887 break;
888 case 1:
889 file->f_pos += offset;
890 break;
891 default:
892 return -EINVAL;
893 }
894 force_successful_syscall_return();
895 return file->f_pos;
896}
897
Arjan van de Ven00977a52007-02-12 00:55:34 -0800898static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 .llseek = mem_lseek,
900 .read = mem_read,
901 .write = mem_write,
902 .open = mem_open,
903};
904
James Pearson315e28c2007-10-16 23:30:17 -0700905static ssize_t environ_read(struct file *file, char __user *buf,
906 size_t count, loff_t *ppos)
907{
908 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
909 char *page;
910 unsigned long src = *ppos;
911 int ret = -ESRCH;
912 struct mm_struct *mm;
913
914 if (!task)
915 goto out_no_task;
916
James Pearson315e28c2007-10-16 23:30:17 -0700917 ret = -ENOMEM;
918 page = (char *)__get_free_page(GFP_TEMPORARY);
919 if (!page)
920 goto out;
921
James Pearson315e28c2007-10-16 23:30:17 -0700922
Al Virod6f64b82011-02-15 22:26:01 -0500923 mm = mm_for_maps(task);
924 ret = PTR_ERR(mm);
925 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700926 goto out_free;
927
Al Virod6f64b82011-02-15 22:26:01 -0500928 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -0700929 while (count > 0) {
930 int this_len, retval, max_len;
931
932 this_len = mm->env_end - (mm->env_start + src);
933
934 if (this_len <= 0)
935 break;
936
937 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
938 this_len = (this_len > max_len) ? max_len : this_len;
939
940 retval = access_process_vm(task, (mm->env_start + src),
941 page, this_len, 0);
942
943 if (retval <= 0) {
944 ret = retval;
945 break;
946 }
947
948 if (copy_to_user(buf, page, retval)) {
949 ret = -EFAULT;
950 break;
951 }
952
953 ret += retval;
954 src += retval;
955 buf += retval;
956 count -= retval;
957 }
958 *ppos = src;
959
960 mmput(mm);
961out_free:
962 free_page((unsigned long) page);
963out:
964 put_task_struct(task);
965out_no_task:
966 return ret;
967}
968
969static const struct file_operations proc_environ_operations = {
970 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100971 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700972};
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static ssize_t oom_adjust_read(struct file *file, char __user *buf,
975 size_t count, loff_t *ppos)
976{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800977 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700978 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700980 int oom_adjust = OOM_DISABLE;
981 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Eric W. Biederman99f89552006-06-26 00:25:55 -0700983 if (!task)
984 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700985
986 if (lock_task_sighand(task, &flags)) {
987 oom_adjust = task->signal->oom_adj;
988 unlock_task_sighand(task, &flags);
989 }
990
Eric W. Biederman99f89552006-06-26 00:25:55 -0700991 put_task_struct(task);
992
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700993 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700994
995 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
999 size_t count, loff_t *ppos)
1000{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001001 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001002 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001003 int oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001004 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001005 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001007 memset(buffer, 0, sizeof(buffer));
1008 if (count > sizeof(buffer) - 1)
1009 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001010 if (copy_from_user(buffer, buf, count)) {
1011 err = -EFAULT;
1012 goto out;
1013 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001014
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001015 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001016 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001017 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001018 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -07001019 oom_adjust != OOM_DISABLE) {
1020 err = -EINVAL;
1021 goto out;
1022 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001023
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001024 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001025 if (!task) {
1026 err = -ESRCH;
1027 goto out;
1028 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001029
Ying Han3d5992d2010-10-26 14:21:23 -07001030 task_lock(task);
1031 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001032 err = -EINVAL;
1033 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001034 }
1035
David Rientjesd19d5472010-10-26 14:21:26 -07001036 if (!lock_task_sighand(task, &flags)) {
1037 err = -ESRCH;
1038 goto err_task_lock;
1039 }
1040
1041 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1042 err = -EACCES;
1043 goto err_sighand;
1044 }
1045
David Rientjes51b1bd22010-08-09 17:19:47 -07001046 /*
1047 * Warn that /proc/pid/oom_adj is deprecated, see
1048 * Documentation/feature-removal-schedule.txt.
1049 */
Linus Torvaldsc2142702011-08-06 11:43:08 -07001050 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
David Rientjesbe8f6842011-07-25 17:12:18 -07001051 current->comm, task_pid_nr(current), task_pid_nr(task),
1052 task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001053 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -07001054 /*
1055 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1056 * value is always attainable.
1057 */
1058 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1059 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1060 else
1061 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1062 -OOM_DISABLE;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001063 trace_oom_score_adj_update(task);
David Rientjes723548b2010-10-26 14:21:25 -07001064err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001065 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001066err_task_lock:
1067 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001068 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001069out:
1070 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Arjan van de Ven00977a52007-02-12 00:55:34 -08001073static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 .read = oom_adjust_read,
1075 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001076 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077};
1078
David Rientjesa63d83f2010-08-09 17:19:46 -07001079static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1080 size_t count, loff_t *ppos)
1081{
1082 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1083 char buffer[PROC_NUMBUF];
1084 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1085 unsigned long flags;
1086 size_t len;
1087
1088 if (!task)
1089 return -ESRCH;
1090 if (lock_task_sighand(task, &flags)) {
1091 oom_score_adj = task->signal->oom_score_adj;
1092 unlock_task_sighand(task, &flags);
1093 }
1094 put_task_struct(task);
1095 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1096 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1097}
1098
1099static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1100 size_t count, loff_t *ppos)
1101{
1102 struct task_struct *task;
1103 char buffer[PROC_NUMBUF];
1104 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001105 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001106 int err;
1107
1108 memset(buffer, 0, sizeof(buffer));
1109 if (count > sizeof(buffer) - 1)
1110 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001111 if (copy_from_user(buffer, buf, count)) {
1112 err = -EFAULT;
1113 goto out;
1114 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001115
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001116 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001117 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001118 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001119 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001120 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1121 err = -EINVAL;
1122 goto out;
1123 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001124
1125 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001126 if (!task) {
1127 err = -ESRCH;
1128 goto out;
1129 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001130
Ying Han3d5992d2010-10-26 14:21:23 -07001131 task_lock(task);
1132 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001133 err = -EINVAL;
1134 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001135 }
David Rientjesd19d5472010-10-26 14:21:26 -07001136
1137 if (!lock_task_sighand(task, &flags)) {
1138 err = -ESRCH;
1139 goto err_task_lock;
1140 }
1141
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001142 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001143 !capable(CAP_SYS_RESOURCE)) {
1144 err = -EACCES;
1145 goto err_sighand;
1146 }
1147
David Rientjesa63d83f2010-08-09 17:19:46 -07001148 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001149 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1150 task->signal->oom_score_adj_min = oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001151 trace_oom_score_adj_update(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001152 /*
1153 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1154 * always attainable.
1155 */
1156 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1157 task->signal->oom_adj = OOM_DISABLE;
1158 else
1159 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1160 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001161err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001162 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001163err_task_lock:
1164 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001165 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001166out:
1167 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001168}
1169
1170static const struct file_operations proc_oom_score_adj_operations = {
1171 .read = oom_score_adj_read,
1172 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001173 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001174};
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176#ifdef CONFIG_AUDITSYSCALL
1177#define TMPBUFLEN 21
1178static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1179 size_t count, loff_t *ppos)
1180{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001181 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001182 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 ssize_t length;
1184 char tmpbuf[TMPBUFLEN];
1185
Eric W. Biederman99f89552006-06-26 00:25:55 -07001186 if (!task)
1187 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001189 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001190 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1192}
1193
1194static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1195 size_t count, loff_t *ppos)
1196{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001197 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 char *page, *tmp;
1199 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 uid_t loginuid;
1201
1202 if (!capable(CAP_AUDIT_CONTROL))
1203 return -EPERM;
1204
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001205 rcu_read_lock();
1206 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1207 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001209 }
1210 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Al Viroe0182902006-05-18 08:28:02 -04001212 if (count >= PAGE_SIZE)
1213 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (*ppos != 0) {
1216 /* No partial writes. */
1217 return -EINVAL;
1218 }
Mel Gormane12ba742007-10-16 01:25:52 -07001219 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!page)
1221 return -ENOMEM;
1222 length = -EFAULT;
1223 if (copy_from_user(page, buf, count))
1224 goto out_free_page;
1225
Al Viroe0182902006-05-18 08:28:02 -04001226 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 loginuid = simple_strtoul(page, &tmp, 10);
1228 if (tmp == page) {
1229 length = -EINVAL;
1230 goto out_free_page;
1231
1232 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001233 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (likely(length == 0))
1235 length = count;
1236
1237out_free_page:
1238 free_page((unsigned long) page);
1239 return length;
1240}
1241
Arjan van de Ven00977a52007-02-12 00:55:34 -08001242static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 .read = proc_loginuid_read,
1244 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001245 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246};
Eric Paris1e0bd752008-03-13 08:15:31 -04001247
1248static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1249 size_t count, loff_t *ppos)
1250{
1251 struct inode * inode = file->f_path.dentry->d_inode;
1252 struct task_struct *task = get_proc_task(inode);
1253 ssize_t length;
1254 char tmpbuf[TMPBUFLEN];
1255
1256 if (!task)
1257 return -ESRCH;
1258 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1259 audit_get_sessionid(task));
1260 put_task_struct(task);
1261 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1262}
1263
1264static const struct file_operations proc_sessionid_operations = {
1265 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001266 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001267};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#endif
1269
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001270#ifdef CONFIG_FAULT_INJECTION
1271static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1272 size_t count, loff_t *ppos)
1273{
1274 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1275 char buffer[PROC_NUMBUF];
1276 size_t len;
1277 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001278
1279 if (!task)
1280 return -ESRCH;
1281 make_it_fail = task->make_it_fail;
1282 put_task_struct(task);
1283
1284 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001285
1286 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001287}
1288
1289static ssize_t proc_fault_inject_write(struct file * file,
1290 const char __user * buf, size_t count, loff_t *ppos)
1291{
1292 struct task_struct *task;
1293 char buffer[PROC_NUMBUF], *end;
1294 int make_it_fail;
1295
1296 if (!capable(CAP_SYS_RESOURCE))
1297 return -EPERM;
1298 memset(buffer, 0, sizeof(buffer));
1299 if (count > sizeof(buffer) - 1)
1300 count = sizeof(buffer) - 1;
1301 if (copy_from_user(buffer, buf, count))
1302 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001303 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1304 if (*end)
1305 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001306 task = get_proc_task(file->f_dentry->d_inode);
1307 if (!task)
1308 return -ESRCH;
1309 task->make_it_fail = make_it_fail;
1310 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001311
1312 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001313}
1314
Arjan van de Ven00977a52007-02-12 00:55:34 -08001315static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001316 .read = proc_fault_inject_read,
1317 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001318 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001319};
1320#endif
1321
Arjan van de Ven97455122008-01-25 21:08:34 +01001322
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001323#ifdef CONFIG_SCHED_DEBUG
1324/*
1325 * Print out various scheduling related per-task fields:
1326 */
1327static int sched_show(struct seq_file *m, void *v)
1328{
1329 struct inode *inode = m->private;
1330 struct task_struct *p;
1331
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001332 p = get_proc_task(inode);
1333 if (!p)
1334 return -ESRCH;
1335 proc_sched_show_task(p, m);
1336
1337 put_task_struct(p);
1338
1339 return 0;
1340}
1341
1342static ssize_t
1343sched_write(struct file *file, const char __user *buf,
1344 size_t count, loff_t *offset)
1345{
1346 struct inode *inode = file->f_path.dentry->d_inode;
1347 struct task_struct *p;
1348
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001349 p = get_proc_task(inode);
1350 if (!p)
1351 return -ESRCH;
1352 proc_sched_set_task(p);
1353
1354 put_task_struct(p);
1355
1356 return count;
1357}
1358
1359static int sched_open(struct inode *inode, struct file *filp)
1360{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001361 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001362}
1363
1364static const struct file_operations proc_pid_sched_operations = {
1365 .open = sched_open,
1366 .read = seq_read,
1367 .write = sched_write,
1368 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001369 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001370};
1371
1372#endif
1373
Mike Galbraith5091faa2010-11-30 14:18:03 +01001374#ifdef CONFIG_SCHED_AUTOGROUP
1375/*
1376 * Print out autogroup related information:
1377 */
1378static int sched_autogroup_show(struct seq_file *m, void *v)
1379{
1380 struct inode *inode = m->private;
1381 struct task_struct *p;
1382
1383 p = get_proc_task(inode);
1384 if (!p)
1385 return -ESRCH;
1386 proc_sched_autogroup_show_task(p, m);
1387
1388 put_task_struct(p);
1389
1390 return 0;
1391}
1392
1393static ssize_t
1394sched_autogroup_write(struct file *file, const char __user *buf,
1395 size_t count, loff_t *offset)
1396{
1397 struct inode *inode = file->f_path.dentry->d_inode;
1398 struct task_struct *p;
1399 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001400 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001401 int err;
1402
1403 memset(buffer, 0, sizeof(buffer));
1404 if (count > sizeof(buffer) - 1)
1405 count = sizeof(buffer) - 1;
1406 if (copy_from_user(buffer, buf, count))
1407 return -EFAULT;
1408
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001409 err = kstrtoint(strstrip(buffer), 0, &nice);
1410 if (err < 0)
1411 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001412
1413 p = get_proc_task(inode);
1414 if (!p)
1415 return -ESRCH;
1416
1417 err = nice;
1418 err = proc_sched_autogroup_set_nice(p, &err);
1419 if (err)
1420 count = err;
1421
1422 put_task_struct(p);
1423
1424 return count;
1425}
1426
1427static int sched_autogroup_open(struct inode *inode, struct file *filp)
1428{
1429 int ret;
1430
1431 ret = single_open(filp, sched_autogroup_show, NULL);
1432 if (!ret) {
1433 struct seq_file *m = filp->private_data;
1434
1435 m->private = inode;
1436 }
1437 return ret;
1438}
1439
1440static const struct file_operations proc_pid_sched_autogroup_operations = {
1441 .open = sched_autogroup_open,
1442 .read = seq_read,
1443 .write = sched_autogroup_write,
1444 .llseek = seq_lseek,
1445 .release = single_release,
1446};
1447
1448#endif /* CONFIG_SCHED_AUTOGROUP */
1449
john stultz4614a696b2009-12-14 18:00:05 -08001450static ssize_t comm_write(struct file *file, const char __user *buf,
1451 size_t count, loff_t *offset)
1452{
1453 struct inode *inode = file->f_path.dentry->d_inode;
1454 struct task_struct *p;
1455 char buffer[TASK_COMM_LEN];
1456
1457 memset(buffer, 0, sizeof(buffer));
1458 if (count > sizeof(buffer) - 1)
1459 count = sizeof(buffer) - 1;
1460 if (copy_from_user(buffer, buf, count))
1461 return -EFAULT;
1462
1463 p = get_proc_task(inode);
1464 if (!p)
1465 return -ESRCH;
1466
1467 if (same_thread_group(current, p))
1468 set_task_comm(p, buffer);
1469 else
1470 count = -EINVAL;
1471
1472 put_task_struct(p);
1473
1474 return count;
1475}
1476
1477static int comm_show(struct seq_file *m, void *v)
1478{
1479 struct inode *inode = m->private;
1480 struct task_struct *p;
1481
1482 p = get_proc_task(inode);
1483 if (!p)
1484 return -ESRCH;
1485
1486 task_lock(p);
1487 seq_printf(m, "%s\n", p->comm);
1488 task_unlock(p);
1489
1490 put_task_struct(p);
1491
1492 return 0;
1493}
1494
1495static int comm_open(struct inode *inode, struct file *filp)
1496{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001497 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001498}
1499
1500static const struct file_operations proc_pid_set_comm_operations = {
1501 .open = comm_open,
1502 .read = seq_read,
1503 .write = comm_write,
1504 .llseek = seq_lseek,
1505 .release = single_release,
1506};
1507
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001508static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001509{
1510 struct task_struct *task;
1511 struct mm_struct *mm;
1512 struct file *exe_file;
1513
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001514 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001515 if (!task)
1516 return -ENOENT;
1517 mm = get_task_mm(task);
1518 put_task_struct(task);
1519 if (!mm)
1520 return -ENOENT;
1521 exe_file = get_mm_exe_file(mm);
1522 mmput(mm);
1523 if (exe_file) {
1524 *exe_path = exe_file->f_path;
1525 path_get(&exe_file->f_path);
1526 fput(exe_file);
1527 return 0;
1528 } else
1529 return -ENOENT;
1530}
1531
Al Viro008b1502005-08-20 00:17:39 +01001532static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct inode *inode = dentry->d_inode;
1535 int error = -EACCES;
1536
1537 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001538 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Eric W. Biederman778c1142006-06-26 00:25:58 -07001540 /* Are we allowed to snoop on the tasks file descriptors? */
1541 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001544 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545out:
Al Viro008b1502005-08-20 00:17:39 +01001546 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001549static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Mel Gormane12ba742007-10-16 01:25:52 -07001551 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001552 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int len;
1554
1555 if (!tmp)
1556 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001557
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001558 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001559 len = PTR_ERR(pathname);
1560 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001562 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 if (len > buflen)
1565 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001566 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 len = -EFAULT;
1568 out:
1569 free_page((unsigned long)tmp);
1570 return len;
1571}
1572
1573static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1574{
1575 int error = -EACCES;
1576 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001577 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Eric W. Biederman778c1142006-06-26 00:25:58 -07001579 /* Are we allowed to snoop on the tasks file descriptors? */
1580 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001583 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (error)
1585 goto out;
1586
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001587 error = do_proc_readlink(&path, buffer, buflen);
1588 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return error;
1591}
1592
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001593static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001595 .follow_link = proc_pid_follow_link,
1596 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597};
1598
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001599
1600/* building an inode */
1601
1602static int task_dumpable(struct task_struct *task)
1603{
1604 int dumpable = 0;
1605 struct mm_struct *mm;
1606
1607 task_lock(task);
1608 mm = task->mm;
1609 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001610 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001611 task_unlock(task);
1612 if(dumpable == 1)
1613 return 1;
1614 return 0;
1615}
1616
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001617struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001618{
1619 struct inode * inode;
1620 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001621 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001622
1623 /* We need a new inode */
1624
1625 inode = new_inode(sb);
1626 if (!inode)
1627 goto out;
1628
1629 /* Common stuff */
1630 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001631 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001632 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001633 inode->i_op = &proc_def_inode_operations;
1634
1635 /*
1636 * grab the reference to task.
1637 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001638 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001639 if (!ei->pid)
1640 goto out_unlock;
1641
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001642 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001643 rcu_read_lock();
1644 cred = __task_cred(task);
1645 inode->i_uid = cred->euid;
1646 inode->i_gid = cred->egid;
1647 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001648 }
1649 security_task_to_inode(task, inode);
1650
1651out:
1652 return inode;
1653
1654out_unlock:
1655 iput(inode);
1656 return NULL;
1657}
1658
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001659int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001660{
1661 struct inode *inode = dentry->d_inode;
1662 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001663 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001664 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001665
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001666 generic_fillattr(inode, stat);
1667
1668 rcu_read_lock();
1669 stat->uid = 0;
1670 stat->gid = 0;
1671 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1672 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001673 if (!has_pid_permissions(pid, task, 2)) {
1674 rcu_read_unlock();
1675 /*
1676 * This doesn't prevent learning whether PID exists,
1677 * it only makes getattr() consistent with readdir().
1678 */
1679 return -ENOENT;
1680 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001681 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1682 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001683 cred = __task_cred(task);
1684 stat->uid = cred->euid;
1685 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001686 }
1687 }
1688 rcu_read_unlock();
1689 return 0;
1690}
1691
1692/* dentry stuff */
1693
1694/*
1695 * Exceptional case: normally we are not allowed to unhash a busy
1696 * directory. In this case, however, we can do it - no aliasing problems
1697 * due to the way we treat inodes.
1698 *
1699 * Rewrite the inode's ownerships here because the owning task may have
1700 * performed a setuid(), etc.
1701 *
1702 * Before the /proc/pid/status file was created the only way to read
1703 * the effective uid of a /process was to stat /proc/pid. Reading
1704 * /proc/pid/status is slow enough that procps and other packages
1705 * kept stating /proc/pid. To keep the rules in /proc simple I have
1706 * made this apply to all per process world readable and executable
1707 * directories.
1708 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001709int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001710{
Nick Piggin34286d62011-01-07 17:49:57 +11001711 struct inode *inode;
1712 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001713 const struct cred *cred;
1714
Nick Piggin34286d62011-01-07 17:49:57 +11001715 if (nd && nd->flags & LOOKUP_RCU)
1716 return -ECHILD;
1717
1718 inode = dentry->d_inode;
1719 task = get_proc_task(inode);
1720
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001721 if (task) {
1722 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1723 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001724 rcu_read_lock();
1725 cred = __task_cred(task);
1726 inode->i_uid = cred->euid;
1727 inode->i_gid = cred->egid;
1728 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001729 } else {
1730 inode->i_uid = 0;
1731 inode->i_gid = 0;
1732 }
1733 inode->i_mode &= ~(S_ISUID | S_ISGID);
1734 security_task_to_inode(task, inode);
1735 put_task_struct(task);
1736 return 1;
1737 }
1738 d_drop(dentry);
1739 return 0;
1740}
1741
Nick Pigginfe15ce42011-01-07 17:49:23 +11001742static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001743{
1744 /* Is the task we represent dead?
1745 * If so, then don't put the dentry on the lru list,
1746 * kill it immediately.
1747 */
1748 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1749}
1750
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001751const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001752{
1753 .d_revalidate = pid_revalidate,
1754 .d_delete = pid_delete_dentry,
1755};
1756
1757/* Lookups */
1758
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001759/*
1760 * Fill a directory entry.
1761 *
1762 * If possible create the dcache entry and derive our inode number and
1763 * file type from dcache entry.
1764 *
1765 * Since all of the proc inode numbers are dynamically generated, the inode
1766 * numbers do not exist until the inode is cache. This means creating the
1767 * the dcache entry in readdir is necessary to keep the inode numbers
1768 * reported by readdir in sync with the inode numbers reported
1769 * by stat.
1770 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001771int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1772 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001773 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001774{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001775 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001776 struct inode *inode;
1777 struct qstr qname;
1778 ino_t ino = 0;
1779 unsigned type = DT_UNKNOWN;
1780
1781 qname.name = name;
1782 qname.len = len;
1783 qname.hash = full_name_hash(name, len);
1784
1785 child = d_lookup(dir, &qname);
1786 if (!child) {
1787 struct dentry *new;
1788 new = d_alloc(dir, &qname);
1789 if (new) {
1790 child = instantiate(dir->d_inode, new, task, ptr);
1791 if (child)
1792 dput(new);
1793 else
1794 child = new;
1795 }
1796 }
1797 if (!child || IS_ERR(child) || !child->d_inode)
1798 goto end_instantiate;
1799 inode = child->d_inode;
1800 if (inode) {
1801 ino = inode->i_ino;
1802 type = inode->i_mode >> 12;
1803 }
1804 dput(child);
1805end_instantiate:
1806 if (!ino)
1807 ino = find_inode_number(dir, &qname);
1808 if (!ino)
1809 ino = 1;
1810 return filldir(dirent, name, len, filp->f_pos, ino, type);
1811}
1812
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001813static unsigned name_to_int(struct dentry *dentry)
1814{
1815 const char *name = dentry->d_name.name;
1816 int len = dentry->d_name.len;
1817 unsigned n = 0;
1818
1819 if (len > 1 && *name == '0')
1820 goto out;
1821 while (len-- > 0) {
1822 unsigned c = *name++ - '0';
1823 if (c > 9)
1824 goto out;
1825 if (n >= (~0U-9)/10)
1826 goto out;
1827 n *= 10;
1828 n += c;
1829 }
1830 return n;
1831out:
1832 return ~0U;
1833}
1834
Miklos Szeredi27932742007-05-08 00:26:17 -07001835#define PROC_FDINFO_MAX 64
1836
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001837static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001838{
Linus Torvalds5e442a42011-11-09 18:16:00 -05001839 struct task_struct *task = get_proc_task(inode);
1840 struct files_struct *files = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001841 struct file *file;
1842 int fd = proc_fd(inode);
1843
Linus Torvalds5e442a42011-11-09 18:16:00 -05001844 if (task) {
1845 files = get_files_struct(task);
1846 put_task_struct(task);
1847 }
1848 if (files) {
1849 /*
1850 * We are not taking a ref to the file structure, so we must
1851 * hold ->file_lock.
1852 */
1853 spin_lock(&files->file_lock);
1854 file = fcheck_files(files, fd);
1855 if (file) {
1856 unsigned int f_flags;
1857 struct fdtable *fdt;
Linus Torvalds1117f722011-08-06 11:51:33 -07001858
Linus Torvalds5e442a42011-11-09 18:16:00 -05001859 fdt = files_fdtable(files);
1860 f_flags = file->f_flags & ~O_CLOEXEC;
1861 if (FD_ISSET(fd, fdt->close_on_exec))
1862 f_flags |= O_CLOEXEC;
Linus Torvalds1117f722011-08-06 11:51:33 -07001863
Linus Torvalds5e442a42011-11-09 18:16:00 -05001864 if (path) {
1865 *path = file->f_path;
1866 path_get(&file->f_path);
1867 }
1868 if (info)
1869 snprintf(info, PROC_FDINFO_MAX,
1870 "pos:\t%lli\n"
1871 "flags:\t0%o\n",
1872 (long long) file->f_pos,
1873 f_flags);
1874 spin_unlock(&files->file_lock);
1875 put_files_struct(files);
1876 return 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001877 }
Linus Torvalds5e442a42011-11-09 18:16:00 -05001878 spin_unlock(&files->file_lock);
1879 put_files_struct(files);
1880 }
1881 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001882}
1883
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001884static int proc_fd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001885{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001886 return proc_fd_info(dentry->d_inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001887}
1888
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001889static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1890{
Nick Piggin34286d62011-01-07 17:49:57 +11001891 struct inode *inode;
1892 struct task_struct *task;
1893 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001894 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001895 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001896
Nick Piggin34286d62011-01-07 17:49:57 +11001897 if (nd && nd->flags & LOOKUP_RCU)
1898 return -ECHILD;
1899
1900 inode = dentry->d_inode;
1901 task = get_proc_task(inode);
1902 fd = proc_fd(inode);
1903
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001904 if (task) {
1905 files = get_files_struct(task);
1906 if (files) {
1907 rcu_read_lock();
1908 if (fcheck_files(files, fd)) {
1909 rcu_read_unlock();
1910 put_files_struct(files);
1911 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001912 rcu_read_lock();
1913 cred = __task_cred(task);
1914 inode->i_uid = cred->euid;
1915 inode->i_gid = cred->egid;
1916 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001917 } else {
1918 inode->i_uid = 0;
1919 inode->i_gid = 0;
1920 }
1921 inode->i_mode &= ~(S_ISUID | S_ISGID);
1922 security_task_to_inode(task, inode);
1923 put_task_struct(task);
1924 return 1;
1925 }
1926 rcu_read_unlock();
1927 put_files_struct(files);
1928 }
1929 put_task_struct(task);
1930 }
1931 d_drop(dentry);
1932 return 0;
1933}
1934
Al Virod72f71e2009-02-20 05:58:47 +00001935static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001936{
1937 .d_revalidate = tid_fd_revalidate,
1938 .d_delete = pid_delete_dentry,
1939};
1940
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001941static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001942 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001943{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001944 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001945 struct file *file;
1946 struct files_struct *files;
1947 struct inode *inode;
1948 struct proc_inode *ei;
1949 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001950
Eric W. Biederman61a28782006-10-02 02:18:49 -07001951 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001952 if (!inode)
1953 goto out;
1954 ei = PROC_I(inode);
1955 ei->fd = fd;
1956 files = get_files_struct(task);
1957 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001958 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001959 inode->i_mode = S_IFLNK;
1960
1961 /*
1962 * We are not taking a ref to the file structure, so we must
1963 * hold ->file_lock.
1964 */
1965 spin_lock(&files->file_lock);
1966 file = fcheck_files(files, fd);
1967 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001968 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001969 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001970 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001971 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001972 inode->i_mode |= S_IWUSR | S_IXUSR;
1973 spin_unlock(&files->file_lock);
1974 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001975
Linus Torvalds5e442a42011-11-09 18:16:00 -05001976 inode->i_op = &proc_pid_link_inode_operations;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001977 inode->i_size = 64;
1978 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001979 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001980 d_add(dentry, inode);
1981 /* Close the race of the process dying before we return the dentry */
1982 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001983 error = NULL;
1984
1985 out:
1986 return error;
1987out_unlock:
1988 spin_unlock(&files->file_lock);
1989 put_files_struct(files);
1990out_iput:
1991 iput(inode);
1992 goto out;
1993}
1994
Miklos Szeredi27932742007-05-08 00:26:17 -07001995static struct dentry *proc_lookupfd_common(struct inode *dir,
1996 struct dentry *dentry,
1997 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001998{
1999 struct task_struct *task = get_proc_task(dir);
2000 unsigned fd = name_to_int(dentry);
2001 struct dentry *result = ERR_PTR(-ENOENT);
2002
2003 if (!task)
2004 goto out_no_task;
2005 if (fd == ~0U)
2006 goto out;
2007
Miklos Szeredi27932742007-05-08 00:26:17 -07002008 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002009out:
2010 put_task_struct(task);
2011out_no_task:
2012 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002013}
2014
Miklos Szeredi27932742007-05-08 00:26:17 -07002015static int proc_readfd_common(struct file * filp, void * dirent,
2016 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002018 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07002019 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002020 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07002021 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 struct files_struct * files;
2024
2025 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002026 if (!p)
2027 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 fd = filp->f_pos;
2031 switch (fd) {
2032 case 0:
2033 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
Linus Torvalds5e442a42011-11-09 18:16:00 -05002034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 filp->f_pos++;
2036 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07002037 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Linus Torvalds5e442a42011-11-09 18:16:00 -05002039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 filp->f_pos++;
2041 default:
2042 files = get_files_struct(p);
2043 if (!files)
Linus Torvalds5e442a42011-11-09 18:16:00 -05002044 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002045 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04002047 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07002049 char name[PROC_NUMBUF];
2050 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 if (!fcheck_files(files, fd))
2053 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002054 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Miklos Szeredi27932742007-05-08 00:26:17 -07002056 len = snprintf(name, sizeof(name), "%d", fd);
2057 if (proc_fill_cache(filp, dirent, filldir,
2058 name, len, instantiate,
2059 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07002060 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 break;
2062 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002063 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002065 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 put_files_struct(files);
2067 }
2068out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002069 put_task_struct(p);
2070out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return retval;
2072}
2073
Miklos Szeredi27932742007-05-08 00:26:17 -07002074static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2075 struct nameidata *nd)
2076{
2077 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2078}
2079
2080static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2081{
2082 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2083}
2084
2085static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2086 size_t len, loff_t *ppos)
2087{
2088 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002089 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002090 if (!err)
2091 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2092 return err;
2093}
2094
2095static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002096 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002097 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002098 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002099};
2100
Arjan van de Ven00977a52007-02-12 00:55:34 -08002101static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 .read = generic_read_dir,
2103 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002104 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105};
2106
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002107#ifdef CONFIG_CHECKPOINT_RESTORE
2108
2109/*
2110 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2111 * which represent vma start and end addresses.
2112 */
2113static int dname_to_vma_addr(struct dentry *dentry,
2114 unsigned long *start, unsigned long *end)
2115{
2116 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2117 return -EINVAL;
2118
2119 return 0;
2120}
2121
2122static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2123{
2124 unsigned long vm_start, vm_end;
2125 bool exact_vma_exists = false;
2126 struct mm_struct *mm = NULL;
2127 struct task_struct *task;
2128 const struct cred *cred;
2129 struct inode *inode;
2130 int status = 0;
2131
2132 if (nd && nd->flags & LOOKUP_RCU)
2133 return -ECHILD;
2134
2135 if (!capable(CAP_SYS_ADMIN)) {
2136 status = -EACCES;
2137 goto out_notask;
2138 }
2139
2140 inode = dentry->d_inode;
2141 task = get_proc_task(inode);
2142 if (!task)
2143 goto out_notask;
2144
2145 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2146 goto out;
2147
2148 mm = get_task_mm(task);
2149 if (!mm)
2150 goto out;
2151
2152 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2153 down_read(&mm->mmap_sem);
2154 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2155 up_read(&mm->mmap_sem);
2156 }
2157
2158 mmput(mm);
2159
2160 if (exact_vma_exists) {
2161 if (task_dumpable(task)) {
2162 rcu_read_lock();
2163 cred = __task_cred(task);
2164 inode->i_uid = cred->euid;
2165 inode->i_gid = cred->egid;
2166 rcu_read_unlock();
2167 } else {
2168 inode->i_uid = 0;
2169 inode->i_gid = 0;
2170 }
2171 security_task_to_inode(task, inode);
2172 status = 1;
2173 }
2174
2175out:
2176 put_task_struct(task);
2177
2178out_notask:
2179 if (status <= 0)
2180 d_drop(dentry);
2181
2182 return status;
2183}
2184
2185static const struct dentry_operations tid_map_files_dentry_operations = {
2186 .d_revalidate = map_files_d_revalidate,
2187 .d_delete = pid_delete_dentry,
2188};
2189
2190static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2191{
2192 unsigned long vm_start, vm_end;
2193 struct vm_area_struct *vma;
2194 struct task_struct *task;
2195 struct mm_struct *mm;
2196 int rc;
2197
2198 rc = -ENOENT;
2199 task = get_proc_task(dentry->d_inode);
2200 if (!task)
2201 goto out;
2202
2203 mm = get_task_mm(task);
2204 put_task_struct(task);
2205 if (!mm)
2206 goto out;
2207
2208 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2209 if (rc)
2210 goto out_mmput;
2211
2212 down_read(&mm->mmap_sem);
2213 vma = find_exact_vma(mm, vm_start, vm_end);
2214 if (vma && vma->vm_file) {
2215 *path = vma->vm_file->f_path;
2216 path_get(path);
2217 rc = 0;
2218 }
2219 up_read(&mm->mmap_sem);
2220
2221out_mmput:
2222 mmput(mm);
2223out:
2224 return rc;
2225}
2226
2227struct map_files_info {
2228 struct file *file;
2229 unsigned long len;
2230 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2231};
2232
2233static struct dentry *
2234proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2235 struct task_struct *task, const void *ptr)
2236{
2237 const struct file *file = ptr;
2238 struct proc_inode *ei;
2239 struct inode *inode;
2240
2241 if (!file)
2242 return ERR_PTR(-ENOENT);
2243
2244 inode = proc_pid_make_inode(dir->i_sb, task);
2245 if (!inode)
2246 return ERR_PTR(-ENOENT);
2247
2248 ei = PROC_I(inode);
2249 ei->op.proc_get_link = proc_map_files_get_link;
2250
2251 inode->i_op = &proc_pid_link_inode_operations;
2252 inode->i_size = 64;
2253 inode->i_mode = S_IFLNK;
2254
2255 if (file->f_mode & FMODE_READ)
2256 inode->i_mode |= S_IRUSR;
2257 if (file->f_mode & FMODE_WRITE)
2258 inode->i_mode |= S_IWUSR;
2259
2260 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2261 d_add(dentry, inode);
2262
2263 return NULL;
2264}
2265
2266static struct dentry *proc_map_files_lookup(struct inode *dir,
2267 struct dentry *dentry, struct nameidata *nd)
2268{
2269 unsigned long vm_start, vm_end;
2270 struct vm_area_struct *vma;
2271 struct task_struct *task;
2272 struct dentry *result;
2273 struct mm_struct *mm;
2274
2275 result = ERR_PTR(-EACCES);
2276 if (!capable(CAP_SYS_ADMIN))
2277 goto out;
2278
2279 result = ERR_PTR(-ENOENT);
2280 task = get_proc_task(dir);
2281 if (!task)
2282 goto out;
2283
2284 result = ERR_PTR(-EACCES);
2285 if (lock_trace(task))
2286 goto out_put_task;
2287
2288 result = ERR_PTR(-ENOENT);
2289 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2290 goto out_unlock;
2291
2292 mm = get_task_mm(task);
2293 if (!mm)
2294 goto out_unlock;
2295
2296 down_read(&mm->mmap_sem);
2297 vma = find_exact_vma(mm, vm_start, vm_end);
2298 if (!vma)
2299 goto out_no_vma;
2300
2301 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2302
2303out_no_vma:
2304 up_read(&mm->mmap_sem);
2305 mmput(mm);
2306out_unlock:
2307 unlock_trace(task);
2308out_put_task:
2309 put_task_struct(task);
2310out:
2311 return result;
2312}
2313
2314static const struct inode_operations proc_map_files_inode_operations = {
2315 .lookup = proc_map_files_lookup,
2316 .permission = proc_fd_permission,
2317 .setattr = proc_setattr,
2318};
2319
2320static int
2321proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2322{
2323 struct dentry *dentry = filp->f_path.dentry;
2324 struct inode *inode = dentry->d_inode;
2325 struct vm_area_struct *vma;
2326 struct task_struct *task;
2327 struct mm_struct *mm;
2328 ino_t ino;
2329 int ret;
2330
2331 ret = -EACCES;
2332 if (!capable(CAP_SYS_ADMIN))
2333 goto out;
2334
2335 ret = -ENOENT;
2336 task = get_proc_task(inode);
2337 if (!task)
2338 goto out;
2339
2340 ret = -EACCES;
2341 if (lock_trace(task))
2342 goto out_put_task;
2343
2344 ret = 0;
2345 switch (filp->f_pos) {
2346 case 0:
2347 ino = inode->i_ino;
2348 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2349 goto out_unlock;
2350 filp->f_pos++;
2351 case 1:
2352 ino = parent_ino(dentry);
2353 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2354 goto out_unlock;
2355 filp->f_pos++;
2356 default:
2357 {
2358 unsigned long nr_files, pos, i;
2359 struct flex_array *fa = NULL;
2360 struct map_files_info info;
2361 struct map_files_info *p;
2362
2363 mm = get_task_mm(task);
2364 if (!mm)
2365 goto out_unlock;
2366 down_read(&mm->mmap_sem);
2367
2368 nr_files = 0;
2369
2370 /*
2371 * We need two passes here:
2372 *
2373 * 1) Collect vmas of mapped files with mmap_sem taken
2374 * 2) Release mmap_sem and instantiate entries
2375 *
2376 * otherwise we get lockdep complained, since filldir()
2377 * routine might require mmap_sem taken in might_fault().
2378 */
2379
2380 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2381 if (vma->vm_file && ++pos > filp->f_pos)
2382 nr_files++;
2383 }
2384
2385 if (nr_files) {
2386 fa = flex_array_alloc(sizeof(info), nr_files,
2387 GFP_KERNEL);
2388 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2389 GFP_KERNEL)) {
2390 ret = -ENOMEM;
2391 if (fa)
2392 flex_array_free(fa);
2393 up_read(&mm->mmap_sem);
2394 mmput(mm);
2395 goto out_unlock;
2396 }
2397 for (i = 0, vma = mm->mmap, pos = 2; vma;
2398 vma = vma->vm_next) {
2399 if (!vma->vm_file)
2400 continue;
2401 if (++pos <= filp->f_pos)
2402 continue;
2403
2404 get_file(vma->vm_file);
2405 info.file = vma->vm_file;
2406 info.len = snprintf(info.name,
2407 sizeof(info.name), "%lx-%lx",
2408 vma->vm_start, vma->vm_end);
2409 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2410 BUG();
2411 }
2412 }
2413 up_read(&mm->mmap_sem);
2414
2415 for (i = 0; i < nr_files; i++) {
2416 p = flex_array_get(fa, i);
2417 ret = proc_fill_cache(filp, dirent, filldir,
2418 p->name, p->len,
2419 proc_map_files_instantiate,
2420 task, p->file);
2421 if (ret)
2422 break;
2423 filp->f_pos++;
2424 fput(p->file);
2425 }
2426 for (; i < nr_files; i++) {
2427 /*
2428 * In case of error don't forget
2429 * to put rest of file refs.
2430 */
2431 p = flex_array_get(fa, i);
2432 fput(p->file);
2433 }
2434 if (fa)
2435 flex_array_free(fa);
2436 mmput(mm);
2437 }
2438 }
2439
2440out_unlock:
2441 unlock_trace(task);
2442out_put_task:
2443 put_task_struct(task);
2444out:
2445 return ret;
2446}
2447
2448static const struct file_operations proc_map_files_operations = {
2449 .read = generic_read_dir,
2450 .readdir = proc_map_files_readdir,
2451 .llseek = default_llseek,
2452};
2453
2454#endif /* CONFIG_CHECKPOINT_RESTORE */
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002457 * /proc/pid/fd needs a special permission handler so that a process can still
2458 * access /proc/self/fd after it has executed a setuid().
2459 */
Al Viro10556cb2011-06-20 19:28:19 -04002460static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002461{
Al Viro2830ba72011-06-20 19:16:29 -04002462 int rv = generic_permission(inode, mask);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002463 if (rv == 0)
2464 return 0;
2465 if (task_pid(current) == proc_pid(inode))
2466 rv = 0;
2467 return rv;
2468}
2469
2470/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 * proc directories can do almost nothing..
2472 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002473static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002475 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002476 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477};
2478
Miklos Szeredi27932742007-05-08 00:26:17 -07002479static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2480 struct dentry *dentry, struct task_struct *task, const void *ptr)
2481{
2482 unsigned fd = *(unsigned *)ptr;
2483 struct inode *inode;
2484 struct proc_inode *ei;
2485 struct dentry *error = ERR_PTR(-ENOENT);
2486
2487 inode = proc_pid_make_inode(dir->i_sb, task);
2488 if (!inode)
2489 goto out;
2490 ei = PROC_I(inode);
2491 ei->fd = fd;
2492 inode->i_mode = S_IFREG | S_IRUSR;
2493 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002494 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002495 d_add(dentry, inode);
2496 /* Close the race of the process dying before we return the dentry */
2497 if (tid_fd_revalidate(dentry, NULL))
2498 error = NULL;
2499
2500 out:
2501 return error;
2502}
2503
2504static struct dentry *proc_lookupfdinfo(struct inode *dir,
2505 struct dentry *dentry,
2506 struct nameidata *nd)
2507{
2508 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2509}
2510
2511static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2512{
2513 return proc_readfd_common(filp, dirent, filldir,
2514 proc_fdinfo_instantiate);
2515}
2516
2517static const struct file_operations proc_fdinfo_operations = {
2518 .read = generic_read_dir,
2519 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002520 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002521};
2522
2523/*
2524 * proc directories can do almost nothing..
2525 */
2526static const struct inode_operations proc_fdinfo_inode_operations = {
2527 .lookup = proc_lookupfdinfo,
2528 .setattr = proc_setattr,
2529};
2530
2531
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002532static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002533 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002534{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002535 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002536 struct inode *inode;
2537 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002538 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002539
Eric W. Biederman61a28782006-10-02 02:18:49 -07002540 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002541 if (!inode)
2542 goto out;
2543
2544 ei = PROC_I(inode);
2545 inode->i_mode = p->mode;
2546 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002547 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002548 if (p->iop)
2549 inode->i_op = p->iop;
2550 if (p->fop)
2551 inode->i_fop = p->fop;
2552 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002553 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002554 d_add(dentry, inode);
2555 /* Close the race of the process dying before we return the dentry */
2556 if (pid_revalidate(dentry, NULL))
2557 error = NULL;
2558out:
2559 return error;
2560}
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562static struct dentry *proc_pident_lookup(struct inode *dir,
2563 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002564 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002565 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002567 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002568 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002569 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002571 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Eric W. Biederman99f89552006-06-26 00:25:55 -07002573 if (!task)
2574 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002576 /*
2577 * Yes, it does not scale. And it should not. Don't add
2578 * new entries into /proc/<tgid>/ without very good reasons.
2579 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002580 last = &ents[nents - 1];
2581 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (p->len != dentry->d_name.len)
2583 continue;
2584 if (!memcmp(dentry->d_name.name, p->name, p->len))
2585 break;
2586 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002587 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 goto out;
2589
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002590 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002592 put_task_struct(task);
2593out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002594 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
Eric Dumazetc5141e62007-05-08 00:26:15 -07002597static int proc_pident_fill_cache(struct file *filp, void *dirent,
2598 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002599{
2600 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2601 proc_pident_instantiate, task, p);
2602}
2603
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002604static int proc_pident_readdir(struct file *filp,
2605 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002606 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002607{
2608 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002609 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002610 struct inode *inode = dentry->d_inode;
2611 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002612 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002613 ino_t ino;
2614 int ret;
2615
2616 ret = -ENOENT;
2617 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002618 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002619
2620 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002621 i = filp->f_pos;
2622 switch (i) {
2623 case 0:
2624 ino = inode->i_ino;
2625 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2626 goto out;
2627 i++;
2628 filp->f_pos++;
2629 /* fall through */
2630 case 1:
2631 ino = parent_ino(dentry);
2632 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2633 goto out;
2634 i++;
2635 filp->f_pos++;
2636 /* fall through */
2637 default:
2638 i -= 2;
2639 if (i >= nents) {
2640 ret = 1;
2641 goto out;
2642 }
2643 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002644 last = &ents[nents - 1];
2645 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002646 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002647 goto out;
2648 filp->f_pos++;
2649 p++;
2650 }
2651 }
2652
2653 ret = 1;
2654out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002655 put_task_struct(task);
2656out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002661static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2662 size_t count, loff_t *ppos)
2663{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002664 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002665 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002666 ssize_t length;
2667 struct task_struct *task = get_proc_task(inode);
2668
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002669 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002670 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002671
2672 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002673 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002674 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002675 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002676 if (length > 0)
2677 length = simple_read_from_buffer(buf, count, ppos, p, length);
2678 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002679 return length;
2680}
2681
2682static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2683 size_t count, loff_t *ppos)
2684{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002685 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002686 char *page;
2687 ssize_t length;
2688 struct task_struct *task = get_proc_task(inode);
2689
2690 length = -ESRCH;
2691 if (!task)
2692 goto out_no_task;
2693 if (count > PAGE_SIZE)
2694 count = PAGE_SIZE;
2695
2696 /* No partial writes. */
2697 length = -EINVAL;
2698 if (*ppos != 0)
2699 goto out;
2700
2701 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002702 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002703 if (!page)
2704 goto out;
2705
2706 length = -EFAULT;
2707 if (copy_from_user(page, buf, count))
2708 goto out_free;
2709
David Howells107db7c2009-05-08 13:55:27 +01002710 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002711 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002712 if (length < 0)
2713 goto out_free;
2714
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002715 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002716 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002717 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002718 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002719out_free:
2720 free_page((unsigned long) page);
2721out:
2722 put_task_struct(task);
2723out_no_task:
2724 return length;
2725}
2726
Arjan van de Ven00977a52007-02-12 00:55:34 -08002727static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002728 .read = proc_pid_attr_read,
2729 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002730 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002731};
2732
Eric Dumazetc5141e62007-05-08 00:26:15 -07002733static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002734 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2735 REG("prev", S_IRUGO, proc_pid_attr_operations),
2736 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2737 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2738 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2739 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002740};
2741
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002742static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 void * dirent, filldir_t filldir)
2744{
2745 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002746 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
Arjan van de Ven00977a52007-02-12 00:55:34 -08002749static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002751 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002752 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753};
2754
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002755static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 struct dentry *dentry, struct nameidata *nd)
2757{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002758 return proc_pident_lookup(dir, dentry,
2759 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760}
2761
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002762static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002763 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002764 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002765 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766};
2767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768#endif
2769
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002770#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002771static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2772 size_t count, loff_t *ppos)
2773{
2774 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2775 struct mm_struct *mm;
2776 char buffer[PROC_NUMBUF];
2777 size_t len;
2778 int ret;
2779
2780 if (!task)
2781 return -ESRCH;
2782
2783 ret = 0;
2784 mm = get_task_mm(task);
2785 if (mm) {
2786 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2787 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2788 MMF_DUMP_FILTER_SHIFT));
2789 mmput(mm);
2790 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2791 }
2792
2793 put_task_struct(task);
2794
2795 return ret;
2796}
2797
2798static ssize_t proc_coredump_filter_write(struct file *file,
2799 const char __user *buf,
2800 size_t count,
2801 loff_t *ppos)
2802{
2803 struct task_struct *task;
2804 struct mm_struct *mm;
2805 char buffer[PROC_NUMBUF], *end;
2806 unsigned int val;
2807 int ret;
2808 int i;
2809 unsigned long mask;
2810
2811 ret = -EFAULT;
2812 memset(buffer, 0, sizeof(buffer));
2813 if (count > sizeof(buffer) - 1)
2814 count = sizeof(buffer) - 1;
2815 if (copy_from_user(buffer, buf, count))
2816 goto out_no_task;
2817
2818 ret = -EINVAL;
2819 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2820 if (*end == '\n')
2821 end++;
2822 if (end - buffer == 0)
2823 goto out_no_task;
2824
2825 ret = -ESRCH;
2826 task = get_proc_task(file->f_dentry->d_inode);
2827 if (!task)
2828 goto out_no_task;
2829
2830 ret = end - buffer;
2831 mm = get_task_mm(task);
2832 if (!mm)
2833 goto out_no_mm;
2834
2835 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2836 if (val & mask)
2837 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2838 else
2839 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2840 }
2841
2842 mmput(mm);
2843 out_no_mm:
2844 put_task_struct(task);
2845 out_no_task:
2846 return ret;
2847}
2848
2849static const struct file_operations proc_coredump_filter_operations = {
2850 .read = proc_coredump_filter_read,
2851 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002852 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002853};
2854#endif
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856/*
2857 * /proc/self:
2858 */
2859static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2860 int buflen)
2861{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002862 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002863 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002864 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002865 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002866 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002867 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return vfs_readlink(dentry,buffer,buflen,tmp);
2869}
2870
Al Viro008b1502005-08-20 00:17:39 +01002871static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002873 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002874 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002875 char *name = ERR_PTR(-ENOENT);
2876 if (tgid) {
2877 name = __getname();
2878 if (!name)
2879 name = ERR_PTR(-ENOMEM);
2880 else
2881 sprintf(name, "%d", tgid);
2882 }
2883 nd_set_link(nd, name);
2884 return NULL;
2885}
2886
2887static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2888 void *cookie)
2889{
2890 char *s = nd_get_link(nd);
2891 if (!IS_ERR(s))
2892 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002893}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002895static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 .readlink = proc_self_readlink,
2897 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002898 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899};
2900
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002901/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002902 * proc base
2903 *
2904 * These are the directory entries in the root directory of /proc
2905 * that properly belong to the /proc filesystem, as they describe
2906 * describe something that is process related.
2907 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002908static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002909 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002910 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002911};
2912
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002913static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002914 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002915{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002916 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002917 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002918 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002919 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002920
2921 /* Allocate the inode */
2922 error = ERR_PTR(-ENOMEM);
2923 inode = new_inode(dir->i_sb);
2924 if (!inode)
2925 goto out;
2926
2927 /* Initialize the inode */
2928 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002929 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002930 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002931
2932 /*
2933 * grab the reference to the task.
2934 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002935 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002936 if (!ei->pid)
2937 goto out_iput;
2938
Eric W. Biederman801199c2006-10-02 02:18:48 -07002939 inode->i_mode = p->mode;
2940 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002941 set_nlink(inode, 2);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002942 if (S_ISLNK(inode->i_mode))
2943 inode->i_size = 64;
2944 if (p->iop)
2945 inode->i_op = p->iop;
2946 if (p->fop)
2947 inode->i_fop = p->fop;
2948 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002949 d_add(dentry, inode);
2950 error = NULL;
2951out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002952 return error;
2953out_iput:
2954 iput(inode);
2955 goto out;
2956}
2957
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002958static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2959{
2960 struct dentry *error;
2961 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002962 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002963
2964 error = ERR_PTR(-ENOENT);
2965
2966 if (!task)
2967 goto out_no_task;
2968
2969 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002970 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2971 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002972 if (p->len != dentry->d_name.len)
2973 continue;
2974 if (!memcmp(dentry->d_name.name, p->name, p->len))
2975 break;
2976 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002977 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002978 goto out;
2979
2980 error = proc_base_instantiate(dir, dentry, task, p);
2981
2982out:
2983 put_task_struct(task);
2984out_no_task:
2985 return error;
2986}
2987
Eric Dumazetc5141e62007-05-08 00:26:15 -07002988static int proc_base_fill_cache(struct file *filp, void *dirent,
2989 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002990{
2991 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2992 proc_base_instantiate, task, p);
2993}
2994
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002995#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002996static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002997{
Andrea Righi940389b2008-07-28 00:48:12 +02002998 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002999 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07003000 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07003001
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07003002 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
3003 if (result)
3004 return result;
3005
3006 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
3007 result = -EACCES;
3008 goto out_unlock;
3009 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003010
Andrea Righi59954772008-07-27 17:29:15 +02003011 if (whole && lock_task_sighand(task, &flags)) {
3012 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07003013
Andrea Righi59954772008-07-27 17:29:15 +02003014 task_io_accounting_add(&acct, &task->signal->ioac);
3015 while_each_thread(task, t)
3016 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07003017
Andrea Righi59954772008-07-27 17:29:15 +02003018 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07003019 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07003020 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003021 "rchar: %llu\n"
3022 "wchar: %llu\n"
3023 "syscr: %llu\n"
3024 "syscw: %llu\n"
3025 "read_bytes: %llu\n"
3026 "write_bytes: %llu\n"
3027 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07003028 (unsigned long long)acct.rchar,
3029 (unsigned long long)acct.wchar,
3030 (unsigned long long)acct.syscr,
3031 (unsigned long long)acct.syscw,
3032 (unsigned long long)acct.read_bytes,
3033 (unsigned long long)acct.write_bytes,
3034 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07003035out_unlock:
3036 mutex_unlock(&task->signal->cred_guard_mutex);
3037 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003038}
Andrea Righi297c5d92008-07-25 01:48:49 -07003039
3040static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
3041{
3042 return do_io_accounting(task, buffer, 0);
3043}
3044
3045static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
3046{
3047 return do_io_accounting(task, buffer, 1);
3048}
3049#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003050
Kees Cook47830722008-10-06 03:11:58 +04003051static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3052 struct pid *pid, struct task_struct *task)
3053{
Al Viroa9712bc2011-03-23 15:52:50 -04003054 int err = lock_trace(task);
3055 if (!err) {
3056 seq_printf(m, "%08x\n", task->personality);
3057 unlock_trace(task);
3058 }
3059 return err;
Kees Cook47830722008-10-06 03:11:58 +04003060}
3061
Eric W. Biederman801199c2006-10-02 02:18:48 -07003062/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003063 * Thread groups
3064 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003065static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003066static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07003067
Eric Dumazetc5141e62007-05-08 00:26:15 -07003068static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003069 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3070 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08003071#ifdef CONFIG_CHECKPOINT_RESTORE
3072 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3073#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003074 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003075 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07003076#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003077 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07003078#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003079 REG("environ", S_IRUSR, proc_environ_operations),
3080 INF("auxv", S_IRUSR, proc_pid_auxv),
3081 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003082 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003083 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003084#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003085 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003086#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01003087#ifdef CONFIG_SCHED_AUTOGROUP
3088 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3089#endif
john stultz4614a696b2009-12-14 18:00:05 -08003090 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003091#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003092 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003093#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003094 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3095 ONE("stat", S_IRUGO, proc_tgid_stat),
3096 ONE("statm", S_IRUGO, proc_pid_statm),
3097 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003098#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003099 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003100#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003101 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3102 LNK("cwd", proc_cwd_link),
3103 LNK("root", proc_root_link),
3104 LNK("exe", proc_exe_link),
3105 REG("mounts", S_IRUGO, proc_mounts_operations),
3106 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3107 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003108#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003109 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3110 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003111 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003112#endif
3113#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003114 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003115#endif
3116#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003117 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003118#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003119#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003120 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003121#endif
3122#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003123 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003124#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003125#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003126 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003127#endif
Paul Menage8793d852007-10-18 23:39:39 -07003128#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003129 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003130#endif
Paul Menagea4243162007-10-18 23:39:35 -07003131#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003132 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003133#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003134 INF("oom_score", S_IRUGO, proc_oom_score),
3135 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003136 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003137#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003138 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3139 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003140#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003141#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003142 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003143#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003144#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003145 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003146#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003147#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003148 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003149#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003150#ifdef CONFIG_HARDWALL
3151 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3152#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003153};
3154
3155static int proc_tgid_base_readdir(struct file * filp,
3156 void * dirent, filldir_t filldir)
3157{
3158 return proc_pident_readdir(filp,dirent,filldir,
3159 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3160}
3161
Arjan van de Ven00977a52007-02-12 00:55:34 -08003162static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003163 .read = generic_read_dir,
3164 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003165 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003166};
3167
3168static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003169 return proc_pident_lookup(dir, dentry,
3170 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003171}
3172
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003173static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003174 .lookup = proc_tgid_base_lookup,
3175 .getattr = pid_getattr,
3176 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003177 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003178};
3179
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003180static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003182 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003183 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003184 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Eric W. Biederman48e64842006-06-26 00:25:48 -07003186 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003187 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3188 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003189 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08003190 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003191 d_drop(dentry);
3192 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Eric W. Biederman48e64842006-06-26 00:25:48 -07003195 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003196 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3197 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003198 if (!leader)
3199 goto out;
3200
3201 name.name = "task";
3202 name.len = strlen(name.name);
3203 dir = d_hash_and_lookup(leader, &name);
3204 if (!dir)
3205 goto out_put_leader;
3206
3207 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003208 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003209 dentry = d_hash_and_lookup(dir, &name);
3210 if (dentry) {
3211 shrink_dcache_parent(dentry);
3212 d_drop(dentry);
3213 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003215
3216 dput(dir);
3217out_put_leader:
3218 dput(leader);
3219out:
3220 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Randy Dunlap0895e912007-10-21 21:00:10 -07003223/**
3224 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3225 * @task: task that should be flushed.
3226 *
3227 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003228 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003229 * in. This call is supposed to do all of this job.
3230 *
3231 * Looks in the dcache for
3232 * /proc/@pid
3233 * /proc/@tgid/task/@pid
3234 * if either directory is present flushes it and all of it'ts children
3235 * from the dcache.
3236 *
3237 * It is safe and reasonable to cache /proc entries for a task until
3238 * that task exits. After that they just clog up the dcache with
3239 * useless entries, possibly causing useful dcache entries to be
3240 * flushed instead. This routine is proved to flush those useless
3241 * dcache entries at process exit time.
3242 *
3243 * NOTE: This routine is just an optimization so it does not guarantee
3244 * that no dcache entries will exist at process exit time it
3245 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003246 */
3247
3248void proc_flush_task(struct task_struct *task)
3249{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003250 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003251 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003252 struct upid *upid;
3253
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003254 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003255 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003256
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003257 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003258 upid = &pid->numbers[i];
3259 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003260 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003261 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07003262
3263 upid = &pid->numbers[pid->level];
3264 if (upid->nr == 1)
3265 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003266}
3267
Adrian Bunk9711ef92006-12-06 20:38:31 -08003268static struct dentry *proc_pid_instantiate(struct inode *dir,
3269 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003270 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003271{
3272 struct dentry *error = ERR_PTR(-ENOENT);
3273 struct inode *inode;
3274
Eric W. Biederman61a28782006-10-02 02:18:49 -07003275 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003276 if (!inode)
3277 goto out;
3278
3279 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3280 inode->i_op = &proc_tgid_base_inode_operations;
3281 inode->i_fop = &proc_tgid_base_operations;
3282 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003283
Miklos Szeredibfe86842011-10-28 14:13:29 +02003284 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3285 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003286
Nick Pigginfb045ad2011-01-07 17:49:55 +11003287 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003288
3289 d_add(dentry, inode);
3290 /* Close the race of the process dying before we return the dentry */
3291 if (pid_revalidate(dentry, NULL))
3292 error = NULL;
3293out:
3294 return error;
3295}
3296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3298{
Dan Carpenter73d36462010-05-26 14:43:25 -07003299 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003302 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Eric W. Biederman801199c2006-10-02 02:18:48 -07003304 result = proc_base_lookup(dir, dentry);
3305 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3306 goto out;
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 tgid = name_to_int(dentry);
3309 if (tgid == ~0U)
3310 goto out;
3311
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003312 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003313 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003314 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (task)
3316 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003317 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 if (!task)
3319 goto out;
3320
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003321 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003322 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07003324 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325}
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003328 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003329 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003331struct tgid_iter {
3332 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003333 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003334};
3335static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3336{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003337 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003339 if (iter.task)
3340 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003341 rcu_read_lock();
3342retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003343 iter.task = NULL;
3344 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003345 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003346 iter.tgid = pid_nr_ns(pid, ns);
3347 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003348 /* What we to know is if the pid we have find is the
3349 * pid of a thread_group_leader. Testing for task
3350 * being a thread_group_leader is the obvious thing
3351 * todo but there is a window when it fails, due to
3352 * the pid transfer logic in de_thread.
3353 *
3354 * So we perform the straight forward test of seeing
3355 * if the pid we have found is the pid of a thread
3356 * group leader, and don't worry if the task we have
3357 * found doesn't happen to be a thread group leader.
3358 * As we don't care in the case of readdir.
3359 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003360 if (!iter.task || !has_group_leader_pid(iter.task)) {
3361 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003362 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003363 }
3364 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003366 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003367 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003370#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Eric W. Biederman61a28782006-10-02 02:18:49 -07003372static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003373 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003374{
3375 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003376 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003377 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003378 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003379}
3380
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003381static int fake_filldir(void *buf, const char *name, int namelen,
3382 loff_t offset, u64 ino, unsigned d_type)
3383{
3384 return 0;
3385}
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387/* for the /proc/ directory itself, after non-process stuff has been done */
3388int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3389{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003390 unsigned int nr;
3391 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003392 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003393 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003394 filldir_t __filldir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003396 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3397 goto out_no_task;
3398 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3399
3400 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003401 if (!reaper)
3402 goto out_no_task;
3403
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003404 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003405 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003406 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003410 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003411 iter.task = NULL;
3412 iter.tgid = filp->f_pos - TGID_OFFSET;
3413 for (iter = next_tgid(ns, iter);
3414 iter.task;
3415 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003416 if (has_pid_permissions(ns, iter.task, 2))
3417 __filldir = filldir;
3418 else
3419 __filldir = fake_filldir;
3420
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003421 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003422 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003423 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003427 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3428out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003429 put_task_struct(reaper);
3430out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return 0;
3432}
3433
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003434/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003435 * Tasks
3436 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003437static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003438 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003439 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003440 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003441 REG("environ", S_IRUSR, proc_environ_operations),
3442 INF("auxv", S_IRUSR, proc_pid_auxv),
3443 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003444 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003445 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003446#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003447 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003448#endif
john stultz4614a696b2009-12-14 18:00:05 -08003449 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003450#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003451 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003452#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003453 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3454 ONE("stat", S_IRUGO, proc_tid_stat),
3455 ONE("statm", S_IRUGO, proc_pid_statm),
3456 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003457#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003458 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003459#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003460 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3461 LNK("cwd", proc_cwd_link),
3462 LNK("root", proc_root_link),
3463 LNK("exe", proc_exe_link),
3464 REG("mounts", S_IRUGO, proc_mounts_operations),
3465 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003466#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003467 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3468 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003469 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003470#endif
3471#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003472 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003473#endif
3474#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003475 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003476#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003477#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003478 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003479#endif
3480#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003481 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003482#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003483#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003484 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003485#endif
Paul Menage8793d852007-10-18 23:39:39 -07003486#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003487 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003488#endif
Paul Menagea4243162007-10-18 23:39:35 -07003489#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003490 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003491#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003492 INF("oom_score", S_IRUGO, proc_oom_score),
3493 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003494 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003495#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003496 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003497 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003498#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003499#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003500 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003501#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003502#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003503 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003504#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003505#ifdef CONFIG_HARDWALL
3506 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3507#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003508};
3509
3510static int proc_tid_base_readdir(struct file * filp,
3511 void * dirent, filldir_t filldir)
3512{
3513 return proc_pident_readdir(filp,dirent,filldir,
3514 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3515}
3516
3517static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003518 return proc_pident_lookup(dir, dentry,
3519 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003520}
3521
Arjan van de Ven00977a52007-02-12 00:55:34 -08003522static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003523 .read = generic_read_dir,
3524 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003525 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003526};
3527
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003528static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003529 .lookup = proc_tid_base_lookup,
3530 .getattr = pid_getattr,
3531 .setattr = proc_setattr,
3532};
3533
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003534static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003535 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003536{
3537 struct dentry *error = ERR_PTR(-ENOENT);
3538 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003539 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003540
3541 if (!inode)
3542 goto out;
3543 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3544 inode->i_op = &proc_tid_base_inode_operations;
3545 inode->i_fop = &proc_tid_base_operations;
3546 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003547
Miklos Szeredibfe86842011-10-28 14:13:29 +02003548 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3549 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003550
Nick Pigginfb045ad2011-01-07 17:49:55 +11003551 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003552
3553 d_add(dentry, inode);
3554 /* Close the race of the process dying before we return the dentry */
3555 if (pid_revalidate(dentry, NULL))
3556 error = NULL;
3557out:
3558 return error;
3559}
3560
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003561static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3562{
3563 struct dentry *result = ERR_PTR(-ENOENT);
3564 struct task_struct *task;
3565 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003566 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003567 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003568
3569 if (!leader)
3570 goto out_no_task;
3571
3572 tid = name_to_int(dentry);
3573 if (tid == ~0U)
3574 goto out;
3575
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003576 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003577 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003578 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003579 if (task)
3580 get_task_struct(task);
3581 rcu_read_unlock();
3582 if (!task)
3583 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003584 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003585 goto out_drop_task;
3586
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003587 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003588out_drop_task:
3589 put_task_struct(task);
3590out:
3591 put_task_struct(leader);
3592out_no_task:
3593 return result;
3594}
3595
3596/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003597 * Find the first tid of a thread group to return to user space.
3598 *
3599 * Usually this is just the thread group leader, but if the users
3600 * buffer was too small or there was a seek into the middle of the
3601 * directory we have more work todo.
3602 *
3603 * In the case of a short read we start with find_task_by_pid.
3604 *
3605 * In the case of a seek we start with the leader and walk nr
3606 * threads past it.
3607 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003608static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003609 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003610{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003611 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003612
Eric W. Biedermancc288732006-06-26 00:26:01 -07003613 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003614 /* Attempt to start with the pid of a thread */
3615 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003616 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003617 if (pos && (pos->group_leader == leader))
3618 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003619 }
3620
3621 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003622 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003623 if (nr && nr >= get_nr_threads(leader))
3624 goto out;
3625
3626 /* If we haven't found our starting place yet start
3627 * with the leader and walk nr threads forward.
3628 */
3629 for (pos = leader; nr > 0; --nr) {
3630 pos = next_thread(pos);
3631 if (pos == leader) {
3632 pos = NULL;
3633 goto out;
3634 }
3635 }
3636found:
3637 get_task_struct(pos);
3638out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003639 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003640 return pos;
3641}
3642
3643/*
3644 * Find the next thread in the thread list.
3645 * Return NULL if there is an error or no next thread.
3646 *
3647 * The reference to the input task_struct is released.
3648 */
3649static struct task_struct *next_tid(struct task_struct *start)
3650{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003651 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003652 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003653 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003654 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003655 if (thread_group_leader(pos))
3656 pos = NULL;
3657 else
3658 get_task_struct(pos);
3659 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003660 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003661 put_task_struct(start);
3662 return pos;
3663}
3664
Eric W. Biederman61a28782006-10-02 02:18:49 -07003665static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3666 struct task_struct *task, int tid)
3667{
3668 char name[PROC_NUMBUF];
3669 int len = snprintf(name, sizeof(name), "%d", tid);
3670 return proc_fill_cache(filp, dirent, filldir, name, len,
3671 proc_task_instantiate, task, NULL);
3672}
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674/* for the /proc/TGID/task/ directories */
3675static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3676{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003677 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003679 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003680 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 int retval = -ENOENT;
3682 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003683 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003684 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003686 task = get_proc_task(inode);
3687 if (!task)
3688 goto out_no_task;
3689 rcu_read_lock();
3690 if (pid_alive(task)) {
3691 leader = task->group_leader;
3692 get_task_struct(leader);
3693 }
3694 rcu_read_unlock();
3695 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003696 if (!leader)
3697 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 retval = 0;
3699
Linus Torvaldsee568b22009-03-17 10:02:35 -07003700 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 case 0:
3702 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003703 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003705 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 /* fall through */
3707 case 1:
3708 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003709 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003711 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 /* fall through */
3713 }
3714
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003715 /* f_version caches the tgid value that the last readdir call couldn't
3716 * return. lseek aka telldir automagically resets f_version to 0.
3717 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003718 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003719 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003720 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003721 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003722 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003723 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003724 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003725 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003726 /* returning this tgid failed, save it as the first
3727 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003728 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003729 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 }
3733out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003734 put_task_struct(leader);
3735out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 return retval;
3737}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003738
3739static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3740{
3741 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003742 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003743 generic_fillattr(inode, stat);
3744
Eric W. Biederman99f89552006-06-26 00:25:55 -07003745 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003746 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003747 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003748 }
3749
3750 return 0;
3751}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003752
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003753static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003754 .lookup = proc_task_lookup,
3755 .getattr = proc_task_getattr,
3756 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003757 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003758};
3759
Arjan van de Ven00977a52007-02-12 00:55:34 -08003760static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003761 .read = generic_read_dir,
3762 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003763 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003764};