blob: d9512bd03e6ccc8c7a1f6b6d4887429d203aa546 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070076#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/cpuset.h>
78#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050079#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070080#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070081#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070082#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070083#include <linux/pid_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040084#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090085#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080086#include <linux/flex_array.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040087#ifdef CONFIG_HARDWALL
88#include <asm/hardwall.h>
89#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080090#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include "internal.h"
92
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070093/* NOTE:
94 * Implementing inode permission operations in /proc is almost
95 * certainly an error. Permission checks need to happen during
96 * each system call not at open time. The reason is that most of
97 * what we wish to check for permissions in /proc varies at runtime.
98 *
99 * The classic example of a problem is opening file descriptors
100 * in /proc for a task before it execs a suid executable.
101 */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700105 int len;
Al Virod161a132011-07-24 03:36:29 -0400106 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800107 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800108 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700109 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
Eric W. Biederman61a28782006-10-02 02:18:49 -0700112#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700114 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 .mode = MODE, \
116 .iop = IOP, \
117 .fop = FOP, \
118 .op = OP, \
119}
120
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300121#define DIR(NAME, MODE, iops, fops) \
122 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
123#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700124 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700125 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300126 { .proc_get_link = get_link } )
127#define REG(NAME, MODE, fops) \
128 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
129#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700130 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700131 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300132 { .proc_read = read } )
133#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Pavel Emelyanov640708a2012-01-10 15:11:23 -0800138static int proc_fd_permission(struct inode *inode, int mask);
139
Vegard Nossumaed54172008-06-05 22:46:53 -0700140/*
141 * Count the number of hardlinks for the pid_entry table, excluding the .
142 * and .. links.
143 */
144static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145 unsigned int n)
146{
147 unsigned int i;
148 unsigned int count;
149
150 count = 0;
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
153 ++count;
154 }
155
156 return count;
157}
158
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200159static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000161 int result = -ENOENT;
162
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700163 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200164 if (task->fs) {
165 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000166 result = 0;
167 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000169 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170}
171
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800172static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700173{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800174 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176
177 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200178 task_lock(task);
179 if (task->fs) {
180 get_fs_pwd(task->fs, path);
181 result = 0;
182 }
183 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700184 put_task_struct(task);
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return result;
187}
188
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800189static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800191 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700193
194 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200195 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700196 put_task_struct(task);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return result;
199}
200
Linus Torvaldse2683372012-01-17 15:21:19 -0800201static struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
Al Viro831830b2008-01-02 14:09:57 +0000202{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200203 struct mm_struct *mm;
Al Viroec6fd8a2011-02-15 22:22:54 -0500204 int err;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200205
Al Viroec6fd8a2011-02-15 22:22:54 -0500206 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
207 if (err)
208 return ERR_PTR(err);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200209
210 mm = get_task_mm(task);
211 if (mm && mm != current->mm &&
Linus Torvaldse2683372012-01-17 15:21:19 -0800212 !ptrace_may_access(task, mode)) {
Oleg Nesterov704b8362009-07-10 03:27:40 +0200213 mmput(mm);
Al Viroec6fd8a2011-02-15 22:22:54 -0500214 mm = ERR_PTR(-EACCES);
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200215 }
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700216 mutex_unlock(&task->signal->cred_guard_mutex);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200217
Al Viro831830b2008-01-02 14:09:57 +0000218 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000219}
220
Linus Torvaldse2683372012-01-17 15:21:19 -0800221struct mm_struct *mm_for_maps(struct task_struct *task)
222{
223 return mm_access(task, PTRACE_MODE_READ);
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int proc_pid_cmdline(struct task_struct *task, char * buffer)
227{
228 int res = 0;
229 unsigned int len;
230 struct mm_struct *mm = get_task_mm(task);
231 if (!mm)
232 goto out;
233 if (!mm->arg_end)
234 goto out_mm; /* Shh! No looking before we're done */
235
236 len = mm->arg_end - mm->arg_start;
237
238 if (len > PAGE_SIZE)
239 len = PAGE_SIZE;
240
241 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
242
243 // If the nul at the end of args has been overwritten, then
244 // assume application is using setproctitle(3).
245 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
246 len = strnlen(buffer, res);
247 if (len < res) {
248 res = len;
249 } else {
250 len = mm->env_end - mm->env_start;
251 if (len > PAGE_SIZE - res)
252 len = PAGE_SIZE - res;
253 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
254 res = strnlen(buffer, res);
255 }
256 }
257out_mm:
258 mmput(mm);
259out:
260 return res;
261}
262
263static int proc_pid_auxv(struct task_struct *task, char *buffer)
264{
Al Viro2fadaef2011-02-15 22:52:11 -0500265 struct mm_struct *mm = mm_for_maps(task);
266 int res = PTR_ERR(mm);
267 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300269 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300271 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 res = nwords * sizeof(mm->saved_auxv[0]);
273 if (res > PAGE_SIZE)
274 res = PAGE_SIZE;
275 memcpy(buffer, mm->saved_auxv, res);
276 mmput(mm);
277 }
278 return res;
279}
280
281
282#ifdef CONFIG_KALLSYMS
283/*
284 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
285 * Returns the resolved symbol. If that fails, simply return the address.
286 */
287static int proc_pid_wchan(struct task_struct *task, char *buffer)
288{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700289 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700290 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 wchan = get_wchan(task);
293
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700294 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600295 if (!ptrace_may_access(task, PTRACE_MODE_READ))
296 return 0;
297 else
298 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700299 else
300 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302#endif /* CONFIG_KALLSYMS */
303
Al Viroa9712bc2011-03-23 15:52:50 -0400304static int lock_trace(struct task_struct *task)
305{
306 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
307 if (err)
308 return err;
309 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
310 mutex_unlock(&task->signal->cred_guard_mutex);
311 return -EPERM;
312 }
313 return 0;
314}
315
316static void unlock_trace(struct task_struct *task)
317{
318 mutex_unlock(&task->signal->cred_guard_mutex);
319}
320
Ken Chen2ec220e2008-11-10 11:26:08 +0300321#ifdef CONFIG_STACKTRACE
322
323#define MAX_STACK_TRACE_DEPTH 64
324
325static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
326 struct pid *pid, struct task_struct *task)
327{
328 struct stack_trace trace;
329 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400330 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300331 int i;
332
333 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
334 if (!entries)
335 return -ENOMEM;
336
337 trace.nr_entries = 0;
338 trace.max_entries = MAX_STACK_TRACE_DEPTH;
339 trace.entries = entries;
340 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300341
Al Viroa9712bc2011-03-23 15:52:50 -0400342 err = lock_trace(task);
343 if (!err) {
344 save_stack_trace_tsk(task, &trace);
345
346 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700347 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400348 (void *)entries[i], (void *)entries[i]);
349 }
350 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300351 }
352 kfree(entries);
353
Al Viroa9712bc2011-03-23 15:52:50 -0400354 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300355}
356#endif
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#ifdef CONFIG_SCHEDSTATS
359/*
360 * Provides /proc/PID/schedstat
361 */
362static int proc_pid_schedstat(struct task_struct *task, char *buffer)
363{
Balbir Singh172ba842007-07-09 18:52:00 +0200364 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100365 (unsigned long long)task->se.sum_exec_runtime,
366 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200367 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369#endif
370
Arjan van de Ven97455122008-01-25 21:08:34 +0100371#ifdef CONFIG_LATENCYTOP
372static int lstats_show_proc(struct seq_file *m, void *v)
373{
374 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800375 struct inode *inode = m->private;
376 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100377
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800378 if (!task)
379 return -ESRCH;
380 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100381 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800382 struct latency_record *lr = &task->latency_record[i];
383 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100384 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800385 seq_printf(m, "%i %li %li",
386 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100387 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800388 unsigned long bt = lr->backtrace[q];
389 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100390 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800391 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100392 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800393 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100394 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800395 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100396 }
397
398 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800399 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100400 return 0;
401}
402
403static int lstats_open(struct inode *inode, struct file *file)
404{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800405 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800406}
407
Arjan van de Ven97455122008-01-25 21:08:34 +0100408static ssize_t lstats_write(struct file *file, const char __user *buf,
409 size_t count, loff_t *offs)
410{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800411 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100412
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800413 if (!task)
414 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100415 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800416 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100417
418 return count;
419}
420
421static const struct file_operations proc_lstats_operations = {
422 .open = lstats_open,
423 .read = seq_read,
424 .write = lstats_write,
425 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800426 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100427};
428
429#endif
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static int proc_oom_score(struct task_struct *task, char *buffer)
432{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200433 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700435 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200436 if (pid_alive(task))
David Rientjesa63d83f2010-08-09 17:19:46 -0700437 points = oom_badness(task, NULL, NULL,
438 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700439 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return sprintf(buffer, "%lu\n", points);
441}
442
Neil Hormand85f50d2007-10-18 23:40:37 -0700443struct limit_names {
444 char *name;
445 char *unit;
446};
447
448static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700449 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700450 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
451 [RLIMIT_DATA] = {"Max data size", "bytes"},
452 [RLIMIT_STACK] = {"Max stack size", "bytes"},
453 [RLIMIT_CORE] = {"Max core file size", "bytes"},
454 [RLIMIT_RSS] = {"Max resident set", "bytes"},
455 [RLIMIT_NPROC] = {"Max processes", "processes"},
456 [RLIMIT_NOFILE] = {"Max open files", "files"},
457 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
458 [RLIMIT_AS] = {"Max address space", "bytes"},
459 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
460 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
461 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
462 [RLIMIT_NICE] = {"Max nice priority", NULL},
463 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800464 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700465};
466
467/* Display limits for a process */
468static int proc_pid_limits(struct task_struct *task, char *buffer)
469{
470 unsigned int i;
471 int count = 0;
472 unsigned long flags;
473 char *bufptr = buffer;
474
475 struct rlimit rlim[RLIM_NLIMITS];
476
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400477 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700478 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700479 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
480 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700481
482 /*
483 * print the file header
484 */
485 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
486 "Limit", "Soft Limit", "Hard Limit", "Units");
487
488 for (i = 0; i < RLIM_NLIMITS; i++) {
489 if (rlim[i].rlim_cur == RLIM_INFINITY)
490 count += sprintf(&bufptr[count], "%-25s %-20s ",
491 lnames[i].name, "unlimited");
492 else
493 count += sprintf(&bufptr[count], "%-25s %-20lu ",
494 lnames[i].name, rlim[i].rlim_cur);
495
496 if (rlim[i].rlim_max == RLIM_INFINITY)
497 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
498 else
499 count += sprintf(&bufptr[count], "%-20lu ",
500 rlim[i].rlim_max);
501
502 if (lnames[i].unit)
503 count += sprintf(&bufptr[count], "%-10s\n",
504 lnames[i].unit);
505 else
506 count += sprintf(&bufptr[count], "\n");
507 }
508
509 return count;
510}
511
Roland McGrathebcb6732008-07-25 19:46:00 -0700512#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513static int proc_pid_syscall(struct task_struct *task, char *buffer)
514{
515 long nr;
516 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400517 int res = lock_trace(task);
518 if (res)
519 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700520
521 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400522 res = sprintf(buffer, "running\n");
523 else if (nr < 0)
524 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
525 else
526 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700527 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
528 nr,
529 args[0], args[1], args[2], args[3], args[4], args[5],
530 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400531 unlock_trace(task);
532 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700533}
534#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/************************************************************************/
537/* Here the fs part begins */
538/************************************************************************/
539
540/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700541static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700543 struct task_struct *task;
544 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700545 /* Allow access to a task's file descriptors if it is us or we
546 * may use ptrace attach to the process and find out that
547 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700548 */
549 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700550 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400551 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700552 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700553 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700554 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800557int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700558{
559 int error;
560 struct inode *inode = dentry->d_inode;
561
562 if (attr->ia_valid & ATTR_MODE)
563 return -EPERM;
564
565 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200566 if (error)
567 return error;
568
569 if ((attr->ia_valid & ATTR_SIZE) &&
570 attr->ia_size != i_size_read(inode)) {
571 error = vmtruncate(inode, attr->ia_size);
572 if (error)
573 return error;
574 }
575
576 setattr_copy(inode, attr);
577 mark_inode_dirty(inode);
578 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700579}
580
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800581/*
582 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
583 * or euid/egid (for hide_pid_min=2)?
584 */
585static bool has_pid_permissions(struct pid_namespace *pid,
586 struct task_struct *task,
587 int hide_pid_min)
588{
589 if (pid->hide_pid < hide_pid_min)
590 return true;
591 if (in_group_p(pid->pid_gid))
592 return true;
593 return ptrace_may_access(task, PTRACE_MODE_READ);
594}
595
596
597static int proc_pid_permission(struct inode *inode, int mask)
598{
599 struct pid_namespace *pid = inode->i_sb->s_fs_info;
600 struct task_struct *task;
601 bool has_perms;
602
603 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800604 if (!task)
605 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800606 has_perms = has_pid_permissions(pid, task, 1);
607 put_task_struct(task);
608
609 if (!has_perms) {
610 if (pid->hide_pid == 2) {
611 /*
612 * Let's make getdents(), stat(), and open()
613 * consistent with each other. If a process
614 * may not stat() a file, it shouldn't be seen
615 * in procfs at all.
616 */
617 return -ENOENT;
618 }
619
620 return -EPERM;
621 }
622 return generic_permission(inode, mask);
623}
624
625
626
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800627static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700628 .setattr = proc_setattr,
629};
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
632
633static ssize_t proc_info_read(struct file * file, char __user * buf,
634 size_t count, loff_t *ppos)
635{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800636 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 unsigned long page;
638 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700639 struct task_struct *task = get_proc_task(inode);
640
641 length = -ESRCH;
642 if (!task)
643 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (count > PROC_BLOCK_SIZE)
646 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700647
648 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700649 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700650 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 length = PROC_I(inode)->op.proc_read(task, (char*)page);
653
654 if (length >= 0)
655 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
656 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700657out:
658 put_task_struct(task);
659out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return length;
661}
662
Arjan van de Ven00977a52007-02-12 00:55:34 -0800663static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100665 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666};
667
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800668static int proc_single_show(struct seq_file *m, void *v)
669{
670 struct inode *inode = m->private;
671 struct pid_namespace *ns;
672 struct pid *pid;
673 struct task_struct *task;
674 int ret;
675
676 ns = inode->i_sb->s_fs_info;
677 pid = proc_pid(inode);
678 task = get_pid_task(pid, PIDTYPE_PID);
679 if (!task)
680 return -ESRCH;
681
682 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
683
684 put_task_struct(task);
685 return ret;
686}
687
688static int proc_single_open(struct inode *inode, struct file *filp)
689{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800690 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800691}
692
693static const struct file_operations proc_single_file_operations = {
694 .open = proc_single_open,
695 .read = seq_read,
696 .llseek = seq_lseek,
697 .release = single_release,
698};
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static int mem_open(struct inode* inode, struct file* file)
701{
Linus Torvaldse2683372012-01-17 15:21:19 -0800702 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
703 struct mm_struct *mm;
704
705 if (!task)
706 return -ESRCH;
707
708 mm = mm_access(task, PTRACE_MODE_ATTACH);
709 put_task_struct(task);
710
711 if (IS_ERR(mm))
712 return PTR_ERR(mm);
713
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100714 if (mm) {
715 /* ensure this mm_struct can't be freed */
716 atomic_inc(&mm->mm_count);
717 /* but do not pin its memory */
718 mmput(mm);
719 }
720
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700721 /* OK to pass negative loff_t, we can catch out-of-range */
722 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvaldse2683372012-01-17 15:21:19 -0800723 file->private_data = mm;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
727
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100728static ssize_t mem_rw(struct file *file, char __user *buf,
729 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Linus Torvaldse2683372012-01-17 15:21:19 -0800731 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100732 unsigned long addr = *ppos;
733 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvaldse2683372012-01-17 15:21:19 -0800736 if (!mm)
737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Mel Gormane12ba742007-10-16 01:25:52 -0700739 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800741 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700743 copied = 0;
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100744 if (!atomic_inc_not_zero(&mm->mm_users))
745 goto free;
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100748 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100750 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 copied = -EFAULT;
752 break;
753 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100754
755 this_len = access_remote_vm(mm, addr, page, this_len, write);
756 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!copied)
758 copied = -EIO;
759 break;
760 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100761
762 if (!write && copy_to_user(buf, page, this_len)) {
763 copied = -EFAULT;
764 break;
765 }
766
767 buf += this_len;
768 addr += this_len;
769 copied += this_len;
770 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100772 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700773
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100774 mmput(mm);
775free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700776 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return copied;
778}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100780static ssize_t mem_read(struct file *file, char __user *buf,
781 size_t count, loff_t *ppos)
782{
783 return mem_rw(file, buf, count, ppos, 0);
784}
785
786static ssize_t mem_write(struct file *file, const char __user *buf,
787 size_t count, loff_t *ppos)
788{
789 return mem_rw(file, (char __user*)buf, count, ppos, 1);
790}
791
Matt Mackall85863e42008-02-04 22:29:04 -0800792loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 switch (orig) {
795 case 0:
796 file->f_pos = offset;
797 break;
798 case 1:
799 file->f_pos += offset;
800 break;
801 default:
802 return -EINVAL;
803 }
804 force_successful_syscall_return();
805 return file->f_pos;
806}
807
Linus Torvaldse2683372012-01-17 15:21:19 -0800808static int mem_release(struct inode *inode, struct file *file)
809{
810 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100811 if (mm)
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100812 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800813 return 0;
814}
815
Arjan van de Ven00977a52007-02-12 00:55:34 -0800816static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 .llseek = mem_lseek,
818 .read = mem_read,
819 .write = mem_write,
820 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800821 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822};
823
James Pearson315e28c2007-10-16 23:30:17 -0700824static ssize_t environ_read(struct file *file, char __user *buf,
825 size_t count, loff_t *ppos)
826{
827 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
828 char *page;
829 unsigned long src = *ppos;
830 int ret = -ESRCH;
831 struct mm_struct *mm;
832
833 if (!task)
834 goto out_no_task;
835
James Pearson315e28c2007-10-16 23:30:17 -0700836 ret = -ENOMEM;
837 page = (char *)__get_free_page(GFP_TEMPORARY);
838 if (!page)
839 goto out;
840
James Pearson315e28c2007-10-16 23:30:17 -0700841
Al Virod6f64b82011-02-15 22:26:01 -0500842 mm = mm_for_maps(task);
843 ret = PTR_ERR(mm);
844 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700845 goto out_free;
846
Al Virod6f64b82011-02-15 22:26:01 -0500847 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -0700848 while (count > 0) {
849 int this_len, retval, max_len;
850
851 this_len = mm->env_end - (mm->env_start + src);
852
853 if (this_len <= 0)
854 break;
855
856 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
857 this_len = (this_len > max_len) ? max_len : this_len;
858
859 retval = access_process_vm(task, (mm->env_start + src),
860 page, this_len, 0);
861
862 if (retval <= 0) {
863 ret = retval;
864 break;
865 }
866
867 if (copy_to_user(buf, page, retval)) {
868 ret = -EFAULT;
869 break;
870 }
871
872 ret += retval;
873 src += retval;
874 buf += retval;
875 count -= retval;
876 }
877 *ppos = src;
878
879 mmput(mm);
880out_free:
881 free_page((unsigned long) page);
882out:
883 put_task_struct(task);
884out_no_task:
885 return ret;
886}
887
888static const struct file_operations proc_environ_operations = {
889 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100890 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700891};
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static ssize_t oom_adjust_read(struct file *file, char __user *buf,
894 size_t count, loff_t *ppos)
895{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800896 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700897 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700899 int oom_adjust = OOM_DISABLE;
900 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Eric W. Biederman99f89552006-06-26 00:25:55 -0700902 if (!task)
903 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700904
905 if (lock_task_sighand(task, &flags)) {
906 oom_adjust = task->signal->oom_adj;
907 unlock_task_sighand(task, &flags);
908 }
909
Eric W. Biederman99f89552006-06-26 00:25:55 -0700910 put_task_struct(task);
911
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700912 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700913
914 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
918 size_t count, loff_t *ppos)
919{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700920 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700921 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700922 int oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700923 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700924 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700926 memset(buffer, 0, sizeof(buffer));
927 if (count > sizeof(buffer) - 1)
928 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700929 if (copy_from_user(buffer, buf, count)) {
930 err = -EFAULT;
931 goto out;
932 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700933
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700934 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700935 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700936 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700937 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -0700938 oom_adjust != OOM_DISABLE) {
939 err = -EINVAL;
940 goto out;
941 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700942
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800943 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -0700944 if (!task) {
945 err = -ESRCH;
946 goto out;
947 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700948
Ying Han3d5992d2010-10-26 14:21:23 -0700949 task_lock(task);
950 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -0700951 err = -EINVAL;
952 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -0700953 }
954
David Rientjesd19d5472010-10-26 14:21:26 -0700955 if (!lock_task_sighand(task, &flags)) {
956 err = -ESRCH;
957 goto err_task_lock;
958 }
959
960 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
961 err = -EACCES;
962 goto err_sighand;
963 }
964
David Rientjes51b1bd22010-08-09 17:19:47 -0700965 /*
966 * Warn that /proc/pid/oom_adj is deprecated, see
967 * Documentation/feature-removal-schedule.txt.
968 */
Linus Torvaldsc2142702011-08-06 11:43:08 -0700969 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 -0700970 current->comm, task_pid_nr(current), task_pid_nr(task),
971 task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700972 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -0700973 /*
974 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
975 * value is always attainable.
976 */
977 if (task->signal->oom_adj == OOM_ADJUST_MAX)
978 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
979 else
980 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
981 -OOM_DISABLE;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800982 trace_oom_score_adj_update(task);
David Rientjes723548b2010-10-26 14:21:25 -0700983err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700984 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -0700985err_task_lock:
986 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700987 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -0700988out:
989 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Arjan van de Ven00977a52007-02-12 00:55:34 -0800992static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 .read = oom_adjust_read,
994 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100995 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996};
997
David Rientjesa63d83f2010-08-09 17:19:46 -0700998static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
999 size_t count, loff_t *ppos)
1000{
1001 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1002 char buffer[PROC_NUMBUF];
1003 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1004 unsigned long flags;
1005 size_t len;
1006
1007 if (!task)
1008 return -ESRCH;
1009 if (lock_task_sighand(task, &flags)) {
1010 oom_score_adj = task->signal->oom_score_adj;
1011 unlock_task_sighand(task, &flags);
1012 }
1013 put_task_struct(task);
1014 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1015 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1016}
1017
1018static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1019 size_t count, loff_t *ppos)
1020{
1021 struct task_struct *task;
1022 char buffer[PROC_NUMBUF];
1023 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001024 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001025 int err;
1026
1027 memset(buffer, 0, sizeof(buffer));
1028 if (count > sizeof(buffer) - 1)
1029 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001030 if (copy_from_user(buffer, buf, count)) {
1031 err = -EFAULT;
1032 goto out;
1033 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001034
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001035 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001036 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001037 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001038 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001039 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1040 err = -EINVAL;
1041 goto out;
1042 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001043
1044 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001045 if (!task) {
1046 err = -ESRCH;
1047 goto out;
1048 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001049
Ying Han3d5992d2010-10-26 14:21:23 -07001050 task_lock(task);
1051 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001052 err = -EINVAL;
1053 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001054 }
David Rientjesd19d5472010-10-26 14:21:26 -07001055
1056 if (!lock_task_sighand(task, &flags)) {
1057 err = -ESRCH;
1058 goto err_task_lock;
1059 }
1060
Mandeep Singh Bainesdabb16f632011-01-13 15:46:05 -08001061 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001062 !capable(CAP_SYS_RESOURCE)) {
1063 err = -EACCES;
1064 goto err_sighand;
1065 }
1066
David Rientjesa63d83f2010-08-09 17:19:46 -07001067 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f632011-01-13 15:46:05 -08001068 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1069 task->signal->oom_score_adj_min = oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001070 trace_oom_score_adj_update(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001071 /*
1072 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1073 * always attainable.
1074 */
1075 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1076 task->signal->oom_adj = OOM_DISABLE;
1077 else
1078 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1079 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001080err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001081 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001082err_task_lock:
1083 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001084 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001085out:
1086 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001087}
1088
1089static const struct file_operations proc_oom_score_adj_operations = {
1090 .read = oom_score_adj_read,
1091 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001092 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001093};
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095#ifdef CONFIG_AUDITSYSCALL
1096#define TMPBUFLEN 21
1097static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1098 size_t count, loff_t *ppos)
1099{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001100 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001101 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ssize_t length;
1103 char tmpbuf[TMPBUFLEN];
1104
Eric W. Biederman99f89552006-06-26 00:25:55 -07001105 if (!task)
1106 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001108 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001109 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1111}
1112
1113static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1114 size_t count, loff_t *ppos)
1115{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001116 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 char *page, *tmp;
1118 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 uid_t loginuid;
1120
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001121 rcu_read_lock();
1122 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1123 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001125 }
1126 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Al Viroe0182902006-05-18 08:28:02 -04001128 if (count >= PAGE_SIZE)
1129 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (*ppos != 0) {
1132 /* No partial writes. */
1133 return -EINVAL;
1134 }
Mel Gormane12ba742007-10-16 01:25:52 -07001135 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (!page)
1137 return -ENOMEM;
1138 length = -EFAULT;
1139 if (copy_from_user(page, buf, count))
1140 goto out_free_page;
1141
Al Viroe0182902006-05-18 08:28:02 -04001142 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 loginuid = simple_strtoul(page, &tmp, 10);
1144 if (tmp == page) {
1145 length = -EINVAL;
1146 goto out_free_page;
1147
1148 }
Eric Paris0a300be2012-01-03 14:23:08 -05001149 length = audit_set_loginuid(loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (likely(length == 0))
1151 length = count;
1152
1153out_free_page:
1154 free_page((unsigned long) page);
1155 return length;
1156}
1157
Arjan van de Ven00977a52007-02-12 00:55:34 -08001158static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 .read = proc_loginuid_read,
1160 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001161 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162};
Eric Paris1e0bd752008-03-13 08:15:31 -04001163
1164static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1165 size_t count, loff_t *ppos)
1166{
1167 struct inode * inode = file->f_path.dentry->d_inode;
1168 struct task_struct *task = get_proc_task(inode);
1169 ssize_t length;
1170 char tmpbuf[TMPBUFLEN];
1171
1172 if (!task)
1173 return -ESRCH;
1174 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1175 audit_get_sessionid(task));
1176 put_task_struct(task);
1177 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1178}
1179
1180static const struct file_operations proc_sessionid_operations = {
1181 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001182 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001183};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184#endif
1185
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001186#ifdef CONFIG_FAULT_INJECTION
1187static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1188 size_t count, loff_t *ppos)
1189{
1190 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1191 char buffer[PROC_NUMBUF];
1192 size_t len;
1193 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001194
1195 if (!task)
1196 return -ESRCH;
1197 make_it_fail = task->make_it_fail;
1198 put_task_struct(task);
1199
1200 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001201
1202 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001203}
1204
1205static ssize_t proc_fault_inject_write(struct file * file,
1206 const char __user * buf, size_t count, loff_t *ppos)
1207{
1208 struct task_struct *task;
1209 char buffer[PROC_NUMBUF], *end;
1210 int make_it_fail;
1211
1212 if (!capable(CAP_SYS_RESOURCE))
1213 return -EPERM;
1214 memset(buffer, 0, sizeof(buffer));
1215 if (count > sizeof(buffer) - 1)
1216 count = sizeof(buffer) - 1;
1217 if (copy_from_user(buffer, buf, count))
1218 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001219 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1220 if (*end)
1221 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001222 task = get_proc_task(file->f_dentry->d_inode);
1223 if (!task)
1224 return -ESRCH;
1225 task->make_it_fail = make_it_fail;
1226 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001227
1228 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001229}
1230
Arjan van de Ven00977a52007-02-12 00:55:34 -08001231static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001232 .read = proc_fault_inject_read,
1233 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001234 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001235};
1236#endif
1237
Arjan van de Ven97455122008-01-25 21:08:34 +01001238
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001239#ifdef CONFIG_SCHED_DEBUG
1240/*
1241 * Print out various scheduling related per-task fields:
1242 */
1243static int sched_show(struct seq_file *m, void *v)
1244{
1245 struct inode *inode = m->private;
1246 struct task_struct *p;
1247
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001248 p = get_proc_task(inode);
1249 if (!p)
1250 return -ESRCH;
1251 proc_sched_show_task(p, m);
1252
1253 put_task_struct(p);
1254
1255 return 0;
1256}
1257
1258static ssize_t
1259sched_write(struct file *file, const char __user *buf,
1260 size_t count, loff_t *offset)
1261{
1262 struct inode *inode = file->f_path.dentry->d_inode;
1263 struct task_struct *p;
1264
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001265 p = get_proc_task(inode);
1266 if (!p)
1267 return -ESRCH;
1268 proc_sched_set_task(p);
1269
1270 put_task_struct(p);
1271
1272 return count;
1273}
1274
1275static int sched_open(struct inode *inode, struct file *filp)
1276{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001277 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001278}
1279
1280static const struct file_operations proc_pid_sched_operations = {
1281 .open = sched_open,
1282 .read = seq_read,
1283 .write = sched_write,
1284 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001285 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001286};
1287
1288#endif
1289
Mike Galbraith5091faa2010-11-30 14:18:03 +01001290#ifdef CONFIG_SCHED_AUTOGROUP
1291/*
1292 * Print out autogroup related information:
1293 */
1294static int sched_autogroup_show(struct seq_file *m, void *v)
1295{
1296 struct inode *inode = m->private;
1297 struct task_struct *p;
1298
1299 p = get_proc_task(inode);
1300 if (!p)
1301 return -ESRCH;
1302 proc_sched_autogroup_show_task(p, m);
1303
1304 put_task_struct(p);
1305
1306 return 0;
1307}
1308
1309static ssize_t
1310sched_autogroup_write(struct file *file, const char __user *buf,
1311 size_t count, loff_t *offset)
1312{
1313 struct inode *inode = file->f_path.dentry->d_inode;
1314 struct task_struct *p;
1315 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001316 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001317 int err;
1318
1319 memset(buffer, 0, sizeof(buffer));
1320 if (count > sizeof(buffer) - 1)
1321 count = sizeof(buffer) - 1;
1322 if (copy_from_user(buffer, buf, count))
1323 return -EFAULT;
1324
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001325 err = kstrtoint(strstrip(buffer), 0, &nice);
1326 if (err < 0)
1327 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001328
1329 p = get_proc_task(inode);
1330 if (!p)
1331 return -ESRCH;
1332
1333 err = nice;
1334 err = proc_sched_autogroup_set_nice(p, &err);
1335 if (err)
1336 count = err;
1337
1338 put_task_struct(p);
1339
1340 return count;
1341}
1342
1343static int sched_autogroup_open(struct inode *inode, struct file *filp)
1344{
1345 int ret;
1346
1347 ret = single_open(filp, sched_autogroup_show, NULL);
1348 if (!ret) {
1349 struct seq_file *m = filp->private_data;
1350
1351 m->private = inode;
1352 }
1353 return ret;
1354}
1355
1356static const struct file_operations proc_pid_sched_autogroup_operations = {
1357 .open = sched_autogroup_open,
1358 .read = seq_read,
1359 .write = sched_autogroup_write,
1360 .llseek = seq_lseek,
1361 .release = single_release,
1362};
1363
1364#endif /* CONFIG_SCHED_AUTOGROUP */
1365
john stultz4614a696b2009-12-14 18:00:05 -08001366static ssize_t comm_write(struct file *file, const char __user *buf,
1367 size_t count, loff_t *offset)
1368{
1369 struct inode *inode = file->f_path.dentry->d_inode;
1370 struct task_struct *p;
1371 char buffer[TASK_COMM_LEN];
1372
1373 memset(buffer, 0, sizeof(buffer));
1374 if (count > sizeof(buffer) - 1)
1375 count = sizeof(buffer) - 1;
1376 if (copy_from_user(buffer, buf, count))
1377 return -EFAULT;
1378
1379 p = get_proc_task(inode);
1380 if (!p)
1381 return -ESRCH;
1382
1383 if (same_thread_group(current, p))
1384 set_task_comm(p, buffer);
1385 else
1386 count = -EINVAL;
1387
1388 put_task_struct(p);
1389
1390 return count;
1391}
1392
1393static int comm_show(struct seq_file *m, void *v)
1394{
1395 struct inode *inode = m->private;
1396 struct task_struct *p;
1397
1398 p = get_proc_task(inode);
1399 if (!p)
1400 return -ESRCH;
1401
1402 task_lock(p);
1403 seq_printf(m, "%s\n", p->comm);
1404 task_unlock(p);
1405
1406 put_task_struct(p);
1407
1408 return 0;
1409}
1410
1411static int comm_open(struct inode *inode, struct file *filp)
1412{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001413 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001414}
1415
1416static const struct file_operations proc_pid_set_comm_operations = {
1417 .open = comm_open,
1418 .read = seq_read,
1419 .write = comm_write,
1420 .llseek = seq_lseek,
1421 .release = single_release,
1422};
1423
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001424static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001425{
1426 struct task_struct *task;
1427 struct mm_struct *mm;
1428 struct file *exe_file;
1429
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001430 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001431 if (!task)
1432 return -ENOENT;
1433 mm = get_task_mm(task);
1434 put_task_struct(task);
1435 if (!mm)
1436 return -ENOENT;
1437 exe_file = get_mm_exe_file(mm);
1438 mmput(mm);
1439 if (exe_file) {
1440 *exe_path = exe_file->f_path;
1441 path_get(&exe_file->f_path);
1442 fput(exe_file);
1443 return 0;
1444 } else
1445 return -ENOENT;
1446}
1447
Al Viro008b1502005-08-20 00:17:39 +01001448static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct inode *inode = dentry->d_inode;
1451 int error = -EACCES;
1452
1453 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001454 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Eric W. Biederman778c1142006-06-26 00:25:58 -07001456 /* Are we allowed to snoop on the tasks file descriptors? */
1457 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001460 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461out:
Al Viro008b1502005-08-20 00:17:39 +01001462 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001465static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Mel Gormane12ba742007-10-16 01:25:52 -07001467 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001468 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int len;
1470
1471 if (!tmp)
1472 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001473
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001474 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001475 len = PTR_ERR(pathname);
1476 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001478 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 if (len > buflen)
1481 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001482 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 len = -EFAULT;
1484 out:
1485 free_page((unsigned long)tmp);
1486 return len;
1487}
1488
1489static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1490{
1491 int error = -EACCES;
1492 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001493 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Eric W. Biederman778c1142006-06-26 00:25:58 -07001495 /* Are we allowed to snoop on the tasks file descriptors? */
1496 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001499 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (error)
1501 goto out;
1502
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001503 error = do_proc_readlink(&path, buffer, buflen);
1504 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return error;
1507}
1508
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001509static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001511 .follow_link = proc_pid_follow_link,
1512 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513};
1514
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001515
1516/* building an inode */
1517
1518static int task_dumpable(struct task_struct *task)
1519{
1520 int dumpable = 0;
1521 struct mm_struct *mm;
1522
1523 task_lock(task);
1524 mm = task->mm;
1525 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001526 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001527 task_unlock(task);
1528 if(dumpable == 1)
1529 return 1;
1530 return 0;
1531}
1532
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001533struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001534{
1535 struct inode * inode;
1536 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001537 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001538
1539 /* We need a new inode */
1540
1541 inode = new_inode(sb);
1542 if (!inode)
1543 goto out;
1544
1545 /* Common stuff */
1546 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001547 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001548 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001549 inode->i_op = &proc_def_inode_operations;
1550
1551 /*
1552 * grab the reference to task.
1553 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001554 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001555 if (!ei->pid)
1556 goto out_unlock;
1557
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001558 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001559 rcu_read_lock();
1560 cred = __task_cred(task);
1561 inode->i_uid = cred->euid;
1562 inode->i_gid = cred->egid;
1563 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001564 }
1565 security_task_to_inode(task, inode);
1566
1567out:
1568 return inode;
1569
1570out_unlock:
1571 iput(inode);
1572 return NULL;
1573}
1574
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001575int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001576{
1577 struct inode *inode = dentry->d_inode;
1578 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001579 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001580 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001581
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001582 generic_fillattr(inode, stat);
1583
1584 rcu_read_lock();
1585 stat->uid = 0;
1586 stat->gid = 0;
1587 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1588 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001589 if (!has_pid_permissions(pid, task, 2)) {
1590 rcu_read_unlock();
1591 /*
1592 * This doesn't prevent learning whether PID exists,
1593 * it only makes getattr() consistent with readdir().
1594 */
1595 return -ENOENT;
1596 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001597 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1598 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001599 cred = __task_cred(task);
1600 stat->uid = cred->euid;
1601 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001602 }
1603 }
1604 rcu_read_unlock();
1605 return 0;
1606}
1607
1608/* dentry stuff */
1609
1610/*
1611 * Exceptional case: normally we are not allowed to unhash a busy
1612 * directory. In this case, however, we can do it - no aliasing problems
1613 * due to the way we treat inodes.
1614 *
1615 * Rewrite the inode's ownerships here because the owning task may have
1616 * performed a setuid(), etc.
1617 *
1618 * Before the /proc/pid/status file was created the only way to read
1619 * the effective uid of a /process was to stat /proc/pid. Reading
1620 * /proc/pid/status is slow enough that procps and other packages
1621 * kept stating /proc/pid. To keep the rules in /proc simple I have
1622 * made this apply to all per process world readable and executable
1623 * directories.
1624 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001625int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001626{
Nick Piggin34286d62011-01-07 17:49:57 +11001627 struct inode *inode;
1628 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001629 const struct cred *cred;
1630
Nick Piggin34286d62011-01-07 17:49:57 +11001631 if (nd && nd->flags & LOOKUP_RCU)
1632 return -ECHILD;
1633
1634 inode = dentry->d_inode;
1635 task = get_proc_task(inode);
1636
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001637 if (task) {
1638 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1639 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001640 rcu_read_lock();
1641 cred = __task_cred(task);
1642 inode->i_uid = cred->euid;
1643 inode->i_gid = cred->egid;
1644 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001645 } else {
1646 inode->i_uid = 0;
1647 inode->i_gid = 0;
1648 }
1649 inode->i_mode &= ~(S_ISUID | S_ISGID);
1650 security_task_to_inode(task, inode);
1651 put_task_struct(task);
1652 return 1;
1653 }
1654 d_drop(dentry);
1655 return 0;
1656}
1657
Nick Pigginfe15ce42011-01-07 17:49:23 +11001658static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001659{
1660 /* Is the task we represent dead?
1661 * If so, then don't put the dentry on the lru list,
1662 * kill it immediately.
1663 */
1664 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1665}
1666
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001667const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001668{
1669 .d_revalidate = pid_revalidate,
1670 .d_delete = pid_delete_dentry,
1671};
1672
1673/* Lookups */
1674
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001675/*
1676 * Fill a directory entry.
1677 *
1678 * If possible create the dcache entry and derive our inode number and
1679 * file type from dcache entry.
1680 *
1681 * Since all of the proc inode numbers are dynamically generated, the inode
1682 * numbers do not exist until the inode is cache. This means creating the
1683 * the dcache entry in readdir is necessary to keep the inode numbers
1684 * reported by readdir in sync with the inode numbers reported
1685 * by stat.
1686 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001687int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1688 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001689 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001690{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001691 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001692 struct inode *inode;
1693 struct qstr qname;
1694 ino_t ino = 0;
1695 unsigned type = DT_UNKNOWN;
1696
1697 qname.name = name;
1698 qname.len = len;
1699 qname.hash = full_name_hash(name, len);
1700
1701 child = d_lookup(dir, &qname);
1702 if (!child) {
1703 struct dentry *new;
1704 new = d_alloc(dir, &qname);
1705 if (new) {
1706 child = instantiate(dir->d_inode, new, task, ptr);
1707 if (child)
1708 dput(new);
1709 else
1710 child = new;
1711 }
1712 }
1713 if (!child || IS_ERR(child) || !child->d_inode)
1714 goto end_instantiate;
1715 inode = child->d_inode;
1716 if (inode) {
1717 ino = inode->i_ino;
1718 type = inode->i_mode >> 12;
1719 }
1720 dput(child);
1721end_instantiate:
1722 if (!ino)
1723 ino = find_inode_number(dir, &qname);
1724 if (!ino)
1725 ino = 1;
1726 return filldir(dirent, name, len, filp->f_pos, ino, type);
1727}
1728
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001729static unsigned name_to_int(struct dentry *dentry)
1730{
1731 const char *name = dentry->d_name.name;
1732 int len = dentry->d_name.len;
1733 unsigned n = 0;
1734
1735 if (len > 1 && *name == '0')
1736 goto out;
1737 while (len-- > 0) {
1738 unsigned c = *name++ - '0';
1739 if (c > 9)
1740 goto out;
1741 if (n >= (~0U-9)/10)
1742 goto out;
1743 n *= 10;
1744 n += c;
1745 }
1746 return n;
1747out:
1748 return ~0U;
1749}
1750
Miklos Szeredi27932742007-05-08 00:26:17 -07001751#define PROC_FDINFO_MAX 64
1752
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001753static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001754{
Linus Torvalds5e442a492011-11-09 18:16:00 -05001755 struct task_struct *task = get_proc_task(inode);
1756 struct files_struct *files = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001757 struct file *file;
1758 int fd = proc_fd(inode);
1759
Linus Torvalds5e442a492011-11-09 18:16:00 -05001760 if (task) {
1761 files = get_files_struct(task);
1762 put_task_struct(task);
1763 }
1764 if (files) {
1765 /*
1766 * We are not taking a ref to the file structure, so we must
1767 * hold ->file_lock.
1768 */
1769 spin_lock(&files->file_lock);
1770 file = fcheck_files(files, fd);
1771 if (file) {
1772 unsigned int f_flags;
1773 struct fdtable *fdt;
Linus Torvalds1117f722011-08-06 11:51:33 -07001774
Linus Torvalds5e442a492011-11-09 18:16:00 -05001775 fdt = files_fdtable(files);
1776 f_flags = file->f_flags & ~O_CLOEXEC;
1777 if (FD_ISSET(fd, fdt->close_on_exec))
1778 f_flags |= O_CLOEXEC;
Linus Torvalds1117f722011-08-06 11:51:33 -07001779
Linus Torvalds5e442a492011-11-09 18:16:00 -05001780 if (path) {
1781 *path = file->f_path;
1782 path_get(&file->f_path);
1783 }
1784 if (info)
1785 snprintf(info, PROC_FDINFO_MAX,
1786 "pos:\t%lli\n"
1787 "flags:\t0%o\n",
1788 (long long) file->f_pos,
1789 f_flags);
1790 spin_unlock(&files->file_lock);
1791 put_files_struct(files);
1792 return 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001793 }
Linus Torvalds5e442a492011-11-09 18:16:00 -05001794 spin_unlock(&files->file_lock);
1795 put_files_struct(files);
1796 }
1797 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001798}
1799
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001800static int proc_fd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001801{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001802 return proc_fd_info(dentry->d_inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001803}
1804
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001805static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1806{
Nick Piggin34286d62011-01-07 17:49:57 +11001807 struct inode *inode;
1808 struct task_struct *task;
1809 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001810 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001811 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001812
Nick Piggin34286d62011-01-07 17:49:57 +11001813 if (nd && nd->flags & LOOKUP_RCU)
1814 return -ECHILD;
1815
1816 inode = dentry->d_inode;
1817 task = get_proc_task(inode);
1818 fd = proc_fd(inode);
1819
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001820 if (task) {
1821 files = get_files_struct(task);
1822 if (files) {
1823 rcu_read_lock();
1824 if (fcheck_files(files, fd)) {
1825 rcu_read_unlock();
1826 put_files_struct(files);
1827 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001828 rcu_read_lock();
1829 cred = __task_cred(task);
1830 inode->i_uid = cred->euid;
1831 inode->i_gid = cred->egid;
1832 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001833 } else {
1834 inode->i_uid = 0;
1835 inode->i_gid = 0;
1836 }
1837 inode->i_mode &= ~(S_ISUID | S_ISGID);
1838 security_task_to_inode(task, inode);
1839 put_task_struct(task);
1840 return 1;
1841 }
1842 rcu_read_unlock();
1843 put_files_struct(files);
1844 }
1845 put_task_struct(task);
1846 }
1847 d_drop(dentry);
1848 return 0;
1849}
1850
Al Virod72f71e2009-02-20 05:58:47 +00001851static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001852{
1853 .d_revalidate = tid_fd_revalidate,
1854 .d_delete = pid_delete_dentry,
1855};
1856
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001857static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001858 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001859{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001860 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001861 struct file *file;
1862 struct files_struct *files;
1863 struct inode *inode;
1864 struct proc_inode *ei;
1865 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001866
Eric W. Biederman61a28782006-10-02 02:18:49 -07001867 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001868 if (!inode)
1869 goto out;
1870 ei = PROC_I(inode);
1871 ei->fd = fd;
1872 files = get_files_struct(task);
1873 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001874 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001875 inode->i_mode = S_IFLNK;
1876
1877 /*
1878 * We are not taking a ref to the file structure, so we must
1879 * hold ->file_lock.
1880 */
1881 spin_lock(&files->file_lock);
1882 file = fcheck_files(files, fd);
1883 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001884 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001885 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001886 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001887 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001888 inode->i_mode |= S_IWUSR | S_IXUSR;
1889 spin_unlock(&files->file_lock);
1890 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001891
Linus Torvalds5e442a492011-11-09 18:16:00 -05001892 inode->i_op = &proc_pid_link_inode_operations;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001893 inode->i_size = 64;
1894 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001895 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001896 d_add(dentry, inode);
1897 /* Close the race of the process dying before we return the dentry */
1898 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001899 error = NULL;
1900
1901 out:
1902 return error;
1903out_unlock:
1904 spin_unlock(&files->file_lock);
1905 put_files_struct(files);
1906out_iput:
1907 iput(inode);
1908 goto out;
1909}
1910
Miklos Szeredi27932742007-05-08 00:26:17 -07001911static struct dentry *proc_lookupfd_common(struct inode *dir,
1912 struct dentry *dentry,
1913 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001914{
1915 struct task_struct *task = get_proc_task(dir);
1916 unsigned fd = name_to_int(dentry);
1917 struct dentry *result = ERR_PTR(-ENOENT);
1918
1919 if (!task)
1920 goto out_no_task;
1921 if (fd == ~0U)
1922 goto out;
1923
Miklos Szeredi27932742007-05-08 00:26:17 -07001924 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001925out:
1926 put_task_struct(task);
1927out_no_task:
1928 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001929}
1930
Miklos Szeredi27932742007-05-08 00:26:17 -07001931static int proc_readfd_common(struct file * filp, void * dirent,
1932 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001934 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001935 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001936 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001937 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 struct files_struct * files;
1940
1941 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001942 if (!p)
1943 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 fd = filp->f_pos;
1947 switch (fd) {
1948 case 0:
1949 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
Linus Torvalds5e442a492011-11-09 18:16:00 -05001950 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 filp->f_pos++;
1952 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001953 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Linus Torvalds5e442a492011-11-09 18:16:00 -05001955 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 filp->f_pos++;
1957 default:
1958 files = get_files_struct(p);
1959 if (!files)
Linus Torvalds5e442a492011-11-09 18:16:00 -05001960 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001961 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001963 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001965 char name[PROC_NUMBUF];
1966 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 if (!fcheck_files(files, fd))
1969 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001970 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Miklos Szeredi27932742007-05-08 00:26:17 -07001972 len = snprintf(name, sizeof(name), "%d", fd);
1973 if (proc_fill_cache(filp, dirent, filldir,
1974 name, len, instantiate,
1975 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001976 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
1978 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001979 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001981 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 put_files_struct(files);
1983 }
1984out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001985 put_task_struct(p);
1986out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return retval;
1988}
1989
Miklos Szeredi27932742007-05-08 00:26:17 -07001990static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1991 struct nameidata *nd)
1992{
1993 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1994}
1995
1996static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1997{
1998 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1999}
2000
2001static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2002 size_t len, loff_t *ppos)
2003{
2004 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002005 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002006 if (!err)
2007 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2008 return err;
2009}
2010
2011static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002012 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002013 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002014 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002015};
2016
Arjan van de Ven00977a52007-02-12 00:55:34 -08002017static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 .read = generic_read_dir,
2019 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002020 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021};
2022
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002023#ifdef CONFIG_CHECKPOINT_RESTORE
2024
2025/*
2026 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2027 * which represent vma start and end addresses.
2028 */
2029static int dname_to_vma_addr(struct dentry *dentry,
2030 unsigned long *start, unsigned long *end)
2031{
2032 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2033 return -EINVAL;
2034
2035 return 0;
2036}
2037
2038static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2039{
2040 unsigned long vm_start, vm_end;
2041 bool exact_vma_exists = false;
2042 struct mm_struct *mm = NULL;
2043 struct task_struct *task;
2044 const struct cred *cred;
2045 struct inode *inode;
2046 int status = 0;
2047
2048 if (nd && nd->flags & LOOKUP_RCU)
2049 return -ECHILD;
2050
2051 if (!capable(CAP_SYS_ADMIN)) {
2052 status = -EACCES;
2053 goto out_notask;
2054 }
2055
2056 inode = dentry->d_inode;
2057 task = get_proc_task(inode);
2058 if (!task)
2059 goto out_notask;
2060
2061 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2062 goto out;
2063
2064 mm = get_task_mm(task);
2065 if (!mm)
2066 goto out;
2067
2068 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2069 down_read(&mm->mmap_sem);
2070 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2071 up_read(&mm->mmap_sem);
2072 }
2073
2074 mmput(mm);
2075
2076 if (exact_vma_exists) {
2077 if (task_dumpable(task)) {
2078 rcu_read_lock();
2079 cred = __task_cred(task);
2080 inode->i_uid = cred->euid;
2081 inode->i_gid = cred->egid;
2082 rcu_read_unlock();
2083 } else {
2084 inode->i_uid = 0;
2085 inode->i_gid = 0;
2086 }
2087 security_task_to_inode(task, inode);
2088 status = 1;
2089 }
2090
2091out:
2092 put_task_struct(task);
2093
2094out_notask:
2095 if (status <= 0)
2096 d_drop(dentry);
2097
2098 return status;
2099}
2100
2101static const struct dentry_operations tid_map_files_dentry_operations = {
2102 .d_revalidate = map_files_d_revalidate,
2103 .d_delete = pid_delete_dentry,
2104};
2105
2106static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2107{
2108 unsigned long vm_start, vm_end;
2109 struct vm_area_struct *vma;
2110 struct task_struct *task;
2111 struct mm_struct *mm;
2112 int rc;
2113
2114 rc = -ENOENT;
2115 task = get_proc_task(dentry->d_inode);
2116 if (!task)
2117 goto out;
2118
2119 mm = get_task_mm(task);
2120 put_task_struct(task);
2121 if (!mm)
2122 goto out;
2123
2124 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2125 if (rc)
2126 goto out_mmput;
2127
2128 down_read(&mm->mmap_sem);
2129 vma = find_exact_vma(mm, vm_start, vm_end);
2130 if (vma && vma->vm_file) {
2131 *path = vma->vm_file->f_path;
2132 path_get(path);
2133 rc = 0;
2134 }
2135 up_read(&mm->mmap_sem);
2136
2137out_mmput:
2138 mmput(mm);
2139out:
2140 return rc;
2141}
2142
2143struct map_files_info {
2144 struct file *file;
2145 unsigned long len;
2146 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2147};
2148
2149static struct dentry *
2150proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2151 struct task_struct *task, const void *ptr)
2152{
2153 const struct file *file = ptr;
2154 struct proc_inode *ei;
2155 struct inode *inode;
2156
2157 if (!file)
2158 return ERR_PTR(-ENOENT);
2159
2160 inode = proc_pid_make_inode(dir->i_sb, task);
2161 if (!inode)
2162 return ERR_PTR(-ENOENT);
2163
2164 ei = PROC_I(inode);
2165 ei->op.proc_get_link = proc_map_files_get_link;
2166
2167 inode->i_op = &proc_pid_link_inode_operations;
2168 inode->i_size = 64;
2169 inode->i_mode = S_IFLNK;
2170
2171 if (file->f_mode & FMODE_READ)
2172 inode->i_mode |= S_IRUSR;
2173 if (file->f_mode & FMODE_WRITE)
2174 inode->i_mode |= S_IWUSR;
2175
2176 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2177 d_add(dentry, inode);
2178
2179 return NULL;
2180}
2181
2182static struct dentry *proc_map_files_lookup(struct inode *dir,
2183 struct dentry *dentry, struct nameidata *nd)
2184{
2185 unsigned long vm_start, vm_end;
2186 struct vm_area_struct *vma;
2187 struct task_struct *task;
2188 struct dentry *result;
2189 struct mm_struct *mm;
2190
2191 result = ERR_PTR(-EACCES);
2192 if (!capable(CAP_SYS_ADMIN))
2193 goto out;
2194
2195 result = ERR_PTR(-ENOENT);
2196 task = get_proc_task(dir);
2197 if (!task)
2198 goto out;
2199
2200 result = ERR_PTR(-EACCES);
2201 if (lock_trace(task))
2202 goto out_put_task;
2203
2204 result = ERR_PTR(-ENOENT);
2205 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2206 goto out_unlock;
2207
2208 mm = get_task_mm(task);
2209 if (!mm)
2210 goto out_unlock;
2211
2212 down_read(&mm->mmap_sem);
2213 vma = find_exact_vma(mm, vm_start, vm_end);
2214 if (!vma)
2215 goto out_no_vma;
2216
2217 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2218
2219out_no_vma:
2220 up_read(&mm->mmap_sem);
2221 mmput(mm);
2222out_unlock:
2223 unlock_trace(task);
2224out_put_task:
2225 put_task_struct(task);
2226out:
2227 return result;
2228}
2229
2230static const struct inode_operations proc_map_files_inode_operations = {
2231 .lookup = proc_map_files_lookup,
2232 .permission = proc_fd_permission,
2233 .setattr = proc_setattr,
2234};
2235
2236static int
2237proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2238{
2239 struct dentry *dentry = filp->f_path.dentry;
2240 struct inode *inode = dentry->d_inode;
2241 struct vm_area_struct *vma;
2242 struct task_struct *task;
2243 struct mm_struct *mm;
2244 ino_t ino;
2245 int ret;
2246
2247 ret = -EACCES;
2248 if (!capable(CAP_SYS_ADMIN))
2249 goto out;
2250
2251 ret = -ENOENT;
2252 task = get_proc_task(inode);
2253 if (!task)
2254 goto out;
2255
2256 ret = -EACCES;
2257 if (lock_trace(task))
2258 goto out_put_task;
2259
2260 ret = 0;
2261 switch (filp->f_pos) {
2262 case 0:
2263 ino = inode->i_ino;
2264 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2265 goto out_unlock;
2266 filp->f_pos++;
2267 case 1:
2268 ino = parent_ino(dentry);
2269 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2270 goto out_unlock;
2271 filp->f_pos++;
2272 default:
2273 {
2274 unsigned long nr_files, pos, i;
2275 struct flex_array *fa = NULL;
2276 struct map_files_info info;
2277 struct map_files_info *p;
2278
2279 mm = get_task_mm(task);
2280 if (!mm)
2281 goto out_unlock;
2282 down_read(&mm->mmap_sem);
2283
2284 nr_files = 0;
2285
2286 /*
2287 * We need two passes here:
2288 *
2289 * 1) Collect vmas of mapped files with mmap_sem taken
2290 * 2) Release mmap_sem and instantiate entries
2291 *
2292 * otherwise we get lockdep complained, since filldir()
2293 * routine might require mmap_sem taken in might_fault().
2294 */
2295
2296 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2297 if (vma->vm_file && ++pos > filp->f_pos)
2298 nr_files++;
2299 }
2300
2301 if (nr_files) {
2302 fa = flex_array_alloc(sizeof(info), nr_files,
2303 GFP_KERNEL);
2304 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2305 GFP_KERNEL)) {
2306 ret = -ENOMEM;
2307 if (fa)
2308 flex_array_free(fa);
2309 up_read(&mm->mmap_sem);
2310 mmput(mm);
2311 goto out_unlock;
2312 }
2313 for (i = 0, vma = mm->mmap, pos = 2; vma;
2314 vma = vma->vm_next) {
2315 if (!vma->vm_file)
2316 continue;
2317 if (++pos <= filp->f_pos)
2318 continue;
2319
2320 get_file(vma->vm_file);
2321 info.file = vma->vm_file;
2322 info.len = snprintf(info.name,
2323 sizeof(info.name), "%lx-%lx",
2324 vma->vm_start, vma->vm_end);
2325 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2326 BUG();
2327 }
2328 }
2329 up_read(&mm->mmap_sem);
2330
2331 for (i = 0; i < nr_files; i++) {
2332 p = flex_array_get(fa, i);
2333 ret = proc_fill_cache(filp, dirent, filldir,
2334 p->name, p->len,
2335 proc_map_files_instantiate,
2336 task, p->file);
2337 if (ret)
2338 break;
2339 filp->f_pos++;
2340 fput(p->file);
2341 }
2342 for (; i < nr_files; i++) {
2343 /*
2344 * In case of error don't forget
2345 * to put rest of file refs.
2346 */
2347 p = flex_array_get(fa, i);
2348 fput(p->file);
2349 }
2350 if (fa)
2351 flex_array_free(fa);
2352 mmput(mm);
2353 }
2354 }
2355
2356out_unlock:
2357 unlock_trace(task);
2358out_put_task:
2359 put_task_struct(task);
2360out:
2361 return ret;
2362}
2363
2364static const struct file_operations proc_map_files_operations = {
2365 .read = generic_read_dir,
2366 .readdir = proc_map_files_readdir,
2367 .llseek = default_llseek,
2368};
2369
2370#endif /* CONFIG_CHECKPOINT_RESTORE */
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002373 * /proc/pid/fd needs a special permission handler so that a process can still
2374 * access /proc/self/fd after it has executed a setuid().
2375 */
Al Viro10556cb22011-06-20 19:28:19 -04002376static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002377{
Al Viro2830ba72011-06-20 19:16:29 -04002378 int rv = generic_permission(inode, mask);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002379 if (rv == 0)
2380 return 0;
2381 if (task_pid(current) == proc_pid(inode))
2382 rv = 0;
2383 return rv;
2384}
2385
2386/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 * proc directories can do almost nothing..
2388 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002389static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002391 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002392 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393};
2394
Miklos Szeredi27932742007-05-08 00:26:17 -07002395static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2396 struct dentry *dentry, struct task_struct *task, const void *ptr)
2397{
2398 unsigned fd = *(unsigned *)ptr;
2399 struct inode *inode;
2400 struct proc_inode *ei;
2401 struct dentry *error = ERR_PTR(-ENOENT);
2402
2403 inode = proc_pid_make_inode(dir->i_sb, task);
2404 if (!inode)
2405 goto out;
2406 ei = PROC_I(inode);
2407 ei->fd = fd;
2408 inode->i_mode = S_IFREG | S_IRUSR;
2409 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002410 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002411 d_add(dentry, inode);
2412 /* Close the race of the process dying before we return the dentry */
2413 if (tid_fd_revalidate(dentry, NULL))
2414 error = NULL;
2415
2416 out:
2417 return error;
2418}
2419
2420static struct dentry *proc_lookupfdinfo(struct inode *dir,
2421 struct dentry *dentry,
2422 struct nameidata *nd)
2423{
2424 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2425}
2426
2427static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2428{
2429 return proc_readfd_common(filp, dirent, filldir,
2430 proc_fdinfo_instantiate);
2431}
2432
2433static const struct file_operations proc_fdinfo_operations = {
2434 .read = generic_read_dir,
2435 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002436 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002437};
2438
2439/*
2440 * proc directories can do almost nothing..
2441 */
2442static const struct inode_operations proc_fdinfo_inode_operations = {
2443 .lookup = proc_lookupfdinfo,
2444 .setattr = proc_setattr,
2445};
2446
2447
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002448static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002449 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002450{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002451 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002452 struct inode *inode;
2453 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002454 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002455
Eric W. Biederman61a28782006-10-02 02:18:49 -07002456 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002457 if (!inode)
2458 goto out;
2459
2460 ei = PROC_I(inode);
2461 inode->i_mode = p->mode;
2462 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002463 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002464 if (p->iop)
2465 inode->i_op = p->iop;
2466 if (p->fop)
2467 inode->i_fop = p->fop;
2468 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002469 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002470 d_add(dentry, inode);
2471 /* Close the race of the process dying before we return the dentry */
2472 if (pid_revalidate(dentry, NULL))
2473 error = NULL;
2474out:
2475 return error;
2476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478static struct dentry *proc_pident_lookup(struct inode *dir,
2479 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002480 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002481 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002483 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002484 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002485 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002487 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Eric W. Biederman99f89552006-06-26 00:25:55 -07002489 if (!task)
2490 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002492 /*
2493 * Yes, it does not scale. And it should not. Don't add
2494 * new entries into /proc/<tgid>/ without very good reasons.
2495 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002496 last = &ents[nents - 1];
2497 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (p->len != dentry->d_name.len)
2499 continue;
2500 if (!memcmp(dentry->d_name.name, p->name, p->len))
2501 break;
2502 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002503 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 goto out;
2505
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002506 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002508 put_task_struct(task);
2509out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002510 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
Eric Dumazetc5141e62007-05-08 00:26:15 -07002513static int proc_pident_fill_cache(struct file *filp, void *dirent,
2514 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002515{
2516 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2517 proc_pident_instantiate, task, p);
2518}
2519
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002520static int proc_pident_readdir(struct file *filp,
2521 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002522 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523{
2524 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002525 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002526 struct inode *inode = dentry->d_inode;
2527 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002528 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002529 ino_t ino;
2530 int ret;
2531
2532 ret = -ENOENT;
2533 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002534 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002535
2536 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002537 i = filp->f_pos;
2538 switch (i) {
2539 case 0:
2540 ino = inode->i_ino;
2541 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2542 goto out;
2543 i++;
2544 filp->f_pos++;
2545 /* fall through */
2546 case 1:
2547 ino = parent_ino(dentry);
2548 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2549 goto out;
2550 i++;
2551 filp->f_pos++;
2552 /* fall through */
2553 default:
2554 i -= 2;
2555 if (i >= nents) {
2556 ret = 1;
2557 goto out;
2558 }
2559 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002560 last = &ents[nents - 1];
2561 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002562 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002563 goto out;
2564 filp->f_pos++;
2565 p++;
2566 }
2567 }
2568
2569 ret = 1;
2570out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002571 put_task_struct(task);
2572out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002573 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002577static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2578 size_t count, loff_t *ppos)
2579{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002580 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002581 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002582 ssize_t length;
2583 struct task_struct *task = get_proc_task(inode);
2584
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002585 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002586 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002587
2588 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002589 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002590 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002591 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002592 if (length > 0)
2593 length = simple_read_from_buffer(buf, count, ppos, p, length);
2594 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002595 return length;
2596}
2597
2598static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2599 size_t count, loff_t *ppos)
2600{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002601 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002602 char *page;
2603 ssize_t length;
2604 struct task_struct *task = get_proc_task(inode);
2605
2606 length = -ESRCH;
2607 if (!task)
2608 goto out_no_task;
2609 if (count > PAGE_SIZE)
2610 count = PAGE_SIZE;
2611
2612 /* No partial writes. */
2613 length = -EINVAL;
2614 if (*ppos != 0)
2615 goto out;
2616
2617 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002618 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002619 if (!page)
2620 goto out;
2621
2622 length = -EFAULT;
2623 if (copy_from_user(page, buf, count))
2624 goto out_free;
2625
David Howells107db7c2009-05-08 13:55:27 +01002626 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002627 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002628 if (length < 0)
2629 goto out_free;
2630
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002631 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002632 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002633 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002634 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002635out_free:
2636 free_page((unsigned long) page);
2637out:
2638 put_task_struct(task);
2639out_no_task:
2640 return length;
2641}
2642
Arjan van de Ven00977a52007-02-12 00:55:34 -08002643static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002644 .read = proc_pid_attr_read,
2645 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002646 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002647};
2648
Eric Dumazetc5141e62007-05-08 00:26:15 -07002649static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002650 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2651 REG("prev", S_IRUGO, proc_pid_attr_operations),
2652 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2653 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2654 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2655 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002656};
2657
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002658static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 void * dirent, filldir_t filldir)
2660{
2661 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002662 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
Arjan van de Ven00977a52007-02-12 00:55:34 -08002665static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002667 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002668 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669};
2670
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002671static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct dentry *dentry, struct nameidata *nd)
2673{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002674 return proc_pident_lookup(dir, dentry,
2675 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002678static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002679 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002680 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002681 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682};
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684#endif
2685
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002686#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002687static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2688 size_t count, loff_t *ppos)
2689{
2690 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2691 struct mm_struct *mm;
2692 char buffer[PROC_NUMBUF];
2693 size_t len;
2694 int ret;
2695
2696 if (!task)
2697 return -ESRCH;
2698
2699 ret = 0;
2700 mm = get_task_mm(task);
2701 if (mm) {
2702 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2703 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2704 MMF_DUMP_FILTER_SHIFT));
2705 mmput(mm);
2706 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2707 }
2708
2709 put_task_struct(task);
2710
2711 return ret;
2712}
2713
2714static ssize_t proc_coredump_filter_write(struct file *file,
2715 const char __user *buf,
2716 size_t count,
2717 loff_t *ppos)
2718{
2719 struct task_struct *task;
2720 struct mm_struct *mm;
2721 char buffer[PROC_NUMBUF], *end;
2722 unsigned int val;
2723 int ret;
2724 int i;
2725 unsigned long mask;
2726
2727 ret = -EFAULT;
2728 memset(buffer, 0, sizeof(buffer));
2729 if (count > sizeof(buffer) - 1)
2730 count = sizeof(buffer) - 1;
2731 if (copy_from_user(buffer, buf, count))
2732 goto out_no_task;
2733
2734 ret = -EINVAL;
2735 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2736 if (*end == '\n')
2737 end++;
2738 if (end - buffer == 0)
2739 goto out_no_task;
2740
2741 ret = -ESRCH;
2742 task = get_proc_task(file->f_dentry->d_inode);
2743 if (!task)
2744 goto out_no_task;
2745
2746 ret = end - buffer;
2747 mm = get_task_mm(task);
2748 if (!mm)
2749 goto out_no_mm;
2750
2751 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2752 if (val & mask)
2753 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2754 else
2755 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2756 }
2757
2758 mmput(mm);
2759 out_no_mm:
2760 put_task_struct(task);
2761 out_no_task:
2762 return ret;
2763}
2764
2765static const struct file_operations proc_coredump_filter_operations = {
2766 .read = proc_coredump_filter_read,
2767 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002768 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002769};
2770#endif
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772/*
2773 * /proc/self:
2774 */
2775static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2776 int buflen)
2777{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002778 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002779 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002780 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002781 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002782 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002783 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return vfs_readlink(dentry,buffer,buflen,tmp);
2785}
2786
Al Viro008b1502005-08-20 00:17:39 +01002787static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002789 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002790 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002791 char *name = ERR_PTR(-ENOENT);
2792 if (tgid) {
2793 name = __getname();
2794 if (!name)
2795 name = ERR_PTR(-ENOMEM);
2796 else
2797 sprintf(name, "%d", tgid);
2798 }
2799 nd_set_link(nd, name);
2800 return NULL;
2801}
2802
2803static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2804 void *cookie)
2805{
2806 char *s = nd_get_link(nd);
2807 if (!IS_ERR(s))
2808 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002809}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002811static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 .readlink = proc_self_readlink,
2813 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002814 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815};
2816
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002817/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002818 * proc base
2819 *
2820 * These are the directory entries in the root directory of /proc
2821 * that properly belong to the /proc filesystem, as they describe
2822 * describe something that is process related.
2823 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002824static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002825 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002826 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002827};
2828
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002829static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002830 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002831{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002832 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002833 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002834 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002835 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002836
2837 /* Allocate the inode */
2838 error = ERR_PTR(-ENOMEM);
2839 inode = new_inode(dir->i_sb);
2840 if (!inode)
2841 goto out;
2842
2843 /* Initialize the inode */
2844 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002845 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002846 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002847
2848 /*
2849 * grab the reference to the task.
2850 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002851 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002852 if (!ei->pid)
2853 goto out_iput;
2854
Eric W. Biederman801199c2006-10-02 02:18:48 -07002855 inode->i_mode = p->mode;
2856 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002857 set_nlink(inode, 2);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002858 if (S_ISLNK(inode->i_mode))
2859 inode->i_size = 64;
2860 if (p->iop)
2861 inode->i_op = p->iop;
2862 if (p->fop)
2863 inode->i_fop = p->fop;
2864 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002865 d_add(dentry, inode);
2866 error = NULL;
2867out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002868 return error;
2869out_iput:
2870 iput(inode);
2871 goto out;
2872}
2873
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002874static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2875{
2876 struct dentry *error;
2877 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002878 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002879
2880 error = ERR_PTR(-ENOENT);
2881
2882 if (!task)
2883 goto out_no_task;
2884
2885 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002886 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2887 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002888 if (p->len != dentry->d_name.len)
2889 continue;
2890 if (!memcmp(dentry->d_name.name, p->name, p->len))
2891 break;
2892 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002893 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002894 goto out;
2895
2896 error = proc_base_instantiate(dir, dentry, task, p);
2897
2898out:
2899 put_task_struct(task);
2900out_no_task:
2901 return error;
2902}
2903
Eric Dumazetc5141e62007-05-08 00:26:15 -07002904static int proc_base_fill_cache(struct file *filp, void *dirent,
2905 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002906{
2907 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2908 proc_base_instantiate, task, p);
2909}
2910
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002911#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002912static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002913{
Andrea Righi940389b2008-07-28 00:48:12 +02002914 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002915 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002916 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002917
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002918 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2919 if (result)
2920 return result;
2921
2922 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2923 result = -EACCES;
2924 goto out_unlock;
2925 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002926
Andrea Righi59954772008-07-27 17:29:15 +02002927 if (whole && lock_task_sighand(task, &flags)) {
2928 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002929
Andrea Righi59954772008-07-27 17:29:15 +02002930 task_io_accounting_add(&acct, &task->signal->ioac);
2931 while_each_thread(task, t)
2932 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002933
Andrea Righi59954772008-07-27 17:29:15 +02002934 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002935 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002936 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002937 "rchar: %llu\n"
2938 "wchar: %llu\n"
2939 "syscr: %llu\n"
2940 "syscw: %llu\n"
2941 "read_bytes: %llu\n"
2942 "write_bytes: %llu\n"
2943 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002944 (unsigned long long)acct.rchar,
2945 (unsigned long long)acct.wchar,
2946 (unsigned long long)acct.syscr,
2947 (unsigned long long)acct.syscw,
2948 (unsigned long long)acct.read_bytes,
2949 (unsigned long long)acct.write_bytes,
2950 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002951out_unlock:
2952 mutex_unlock(&task->signal->cred_guard_mutex);
2953 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002954}
Andrea Righi297c5d92008-07-25 01:48:49 -07002955
2956static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2957{
2958 return do_io_accounting(task, buffer, 0);
2959}
2960
2961static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2962{
2963 return do_io_accounting(task, buffer, 1);
2964}
2965#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002966
Kees Cook47830722008-10-06 03:11:58 +04002967static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2968 struct pid *pid, struct task_struct *task)
2969{
Al Viroa9712bc2011-03-23 15:52:50 -04002970 int err = lock_trace(task);
2971 if (!err) {
2972 seq_printf(m, "%08x\n", task->personality);
2973 unlock_trace(task);
2974 }
2975 return err;
Kees Cook47830722008-10-06 03:11:58 +04002976}
2977
Eric W. Biederman801199c2006-10-02 02:18:48 -07002978/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002979 * Thread groups
2980 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002981static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002982static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002983
Eric Dumazetc5141e62007-05-08 00:26:15 -07002984static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002985 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2986 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002987#ifdef CONFIG_CHECKPOINT_RESTORE
2988 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2989#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002990 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002991 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002992#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002993 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002994#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002995 REG("environ", S_IRUSR, proc_environ_operations),
2996 INF("auxv", S_IRUSR, proc_pid_auxv),
2997 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002998 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002999 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003000#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003001 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003002#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01003003#ifdef CONFIG_SCHED_AUTOGROUP
3004 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3005#endif
john stultz4614a696b2009-12-14 18:00:05 -08003006 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003007#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003008 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003009#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003010 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3011 ONE("stat", S_IRUGO, proc_tgid_stat),
3012 ONE("statm", S_IRUGO, proc_pid_statm),
3013 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003014#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003015 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003016#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003017 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3018 LNK("cwd", proc_cwd_link),
3019 LNK("root", proc_root_link),
3020 LNK("exe", proc_exe_link),
3021 REG("mounts", S_IRUGO, proc_mounts_operations),
3022 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3023 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003024#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003025 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3026 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003027 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003028#endif
3029#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003030 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003031#endif
3032#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003033 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003034#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003035#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003036 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003037#endif
3038#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003039 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003040#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003041#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003042 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003043#endif
Paul Menage8793d852007-10-18 23:39:39 -07003044#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003045 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003046#endif
Paul Menagea4243162007-10-18 23:39:35 -07003047#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003048 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003049#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003050 INF("oom_score", S_IRUGO, proc_oom_score),
3051 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003052 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003053#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003054 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3055 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003056#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003057#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003058 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003059#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003060#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003061 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003062#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003063#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003064 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003065#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003066#ifdef CONFIG_HARDWALL
3067 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3068#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003069};
3070
3071static int proc_tgid_base_readdir(struct file * filp,
3072 void * dirent, filldir_t filldir)
3073{
3074 return proc_pident_readdir(filp,dirent,filldir,
3075 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3076}
3077
Arjan van de Ven00977a52007-02-12 00:55:34 -08003078static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003079 .read = generic_read_dir,
3080 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003081 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003082};
3083
3084static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003085 return proc_pident_lookup(dir, dentry,
3086 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003087}
3088
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003089static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003090 .lookup = proc_tgid_base_lookup,
3091 .getattr = pid_getattr,
3092 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003093 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003094};
3095
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003096static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003098 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003099 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003100 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Eric W. Biederman48e64842006-06-26 00:25:48 -07003102 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003103 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3104 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003105 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08003106 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003107 d_drop(dentry);
3108 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Eric W. Biederman48e64842006-06-26 00:25:48 -07003111 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003112 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3113 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003114 if (!leader)
3115 goto out;
3116
3117 name.name = "task";
3118 name.len = strlen(name.name);
3119 dir = d_hash_and_lookup(leader, &name);
3120 if (!dir)
3121 goto out_put_leader;
3122
3123 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003124 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003125 dentry = d_hash_and_lookup(dir, &name);
3126 if (dentry) {
3127 shrink_dcache_parent(dentry);
3128 d_drop(dentry);
3129 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003131
3132 dput(dir);
3133out_put_leader:
3134 dput(leader);
3135out:
3136 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
Randy Dunlap0895e912007-10-21 21:00:10 -07003139/**
3140 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3141 * @task: task that should be flushed.
3142 *
3143 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003144 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003145 * in. This call is supposed to do all of this job.
3146 *
3147 * Looks in the dcache for
3148 * /proc/@pid
3149 * /proc/@tgid/task/@pid
3150 * if either directory is present flushes it and all of it'ts children
3151 * from the dcache.
3152 *
3153 * It is safe and reasonable to cache /proc entries for a task until
3154 * that task exits. After that they just clog up the dcache with
3155 * useless entries, possibly causing useful dcache entries to be
3156 * flushed instead. This routine is proved to flush those useless
3157 * dcache entries at process exit time.
3158 *
3159 * NOTE: This routine is just an optimization so it does not guarantee
3160 * that no dcache entries will exist at process exit time it
3161 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003162 */
3163
3164void proc_flush_task(struct task_struct *task)
3165{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003166 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003167 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003168 struct upid *upid;
3169
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003170 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003171 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003172
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003173 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003174 upid = &pid->numbers[i];
3175 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003176 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003177 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07003178
3179 upid = &pid->numbers[pid->level];
3180 if (upid->nr == 1)
3181 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003182}
3183
Adrian Bunk9711ef92006-12-06 20:38:31 -08003184static struct dentry *proc_pid_instantiate(struct inode *dir,
3185 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003186 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003187{
3188 struct dentry *error = ERR_PTR(-ENOENT);
3189 struct inode *inode;
3190
Eric W. Biederman61a28782006-10-02 02:18:49 -07003191 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003192 if (!inode)
3193 goto out;
3194
3195 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3196 inode->i_op = &proc_tgid_base_inode_operations;
3197 inode->i_fop = &proc_tgid_base_operations;
3198 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003199
Miklos Szeredibfe86842011-10-28 14:13:29 +02003200 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3201 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003202
Nick Pigginfb045ad2011-01-07 17:49:55 +11003203 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003204
3205 d_add(dentry, inode);
3206 /* Close the race of the process dying before we return the dentry */
3207 if (pid_revalidate(dentry, NULL))
3208 error = NULL;
3209out:
3210 return error;
3211}
3212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3214{
Dan Carpenter73d36462010-05-26 14:43:25 -07003215 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003218 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Eric W. Biederman801199c2006-10-02 02:18:48 -07003220 result = proc_base_lookup(dir, dentry);
3221 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3222 goto out;
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 tgid = name_to_int(dentry);
3225 if (tgid == ~0U)
3226 goto out;
3227
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003228 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003229 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003230 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 if (task)
3232 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003233 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (!task)
3235 goto out;
3236
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003237 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003238 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07003240 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003244 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003245 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003247struct tgid_iter {
3248 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003249 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003250};
3251static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3252{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003253 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003255 if (iter.task)
3256 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003257 rcu_read_lock();
3258retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003259 iter.task = NULL;
3260 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003261 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003262 iter.tgid = pid_nr_ns(pid, ns);
3263 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003264 /* What we to know is if the pid we have find is the
3265 * pid of a thread_group_leader. Testing for task
3266 * being a thread_group_leader is the obvious thing
3267 * todo but there is a window when it fails, due to
3268 * the pid transfer logic in de_thread.
3269 *
3270 * So we perform the straight forward test of seeing
3271 * if the pid we have found is the pid of a thread
3272 * group leader, and don't worry if the task we have
3273 * found doesn't happen to be a thread group leader.
3274 * As we don't care in the case of readdir.
3275 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003276 if (!iter.task || !has_group_leader_pid(iter.task)) {
3277 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003278 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003279 }
3280 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003282 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003283 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003286#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Eric W. Biederman61a28782006-10-02 02:18:49 -07003288static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003289 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003290{
3291 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003292 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003293 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003294 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003295}
3296
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003297static int fake_filldir(void *buf, const char *name, int namelen,
3298 loff_t offset, u64 ino, unsigned d_type)
3299{
3300 return 0;
3301}
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303/* for the /proc/ directory itself, after non-process stuff has been done */
3304int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3305{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003306 unsigned int nr;
3307 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003308 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003309 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003310 filldir_t __filldir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003312 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3313 goto out_no_task;
3314 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3315
3316 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003317 if (!reaper)
3318 goto out_no_task;
3319
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003320 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003321 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003322 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003323 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003326 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003327 iter.task = NULL;
3328 iter.tgid = filp->f_pos - TGID_OFFSET;
3329 for (iter = next_tgid(ns, iter);
3330 iter.task;
3331 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003332 if (has_pid_permissions(ns, iter.task, 2))
3333 __filldir = filldir;
3334 else
3335 __filldir = fake_filldir;
3336
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003337 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003338 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003339 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003340 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003343 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3344out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003345 put_task_struct(reaper);
3346out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 return 0;
3348}
3349
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003350/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003351 * Tasks
3352 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003353static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003354 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003355 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003356 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003357 REG("environ", S_IRUSR, proc_environ_operations),
3358 INF("auxv", S_IRUSR, proc_pid_auxv),
3359 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003360 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003361 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003362#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003363 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003364#endif
john stultz4614a696b2009-12-14 18:00:05 -08003365 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003366#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003367 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003368#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003369 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3370 ONE("stat", S_IRUGO, proc_tid_stat),
3371 ONE("statm", S_IRUGO, proc_pid_statm),
3372 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003373#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003374 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003375#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003376 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3377 LNK("cwd", proc_cwd_link),
3378 LNK("root", proc_root_link),
3379 LNK("exe", proc_exe_link),
3380 REG("mounts", S_IRUGO, proc_mounts_operations),
3381 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003382#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003383 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3384 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003385 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003386#endif
3387#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003388 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003389#endif
3390#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003391 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003392#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003393#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003394 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003395#endif
3396#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003397 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003398#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003399#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003400 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003401#endif
Paul Menage8793d852007-10-18 23:39:39 -07003402#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003403 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003404#endif
Paul Menagea4243162007-10-18 23:39:35 -07003405#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003406 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003407#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003408 INF("oom_score", S_IRUGO, proc_oom_score),
3409 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003410 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003411#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003412 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003413 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003414#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003415#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003416 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003417#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003418#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003419 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003420#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003421#ifdef CONFIG_HARDWALL
3422 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3423#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003424};
3425
3426static int proc_tid_base_readdir(struct file * filp,
3427 void * dirent, filldir_t filldir)
3428{
3429 return proc_pident_readdir(filp,dirent,filldir,
3430 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3431}
3432
3433static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003434 return proc_pident_lookup(dir, dentry,
3435 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003436}
3437
Arjan van de Ven00977a52007-02-12 00:55:34 -08003438static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003439 .read = generic_read_dir,
3440 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003441 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003442};
3443
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003444static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003445 .lookup = proc_tid_base_lookup,
3446 .getattr = pid_getattr,
3447 .setattr = proc_setattr,
3448};
3449
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003450static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003451 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003452{
3453 struct dentry *error = ERR_PTR(-ENOENT);
3454 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003455 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003456
3457 if (!inode)
3458 goto out;
3459 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3460 inode->i_op = &proc_tid_base_inode_operations;
3461 inode->i_fop = &proc_tid_base_operations;
3462 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003463
Miklos Szeredibfe86842011-10-28 14:13:29 +02003464 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3465 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003466
Nick Pigginfb045ad2011-01-07 17:49:55 +11003467 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003468
3469 d_add(dentry, inode);
3470 /* Close the race of the process dying before we return the dentry */
3471 if (pid_revalidate(dentry, NULL))
3472 error = NULL;
3473out:
3474 return error;
3475}
3476
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003477static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3478{
3479 struct dentry *result = ERR_PTR(-ENOENT);
3480 struct task_struct *task;
3481 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003482 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003483 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003484
3485 if (!leader)
3486 goto out_no_task;
3487
3488 tid = name_to_int(dentry);
3489 if (tid == ~0U)
3490 goto out;
3491
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003492 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003493 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003494 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003495 if (task)
3496 get_task_struct(task);
3497 rcu_read_unlock();
3498 if (!task)
3499 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003500 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003501 goto out_drop_task;
3502
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003503 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003504out_drop_task:
3505 put_task_struct(task);
3506out:
3507 put_task_struct(leader);
3508out_no_task:
3509 return result;
3510}
3511
3512/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003513 * Find the first tid of a thread group to return to user space.
3514 *
3515 * Usually this is just the thread group leader, but if the users
3516 * buffer was too small or there was a seek into the middle of the
3517 * directory we have more work todo.
3518 *
3519 * In the case of a short read we start with find_task_by_pid.
3520 *
3521 * In the case of a seek we start with the leader and walk nr
3522 * threads past it.
3523 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003524static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003525 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003526{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003527 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003528
Eric W. Biedermancc288732006-06-26 00:26:01 -07003529 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003530 /* Attempt to start with the pid of a thread */
3531 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003532 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003533 if (pos && (pos->group_leader == leader))
3534 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003535 }
3536
3537 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003538 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003539 if (nr && nr >= get_nr_threads(leader))
3540 goto out;
3541
3542 /* If we haven't found our starting place yet start
3543 * with the leader and walk nr threads forward.
3544 */
3545 for (pos = leader; nr > 0; --nr) {
3546 pos = next_thread(pos);
3547 if (pos == leader) {
3548 pos = NULL;
3549 goto out;
3550 }
3551 }
3552found:
3553 get_task_struct(pos);
3554out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003555 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003556 return pos;
3557}
3558
3559/*
3560 * Find the next thread in the thread list.
3561 * Return NULL if there is an error or no next thread.
3562 *
3563 * The reference to the input task_struct is released.
3564 */
3565static struct task_struct *next_tid(struct task_struct *start)
3566{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003567 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003568 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003569 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003570 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003571 if (thread_group_leader(pos))
3572 pos = NULL;
3573 else
3574 get_task_struct(pos);
3575 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003576 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003577 put_task_struct(start);
3578 return pos;
3579}
3580
Eric W. Biederman61a28782006-10-02 02:18:49 -07003581static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3582 struct task_struct *task, int tid)
3583{
3584 char name[PROC_NUMBUF];
3585 int len = snprintf(name, sizeof(name), "%d", tid);
3586 return proc_fill_cache(filp, dirent, filldir, name, len,
3587 proc_task_instantiate, task, NULL);
3588}
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590/* for the /proc/TGID/task/ directories */
3591static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3592{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003593 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003595 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003596 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 int retval = -ENOENT;
3598 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003599 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003600 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003602 task = get_proc_task(inode);
3603 if (!task)
3604 goto out_no_task;
3605 rcu_read_lock();
3606 if (pid_alive(task)) {
3607 leader = task->group_leader;
3608 get_task_struct(leader);
3609 }
3610 rcu_read_unlock();
3611 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003612 if (!leader)
3613 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 retval = 0;
3615
Linus Torvaldsee568b22009-03-17 10:02:35 -07003616 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 case 0:
3618 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003619 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003621 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 /* fall through */
3623 case 1:
3624 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003625 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003627 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 /* fall through */
3629 }
3630
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003631 /* f_version caches the tgid value that the last readdir call couldn't
3632 * return. lseek aka telldir automagically resets f_version to 0.
3633 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003634 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003635 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003636 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003637 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003638 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003639 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003640 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003641 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003642 /* returning this tgid failed, save it as the first
3643 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003644 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003645 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
3649out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003650 put_task_struct(leader);
3651out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return retval;
3653}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003654
3655static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3656{
3657 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003658 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003659 generic_fillattr(inode, stat);
3660
Eric W. Biederman99f89552006-06-26 00:25:55 -07003661 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003662 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003663 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003664 }
3665
3666 return 0;
3667}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003668
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003669static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003670 .lookup = proc_task_lookup,
3671 .getattr = proc_task_getattr,
3672 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003673 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003674};
3675
Arjan van de Ven00977a52007-02-12 00:55:34 -08003676static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003677 .read = generic_read_dir,
3678 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003679 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003680};