blob: 662ddf2ec4f1ad7060f49d07ab1d26ebc631c72d [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
721static ssize_t mem_read(struct file * file, char __user * buf,
722 size_t count, loff_t *ppos)
723{
Linus Torvaldse2683372012-01-17 15:21:19 -0800724 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 char *page;
726 unsigned long src = *ppos;
Linus Torvaldse2683372012-01-17 15:21:19 -0800727 struct mm_struct *mm = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Linus Torvaldse2683372012-01-17 15:21:19 -0800729 if (!mm)
730 return 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -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
736 ret = 0;
737
738 while (count > 0) {
739 int this_len, retval;
740
741 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400742 retval = access_remote_vm(mm, src, page, this_len, 0);
743 if (!retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!ret)
745 ret = -EIO;
746 break;
747 }
748
749 if (copy_to_user(buf, page, retval)) {
750 ret = -EFAULT;
751 break;
752 }
753
754 ret += retval;
755 src += retval;
756 buf += retval;
757 count -= retval;
758 }
759 *ppos = src;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return ret;
763}
764
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800765static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 size_t count, loff_t *ppos)
767{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700768 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 unsigned long dst = *ppos;
Linus Torvaldse2683372012-01-17 15:21:19 -0800771 struct mm_struct *mm = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Linus Torvaldse2683372012-01-17 15:21:19 -0800773 if (!mm)
774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Mel Gormane12ba742007-10-16 01:25:52 -0700776 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800778 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700780 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 while (count > 0) {
782 int this_len, retval;
783
784 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
785 if (copy_from_user(page, buf, this_len)) {
786 copied = -EFAULT;
787 break;
788 }
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400789 retval = access_remote_vm(mm, dst, page, this_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (!retval) {
791 if (!copied)
792 copied = -EIO;
793 break;
794 }
795 copied += retval;
796 buf += retval;
797 dst += retval;
798 count -= retval;
799 }
800 *ppos = dst;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700801
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700802 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return copied;
804}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Matt Mackall85863e42008-02-04 22:29:04 -0800806loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 switch (orig) {
809 case 0:
810 file->f_pos = offset;
811 break;
812 case 1:
813 file->f_pos += offset;
814 break;
815 default:
816 return -EINVAL;
817 }
818 force_successful_syscall_return();
819 return file->f_pos;
820}
821
Linus Torvaldse2683372012-01-17 15:21:19 -0800822static int mem_release(struct inode *inode, struct file *file)
823{
824 struct mm_struct *mm = file->private_data;
825
826 mmput(mm);
827 return 0;
828}
829
Arjan van de Ven00977a52007-02-12 00:55:34 -0800830static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .llseek = mem_lseek,
832 .read = mem_read,
833 .write = mem_write,
834 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800835 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836};
837
James Pearson315e28c2007-10-16 23:30:17 -0700838static ssize_t environ_read(struct file *file, char __user *buf,
839 size_t count, loff_t *ppos)
840{
841 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
842 char *page;
843 unsigned long src = *ppos;
844 int ret = -ESRCH;
845 struct mm_struct *mm;
846
847 if (!task)
848 goto out_no_task;
849
James Pearson315e28c2007-10-16 23:30:17 -0700850 ret = -ENOMEM;
851 page = (char *)__get_free_page(GFP_TEMPORARY);
852 if (!page)
853 goto out;
854
James Pearson315e28c2007-10-16 23:30:17 -0700855
Al Virod6f64b82011-02-15 22:26:01 -0500856 mm = mm_for_maps(task);
857 ret = PTR_ERR(mm);
858 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700859 goto out_free;
860
Al Virod6f64b82011-02-15 22:26:01 -0500861 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -0700862 while (count > 0) {
863 int this_len, retval, max_len;
864
865 this_len = mm->env_end - (mm->env_start + src);
866
867 if (this_len <= 0)
868 break;
869
870 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
871 this_len = (this_len > max_len) ? max_len : this_len;
872
873 retval = access_process_vm(task, (mm->env_start + src),
874 page, this_len, 0);
875
876 if (retval <= 0) {
877 ret = retval;
878 break;
879 }
880
881 if (copy_to_user(buf, page, retval)) {
882 ret = -EFAULT;
883 break;
884 }
885
886 ret += retval;
887 src += retval;
888 buf += retval;
889 count -= retval;
890 }
891 *ppos = src;
892
893 mmput(mm);
894out_free:
895 free_page((unsigned long) page);
896out:
897 put_task_struct(task);
898out_no_task:
899 return ret;
900}
901
902static const struct file_operations proc_environ_operations = {
903 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100904 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700905};
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static ssize_t oom_adjust_read(struct file *file, char __user *buf,
908 size_t count, loff_t *ppos)
909{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800910 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700911 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700913 int oom_adjust = OOM_DISABLE;
914 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Eric W. Biederman99f89552006-06-26 00:25:55 -0700916 if (!task)
917 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700918
919 if (lock_task_sighand(task, &flags)) {
920 oom_adjust = task->signal->oom_adj;
921 unlock_task_sighand(task, &flags);
922 }
923
Eric W. Biederman99f89552006-06-26 00:25:55 -0700924 put_task_struct(task);
925
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700926 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700927
928 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
932 size_t count, loff_t *ppos)
933{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700934 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700935 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700936 int oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700937 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700938 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700940 memset(buffer, 0, sizeof(buffer));
941 if (count > sizeof(buffer) - 1)
942 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700943 if (copy_from_user(buffer, buf, count)) {
944 err = -EFAULT;
945 goto out;
946 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700947
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700948 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700949 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700950 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700951 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -0700952 oom_adjust != OOM_DISABLE) {
953 err = -EINVAL;
954 goto out;
955 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -0700956
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800957 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -0700958 if (!task) {
959 err = -ESRCH;
960 goto out;
961 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700962
Ying Han3d5992d2010-10-26 14:21:23 -0700963 task_lock(task);
964 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -0700965 err = -EINVAL;
966 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -0700967 }
968
David Rientjesd19d5472010-10-26 14:21:26 -0700969 if (!lock_task_sighand(task, &flags)) {
970 err = -ESRCH;
971 goto err_task_lock;
972 }
973
974 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
975 err = -EACCES;
976 goto err_sighand;
977 }
978
David Rientjes51b1bd22010-08-09 17:19:47 -0700979 /*
980 * Warn that /proc/pid/oom_adj is deprecated, see
981 * Documentation/feature-removal-schedule.txt.
982 */
Linus Torvaldsc2142702011-08-06 11:43:08 -0700983 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 -0700984 current->comm, task_pid_nr(current), task_pid_nr(task),
985 task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700986 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -0700987 /*
988 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
989 * value is always attainable.
990 */
991 if (task->signal->oom_adj == OOM_ADJUST_MAX)
992 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
993 else
994 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
995 -OOM_DISABLE;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800996 trace_oom_score_adj_update(task);
David Rientjes723548b2010-10-26 14:21:25 -0700997err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700998 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -0700999err_task_lock:
1000 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001001 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001002out:
1003 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Arjan van de Ven00977a52007-02-12 00:55:34 -08001006static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 .read = oom_adjust_read,
1008 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001009 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010};
1011
David Rientjesa63d83f2010-08-09 17:19:46 -07001012static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1013 size_t count, loff_t *ppos)
1014{
1015 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1016 char buffer[PROC_NUMBUF];
1017 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1018 unsigned long flags;
1019 size_t len;
1020
1021 if (!task)
1022 return -ESRCH;
1023 if (lock_task_sighand(task, &flags)) {
1024 oom_score_adj = task->signal->oom_score_adj;
1025 unlock_task_sighand(task, &flags);
1026 }
1027 put_task_struct(task);
1028 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1029 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1030}
1031
1032static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1033 size_t count, loff_t *ppos)
1034{
1035 struct task_struct *task;
1036 char buffer[PROC_NUMBUF];
1037 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001038 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001039 int err;
1040
1041 memset(buffer, 0, sizeof(buffer));
1042 if (count > sizeof(buffer) - 1)
1043 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001044 if (copy_from_user(buffer, buf, count)) {
1045 err = -EFAULT;
1046 goto out;
1047 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001048
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001049 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001050 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001051 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001052 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001053 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1054 err = -EINVAL;
1055 goto out;
1056 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001057
1058 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001059 if (!task) {
1060 err = -ESRCH;
1061 goto out;
1062 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001063
Ying Han3d5992d2010-10-26 14:21:23 -07001064 task_lock(task);
1065 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001066 err = -EINVAL;
1067 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001068 }
David Rientjesd19d5472010-10-26 14:21:26 -07001069
1070 if (!lock_task_sighand(task, &flags)) {
1071 err = -ESRCH;
1072 goto err_task_lock;
1073 }
1074
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001075 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001076 !capable(CAP_SYS_RESOURCE)) {
1077 err = -EACCES;
1078 goto err_sighand;
1079 }
1080
David Rientjesa63d83f2010-08-09 17:19:46 -07001081 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001082 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1083 task->signal->oom_score_adj_min = oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001084 trace_oom_score_adj_update(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001085 /*
1086 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1087 * always attainable.
1088 */
1089 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1090 task->signal->oom_adj = OOM_DISABLE;
1091 else
1092 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1093 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001094err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001095 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001096err_task_lock:
1097 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001098 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001099out:
1100 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001101}
1102
1103static const struct file_operations proc_oom_score_adj_operations = {
1104 .read = oom_score_adj_read,
1105 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001106 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001107};
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109#ifdef CONFIG_AUDITSYSCALL
1110#define TMPBUFLEN 21
1111static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1112 size_t count, loff_t *ppos)
1113{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001114 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001115 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ssize_t length;
1117 char tmpbuf[TMPBUFLEN];
1118
Eric W. Biederman99f89552006-06-26 00:25:55 -07001119 if (!task)
1120 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001122 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001123 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1125}
1126
1127static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1128 size_t count, loff_t *ppos)
1129{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001130 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 char *page, *tmp;
1132 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 uid_t loginuid;
1134
1135 if (!capable(CAP_AUDIT_CONTROL))
1136 return -EPERM;
1137
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001138 rcu_read_lock();
1139 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1140 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001142 }
1143 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Al Viroe0182902006-05-18 08:28:02 -04001145 if (count >= PAGE_SIZE)
1146 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 if (*ppos != 0) {
1149 /* No partial writes. */
1150 return -EINVAL;
1151 }
Mel Gormane12ba742007-10-16 01:25:52 -07001152 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!page)
1154 return -ENOMEM;
1155 length = -EFAULT;
1156 if (copy_from_user(page, buf, count))
1157 goto out_free_page;
1158
Al Viroe0182902006-05-18 08:28:02 -04001159 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 loginuid = simple_strtoul(page, &tmp, 10);
1161 if (tmp == page) {
1162 length = -EINVAL;
1163 goto out_free_page;
1164
1165 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001166 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (likely(length == 0))
1168 length = count;
1169
1170out_free_page:
1171 free_page((unsigned long) page);
1172 return length;
1173}
1174
Arjan van de Ven00977a52007-02-12 00:55:34 -08001175static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 .read = proc_loginuid_read,
1177 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001178 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179};
Eric Paris1e0bd752008-03-13 08:15:31 -04001180
1181static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1182 size_t count, loff_t *ppos)
1183{
1184 struct inode * inode = file->f_path.dentry->d_inode;
1185 struct task_struct *task = get_proc_task(inode);
1186 ssize_t length;
1187 char tmpbuf[TMPBUFLEN];
1188
1189 if (!task)
1190 return -ESRCH;
1191 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1192 audit_get_sessionid(task));
1193 put_task_struct(task);
1194 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1195}
1196
1197static const struct file_operations proc_sessionid_operations = {
1198 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001199 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001200};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#endif
1202
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001203#ifdef CONFIG_FAULT_INJECTION
1204static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1205 size_t count, loff_t *ppos)
1206{
1207 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1208 char buffer[PROC_NUMBUF];
1209 size_t len;
1210 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001211
1212 if (!task)
1213 return -ESRCH;
1214 make_it_fail = task->make_it_fail;
1215 put_task_struct(task);
1216
1217 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001218
1219 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001220}
1221
1222static ssize_t proc_fault_inject_write(struct file * file,
1223 const char __user * buf, size_t count, loff_t *ppos)
1224{
1225 struct task_struct *task;
1226 char buffer[PROC_NUMBUF], *end;
1227 int make_it_fail;
1228
1229 if (!capable(CAP_SYS_RESOURCE))
1230 return -EPERM;
1231 memset(buffer, 0, sizeof(buffer));
1232 if (count > sizeof(buffer) - 1)
1233 count = sizeof(buffer) - 1;
1234 if (copy_from_user(buffer, buf, count))
1235 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001236 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1237 if (*end)
1238 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001239 task = get_proc_task(file->f_dentry->d_inode);
1240 if (!task)
1241 return -ESRCH;
1242 task->make_it_fail = make_it_fail;
1243 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001244
1245 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001246}
1247
Arjan van de Ven00977a52007-02-12 00:55:34 -08001248static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001249 .read = proc_fault_inject_read,
1250 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001251 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001252};
1253#endif
1254
Arjan van de Ven97455122008-01-25 21:08:34 +01001255
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001256#ifdef CONFIG_SCHED_DEBUG
1257/*
1258 * Print out various scheduling related per-task fields:
1259 */
1260static int sched_show(struct seq_file *m, void *v)
1261{
1262 struct inode *inode = m->private;
1263 struct task_struct *p;
1264
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001265 p = get_proc_task(inode);
1266 if (!p)
1267 return -ESRCH;
1268 proc_sched_show_task(p, m);
1269
1270 put_task_struct(p);
1271
1272 return 0;
1273}
1274
1275static ssize_t
1276sched_write(struct file *file, const char __user *buf,
1277 size_t count, loff_t *offset)
1278{
1279 struct inode *inode = file->f_path.dentry->d_inode;
1280 struct task_struct *p;
1281
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001282 p = get_proc_task(inode);
1283 if (!p)
1284 return -ESRCH;
1285 proc_sched_set_task(p);
1286
1287 put_task_struct(p);
1288
1289 return count;
1290}
1291
1292static int sched_open(struct inode *inode, struct file *filp)
1293{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001294 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001295}
1296
1297static const struct file_operations proc_pid_sched_operations = {
1298 .open = sched_open,
1299 .read = seq_read,
1300 .write = sched_write,
1301 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001302 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001303};
1304
1305#endif
1306
Mike Galbraith5091faa2010-11-30 14:18:03 +01001307#ifdef CONFIG_SCHED_AUTOGROUP
1308/*
1309 * Print out autogroup related information:
1310 */
1311static int sched_autogroup_show(struct seq_file *m, void *v)
1312{
1313 struct inode *inode = m->private;
1314 struct task_struct *p;
1315
1316 p = get_proc_task(inode);
1317 if (!p)
1318 return -ESRCH;
1319 proc_sched_autogroup_show_task(p, m);
1320
1321 put_task_struct(p);
1322
1323 return 0;
1324}
1325
1326static ssize_t
1327sched_autogroup_write(struct file *file, const char __user *buf,
1328 size_t count, loff_t *offset)
1329{
1330 struct inode *inode = file->f_path.dentry->d_inode;
1331 struct task_struct *p;
1332 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001333 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001334 int err;
1335
1336 memset(buffer, 0, sizeof(buffer));
1337 if (count > sizeof(buffer) - 1)
1338 count = sizeof(buffer) - 1;
1339 if (copy_from_user(buffer, buf, count))
1340 return -EFAULT;
1341
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001342 err = kstrtoint(strstrip(buffer), 0, &nice);
1343 if (err < 0)
1344 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001345
1346 p = get_proc_task(inode);
1347 if (!p)
1348 return -ESRCH;
1349
1350 err = nice;
1351 err = proc_sched_autogroup_set_nice(p, &err);
1352 if (err)
1353 count = err;
1354
1355 put_task_struct(p);
1356
1357 return count;
1358}
1359
1360static int sched_autogroup_open(struct inode *inode, struct file *filp)
1361{
1362 int ret;
1363
1364 ret = single_open(filp, sched_autogroup_show, NULL);
1365 if (!ret) {
1366 struct seq_file *m = filp->private_data;
1367
1368 m->private = inode;
1369 }
1370 return ret;
1371}
1372
1373static const struct file_operations proc_pid_sched_autogroup_operations = {
1374 .open = sched_autogroup_open,
1375 .read = seq_read,
1376 .write = sched_autogroup_write,
1377 .llseek = seq_lseek,
1378 .release = single_release,
1379};
1380
1381#endif /* CONFIG_SCHED_AUTOGROUP */
1382
john stultz4614a696b2009-12-14 18:00:05 -08001383static ssize_t comm_write(struct file *file, const char __user *buf,
1384 size_t count, loff_t *offset)
1385{
1386 struct inode *inode = file->f_path.dentry->d_inode;
1387 struct task_struct *p;
1388 char buffer[TASK_COMM_LEN];
1389
1390 memset(buffer, 0, sizeof(buffer));
1391 if (count > sizeof(buffer) - 1)
1392 count = sizeof(buffer) - 1;
1393 if (copy_from_user(buffer, buf, count))
1394 return -EFAULT;
1395
1396 p = get_proc_task(inode);
1397 if (!p)
1398 return -ESRCH;
1399
1400 if (same_thread_group(current, p))
1401 set_task_comm(p, buffer);
1402 else
1403 count = -EINVAL;
1404
1405 put_task_struct(p);
1406
1407 return count;
1408}
1409
1410static int comm_show(struct seq_file *m, void *v)
1411{
1412 struct inode *inode = m->private;
1413 struct task_struct *p;
1414
1415 p = get_proc_task(inode);
1416 if (!p)
1417 return -ESRCH;
1418
1419 task_lock(p);
1420 seq_printf(m, "%s\n", p->comm);
1421 task_unlock(p);
1422
1423 put_task_struct(p);
1424
1425 return 0;
1426}
1427
1428static int comm_open(struct inode *inode, struct file *filp)
1429{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001430 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001431}
1432
1433static const struct file_operations proc_pid_set_comm_operations = {
1434 .open = comm_open,
1435 .read = seq_read,
1436 .write = comm_write,
1437 .llseek = seq_lseek,
1438 .release = single_release,
1439};
1440
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001441static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001442{
1443 struct task_struct *task;
1444 struct mm_struct *mm;
1445 struct file *exe_file;
1446
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001447 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001448 if (!task)
1449 return -ENOENT;
1450 mm = get_task_mm(task);
1451 put_task_struct(task);
1452 if (!mm)
1453 return -ENOENT;
1454 exe_file = get_mm_exe_file(mm);
1455 mmput(mm);
1456 if (exe_file) {
1457 *exe_path = exe_file->f_path;
1458 path_get(&exe_file->f_path);
1459 fput(exe_file);
1460 return 0;
1461 } else
1462 return -ENOENT;
1463}
1464
Al Viro008b1502005-08-20 00:17:39 +01001465static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct inode *inode = dentry->d_inode;
1468 int error = -EACCES;
1469
1470 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001471 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Eric W. Biederman778c1142006-06-26 00:25:58 -07001473 /* Are we allowed to snoop on the tasks file descriptors? */
1474 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001477 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478out:
Al Viro008b1502005-08-20 00:17:39 +01001479 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001482static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Mel Gormane12ba742007-10-16 01:25:52 -07001484 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001485 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int len;
1487
1488 if (!tmp)
1489 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001490
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001491 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001492 len = PTR_ERR(pathname);
1493 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001495 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 if (len > buflen)
1498 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001499 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 len = -EFAULT;
1501 out:
1502 free_page((unsigned long)tmp);
1503 return len;
1504}
1505
1506static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1507{
1508 int error = -EACCES;
1509 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001510 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Eric W. Biederman778c1142006-06-26 00:25:58 -07001512 /* Are we allowed to snoop on the tasks file descriptors? */
1513 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001516 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (error)
1518 goto out;
1519
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001520 error = do_proc_readlink(&path, buffer, buflen);
1521 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return error;
1524}
1525
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001526static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001528 .follow_link = proc_pid_follow_link,
1529 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530};
1531
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001532
1533/* building an inode */
1534
1535static int task_dumpable(struct task_struct *task)
1536{
1537 int dumpable = 0;
1538 struct mm_struct *mm;
1539
1540 task_lock(task);
1541 mm = task->mm;
1542 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001543 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001544 task_unlock(task);
1545 if(dumpable == 1)
1546 return 1;
1547 return 0;
1548}
1549
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001550struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001551{
1552 struct inode * inode;
1553 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001554 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001555
1556 /* We need a new inode */
1557
1558 inode = new_inode(sb);
1559 if (!inode)
1560 goto out;
1561
1562 /* Common stuff */
1563 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001564 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001565 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001566 inode->i_op = &proc_def_inode_operations;
1567
1568 /*
1569 * grab the reference to task.
1570 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001571 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001572 if (!ei->pid)
1573 goto out_unlock;
1574
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001575 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001576 rcu_read_lock();
1577 cred = __task_cred(task);
1578 inode->i_uid = cred->euid;
1579 inode->i_gid = cred->egid;
1580 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001581 }
1582 security_task_to_inode(task, inode);
1583
1584out:
1585 return inode;
1586
1587out_unlock:
1588 iput(inode);
1589 return NULL;
1590}
1591
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001592int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001593{
1594 struct inode *inode = dentry->d_inode;
1595 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001596 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001597 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001598
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001599 generic_fillattr(inode, stat);
1600
1601 rcu_read_lock();
1602 stat->uid = 0;
1603 stat->gid = 0;
1604 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1605 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001606 if (!has_pid_permissions(pid, task, 2)) {
1607 rcu_read_unlock();
1608 /*
1609 * This doesn't prevent learning whether PID exists,
1610 * it only makes getattr() consistent with readdir().
1611 */
1612 return -ENOENT;
1613 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001614 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1615 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001616 cred = __task_cred(task);
1617 stat->uid = cred->euid;
1618 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001619 }
1620 }
1621 rcu_read_unlock();
1622 return 0;
1623}
1624
1625/* dentry stuff */
1626
1627/*
1628 * Exceptional case: normally we are not allowed to unhash a busy
1629 * directory. In this case, however, we can do it - no aliasing problems
1630 * due to the way we treat inodes.
1631 *
1632 * Rewrite the inode's ownerships here because the owning task may have
1633 * performed a setuid(), etc.
1634 *
1635 * Before the /proc/pid/status file was created the only way to read
1636 * the effective uid of a /process was to stat /proc/pid. Reading
1637 * /proc/pid/status is slow enough that procps and other packages
1638 * kept stating /proc/pid. To keep the rules in /proc simple I have
1639 * made this apply to all per process world readable and executable
1640 * directories.
1641 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001642int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001643{
Nick Piggin34286d62011-01-07 17:49:57 +11001644 struct inode *inode;
1645 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001646 const struct cred *cred;
1647
Nick Piggin34286d62011-01-07 17:49:57 +11001648 if (nd && nd->flags & LOOKUP_RCU)
1649 return -ECHILD;
1650
1651 inode = dentry->d_inode;
1652 task = get_proc_task(inode);
1653
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001654 if (task) {
1655 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1656 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001657 rcu_read_lock();
1658 cred = __task_cred(task);
1659 inode->i_uid = cred->euid;
1660 inode->i_gid = cred->egid;
1661 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001662 } else {
1663 inode->i_uid = 0;
1664 inode->i_gid = 0;
1665 }
1666 inode->i_mode &= ~(S_ISUID | S_ISGID);
1667 security_task_to_inode(task, inode);
1668 put_task_struct(task);
1669 return 1;
1670 }
1671 d_drop(dentry);
1672 return 0;
1673}
1674
Nick Pigginfe15ce42011-01-07 17:49:23 +11001675static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001676{
1677 /* Is the task we represent dead?
1678 * If so, then don't put the dentry on the lru list,
1679 * kill it immediately.
1680 */
1681 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1682}
1683
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001684const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001685{
1686 .d_revalidate = pid_revalidate,
1687 .d_delete = pid_delete_dentry,
1688};
1689
1690/* Lookups */
1691
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001692/*
1693 * Fill a directory entry.
1694 *
1695 * If possible create the dcache entry and derive our inode number and
1696 * file type from dcache entry.
1697 *
1698 * Since all of the proc inode numbers are dynamically generated, the inode
1699 * numbers do not exist until the inode is cache. This means creating the
1700 * the dcache entry in readdir is necessary to keep the inode numbers
1701 * reported by readdir in sync with the inode numbers reported
1702 * by stat.
1703 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001704int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1705 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001706 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001707{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001708 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001709 struct inode *inode;
1710 struct qstr qname;
1711 ino_t ino = 0;
1712 unsigned type = DT_UNKNOWN;
1713
1714 qname.name = name;
1715 qname.len = len;
1716 qname.hash = full_name_hash(name, len);
1717
1718 child = d_lookup(dir, &qname);
1719 if (!child) {
1720 struct dentry *new;
1721 new = d_alloc(dir, &qname);
1722 if (new) {
1723 child = instantiate(dir->d_inode, new, task, ptr);
1724 if (child)
1725 dput(new);
1726 else
1727 child = new;
1728 }
1729 }
1730 if (!child || IS_ERR(child) || !child->d_inode)
1731 goto end_instantiate;
1732 inode = child->d_inode;
1733 if (inode) {
1734 ino = inode->i_ino;
1735 type = inode->i_mode >> 12;
1736 }
1737 dput(child);
1738end_instantiate:
1739 if (!ino)
1740 ino = find_inode_number(dir, &qname);
1741 if (!ino)
1742 ino = 1;
1743 return filldir(dirent, name, len, filp->f_pos, ino, type);
1744}
1745
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001746static unsigned name_to_int(struct dentry *dentry)
1747{
1748 const char *name = dentry->d_name.name;
1749 int len = dentry->d_name.len;
1750 unsigned n = 0;
1751
1752 if (len > 1 && *name == '0')
1753 goto out;
1754 while (len-- > 0) {
1755 unsigned c = *name++ - '0';
1756 if (c > 9)
1757 goto out;
1758 if (n >= (~0U-9)/10)
1759 goto out;
1760 n *= 10;
1761 n += c;
1762 }
1763 return n;
1764out:
1765 return ~0U;
1766}
1767
Miklos Szeredi27932742007-05-08 00:26:17 -07001768#define PROC_FDINFO_MAX 64
1769
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001770static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001771{
Linus Torvalds5e442a42011-11-09 18:16:00 -05001772 struct task_struct *task = get_proc_task(inode);
1773 struct files_struct *files = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001774 struct file *file;
1775 int fd = proc_fd(inode);
1776
Linus Torvalds5e442a42011-11-09 18:16:00 -05001777 if (task) {
1778 files = get_files_struct(task);
1779 put_task_struct(task);
1780 }
1781 if (files) {
1782 /*
1783 * We are not taking a ref to the file structure, so we must
1784 * hold ->file_lock.
1785 */
1786 spin_lock(&files->file_lock);
1787 file = fcheck_files(files, fd);
1788 if (file) {
1789 unsigned int f_flags;
1790 struct fdtable *fdt;
Linus Torvalds1117f722011-08-06 11:51:33 -07001791
Linus Torvalds5e442a42011-11-09 18:16:00 -05001792 fdt = files_fdtable(files);
1793 f_flags = file->f_flags & ~O_CLOEXEC;
1794 if (FD_ISSET(fd, fdt->close_on_exec))
1795 f_flags |= O_CLOEXEC;
Linus Torvalds1117f722011-08-06 11:51:33 -07001796
Linus Torvalds5e442a42011-11-09 18:16:00 -05001797 if (path) {
1798 *path = file->f_path;
1799 path_get(&file->f_path);
1800 }
1801 if (info)
1802 snprintf(info, PROC_FDINFO_MAX,
1803 "pos:\t%lli\n"
1804 "flags:\t0%o\n",
1805 (long long) file->f_pos,
1806 f_flags);
1807 spin_unlock(&files->file_lock);
1808 put_files_struct(files);
1809 return 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001810 }
Linus Torvalds5e442a42011-11-09 18:16:00 -05001811 spin_unlock(&files->file_lock);
1812 put_files_struct(files);
1813 }
1814 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001815}
1816
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001817static int proc_fd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001818{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001819 return proc_fd_info(dentry->d_inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001820}
1821
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001822static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1823{
Nick Piggin34286d62011-01-07 17:49:57 +11001824 struct inode *inode;
1825 struct task_struct *task;
1826 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001827 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001828 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001829
Nick Piggin34286d62011-01-07 17:49:57 +11001830 if (nd && nd->flags & LOOKUP_RCU)
1831 return -ECHILD;
1832
1833 inode = dentry->d_inode;
1834 task = get_proc_task(inode);
1835 fd = proc_fd(inode);
1836
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001837 if (task) {
1838 files = get_files_struct(task);
1839 if (files) {
1840 rcu_read_lock();
1841 if (fcheck_files(files, fd)) {
1842 rcu_read_unlock();
1843 put_files_struct(files);
1844 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001845 rcu_read_lock();
1846 cred = __task_cred(task);
1847 inode->i_uid = cred->euid;
1848 inode->i_gid = cred->egid;
1849 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001850 } else {
1851 inode->i_uid = 0;
1852 inode->i_gid = 0;
1853 }
1854 inode->i_mode &= ~(S_ISUID | S_ISGID);
1855 security_task_to_inode(task, inode);
1856 put_task_struct(task);
1857 return 1;
1858 }
1859 rcu_read_unlock();
1860 put_files_struct(files);
1861 }
1862 put_task_struct(task);
1863 }
1864 d_drop(dentry);
1865 return 0;
1866}
1867
Al Virod72f71e2009-02-20 05:58:47 +00001868static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001869{
1870 .d_revalidate = tid_fd_revalidate,
1871 .d_delete = pid_delete_dentry,
1872};
1873
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001874static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001875 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001876{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001877 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001878 struct file *file;
1879 struct files_struct *files;
1880 struct inode *inode;
1881 struct proc_inode *ei;
1882 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001883
Eric W. Biederman61a28782006-10-02 02:18:49 -07001884 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001885 if (!inode)
1886 goto out;
1887 ei = PROC_I(inode);
1888 ei->fd = fd;
1889 files = get_files_struct(task);
1890 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001891 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001892 inode->i_mode = S_IFLNK;
1893
1894 /*
1895 * We are not taking a ref to the file structure, so we must
1896 * hold ->file_lock.
1897 */
1898 spin_lock(&files->file_lock);
1899 file = fcheck_files(files, fd);
1900 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001901 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001902 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001903 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001904 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001905 inode->i_mode |= S_IWUSR | S_IXUSR;
1906 spin_unlock(&files->file_lock);
1907 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001908
Linus Torvalds5e442a42011-11-09 18:16:00 -05001909 inode->i_op = &proc_pid_link_inode_operations;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001910 inode->i_size = 64;
1911 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11001912 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001913 d_add(dentry, inode);
1914 /* Close the race of the process dying before we return the dentry */
1915 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001916 error = NULL;
1917
1918 out:
1919 return error;
1920out_unlock:
1921 spin_unlock(&files->file_lock);
1922 put_files_struct(files);
1923out_iput:
1924 iput(inode);
1925 goto out;
1926}
1927
Miklos Szeredi27932742007-05-08 00:26:17 -07001928static struct dentry *proc_lookupfd_common(struct inode *dir,
1929 struct dentry *dentry,
1930 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001931{
1932 struct task_struct *task = get_proc_task(dir);
1933 unsigned fd = name_to_int(dentry);
1934 struct dentry *result = ERR_PTR(-ENOENT);
1935
1936 if (!task)
1937 goto out_no_task;
1938 if (fd == ~0U)
1939 goto out;
1940
Miklos Szeredi27932742007-05-08 00:26:17 -07001941 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001942out:
1943 put_task_struct(task);
1944out_no_task:
1945 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001946}
1947
Miklos Szeredi27932742007-05-08 00:26:17 -07001948static int proc_readfd_common(struct file * filp, void * dirent,
1949 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001951 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001952 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001953 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001954 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 struct files_struct * files;
1957
1958 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001959 if (!p)
1960 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 fd = filp->f_pos;
1964 switch (fd) {
1965 case 0:
1966 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
Linus Torvalds5e442a42011-11-09 18:16:00 -05001967 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 filp->f_pos++;
1969 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001970 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Linus Torvalds5e442a42011-11-09 18:16:00 -05001972 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 filp->f_pos++;
1974 default:
1975 files = get_files_struct(p);
1976 if (!files)
Linus Torvalds5e442a42011-11-09 18:16:00 -05001977 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001978 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001980 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001982 char name[PROC_NUMBUF];
1983 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 if (!fcheck_files(files, fd))
1986 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001987 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Miklos Szeredi27932742007-05-08 00:26:17 -07001989 len = snprintf(name, sizeof(name), "%d", fd);
1990 if (proc_fill_cache(filp, dirent, filldir,
1991 name, len, instantiate,
1992 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001993 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 break;
1995 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001996 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001998 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 put_files_struct(files);
2000 }
2001out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002002 put_task_struct(p);
2003out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return retval;
2005}
2006
Miklos Szeredi27932742007-05-08 00:26:17 -07002007static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2008 struct nameidata *nd)
2009{
2010 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2011}
2012
2013static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2014{
2015 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2016}
2017
2018static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2019 size_t len, loff_t *ppos)
2020{
2021 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002022 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002023 if (!err)
2024 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2025 return err;
2026}
2027
2028static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002029 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002030 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002031 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002032};
2033
Arjan van de Ven00977a52007-02-12 00:55:34 -08002034static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 .read = generic_read_dir,
2036 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002037 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038};
2039
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002040#ifdef CONFIG_CHECKPOINT_RESTORE
2041
2042/*
2043 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2044 * which represent vma start and end addresses.
2045 */
2046static int dname_to_vma_addr(struct dentry *dentry,
2047 unsigned long *start, unsigned long *end)
2048{
2049 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2050 return -EINVAL;
2051
2052 return 0;
2053}
2054
2055static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2056{
2057 unsigned long vm_start, vm_end;
2058 bool exact_vma_exists = false;
2059 struct mm_struct *mm = NULL;
2060 struct task_struct *task;
2061 const struct cred *cred;
2062 struct inode *inode;
2063 int status = 0;
2064
2065 if (nd && nd->flags & LOOKUP_RCU)
2066 return -ECHILD;
2067
2068 if (!capable(CAP_SYS_ADMIN)) {
2069 status = -EACCES;
2070 goto out_notask;
2071 }
2072
2073 inode = dentry->d_inode;
2074 task = get_proc_task(inode);
2075 if (!task)
2076 goto out_notask;
2077
2078 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2079 goto out;
2080
2081 mm = get_task_mm(task);
2082 if (!mm)
2083 goto out;
2084
2085 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2086 down_read(&mm->mmap_sem);
2087 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2088 up_read(&mm->mmap_sem);
2089 }
2090
2091 mmput(mm);
2092
2093 if (exact_vma_exists) {
2094 if (task_dumpable(task)) {
2095 rcu_read_lock();
2096 cred = __task_cred(task);
2097 inode->i_uid = cred->euid;
2098 inode->i_gid = cred->egid;
2099 rcu_read_unlock();
2100 } else {
2101 inode->i_uid = 0;
2102 inode->i_gid = 0;
2103 }
2104 security_task_to_inode(task, inode);
2105 status = 1;
2106 }
2107
2108out:
2109 put_task_struct(task);
2110
2111out_notask:
2112 if (status <= 0)
2113 d_drop(dentry);
2114
2115 return status;
2116}
2117
2118static const struct dentry_operations tid_map_files_dentry_operations = {
2119 .d_revalidate = map_files_d_revalidate,
2120 .d_delete = pid_delete_dentry,
2121};
2122
2123static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2124{
2125 unsigned long vm_start, vm_end;
2126 struct vm_area_struct *vma;
2127 struct task_struct *task;
2128 struct mm_struct *mm;
2129 int rc;
2130
2131 rc = -ENOENT;
2132 task = get_proc_task(dentry->d_inode);
2133 if (!task)
2134 goto out;
2135
2136 mm = get_task_mm(task);
2137 put_task_struct(task);
2138 if (!mm)
2139 goto out;
2140
2141 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2142 if (rc)
2143 goto out_mmput;
2144
2145 down_read(&mm->mmap_sem);
2146 vma = find_exact_vma(mm, vm_start, vm_end);
2147 if (vma && vma->vm_file) {
2148 *path = vma->vm_file->f_path;
2149 path_get(path);
2150 rc = 0;
2151 }
2152 up_read(&mm->mmap_sem);
2153
2154out_mmput:
2155 mmput(mm);
2156out:
2157 return rc;
2158}
2159
2160struct map_files_info {
2161 struct file *file;
2162 unsigned long len;
2163 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2164};
2165
2166static struct dentry *
2167proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2168 struct task_struct *task, const void *ptr)
2169{
2170 const struct file *file = ptr;
2171 struct proc_inode *ei;
2172 struct inode *inode;
2173
2174 if (!file)
2175 return ERR_PTR(-ENOENT);
2176
2177 inode = proc_pid_make_inode(dir->i_sb, task);
2178 if (!inode)
2179 return ERR_PTR(-ENOENT);
2180
2181 ei = PROC_I(inode);
2182 ei->op.proc_get_link = proc_map_files_get_link;
2183
2184 inode->i_op = &proc_pid_link_inode_operations;
2185 inode->i_size = 64;
2186 inode->i_mode = S_IFLNK;
2187
2188 if (file->f_mode & FMODE_READ)
2189 inode->i_mode |= S_IRUSR;
2190 if (file->f_mode & FMODE_WRITE)
2191 inode->i_mode |= S_IWUSR;
2192
2193 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2194 d_add(dentry, inode);
2195
2196 return NULL;
2197}
2198
2199static struct dentry *proc_map_files_lookup(struct inode *dir,
2200 struct dentry *dentry, struct nameidata *nd)
2201{
2202 unsigned long vm_start, vm_end;
2203 struct vm_area_struct *vma;
2204 struct task_struct *task;
2205 struct dentry *result;
2206 struct mm_struct *mm;
2207
2208 result = ERR_PTR(-EACCES);
2209 if (!capable(CAP_SYS_ADMIN))
2210 goto out;
2211
2212 result = ERR_PTR(-ENOENT);
2213 task = get_proc_task(dir);
2214 if (!task)
2215 goto out;
2216
2217 result = ERR_PTR(-EACCES);
2218 if (lock_trace(task))
2219 goto out_put_task;
2220
2221 result = ERR_PTR(-ENOENT);
2222 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2223 goto out_unlock;
2224
2225 mm = get_task_mm(task);
2226 if (!mm)
2227 goto out_unlock;
2228
2229 down_read(&mm->mmap_sem);
2230 vma = find_exact_vma(mm, vm_start, vm_end);
2231 if (!vma)
2232 goto out_no_vma;
2233
2234 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2235
2236out_no_vma:
2237 up_read(&mm->mmap_sem);
2238 mmput(mm);
2239out_unlock:
2240 unlock_trace(task);
2241out_put_task:
2242 put_task_struct(task);
2243out:
2244 return result;
2245}
2246
2247static const struct inode_operations proc_map_files_inode_operations = {
2248 .lookup = proc_map_files_lookup,
2249 .permission = proc_fd_permission,
2250 .setattr = proc_setattr,
2251};
2252
2253static int
2254proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2255{
2256 struct dentry *dentry = filp->f_path.dentry;
2257 struct inode *inode = dentry->d_inode;
2258 struct vm_area_struct *vma;
2259 struct task_struct *task;
2260 struct mm_struct *mm;
2261 ino_t ino;
2262 int ret;
2263
2264 ret = -EACCES;
2265 if (!capable(CAP_SYS_ADMIN))
2266 goto out;
2267
2268 ret = -ENOENT;
2269 task = get_proc_task(inode);
2270 if (!task)
2271 goto out;
2272
2273 ret = -EACCES;
2274 if (lock_trace(task))
2275 goto out_put_task;
2276
2277 ret = 0;
2278 switch (filp->f_pos) {
2279 case 0:
2280 ino = inode->i_ino;
2281 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2282 goto out_unlock;
2283 filp->f_pos++;
2284 case 1:
2285 ino = parent_ino(dentry);
2286 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2287 goto out_unlock;
2288 filp->f_pos++;
2289 default:
2290 {
2291 unsigned long nr_files, pos, i;
2292 struct flex_array *fa = NULL;
2293 struct map_files_info info;
2294 struct map_files_info *p;
2295
2296 mm = get_task_mm(task);
2297 if (!mm)
2298 goto out_unlock;
2299 down_read(&mm->mmap_sem);
2300
2301 nr_files = 0;
2302
2303 /*
2304 * We need two passes here:
2305 *
2306 * 1) Collect vmas of mapped files with mmap_sem taken
2307 * 2) Release mmap_sem and instantiate entries
2308 *
2309 * otherwise we get lockdep complained, since filldir()
2310 * routine might require mmap_sem taken in might_fault().
2311 */
2312
2313 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2314 if (vma->vm_file && ++pos > filp->f_pos)
2315 nr_files++;
2316 }
2317
2318 if (nr_files) {
2319 fa = flex_array_alloc(sizeof(info), nr_files,
2320 GFP_KERNEL);
2321 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2322 GFP_KERNEL)) {
2323 ret = -ENOMEM;
2324 if (fa)
2325 flex_array_free(fa);
2326 up_read(&mm->mmap_sem);
2327 mmput(mm);
2328 goto out_unlock;
2329 }
2330 for (i = 0, vma = mm->mmap, pos = 2; vma;
2331 vma = vma->vm_next) {
2332 if (!vma->vm_file)
2333 continue;
2334 if (++pos <= filp->f_pos)
2335 continue;
2336
2337 get_file(vma->vm_file);
2338 info.file = vma->vm_file;
2339 info.len = snprintf(info.name,
2340 sizeof(info.name), "%lx-%lx",
2341 vma->vm_start, vma->vm_end);
2342 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2343 BUG();
2344 }
2345 }
2346 up_read(&mm->mmap_sem);
2347
2348 for (i = 0; i < nr_files; i++) {
2349 p = flex_array_get(fa, i);
2350 ret = proc_fill_cache(filp, dirent, filldir,
2351 p->name, p->len,
2352 proc_map_files_instantiate,
2353 task, p->file);
2354 if (ret)
2355 break;
2356 filp->f_pos++;
2357 fput(p->file);
2358 }
2359 for (; i < nr_files; i++) {
2360 /*
2361 * In case of error don't forget
2362 * to put rest of file refs.
2363 */
2364 p = flex_array_get(fa, i);
2365 fput(p->file);
2366 }
2367 if (fa)
2368 flex_array_free(fa);
2369 mmput(mm);
2370 }
2371 }
2372
2373out_unlock:
2374 unlock_trace(task);
2375out_put_task:
2376 put_task_struct(task);
2377out:
2378 return ret;
2379}
2380
2381static const struct file_operations proc_map_files_operations = {
2382 .read = generic_read_dir,
2383 .readdir = proc_map_files_readdir,
2384 .llseek = default_llseek,
2385};
2386
2387#endif /* CONFIG_CHECKPOINT_RESTORE */
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002390 * /proc/pid/fd needs a special permission handler so that a process can still
2391 * access /proc/self/fd after it has executed a setuid().
2392 */
Al Viro10556cb2011-06-20 19:28:19 -04002393static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002394{
Al Viro2830ba72011-06-20 19:16:29 -04002395 int rv = generic_permission(inode, mask);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002396 if (rv == 0)
2397 return 0;
2398 if (task_pid(current) == proc_pid(inode))
2399 rv = 0;
2400 return rv;
2401}
2402
2403/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 * proc directories can do almost nothing..
2405 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002406static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002408 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002409 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410};
2411
Miklos Szeredi27932742007-05-08 00:26:17 -07002412static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2413 struct dentry *dentry, struct task_struct *task, const void *ptr)
2414{
2415 unsigned fd = *(unsigned *)ptr;
2416 struct inode *inode;
2417 struct proc_inode *ei;
2418 struct dentry *error = ERR_PTR(-ENOENT);
2419
2420 inode = proc_pid_make_inode(dir->i_sb, task);
2421 if (!inode)
2422 goto out;
2423 ei = PROC_I(inode);
2424 ei->fd = fd;
2425 inode->i_mode = S_IFREG | S_IRUSR;
2426 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002427 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002428 d_add(dentry, inode);
2429 /* Close the race of the process dying before we return the dentry */
2430 if (tid_fd_revalidate(dentry, NULL))
2431 error = NULL;
2432
2433 out:
2434 return error;
2435}
2436
2437static struct dentry *proc_lookupfdinfo(struct inode *dir,
2438 struct dentry *dentry,
2439 struct nameidata *nd)
2440{
2441 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2442}
2443
2444static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2445{
2446 return proc_readfd_common(filp, dirent, filldir,
2447 proc_fdinfo_instantiate);
2448}
2449
2450static const struct file_operations proc_fdinfo_operations = {
2451 .read = generic_read_dir,
2452 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002453 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002454};
2455
2456/*
2457 * proc directories can do almost nothing..
2458 */
2459static const struct inode_operations proc_fdinfo_inode_operations = {
2460 .lookup = proc_lookupfdinfo,
2461 .setattr = proc_setattr,
2462};
2463
2464
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002465static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002466 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002467{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002468 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002469 struct inode *inode;
2470 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002471 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002472
Eric W. Biederman61a28782006-10-02 02:18:49 -07002473 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002474 if (!inode)
2475 goto out;
2476
2477 ei = PROC_I(inode);
2478 inode->i_mode = p->mode;
2479 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002480 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002481 if (p->iop)
2482 inode->i_op = p->iop;
2483 if (p->fop)
2484 inode->i_fop = p->fop;
2485 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002486 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002487 d_add(dentry, inode);
2488 /* Close the race of the process dying before we return the dentry */
2489 if (pid_revalidate(dentry, NULL))
2490 error = NULL;
2491out:
2492 return error;
2493}
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495static struct dentry *proc_pident_lookup(struct inode *dir,
2496 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002497 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002498 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002500 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002501 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002502 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002504 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Eric W. Biederman99f89552006-06-26 00:25:55 -07002506 if (!task)
2507 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002509 /*
2510 * Yes, it does not scale. And it should not. Don't add
2511 * new entries into /proc/<tgid>/ without very good reasons.
2512 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002513 last = &ents[nents - 1];
2514 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (p->len != dentry->d_name.len)
2516 continue;
2517 if (!memcmp(dentry->d_name.name, p->name, p->len))
2518 break;
2519 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002520 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 goto out;
2522
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002523 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002525 put_task_struct(task);
2526out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002527 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
Eric Dumazetc5141e62007-05-08 00:26:15 -07002530static int proc_pident_fill_cache(struct file *filp, void *dirent,
2531 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002532{
2533 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2534 proc_pident_instantiate, task, p);
2535}
2536
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002537static int proc_pident_readdir(struct file *filp,
2538 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002539 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002540{
2541 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002542 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002543 struct inode *inode = dentry->d_inode;
2544 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002545 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002546 ino_t ino;
2547 int ret;
2548
2549 ret = -ENOENT;
2550 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002551 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002552
2553 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002554 i = filp->f_pos;
2555 switch (i) {
2556 case 0:
2557 ino = inode->i_ino;
2558 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2559 goto out;
2560 i++;
2561 filp->f_pos++;
2562 /* fall through */
2563 case 1:
2564 ino = parent_ino(dentry);
2565 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2566 goto out;
2567 i++;
2568 filp->f_pos++;
2569 /* fall through */
2570 default:
2571 i -= 2;
2572 if (i >= nents) {
2573 ret = 1;
2574 goto out;
2575 }
2576 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002577 last = &ents[nents - 1];
2578 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002579 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002580 goto out;
2581 filp->f_pos++;
2582 p++;
2583 }
2584 }
2585
2586 ret = 1;
2587out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002588 put_task_struct(task);
2589out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002590 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002594static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2595 size_t count, loff_t *ppos)
2596{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002597 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002598 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002599 ssize_t length;
2600 struct task_struct *task = get_proc_task(inode);
2601
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002602 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002603 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002604
2605 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002606 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002607 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002608 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002609 if (length > 0)
2610 length = simple_read_from_buffer(buf, count, ppos, p, length);
2611 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002612 return length;
2613}
2614
2615static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2616 size_t count, loff_t *ppos)
2617{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002618 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002619 char *page;
2620 ssize_t length;
2621 struct task_struct *task = get_proc_task(inode);
2622
2623 length = -ESRCH;
2624 if (!task)
2625 goto out_no_task;
2626 if (count > PAGE_SIZE)
2627 count = PAGE_SIZE;
2628
2629 /* No partial writes. */
2630 length = -EINVAL;
2631 if (*ppos != 0)
2632 goto out;
2633
2634 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002635 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002636 if (!page)
2637 goto out;
2638
2639 length = -EFAULT;
2640 if (copy_from_user(page, buf, count))
2641 goto out_free;
2642
David Howells107db7c2009-05-08 13:55:27 +01002643 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002644 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002645 if (length < 0)
2646 goto out_free;
2647
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002648 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002649 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002650 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002651 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002652out_free:
2653 free_page((unsigned long) page);
2654out:
2655 put_task_struct(task);
2656out_no_task:
2657 return length;
2658}
2659
Arjan van de Ven00977a52007-02-12 00:55:34 -08002660static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002661 .read = proc_pid_attr_read,
2662 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002663 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002664};
2665
Eric Dumazetc5141e62007-05-08 00:26:15 -07002666static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002667 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2668 REG("prev", S_IRUGO, proc_pid_attr_operations),
2669 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2670 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2671 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2672 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002673};
2674
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002675static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 void * dirent, filldir_t filldir)
2677{
2678 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002679 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Arjan van de Ven00977a52007-02-12 00:55:34 -08002682static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002684 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002685 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686};
2687
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002688static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 struct dentry *dentry, struct nameidata *nd)
2690{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002691 return proc_pident_lookup(dir, dentry,
2692 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002695static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002696 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002697 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002698 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699};
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701#endif
2702
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002703#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002704static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2705 size_t count, loff_t *ppos)
2706{
2707 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2708 struct mm_struct *mm;
2709 char buffer[PROC_NUMBUF];
2710 size_t len;
2711 int ret;
2712
2713 if (!task)
2714 return -ESRCH;
2715
2716 ret = 0;
2717 mm = get_task_mm(task);
2718 if (mm) {
2719 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2720 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2721 MMF_DUMP_FILTER_SHIFT));
2722 mmput(mm);
2723 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2724 }
2725
2726 put_task_struct(task);
2727
2728 return ret;
2729}
2730
2731static ssize_t proc_coredump_filter_write(struct file *file,
2732 const char __user *buf,
2733 size_t count,
2734 loff_t *ppos)
2735{
2736 struct task_struct *task;
2737 struct mm_struct *mm;
2738 char buffer[PROC_NUMBUF], *end;
2739 unsigned int val;
2740 int ret;
2741 int i;
2742 unsigned long mask;
2743
2744 ret = -EFAULT;
2745 memset(buffer, 0, sizeof(buffer));
2746 if (count > sizeof(buffer) - 1)
2747 count = sizeof(buffer) - 1;
2748 if (copy_from_user(buffer, buf, count))
2749 goto out_no_task;
2750
2751 ret = -EINVAL;
2752 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2753 if (*end == '\n')
2754 end++;
2755 if (end - buffer == 0)
2756 goto out_no_task;
2757
2758 ret = -ESRCH;
2759 task = get_proc_task(file->f_dentry->d_inode);
2760 if (!task)
2761 goto out_no_task;
2762
2763 ret = end - buffer;
2764 mm = get_task_mm(task);
2765 if (!mm)
2766 goto out_no_mm;
2767
2768 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2769 if (val & mask)
2770 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2771 else
2772 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2773 }
2774
2775 mmput(mm);
2776 out_no_mm:
2777 put_task_struct(task);
2778 out_no_task:
2779 return ret;
2780}
2781
2782static const struct file_operations proc_coredump_filter_operations = {
2783 .read = proc_coredump_filter_read,
2784 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002785 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002786};
2787#endif
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789/*
2790 * /proc/self:
2791 */
2792static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2793 int buflen)
2794{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002795 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002796 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002797 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002798 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002799 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002800 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return vfs_readlink(dentry,buffer,buflen,tmp);
2802}
2803
Al Viro008b1502005-08-20 00:17:39 +01002804static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002806 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002807 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002808 char *name = ERR_PTR(-ENOENT);
2809 if (tgid) {
2810 name = __getname();
2811 if (!name)
2812 name = ERR_PTR(-ENOMEM);
2813 else
2814 sprintf(name, "%d", tgid);
2815 }
2816 nd_set_link(nd, name);
2817 return NULL;
2818}
2819
2820static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2821 void *cookie)
2822{
2823 char *s = nd_get_link(nd);
2824 if (!IS_ERR(s))
2825 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002828static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 .readlink = proc_self_readlink,
2830 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002831 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832};
2833
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002834/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002835 * proc base
2836 *
2837 * These are the directory entries in the root directory of /proc
2838 * that properly belong to the /proc filesystem, as they describe
2839 * describe something that is process related.
2840 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002841static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002842 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002843 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002844};
2845
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002846static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002847 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002848{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002849 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002850 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002851 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002852 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002853
2854 /* Allocate the inode */
2855 error = ERR_PTR(-ENOMEM);
2856 inode = new_inode(dir->i_sb);
2857 if (!inode)
2858 goto out;
2859
2860 /* Initialize the inode */
2861 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002862 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002863 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002864
2865 /*
2866 * grab the reference to the task.
2867 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002868 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002869 if (!ei->pid)
2870 goto out_iput;
2871
Eric W. Biederman801199c2006-10-02 02:18:48 -07002872 inode->i_mode = p->mode;
2873 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002874 set_nlink(inode, 2);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002875 if (S_ISLNK(inode->i_mode))
2876 inode->i_size = 64;
2877 if (p->iop)
2878 inode->i_op = p->iop;
2879 if (p->fop)
2880 inode->i_fop = p->fop;
2881 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002882 d_add(dentry, inode);
2883 error = NULL;
2884out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002885 return error;
2886out_iput:
2887 iput(inode);
2888 goto out;
2889}
2890
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002891static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2892{
2893 struct dentry *error;
2894 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002895 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002896
2897 error = ERR_PTR(-ENOENT);
2898
2899 if (!task)
2900 goto out_no_task;
2901
2902 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002903 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2904 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002905 if (p->len != dentry->d_name.len)
2906 continue;
2907 if (!memcmp(dentry->d_name.name, p->name, p->len))
2908 break;
2909 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002910 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002911 goto out;
2912
2913 error = proc_base_instantiate(dir, dentry, task, p);
2914
2915out:
2916 put_task_struct(task);
2917out_no_task:
2918 return error;
2919}
2920
Eric Dumazetc5141e62007-05-08 00:26:15 -07002921static int proc_base_fill_cache(struct file *filp, void *dirent,
2922 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002923{
2924 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2925 proc_base_instantiate, task, p);
2926}
2927
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002928#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002929static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002930{
Andrea Righi940389b2008-07-28 00:48:12 +02002931 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002932 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002933 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002934
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002935 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2936 if (result)
2937 return result;
2938
2939 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2940 result = -EACCES;
2941 goto out_unlock;
2942 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002943
Andrea Righi59954772008-07-27 17:29:15 +02002944 if (whole && lock_task_sighand(task, &flags)) {
2945 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002946
Andrea Righi59954772008-07-27 17:29:15 +02002947 task_io_accounting_add(&acct, &task->signal->ioac);
2948 while_each_thread(task, t)
2949 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002950
Andrea Righi59954772008-07-27 17:29:15 +02002951 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002952 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002953 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002954 "rchar: %llu\n"
2955 "wchar: %llu\n"
2956 "syscr: %llu\n"
2957 "syscw: %llu\n"
2958 "read_bytes: %llu\n"
2959 "write_bytes: %llu\n"
2960 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002961 (unsigned long long)acct.rchar,
2962 (unsigned long long)acct.wchar,
2963 (unsigned long long)acct.syscr,
2964 (unsigned long long)acct.syscw,
2965 (unsigned long long)acct.read_bytes,
2966 (unsigned long long)acct.write_bytes,
2967 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002968out_unlock:
2969 mutex_unlock(&task->signal->cred_guard_mutex);
2970 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002971}
Andrea Righi297c5d92008-07-25 01:48:49 -07002972
2973static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2974{
2975 return do_io_accounting(task, buffer, 0);
2976}
2977
2978static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2979{
2980 return do_io_accounting(task, buffer, 1);
2981}
2982#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002983
Kees Cook47830722008-10-06 03:11:58 +04002984static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2985 struct pid *pid, struct task_struct *task)
2986{
Al Viroa9712bc2011-03-23 15:52:50 -04002987 int err = lock_trace(task);
2988 if (!err) {
2989 seq_printf(m, "%08x\n", task->personality);
2990 unlock_trace(task);
2991 }
2992 return err;
Kees Cook47830722008-10-06 03:11:58 +04002993}
2994
Eric W. Biederman801199c2006-10-02 02:18:48 -07002995/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002996 * Thread groups
2997 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002998static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002999static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07003000
Eric Dumazetc5141e62007-05-08 00:26:15 -07003001static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003002 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3003 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08003004#ifdef CONFIG_CHECKPOINT_RESTORE
3005 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3006#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003007 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003008 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07003009#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003010 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07003011#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003012 REG("environ", S_IRUSR, proc_environ_operations),
3013 INF("auxv", S_IRUSR, proc_pid_auxv),
3014 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003015 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003016 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003017#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003018 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003019#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01003020#ifdef CONFIG_SCHED_AUTOGROUP
3021 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3022#endif
john stultz4614a696b2009-12-14 18:00:05 -08003023 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003024#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003025 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003026#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003027 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3028 ONE("stat", S_IRUGO, proc_tgid_stat),
3029 ONE("statm", S_IRUGO, proc_pid_statm),
3030 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003031#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003032 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003033#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003034 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3035 LNK("cwd", proc_cwd_link),
3036 LNK("root", proc_root_link),
3037 LNK("exe", proc_exe_link),
3038 REG("mounts", S_IRUGO, proc_mounts_operations),
3039 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3040 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003041#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003042 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3043 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003044 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003045#endif
3046#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003047 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003048#endif
3049#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003050 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003051#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003052#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003053 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003054#endif
3055#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003056 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003057#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003058#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003059 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003060#endif
Paul Menage8793d852007-10-18 23:39:39 -07003061#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003062 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003063#endif
Paul Menagea4243162007-10-18 23:39:35 -07003064#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003065 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003066#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003067 INF("oom_score", S_IRUGO, proc_oom_score),
3068 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003069 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003070#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003071 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3072 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003073#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003074#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003075 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003076#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003077#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003078 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003079#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003080#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003081 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003082#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003083#ifdef CONFIG_HARDWALL
3084 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3085#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003086};
3087
3088static int proc_tgid_base_readdir(struct file * filp,
3089 void * dirent, filldir_t filldir)
3090{
3091 return proc_pident_readdir(filp,dirent,filldir,
3092 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3093}
3094
Arjan van de Ven00977a52007-02-12 00:55:34 -08003095static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003096 .read = generic_read_dir,
3097 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003098 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003099};
3100
3101static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003102 return proc_pident_lookup(dir, dentry,
3103 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003104}
3105
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003106static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003107 .lookup = proc_tgid_base_lookup,
3108 .getattr = pid_getattr,
3109 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003110 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003111};
3112
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003113static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003115 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003116 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003117 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Eric W. Biederman48e64842006-06-26 00:25:48 -07003119 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003120 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3121 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003122 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08003123 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003124 d_drop(dentry);
3125 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Eric W. Biederman48e64842006-06-26 00:25:48 -07003128 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003129 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3130 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003131 if (!leader)
3132 goto out;
3133
3134 name.name = "task";
3135 name.len = strlen(name.name);
3136 dir = d_hash_and_lookup(leader, &name);
3137 if (!dir)
3138 goto out_put_leader;
3139
3140 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003141 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003142 dentry = d_hash_and_lookup(dir, &name);
3143 if (dentry) {
3144 shrink_dcache_parent(dentry);
3145 d_drop(dentry);
3146 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003148
3149 dput(dir);
3150out_put_leader:
3151 dput(leader);
3152out:
3153 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
Randy Dunlap0895e912007-10-21 21:00:10 -07003156/**
3157 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3158 * @task: task that should be flushed.
3159 *
3160 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003161 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003162 * in. This call is supposed to do all of this job.
3163 *
3164 * Looks in the dcache for
3165 * /proc/@pid
3166 * /proc/@tgid/task/@pid
3167 * if either directory is present flushes it and all of it'ts children
3168 * from the dcache.
3169 *
3170 * It is safe and reasonable to cache /proc entries for a task until
3171 * that task exits. After that they just clog up the dcache with
3172 * useless entries, possibly causing useful dcache entries to be
3173 * flushed instead. This routine is proved to flush those useless
3174 * dcache entries at process exit time.
3175 *
3176 * NOTE: This routine is just an optimization so it does not guarantee
3177 * that no dcache entries will exist at process exit time it
3178 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003179 */
3180
3181void proc_flush_task(struct task_struct *task)
3182{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003183 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003184 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003185 struct upid *upid;
3186
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003187 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003188 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003189
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003190 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003191 upid = &pid->numbers[i];
3192 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003193 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003194 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07003195
3196 upid = &pid->numbers[pid->level];
3197 if (upid->nr == 1)
3198 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003199}
3200
Adrian Bunk9711ef92006-12-06 20:38:31 -08003201static struct dentry *proc_pid_instantiate(struct inode *dir,
3202 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003203 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003204{
3205 struct dentry *error = ERR_PTR(-ENOENT);
3206 struct inode *inode;
3207
Eric W. Biederman61a28782006-10-02 02:18:49 -07003208 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003209 if (!inode)
3210 goto out;
3211
3212 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3213 inode->i_op = &proc_tgid_base_inode_operations;
3214 inode->i_fop = &proc_tgid_base_operations;
3215 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003216
Miklos Szeredibfe86842011-10-28 14:13:29 +02003217 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3218 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003219
Nick Pigginfb045ad2011-01-07 17:49:55 +11003220 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003221
3222 d_add(dentry, inode);
3223 /* Close the race of the process dying before we return the dentry */
3224 if (pid_revalidate(dentry, NULL))
3225 error = NULL;
3226out:
3227 return error;
3228}
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3231{
Dan Carpenter73d36462010-05-26 14:43:25 -07003232 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003235 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Eric W. Biederman801199c2006-10-02 02:18:48 -07003237 result = proc_base_lookup(dir, dentry);
3238 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3239 goto out;
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 tgid = name_to_int(dentry);
3242 if (tgid == ~0U)
3243 goto out;
3244
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003245 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003246 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003247 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (task)
3249 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003250 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (!task)
3252 goto out;
3253
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003254 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003255 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07003257 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003261 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003262 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003264struct tgid_iter {
3265 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003266 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003267};
3268static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3269{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003270 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003272 if (iter.task)
3273 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003274 rcu_read_lock();
3275retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003276 iter.task = NULL;
3277 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003278 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003279 iter.tgid = pid_nr_ns(pid, ns);
3280 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003281 /* What we to know is if the pid we have find is the
3282 * pid of a thread_group_leader. Testing for task
3283 * being a thread_group_leader is the obvious thing
3284 * todo but there is a window when it fails, due to
3285 * the pid transfer logic in de_thread.
3286 *
3287 * So we perform the straight forward test of seeing
3288 * if the pid we have found is the pid of a thread
3289 * group leader, and don't worry if the task we have
3290 * found doesn't happen to be a thread group leader.
3291 * As we don't care in the case of readdir.
3292 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003293 if (!iter.task || !has_group_leader_pid(iter.task)) {
3294 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003295 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003296 }
3297 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003299 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003300 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003303#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Eric W. Biederman61a28782006-10-02 02:18:49 -07003305static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003306 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003307{
3308 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003309 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003310 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003311 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003312}
3313
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003314static int fake_filldir(void *buf, const char *name, int namelen,
3315 loff_t offset, u64 ino, unsigned d_type)
3316{
3317 return 0;
3318}
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320/* for the /proc/ directory itself, after non-process stuff has been done */
3321int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3322{
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003323 unsigned int nr;
3324 struct task_struct *reaper;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003325 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003326 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003327 filldir_t __filldir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07003329 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3330 goto out_no_task;
3331 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3332
3333 reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003334 if (!reaper)
3335 goto out_no_task;
3336
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003337 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003338 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003339 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003340 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
3342
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003343 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003344 iter.task = NULL;
3345 iter.tgid = filp->f_pos - TGID_OFFSET;
3346 for (iter = next_tgid(ns, iter);
3347 iter.task;
3348 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003349 if (has_pid_permissions(ns, iter.task, 2))
3350 __filldir = filldir;
3351 else
3352 __filldir = fake_filldir;
3353
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003354 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003355 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003356 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003360 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3361out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003362 put_task_struct(reaper);
3363out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return 0;
3365}
3366
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003367/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003368 * Tasks
3369 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003370static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003371 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003372 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003373 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003374 REG("environ", S_IRUSR, proc_environ_operations),
3375 INF("auxv", S_IRUSR, proc_pid_auxv),
3376 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04003377 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003378 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003379#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003380 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003381#endif
john stultz4614a696b2009-12-14 18:00:05 -08003382 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003383#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04003384 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003385#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003386 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3387 ONE("stat", S_IRUGO, proc_tid_stat),
3388 ONE("statm", S_IRUGO, proc_pid_statm),
3389 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003390#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003391 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003392#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003393 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3394 LNK("cwd", proc_cwd_link),
3395 LNK("root", proc_root_link),
3396 LNK("exe", proc_exe_link),
3397 REG("mounts", S_IRUGO, proc_mounts_operations),
3398 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003399#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003400 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3401 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003402 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003403#endif
3404#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003405 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003406#endif
3407#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003408 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003409#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003410#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04003411 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003412#endif
3413#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003414 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003415#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003416#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003417 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003418#endif
Paul Menage8793d852007-10-18 23:39:39 -07003419#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003420 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003421#endif
Paul Menagea4243162007-10-18 23:39:35 -07003422#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003423 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003424#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003425 INF("oom_score", S_IRUGO, proc_oom_score),
3426 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003427 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003428#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003429 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003430 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003431#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003432#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003433 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003434#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003435#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003436 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003437#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003438#ifdef CONFIG_HARDWALL
3439 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3440#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003441};
3442
3443static int proc_tid_base_readdir(struct file * filp,
3444 void * dirent, filldir_t filldir)
3445{
3446 return proc_pident_readdir(filp,dirent,filldir,
3447 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3448}
3449
3450static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003451 return proc_pident_lookup(dir, dentry,
3452 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003453}
3454
Arjan van de Ven00977a52007-02-12 00:55:34 -08003455static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003456 .read = generic_read_dir,
3457 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003458 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003459};
3460
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003461static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003462 .lookup = proc_tid_base_lookup,
3463 .getattr = pid_getattr,
3464 .setattr = proc_setattr,
3465};
3466
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003467static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003468 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003469{
3470 struct dentry *error = ERR_PTR(-ENOENT);
3471 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003472 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003473
3474 if (!inode)
3475 goto out;
3476 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3477 inode->i_op = &proc_tid_base_inode_operations;
3478 inode->i_fop = &proc_tid_base_operations;
3479 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003480
Miklos Szeredibfe86842011-10-28 14:13:29 +02003481 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3482 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003483
Nick Pigginfb045ad2011-01-07 17:49:55 +11003484 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003485
3486 d_add(dentry, inode);
3487 /* Close the race of the process dying before we return the dentry */
3488 if (pid_revalidate(dentry, NULL))
3489 error = NULL;
3490out:
3491 return error;
3492}
3493
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003494static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3495{
3496 struct dentry *result = ERR_PTR(-ENOENT);
3497 struct task_struct *task;
3498 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003499 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003500 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003501
3502 if (!leader)
3503 goto out_no_task;
3504
3505 tid = name_to_int(dentry);
3506 if (tid == ~0U)
3507 goto out;
3508
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003509 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003510 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003511 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003512 if (task)
3513 get_task_struct(task);
3514 rcu_read_unlock();
3515 if (!task)
3516 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003517 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003518 goto out_drop_task;
3519
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003520 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003521out_drop_task:
3522 put_task_struct(task);
3523out:
3524 put_task_struct(leader);
3525out_no_task:
3526 return result;
3527}
3528
3529/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003530 * Find the first tid of a thread group to return to user space.
3531 *
3532 * Usually this is just the thread group leader, but if the users
3533 * buffer was too small or there was a seek into the middle of the
3534 * directory we have more work todo.
3535 *
3536 * In the case of a short read we start with find_task_by_pid.
3537 *
3538 * In the case of a seek we start with the leader and walk nr
3539 * threads past it.
3540 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003541static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003542 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003543{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003544 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003545
Eric W. Biedermancc288732006-06-26 00:26:01 -07003546 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003547 /* Attempt to start with the pid of a thread */
3548 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003549 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003550 if (pos && (pos->group_leader == leader))
3551 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003552 }
3553
3554 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003555 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003556 if (nr && nr >= get_nr_threads(leader))
3557 goto out;
3558
3559 /* If we haven't found our starting place yet start
3560 * with the leader and walk nr threads forward.
3561 */
3562 for (pos = leader; nr > 0; --nr) {
3563 pos = next_thread(pos);
3564 if (pos == leader) {
3565 pos = NULL;
3566 goto out;
3567 }
3568 }
3569found:
3570 get_task_struct(pos);
3571out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003572 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003573 return pos;
3574}
3575
3576/*
3577 * Find the next thread in the thread list.
3578 * Return NULL if there is an error or no next thread.
3579 *
3580 * The reference to the input task_struct is released.
3581 */
3582static struct task_struct *next_tid(struct task_struct *start)
3583{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003584 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003585 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003586 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003587 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003588 if (thread_group_leader(pos))
3589 pos = NULL;
3590 else
3591 get_task_struct(pos);
3592 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003593 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003594 put_task_struct(start);
3595 return pos;
3596}
3597
Eric W. Biederman61a28782006-10-02 02:18:49 -07003598static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3599 struct task_struct *task, int tid)
3600{
3601 char name[PROC_NUMBUF];
3602 int len = snprintf(name, sizeof(name), "%d", tid);
3603 return proc_fill_cache(filp, dirent, filldir, name, len,
3604 proc_task_instantiate, task, NULL);
3605}
3606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607/* for the /proc/TGID/task/ directories */
3608static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3609{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003610 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003612 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003613 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 int retval = -ENOENT;
3615 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003616 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003617 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003619 task = get_proc_task(inode);
3620 if (!task)
3621 goto out_no_task;
3622 rcu_read_lock();
3623 if (pid_alive(task)) {
3624 leader = task->group_leader;
3625 get_task_struct(leader);
3626 }
3627 rcu_read_unlock();
3628 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003629 if (!leader)
3630 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 retval = 0;
3632
Linus Torvaldsee568b22009-03-17 10:02:35 -07003633 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 case 0:
3635 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003636 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003638 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 /* fall through */
3640 case 1:
3641 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003642 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003644 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 /* fall through */
3646 }
3647
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003648 /* f_version caches the tgid value that the last readdir call couldn't
3649 * return. lseek aka telldir automagically resets f_version to 0.
3650 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003651 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003652 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003653 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003654 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003655 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003656 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003657 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003658 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003659 /* returning this tgid failed, save it as the first
3660 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003661 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003662 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003667 put_task_struct(leader);
3668out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return retval;
3670}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003671
3672static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3673{
3674 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003675 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003676 generic_fillattr(inode, stat);
3677
Eric W. Biederman99f89552006-06-26 00:25:55 -07003678 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003679 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003680 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003681 }
3682
3683 return 0;
3684}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003685
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003686static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003687 .lookup = proc_task_lookup,
3688 .getattr = proc_task_getattr,
3689 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003690 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003691};
3692
Arjan van de Ven00977a52007-02-12 00:55:34 -08003693static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003694 .read = generic_read_dir,
3695 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003696 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003697};