blob: dbf2283e67e6697f2f237c1420c52f40fd95a67d [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
Mike Chanf371edd2008-04-24 10:22:26 -0700140/* ANDROID is for special files in /proc. */
141#define ANDROID(NAME, MODE, OTYPE) \
142 NOD(NAME, (S_IFREG|(MODE)), \
143 &proc_##OTYPE##_inode_operations, \
144 &proc_##OTYPE##_operations, {})
145
Vegard Nossumaed54172008-06-05 22:46:53 -0700146/*
147 * Count the number of hardlinks for the pid_entry table, excluding the .
148 * and .. links.
149 */
150static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
151 unsigned int n)
152{
153 unsigned int i;
154 unsigned int count;
155
156 count = 0;
157 for (i = 0; i < n; ++i) {
158 if (S_ISDIR(entries[i].mode))
159 ++count;
160 }
161
162 return count;
163}
164
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200165static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 int result = -ENOENT;
168
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700169 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200170 if (task->fs) {
171 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000172 result = 0;
173 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700174 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000175 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700176}
177
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800178static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700179{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800180 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700181 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182
183 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200184 task_lock(task);
185 if (task->fs) {
186 get_fs_pwd(task->fs, path);
187 result = 0;
188 }
189 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700190 put_task_struct(task);
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return result;
193}
194
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800195static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800197 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700199
200 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200201 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700202 put_task_struct(task);
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return result;
205}
206
Linus Torvaldse2683372012-01-17 15:21:19 -0800207struct mm_struct *mm_for_maps(struct task_struct *task)
208{
209 return mm_access(task, PTRACE_MODE_READ);
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int proc_pid_cmdline(struct task_struct *task, char * buffer)
213{
214 int res = 0;
215 unsigned int len;
216 struct mm_struct *mm = get_task_mm(task);
217 if (!mm)
218 goto out;
219 if (!mm->arg_end)
220 goto out_mm; /* Shh! No looking before we're done */
221
222 len = mm->arg_end - mm->arg_start;
223
224 if (len > PAGE_SIZE)
225 len = PAGE_SIZE;
226
227 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
228
229 // If the nul at the end of args has been overwritten, then
230 // assume application is using setproctitle(3).
231 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
232 len = strnlen(buffer, res);
233 if (len < res) {
234 res = len;
235 } else {
236 len = mm->env_end - mm->env_start;
237 if (len > PAGE_SIZE - res)
238 len = PAGE_SIZE - res;
239 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
240 res = strnlen(buffer, res);
241 }
242 }
243out_mm:
244 mmput(mm);
245out:
246 return res;
247}
248
249static int proc_pid_auxv(struct task_struct *task, char *buffer)
250{
Al Viro2fadaef2011-02-15 22:52:11 -0500251 struct mm_struct *mm = mm_for_maps(task);
252 int res = PTR_ERR(mm);
253 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300255 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300257 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 res = nwords * sizeof(mm->saved_auxv[0]);
259 if (res > PAGE_SIZE)
260 res = PAGE_SIZE;
261 memcpy(buffer, mm->saved_auxv, res);
262 mmput(mm);
263 }
264 return res;
265}
266
267
268#ifdef CONFIG_KALLSYMS
269/*
270 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
271 * Returns the resolved symbol. If that fails, simply return the address.
272 */
273static int proc_pid_wchan(struct task_struct *task, char *buffer)
274{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700275 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700276 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 wchan = get_wchan(task);
279
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700280 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600281 if (!ptrace_may_access(task, PTRACE_MODE_READ))
282 return 0;
283 else
284 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700285 else
286 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288#endif /* CONFIG_KALLSYMS */
289
Al Viroa9712bc2011-03-23 15:52:50 -0400290static int lock_trace(struct task_struct *task)
291{
292 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
293 if (err)
294 return err;
295 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
296 mutex_unlock(&task->signal->cred_guard_mutex);
297 return -EPERM;
298 }
299 return 0;
300}
301
302static void unlock_trace(struct task_struct *task)
303{
304 mutex_unlock(&task->signal->cred_guard_mutex);
305}
306
Ken Chen2ec220e2008-11-10 11:26:08 +0300307#ifdef CONFIG_STACKTRACE
308
309#define MAX_STACK_TRACE_DEPTH 64
310
311static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
312 struct pid *pid, struct task_struct *task)
313{
314 struct stack_trace trace;
315 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400316 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300317 int i;
318
319 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
320 if (!entries)
321 return -ENOMEM;
322
323 trace.nr_entries = 0;
324 trace.max_entries = MAX_STACK_TRACE_DEPTH;
325 trace.entries = entries;
326 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300327
Al Viroa9712bc2011-03-23 15:52:50 -0400328 err = lock_trace(task);
329 if (!err) {
330 save_stack_trace_tsk(task, &trace);
331
332 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700333 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400334 (void *)entries[i], (void *)entries[i]);
335 }
336 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300337 }
338 kfree(entries);
339
Al Viroa9712bc2011-03-23 15:52:50 -0400340 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300341}
342#endif
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#ifdef CONFIG_SCHEDSTATS
345/*
346 * Provides /proc/PID/schedstat
347 */
348static int proc_pid_schedstat(struct task_struct *task, char *buffer)
349{
Balbir Singh172ba842007-07-09 18:52:00 +0200350 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100351 (unsigned long long)task->se.sum_exec_runtime,
352 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200353 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355#endif
356
Arjan van de Ven97455122008-01-25 21:08:34 +0100357#ifdef CONFIG_LATENCYTOP
358static int lstats_show_proc(struct seq_file *m, void *v)
359{
360 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800361 struct inode *inode = m->private;
362 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100363
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800364 if (!task)
365 return -ESRCH;
366 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100367 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800368 struct latency_record *lr = &task->latency_record[i];
369 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100370 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800371 seq_printf(m, "%i %li %li",
372 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100373 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800374 unsigned long bt = lr->backtrace[q];
375 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100376 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800377 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100378 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800379 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100380 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800381 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100382 }
383
384 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800385 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100386 return 0;
387}
388
389static int lstats_open(struct inode *inode, struct file *file)
390{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800391 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800392}
393
Arjan van de Ven97455122008-01-25 21:08:34 +0100394static ssize_t lstats_write(struct file *file, const char __user *buf,
395 size_t count, loff_t *offs)
396{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800397 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100398
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800399 if (!task)
400 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100401 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800402 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100403
404 return count;
405}
406
407static const struct file_operations proc_lstats_operations = {
408 .open = lstats_open,
409 .read = seq_read,
410 .write = lstats_write,
411 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800412 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100413};
414
415#endif
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static int proc_oom_score(struct task_struct *task, char *buffer)
418{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200419 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700421 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200422 if (pid_alive(task))
David Rientjesa63d83f2010-08-09 17:19:46 -0700423 points = oom_badness(task, NULL, NULL,
424 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700425 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return sprintf(buffer, "%lu\n", points);
427}
428
Neil Hormand85f50d2007-10-18 23:40:37 -0700429struct limit_names {
430 char *name;
431 char *unit;
432};
433
434static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700435 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700436 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
437 [RLIMIT_DATA] = {"Max data size", "bytes"},
438 [RLIMIT_STACK] = {"Max stack size", "bytes"},
439 [RLIMIT_CORE] = {"Max core file size", "bytes"},
440 [RLIMIT_RSS] = {"Max resident set", "bytes"},
441 [RLIMIT_NPROC] = {"Max processes", "processes"},
442 [RLIMIT_NOFILE] = {"Max open files", "files"},
443 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
444 [RLIMIT_AS] = {"Max address space", "bytes"},
445 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
446 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
447 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
448 [RLIMIT_NICE] = {"Max nice priority", NULL},
449 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800450 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700451};
452
453/* Display limits for a process */
454static int proc_pid_limits(struct task_struct *task, char *buffer)
455{
456 unsigned int i;
457 int count = 0;
458 unsigned long flags;
459 char *bufptr = buffer;
460
461 struct rlimit rlim[RLIM_NLIMITS];
462
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400463 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700464 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700465 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
466 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700467
468 /*
469 * print the file header
470 */
471 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
472 "Limit", "Soft Limit", "Hard Limit", "Units");
473
474 for (i = 0; i < RLIM_NLIMITS; i++) {
475 if (rlim[i].rlim_cur == RLIM_INFINITY)
476 count += sprintf(&bufptr[count], "%-25s %-20s ",
477 lnames[i].name, "unlimited");
478 else
479 count += sprintf(&bufptr[count], "%-25s %-20lu ",
480 lnames[i].name, rlim[i].rlim_cur);
481
482 if (rlim[i].rlim_max == RLIM_INFINITY)
483 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
484 else
485 count += sprintf(&bufptr[count], "%-20lu ",
486 rlim[i].rlim_max);
487
488 if (lnames[i].unit)
489 count += sprintf(&bufptr[count], "%-10s\n",
490 lnames[i].unit);
491 else
492 count += sprintf(&bufptr[count], "\n");
493 }
494
495 return count;
496}
497
Roland McGrathebcb6732008-07-25 19:46:00 -0700498#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
499static int proc_pid_syscall(struct task_struct *task, char *buffer)
500{
501 long nr;
502 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400503 int res = lock_trace(task);
504 if (res)
505 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700506
507 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400508 res = sprintf(buffer, "running\n");
509 else if (nr < 0)
510 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
511 else
512 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700513 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
514 nr,
515 args[0], args[1], args[2], args[3], args[4], args[5],
516 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400517 unlock_trace(task);
518 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700519}
520#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/************************************************************************/
523/* Here the fs part begins */
524/************************************************************************/
525
526/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700527static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700529 struct task_struct *task;
530 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700531 /* Allow access to a task's file descriptors if it is us or we
532 * may use ptrace attach to the process and find out that
533 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700534 */
535 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700536 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400537 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700538 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700539 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700540 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800543int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700544{
545 int error;
546 struct inode *inode = dentry->d_inode;
547
548 if (attr->ia_valid & ATTR_MODE)
549 return -EPERM;
550
551 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200552 if (error)
553 return error;
554
555 if ((attr->ia_valid & ATTR_SIZE) &&
556 attr->ia_size != i_size_read(inode)) {
557 error = vmtruncate(inode, attr->ia_size);
558 if (error)
559 return error;
560 }
561
562 setattr_copy(inode, attr);
563 mark_inode_dirty(inode);
564 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700565}
566
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800567/*
568 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
569 * or euid/egid (for hide_pid_min=2)?
570 */
571static bool has_pid_permissions(struct pid_namespace *pid,
572 struct task_struct *task,
573 int hide_pid_min)
574{
575 if (pid->hide_pid < hide_pid_min)
576 return true;
577 if (in_group_p(pid->pid_gid))
578 return true;
579 return ptrace_may_access(task, PTRACE_MODE_READ);
580}
581
582
583static int proc_pid_permission(struct inode *inode, int mask)
584{
585 struct pid_namespace *pid = inode->i_sb->s_fs_info;
586 struct task_struct *task;
587 bool has_perms;
588
589 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800590 if (!task)
591 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800592 has_perms = has_pid_permissions(pid, task, 1);
593 put_task_struct(task);
594
595 if (!has_perms) {
596 if (pid->hide_pid == 2) {
597 /*
598 * Let's make getdents(), stat(), and open()
599 * consistent with each other. If a process
600 * may not stat() a file, it shouldn't be seen
601 * in procfs at all.
602 */
603 return -ENOENT;
604 }
605
606 return -EPERM;
607 }
608 return generic_permission(inode, mask);
609}
610
611
612
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800613static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700614 .setattr = proc_setattr,
615};
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
618
619static ssize_t proc_info_read(struct file * file, char __user * buf,
620 size_t count, loff_t *ppos)
621{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800622 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unsigned long page;
624 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700625 struct task_struct *task = get_proc_task(inode);
626
627 length = -ESRCH;
628 if (!task)
629 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (count > PROC_BLOCK_SIZE)
632 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700633
634 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700635 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700636 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 length = PROC_I(inode)->op.proc_read(task, (char*)page);
639
640 if (length >= 0)
641 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
642 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700643out:
644 put_task_struct(task);
645out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return length;
647}
648
Arjan van de Ven00977a52007-02-12 00:55:34 -0800649static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100651 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652};
653
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800654static int proc_single_show(struct seq_file *m, void *v)
655{
656 struct inode *inode = m->private;
657 struct pid_namespace *ns;
658 struct pid *pid;
659 struct task_struct *task;
660 int ret;
661
662 ns = inode->i_sb->s_fs_info;
663 pid = proc_pid(inode);
664 task = get_pid_task(pid, PIDTYPE_PID);
665 if (!task)
666 return -ESRCH;
667
668 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
669
670 put_task_struct(task);
671 return ret;
672}
673
674static int proc_single_open(struct inode *inode, struct file *filp)
675{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800676 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800677}
678
679static const struct file_operations proc_single_file_operations = {
680 .open = proc_single_open,
681 .read = seq_read,
682 .llseek = seq_lseek,
683 .release = single_release,
684};
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686static int mem_open(struct inode* inode, struct file* file)
687{
Linus Torvaldse2683372012-01-17 15:21:19 -0800688 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
689 struct mm_struct *mm;
690
691 if (!task)
692 return -ESRCH;
693
694 mm = mm_access(task, PTRACE_MODE_ATTACH);
695 put_task_struct(task);
696
697 if (IS_ERR(mm))
698 return PTR_ERR(mm);
699
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100700 if (mm) {
701 /* ensure this mm_struct can't be freed */
702 atomic_inc(&mm->mm_count);
703 /* but do not pin its memory */
704 mmput(mm);
705 }
706
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700707 /* OK to pass negative loff_t, we can catch out-of-range */
708 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvaldse2683372012-01-17 15:21:19 -0800709 file->private_data = mm;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return 0;
712}
713
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100714static ssize_t mem_rw(struct file *file, char __user *buf,
715 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Linus Torvaldse2683372012-01-17 15:21:19 -0800717 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100718 unsigned long addr = *ppos;
719 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Linus Torvaldse2683372012-01-17 15:21:19 -0800722 if (!mm)
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Mel Gormane12ba742007-10-16 01:25:52 -0700725 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800727 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700729 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100730 if (!atomic_inc_not_zero(&mm->mm_users))
731 goto free;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100734 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100736 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 copied = -EFAULT;
738 break;
739 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100740
741 this_len = access_remote_vm(mm, addr, page, this_len, write);
742 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!copied)
744 copied = -EIO;
745 break;
746 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100747
748 if (!write && copy_to_user(buf, page, this_len)) {
749 copied = -EFAULT;
750 break;
751 }
752
753 buf += this_len;
754 addr += this_len;
755 copied += this_len;
756 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100758 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700759
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100760 mmput(mm);
761free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700762 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return copied;
764}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100766static ssize_t mem_read(struct file *file, char __user *buf,
767 size_t count, loff_t *ppos)
768{
769 return mem_rw(file, buf, count, ppos, 0);
770}
771
772static ssize_t mem_write(struct file *file, const char __user *buf,
773 size_t count, loff_t *ppos)
774{
775 return mem_rw(file, (char __user*)buf, count, ppos, 1);
776}
777
Matt Mackall85863e42008-02-04 22:29:04 -0800778loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 switch (orig) {
781 case 0:
782 file->f_pos = offset;
783 break;
784 case 1:
785 file->f_pos += offset;
786 break;
787 default:
788 return -EINVAL;
789 }
790 force_successful_syscall_return();
791 return file->f_pos;
792}
793
Linus Torvaldse2683372012-01-17 15:21:19 -0800794static int mem_release(struct inode *inode, struct file *file)
795{
796 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100797 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100798 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800799 return 0;
800}
801
Arjan van de Ven00977a52007-02-12 00:55:34 -0800802static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 .llseek = mem_lseek,
804 .read = mem_read,
805 .write = mem_write,
806 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800807 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808};
809
James Pearson315e28c2007-10-16 23:30:17 -0700810static ssize_t environ_read(struct file *file, char __user *buf,
811 size_t count, loff_t *ppos)
812{
813 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
814 char *page;
815 unsigned long src = *ppos;
816 int ret = -ESRCH;
817 struct mm_struct *mm;
818
819 if (!task)
820 goto out_no_task;
821
James Pearson315e28c2007-10-16 23:30:17 -0700822 ret = -ENOMEM;
823 page = (char *)__get_free_page(GFP_TEMPORARY);
824 if (!page)
825 goto out;
826
James Pearson315e28c2007-10-16 23:30:17 -0700827
Al Virod6f64b82011-02-15 22:26:01 -0500828 mm = mm_for_maps(task);
829 ret = PTR_ERR(mm);
830 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700831 goto out_free;
832
Al Virod6f64b82011-02-15 22:26:01 -0500833 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -0700834 while (count > 0) {
835 int this_len, retval, max_len;
836
837 this_len = mm->env_end - (mm->env_start + src);
838
839 if (this_len <= 0)
840 break;
841
842 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
843 this_len = (this_len > max_len) ? max_len : this_len;
844
845 retval = access_process_vm(task, (mm->env_start + src),
846 page, this_len, 0);
847
848 if (retval <= 0) {
849 ret = retval;
850 break;
851 }
852
853 if (copy_to_user(buf, page, retval)) {
854 ret = -EFAULT;
855 break;
856 }
857
858 ret += retval;
859 src += retval;
860 buf += retval;
861 count -= retval;
862 }
863 *ppos = src;
864
865 mmput(mm);
866out_free:
867 free_page((unsigned long) page);
868out:
869 put_task_struct(task);
870out_no_task:
871 return ret;
872}
873
874static const struct file_operations proc_environ_operations = {
875 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100876 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700877};
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879static ssize_t oom_adjust_read(struct file *file, char __user *buf,
880 size_t count, loff_t *ppos)
881{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800882 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700883 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700885 int oom_adjust = OOM_DISABLE;
886 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Eric W. Biederman99f89552006-06-26 00:25:55 -0700888 if (!task)
889 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700890
891 if (lock_task_sighand(task, &flags)) {
892 oom_adjust = task->signal->oom_adj;
893 unlock_task_sighand(task, &flags);
894 }
895
Eric W. Biederman99f89552006-06-26 00:25:55 -0700896 put_task_struct(task);
897
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700898 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700899
900 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
904 size_t count, loff_t *ppos)
905{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700906 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700907 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700908 int oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700909 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700910 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700912 memset(buffer, 0, sizeof(buffer));
913 if (count > sizeof(buffer) - 1)
914 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700915 if (copy_from_user(buffer, buf, count)) {
916 err = -EFAULT;
917 goto out;
918 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700919
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700920 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700921 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700922 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700923 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -0700924 oom_adjust != OOM_DISABLE) {
925 err = -EINVAL;
926 goto out;
927 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700928
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800929 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -0700930 if (!task) {
931 err = -ESRCH;
932 goto out;
933 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700934
Ying Han3d5992d2010-10-26 14:21:23 -0700935 task_lock(task);
936 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -0700937 err = -EINVAL;
938 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -0700939 }
940
David Rientjesd19d5472010-10-26 14:21:26 -0700941 if (!lock_task_sighand(task, &flags)) {
942 err = -ESRCH;
943 goto err_task_lock;
944 }
945
946 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
947 err = -EACCES;
948 goto err_sighand;
949 }
950
David Rientjes51b1bd22010-08-09 17:19:47 -0700951 /*
952 * Warn that /proc/pid/oom_adj is deprecated, see
953 * Documentation/feature-removal-schedule.txt.
954 */
Linus Torvaldsc2142702011-08-06 11:43:08 -0700955 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 -0700956 current->comm, task_pid_nr(current), task_pid_nr(task),
957 task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700958 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -0700959 /*
960 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
961 * value is always attainable.
962 */
963 if (task->signal->oom_adj == OOM_ADJUST_MAX)
964 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
965 else
966 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
967 -OOM_DISABLE;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800968 trace_oom_score_adj_update(task);
David Rientjes723548b2010-10-26 14:21:25 -0700969err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700970 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -0700971err_task_lock:
972 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700973 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -0700974out:
975 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Mike Chanf371edd2008-04-24 10:22:26 -0700978static int oom_adjust_permission(struct inode *inode, int mask)
979{
980 uid_t uid;
981 struct task_struct *p;
982
983 p = get_proc_task(inode);
984 if(p) {
985 uid = task_uid(p);
986 put_task_struct(p);
987 }
988
989 /*
990 * System Server (uid == 1000) is granted access to oom_adj of all
991 * android applications (uid > 10000) as and services (uid >= 1000)
992 */
993 if (p && (current_fsuid() == 1000) && (uid >= 1000)) {
994 if (inode->i_mode >> 6 & mask) {
995 return 0;
996 }
997 }
998
999 /* Fall back to default. */
1000 return generic_permission(inode, mask);
1001}
1002
1003static const struct inode_operations proc_oom_adjust_inode_operations = {
1004 .permission = oom_adjust_permission,
1005};
1006
Arjan van de Ven00977a52007-02-12 00:55:34 -08001007static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 .read = oom_adjust_read,
1009 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001010 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011};
1012
David Rientjesa63d83f2010-08-09 17:19:46 -07001013static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1014 size_t count, loff_t *ppos)
1015{
1016 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1017 char buffer[PROC_NUMBUF];
1018 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1019 unsigned long flags;
1020 size_t len;
1021
1022 if (!task)
1023 return -ESRCH;
1024 if (lock_task_sighand(task, &flags)) {
1025 oom_score_adj = task->signal->oom_score_adj;
1026 unlock_task_sighand(task, &flags);
1027 }
1028 put_task_struct(task);
1029 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1030 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1031}
1032
1033static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1034 size_t count, loff_t *ppos)
1035{
1036 struct task_struct *task;
1037 char buffer[PROC_NUMBUF];
1038 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001039 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001040 int err;
1041
1042 memset(buffer, 0, sizeof(buffer));
1043 if (count > sizeof(buffer) - 1)
1044 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001045 if (copy_from_user(buffer, buf, count)) {
1046 err = -EFAULT;
1047 goto out;
1048 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001049
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001050 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001051 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001052 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001053 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001054 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1055 err = -EINVAL;
1056 goto out;
1057 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001058
1059 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001060 if (!task) {
1061 err = -ESRCH;
1062 goto out;
1063 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001064
Ying Han3d5992d2010-10-26 14:21:23 -07001065 task_lock(task);
1066 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001067 err = -EINVAL;
1068 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001069 }
David Rientjesd19d5472010-10-26 14:21:26 -07001070
1071 if (!lock_task_sighand(task, &flags)) {
1072 err = -ESRCH;
1073 goto err_task_lock;
1074 }
1075
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001076 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001077 !capable(CAP_SYS_RESOURCE)) {
1078 err = -EACCES;
1079 goto err_sighand;
1080 }
1081
David Rientjesa63d83f2010-08-09 17:19:46 -07001082 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001083 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1084 task->signal->oom_score_adj_min = oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001085 trace_oom_score_adj_update(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001086 /*
1087 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1088 * always attainable.
1089 */
1090 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1091 task->signal->oom_adj = OOM_DISABLE;
1092 else
1093 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1094 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001095err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001096 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001097err_task_lock:
1098 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001099 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001100out:
1101 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001102}
1103
1104static const struct file_operations proc_oom_score_adj_operations = {
1105 .read = oom_score_adj_read,
1106 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001107 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001108};
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110#ifdef CONFIG_AUDITSYSCALL
1111#define TMPBUFLEN 21
1112static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1113 size_t count, loff_t *ppos)
1114{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001115 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001116 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ssize_t length;
1118 char tmpbuf[TMPBUFLEN];
1119
Eric W. Biederman99f89552006-06-26 00:25:55 -07001120 if (!task)
1121 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001123 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001124 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1126}
1127
1128static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1129 size_t count, loff_t *ppos)
1130{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001131 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 char *page, *tmp;
1133 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 uid_t loginuid;
1135
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001136 rcu_read_lock();
1137 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1138 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001140 }
1141 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Al Viroe0182902006-05-18 08:28:02 -04001143 if (count >= PAGE_SIZE)
1144 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (*ppos != 0) {
1147 /* No partial writes. */
1148 return -EINVAL;
1149 }
Mel Gormane12ba742007-10-16 01:25:52 -07001150 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (!page)
1152 return -ENOMEM;
1153 length = -EFAULT;
1154 if (copy_from_user(page, buf, count))
1155 goto out_free_page;
1156
Al Viroe0182902006-05-18 08:28:02 -04001157 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 loginuid = simple_strtoul(page, &tmp, 10);
1159 if (tmp == page) {
1160 length = -EINVAL;
1161 goto out_free_page;
1162
1163 }
Eric Paris0a300be2012-01-03 14:23:08 -05001164 length = audit_set_loginuid(loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (likely(length == 0))
1166 length = count;
1167
1168out_free_page:
1169 free_page((unsigned long) page);
1170 return length;
1171}
1172
Arjan van de Ven00977a52007-02-12 00:55:34 -08001173static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 .read = proc_loginuid_read,
1175 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001176 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177};
Eric Paris1e0bd752008-03-13 08:15:31 -04001178
1179static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1180 size_t count, loff_t *ppos)
1181{
1182 struct inode * inode = file->f_path.dentry->d_inode;
1183 struct task_struct *task = get_proc_task(inode);
1184 ssize_t length;
1185 char tmpbuf[TMPBUFLEN];
1186
1187 if (!task)
1188 return -ESRCH;
1189 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1190 audit_get_sessionid(task));
1191 put_task_struct(task);
1192 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1193}
1194
1195static const struct file_operations proc_sessionid_operations = {
1196 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001197 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001198};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199#endif
1200
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001201#ifdef CONFIG_FAULT_INJECTION
1202static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1203 size_t count, loff_t *ppos)
1204{
1205 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1206 char buffer[PROC_NUMBUF];
1207 size_t len;
1208 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001209
1210 if (!task)
1211 return -ESRCH;
1212 make_it_fail = task->make_it_fail;
1213 put_task_struct(task);
1214
1215 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001216
1217 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001218}
1219
1220static ssize_t proc_fault_inject_write(struct file * file,
1221 const char __user * buf, size_t count, loff_t *ppos)
1222{
1223 struct task_struct *task;
1224 char buffer[PROC_NUMBUF], *end;
1225 int make_it_fail;
1226
1227 if (!capable(CAP_SYS_RESOURCE))
1228 return -EPERM;
1229 memset(buffer, 0, sizeof(buffer));
1230 if (count > sizeof(buffer) - 1)
1231 count = sizeof(buffer) - 1;
1232 if (copy_from_user(buffer, buf, count))
1233 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001234 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1235 if (*end)
1236 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001237 task = get_proc_task(file->f_dentry->d_inode);
1238 if (!task)
1239 return -ESRCH;
1240 task->make_it_fail = make_it_fail;
1241 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001242
1243 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001244}
1245
Arjan van de Ven00977a52007-02-12 00:55:34 -08001246static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001247 .read = proc_fault_inject_read,
1248 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001249 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001250};
1251#endif
1252
Arjan van de Ven97455122008-01-25 21:08:34 +01001253
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001254#ifdef CONFIG_SCHED_DEBUG
1255/*
1256 * Print out various scheduling related per-task fields:
1257 */
1258static int sched_show(struct seq_file *m, void *v)
1259{
1260 struct inode *inode = m->private;
1261 struct task_struct *p;
1262
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001263 p = get_proc_task(inode);
1264 if (!p)
1265 return -ESRCH;
1266 proc_sched_show_task(p, m);
1267
1268 put_task_struct(p);
1269
1270 return 0;
1271}
1272
1273static ssize_t
1274sched_write(struct file *file, const char __user *buf,
1275 size_t count, loff_t *offset)
1276{
1277 struct inode *inode = file->f_path.dentry->d_inode;
1278 struct task_struct *p;
1279
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001280 p = get_proc_task(inode);
1281 if (!p)
1282 return -ESRCH;
1283 proc_sched_set_task(p);
1284
1285 put_task_struct(p);
1286
1287 return count;
1288}
1289
1290static int sched_open(struct inode *inode, struct file *filp)
1291{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001292 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001293}
1294
1295static const struct file_operations proc_pid_sched_operations = {
1296 .open = sched_open,
1297 .read = seq_read,
1298 .write = sched_write,
1299 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001300 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001301};
1302
1303#endif
1304
Mike Galbraith5091faa2010-11-30 14:18:03 +01001305#ifdef CONFIG_SCHED_AUTOGROUP
1306/*
1307 * Print out autogroup related information:
1308 */
1309static int sched_autogroup_show(struct seq_file *m, void *v)
1310{
1311 struct inode *inode = m->private;
1312 struct task_struct *p;
1313
1314 p = get_proc_task(inode);
1315 if (!p)
1316 return -ESRCH;
1317 proc_sched_autogroup_show_task(p, m);
1318
1319 put_task_struct(p);
1320
1321 return 0;
1322}
1323
1324static ssize_t
1325sched_autogroup_write(struct file *file, const char __user *buf,
1326 size_t count, loff_t *offset)
1327{
1328 struct inode *inode = file->f_path.dentry->d_inode;
1329 struct task_struct *p;
1330 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001331 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001332 int err;
1333
1334 memset(buffer, 0, sizeof(buffer));
1335 if (count > sizeof(buffer) - 1)
1336 count = sizeof(buffer) - 1;
1337 if (copy_from_user(buffer, buf, count))
1338 return -EFAULT;
1339
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001340 err = kstrtoint(strstrip(buffer), 0, &nice);
1341 if (err < 0)
1342 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001343
1344 p = get_proc_task(inode);
1345 if (!p)
1346 return -ESRCH;
1347
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001348 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001349 if (err)
1350 count = err;
1351
1352 put_task_struct(p);
1353
1354 return count;
1355}
1356
1357static int sched_autogroup_open(struct inode *inode, struct file *filp)
1358{
1359 int ret;
1360
1361 ret = single_open(filp, sched_autogroup_show, NULL);
1362 if (!ret) {
1363 struct seq_file *m = filp->private_data;
1364
1365 m->private = inode;
1366 }
1367 return ret;
1368}
1369
1370static const struct file_operations proc_pid_sched_autogroup_operations = {
1371 .open = sched_autogroup_open,
1372 .read = seq_read,
1373 .write = sched_autogroup_write,
1374 .llseek = seq_lseek,
1375 .release = single_release,
1376};
1377
1378#endif /* CONFIG_SCHED_AUTOGROUP */
1379
john stultz4614a696b2009-12-14 18:00:05 -08001380static ssize_t comm_write(struct file *file, const char __user *buf,
1381 size_t count, loff_t *offset)
1382{
1383 struct inode *inode = file->f_path.dentry->d_inode;
1384 struct task_struct *p;
1385 char buffer[TASK_COMM_LEN];
1386
1387 memset(buffer, 0, sizeof(buffer));
1388 if (count > sizeof(buffer) - 1)
1389 count = sizeof(buffer) - 1;
1390 if (copy_from_user(buffer, buf, count))
1391 return -EFAULT;
1392
1393 p = get_proc_task(inode);
1394 if (!p)
1395 return -ESRCH;
1396
1397 if (same_thread_group(current, p))
1398 set_task_comm(p, buffer);
1399 else
1400 count = -EINVAL;
1401
1402 put_task_struct(p);
1403
1404 return count;
1405}
1406
1407static int comm_show(struct seq_file *m, void *v)
1408{
1409 struct inode *inode = m->private;
1410 struct task_struct *p;
1411
1412 p = get_proc_task(inode);
1413 if (!p)
1414 return -ESRCH;
1415
1416 task_lock(p);
1417 seq_printf(m, "%s\n", p->comm);
1418 task_unlock(p);
1419
1420 put_task_struct(p);
1421
1422 return 0;
1423}
1424
1425static int comm_open(struct inode *inode, struct file *filp)
1426{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001427 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001428}
1429
1430static const struct file_operations proc_pid_set_comm_operations = {
1431 .open = comm_open,
1432 .read = seq_read,
1433 .write = comm_write,
1434 .llseek = seq_lseek,
1435 .release = single_release,
1436};
1437
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001438static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001439{
1440 struct task_struct *task;
1441 struct mm_struct *mm;
1442 struct file *exe_file;
1443
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001444 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001445 if (!task)
1446 return -ENOENT;
1447 mm = get_task_mm(task);
1448 put_task_struct(task);
1449 if (!mm)
1450 return -ENOENT;
1451 exe_file = get_mm_exe_file(mm);
1452 mmput(mm);
1453 if (exe_file) {
1454 *exe_path = exe_file->f_path;
1455 path_get(&exe_file->f_path);
1456 fput(exe_file);
1457 return 0;
1458 } else
1459 return -ENOENT;
1460}
1461
Al Viro008b1502005-08-20 00:17:39 +01001462static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct inode *inode = dentry->d_inode;
1465 int error = -EACCES;
1466
1467 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001468 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Eric W. Biederman778c1142006-06-26 00:25:58 -07001470 /* Are we allowed to snoop on the tasks file descriptors? */
1471 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001474 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475out:
Al Viro008b1502005-08-20 00:17:39 +01001476 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001479static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Mel Gormane12ba742007-10-16 01:25:52 -07001481 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001482 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 int len;
1484
1485 if (!tmp)
1486 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001487
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001488 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001489 len = PTR_ERR(pathname);
1490 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001492 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 if (len > buflen)
1495 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001496 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 len = -EFAULT;
1498 out:
1499 free_page((unsigned long)tmp);
1500 return len;
1501}
1502
1503static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1504{
1505 int error = -EACCES;
1506 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001507 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Eric W. Biederman778c1142006-06-26 00:25:58 -07001509 /* Are we allowed to snoop on the tasks file descriptors? */
1510 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001513 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (error)
1515 goto out;
1516
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001517 error = do_proc_readlink(&path, buffer, buflen);
1518 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return error;
1521}
1522
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001523static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001525 .follow_link = proc_pid_follow_link,
1526 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527};
1528
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001529
1530/* building an inode */
1531
1532static int task_dumpable(struct task_struct *task)
1533{
1534 int dumpable = 0;
1535 struct mm_struct *mm;
1536
1537 task_lock(task);
1538 mm = task->mm;
1539 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001540 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001541 task_unlock(task);
1542 if(dumpable == 1)
1543 return 1;
1544 return 0;
1545}
1546
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001547struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001548{
1549 struct inode * inode;
1550 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001551 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001552
1553 /* We need a new inode */
1554
1555 inode = new_inode(sb);
1556 if (!inode)
1557 goto out;
1558
1559 /* Common stuff */
1560 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001561 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001562 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001563 inode->i_op = &proc_def_inode_operations;
1564
1565 /*
1566 * grab the reference to task.
1567 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001568 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001569 if (!ei->pid)
1570 goto out_unlock;
1571
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001572 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001573 rcu_read_lock();
1574 cred = __task_cred(task);
1575 inode->i_uid = cred->euid;
1576 inode->i_gid = cred->egid;
1577 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001578 }
1579 security_task_to_inode(task, inode);
1580
1581out:
1582 return inode;
1583
1584out_unlock:
1585 iput(inode);
1586 return NULL;
1587}
1588
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001589int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001590{
1591 struct inode *inode = dentry->d_inode;
1592 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001593 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001594 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001595
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001596 generic_fillattr(inode, stat);
1597
1598 rcu_read_lock();
1599 stat->uid = 0;
1600 stat->gid = 0;
1601 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1602 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001603 if (!has_pid_permissions(pid, task, 2)) {
1604 rcu_read_unlock();
1605 /*
1606 * This doesn't prevent learning whether PID exists,
1607 * it only makes getattr() consistent with readdir().
1608 */
1609 return -ENOENT;
1610 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001611 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1612 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001613 cred = __task_cred(task);
1614 stat->uid = cred->euid;
1615 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001616 }
1617 }
1618 rcu_read_unlock();
1619 return 0;
1620}
1621
1622/* dentry stuff */
1623
1624/*
1625 * Exceptional case: normally we are not allowed to unhash a busy
1626 * directory. In this case, however, we can do it - no aliasing problems
1627 * due to the way we treat inodes.
1628 *
1629 * Rewrite the inode's ownerships here because the owning task may have
1630 * performed a setuid(), etc.
1631 *
1632 * Before the /proc/pid/status file was created the only way to read
1633 * the effective uid of a /process was to stat /proc/pid. Reading
1634 * /proc/pid/status is slow enough that procps and other packages
1635 * kept stating /proc/pid. To keep the rules in /proc simple I have
1636 * made this apply to all per process world readable and executable
1637 * directories.
1638 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001639int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001640{
Nick Piggin34286d62011-01-07 17:49:57 +11001641 struct inode *inode;
1642 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001643 const struct cred *cred;
1644
Nick Piggin34286d62011-01-07 17:49:57 +11001645 if (nd && nd->flags & LOOKUP_RCU)
1646 return -ECHILD;
1647
1648 inode = dentry->d_inode;
1649 task = get_proc_task(inode);
1650
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001651 if (task) {
1652 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1653 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001654 rcu_read_lock();
1655 cred = __task_cred(task);
1656 inode->i_uid = cred->euid;
1657 inode->i_gid = cred->egid;
1658 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001659 } else {
1660 inode->i_uid = 0;
1661 inode->i_gid = 0;
1662 }
1663 inode->i_mode &= ~(S_ISUID | S_ISGID);
1664 security_task_to_inode(task, inode);
1665 put_task_struct(task);
1666 return 1;
1667 }
1668 d_drop(dentry);
1669 return 0;
1670}
1671
Nick Pigginfe15ce42011-01-07 17:49:23 +11001672static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001673{
1674 /* Is the task we represent dead?
1675 * If so, then don't put the dentry on the lru list,
1676 * kill it immediately.
1677 */
1678 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1679}
1680
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001681const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001682{
1683 .d_revalidate = pid_revalidate,
1684 .d_delete = pid_delete_dentry,
1685};
1686
1687/* Lookups */
1688
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001689/*
1690 * Fill a directory entry.
1691 *
1692 * If possible create the dcache entry and derive our inode number and
1693 * file type from dcache entry.
1694 *
1695 * Since all of the proc inode numbers are dynamically generated, the inode
1696 * numbers do not exist until the inode is cache. This means creating the
1697 * the dcache entry in readdir is necessary to keep the inode numbers
1698 * reported by readdir in sync with the inode numbers reported
1699 * by stat.
1700 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001701int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1702 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001703 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001704{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001705 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001706 struct inode *inode;
1707 struct qstr qname;
1708 ino_t ino = 0;
1709 unsigned type = DT_UNKNOWN;
1710
1711 qname.name = name;
1712 qname.len = len;
1713 qname.hash = full_name_hash(name, len);
1714
1715 child = d_lookup(dir, &qname);
1716 if (!child) {
1717 struct dentry *new;
1718 new = d_alloc(dir, &qname);
1719 if (new) {
1720 child = instantiate(dir->d_inode, new, task, ptr);
1721 if (child)
1722 dput(new);
1723 else
1724 child = new;
1725 }
1726 }
1727 if (!child || IS_ERR(child) || !child->d_inode)
1728 goto end_instantiate;
1729 inode = child->d_inode;
1730 if (inode) {
1731 ino = inode->i_ino;
1732 type = inode->i_mode >> 12;
1733 }
1734 dput(child);
1735end_instantiate:
1736 if (!ino)
1737 ino = find_inode_number(dir, &qname);
1738 if (!ino)
1739 ino = 1;
1740 return filldir(dirent, name, len, filp->f_pos, ino, type);
1741}
1742
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001743static unsigned name_to_int(struct dentry *dentry)
1744{
1745 const char *name = dentry->d_name.name;
1746 int len = dentry->d_name.len;
1747 unsigned n = 0;
1748
1749 if (len > 1 && *name == '0')
1750 goto out;
1751 while (len-- > 0) {
1752 unsigned c = *name++ - '0';
1753 if (c > 9)
1754 goto out;
1755 if (n >= (~0U-9)/10)
1756 goto out;
1757 n *= 10;
1758 n += c;
1759 }
1760 return n;
1761out:
1762 return ~0U;
1763}
1764
Miklos Szeredi27932742007-05-08 00:26:17 -07001765#define PROC_FDINFO_MAX 64
1766
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001767static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768{
Linus Torvalds5e442a42011-11-09 18:16:00 -05001769 struct task_struct *task = get_proc_task(inode);
1770 struct files_struct *files = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001771 struct file *file;
1772 int fd = proc_fd(inode);
1773
Linus Torvalds5e442a42011-11-09 18:16:00 -05001774 if (task) {
1775 files = get_files_struct(task);
1776 put_task_struct(task);
1777 }
1778 if (files) {
1779 /*
1780 * We are not taking a ref to the file structure, so we must
1781 * hold ->file_lock.
1782 */
1783 spin_lock(&files->file_lock);
1784 file = fcheck_files(files, fd);
1785 if (file) {
1786 unsigned int f_flags;
1787 struct fdtable *fdt;
Linus Torvalds1117f722011-08-06 11:51:33 -07001788
Linus Torvalds5e442a42011-11-09 18:16:00 -05001789 fdt = files_fdtable(files);
1790 f_flags = file->f_flags & ~O_CLOEXEC;
David Howells1dce27c2012-02-16 17:49:42 +00001791 if (close_on_exec(fd, fdt))
Linus Torvalds5e442a42011-11-09 18:16:00 -05001792 f_flags |= O_CLOEXEC;
Linus Torvalds1117f722011-08-06 11:51:33 -07001793
Linus Torvalds5e442a42011-11-09 18:16:00 -05001794 if (path) {
1795 *path = file->f_path;
1796 path_get(&file->f_path);
1797 }
1798 if (info)
1799 snprintf(info, PROC_FDINFO_MAX,
1800 "pos:\t%lli\n"
1801 "flags:\t0%o\n",
1802 (long long) file->f_pos,
1803 f_flags);
1804 spin_unlock(&files->file_lock);
1805 put_files_struct(files);
1806 return 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001807 }
Linus Torvalds5e442a42011-11-09 18:16:00 -05001808 spin_unlock(&files->file_lock);
1809 put_files_struct(files);
1810 }
1811 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001812}
1813
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001814static int proc_fd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001815{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001816 return proc_fd_info(dentry->d_inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001817}
1818
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001819static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1820{
Nick Piggin34286d62011-01-07 17:49:57 +11001821 struct inode *inode;
1822 struct task_struct *task;
1823 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001824 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001825 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001826
Nick Piggin34286d62011-01-07 17:49:57 +11001827 if (nd && nd->flags & LOOKUP_RCU)
1828 return -ECHILD;
1829
1830 inode = dentry->d_inode;
1831 task = get_proc_task(inode);
1832 fd = proc_fd(inode);
1833
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001834 if (task) {
1835 files = get_files_struct(task);
1836 if (files) {
Linus Torvalds30a08bf2012-05-18 11:32:15 -07001837 struct file *file;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001838 rcu_read_lock();
Linus Torvalds30a08bf2012-05-18 11:32:15 -07001839 file = fcheck_files(files, fd);
1840 if (file) {
Linus Torvaldscf9ab4c2012-06-04 11:00:45 -07001841 unsigned f_mode = file->f_mode;
Linus Torvalds30a08bf2012-05-18 11:32:15 -07001842
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001843 rcu_read_unlock();
1844 put_files_struct(files);
Linus Torvalds30a08bf2012-05-18 11:32:15 -07001845
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001846 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001847 rcu_read_lock();
1848 cred = __task_cred(task);
1849 inode->i_uid = cred->euid;
1850 inode->i_gid = cred->egid;
1851 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001852 } else {
1853 inode->i_uid = 0;
1854 inode->i_gid = 0;
1855 }
Linus Torvalds30a08bf2012-05-18 11:32:15 -07001856
Linus Torvaldscf9ab4c2012-06-04 11:00:45 -07001857 if (S_ISLNK(inode->i_mode)) {
1858 unsigned i_mode = S_IFLNK;
1859 if (f_mode & FMODE_READ)
1860 i_mode |= S_IRUSR | S_IXUSR;
1861 if (f_mode & FMODE_WRITE)
1862 i_mode |= S_IWUSR | S_IXUSR;
1863 inode->i_mode = i_mode;
1864 }
Linus Torvalds30a08bf2012-05-18 11:32:15 -07001865
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001866 security_task_to_inode(task, inode);
1867 put_task_struct(task);
1868 return 1;
1869 }
1870 rcu_read_unlock();
1871 put_files_struct(files);
1872 }
1873 put_task_struct(task);
1874 }
1875 d_drop(dentry);
1876 return 0;
1877}
1878
Al Virod72f71e2009-02-20 05:58:47 +00001879static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001880{
1881 .d_revalidate = tid_fd_revalidate,
1882 .d_delete = pid_delete_dentry,
1883};
1884
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001885static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001886 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001887{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001888 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001889 struct inode *inode;
1890 struct proc_inode *ei;
1891 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001892
Eric W. Biederman61a28782006-10-02 02:18:49 -07001893 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001894 if (!inode)
1895 goto out;
1896 ei = PROC_I(inode);
1897 ei->fd = fd;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001898
Linus Torvaldscf9ab4c2012-06-04 11:00:45 -07001899 inode->i_mode = S_IFLNK;
Linus Torvalds5e442a42011-11-09 18:16:00 -05001900 inode->i_op = &proc_pid_link_inode_operations;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001901 inode->i_size = 64;
1902 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001903 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001904 d_add(dentry, inode);
1905 /* Close the race of the process dying before we return the dentry */
1906 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001907 error = NULL;
1908
1909 out:
1910 return error;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001911}
1912
Miklos Szeredi27932742007-05-08 00:26:17 -07001913static struct dentry *proc_lookupfd_common(struct inode *dir,
1914 struct dentry *dentry,
1915 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001916{
1917 struct task_struct *task = get_proc_task(dir);
1918 unsigned fd = name_to_int(dentry);
1919 struct dentry *result = ERR_PTR(-ENOENT);
1920
1921 if (!task)
1922 goto out_no_task;
1923 if (fd == ~0U)
1924 goto out;
1925
Miklos Szeredi27932742007-05-08 00:26:17 -07001926 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001927out:
1928 put_task_struct(task);
1929out_no_task:
1930 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001931}
1932
Miklos Szeredi27932742007-05-08 00:26:17 -07001933static int proc_readfd_common(struct file * filp, void * dirent,
1934 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001936 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001937 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001938 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001939 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 struct files_struct * files;
1942
1943 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001944 if (!p)
1945 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 fd = filp->f_pos;
1949 switch (fd) {
1950 case 0:
1951 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
Linus Torvalds5e442a42011-11-09 18:16:00 -05001952 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 filp->f_pos++;
1954 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001955 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Linus Torvalds5e442a42011-11-09 18:16:00 -05001957 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 filp->f_pos++;
1959 default:
1960 files = get_files_struct(p);
1961 if (!files)
Linus Torvalds5e442a42011-11-09 18:16:00 -05001962 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001963 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001965 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001967 char name[PROC_NUMBUF];
1968 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 if (!fcheck_files(files, fd))
1971 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001972 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Miklos Szeredi27932742007-05-08 00:26:17 -07001974 len = snprintf(name, sizeof(name), "%d", fd);
1975 if (proc_fill_cache(filp, dirent, filldir,
1976 name, len, instantiate,
1977 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001978 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 break;
1980 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001981 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001983 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 put_files_struct(files);
1985 }
1986out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001987 put_task_struct(p);
1988out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return retval;
1990}
1991
Miklos Szeredi27932742007-05-08 00:26:17 -07001992static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1993 struct nameidata *nd)
1994{
1995 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1996}
1997
1998static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1999{
2000 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2001}
2002
2003static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2004 size_t len, loff_t *ppos)
2005{
2006 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002007 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002008 if (!err)
2009 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2010 return err;
2011}
2012
2013static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002014 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002015 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002016 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002017};
2018
Arjan van de Ven00977a52007-02-12 00:55:34 -08002019static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 .read = generic_read_dir,
2021 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002022 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023};
2024
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002025#ifdef CONFIG_CHECKPOINT_RESTORE
2026
2027/*
2028 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2029 * which represent vma start and end addresses.
2030 */
2031static int dname_to_vma_addr(struct dentry *dentry,
2032 unsigned long *start, unsigned long *end)
2033{
2034 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2035 return -EINVAL;
2036
2037 return 0;
2038}
2039
2040static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2041{
2042 unsigned long vm_start, vm_end;
2043 bool exact_vma_exists = false;
2044 struct mm_struct *mm = NULL;
2045 struct task_struct *task;
2046 const struct cred *cred;
2047 struct inode *inode;
2048 int status = 0;
2049
2050 if (nd && nd->flags & LOOKUP_RCU)
2051 return -ECHILD;
2052
2053 if (!capable(CAP_SYS_ADMIN)) {
2054 status = -EACCES;
2055 goto out_notask;
2056 }
2057
2058 inode = dentry->d_inode;
2059 task = get_proc_task(inode);
2060 if (!task)
2061 goto out_notask;
2062
2063 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2064 goto out;
2065
2066 mm = get_task_mm(task);
2067 if (!mm)
2068 goto out;
2069
2070 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2071 down_read(&mm->mmap_sem);
2072 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2073 up_read(&mm->mmap_sem);
2074 }
2075
2076 mmput(mm);
2077
2078 if (exact_vma_exists) {
2079 if (task_dumpable(task)) {
2080 rcu_read_lock();
2081 cred = __task_cred(task);
2082 inode->i_uid = cred->euid;
2083 inode->i_gid = cred->egid;
2084 rcu_read_unlock();
2085 } else {
2086 inode->i_uid = 0;
2087 inode->i_gid = 0;
2088 }
2089 security_task_to_inode(task, inode);
2090 status = 1;
2091 }
2092
2093out:
2094 put_task_struct(task);
2095
2096out_notask:
2097 if (status <= 0)
2098 d_drop(dentry);
2099
2100 return status;
2101}
2102
2103static const struct dentry_operations tid_map_files_dentry_operations = {
2104 .d_revalidate = map_files_d_revalidate,
2105 .d_delete = pid_delete_dentry,
2106};
2107
2108static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2109{
2110 unsigned long vm_start, vm_end;
2111 struct vm_area_struct *vma;
2112 struct task_struct *task;
2113 struct mm_struct *mm;
2114 int rc;
2115
2116 rc = -ENOENT;
2117 task = get_proc_task(dentry->d_inode);
2118 if (!task)
2119 goto out;
2120
2121 mm = get_task_mm(task);
2122 put_task_struct(task);
2123 if (!mm)
2124 goto out;
2125
2126 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2127 if (rc)
2128 goto out_mmput;
2129
Artem Fetishevbfe635e2014-03-10 15:49:45 -07002130 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002131 down_read(&mm->mmap_sem);
2132 vma = find_exact_vma(mm, vm_start, vm_end);
2133 if (vma && vma->vm_file) {
2134 *path = vma->vm_file->f_path;
2135 path_get(path);
2136 rc = 0;
2137 }
2138 up_read(&mm->mmap_sem);
2139
2140out_mmput:
2141 mmput(mm);
2142out:
2143 return rc;
2144}
2145
2146struct map_files_info {
2147 struct file *file;
2148 unsigned long len;
2149 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2150};
2151
2152static struct dentry *
2153proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2154 struct task_struct *task, const void *ptr)
2155{
2156 const struct file *file = ptr;
2157 struct proc_inode *ei;
2158 struct inode *inode;
2159
2160 if (!file)
2161 return ERR_PTR(-ENOENT);
2162
2163 inode = proc_pid_make_inode(dir->i_sb, task);
2164 if (!inode)
2165 return ERR_PTR(-ENOENT);
2166
2167 ei = PROC_I(inode);
2168 ei->op.proc_get_link = proc_map_files_get_link;
2169
2170 inode->i_op = &proc_pid_link_inode_operations;
2171 inode->i_size = 64;
2172 inode->i_mode = S_IFLNK;
2173
2174 if (file->f_mode & FMODE_READ)
2175 inode->i_mode |= S_IRUSR;
2176 if (file->f_mode & FMODE_WRITE)
2177 inode->i_mode |= S_IWUSR;
2178
2179 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2180 d_add(dentry, inode);
2181
2182 return NULL;
2183}
2184
2185static struct dentry *proc_map_files_lookup(struct inode *dir,
2186 struct dentry *dentry, struct nameidata *nd)
2187{
2188 unsigned long vm_start, vm_end;
2189 struct vm_area_struct *vma;
2190 struct task_struct *task;
2191 struct dentry *result;
2192 struct mm_struct *mm;
2193
2194 result = ERR_PTR(-EACCES);
2195 if (!capable(CAP_SYS_ADMIN))
2196 goto out;
2197
2198 result = ERR_PTR(-ENOENT);
2199 task = get_proc_task(dir);
2200 if (!task)
2201 goto out;
2202
2203 result = ERR_PTR(-EACCES);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002204 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002205 goto out_put_task;
2206
2207 result = ERR_PTR(-ENOENT);
2208 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002209 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002210
2211 mm = get_task_mm(task);
2212 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002213 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002214
2215 down_read(&mm->mmap_sem);
2216 vma = find_exact_vma(mm, vm_start, vm_end);
2217 if (!vma)
2218 goto out_no_vma;
2219
2220 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2221
2222out_no_vma:
2223 up_read(&mm->mmap_sem);
2224 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002225out_put_task:
2226 put_task_struct(task);
2227out:
2228 return result;
2229}
2230
2231static const struct inode_operations proc_map_files_inode_operations = {
2232 .lookup = proc_map_files_lookup,
2233 .permission = proc_fd_permission,
2234 .setattr = proc_setattr,
2235};
2236
2237static int
2238proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2239{
2240 struct dentry *dentry = filp->f_path.dentry;
2241 struct inode *inode = dentry->d_inode;
2242 struct vm_area_struct *vma;
2243 struct task_struct *task;
2244 struct mm_struct *mm;
2245 ino_t ino;
2246 int ret;
2247
2248 ret = -EACCES;
2249 if (!capable(CAP_SYS_ADMIN))
2250 goto out;
2251
2252 ret = -ENOENT;
2253 task = get_proc_task(inode);
2254 if (!task)
2255 goto out;
2256
2257 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002258 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002259 goto out_put_task;
2260
2261 ret = 0;
2262 switch (filp->f_pos) {
2263 case 0:
2264 ino = inode->i_ino;
2265 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002266 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002267 filp->f_pos++;
2268 case 1:
2269 ino = parent_ino(dentry);
2270 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002271 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002272 filp->f_pos++;
2273 default:
2274 {
2275 unsigned long nr_files, pos, i;
2276 struct flex_array *fa = NULL;
2277 struct map_files_info info;
2278 struct map_files_info *p;
2279
2280 mm = get_task_mm(task);
2281 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002282 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002283 down_read(&mm->mmap_sem);
2284
2285 nr_files = 0;
2286
2287 /*
2288 * We need two passes here:
2289 *
2290 * 1) Collect vmas of mapped files with mmap_sem taken
2291 * 2) Release mmap_sem and instantiate entries
2292 *
2293 * otherwise we get lockdep complained, since filldir()
2294 * routine might require mmap_sem taken in might_fault().
2295 */
2296
2297 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2298 if (vma->vm_file && ++pos > filp->f_pos)
2299 nr_files++;
2300 }
2301
2302 if (nr_files) {
2303 fa = flex_array_alloc(sizeof(info), nr_files,
2304 GFP_KERNEL);
2305 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2306 GFP_KERNEL)) {
2307 ret = -ENOMEM;
2308 if (fa)
2309 flex_array_free(fa);
2310 up_read(&mm->mmap_sem);
2311 mmput(mm);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002312 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002313 }
2314 for (i = 0, vma = mm->mmap, pos = 2; vma;
2315 vma = vma->vm_next) {
2316 if (!vma->vm_file)
2317 continue;
2318 if (++pos <= filp->f_pos)
2319 continue;
2320
2321 get_file(vma->vm_file);
2322 info.file = vma->vm_file;
2323 info.len = snprintf(info.name,
2324 sizeof(info.name), "%lx-%lx",
2325 vma->vm_start, vma->vm_end);
2326 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2327 BUG();
2328 }
2329 }
2330 up_read(&mm->mmap_sem);
2331
2332 for (i = 0; i < nr_files; i++) {
2333 p = flex_array_get(fa, i);
2334 ret = proc_fill_cache(filp, dirent, filldir,
2335 p->name, p->len,
2336 proc_map_files_instantiate,
2337 task, p->file);
2338 if (ret)
2339 break;
2340 filp->f_pos++;
2341 fput(p->file);
2342 }
2343 for (; i < nr_files; i++) {
2344 /*
2345 * In case of error don't forget
2346 * to put rest of file refs.
2347 */
2348 p = flex_array_get(fa, i);
2349 fput(p->file);
2350 }
2351 if (fa)
2352 flex_array_free(fa);
2353 mmput(mm);
2354 }
2355 }
2356
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002357out_put_task:
2358 put_task_struct(task);
2359out:
2360 return ret;
2361}
2362
2363static const struct file_operations proc_map_files_operations = {
2364 .read = generic_read_dir,
2365 .readdir = proc_map_files_readdir,
2366 .llseek = default_llseek,
2367};
2368
2369#endif /* CONFIG_CHECKPOINT_RESTORE */
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002372 * /proc/pid/fd needs a special permission handler so that a process can still
2373 * access /proc/self/fd after it has executed a setuid().
2374 */
Al Viro10556cb2011-06-20 19:28:19 -04002375static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002376{
Al Viro2830ba72011-06-20 19:16:29 -04002377 int rv = generic_permission(inode, mask);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002378 if (rv == 0)
2379 return 0;
2380 if (task_pid(current) == proc_pid(inode))
2381 rv = 0;
2382 return rv;
2383}
2384
2385/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 * proc directories can do almost nothing..
2387 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002388static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002390 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002391 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392};
2393
Miklos Szeredi27932742007-05-08 00:26:17 -07002394static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2395 struct dentry *dentry, struct task_struct *task, const void *ptr)
2396{
2397 unsigned fd = *(unsigned *)ptr;
2398 struct inode *inode;
2399 struct proc_inode *ei;
2400 struct dentry *error = ERR_PTR(-ENOENT);
2401
2402 inode = proc_pid_make_inode(dir->i_sb, task);
2403 if (!inode)
2404 goto out;
2405 ei = PROC_I(inode);
2406 ei->fd = fd;
2407 inode->i_mode = S_IFREG | S_IRUSR;
2408 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002409 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002410 d_add(dentry, inode);
2411 /* Close the race of the process dying before we return the dentry */
2412 if (tid_fd_revalidate(dentry, NULL))
2413 error = NULL;
2414
2415 out:
2416 return error;
2417}
2418
2419static struct dentry *proc_lookupfdinfo(struct inode *dir,
2420 struct dentry *dentry,
2421 struct nameidata *nd)
2422{
2423 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2424}
2425
2426static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2427{
2428 return proc_readfd_common(filp, dirent, filldir,
2429 proc_fdinfo_instantiate);
2430}
2431
2432static const struct file_operations proc_fdinfo_operations = {
2433 .read = generic_read_dir,
2434 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002435 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002436};
2437
2438/*
2439 * proc directories can do almost nothing..
2440 */
2441static const struct inode_operations proc_fdinfo_inode_operations = {
2442 .lookup = proc_lookupfdinfo,
2443 .setattr = proc_setattr,
2444};
2445
2446
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002447static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002448 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002449{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002450 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002451 struct inode *inode;
2452 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002453 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002454
Eric W. Biederman61a28782006-10-02 02:18:49 -07002455 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002456 if (!inode)
2457 goto out;
2458
2459 ei = PROC_I(inode);
2460 inode->i_mode = p->mode;
2461 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002462 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002463 if (p->iop)
2464 inode->i_op = p->iop;
2465 if (p->fop)
2466 inode->i_fop = p->fop;
2467 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002468 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002469 d_add(dentry, inode);
2470 /* Close the race of the process dying before we return the dentry */
2471 if (pid_revalidate(dentry, NULL))
2472 error = NULL;
2473out:
2474 return error;
2475}
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477static struct dentry *proc_pident_lookup(struct inode *dir,
2478 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002479 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002480 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002482 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002483 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002484 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002486 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Eric W. Biederman99f89552006-06-26 00:25:55 -07002488 if (!task)
2489 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002491 /*
2492 * Yes, it does not scale. And it should not. Don't add
2493 * new entries into /proc/<tgid>/ without very good reasons.
2494 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002495 last = &ents[nents - 1];
2496 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (p->len != dentry->d_name.len)
2498 continue;
2499 if (!memcmp(dentry->d_name.name, p->name, p->len))
2500 break;
2501 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002502 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 goto out;
2504
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002505 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002507 put_task_struct(task);
2508out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002509 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Eric Dumazetc5141e62007-05-08 00:26:15 -07002512static int proc_pident_fill_cache(struct file *filp, void *dirent,
2513 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002514{
2515 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2516 proc_pident_instantiate, task, p);
2517}
2518
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002519static int proc_pident_readdir(struct file *filp,
2520 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002521 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002522{
2523 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002524 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002525 struct inode *inode = dentry->d_inode;
2526 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002527 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002528 ino_t ino;
2529 int ret;
2530
2531 ret = -ENOENT;
2532 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002533 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002534
2535 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002536 i = filp->f_pos;
2537 switch (i) {
2538 case 0:
2539 ino = inode->i_ino;
2540 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2541 goto out;
2542 i++;
2543 filp->f_pos++;
2544 /* fall through */
2545 case 1:
2546 ino = parent_ino(dentry);
2547 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2548 goto out;
2549 i++;
2550 filp->f_pos++;
2551 /* fall through */
2552 default:
2553 i -= 2;
2554 if (i >= nents) {
2555 ret = 1;
2556 goto out;
2557 }
2558 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002559 last = &ents[nents - 1];
2560 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002561 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002562 goto out;
2563 filp->f_pos++;
2564 p++;
2565 }
2566 }
2567
2568 ret = 1;
2569out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002570 put_task_struct(task);
2571out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002572 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002576static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2577 size_t count, loff_t *ppos)
2578{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002579 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002580 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002581 ssize_t length;
2582 struct task_struct *task = get_proc_task(inode);
2583
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002584 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002585 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002586
2587 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002588 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002589 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002590 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002591 if (length > 0)
2592 length = simple_read_from_buffer(buf, count, ppos, p, length);
2593 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002594 return length;
2595}
2596
2597static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2598 size_t count, loff_t *ppos)
2599{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002600 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002601 char *page;
2602 ssize_t length;
2603 struct task_struct *task = get_proc_task(inode);
2604
2605 length = -ESRCH;
2606 if (!task)
2607 goto out_no_task;
2608 if (count > PAGE_SIZE)
2609 count = PAGE_SIZE;
2610
2611 /* No partial writes. */
2612 length = -EINVAL;
2613 if (*ppos != 0)
2614 goto out;
2615
2616 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002617 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002618 if (!page)
2619 goto out;
2620
2621 length = -EFAULT;
2622 if (copy_from_user(page, buf, count))
2623 goto out_free;
2624
David Howells107db7c2009-05-08 13:55:27 +01002625 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002626 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002627 if (length < 0)
2628 goto out_free;
2629
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002630 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002631 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002632 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002633 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002634out_free:
2635 free_page((unsigned long) page);
2636out:
2637 put_task_struct(task);
2638out_no_task:
2639 return length;
2640}
2641
Arjan van de Ven00977a52007-02-12 00:55:34 -08002642static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002643 .read = proc_pid_attr_read,
2644 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002645 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002646};
2647
Eric Dumazetc5141e62007-05-08 00:26:15 -07002648static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002649 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2650 REG("prev", S_IRUGO, proc_pid_attr_operations),
2651 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2652 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2653 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2654 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002655};
2656
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002657static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 void * dirent, filldir_t filldir)
2659{
2660 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002661 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
Arjan van de Ven00977a52007-02-12 00:55:34 -08002664static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002666 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002667 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668};
2669
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002670static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 struct dentry *dentry, struct nameidata *nd)
2672{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002673 return proc_pident_lookup(dir, dentry,
2674 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002677static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002678 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002679 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002680 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681};
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683#endif
2684
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002685#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002686static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2687 size_t count, loff_t *ppos)
2688{
2689 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2690 struct mm_struct *mm;
2691 char buffer[PROC_NUMBUF];
2692 size_t len;
2693 int ret;
2694
2695 if (!task)
2696 return -ESRCH;
2697
2698 ret = 0;
2699 mm = get_task_mm(task);
2700 if (mm) {
2701 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2702 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2703 MMF_DUMP_FILTER_SHIFT));
2704 mmput(mm);
2705 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2706 }
2707
2708 put_task_struct(task);
2709
2710 return ret;
2711}
2712
2713static ssize_t proc_coredump_filter_write(struct file *file,
2714 const char __user *buf,
2715 size_t count,
2716 loff_t *ppos)
2717{
2718 struct task_struct *task;
2719 struct mm_struct *mm;
2720 char buffer[PROC_NUMBUF], *end;
2721 unsigned int val;
2722 int ret;
2723 int i;
2724 unsigned long mask;
2725
2726 ret = -EFAULT;
2727 memset(buffer, 0, sizeof(buffer));
2728 if (count > sizeof(buffer) - 1)
2729 count = sizeof(buffer) - 1;
2730 if (copy_from_user(buffer, buf, count))
2731 goto out_no_task;
2732
2733 ret = -EINVAL;
2734 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2735 if (*end == '\n')
2736 end++;
2737 if (end - buffer == 0)
2738 goto out_no_task;
2739
2740 ret = -ESRCH;
2741 task = get_proc_task(file->f_dentry->d_inode);
2742 if (!task)
2743 goto out_no_task;
2744
2745 ret = end - buffer;
2746 mm = get_task_mm(task);
2747 if (!mm)
2748 goto out_no_mm;
2749
2750 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2751 if (val & mask)
2752 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2753 else
2754 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2755 }
2756
2757 mmput(mm);
2758 out_no_mm:
2759 put_task_struct(task);
2760 out_no_task:
2761 return ret;
2762}
2763
2764static const struct file_operations proc_coredump_filter_operations = {
2765 .read = proc_coredump_filter_read,
2766 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002767 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002768};
2769#endif
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771/*
2772 * /proc/self:
2773 */
2774static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2775 int buflen)
2776{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002777 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002778 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002779 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002780 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002781 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002782 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return vfs_readlink(dentry,buffer,buflen,tmp);
2784}
2785
Al Viro008b1502005-08-20 00:17:39 +01002786static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002788 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002789 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002790 char *name = ERR_PTR(-ENOENT);
2791 if (tgid) {
2792 name = __getname();
2793 if (!name)
2794 name = ERR_PTR(-ENOMEM);
2795 else
2796 sprintf(name, "%d", tgid);
2797 }
2798 nd_set_link(nd, name);
2799 return NULL;
2800}
2801
2802static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2803 void *cookie)
2804{
2805 char *s = nd_get_link(nd);
2806 if (!IS_ERR(s))
2807 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002808}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002810static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 .readlink = proc_self_readlink,
2812 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002813 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814};
2815
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002816/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002817 * proc base
2818 *
2819 * These are the directory entries in the root directory of /proc
2820 * that properly belong to the /proc filesystem, as they describe
2821 * describe something that is process related.
2822 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002823static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002824 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002825 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002826};
2827
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002828static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002829 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002830{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002831 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002832 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002833 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002834 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002835
2836 /* Allocate the inode */
2837 error = ERR_PTR(-ENOMEM);
2838 inode = new_inode(dir->i_sb);
2839 if (!inode)
2840 goto out;
2841
2842 /* Initialize the inode */
2843 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002844 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002845 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002846
2847 /*
2848 * grab the reference to the task.
2849 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002850 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002851 if (!ei->pid)
2852 goto out_iput;
2853
Eric W. Biederman801199c2006-10-02 02:18:48 -07002854 inode->i_mode = p->mode;
2855 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002856 set_nlink(inode, 2);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002857 if (S_ISLNK(inode->i_mode))
2858 inode->i_size = 64;
2859 if (p->iop)
2860 inode->i_op = p->iop;
2861 if (p->fop)
2862 inode->i_fop = p->fop;
2863 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002864 d_add(dentry, inode);
2865 error = NULL;
2866out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002867 return error;
2868out_iput:
2869 iput(inode);
2870 goto out;
2871}
2872
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002873static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2874{
2875 struct dentry *error;
2876 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002877 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002878
2879 error = ERR_PTR(-ENOENT);
2880
2881 if (!task)
2882 goto out_no_task;
2883
2884 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002885 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2886 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002887 if (p->len != dentry->d_name.len)
2888 continue;
2889 if (!memcmp(dentry->d_name.name, p->name, p->len))
2890 break;
2891 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002892 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002893 goto out;
2894
2895 error = proc_base_instantiate(dir, dentry, task, p);
2896
2897out:
2898 put_task_struct(task);
2899out_no_task:
2900 return error;
2901}
2902
Eric Dumazetc5141e62007-05-08 00:26:15 -07002903static int proc_base_fill_cache(struct file *filp, void *dirent,
2904 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002905{
2906 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2907 proc_base_instantiate, task, p);
2908}
2909
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002910#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002911static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002912{
Andrea Righi940389b2008-07-28 00:48:12 +02002913 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002914 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002915 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002916
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002917 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2918 if (result)
2919 return result;
2920
2921 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2922 result = -EACCES;
2923 goto out_unlock;
2924 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002925
Andrea Righi59954772008-07-27 17:29:15 +02002926 if (whole && lock_task_sighand(task, &flags)) {
2927 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002928
Andrea Righi59954772008-07-27 17:29:15 +02002929 task_io_accounting_add(&acct, &task->signal->ioac);
2930 while_each_thread(task, t)
2931 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002932
Andrea Righi59954772008-07-27 17:29:15 +02002933 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002934 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002935 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002936 "rchar: %llu\n"
2937 "wchar: %llu\n"
2938 "syscr: %llu\n"
2939 "syscw: %llu\n"
2940 "read_bytes: %llu\n"
2941 "write_bytes: %llu\n"
2942 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002943 (unsigned long long)acct.rchar,
2944 (unsigned long long)acct.wchar,
2945 (unsigned long long)acct.syscr,
2946 (unsigned long long)acct.syscw,
2947 (unsigned long long)acct.read_bytes,
2948 (unsigned long long)acct.write_bytes,
2949 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002950out_unlock:
2951 mutex_unlock(&task->signal->cred_guard_mutex);
2952 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002953}
Andrea Righi297c5d92008-07-25 01:48:49 -07002954
2955static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2956{
2957 return do_io_accounting(task, buffer, 0);
2958}
2959
2960static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2961{
2962 return do_io_accounting(task, buffer, 1);
2963}
2964#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002965
Kees Cook47830722008-10-06 03:11:58 +04002966static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2967 struct pid *pid, struct task_struct *task)
2968{
Al Viroa9712bc2011-03-23 15:52:50 -04002969 int err = lock_trace(task);
2970 if (!err) {
2971 seq_printf(m, "%08x\n", task->personality);
2972 unlock_trace(task);
2973 }
2974 return err;
Kees Cook47830722008-10-06 03:11:58 +04002975}
2976
Eric W. Biederman801199c2006-10-02 02:18:48 -07002977/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002978 * Thread groups
2979 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002980static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002981static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002982
Eric Dumazetc5141e62007-05-08 00:26:15 -07002983static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002984 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2985 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002986#ifdef CONFIG_CHECKPOINT_RESTORE
2987 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2988#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002989 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002990 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002991#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002992 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002993#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002994 REG("environ", S_IRUSR, proc_environ_operations),
2995 INF("auxv", S_IRUSR, proc_pid_auxv),
2996 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002997 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002998 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002999#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003000 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003001#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01003002#ifdef CONFIG_SCHED_AUTOGROUP
3003 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3004#endif
john stultz4614a696b2009-12-14 18:00:05 -08003005 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003006#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003007 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003008#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003009 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3010 ONE("stat", S_IRUGO, proc_tgid_stat),
3011 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003012 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003013#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003014 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003016 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3017 LNK("cwd", proc_cwd_link),
3018 LNK("root", proc_root_link),
3019 LNK("exe", proc_exe_link),
3020 REG("mounts", S_IRUGO, proc_mounts_operations),
3021 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3022 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003023#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003024 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003025 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003026 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003027#endif
3028#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003029 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003030#endif
3031#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003032 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003033#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003034#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003035 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003036#endif
3037#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003038 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003039#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003040#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003041 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003042#endif
Paul Menage8793d852007-10-18 23:39:39 -07003043#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003044 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003045#endif
Paul Menagea4243162007-10-18 23:39:35 -07003046#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003047 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003048#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003049 INF("oom_score", S_IRUGO, proc_oom_score),
Mike Chanf371edd2008-04-24 10:22:26 -07003050 ANDROID("oom_adj",S_IRUGO|S_IWUSR, oom_adjust),
David Rientjesa63d83f2010-08-09 17:19:46 -07003051 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003052#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003053 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3054 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003055#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003056#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003057 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003058#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003059#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003060 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003061#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003062#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003063 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003064#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003065#ifdef CONFIG_HARDWALL
3066 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3067#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003068};
3069
3070static int proc_tgid_base_readdir(struct file * filp,
3071 void * dirent, filldir_t filldir)
3072{
3073 return proc_pident_readdir(filp,dirent,filldir,
3074 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3075}
3076
Arjan van de Ven00977a52007-02-12 00:55:34 -08003077static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003078 .read = generic_read_dir,
3079 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003080 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003081};
3082
3083static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003084 return proc_pident_lookup(dir, dentry,
3085 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003086}
3087
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003088static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003089 .lookup = proc_tgid_base_lookup,
3090 .getattr = pid_getattr,
3091 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003092 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003093};
3094
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003095static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003097 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003098 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003099 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Eric W. Biederman48e64842006-06-26 00:25:48 -07003101 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003102 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3103 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003104 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08003105 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003106 d_drop(dentry);
3107 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Eric W. Biederman48e64842006-06-26 00:25:48 -07003110 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003111 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3112 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003113 if (!leader)
3114 goto out;
3115
3116 name.name = "task";
3117 name.len = strlen(name.name);
3118 dir = d_hash_and_lookup(leader, &name);
3119 if (!dir)
3120 goto out_put_leader;
3121
3122 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003123 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003124 dentry = d_hash_and_lookup(dir, &name);
3125 if (dentry) {
3126 shrink_dcache_parent(dentry);
3127 d_drop(dentry);
3128 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003130
3131 dput(dir);
3132out_put_leader:
3133 dput(leader);
3134out:
3135 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Randy Dunlap0895e912007-10-21 21:00:10 -07003138/**
3139 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3140 * @task: task that should be flushed.
3141 *
3142 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003143 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003144 * in. This call is supposed to do all of this job.
3145 *
3146 * Looks in the dcache for
3147 * /proc/@pid
3148 * /proc/@tgid/task/@pid
3149 * if either directory is present flushes it and all of it'ts children
3150 * from the dcache.
3151 *
3152 * It is safe and reasonable to cache /proc entries for a task until
3153 * that task exits. After that they just clog up the dcache with
3154 * useless entries, possibly causing useful dcache entries to be
3155 * flushed instead. This routine is proved to flush those useless
3156 * dcache entries at process exit time.
3157 *
3158 * NOTE: This routine is just an optimization so it does not guarantee
3159 * that no dcache entries will exist at process exit time it
3160 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003161 */
3162
3163void proc_flush_task(struct task_struct *task)
3164{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003165 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003166 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003167 struct upid *upid;
3168
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003169 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003170 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003171
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003172 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003173 upid = &pid->numbers[i];
3174 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003175 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003176 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07003177
3178 upid = &pid->numbers[pid->level];
3179 if (upid->nr == 1)
3180 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003181}
3182
Adrian Bunk9711ef92006-12-06 20:38:31 -08003183static struct dentry *proc_pid_instantiate(struct inode *dir,
3184 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003185 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003186{
3187 struct dentry *error = ERR_PTR(-ENOENT);
3188 struct inode *inode;
3189
Eric W. Biederman61a28782006-10-02 02:18:49 -07003190 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003191 if (!inode)
3192 goto out;
3193
3194 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3195 inode->i_op = &proc_tgid_base_inode_operations;
3196 inode->i_fop = &proc_tgid_base_operations;
3197 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003198
Miklos Szeredibfe86842011-10-28 14:13:29 +02003199 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3200 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003201
Nick Pigginfb045ad2011-01-07 17:49:55 +11003202 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003203
3204 d_add(dentry, inode);
3205 /* Close the race of the process dying before we return the dentry */
3206 if (pid_revalidate(dentry, NULL))
3207 error = NULL;
3208out:
3209 return error;
3210}
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3213{
Dan Carpenter73d36462010-05-26 14:43:25 -07003214 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003217 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Eric W. Biederman801199c2006-10-02 02:18:48 -07003219 result = proc_base_lookup(dir, dentry);
3220 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3221 goto out;
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 tgid = name_to_int(dentry);
3224 if (tgid == ~0U)
3225 goto out;
3226
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003227 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003228 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003229 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (task)
3231 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003232 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 if (!task)
3234 goto out;
3235
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003236 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003237 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07003239 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003243 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003246struct tgid_iter {
3247 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003248 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003249};
3250static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3251{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003252 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003254 if (iter.task)
3255 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003256 rcu_read_lock();
3257retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003258 iter.task = NULL;
3259 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003260 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003261 iter.tgid = pid_nr_ns(pid, ns);
3262 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003263 /* What we to know is if the pid we have find is the
3264 * pid of a thread_group_leader. Testing for task
3265 * being a thread_group_leader is the obvious thing
3266 * todo but there is a window when it fails, due to
3267 * the pid transfer logic in de_thread.
3268 *
3269 * So we perform the straight forward test of seeing
3270 * if the pid we have found is the pid of a thread
3271 * group leader, and don't worry if the task we have
3272 * found doesn't happen to be a thread group leader.
3273 * As we don't care in the case of readdir.
3274 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003275 if (!iter.task || !has_group_leader_pid(iter.task)) {
3276 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003277 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003278 }
3279 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003281 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003282 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003285#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Eric W. Biederman61a28782006-10-02 02:18:49 -07003287static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003288 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003289{
3290 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003291 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003292 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003293 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003294}
3295
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003296static int fake_filldir(void *buf, const char *name, int namelen,
3297 loff_t offset, u64 ino, unsigned d_type)
3298{
3299 return 0;
3300}
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302/* for the /proc/ directory itself, after non-process stuff has been done */
3303int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3304{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003305 unsigned int nr;
3306 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003307 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003308 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003309 filldir_t __filldir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003311 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3312 goto out_no_task;
3313 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3314
3315 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003316 if (!reaper)
3317 goto out_no_task;
3318
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003319 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003320 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003321 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
3324
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003325 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003326 iter.task = NULL;
3327 iter.tgid = filp->f_pos - TGID_OFFSET;
3328 for (iter = next_tgid(ns, iter);
3329 iter.task;
3330 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003331 if (has_pid_permissions(ns, iter.task, 2))
3332 __filldir = filldir;
3333 else
3334 __filldir = fake_filldir;
3335
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003336 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003337 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003338 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003339 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003342 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3343out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003344 put_task_struct(reaper);
3345out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return 0;
3347}
3348
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003349/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003350 * Tasks
3351 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003352static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003353 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003354 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003355 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003356 REG("environ", S_IRUSR, proc_environ_operations),
3357 INF("auxv", S_IRUSR, proc_pid_auxv),
3358 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003359 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003360 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003361#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003362 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003363#endif
john stultz4614a696b2009-12-14 18:00:05 -08003364 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003365#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003366 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003367#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003368 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3369 ONE("stat", S_IRUGO, proc_tid_stat),
3370 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003371 REG("maps", S_IRUGO, proc_tid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003372#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003373 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003374#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003375 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3376 LNK("cwd", proc_cwd_link),
3377 LNK("root", proc_root_link),
3378 LNK("exe", proc_exe_link),
3379 REG("mounts", S_IRUGO, proc_mounts_operations),
3380 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003381#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003382 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003383 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003384 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003385#endif
3386#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003387 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003388#endif
3389#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003390 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003391#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003392#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003393 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003394#endif
3395#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003396 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003397#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003398#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003399 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003400#endif
Paul Menage8793d852007-10-18 23:39:39 -07003401#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003402 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003403#endif
Paul Menagea4243162007-10-18 23:39:35 -07003404#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003405 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003406#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003407 INF("oom_score", S_IRUGO, proc_oom_score),
3408 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003409 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003410#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003411 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003412 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003413#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003414#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003415 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003416#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003417#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003418 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003419#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003420#ifdef CONFIG_HARDWALL
3421 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3422#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003423};
3424
3425static int proc_tid_base_readdir(struct file * filp,
3426 void * dirent, filldir_t filldir)
3427{
3428 return proc_pident_readdir(filp,dirent,filldir,
3429 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3430}
3431
3432static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003433 return proc_pident_lookup(dir, dentry,
3434 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003435}
3436
Arjan van de Ven00977a52007-02-12 00:55:34 -08003437static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003438 .read = generic_read_dir,
3439 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003440 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003441};
3442
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003443static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003444 .lookup = proc_tid_base_lookup,
3445 .getattr = pid_getattr,
3446 .setattr = proc_setattr,
3447};
3448
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003449static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003450 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003451{
3452 struct dentry *error = ERR_PTR(-ENOENT);
3453 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003454 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003455
3456 if (!inode)
3457 goto out;
3458 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3459 inode->i_op = &proc_tid_base_inode_operations;
3460 inode->i_fop = &proc_tid_base_operations;
3461 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003462
Miklos Szeredibfe86842011-10-28 14:13:29 +02003463 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3464 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003465
Nick Pigginfb045ad2011-01-07 17:49:55 +11003466 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003467
3468 d_add(dentry, inode);
3469 /* Close the race of the process dying before we return the dentry */
3470 if (pid_revalidate(dentry, NULL))
3471 error = NULL;
3472out:
3473 return error;
3474}
3475
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003476static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3477{
3478 struct dentry *result = ERR_PTR(-ENOENT);
3479 struct task_struct *task;
3480 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003481 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003482 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003483
3484 if (!leader)
3485 goto out_no_task;
3486
3487 tid = name_to_int(dentry);
3488 if (tid == ~0U)
3489 goto out;
3490
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003491 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003492 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003493 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003494 if (task)
3495 get_task_struct(task);
3496 rcu_read_unlock();
3497 if (!task)
3498 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003499 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003500 goto out_drop_task;
3501
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003502 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003503out_drop_task:
3504 put_task_struct(task);
3505out:
3506 put_task_struct(leader);
3507out_no_task:
3508 return result;
3509}
3510
3511/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003512 * Find the first tid of a thread group to return to user space.
3513 *
3514 * Usually this is just the thread group leader, but if the users
3515 * buffer was too small or there was a seek into the middle of the
3516 * directory we have more work todo.
3517 *
3518 * In the case of a short read we start with find_task_by_pid.
3519 *
3520 * In the case of a seek we start with the leader and walk nr
3521 * threads past it.
3522 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003523static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003524 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003525{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003526 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003527
Eric W. Biedermancc288732006-06-26 00:26:01 -07003528 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003529 /* Attempt to start with the pid of a thread */
3530 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003531 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003532 if (pos && (pos->group_leader == leader))
3533 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003534 }
3535
3536 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003537 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003538 if (nr && nr >= get_nr_threads(leader))
3539 goto out;
3540
3541 /* If we haven't found our starting place yet start
3542 * with the leader and walk nr threads forward.
3543 */
3544 for (pos = leader; nr > 0; --nr) {
3545 pos = next_thread(pos);
3546 if (pos == leader) {
3547 pos = NULL;
3548 goto out;
3549 }
3550 }
3551found:
3552 get_task_struct(pos);
3553out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003554 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003555 return pos;
3556}
3557
3558/*
3559 * Find the next thread in the thread list.
3560 * Return NULL if there is an error or no next thread.
3561 *
3562 * The reference to the input task_struct is released.
3563 */
3564static struct task_struct *next_tid(struct task_struct *start)
3565{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003566 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003567 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003568 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003569 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003570 if (thread_group_leader(pos))
3571 pos = NULL;
3572 else
3573 get_task_struct(pos);
3574 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003575 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003576 put_task_struct(start);
3577 return pos;
3578}
3579
Eric W. Biederman61a28782006-10-02 02:18:49 -07003580static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3581 struct task_struct *task, int tid)
3582{
3583 char name[PROC_NUMBUF];
3584 int len = snprintf(name, sizeof(name), "%d", tid);
3585 return proc_fill_cache(filp, dirent, filldir, name, len,
3586 proc_task_instantiate, task, NULL);
3587}
3588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589/* for the /proc/TGID/task/ directories */
3590static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3591{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003592 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003594 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003595 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 int retval = -ENOENT;
3597 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003598 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003599 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003601 task = get_proc_task(inode);
3602 if (!task)
3603 goto out_no_task;
3604 rcu_read_lock();
3605 if (pid_alive(task)) {
3606 leader = task->group_leader;
3607 get_task_struct(leader);
3608 }
3609 rcu_read_unlock();
3610 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003611 if (!leader)
3612 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 retval = 0;
3614
Linus Torvaldsee568b22009-03-17 10:02:35 -07003615 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 case 0:
3617 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003618 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003620 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /* fall through */
3622 case 1:
3623 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003624 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003626 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 /* fall through */
3628 }
3629
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003630 /* f_version caches the tgid value that the last readdir call couldn't
3631 * return. lseek aka telldir automagically resets f_version to 0.
3632 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003633 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003634 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003635 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003636 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003637 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003638 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003639 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003640 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003641 /* returning this tgid failed, save it as the first
3642 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003643 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003644 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 }
3648out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003649 put_task_struct(leader);
3650out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 return retval;
3652}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003653
3654static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3655{
3656 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003657 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003658 generic_fillattr(inode, stat);
3659
Eric W. Biederman99f89552006-06-26 00:25:55 -07003660 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003661 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003662 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003663 }
3664
3665 return 0;
3666}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003667
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003668static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003669 .lookup = proc_task_lookup,
3670 .getattr = proc_task_getattr,
3671 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003672 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003673};
3674
Arjan van de Ven00977a52007-02-12 00:55:34 -08003675static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003676 .read = generic_read_dir,
3677 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003678 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003679};