blob: be1909041685f0c93b109ef2be8af9560217966f [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
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700714 /* OK to pass negative loff_t, we can catch out-of-range */
715 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvaldse2683372012-01-17 15:21:19 -0800716 file->private_data = mm;
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719}
720
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100721static ssize_t mem_rw(struct file *file, char __user *buf,
722 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Linus Torvaldse2683372012-01-17 15:21:19 -0800724 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100725 unsigned long addr = *ppos;
726 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Linus Torvaldse2683372012-01-17 15:21:19 -0800729 if (!mm)
730 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Mel Gormane12ba742007-10-16 01:25:52 -0700732 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800734 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700736 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100738 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100740 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 copied = -EFAULT;
742 break;
743 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100744
745 this_len = access_remote_vm(mm, addr, page, this_len, write);
746 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!copied)
748 copied = -EIO;
749 break;
750 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100751
752 if (!write && copy_to_user(buf, page, this_len)) {
753 copied = -EFAULT;
754 break;
755 }
756
757 buf += this_len;
758 addr += this_len;
759 copied += this_len;
760 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100762 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700763
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700764 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return copied;
766}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100768static ssize_t mem_read(struct file *file, char __user *buf,
769 size_t count, loff_t *ppos)
770{
771 return mem_rw(file, buf, count, ppos, 0);
772}
773
774static ssize_t mem_write(struct file *file, const char __user *buf,
775 size_t count, loff_t *ppos)
776{
777 return mem_rw(file, (char __user*)buf, count, ppos, 1);
778}
779
Matt Mackall85863e42008-02-04 22:29:04 -0800780loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 switch (orig) {
783 case 0:
784 file->f_pos = offset;
785 break;
786 case 1:
787 file->f_pos += offset;
788 break;
789 default:
790 return -EINVAL;
791 }
792 force_successful_syscall_return();
793 return file->f_pos;
794}
795
Linus Torvaldse2683372012-01-17 15:21:19 -0800796static int mem_release(struct inode *inode, struct file *file)
797{
798 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100799 if (mm)
800 mmput(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800801 return 0;
802}
803
Arjan van de Ven00977a52007-02-12 00:55:34 -0800804static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 .llseek = mem_lseek,
806 .read = mem_read,
807 .write = mem_write,
808 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800809 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810};
811
James Pearson315e28c2007-10-16 23:30:17 -0700812static ssize_t environ_read(struct file *file, char __user *buf,
813 size_t count, loff_t *ppos)
814{
815 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
816 char *page;
817 unsigned long src = *ppos;
818 int ret = -ESRCH;
819 struct mm_struct *mm;
820
821 if (!task)
822 goto out_no_task;
823
James Pearson315e28c2007-10-16 23:30:17 -0700824 ret = -ENOMEM;
825 page = (char *)__get_free_page(GFP_TEMPORARY);
826 if (!page)
827 goto out;
828
James Pearson315e28c2007-10-16 23:30:17 -0700829
Al Virod6f64b82011-02-15 22:26:01 -0500830 mm = mm_for_maps(task);
831 ret = PTR_ERR(mm);
832 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700833 goto out_free;
834
Al Virod6f64b82011-02-15 22:26:01 -0500835 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -0700836 while (count > 0) {
837 int this_len, retval, max_len;
838
839 this_len = mm->env_end - (mm->env_start + src);
840
841 if (this_len <= 0)
842 break;
843
844 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
845 this_len = (this_len > max_len) ? max_len : this_len;
846
847 retval = access_process_vm(task, (mm->env_start + src),
848 page, this_len, 0);
849
850 if (retval <= 0) {
851 ret = retval;
852 break;
853 }
854
855 if (copy_to_user(buf, page, retval)) {
856 ret = -EFAULT;
857 break;
858 }
859
860 ret += retval;
861 src += retval;
862 buf += retval;
863 count -= retval;
864 }
865 *ppos = src;
866
867 mmput(mm);
868out_free:
869 free_page((unsigned long) page);
870out:
871 put_task_struct(task);
872out_no_task:
873 return ret;
874}
875
876static const struct file_operations proc_environ_operations = {
877 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100878 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700879};
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881static ssize_t oom_adjust_read(struct file *file, char __user *buf,
882 size_t count, loff_t *ppos)
883{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800884 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700885 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700887 int oom_adjust = OOM_DISABLE;
888 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Eric W. Biederman99f89552006-06-26 00:25:55 -0700890 if (!task)
891 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700892
893 if (lock_task_sighand(task, &flags)) {
894 oom_adjust = task->signal->oom_adj;
895 unlock_task_sighand(task, &flags);
896 }
897
Eric W. Biederman99f89552006-06-26 00:25:55 -0700898 put_task_struct(task);
899
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700900 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700901
902 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
906 size_t count, loff_t *ppos)
907{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700908 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700909 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700910 int oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700911 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700912 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700914 memset(buffer, 0, sizeof(buffer));
915 if (count > sizeof(buffer) - 1)
916 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700917 if (copy_from_user(buffer, buf, count)) {
918 err = -EFAULT;
919 goto out;
920 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700921
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700922 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700923 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700924 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700925 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -0700926 oom_adjust != OOM_DISABLE) {
927 err = -EINVAL;
928 goto out;
929 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700930
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800931 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -0700932 if (!task) {
933 err = -ESRCH;
934 goto out;
935 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700936
Ying Han3d5992d2010-10-26 14:21:23 -0700937 task_lock(task);
938 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -0700939 err = -EINVAL;
940 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -0700941 }
942
David Rientjesd19d5472010-10-26 14:21:26 -0700943 if (!lock_task_sighand(task, &flags)) {
944 err = -ESRCH;
945 goto err_task_lock;
946 }
947
948 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
949 err = -EACCES;
950 goto err_sighand;
951 }
952
David Rientjes51b1bd22010-08-09 17:19:47 -0700953 /*
954 * Warn that /proc/pid/oom_adj is deprecated, see
955 * Documentation/feature-removal-schedule.txt.
956 */
Linus Torvaldsc2142702011-08-06 11:43:08 -0700957 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 -0700958 current->comm, task_pid_nr(current), task_pid_nr(task),
959 task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700960 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -0700961 /*
962 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
963 * value is always attainable.
964 */
965 if (task->signal->oom_adj == OOM_ADJUST_MAX)
966 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
967 else
968 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
969 -OOM_DISABLE;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800970 trace_oom_score_adj_update(task);
David Rientjes723548b2010-10-26 14:21:25 -0700971err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700972 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -0700973err_task_lock:
974 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700975 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -0700976out:
977 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Arjan van de Ven00977a52007-02-12 00:55:34 -0800980static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 .read = oom_adjust_read,
982 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100983 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984};
985
David Rientjesa63d83f2010-08-09 17:19:46 -0700986static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
987 size_t count, loff_t *ppos)
988{
989 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
990 char buffer[PROC_NUMBUF];
991 int oom_score_adj = OOM_SCORE_ADJ_MIN;
992 unsigned long flags;
993 size_t len;
994
995 if (!task)
996 return -ESRCH;
997 if (lock_task_sighand(task, &flags)) {
998 oom_score_adj = task->signal->oom_score_adj;
999 unlock_task_sighand(task, &flags);
1000 }
1001 put_task_struct(task);
1002 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1003 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1004}
1005
1006static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1007 size_t count, loff_t *ppos)
1008{
1009 struct task_struct *task;
1010 char buffer[PROC_NUMBUF];
1011 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001012 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001013 int err;
1014
1015 memset(buffer, 0, sizeof(buffer));
1016 if (count > sizeof(buffer) - 1)
1017 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001018 if (copy_from_user(buffer, buf, count)) {
1019 err = -EFAULT;
1020 goto out;
1021 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001022
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001023 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001024 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001025 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001026 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001027 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1028 err = -EINVAL;
1029 goto out;
1030 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001031
1032 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001033 if (!task) {
1034 err = -ESRCH;
1035 goto out;
1036 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001037
Ying Han3d5992d2010-10-26 14:21:23 -07001038 task_lock(task);
1039 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001040 err = -EINVAL;
1041 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001042 }
David Rientjesd19d5472010-10-26 14:21:26 -07001043
1044 if (!lock_task_sighand(task, &flags)) {
1045 err = -ESRCH;
1046 goto err_task_lock;
1047 }
1048
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001049 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001050 !capable(CAP_SYS_RESOURCE)) {
1051 err = -EACCES;
1052 goto err_sighand;
1053 }
1054
David Rientjesa63d83f2010-08-09 17:19:46 -07001055 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001056 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1057 task->signal->oom_score_adj_min = oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001058 trace_oom_score_adj_update(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001059 /*
1060 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1061 * always attainable.
1062 */
1063 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1064 task->signal->oom_adj = OOM_DISABLE;
1065 else
1066 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1067 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001068err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001069 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001070err_task_lock:
1071 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001072 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001073out:
1074 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001075}
1076
1077static const struct file_operations proc_oom_score_adj_operations = {
1078 .read = oom_score_adj_read,
1079 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001080 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001081};
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083#ifdef CONFIG_AUDITSYSCALL
1084#define TMPBUFLEN 21
1085static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1086 size_t count, loff_t *ppos)
1087{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001088 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001089 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 ssize_t length;
1091 char tmpbuf[TMPBUFLEN];
1092
Eric W. Biederman99f89552006-06-26 00:25:55 -07001093 if (!task)
1094 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001096 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001097 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1099}
1100
1101static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1102 size_t count, loff_t *ppos)
1103{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001104 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 char *page, *tmp;
1106 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 uid_t loginuid;
1108
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001109 rcu_read_lock();
1110 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1111 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001113 }
1114 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Al Viroe0182902006-05-18 08:28:02 -04001116 if (count >= PAGE_SIZE)
1117 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (*ppos != 0) {
1120 /* No partial writes. */
1121 return -EINVAL;
1122 }
Mel Gormane12ba742007-10-16 01:25:52 -07001123 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (!page)
1125 return -ENOMEM;
1126 length = -EFAULT;
1127 if (copy_from_user(page, buf, count))
1128 goto out_free_page;
1129
Al Viroe0182902006-05-18 08:28:02 -04001130 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 loginuid = simple_strtoul(page, &tmp, 10);
1132 if (tmp == page) {
1133 length = -EINVAL;
1134 goto out_free_page;
1135
1136 }
Eric Paris0a300be2012-01-03 14:23:08 -05001137 length = audit_set_loginuid(loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (likely(length == 0))
1139 length = count;
1140
1141out_free_page:
1142 free_page((unsigned long) page);
1143 return length;
1144}
1145
Arjan van de Ven00977a52007-02-12 00:55:34 -08001146static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 .read = proc_loginuid_read,
1148 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001149 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150};
Eric Paris1e0bd752008-03-13 08:15:31 -04001151
1152static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1153 size_t count, loff_t *ppos)
1154{
1155 struct inode * inode = file->f_path.dentry->d_inode;
1156 struct task_struct *task = get_proc_task(inode);
1157 ssize_t length;
1158 char tmpbuf[TMPBUFLEN];
1159
1160 if (!task)
1161 return -ESRCH;
1162 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1163 audit_get_sessionid(task));
1164 put_task_struct(task);
1165 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1166}
1167
1168static const struct file_operations proc_sessionid_operations = {
1169 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001170 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001171};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172#endif
1173
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001174#ifdef CONFIG_FAULT_INJECTION
1175static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1176 size_t count, loff_t *ppos)
1177{
1178 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1179 char buffer[PROC_NUMBUF];
1180 size_t len;
1181 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001182
1183 if (!task)
1184 return -ESRCH;
1185 make_it_fail = task->make_it_fail;
1186 put_task_struct(task);
1187
1188 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001189
1190 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001191}
1192
1193static ssize_t proc_fault_inject_write(struct file * file,
1194 const char __user * buf, size_t count, loff_t *ppos)
1195{
1196 struct task_struct *task;
1197 char buffer[PROC_NUMBUF], *end;
1198 int make_it_fail;
1199
1200 if (!capable(CAP_SYS_RESOURCE))
1201 return -EPERM;
1202 memset(buffer, 0, sizeof(buffer));
1203 if (count > sizeof(buffer) - 1)
1204 count = sizeof(buffer) - 1;
1205 if (copy_from_user(buffer, buf, count))
1206 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001207 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1208 if (*end)
1209 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001210 task = get_proc_task(file->f_dentry->d_inode);
1211 if (!task)
1212 return -ESRCH;
1213 task->make_it_fail = make_it_fail;
1214 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001215
1216 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001217}
1218
Arjan van de Ven00977a52007-02-12 00:55:34 -08001219static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001220 .read = proc_fault_inject_read,
1221 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001222 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001223};
1224#endif
1225
Arjan van de Ven97455122008-01-25 21:08:34 +01001226
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001227#ifdef CONFIG_SCHED_DEBUG
1228/*
1229 * Print out various scheduling related per-task fields:
1230 */
1231static int sched_show(struct seq_file *m, void *v)
1232{
1233 struct inode *inode = m->private;
1234 struct task_struct *p;
1235
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001236 p = get_proc_task(inode);
1237 if (!p)
1238 return -ESRCH;
1239 proc_sched_show_task(p, m);
1240
1241 put_task_struct(p);
1242
1243 return 0;
1244}
1245
1246static ssize_t
1247sched_write(struct file *file, const char __user *buf,
1248 size_t count, loff_t *offset)
1249{
1250 struct inode *inode = file->f_path.dentry->d_inode;
1251 struct task_struct *p;
1252
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001253 p = get_proc_task(inode);
1254 if (!p)
1255 return -ESRCH;
1256 proc_sched_set_task(p);
1257
1258 put_task_struct(p);
1259
1260 return count;
1261}
1262
1263static int sched_open(struct inode *inode, struct file *filp)
1264{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001265 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001266}
1267
1268static const struct file_operations proc_pid_sched_operations = {
1269 .open = sched_open,
1270 .read = seq_read,
1271 .write = sched_write,
1272 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001273 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001274};
1275
1276#endif
1277
Mike Galbraith5091faa2010-11-30 14:18:03 +01001278#ifdef CONFIG_SCHED_AUTOGROUP
1279/*
1280 * Print out autogroup related information:
1281 */
1282static int sched_autogroup_show(struct seq_file *m, void *v)
1283{
1284 struct inode *inode = m->private;
1285 struct task_struct *p;
1286
1287 p = get_proc_task(inode);
1288 if (!p)
1289 return -ESRCH;
1290 proc_sched_autogroup_show_task(p, m);
1291
1292 put_task_struct(p);
1293
1294 return 0;
1295}
1296
1297static ssize_t
1298sched_autogroup_write(struct file *file, const char __user *buf,
1299 size_t count, loff_t *offset)
1300{
1301 struct inode *inode = file->f_path.dentry->d_inode;
1302 struct task_struct *p;
1303 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001304 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001305 int err;
1306
1307 memset(buffer, 0, sizeof(buffer));
1308 if (count > sizeof(buffer) - 1)
1309 count = sizeof(buffer) - 1;
1310 if (copy_from_user(buffer, buf, count))
1311 return -EFAULT;
1312
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001313 err = kstrtoint(strstrip(buffer), 0, &nice);
1314 if (err < 0)
1315 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001316
1317 p = get_proc_task(inode);
1318 if (!p)
1319 return -ESRCH;
1320
1321 err = nice;
1322 err = proc_sched_autogroup_set_nice(p, &err);
1323 if (err)
1324 count = err;
1325
1326 put_task_struct(p);
1327
1328 return count;
1329}
1330
1331static int sched_autogroup_open(struct inode *inode, struct file *filp)
1332{
1333 int ret;
1334
1335 ret = single_open(filp, sched_autogroup_show, NULL);
1336 if (!ret) {
1337 struct seq_file *m = filp->private_data;
1338
1339 m->private = inode;
1340 }
1341 return ret;
1342}
1343
1344static const struct file_operations proc_pid_sched_autogroup_operations = {
1345 .open = sched_autogroup_open,
1346 .read = seq_read,
1347 .write = sched_autogroup_write,
1348 .llseek = seq_lseek,
1349 .release = single_release,
1350};
1351
1352#endif /* CONFIG_SCHED_AUTOGROUP */
1353
john stultz4614a696b2009-12-14 18:00:05 -08001354static ssize_t comm_write(struct file *file, const char __user *buf,
1355 size_t count, loff_t *offset)
1356{
1357 struct inode *inode = file->f_path.dentry->d_inode;
1358 struct task_struct *p;
1359 char buffer[TASK_COMM_LEN];
1360
1361 memset(buffer, 0, sizeof(buffer));
1362 if (count > sizeof(buffer) - 1)
1363 count = sizeof(buffer) - 1;
1364 if (copy_from_user(buffer, buf, count))
1365 return -EFAULT;
1366
1367 p = get_proc_task(inode);
1368 if (!p)
1369 return -ESRCH;
1370
1371 if (same_thread_group(current, p))
1372 set_task_comm(p, buffer);
1373 else
1374 count = -EINVAL;
1375
1376 put_task_struct(p);
1377
1378 return count;
1379}
1380
1381static int comm_show(struct seq_file *m, void *v)
1382{
1383 struct inode *inode = m->private;
1384 struct task_struct *p;
1385
1386 p = get_proc_task(inode);
1387 if (!p)
1388 return -ESRCH;
1389
1390 task_lock(p);
1391 seq_printf(m, "%s\n", p->comm);
1392 task_unlock(p);
1393
1394 put_task_struct(p);
1395
1396 return 0;
1397}
1398
1399static int comm_open(struct inode *inode, struct file *filp)
1400{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001401 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001402}
1403
1404static const struct file_operations proc_pid_set_comm_operations = {
1405 .open = comm_open,
1406 .read = seq_read,
1407 .write = comm_write,
1408 .llseek = seq_lseek,
1409 .release = single_release,
1410};
1411
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001412static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001413{
1414 struct task_struct *task;
1415 struct mm_struct *mm;
1416 struct file *exe_file;
1417
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001418 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001419 if (!task)
1420 return -ENOENT;
1421 mm = get_task_mm(task);
1422 put_task_struct(task);
1423 if (!mm)
1424 return -ENOENT;
1425 exe_file = get_mm_exe_file(mm);
1426 mmput(mm);
1427 if (exe_file) {
1428 *exe_path = exe_file->f_path;
1429 path_get(&exe_file->f_path);
1430 fput(exe_file);
1431 return 0;
1432 } else
1433 return -ENOENT;
1434}
1435
Al Viro008b1502005-08-20 00:17:39 +01001436static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct inode *inode = dentry->d_inode;
1439 int error = -EACCES;
1440
1441 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001442 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Eric W. Biederman778c1142006-06-26 00:25:58 -07001444 /* Are we allowed to snoop on the tasks file descriptors? */
1445 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001448 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449out:
Al Viro008b1502005-08-20 00:17:39 +01001450 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001453static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Mel Gormane12ba742007-10-16 01:25:52 -07001455 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001456 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int len;
1458
1459 if (!tmp)
1460 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001461
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001462 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001463 len = PTR_ERR(pathname);
1464 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001466 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 if (len > buflen)
1469 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001470 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 len = -EFAULT;
1472 out:
1473 free_page((unsigned long)tmp);
1474 return len;
1475}
1476
1477static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1478{
1479 int error = -EACCES;
1480 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001481 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Eric W. Biederman778c1142006-06-26 00:25:58 -07001483 /* Are we allowed to snoop on the tasks file descriptors? */
1484 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001487 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (error)
1489 goto out;
1490
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001491 error = do_proc_readlink(&path, buffer, buflen);
1492 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return error;
1495}
1496
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001497static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001499 .follow_link = proc_pid_follow_link,
1500 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501};
1502
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001503
1504/* building an inode */
1505
1506static int task_dumpable(struct task_struct *task)
1507{
1508 int dumpable = 0;
1509 struct mm_struct *mm;
1510
1511 task_lock(task);
1512 mm = task->mm;
1513 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001514 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001515 task_unlock(task);
1516 if(dumpable == 1)
1517 return 1;
1518 return 0;
1519}
1520
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001521struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001522{
1523 struct inode * inode;
1524 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001525 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001526
1527 /* We need a new inode */
1528
1529 inode = new_inode(sb);
1530 if (!inode)
1531 goto out;
1532
1533 /* Common stuff */
1534 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001535 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001536 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001537 inode->i_op = &proc_def_inode_operations;
1538
1539 /*
1540 * grab the reference to task.
1541 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001542 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001543 if (!ei->pid)
1544 goto out_unlock;
1545
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001546 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001547 rcu_read_lock();
1548 cred = __task_cred(task);
1549 inode->i_uid = cred->euid;
1550 inode->i_gid = cred->egid;
1551 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001552 }
1553 security_task_to_inode(task, inode);
1554
1555out:
1556 return inode;
1557
1558out_unlock:
1559 iput(inode);
1560 return NULL;
1561}
1562
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001563int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001564{
1565 struct inode *inode = dentry->d_inode;
1566 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001567 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001568 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001569
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001570 generic_fillattr(inode, stat);
1571
1572 rcu_read_lock();
1573 stat->uid = 0;
1574 stat->gid = 0;
1575 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1576 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001577 if (!has_pid_permissions(pid, task, 2)) {
1578 rcu_read_unlock();
1579 /*
1580 * This doesn't prevent learning whether PID exists,
1581 * it only makes getattr() consistent with readdir().
1582 */
1583 return -ENOENT;
1584 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001585 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1586 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001587 cred = __task_cred(task);
1588 stat->uid = cred->euid;
1589 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001590 }
1591 }
1592 rcu_read_unlock();
1593 return 0;
1594}
1595
1596/* dentry stuff */
1597
1598/*
1599 * Exceptional case: normally we are not allowed to unhash a busy
1600 * directory. In this case, however, we can do it - no aliasing problems
1601 * due to the way we treat inodes.
1602 *
1603 * Rewrite the inode's ownerships here because the owning task may have
1604 * performed a setuid(), etc.
1605 *
1606 * Before the /proc/pid/status file was created the only way to read
1607 * the effective uid of a /process was to stat /proc/pid. Reading
1608 * /proc/pid/status is slow enough that procps and other packages
1609 * kept stating /proc/pid. To keep the rules in /proc simple I have
1610 * made this apply to all per process world readable and executable
1611 * directories.
1612 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001613int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001614{
Nick Piggin34286d62011-01-07 17:49:57 +11001615 struct inode *inode;
1616 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001617 const struct cred *cred;
1618
Nick Piggin34286d62011-01-07 17:49:57 +11001619 if (nd && nd->flags & LOOKUP_RCU)
1620 return -ECHILD;
1621
1622 inode = dentry->d_inode;
1623 task = get_proc_task(inode);
1624
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001625 if (task) {
1626 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1627 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001628 rcu_read_lock();
1629 cred = __task_cred(task);
1630 inode->i_uid = cred->euid;
1631 inode->i_gid = cred->egid;
1632 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001633 } else {
1634 inode->i_uid = 0;
1635 inode->i_gid = 0;
1636 }
1637 inode->i_mode &= ~(S_ISUID | S_ISGID);
1638 security_task_to_inode(task, inode);
1639 put_task_struct(task);
1640 return 1;
1641 }
1642 d_drop(dentry);
1643 return 0;
1644}
1645
Nick Pigginfe15ce42011-01-07 17:49:23 +11001646static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001647{
1648 /* Is the task we represent dead?
1649 * If so, then don't put the dentry on the lru list,
1650 * kill it immediately.
1651 */
1652 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1653}
1654
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001655const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001656{
1657 .d_revalidate = pid_revalidate,
1658 .d_delete = pid_delete_dentry,
1659};
1660
1661/* Lookups */
1662
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001663/*
1664 * Fill a directory entry.
1665 *
1666 * If possible create the dcache entry and derive our inode number and
1667 * file type from dcache entry.
1668 *
1669 * Since all of the proc inode numbers are dynamically generated, the inode
1670 * numbers do not exist until the inode is cache. This means creating the
1671 * the dcache entry in readdir is necessary to keep the inode numbers
1672 * reported by readdir in sync with the inode numbers reported
1673 * by stat.
1674 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001675int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1676 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001677 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001678{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001679 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001680 struct inode *inode;
1681 struct qstr qname;
1682 ino_t ino = 0;
1683 unsigned type = DT_UNKNOWN;
1684
1685 qname.name = name;
1686 qname.len = len;
1687 qname.hash = full_name_hash(name, len);
1688
1689 child = d_lookup(dir, &qname);
1690 if (!child) {
1691 struct dentry *new;
1692 new = d_alloc(dir, &qname);
1693 if (new) {
1694 child = instantiate(dir->d_inode, new, task, ptr);
1695 if (child)
1696 dput(new);
1697 else
1698 child = new;
1699 }
1700 }
1701 if (!child || IS_ERR(child) || !child->d_inode)
1702 goto end_instantiate;
1703 inode = child->d_inode;
1704 if (inode) {
1705 ino = inode->i_ino;
1706 type = inode->i_mode >> 12;
1707 }
1708 dput(child);
1709end_instantiate:
1710 if (!ino)
1711 ino = find_inode_number(dir, &qname);
1712 if (!ino)
1713 ino = 1;
1714 return filldir(dirent, name, len, filp->f_pos, ino, type);
1715}
1716
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001717static unsigned name_to_int(struct dentry *dentry)
1718{
1719 const char *name = dentry->d_name.name;
1720 int len = dentry->d_name.len;
1721 unsigned n = 0;
1722
1723 if (len > 1 && *name == '0')
1724 goto out;
1725 while (len-- > 0) {
1726 unsigned c = *name++ - '0';
1727 if (c > 9)
1728 goto out;
1729 if (n >= (~0U-9)/10)
1730 goto out;
1731 n *= 10;
1732 n += c;
1733 }
1734 return n;
1735out:
1736 return ~0U;
1737}
1738
Miklos Szeredi27932742007-05-08 00:26:17 -07001739#define PROC_FDINFO_MAX 64
1740
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001741static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001742{
Linus Torvalds5e442a42011-11-09 18:16:00 -05001743 struct task_struct *task = get_proc_task(inode);
1744 struct files_struct *files = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001745 struct file *file;
1746 int fd = proc_fd(inode);
1747
Linus Torvalds5e442a42011-11-09 18:16:00 -05001748 if (task) {
1749 files = get_files_struct(task);
1750 put_task_struct(task);
1751 }
1752 if (files) {
1753 /*
1754 * We are not taking a ref to the file structure, so we must
1755 * hold ->file_lock.
1756 */
1757 spin_lock(&files->file_lock);
1758 file = fcheck_files(files, fd);
1759 if (file) {
1760 unsigned int f_flags;
1761 struct fdtable *fdt;
Linus Torvalds1117f722011-08-06 11:51:33 -07001762
Linus Torvalds5e442a42011-11-09 18:16:00 -05001763 fdt = files_fdtable(files);
1764 f_flags = file->f_flags & ~O_CLOEXEC;
1765 if (FD_ISSET(fd, fdt->close_on_exec))
1766 f_flags |= O_CLOEXEC;
Linus Torvalds1117f722011-08-06 11:51:33 -07001767
Linus Torvalds5e442a42011-11-09 18:16:00 -05001768 if (path) {
1769 *path = file->f_path;
1770 path_get(&file->f_path);
1771 }
1772 if (info)
1773 snprintf(info, PROC_FDINFO_MAX,
1774 "pos:\t%lli\n"
1775 "flags:\t0%o\n",
1776 (long long) file->f_pos,
1777 f_flags);
1778 spin_unlock(&files->file_lock);
1779 put_files_struct(files);
1780 return 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781 }
Linus Torvalds5e442a42011-11-09 18:16:00 -05001782 spin_unlock(&files->file_lock);
1783 put_files_struct(files);
1784 }
1785 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001786}
1787
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001788static int proc_fd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001789{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001790 return proc_fd_info(dentry->d_inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001791}
1792
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001793static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1794{
Nick Piggin34286d62011-01-07 17:49:57 +11001795 struct inode *inode;
1796 struct task_struct *task;
1797 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001798 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001799 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001800
Nick Piggin34286d62011-01-07 17:49:57 +11001801 if (nd && nd->flags & LOOKUP_RCU)
1802 return -ECHILD;
1803
1804 inode = dentry->d_inode;
1805 task = get_proc_task(inode);
1806 fd = proc_fd(inode);
1807
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001808 if (task) {
1809 files = get_files_struct(task);
1810 if (files) {
1811 rcu_read_lock();
1812 if (fcheck_files(files, fd)) {
1813 rcu_read_unlock();
1814 put_files_struct(files);
1815 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001816 rcu_read_lock();
1817 cred = __task_cred(task);
1818 inode->i_uid = cred->euid;
1819 inode->i_gid = cred->egid;
1820 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001821 } else {
1822 inode->i_uid = 0;
1823 inode->i_gid = 0;
1824 }
1825 inode->i_mode &= ~(S_ISUID | S_ISGID);
1826 security_task_to_inode(task, inode);
1827 put_task_struct(task);
1828 return 1;
1829 }
1830 rcu_read_unlock();
1831 put_files_struct(files);
1832 }
1833 put_task_struct(task);
1834 }
1835 d_drop(dentry);
1836 return 0;
1837}
1838
Al Virod72f71e2009-02-20 05:58:47 +00001839static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001840{
1841 .d_revalidate = tid_fd_revalidate,
1842 .d_delete = pid_delete_dentry,
1843};
1844
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001845static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001846 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001847{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001848 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001849 struct file *file;
1850 struct files_struct *files;
1851 struct inode *inode;
1852 struct proc_inode *ei;
1853 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001854
Eric W. Biederman61a28782006-10-02 02:18:49 -07001855 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001856 if (!inode)
1857 goto out;
1858 ei = PROC_I(inode);
1859 ei->fd = fd;
1860 files = get_files_struct(task);
1861 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001862 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001863 inode->i_mode = S_IFLNK;
1864
1865 /*
1866 * We are not taking a ref to the file structure, so we must
1867 * hold ->file_lock.
1868 */
1869 spin_lock(&files->file_lock);
1870 file = fcheck_files(files, fd);
1871 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001872 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001873 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001874 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001875 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001876 inode->i_mode |= S_IWUSR | S_IXUSR;
1877 spin_unlock(&files->file_lock);
1878 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001879
Linus Torvalds5e442a42011-11-09 18:16:00 -05001880 inode->i_op = &proc_pid_link_inode_operations;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001881 inode->i_size = 64;
1882 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001883 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001884 d_add(dentry, inode);
1885 /* Close the race of the process dying before we return the dentry */
1886 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001887 error = NULL;
1888
1889 out:
1890 return error;
1891out_unlock:
1892 spin_unlock(&files->file_lock);
1893 put_files_struct(files);
1894out_iput:
1895 iput(inode);
1896 goto out;
1897}
1898
Miklos Szeredi27932742007-05-08 00:26:17 -07001899static struct dentry *proc_lookupfd_common(struct inode *dir,
1900 struct dentry *dentry,
1901 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001902{
1903 struct task_struct *task = get_proc_task(dir);
1904 unsigned fd = name_to_int(dentry);
1905 struct dentry *result = ERR_PTR(-ENOENT);
1906
1907 if (!task)
1908 goto out_no_task;
1909 if (fd == ~0U)
1910 goto out;
1911
Miklos Szeredi27932742007-05-08 00:26:17 -07001912 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001913out:
1914 put_task_struct(task);
1915out_no_task:
1916 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001917}
1918
Miklos Szeredi27932742007-05-08 00:26:17 -07001919static int proc_readfd_common(struct file * filp, void * dirent,
1920 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001922 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001923 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001924 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001925 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 struct files_struct * files;
1928
1929 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001930 if (!p)
1931 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 fd = filp->f_pos;
1935 switch (fd) {
1936 case 0:
1937 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
Linus Torvalds5e442a42011-11-09 18:16:00 -05001938 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 filp->f_pos++;
1940 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001941 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Linus Torvalds5e442a42011-11-09 18:16:00 -05001943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 filp->f_pos++;
1945 default:
1946 files = get_files_struct(p);
1947 if (!files)
Linus Torvalds5e442a42011-11-09 18:16:00 -05001948 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001949 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001951 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001953 char name[PROC_NUMBUF];
1954 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 if (!fcheck_files(files, fd))
1957 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001958 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Miklos Szeredi27932742007-05-08 00:26:17 -07001960 len = snprintf(name, sizeof(name), "%d", fd);
1961 if (proc_fill_cache(filp, dirent, filldir,
1962 name, len, instantiate,
1963 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001964 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 break;
1966 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001967 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001969 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 put_files_struct(files);
1971 }
1972out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001973 put_task_struct(p);
1974out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return retval;
1976}
1977
Miklos Szeredi27932742007-05-08 00:26:17 -07001978static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1979 struct nameidata *nd)
1980{
1981 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1982}
1983
1984static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1985{
1986 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1987}
1988
1989static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1990 size_t len, loff_t *ppos)
1991{
1992 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001993 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001994 if (!err)
1995 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1996 return err;
1997}
1998
1999static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002000 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002001 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002002 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002003};
2004
Arjan van de Ven00977a52007-02-12 00:55:34 -08002005static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 .read = generic_read_dir,
2007 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002008 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009};
2010
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002011#ifdef CONFIG_CHECKPOINT_RESTORE
2012
2013/*
2014 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2015 * which represent vma start and end addresses.
2016 */
2017static int dname_to_vma_addr(struct dentry *dentry,
2018 unsigned long *start, unsigned long *end)
2019{
2020 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2021 return -EINVAL;
2022
2023 return 0;
2024}
2025
2026static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2027{
2028 unsigned long vm_start, vm_end;
2029 bool exact_vma_exists = false;
2030 struct mm_struct *mm = NULL;
2031 struct task_struct *task;
2032 const struct cred *cred;
2033 struct inode *inode;
2034 int status = 0;
2035
2036 if (nd && nd->flags & LOOKUP_RCU)
2037 return -ECHILD;
2038
2039 if (!capable(CAP_SYS_ADMIN)) {
2040 status = -EACCES;
2041 goto out_notask;
2042 }
2043
2044 inode = dentry->d_inode;
2045 task = get_proc_task(inode);
2046 if (!task)
2047 goto out_notask;
2048
2049 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2050 goto out;
2051
2052 mm = get_task_mm(task);
2053 if (!mm)
2054 goto out;
2055
2056 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2057 down_read(&mm->mmap_sem);
2058 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2059 up_read(&mm->mmap_sem);
2060 }
2061
2062 mmput(mm);
2063
2064 if (exact_vma_exists) {
2065 if (task_dumpable(task)) {
2066 rcu_read_lock();
2067 cred = __task_cred(task);
2068 inode->i_uid = cred->euid;
2069 inode->i_gid = cred->egid;
2070 rcu_read_unlock();
2071 } else {
2072 inode->i_uid = 0;
2073 inode->i_gid = 0;
2074 }
2075 security_task_to_inode(task, inode);
2076 status = 1;
2077 }
2078
2079out:
2080 put_task_struct(task);
2081
2082out_notask:
2083 if (status <= 0)
2084 d_drop(dentry);
2085
2086 return status;
2087}
2088
2089static const struct dentry_operations tid_map_files_dentry_operations = {
2090 .d_revalidate = map_files_d_revalidate,
2091 .d_delete = pid_delete_dentry,
2092};
2093
2094static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2095{
2096 unsigned long vm_start, vm_end;
2097 struct vm_area_struct *vma;
2098 struct task_struct *task;
2099 struct mm_struct *mm;
2100 int rc;
2101
2102 rc = -ENOENT;
2103 task = get_proc_task(dentry->d_inode);
2104 if (!task)
2105 goto out;
2106
2107 mm = get_task_mm(task);
2108 put_task_struct(task);
2109 if (!mm)
2110 goto out;
2111
2112 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2113 if (rc)
2114 goto out_mmput;
2115
2116 down_read(&mm->mmap_sem);
2117 vma = find_exact_vma(mm, vm_start, vm_end);
2118 if (vma && vma->vm_file) {
2119 *path = vma->vm_file->f_path;
2120 path_get(path);
2121 rc = 0;
2122 }
2123 up_read(&mm->mmap_sem);
2124
2125out_mmput:
2126 mmput(mm);
2127out:
2128 return rc;
2129}
2130
2131struct map_files_info {
2132 struct file *file;
2133 unsigned long len;
2134 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2135};
2136
2137static struct dentry *
2138proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2139 struct task_struct *task, const void *ptr)
2140{
2141 const struct file *file = ptr;
2142 struct proc_inode *ei;
2143 struct inode *inode;
2144
2145 if (!file)
2146 return ERR_PTR(-ENOENT);
2147
2148 inode = proc_pid_make_inode(dir->i_sb, task);
2149 if (!inode)
2150 return ERR_PTR(-ENOENT);
2151
2152 ei = PROC_I(inode);
2153 ei->op.proc_get_link = proc_map_files_get_link;
2154
2155 inode->i_op = &proc_pid_link_inode_operations;
2156 inode->i_size = 64;
2157 inode->i_mode = S_IFLNK;
2158
2159 if (file->f_mode & FMODE_READ)
2160 inode->i_mode |= S_IRUSR;
2161 if (file->f_mode & FMODE_WRITE)
2162 inode->i_mode |= S_IWUSR;
2163
2164 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2165 d_add(dentry, inode);
2166
2167 return NULL;
2168}
2169
2170static struct dentry *proc_map_files_lookup(struct inode *dir,
2171 struct dentry *dentry, struct nameidata *nd)
2172{
2173 unsigned long vm_start, vm_end;
2174 struct vm_area_struct *vma;
2175 struct task_struct *task;
2176 struct dentry *result;
2177 struct mm_struct *mm;
2178
2179 result = ERR_PTR(-EACCES);
2180 if (!capable(CAP_SYS_ADMIN))
2181 goto out;
2182
2183 result = ERR_PTR(-ENOENT);
2184 task = get_proc_task(dir);
2185 if (!task)
2186 goto out;
2187
2188 result = ERR_PTR(-EACCES);
2189 if (lock_trace(task))
2190 goto out_put_task;
2191
2192 result = ERR_PTR(-ENOENT);
2193 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2194 goto out_unlock;
2195
2196 mm = get_task_mm(task);
2197 if (!mm)
2198 goto out_unlock;
2199
2200 down_read(&mm->mmap_sem);
2201 vma = find_exact_vma(mm, vm_start, vm_end);
2202 if (!vma)
2203 goto out_no_vma;
2204
2205 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2206
2207out_no_vma:
2208 up_read(&mm->mmap_sem);
2209 mmput(mm);
2210out_unlock:
2211 unlock_trace(task);
2212out_put_task:
2213 put_task_struct(task);
2214out:
2215 return result;
2216}
2217
2218static const struct inode_operations proc_map_files_inode_operations = {
2219 .lookup = proc_map_files_lookup,
2220 .permission = proc_fd_permission,
2221 .setattr = proc_setattr,
2222};
2223
2224static int
2225proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2226{
2227 struct dentry *dentry = filp->f_path.dentry;
2228 struct inode *inode = dentry->d_inode;
2229 struct vm_area_struct *vma;
2230 struct task_struct *task;
2231 struct mm_struct *mm;
2232 ino_t ino;
2233 int ret;
2234
2235 ret = -EACCES;
2236 if (!capable(CAP_SYS_ADMIN))
2237 goto out;
2238
2239 ret = -ENOENT;
2240 task = get_proc_task(inode);
2241 if (!task)
2242 goto out;
2243
2244 ret = -EACCES;
2245 if (lock_trace(task))
2246 goto out_put_task;
2247
2248 ret = 0;
2249 switch (filp->f_pos) {
2250 case 0:
2251 ino = inode->i_ino;
2252 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2253 goto out_unlock;
2254 filp->f_pos++;
2255 case 1:
2256 ino = parent_ino(dentry);
2257 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2258 goto out_unlock;
2259 filp->f_pos++;
2260 default:
2261 {
2262 unsigned long nr_files, pos, i;
2263 struct flex_array *fa = NULL;
2264 struct map_files_info info;
2265 struct map_files_info *p;
2266
2267 mm = get_task_mm(task);
2268 if (!mm)
2269 goto out_unlock;
2270 down_read(&mm->mmap_sem);
2271
2272 nr_files = 0;
2273
2274 /*
2275 * We need two passes here:
2276 *
2277 * 1) Collect vmas of mapped files with mmap_sem taken
2278 * 2) Release mmap_sem and instantiate entries
2279 *
2280 * otherwise we get lockdep complained, since filldir()
2281 * routine might require mmap_sem taken in might_fault().
2282 */
2283
2284 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2285 if (vma->vm_file && ++pos > filp->f_pos)
2286 nr_files++;
2287 }
2288
2289 if (nr_files) {
2290 fa = flex_array_alloc(sizeof(info), nr_files,
2291 GFP_KERNEL);
2292 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2293 GFP_KERNEL)) {
2294 ret = -ENOMEM;
2295 if (fa)
2296 flex_array_free(fa);
2297 up_read(&mm->mmap_sem);
2298 mmput(mm);
2299 goto out_unlock;
2300 }
2301 for (i = 0, vma = mm->mmap, pos = 2; vma;
2302 vma = vma->vm_next) {
2303 if (!vma->vm_file)
2304 continue;
2305 if (++pos <= filp->f_pos)
2306 continue;
2307
2308 get_file(vma->vm_file);
2309 info.file = vma->vm_file;
2310 info.len = snprintf(info.name,
2311 sizeof(info.name), "%lx-%lx",
2312 vma->vm_start, vma->vm_end);
2313 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2314 BUG();
2315 }
2316 }
2317 up_read(&mm->mmap_sem);
2318
2319 for (i = 0; i < nr_files; i++) {
2320 p = flex_array_get(fa, i);
2321 ret = proc_fill_cache(filp, dirent, filldir,
2322 p->name, p->len,
2323 proc_map_files_instantiate,
2324 task, p->file);
2325 if (ret)
2326 break;
2327 filp->f_pos++;
2328 fput(p->file);
2329 }
2330 for (; i < nr_files; i++) {
2331 /*
2332 * In case of error don't forget
2333 * to put rest of file refs.
2334 */
2335 p = flex_array_get(fa, i);
2336 fput(p->file);
2337 }
2338 if (fa)
2339 flex_array_free(fa);
2340 mmput(mm);
2341 }
2342 }
2343
2344out_unlock:
2345 unlock_trace(task);
2346out_put_task:
2347 put_task_struct(task);
2348out:
2349 return ret;
2350}
2351
2352static const struct file_operations proc_map_files_operations = {
2353 .read = generic_read_dir,
2354 .readdir = proc_map_files_readdir,
2355 .llseek = default_llseek,
2356};
2357
2358#endif /* CONFIG_CHECKPOINT_RESTORE */
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002361 * /proc/pid/fd needs a special permission handler so that a process can still
2362 * access /proc/self/fd after it has executed a setuid().
2363 */
Al Viro10556cb2011-06-20 19:28:19 -04002364static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002365{
Al Viro2830ba72011-06-20 19:16:29 -04002366 int rv = generic_permission(inode, mask);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002367 if (rv == 0)
2368 return 0;
2369 if (task_pid(current) == proc_pid(inode))
2370 rv = 0;
2371 return rv;
2372}
2373
2374/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 * proc directories can do almost nothing..
2376 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002377static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002379 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002380 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381};
2382
Miklos Szeredi27932742007-05-08 00:26:17 -07002383static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2384 struct dentry *dentry, struct task_struct *task, const void *ptr)
2385{
2386 unsigned fd = *(unsigned *)ptr;
2387 struct inode *inode;
2388 struct proc_inode *ei;
2389 struct dentry *error = ERR_PTR(-ENOENT);
2390
2391 inode = proc_pid_make_inode(dir->i_sb, task);
2392 if (!inode)
2393 goto out;
2394 ei = PROC_I(inode);
2395 ei->fd = fd;
2396 inode->i_mode = S_IFREG | S_IRUSR;
2397 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002398 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002399 d_add(dentry, inode);
2400 /* Close the race of the process dying before we return the dentry */
2401 if (tid_fd_revalidate(dentry, NULL))
2402 error = NULL;
2403
2404 out:
2405 return error;
2406}
2407
2408static struct dentry *proc_lookupfdinfo(struct inode *dir,
2409 struct dentry *dentry,
2410 struct nameidata *nd)
2411{
2412 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2413}
2414
2415static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2416{
2417 return proc_readfd_common(filp, dirent, filldir,
2418 proc_fdinfo_instantiate);
2419}
2420
2421static const struct file_operations proc_fdinfo_operations = {
2422 .read = generic_read_dir,
2423 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002424 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002425};
2426
2427/*
2428 * proc directories can do almost nothing..
2429 */
2430static const struct inode_operations proc_fdinfo_inode_operations = {
2431 .lookup = proc_lookupfdinfo,
2432 .setattr = proc_setattr,
2433};
2434
2435
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002436static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002437 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002438{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002439 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002440 struct inode *inode;
2441 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002442 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002443
Eric W. Biederman61a28782006-10-02 02:18:49 -07002444 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002445 if (!inode)
2446 goto out;
2447
2448 ei = PROC_I(inode);
2449 inode->i_mode = p->mode;
2450 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002451 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002452 if (p->iop)
2453 inode->i_op = p->iop;
2454 if (p->fop)
2455 inode->i_fop = p->fop;
2456 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002457 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002458 d_add(dentry, inode);
2459 /* Close the race of the process dying before we return the dentry */
2460 if (pid_revalidate(dentry, NULL))
2461 error = NULL;
2462out:
2463 return error;
2464}
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466static struct dentry *proc_pident_lookup(struct inode *dir,
2467 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002468 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002469 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002471 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002472 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002473 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002475 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Eric W. Biederman99f89552006-06-26 00:25:55 -07002477 if (!task)
2478 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002480 /*
2481 * Yes, it does not scale. And it should not. Don't add
2482 * new entries into /proc/<tgid>/ without very good reasons.
2483 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002484 last = &ents[nents - 1];
2485 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (p->len != dentry->d_name.len)
2487 continue;
2488 if (!memcmp(dentry->d_name.name, p->name, p->len))
2489 break;
2490 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002491 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 goto out;
2493
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002494 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002496 put_task_struct(task);
2497out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002498 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
Eric Dumazetc5141e62007-05-08 00:26:15 -07002501static int proc_pident_fill_cache(struct file *filp, void *dirent,
2502 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002503{
2504 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2505 proc_pident_instantiate, task, p);
2506}
2507
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002508static int proc_pident_readdir(struct file *filp,
2509 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002510 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511{
2512 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002513 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514 struct inode *inode = dentry->d_inode;
2515 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002516 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002517 ino_t ino;
2518 int ret;
2519
2520 ret = -ENOENT;
2521 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002522 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523
2524 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002525 i = filp->f_pos;
2526 switch (i) {
2527 case 0:
2528 ino = inode->i_ino;
2529 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2530 goto out;
2531 i++;
2532 filp->f_pos++;
2533 /* fall through */
2534 case 1:
2535 ino = parent_ino(dentry);
2536 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2537 goto out;
2538 i++;
2539 filp->f_pos++;
2540 /* fall through */
2541 default:
2542 i -= 2;
2543 if (i >= nents) {
2544 ret = 1;
2545 goto out;
2546 }
2547 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002548 last = &ents[nents - 1];
2549 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002550 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551 goto out;
2552 filp->f_pos++;
2553 p++;
2554 }
2555 }
2556
2557 ret = 1;
2558out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002559 put_task_struct(task);
2560out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002561 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002565static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2566 size_t count, loff_t *ppos)
2567{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002568 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002569 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002570 ssize_t length;
2571 struct task_struct *task = get_proc_task(inode);
2572
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002573 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002574 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002575
2576 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002577 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002578 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002579 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002580 if (length > 0)
2581 length = simple_read_from_buffer(buf, count, ppos, p, length);
2582 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002583 return length;
2584}
2585
2586static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2587 size_t count, loff_t *ppos)
2588{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002589 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002590 char *page;
2591 ssize_t length;
2592 struct task_struct *task = get_proc_task(inode);
2593
2594 length = -ESRCH;
2595 if (!task)
2596 goto out_no_task;
2597 if (count > PAGE_SIZE)
2598 count = PAGE_SIZE;
2599
2600 /* No partial writes. */
2601 length = -EINVAL;
2602 if (*ppos != 0)
2603 goto out;
2604
2605 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002606 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002607 if (!page)
2608 goto out;
2609
2610 length = -EFAULT;
2611 if (copy_from_user(page, buf, count))
2612 goto out_free;
2613
David Howells107db7c2009-05-08 13:55:27 +01002614 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002615 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002616 if (length < 0)
2617 goto out_free;
2618
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002619 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002620 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002621 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002622 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002623out_free:
2624 free_page((unsigned long) page);
2625out:
2626 put_task_struct(task);
2627out_no_task:
2628 return length;
2629}
2630
Arjan van de Ven00977a52007-02-12 00:55:34 -08002631static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002632 .read = proc_pid_attr_read,
2633 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002634 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002635};
2636
Eric Dumazetc5141e62007-05-08 00:26:15 -07002637static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002638 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2639 REG("prev", S_IRUGO, proc_pid_attr_operations),
2640 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2641 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2642 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2643 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002644};
2645
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002646static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 void * dirent, filldir_t filldir)
2648{
2649 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002650 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
Arjan van de Ven00977a52007-02-12 00:55:34 -08002653static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002655 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002656 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657};
2658
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002659static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct dentry *dentry, struct nameidata *nd)
2661{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002662 return proc_pident_lookup(dir, dentry,
2663 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002666static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002667 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002668 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002669 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670};
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672#endif
2673
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002674#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002675static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2676 size_t count, loff_t *ppos)
2677{
2678 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2679 struct mm_struct *mm;
2680 char buffer[PROC_NUMBUF];
2681 size_t len;
2682 int ret;
2683
2684 if (!task)
2685 return -ESRCH;
2686
2687 ret = 0;
2688 mm = get_task_mm(task);
2689 if (mm) {
2690 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2691 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2692 MMF_DUMP_FILTER_SHIFT));
2693 mmput(mm);
2694 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2695 }
2696
2697 put_task_struct(task);
2698
2699 return ret;
2700}
2701
2702static ssize_t proc_coredump_filter_write(struct file *file,
2703 const char __user *buf,
2704 size_t count,
2705 loff_t *ppos)
2706{
2707 struct task_struct *task;
2708 struct mm_struct *mm;
2709 char buffer[PROC_NUMBUF], *end;
2710 unsigned int val;
2711 int ret;
2712 int i;
2713 unsigned long mask;
2714
2715 ret = -EFAULT;
2716 memset(buffer, 0, sizeof(buffer));
2717 if (count > sizeof(buffer) - 1)
2718 count = sizeof(buffer) - 1;
2719 if (copy_from_user(buffer, buf, count))
2720 goto out_no_task;
2721
2722 ret = -EINVAL;
2723 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2724 if (*end == '\n')
2725 end++;
2726 if (end - buffer == 0)
2727 goto out_no_task;
2728
2729 ret = -ESRCH;
2730 task = get_proc_task(file->f_dentry->d_inode);
2731 if (!task)
2732 goto out_no_task;
2733
2734 ret = end - buffer;
2735 mm = get_task_mm(task);
2736 if (!mm)
2737 goto out_no_mm;
2738
2739 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2740 if (val & mask)
2741 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2742 else
2743 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2744 }
2745
2746 mmput(mm);
2747 out_no_mm:
2748 put_task_struct(task);
2749 out_no_task:
2750 return ret;
2751}
2752
2753static const struct file_operations proc_coredump_filter_operations = {
2754 .read = proc_coredump_filter_read,
2755 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002756 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002757};
2758#endif
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760/*
2761 * /proc/self:
2762 */
2763static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2764 int buflen)
2765{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002766 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002767 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002768 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002769 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002770 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002771 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return vfs_readlink(dentry,buffer,buflen,tmp);
2773}
2774
Al Viro008b1502005-08-20 00:17:39 +01002775static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002777 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002778 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002779 char *name = ERR_PTR(-ENOENT);
2780 if (tgid) {
2781 name = __getname();
2782 if (!name)
2783 name = ERR_PTR(-ENOMEM);
2784 else
2785 sprintf(name, "%d", tgid);
2786 }
2787 nd_set_link(nd, name);
2788 return NULL;
2789}
2790
2791static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2792 void *cookie)
2793{
2794 char *s = nd_get_link(nd);
2795 if (!IS_ERR(s))
2796 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002797}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002799static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 .readlink = proc_self_readlink,
2801 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002802 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803};
2804
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002805/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002806 * proc base
2807 *
2808 * These are the directory entries in the root directory of /proc
2809 * that properly belong to the /proc filesystem, as they describe
2810 * describe something that is process related.
2811 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002812static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002813 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002814 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002815};
2816
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002817static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002818 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002819{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002820 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002821 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002822 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002823 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002824
2825 /* Allocate the inode */
2826 error = ERR_PTR(-ENOMEM);
2827 inode = new_inode(dir->i_sb);
2828 if (!inode)
2829 goto out;
2830
2831 /* Initialize the inode */
2832 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002833 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002834 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002835
2836 /*
2837 * grab the reference to the task.
2838 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002839 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002840 if (!ei->pid)
2841 goto out_iput;
2842
Eric W. Biederman801199c2006-10-02 02:18:48 -07002843 inode->i_mode = p->mode;
2844 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002845 set_nlink(inode, 2);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002846 if (S_ISLNK(inode->i_mode))
2847 inode->i_size = 64;
2848 if (p->iop)
2849 inode->i_op = p->iop;
2850 if (p->fop)
2851 inode->i_fop = p->fop;
2852 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002853 d_add(dentry, inode);
2854 error = NULL;
2855out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002856 return error;
2857out_iput:
2858 iput(inode);
2859 goto out;
2860}
2861
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002862static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2863{
2864 struct dentry *error;
2865 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002866 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002867
2868 error = ERR_PTR(-ENOENT);
2869
2870 if (!task)
2871 goto out_no_task;
2872
2873 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002874 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2875 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002876 if (p->len != dentry->d_name.len)
2877 continue;
2878 if (!memcmp(dentry->d_name.name, p->name, p->len))
2879 break;
2880 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002881 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002882 goto out;
2883
2884 error = proc_base_instantiate(dir, dentry, task, p);
2885
2886out:
2887 put_task_struct(task);
2888out_no_task:
2889 return error;
2890}
2891
Eric Dumazetc5141e62007-05-08 00:26:15 -07002892static int proc_base_fill_cache(struct file *filp, void *dirent,
2893 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002894{
2895 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2896 proc_base_instantiate, task, p);
2897}
2898
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002899#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002900static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002901{
Andrea Righi940389b2008-07-28 00:48:12 +02002902 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002903 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002904 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002905
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002906 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2907 if (result)
2908 return result;
2909
2910 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2911 result = -EACCES;
2912 goto out_unlock;
2913 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002914
Andrea Righi59954772008-07-27 17:29:15 +02002915 if (whole && lock_task_sighand(task, &flags)) {
2916 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002917
Andrea Righi59954772008-07-27 17:29:15 +02002918 task_io_accounting_add(&acct, &task->signal->ioac);
2919 while_each_thread(task, t)
2920 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002921
Andrea Righi59954772008-07-27 17:29:15 +02002922 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002923 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002924 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002925 "rchar: %llu\n"
2926 "wchar: %llu\n"
2927 "syscr: %llu\n"
2928 "syscw: %llu\n"
2929 "read_bytes: %llu\n"
2930 "write_bytes: %llu\n"
2931 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002932 (unsigned long long)acct.rchar,
2933 (unsigned long long)acct.wchar,
2934 (unsigned long long)acct.syscr,
2935 (unsigned long long)acct.syscw,
2936 (unsigned long long)acct.read_bytes,
2937 (unsigned long long)acct.write_bytes,
2938 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002939out_unlock:
2940 mutex_unlock(&task->signal->cred_guard_mutex);
2941 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002942}
Andrea Righi297c5d92008-07-25 01:48:49 -07002943
2944static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2945{
2946 return do_io_accounting(task, buffer, 0);
2947}
2948
2949static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2950{
2951 return do_io_accounting(task, buffer, 1);
2952}
2953#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002954
Kees Cook47830722008-10-06 03:11:58 +04002955static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2956 struct pid *pid, struct task_struct *task)
2957{
Al Viroa9712bc2011-03-23 15:52:50 -04002958 int err = lock_trace(task);
2959 if (!err) {
2960 seq_printf(m, "%08x\n", task->personality);
2961 unlock_trace(task);
2962 }
2963 return err;
Kees Cook47830722008-10-06 03:11:58 +04002964}
2965
Eric W. Biederman801199c2006-10-02 02:18:48 -07002966/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002967 * Thread groups
2968 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002969static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002970static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002971
Eric Dumazetc5141e62007-05-08 00:26:15 -07002972static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002973 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2974 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002975#ifdef CONFIG_CHECKPOINT_RESTORE
2976 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2977#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002978 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002979 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002980#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002981 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002982#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002983 REG("environ", S_IRUSR, proc_environ_operations),
2984 INF("auxv", S_IRUSR, proc_pid_auxv),
2985 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002986 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002987 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002988#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002989 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002990#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002991#ifdef CONFIG_SCHED_AUTOGROUP
2992 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2993#endif
john stultz4614a696b2009-12-14 18:00:05 -08002994 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002995#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002996 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002997#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002998 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2999 ONE("stat", S_IRUGO, proc_tgid_stat),
3000 ONE("statm", S_IRUGO, proc_pid_statm),
3001 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003002#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003003 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003004#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003005 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3006 LNK("cwd", proc_cwd_link),
3007 LNK("root", proc_root_link),
3008 LNK("exe", proc_exe_link),
3009 REG("mounts", S_IRUGO, proc_mounts_operations),
3010 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3011 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003012#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003013 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3014 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003015 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003016#endif
3017#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003018 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003019#endif
3020#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003021 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003022#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003023#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003024 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003025#endif
3026#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003027 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003028#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003029#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003030 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003031#endif
Paul Menage8793d852007-10-18 23:39:39 -07003032#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003033 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003034#endif
Paul Menagea4243162007-10-18 23:39:35 -07003035#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003036 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003037#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003038 INF("oom_score", S_IRUGO, proc_oom_score),
3039 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003040 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003041#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003042 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3043 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003044#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003045#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003046 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003047#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003048#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003049 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003050#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003051#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003052 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003053#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003054#ifdef CONFIG_HARDWALL
3055 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3056#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003057};
3058
3059static int proc_tgid_base_readdir(struct file * filp,
3060 void * dirent, filldir_t filldir)
3061{
3062 return proc_pident_readdir(filp,dirent,filldir,
3063 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3064}
3065
Arjan van de Ven00977a52007-02-12 00:55:34 -08003066static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003067 .read = generic_read_dir,
3068 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003069 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003070};
3071
3072static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003073 return proc_pident_lookup(dir, dentry,
3074 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003075}
3076
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003077static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003078 .lookup = proc_tgid_base_lookup,
3079 .getattr = pid_getattr,
3080 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003081 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003082};
3083
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003084static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003086 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003087 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003088 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Eric W. Biederman48e64842006-06-26 00:25:48 -07003090 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003091 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3092 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003093 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08003094 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003095 d_drop(dentry);
3096 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Eric W. Biederman48e64842006-06-26 00:25:48 -07003099 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003100 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3101 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003102 if (!leader)
3103 goto out;
3104
3105 name.name = "task";
3106 name.len = strlen(name.name);
3107 dir = d_hash_and_lookup(leader, &name);
3108 if (!dir)
3109 goto out_put_leader;
3110
3111 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003112 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003113 dentry = d_hash_and_lookup(dir, &name);
3114 if (dentry) {
3115 shrink_dcache_parent(dentry);
3116 d_drop(dentry);
3117 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003119
3120 dput(dir);
3121out_put_leader:
3122 dput(leader);
3123out:
3124 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
Randy Dunlap0895e912007-10-21 21:00:10 -07003127/**
3128 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3129 * @task: task that should be flushed.
3130 *
3131 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003132 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003133 * in. This call is supposed to do all of this job.
3134 *
3135 * Looks in the dcache for
3136 * /proc/@pid
3137 * /proc/@tgid/task/@pid
3138 * if either directory is present flushes it and all of it'ts children
3139 * from the dcache.
3140 *
3141 * It is safe and reasonable to cache /proc entries for a task until
3142 * that task exits. After that they just clog up the dcache with
3143 * useless entries, possibly causing useful dcache entries to be
3144 * flushed instead. This routine is proved to flush those useless
3145 * dcache entries at process exit time.
3146 *
3147 * NOTE: This routine is just an optimization so it does not guarantee
3148 * that no dcache entries will exist at process exit time it
3149 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003150 */
3151
3152void proc_flush_task(struct task_struct *task)
3153{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003154 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003155 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003156 struct upid *upid;
3157
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003158 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003159 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003160
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003161 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003162 upid = &pid->numbers[i];
3163 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003164 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003165 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07003166
3167 upid = &pid->numbers[pid->level];
3168 if (upid->nr == 1)
3169 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003170}
3171
Adrian Bunk9711ef92006-12-06 20:38:31 -08003172static struct dentry *proc_pid_instantiate(struct inode *dir,
3173 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003174 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003175{
3176 struct dentry *error = ERR_PTR(-ENOENT);
3177 struct inode *inode;
3178
Eric W. Biederman61a28782006-10-02 02:18:49 -07003179 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003180 if (!inode)
3181 goto out;
3182
3183 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3184 inode->i_op = &proc_tgid_base_inode_operations;
3185 inode->i_fop = &proc_tgid_base_operations;
3186 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003187
Miklos Szeredibfe86842011-10-28 14:13:29 +02003188 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3189 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003190
Nick Pigginfb045ad2011-01-07 17:49:55 +11003191 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003192
3193 d_add(dentry, inode);
3194 /* Close the race of the process dying before we return the dentry */
3195 if (pid_revalidate(dentry, NULL))
3196 error = NULL;
3197out:
3198 return error;
3199}
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3202{
Dan Carpenter73d36462010-05-26 14:43:25 -07003203 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003206 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Eric W. Biederman801199c2006-10-02 02:18:48 -07003208 result = proc_base_lookup(dir, dentry);
3209 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3210 goto out;
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 tgid = name_to_int(dentry);
3213 if (tgid == ~0U)
3214 goto out;
3215
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003216 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003217 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003218 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (task)
3220 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003221 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 if (!task)
3223 goto out;
3224
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003225 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003226 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07003228 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003232 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003233 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003235struct tgid_iter {
3236 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003237 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003238};
3239static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3240{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003241 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003243 if (iter.task)
3244 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003245 rcu_read_lock();
3246retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003247 iter.task = NULL;
3248 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003249 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003250 iter.tgid = pid_nr_ns(pid, ns);
3251 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003252 /* What we to know is if the pid we have find is the
3253 * pid of a thread_group_leader. Testing for task
3254 * being a thread_group_leader is the obvious thing
3255 * todo but there is a window when it fails, due to
3256 * the pid transfer logic in de_thread.
3257 *
3258 * So we perform the straight forward test of seeing
3259 * if the pid we have found is the pid of a thread
3260 * group leader, and don't worry if the task we have
3261 * found doesn't happen to be a thread group leader.
3262 * As we don't care in the case of readdir.
3263 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003264 if (!iter.task || !has_group_leader_pid(iter.task)) {
3265 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003266 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003267 }
3268 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003270 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003271 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003274#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Eric W. Biederman61a28782006-10-02 02:18:49 -07003276static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003277 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003278{
3279 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003280 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003281 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003282 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003283}
3284
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003285static int fake_filldir(void *buf, const char *name, int namelen,
3286 loff_t offset, u64 ino, unsigned d_type)
3287{
3288 return 0;
3289}
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291/* for the /proc/ directory itself, after non-process stuff has been done */
3292int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3293{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003294 unsigned int nr;
3295 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003296 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003297 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003298 filldir_t __filldir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003300 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3301 goto out_no_task;
3302 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3303
3304 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003305 if (!reaper)
3306 goto out_no_task;
3307
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003308 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003309 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003310 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 }
3313
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003314 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003315 iter.task = NULL;
3316 iter.tgid = filp->f_pos - TGID_OFFSET;
3317 for (iter = next_tgid(ns, iter);
3318 iter.task;
3319 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003320 if (has_pid_permissions(ns, iter.task, 2))
3321 __filldir = filldir;
3322 else
3323 __filldir = fake_filldir;
3324
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003325 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003326 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003327 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003328 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003331 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3332out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003333 put_task_struct(reaper);
3334out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return 0;
3336}
3337
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003338/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003339 * Tasks
3340 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003341static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003342 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003343 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003344 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003345 REG("environ", S_IRUSR, proc_environ_operations),
3346 INF("auxv", S_IRUSR, proc_pid_auxv),
3347 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003348 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003349 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003350#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003351 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003352#endif
john stultz4614a696b2009-12-14 18:00:05 -08003353 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003354#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003355 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003356#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003357 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3358 ONE("stat", S_IRUGO, proc_tid_stat),
3359 ONE("statm", S_IRUGO, proc_pid_statm),
3360 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003361#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003362 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003363#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003364 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3365 LNK("cwd", proc_cwd_link),
3366 LNK("root", proc_root_link),
3367 LNK("exe", proc_exe_link),
3368 REG("mounts", S_IRUGO, proc_mounts_operations),
3369 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003370#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003371 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3372 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003373 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003374#endif
3375#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003376 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003377#endif
3378#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003379 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003380#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003381#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003382 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003383#endif
3384#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003385 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003386#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003387#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003388 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003389#endif
Paul Menage8793d852007-10-18 23:39:39 -07003390#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003391 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003392#endif
Paul Menagea4243162007-10-18 23:39:35 -07003393#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003394 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003395#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003396 INF("oom_score", S_IRUGO, proc_oom_score),
3397 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003398 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003399#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003400 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003401 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003402#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003403#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003404 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003405#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003406#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003407 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003408#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003409#ifdef CONFIG_HARDWALL
3410 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3411#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003412};
3413
3414static int proc_tid_base_readdir(struct file * filp,
3415 void * dirent, filldir_t filldir)
3416{
3417 return proc_pident_readdir(filp,dirent,filldir,
3418 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3419}
3420
3421static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003422 return proc_pident_lookup(dir, dentry,
3423 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003424}
3425
Arjan van de Ven00977a52007-02-12 00:55:34 -08003426static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003427 .read = generic_read_dir,
3428 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003429 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003430};
3431
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003432static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003433 .lookup = proc_tid_base_lookup,
3434 .getattr = pid_getattr,
3435 .setattr = proc_setattr,
3436};
3437
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003438static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003439 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003440{
3441 struct dentry *error = ERR_PTR(-ENOENT);
3442 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003443 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003444
3445 if (!inode)
3446 goto out;
3447 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3448 inode->i_op = &proc_tid_base_inode_operations;
3449 inode->i_fop = &proc_tid_base_operations;
3450 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003451
Miklos Szeredibfe86842011-10-28 14:13:29 +02003452 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3453 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003454
Nick Pigginfb045ad2011-01-07 17:49:55 +11003455 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003456
3457 d_add(dentry, inode);
3458 /* Close the race of the process dying before we return the dentry */
3459 if (pid_revalidate(dentry, NULL))
3460 error = NULL;
3461out:
3462 return error;
3463}
3464
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003465static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3466{
3467 struct dentry *result = ERR_PTR(-ENOENT);
3468 struct task_struct *task;
3469 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003470 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003471 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003472
3473 if (!leader)
3474 goto out_no_task;
3475
3476 tid = name_to_int(dentry);
3477 if (tid == ~0U)
3478 goto out;
3479
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003480 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003481 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003482 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003483 if (task)
3484 get_task_struct(task);
3485 rcu_read_unlock();
3486 if (!task)
3487 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003488 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003489 goto out_drop_task;
3490
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003491 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003492out_drop_task:
3493 put_task_struct(task);
3494out:
3495 put_task_struct(leader);
3496out_no_task:
3497 return result;
3498}
3499
3500/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003501 * Find the first tid of a thread group to return to user space.
3502 *
3503 * Usually this is just the thread group leader, but if the users
3504 * buffer was too small or there was a seek into the middle of the
3505 * directory we have more work todo.
3506 *
3507 * In the case of a short read we start with find_task_by_pid.
3508 *
3509 * In the case of a seek we start with the leader and walk nr
3510 * threads past it.
3511 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003512static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003513 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003514{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003515 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003516
Eric W. Biedermancc288732006-06-26 00:26:01 -07003517 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003518 /* Attempt to start with the pid of a thread */
3519 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003520 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003521 if (pos && (pos->group_leader == leader))
3522 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003523 }
3524
3525 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003526 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003527 if (nr && nr >= get_nr_threads(leader))
3528 goto out;
3529
3530 /* If we haven't found our starting place yet start
3531 * with the leader and walk nr threads forward.
3532 */
3533 for (pos = leader; nr > 0; --nr) {
3534 pos = next_thread(pos);
3535 if (pos == leader) {
3536 pos = NULL;
3537 goto out;
3538 }
3539 }
3540found:
3541 get_task_struct(pos);
3542out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003543 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003544 return pos;
3545}
3546
3547/*
3548 * Find the next thread in the thread list.
3549 * Return NULL if there is an error or no next thread.
3550 *
3551 * The reference to the input task_struct is released.
3552 */
3553static struct task_struct *next_tid(struct task_struct *start)
3554{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003555 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003556 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003557 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003558 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003559 if (thread_group_leader(pos))
3560 pos = NULL;
3561 else
3562 get_task_struct(pos);
3563 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003564 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003565 put_task_struct(start);
3566 return pos;
3567}
3568
Eric W. Biederman61a28782006-10-02 02:18:49 -07003569static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3570 struct task_struct *task, int tid)
3571{
3572 char name[PROC_NUMBUF];
3573 int len = snprintf(name, sizeof(name), "%d", tid);
3574 return proc_fill_cache(filp, dirent, filldir, name, len,
3575 proc_task_instantiate, task, NULL);
3576}
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578/* for the /proc/TGID/task/ directories */
3579static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3580{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003581 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003583 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003584 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 int retval = -ENOENT;
3586 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003587 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003588 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003590 task = get_proc_task(inode);
3591 if (!task)
3592 goto out_no_task;
3593 rcu_read_lock();
3594 if (pid_alive(task)) {
3595 leader = task->group_leader;
3596 get_task_struct(leader);
3597 }
3598 rcu_read_unlock();
3599 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003600 if (!leader)
3601 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 retval = 0;
3603
Linus Torvaldsee568b22009-03-17 10:02:35 -07003604 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 case 0:
3606 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003607 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003609 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 /* fall through */
3611 case 1:
3612 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003613 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003615 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 /* fall through */
3617 }
3618
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003619 /* f_version caches the tgid value that the last readdir call couldn't
3620 * return. lseek aka telldir automagically resets f_version to 0.
3621 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003622 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003623 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003624 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003625 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003626 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003627 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003628 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003629 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003630 /* returning this tgid failed, save it as the first
3631 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003632 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003633 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 }
3637out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003638 put_task_struct(leader);
3639out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return retval;
3641}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003642
3643static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3644{
3645 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003646 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003647 generic_fillattr(inode, stat);
3648
Eric W. Biederman99f89552006-06-26 00:25:55 -07003649 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003650 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003651 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003652 }
3653
3654 return 0;
3655}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003656
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003657static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003658 .lookup = proc_task_lookup,
3659 .getattr = proc_task_getattr,
3660 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003661 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003662};
3663
Arjan van de Ven00977a52007-02-12 00:55:34 -08003664static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003665 .read = generic_read_dir,
3666 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003667 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003668};