blob: b48ddb1199459530560a35cfeda4271cda3698b2 [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>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070064#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/kallsyms.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070066#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070067#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/mount.h>
69#include <linux/security.h>
70#include <linux/ptrace.h>
Paul Menagea4243162007-10-18 23:39:35 -070071#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/cpuset.h>
73#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050074#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070075#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070076#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070077#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070078#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include "internal.h"
80
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070081/* NOTE:
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
86 *
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
89 */
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070093 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080095 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080096 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070097 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Eric W. Biederman61a28782006-10-02 02:18:49 -0700100#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700101 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700102 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .mode = MODE, \
104 .iop = IOP, \
105 .fop = FOP, \
106 .op = OP, \
107}
108
Eric W. Biederman61a28782006-10-02 02:18:49 -0700109#define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
112 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700113#define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700117#define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800124#define ONE(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
127 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Kees Cook5096add2007-05-08 00:26:04 -0700129int maps_protect;
130EXPORT_SYMBOL(maps_protect);
131
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700132static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700135 task_lock(task);
136 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if(fs)
138 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700139 task_unlock(task);
140 return fs;
141}
142
Eric W. Biederman99f89552006-06-26 00:25:55 -0700143static int get_nr_threads(struct task_struct *tsk)
144{
145 /* Must be called with the rcu_read_lock held */
146 unsigned long flags;
147 int count = 0;
148
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
152 }
153 return count;
154}
155
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800156static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700160 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700161
162 if (task) {
163 fs = get_fs_struct(task);
164 put_task_struct(task);
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (fs) {
167 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800168 *path = fs->pwd;
169 path_get(&fs->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 read_unlock(&fs->lock);
171 result = 0;
172 put_fs_struct(fs);
173 }
174 return result;
175}
176
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800177static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182
183 if (task) {
184 fs = get_fs_struct(task);
185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (fs) {
188 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800189 *path = fs->root;
190 path_get(&fs->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 read_unlock(&fs->lock);
192 result = 0;
193 put_fs_struct(fs);
194 }
195 return result;
196}
197
198#define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
Matthew Wilcox6d8982d2007-12-06 11:04:01 -0500202 (task_is_stopped_or_traced(task)) && \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 security_ptrace(current,task) == 0))
204
Al Viro831830b2008-01-02 14:09:57 +0000205struct mm_struct *mm_for_maps(struct task_struct *task)
206{
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 return NULL;
210 down_read(&mm->mmap_sem);
211 task_lock(task);
212 if (task->mm != mm)
213 goto out;
214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
215 goto out;
216 task_unlock(task);
217 return mm;
218out:
219 task_unlock(task);
220 up_read(&mm->mmap_sem);
221 mmput(mm);
222 return NULL;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int proc_pid_cmdline(struct task_struct *task, char * buffer)
226{
227 int res = 0;
228 unsigned int len;
229 struct mm_struct *mm = get_task_mm(task);
230 if (!mm)
231 goto out;
232 if (!mm->arg_end)
233 goto out_mm; /* Shh! No looking before we're done */
234
235 len = mm->arg_end - mm->arg_start;
236
237 if (len > PAGE_SIZE)
238 len = PAGE_SIZE;
239
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
241
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
246 if (len < res) {
247 res = len;
248 } else {
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
254 }
255 }
256out_mm:
257 mmput(mm);
258out:
259 return res;
260}
261
262static int proc_pid_auxv(struct task_struct *task, char *buffer)
263{
264 int res = 0;
265 struct mm_struct *mm = get_task_mm(task);
266 if (mm) {
267 unsigned int nwords = 0;
268 do
269 nwords += 2;
270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
272 if (res > PAGE_SIZE)
273 res = PAGE_SIZE;
274 memcpy(buffer, mm->saved_auxv, res);
275 mmput(mm);
276 }
277 return res;
278}
279
280
281#ifdef CONFIG_KALLSYMS
282/*
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
285 */
286static int proc_pid_wchan(struct task_struct *task, char *buffer)
287{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700288 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700289 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 wchan = get_wchan(task);
292
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700293 if (lookup_symbol_name(wchan, symname) < 0)
294 return sprintf(buffer, "%lu", wchan);
295 else
296 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298#endif /* CONFIG_KALLSYMS */
299
300#ifdef CONFIG_SCHEDSTATS
301/*
302 * Provides /proc/PID/schedstat
303 */
304static int proc_pid_schedstat(struct task_struct *task, char *buffer)
305{
Balbir Singh172ba842007-07-09 18:52:00 +0200306 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 task->sched_info.cpu_time,
308 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200309 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311#endif
312
Arjan van de Ven97455122008-01-25 21:08:34 +0100313#ifdef CONFIG_LATENCYTOP
314static int lstats_show_proc(struct seq_file *m, void *v)
315{
316 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800317 struct inode *inode = m->private;
318 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100319
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800320 if (!task)
321 return -ESRCH;
322 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100323 for (i = 0; i < 32; i++) {
324 if (task->latency_record[i].backtrace[0]) {
325 int q;
326 seq_printf(m, "%i %li %li ",
327 task->latency_record[i].count,
328 task->latency_record[i].time,
329 task->latency_record[i].max);
330 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
331 char sym[KSYM_NAME_LEN];
332 char *c;
333 if (!task->latency_record[i].backtrace[q])
334 break;
335 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
336 break;
337 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
338 c = strchr(sym, '+');
339 if (c)
340 *c = 0;
341 seq_printf(m, "%s ", sym);
342 }
343 seq_printf(m, "\n");
344 }
345
346 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800347 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100348 return 0;
349}
350
351static int lstats_open(struct inode *inode, struct file *file)
352{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800353 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800354}
355
Arjan van de Ven97455122008-01-25 21:08:34 +0100356static ssize_t lstats_write(struct file *file, const char __user *buf,
357 size_t count, loff_t *offs)
358{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800359 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100360
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800361 if (!task)
362 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100363 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800364 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100365
366 return count;
367}
368
369static const struct file_operations proc_lstats_operations = {
370 .open = lstats_open,
371 .read = seq_read,
372 .write = lstats_write,
373 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800374 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100375};
376
377#endif
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/* The badness from the OOM killer */
380unsigned long badness(struct task_struct *p, unsigned long uptime);
381static int proc_oom_score(struct task_struct *task, char *buffer)
382{
383 unsigned long points;
384 struct timespec uptime;
385
386 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700387 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700389 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return sprintf(buffer, "%lu\n", points);
391}
392
Neil Hormand85f50d2007-10-18 23:40:37 -0700393struct limit_names {
394 char *name;
395 char *unit;
396};
397
398static const struct limit_names lnames[RLIM_NLIMITS] = {
399 [RLIMIT_CPU] = {"Max cpu time", "ms"},
400 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
401 [RLIMIT_DATA] = {"Max data size", "bytes"},
402 [RLIMIT_STACK] = {"Max stack size", "bytes"},
403 [RLIMIT_CORE] = {"Max core file size", "bytes"},
404 [RLIMIT_RSS] = {"Max resident set", "bytes"},
405 [RLIMIT_NPROC] = {"Max processes", "processes"},
406 [RLIMIT_NOFILE] = {"Max open files", "files"},
407 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
408 [RLIMIT_AS] = {"Max address space", "bytes"},
409 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
410 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
411 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
412 [RLIMIT_NICE] = {"Max nice priority", NULL},
413 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800414 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700415};
416
417/* Display limits for a process */
418static int proc_pid_limits(struct task_struct *task, char *buffer)
419{
420 unsigned int i;
421 int count = 0;
422 unsigned long flags;
423 char *bufptr = buffer;
424
425 struct rlimit rlim[RLIM_NLIMITS];
426
427 rcu_read_lock();
428 if (!lock_task_sighand(task,&flags)) {
429 rcu_read_unlock();
430 return 0;
431 }
432 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
433 unlock_task_sighand(task, &flags);
434 rcu_read_unlock();
435
436 /*
437 * print the file header
438 */
439 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
440 "Limit", "Soft Limit", "Hard Limit", "Units");
441
442 for (i = 0; i < RLIM_NLIMITS; i++) {
443 if (rlim[i].rlim_cur == RLIM_INFINITY)
444 count += sprintf(&bufptr[count], "%-25s %-20s ",
445 lnames[i].name, "unlimited");
446 else
447 count += sprintf(&bufptr[count], "%-25s %-20lu ",
448 lnames[i].name, rlim[i].rlim_cur);
449
450 if (rlim[i].rlim_max == RLIM_INFINITY)
451 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
452 else
453 count += sprintf(&bufptr[count], "%-20lu ",
454 rlim[i].rlim_max);
455
456 if (lnames[i].unit)
457 count += sprintf(&bufptr[count], "%-10s\n",
458 lnames[i].unit);
459 else
460 count += sprintf(&bufptr[count], "\n");
461 }
462
463 return count;
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/************************************************************************/
467/* Here the fs part begins */
468/************************************************************************/
469
470/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700471static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700473 struct task_struct *task;
474 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700475 /* Allow access to a task's file descriptors if it is us or we
476 * may use ptrace attach to the process and find out that
477 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700478 */
479 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700480 if (task) {
481 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700482 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700483 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700484 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700487static int proc_setattr(struct dentry *dentry, struct iattr *attr)
488{
489 int error;
490 struct inode *inode = dentry->d_inode;
491
492 if (attr->ia_valid & ATTR_MODE)
493 return -EPERM;
494
495 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700496 if (!error)
497 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700498 return error;
499}
500
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800501static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700502 .setattr = proc_setattr,
503};
504
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100505static int mounts_open_common(struct inode *inode, struct file *file,
506 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700508 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700509 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800510 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100511 struct fs_struct *fs = NULL;
512 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500513 struct proc_mounts *p;
514 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Eric W. Biederman99f89552006-06-26 00:25:55 -0700516 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700517 rcu_read_lock();
518 nsp = task_nsproxy(task);
519 if (nsp) {
520 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800521 if (ns)
522 get_mnt_ns(ns);
523 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700524 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100525 if (ns)
526 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700527 put_task_struct(task);
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100530 if (!ns)
531 goto err;
532 if (!fs)
533 goto err_put_ns;
534
535 read_lock(&fs->lock);
536 root = fs->root;
537 path_get(&root);
538 read_unlock(&fs->lock);
539 put_fs_struct(fs);
540
541 ret = -ENOMEM;
542 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
543 if (!p)
544 goto err_put_path;
545
546 file->private_data = &p->m;
547 ret = seq_open(file, op);
548 if (ret)
549 goto err_free;
550
551 p->m.private = p;
552 p->ns = ns;
553 p->root = root;
554 p->event = ns->event;
555
556 return 0;
557
558 err_free:
559 kfree(p);
560 err_put_path:
561 path_put(&root);
562 err_put_ns:
563 put_mnt_ns(ns);
564 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return ret;
566}
567
568static int mounts_release(struct inode *inode, struct file *file)
569{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100570 struct proc_mounts *p = file->private_data;
571 path_put(&p->root);
572 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return seq_release(inode, file);
574}
575
Al Viro5addc5d2005-11-07 17:15:49 -0500576static unsigned mounts_poll(struct file *file, poll_table *wait)
577{
578 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100579 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500580 unsigned res = 0;
581
582 poll_wait(file, &ns->poll, wait);
583
584 spin_lock(&vfsmount_lock);
585 if (p->event != ns->event) {
586 p->event = ns->event;
587 res = POLLERR;
588 }
589 spin_unlock(&vfsmount_lock);
590
591 return res;
592}
593
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100594static int mounts_open(struct inode *inode, struct file *file)
595{
596 return mounts_open_common(inode, file, &mounts_op);
597}
598
Arjan van de Ven00977a52007-02-12 00:55:34 -0800599static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .open = mounts_open,
601 .read = seq_read,
602 .llseek = seq_lseek,
603 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500604 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605};
606
Ram Pai2d4d4862008-03-27 13:06:25 +0100607static int mountinfo_open(struct inode *inode, struct file *file)
608{
609 return mounts_open_common(inode, file, &mountinfo_op);
610}
611
612static const struct file_operations proc_mountinfo_operations = {
613 .open = mountinfo_open,
614 .read = seq_read,
615 .llseek = seq_lseek,
616 .release = mounts_release,
617 .poll = mounts_poll,
618};
619
Chuck Leverb4629fe2006-03-20 13:44:12 -0500620static int mountstats_open(struct inode *inode, struct file *file)
621{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100622 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500623}
624
Arjan van de Ven00977a52007-02-12 00:55:34 -0800625static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500626 .open = mountstats_open,
627 .read = seq_read,
628 .llseek = seq_lseek,
629 .release = mounts_release,
630};
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
633
634static ssize_t proc_info_read(struct file * file, char __user * buf,
635 size_t count, loff_t *ppos)
636{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800637 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned long page;
639 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700640 struct task_struct *task = get_proc_task(inode);
641
642 length = -ESRCH;
643 if (!task)
644 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 if (count > PROC_BLOCK_SIZE)
647 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700648
649 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700650 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700651 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 length = PROC_I(inode)->op.proc_read(task, (char*)page);
654
655 if (length >= 0)
656 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
657 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700658out:
659 put_task_struct(task);
660out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return length;
662}
663
Arjan van de Ven00977a52007-02-12 00:55:34 -0800664static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 .read = proc_info_read,
666};
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{
690 int ret;
691 ret = single_open(filp, proc_single_show, NULL);
692 if (!ret) {
693 struct seq_file *m = filp->private_data;
694
695 m->private = inode;
696 }
697 return ret;
698}
699
700static const struct file_operations proc_single_file_operations = {
701 .open = proc_single_open,
702 .read = seq_read,
703 .llseek = seq_lseek,
704 .release = single_release,
705};
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707static int mem_open(struct inode* inode, struct file* file)
708{
709 file->private_data = (void*)((long)current->self_exec_id);
710 return 0;
711}
712
713static ssize_t mem_read(struct file * file, char __user * buf,
714 size_t count, loff_t *ppos)
715{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800716 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 char *page;
718 unsigned long src = *ppos;
719 int ret = -ESRCH;
720 struct mm_struct *mm;
721
Eric W. Biederman99f89552006-06-26 00:25:55 -0700722 if (!task)
723 goto out_no_task;
724
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700725 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto out;
727
728 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700729 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!page)
731 goto out;
732
733 ret = 0;
734
735 mm = get_task_mm(task);
736 if (!mm)
737 goto out_free;
738
739 ret = -EIO;
740
741 if (file->private_data != (void*)((long)current->self_exec_id))
742 goto out_put;
743
744 ret = 0;
745
746 while (count > 0) {
747 int this_len, retval;
748
749 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
750 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700751 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!ret)
753 ret = -EIO;
754 break;
755 }
756
757 if (copy_to_user(buf, page, retval)) {
758 ret = -EFAULT;
759 break;
760 }
761
762 ret += retval;
763 src += retval;
764 buf += retval;
765 count -= retval;
766 }
767 *ppos = src;
768
769out_put:
770 mmput(mm);
771out_free:
772 free_page((unsigned long) page);
773out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700774 put_task_struct(task);
775out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return ret;
777}
778
779#define mem_write NULL
780
781#ifndef mem_write
782/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800783static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 size_t count, loff_t *ppos)
785{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700786 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800788 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 unsigned long dst = *ppos;
790
Eric W. Biederman99f89552006-06-26 00:25:55 -0700791 copied = -ESRCH;
792 if (!task)
793 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Eric W. Biederman99f89552006-06-26 00:25:55 -0700795 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
796 goto out;
797
798 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700799 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700801 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700803 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 while (count > 0) {
805 int this_len, retval;
806
807 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
808 if (copy_from_user(page, buf, this_len)) {
809 copied = -EFAULT;
810 break;
811 }
812 retval = access_process_vm(task, dst, page, this_len, 1);
813 if (!retval) {
814 if (!copied)
815 copied = -EIO;
816 break;
817 }
818 copied += retval;
819 buf += retval;
820 dst += retval;
821 count -= retval;
822 }
823 *ppos = dst;
824 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700825out:
826 put_task_struct(task);
827out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return copied;
829}
830#endif
831
Matt Mackall85863e42008-02-04 22:29:04 -0800832loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 switch (orig) {
835 case 0:
836 file->f_pos = offset;
837 break;
838 case 1:
839 file->f_pos += offset;
840 break;
841 default:
842 return -EINVAL;
843 }
844 force_successful_syscall_return();
845 return file->f_pos;
846}
847
Arjan van de Ven00977a52007-02-12 00:55:34 -0800848static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .llseek = mem_lseek,
850 .read = mem_read,
851 .write = mem_write,
852 .open = mem_open,
853};
854
James Pearson315e28c2007-10-16 23:30:17 -0700855static ssize_t environ_read(struct file *file, char __user *buf,
856 size_t count, loff_t *ppos)
857{
858 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
859 char *page;
860 unsigned long src = *ppos;
861 int ret = -ESRCH;
862 struct mm_struct *mm;
863
864 if (!task)
865 goto out_no_task;
866
867 if (!ptrace_may_attach(task))
868 goto out;
869
870 ret = -ENOMEM;
871 page = (char *)__get_free_page(GFP_TEMPORARY);
872 if (!page)
873 goto out;
874
875 ret = 0;
876
877 mm = get_task_mm(task);
878 if (!mm)
879 goto out_free;
880
881 while (count > 0) {
882 int this_len, retval, max_len;
883
884 this_len = mm->env_end - (mm->env_start + src);
885
886 if (this_len <= 0)
887 break;
888
889 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
890 this_len = (this_len > max_len) ? max_len : this_len;
891
892 retval = access_process_vm(task, (mm->env_start + src),
893 page, this_len, 0);
894
895 if (retval <= 0) {
896 ret = retval;
897 break;
898 }
899
900 if (copy_to_user(buf, page, retval)) {
901 ret = -EFAULT;
902 break;
903 }
904
905 ret += retval;
906 src += retval;
907 buf += retval;
908 count -= retval;
909 }
910 *ppos = src;
911
912 mmput(mm);
913out_free:
914 free_page((unsigned long) page);
915out:
916 put_task_struct(task);
917out_no_task:
918 return ret;
919}
920
921static const struct file_operations proc_environ_operations = {
922 .read = environ_read,
923};
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925static ssize_t oom_adjust_read(struct file *file, char __user *buf,
926 size_t count, loff_t *ppos)
927{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800928 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700929 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700931 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Eric W. Biederman99f89552006-06-26 00:25:55 -0700933 if (!task)
934 return -ESRCH;
935 oom_adjust = task->oomkilladj;
936 put_task_struct(task);
937
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700938 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700939
940 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
944 size_t count, loff_t *ppos)
945{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700946 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700947 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int oom_adjust;
949
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700950 memset(buffer, 0, sizeof(buffer));
951 if (count > sizeof(buffer) - 1)
952 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (copy_from_user(buffer, buf, count))
954 return -EFAULT;
955 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700956 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
957 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return -EINVAL;
959 if (*end == '\n')
960 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800961 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700962 if (!task)
963 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800964 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
965 put_task_struct(task);
966 return -EACCES;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700969 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (end - buffer == 0)
971 return -EIO;
972 return end - buffer;
973}
974
Arjan van de Ven00977a52007-02-12 00:55:34 -0800975static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 .read = oom_adjust_read,
977 .write = oom_adjust_write,
978};
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#ifdef CONFIG_AUDITSYSCALL
981#define TMPBUFLEN 21
982static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
983 size_t count, loff_t *ppos)
984{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800985 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700986 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ssize_t length;
988 char tmpbuf[TMPBUFLEN];
989
Eric W. Biederman99f89552006-06-26 00:25:55 -0700990 if (!task)
991 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -0500993 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700994 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
996}
997
998static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
999 size_t count, loff_t *ppos)
1000{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001001 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 char *page, *tmp;
1003 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 uid_t loginuid;
1005
1006 if (!capable(CAP_AUDIT_CONTROL))
1007 return -EPERM;
1008
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001009 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -EPERM;
1011
Al Viroe0182902006-05-18 08:28:02 -04001012 if (count >= PAGE_SIZE)
1013 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (*ppos != 0) {
1016 /* No partial writes. */
1017 return -EINVAL;
1018 }
Mel Gormane12ba742007-10-16 01:25:52 -07001019 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (!page)
1021 return -ENOMEM;
1022 length = -EFAULT;
1023 if (copy_from_user(page, buf, count))
1024 goto out_free_page;
1025
Al Viroe0182902006-05-18 08:28:02 -04001026 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 loginuid = simple_strtoul(page, &tmp, 10);
1028 if (tmp == page) {
1029 length = -EINVAL;
1030 goto out_free_page;
1031
1032 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001033 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (likely(length == 0))
1035 length = count;
1036
1037out_free_page:
1038 free_page((unsigned long) page);
1039 return length;
1040}
1041
Arjan van de Ven00977a52007-02-12 00:55:34 -08001042static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .read = proc_loginuid_read,
1044 .write = proc_loginuid_write,
1045};
Eric Paris1e0bd752008-03-13 08:15:31 -04001046
1047static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1048 size_t count, loff_t *ppos)
1049{
1050 struct inode * inode = file->f_path.dentry->d_inode;
1051 struct task_struct *task = get_proc_task(inode);
1052 ssize_t length;
1053 char tmpbuf[TMPBUFLEN];
1054
1055 if (!task)
1056 return -ESRCH;
1057 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1058 audit_get_sessionid(task));
1059 put_task_struct(task);
1060 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1061}
1062
1063static const struct file_operations proc_sessionid_operations = {
1064 .read = proc_sessionid_read,
1065};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066#endif
1067
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001068#ifdef CONFIG_FAULT_INJECTION
1069static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1070 size_t count, loff_t *ppos)
1071{
1072 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1073 char buffer[PROC_NUMBUF];
1074 size_t len;
1075 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001076
1077 if (!task)
1078 return -ESRCH;
1079 make_it_fail = task->make_it_fail;
1080 put_task_struct(task);
1081
1082 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001083
1084 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001085}
1086
1087static ssize_t proc_fault_inject_write(struct file * file,
1088 const char __user * buf, size_t count, loff_t *ppos)
1089{
1090 struct task_struct *task;
1091 char buffer[PROC_NUMBUF], *end;
1092 int make_it_fail;
1093
1094 if (!capable(CAP_SYS_RESOURCE))
1095 return -EPERM;
1096 memset(buffer, 0, sizeof(buffer));
1097 if (count > sizeof(buffer) - 1)
1098 count = sizeof(buffer) - 1;
1099 if (copy_from_user(buffer, buf, count))
1100 return -EFAULT;
1101 make_it_fail = simple_strtol(buffer, &end, 0);
1102 if (*end == '\n')
1103 end++;
1104 task = get_proc_task(file->f_dentry->d_inode);
1105 if (!task)
1106 return -ESRCH;
1107 task->make_it_fail = make_it_fail;
1108 put_task_struct(task);
1109 if (end - buffer == 0)
1110 return -EIO;
1111 return end - buffer;
1112}
1113
Arjan van de Ven00977a52007-02-12 00:55:34 -08001114static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001115 .read = proc_fault_inject_read,
1116 .write = proc_fault_inject_write,
1117};
1118#endif
1119
Arjan van de Ven97455122008-01-25 21:08:34 +01001120
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001121#ifdef CONFIG_SCHED_DEBUG
1122/*
1123 * Print out various scheduling related per-task fields:
1124 */
1125static int sched_show(struct seq_file *m, void *v)
1126{
1127 struct inode *inode = m->private;
1128 struct task_struct *p;
1129
1130 WARN_ON(!inode);
1131
1132 p = get_proc_task(inode);
1133 if (!p)
1134 return -ESRCH;
1135 proc_sched_show_task(p, m);
1136
1137 put_task_struct(p);
1138
1139 return 0;
1140}
1141
1142static ssize_t
1143sched_write(struct file *file, const char __user *buf,
1144 size_t count, loff_t *offset)
1145{
1146 struct inode *inode = file->f_path.dentry->d_inode;
1147 struct task_struct *p;
1148
1149 WARN_ON(!inode);
1150
1151 p = get_proc_task(inode);
1152 if (!p)
1153 return -ESRCH;
1154 proc_sched_set_task(p);
1155
1156 put_task_struct(p);
1157
1158 return count;
1159}
1160
1161static int sched_open(struct inode *inode, struct file *filp)
1162{
1163 int ret;
1164
1165 ret = single_open(filp, sched_show, NULL);
1166 if (!ret) {
1167 struct seq_file *m = filp->private_data;
1168
1169 m->private = inode;
1170 }
1171 return ret;
1172}
1173
1174static const struct file_operations proc_pid_sched_operations = {
1175 .open = sched_open,
1176 .read = seq_read,
1177 .write = sched_write,
1178 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001179 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001180};
1181
1182#endif
1183
Matt Helsley925d1c42008-04-29 01:01:36 -07001184/*
1185 * We added or removed a vma mapping the executable. The vmas are only mapped
1186 * during exec and are not mapped with the mmap system call.
1187 * Callers must hold down_write() on the mm's mmap_sem for these
1188 */
1189void added_exe_file_vma(struct mm_struct *mm)
1190{
1191 mm->num_exe_file_vmas++;
1192}
1193
1194void removed_exe_file_vma(struct mm_struct *mm)
1195{
1196 mm->num_exe_file_vmas--;
1197 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1198 fput(mm->exe_file);
1199 mm->exe_file = NULL;
1200 }
1201
1202}
1203
1204void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1205{
1206 if (new_exe_file)
1207 get_file(new_exe_file);
1208 if (mm->exe_file)
1209 fput(mm->exe_file);
1210 mm->exe_file = new_exe_file;
1211 mm->num_exe_file_vmas = 0;
1212}
1213
1214struct file *get_mm_exe_file(struct mm_struct *mm)
1215{
1216 struct file *exe_file;
1217
1218 /* We need mmap_sem to protect against races with removal of
1219 * VM_EXECUTABLE vmas */
1220 down_read(&mm->mmap_sem);
1221 exe_file = mm->exe_file;
1222 if (exe_file)
1223 get_file(exe_file);
1224 up_read(&mm->mmap_sem);
1225 return exe_file;
1226}
1227
1228void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1229{
1230 /* It's safe to write the exe_file pointer without exe_file_lock because
1231 * this is called during fork when the task is not yet in /proc */
1232 newmm->exe_file = get_mm_exe_file(oldmm);
1233}
1234
1235static int proc_exe_link(struct inode *inode, struct path *exe_path)
1236{
1237 struct task_struct *task;
1238 struct mm_struct *mm;
1239 struct file *exe_file;
1240
1241 task = get_proc_task(inode);
1242 if (!task)
1243 return -ENOENT;
1244 mm = get_task_mm(task);
1245 put_task_struct(task);
1246 if (!mm)
1247 return -ENOENT;
1248 exe_file = get_mm_exe_file(mm);
1249 mmput(mm);
1250 if (exe_file) {
1251 *exe_path = exe_file->f_path;
1252 path_get(&exe_file->f_path);
1253 fput(exe_file);
1254 return 0;
1255 } else
1256 return -ENOENT;
1257}
1258
Al Viro008b1502005-08-20 00:17:39 +01001259static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct inode *inode = dentry->d_inode;
1262 int error = -EACCES;
1263
1264 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001265 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Eric W. Biederman778c1142006-06-26 00:25:58 -07001267 /* Are we allowed to snoop on the tasks file descriptors? */
1268 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001271 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 nd->last_type = LAST_BIND;
1273out:
Al Viro008b1502005-08-20 00:17:39 +01001274 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001277static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Mel Gormane12ba742007-10-16 01:25:52 -07001279 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001280 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int len;
1282
1283 if (!tmp)
1284 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001285
Jan Blunckcf28b482008-02-14 19:38:44 -08001286 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001287 len = PTR_ERR(pathname);
1288 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001290 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 if (len > buflen)
1293 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001294 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 len = -EFAULT;
1296 out:
1297 free_page((unsigned long)tmp);
1298 return len;
1299}
1300
1301static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1302{
1303 int error = -EACCES;
1304 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001305 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Eric W. Biederman778c1142006-06-26 00:25:58 -07001307 /* Are we allowed to snoop on the tasks file descriptors? */
1308 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001311 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (error)
1313 goto out;
1314
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001315 error = do_proc_readlink(&path, buffer, buflen);
1316 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return error;
1319}
1320
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001321static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001323 .follow_link = proc_pid_follow_link,
1324 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325};
1326
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001327
1328/* building an inode */
1329
1330static int task_dumpable(struct task_struct *task)
1331{
1332 int dumpable = 0;
1333 struct mm_struct *mm;
1334
1335 task_lock(task);
1336 mm = task->mm;
1337 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001338 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001339 task_unlock(task);
1340 if(dumpable == 1)
1341 return 1;
1342 return 0;
1343}
1344
1345
Eric W. Biederman61a28782006-10-02 02:18:49 -07001346static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001347{
1348 struct inode * inode;
1349 struct proc_inode *ei;
1350
1351 /* We need a new inode */
1352
1353 inode = new_inode(sb);
1354 if (!inode)
1355 goto out;
1356
1357 /* Common stuff */
1358 ei = PROC_I(inode);
1359 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001360 inode->i_op = &proc_def_inode_operations;
1361
1362 /*
1363 * grab the reference to task.
1364 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001365 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001366 if (!ei->pid)
1367 goto out_unlock;
1368
1369 inode->i_uid = 0;
1370 inode->i_gid = 0;
1371 if (task_dumpable(task)) {
1372 inode->i_uid = task->euid;
1373 inode->i_gid = task->egid;
1374 }
1375 security_task_to_inode(task, inode);
1376
1377out:
1378 return inode;
1379
1380out_unlock:
1381 iput(inode);
1382 return NULL;
1383}
1384
1385static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1386{
1387 struct inode *inode = dentry->d_inode;
1388 struct task_struct *task;
1389 generic_fillattr(inode, stat);
1390
1391 rcu_read_lock();
1392 stat->uid = 0;
1393 stat->gid = 0;
1394 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1395 if (task) {
1396 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1397 task_dumpable(task)) {
1398 stat->uid = task->euid;
1399 stat->gid = task->egid;
1400 }
1401 }
1402 rcu_read_unlock();
1403 return 0;
1404}
1405
1406/* dentry stuff */
1407
1408/*
1409 * Exceptional case: normally we are not allowed to unhash a busy
1410 * directory. In this case, however, we can do it - no aliasing problems
1411 * due to the way we treat inodes.
1412 *
1413 * Rewrite the inode's ownerships here because the owning task may have
1414 * performed a setuid(), etc.
1415 *
1416 * Before the /proc/pid/status file was created the only way to read
1417 * the effective uid of a /process was to stat /proc/pid. Reading
1418 * /proc/pid/status is slow enough that procps and other packages
1419 * kept stating /proc/pid. To keep the rules in /proc simple I have
1420 * made this apply to all per process world readable and executable
1421 * directories.
1422 */
1423static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1424{
1425 struct inode *inode = dentry->d_inode;
1426 struct task_struct *task = get_proc_task(inode);
1427 if (task) {
1428 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1429 task_dumpable(task)) {
1430 inode->i_uid = task->euid;
1431 inode->i_gid = task->egid;
1432 } else {
1433 inode->i_uid = 0;
1434 inode->i_gid = 0;
1435 }
1436 inode->i_mode &= ~(S_ISUID | S_ISGID);
1437 security_task_to_inode(task, inode);
1438 put_task_struct(task);
1439 return 1;
1440 }
1441 d_drop(dentry);
1442 return 0;
1443}
1444
1445static int pid_delete_dentry(struct dentry * dentry)
1446{
1447 /* Is the task we represent dead?
1448 * If so, then don't put the dentry on the lru list,
1449 * kill it immediately.
1450 */
1451 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1452}
1453
1454static struct dentry_operations pid_dentry_operations =
1455{
1456 .d_revalidate = pid_revalidate,
1457 .d_delete = pid_delete_dentry,
1458};
1459
1460/* Lookups */
1461
Eric Dumazetc5141e62007-05-08 00:26:15 -07001462typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1463 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001464
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001465/*
1466 * Fill a directory entry.
1467 *
1468 * If possible create the dcache entry and derive our inode number and
1469 * file type from dcache entry.
1470 *
1471 * Since all of the proc inode numbers are dynamically generated, the inode
1472 * numbers do not exist until the inode is cache. This means creating the
1473 * the dcache entry in readdir is necessary to keep the inode numbers
1474 * reported by readdir in sync with the inode numbers reported
1475 * by stat.
1476 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001477static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1478 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001479 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001480{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001481 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001482 struct inode *inode;
1483 struct qstr qname;
1484 ino_t ino = 0;
1485 unsigned type = DT_UNKNOWN;
1486
1487 qname.name = name;
1488 qname.len = len;
1489 qname.hash = full_name_hash(name, len);
1490
1491 child = d_lookup(dir, &qname);
1492 if (!child) {
1493 struct dentry *new;
1494 new = d_alloc(dir, &qname);
1495 if (new) {
1496 child = instantiate(dir->d_inode, new, task, ptr);
1497 if (child)
1498 dput(new);
1499 else
1500 child = new;
1501 }
1502 }
1503 if (!child || IS_ERR(child) || !child->d_inode)
1504 goto end_instantiate;
1505 inode = child->d_inode;
1506 if (inode) {
1507 ino = inode->i_ino;
1508 type = inode->i_mode >> 12;
1509 }
1510 dput(child);
1511end_instantiate:
1512 if (!ino)
1513 ino = find_inode_number(dir, &qname);
1514 if (!ino)
1515 ino = 1;
1516 return filldir(dirent, name, len, filp->f_pos, ino, type);
1517}
1518
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001519static unsigned name_to_int(struct dentry *dentry)
1520{
1521 const char *name = dentry->d_name.name;
1522 int len = dentry->d_name.len;
1523 unsigned n = 0;
1524
1525 if (len > 1 && *name == '0')
1526 goto out;
1527 while (len-- > 0) {
1528 unsigned c = *name++ - '0';
1529 if (c > 9)
1530 goto out;
1531 if (n >= (~0U-9)/10)
1532 goto out;
1533 n *= 10;
1534 n += c;
1535 }
1536 return n;
1537out:
1538 return ~0U;
1539}
1540
Miklos Szeredi27932742007-05-08 00:26:17 -07001541#define PROC_FDINFO_MAX 64
1542
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001543static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001544{
1545 struct task_struct *task = get_proc_task(inode);
1546 struct files_struct *files = NULL;
1547 struct file *file;
1548 int fd = proc_fd(inode);
1549
1550 if (task) {
1551 files = get_files_struct(task);
1552 put_task_struct(task);
1553 }
1554 if (files) {
1555 /*
1556 * We are not taking a ref to the file structure, so we must
1557 * hold ->file_lock.
1558 */
1559 spin_lock(&files->file_lock);
1560 file = fcheck_files(files, fd);
1561 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001562 if (path) {
1563 *path = file->f_path;
1564 path_get(&file->f_path);
1565 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001566 if (info)
1567 snprintf(info, PROC_FDINFO_MAX,
1568 "pos:\t%lli\n"
1569 "flags:\t0%o\n",
1570 (long long) file->f_pos,
1571 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001572 spin_unlock(&files->file_lock);
1573 put_files_struct(files);
1574 return 0;
1575 }
1576 spin_unlock(&files->file_lock);
1577 put_files_struct(files);
1578 }
1579 return -ENOENT;
1580}
1581
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001582static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001583{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001584 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001585}
1586
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001587static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1588{
1589 struct inode *inode = dentry->d_inode;
1590 struct task_struct *task = get_proc_task(inode);
1591 int fd = proc_fd(inode);
1592 struct files_struct *files;
1593
1594 if (task) {
1595 files = get_files_struct(task);
1596 if (files) {
1597 rcu_read_lock();
1598 if (fcheck_files(files, fd)) {
1599 rcu_read_unlock();
1600 put_files_struct(files);
1601 if (task_dumpable(task)) {
1602 inode->i_uid = task->euid;
1603 inode->i_gid = task->egid;
1604 } else {
1605 inode->i_uid = 0;
1606 inode->i_gid = 0;
1607 }
1608 inode->i_mode &= ~(S_ISUID | S_ISGID);
1609 security_task_to_inode(task, inode);
1610 put_task_struct(task);
1611 return 1;
1612 }
1613 rcu_read_unlock();
1614 put_files_struct(files);
1615 }
1616 put_task_struct(task);
1617 }
1618 d_drop(dentry);
1619 return 0;
1620}
1621
1622static struct dentry_operations tid_fd_dentry_operations =
1623{
1624 .d_revalidate = tid_fd_revalidate,
1625 .d_delete = pid_delete_dentry,
1626};
1627
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001628static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001629 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001630{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001631 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001632 struct file *file;
1633 struct files_struct *files;
1634 struct inode *inode;
1635 struct proc_inode *ei;
1636 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001637
Eric W. Biederman61a28782006-10-02 02:18:49 -07001638 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001639 if (!inode)
1640 goto out;
1641 ei = PROC_I(inode);
1642 ei->fd = fd;
1643 files = get_files_struct(task);
1644 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001645 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001646 inode->i_mode = S_IFLNK;
1647
1648 /*
1649 * We are not taking a ref to the file structure, so we must
1650 * hold ->file_lock.
1651 */
1652 spin_lock(&files->file_lock);
1653 file = fcheck_files(files, fd);
1654 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001655 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001656 if (file->f_mode & 1)
1657 inode->i_mode |= S_IRUSR | S_IXUSR;
1658 if (file->f_mode & 2)
1659 inode->i_mode |= S_IWUSR | S_IXUSR;
1660 spin_unlock(&files->file_lock);
1661 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001662
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001663 inode->i_op = &proc_pid_link_inode_operations;
1664 inode->i_size = 64;
1665 ei->op.proc_get_link = proc_fd_link;
1666 dentry->d_op = &tid_fd_dentry_operations;
1667 d_add(dentry, inode);
1668 /* Close the race of the process dying before we return the dentry */
1669 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001670 error = NULL;
1671
1672 out:
1673 return error;
1674out_unlock:
1675 spin_unlock(&files->file_lock);
1676 put_files_struct(files);
1677out_iput:
1678 iput(inode);
1679 goto out;
1680}
1681
Miklos Szeredi27932742007-05-08 00:26:17 -07001682static struct dentry *proc_lookupfd_common(struct inode *dir,
1683 struct dentry *dentry,
1684 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001685{
1686 struct task_struct *task = get_proc_task(dir);
1687 unsigned fd = name_to_int(dentry);
1688 struct dentry *result = ERR_PTR(-ENOENT);
1689
1690 if (!task)
1691 goto out_no_task;
1692 if (fd == ~0U)
1693 goto out;
1694
Miklos Szeredi27932742007-05-08 00:26:17 -07001695 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001696out:
1697 put_task_struct(task);
1698out_no_task:
1699 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001700}
1701
Miklos Szeredi27932742007-05-08 00:26:17 -07001702static int proc_readfd_common(struct file * filp, void * dirent,
1703 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001705 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001706 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001707 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001708 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 struct files_struct * files;
1711
1712 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001713 if (!p)
1714 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 fd = filp->f_pos;
1718 switch (fd) {
1719 case 0:
1720 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1721 goto out;
1722 filp->f_pos++;
1723 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001724 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1726 goto out;
1727 filp->f_pos++;
1728 default:
1729 files = get_files_struct(p);
1730 if (!files)
1731 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001732 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001734 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001736 char name[PROC_NUMBUF];
1737 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (!fcheck_files(files, fd))
1740 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001741 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Miklos Szeredi27932742007-05-08 00:26:17 -07001743 len = snprintf(name, sizeof(name), "%d", fd);
1744 if (proc_fill_cache(filp, dirent, filldir,
1745 name, len, instantiate,
1746 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001747 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
1749 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001750 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001752 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 put_files_struct(files);
1754 }
1755out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001756 put_task_struct(p);
1757out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return retval;
1759}
1760
Miklos Szeredi27932742007-05-08 00:26:17 -07001761static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1762 struct nameidata *nd)
1763{
1764 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1765}
1766
1767static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1768{
1769 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1770}
1771
1772static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1773 size_t len, loff_t *ppos)
1774{
1775 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001776 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001777 if (!err)
1778 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1779 return err;
1780}
1781
1782static const struct file_operations proc_fdinfo_file_operations = {
1783 .open = nonseekable_open,
1784 .read = proc_fdinfo_read,
1785};
1786
Arjan van de Ven00977a52007-02-12 00:55:34 -08001787static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 .read = generic_read_dir,
1789 .readdir = proc_readfd,
1790};
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001793 * /proc/pid/fd needs a special permission handler so that a process can still
1794 * access /proc/self/fd after it has executed a setuid().
1795 */
1796static int proc_fd_permission(struct inode *inode, int mask,
1797 struct nameidata *nd)
1798{
1799 int rv;
1800
1801 rv = generic_permission(inode, mask, NULL);
1802 if (rv == 0)
1803 return 0;
1804 if (task_pid(current) == proc_pid(inode))
1805 rv = 0;
1806 return rv;
1807}
1808
1809/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 * proc directories can do almost nothing..
1811 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001812static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001814 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001815 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816};
1817
Miklos Szeredi27932742007-05-08 00:26:17 -07001818static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1819 struct dentry *dentry, struct task_struct *task, const void *ptr)
1820{
1821 unsigned fd = *(unsigned *)ptr;
1822 struct inode *inode;
1823 struct proc_inode *ei;
1824 struct dentry *error = ERR_PTR(-ENOENT);
1825
1826 inode = proc_pid_make_inode(dir->i_sb, task);
1827 if (!inode)
1828 goto out;
1829 ei = PROC_I(inode);
1830 ei->fd = fd;
1831 inode->i_mode = S_IFREG | S_IRUSR;
1832 inode->i_fop = &proc_fdinfo_file_operations;
1833 dentry->d_op = &tid_fd_dentry_operations;
1834 d_add(dentry, inode);
1835 /* Close the race of the process dying before we return the dentry */
1836 if (tid_fd_revalidate(dentry, NULL))
1837 error = NULL;
1838
1839 out:
1840 return error;
1841}
1842
1843static struct dentry *proc_lookupfdinfo(struct inode *dir,
1844 struct dentry *dentry,
1845 struct nameidata *nd)
1846{
1847 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1848}
1849
1850static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1851{
1852 return proc_readfd_common(filp, dirent, filldir,
1853 proc_fdinfo_instantiate);
1854}
1855
1856static const struct file_operations proc_fdinfo_operations = {
1857 .read = generic_read_dir,
1858 .readdir = proc_readfdinfo,
1859};
1860
1861/*
1862 * proc directories can do almost nothing..
1863 */
1864static const struct inode_operations proc_fdinfo_inode_operations = {
1865 .lookup = proc_lookupfdinfo,
1866 .setattr = proc_setattr,
1867};
1868
1869
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001870static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001871 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001872{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001873 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001874 struct inode *inode;
1875 struct proc_inode *ei;
1876 struct dentry *error = ERR_PTR(-EINVAL);
1877
Eric W. Biederman61a28782006-10-02 02:18:49 -07001878 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001879 if (!inode)
1880 goto out;
1881
1882 ei = PROC_I(inode);
1883 inode->i_mode = p->mode;
1884 if (S_ISDIR(inode->i_mode))
1885 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1886 if (p->iop)
1887 inode->i_op = p->iop;
1888 if (p->fop)
1889 inode->i_fop = p->fop;
1890 ei->op = p->op;
1891 dentry->d_op = &pid_dentry_operations;
1892 d_add(dentry, inode);
1893 /* Close the race of the process dying before we return the dentry */
1894 if (pid_revalidate(dentry, NULL))
1895 error = NULL;
1896out:
1897 return error;
1898}
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900static struct dentry *proc_pident_lookup(struct inode *dir,
1901 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001902 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001903 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001906 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001907 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001908 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001910 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 inode = NULL;
1912
Eric W. Biederman99f89552006-06-26 00:25:55 -07001913 if (!task)
1914 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001916 /*
1917 * Yes, it does not scale. And it should not. Don't add
1918 * new entries into /proc/<tgid>/ without very good reasons.
1919 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001920 last = &ents[nents - 1];
1921 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (p->len != dentry->d_name.len)
1923 continue;
1924 if (!memcmp(dentry->d_name.name, p->name, p->len))
1925 break;
1926 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001927 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 goto out;
1929
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001930 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001932 put_task_struct(task);
1933out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001934 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
Eric Dumazetc5141e62007-05-08 00:26:15 -07001937static int proc_pident_fill_cache(struct file *filp, void *dirent,
1938 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001939{
1940 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1941 proc_pident_instantiate, task, p);
1942}
1943
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001944static int proc_pident_readdir(struct file *filp,
1945 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001946 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001947{
1948 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001949 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001950 struct inode *inode = dentry->d_inode;
1951 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001952 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001953 ino_t ino;
1954 int ret;
1955
1956 ret = -ENOENT;
1957 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001958 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001959
1960 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001961 i = filp->f_pos;
1962 switch (i) {
1963 case 0:
1964 ino = inode->i_ino;
1965 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1966 goto out;
1967 i++;
1968 filp->f_pos++;
1969 /* fall through */
1970 case 1:
1971 ino = parent_ino(dentry);
1972 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1973 goto out;
1974 i++;
1975 filp->f_pos++;
1976 /* fall through */
1977 default:
1978 i -= 2;
1979 if (i >= nents) {
1980 ret = 1;
1981 goto out;
1982 }
1983 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001984 last = &ents[nents - 1];
1985 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001986 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001987 goto out;
1988 filp->f_pos++;
1989 p++;
1990 }
1991 }
1992
1993 ret = 1;
1994out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001995 put_task_struct(task);
1996out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001997 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002001static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2002 size_t count, loff_t *ppos)
2003{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002004 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002005 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002006 ssize_t length;
2007 struct task_struct *task = get_proc_task(inode);
2008
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002009 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002010 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002011
2012 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002013 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002014 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002015 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002016 if (length > 0)
2017 length = simple_read_from_buffer(buf, count, ppos, p, length);
2018 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002019 return length;
2020}
2021
2022static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2023 size_t count, loff_t *ppos)
2024{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002025 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002026 char *page;
2027 ssize_t length;
2028 struct task_struct *task = get_proc_task(inode);
2029
2030 length = -ESRCH;
2031 if (!task)
2032 goto out_no_task;
2033 if (count > PAGE_SIZE)
2034 count = PAGE_SIZE;
2035
2036 /* No partial writes. */
2037 length = -EINVAL;
2038 if (*ppos != 0)
2039 goto out;
2040
2041 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002042 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002043 if (!page)
2044 goto out;
2045
2046 length = -EFAULT;
2047 if (copy_from_user(page, buf, count))
2048 goto out_free;
2049
2050 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002051 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002052 (void*)page, count);
2053out_free:
2054 free_page((unsigned long) page);
2055out:
2056 put_task_struct(task);
2057out_no_task:
2058 return length;
2059}
2060
Arjan van de Ven00977a52007-02-12 00:55:34 -08002061static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002062 .read = proc_pid_attr_read,
2063 .write = proc_pid_attr_write,
2064};
2065
Eric Dumazetc5141e62007-05-08 00:26:15 -07002066static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002067 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2068 REG("prev", S_IRUGO, pid_attr),
2069 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2070 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2071 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2072 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002073};
2074
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002075static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 void * dirent, filldir_t filldir)
2077{
2078 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002079 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Arjan van de Ven00977a52007-02-12 00:55:34 -08002082static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002084 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085};
2086
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002087static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 struct dentry *dentry, struct nameidata *nd)
2089{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002090 return proc_pident_lookup(dir, dentry,
2091 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002094static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002095 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002096 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002097 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098};
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100#endif
2101
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002102#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2103static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2104 size_t count, loff_t *ppos)
2105{
2106 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2107 struct mm_struct *mm;
2108 char buffer[PROC_NUMBUF];
2109 size_t len;
2110 int ret;
2111
2112 if (!task)
2113 return -ESRCH;
2114
2115 ret = 0;
2116 mm = get_task_mm(task);
2117 if (mm) {
2118 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2119 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2120 MMF_DUMP_FILTER_SHIFT));
2121 mmput(mm);
2122 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2123 }
2124
2125 put_task_struct(task);
2126
2127 return ret;
2128}
2129
2130static ssize_t proc_coredump_filter_write(struct file *file,
2131 const char __user *buf,
2132 size_t count,
2133 loff_t *ppos)
2134{
2135 struct task_struct *task;
2136 struct mm_struct *mm;
2137 char buffer[PROC_NUMBUF], *end;
2138 unsigned int val;
2139 int ret;
2140 int i;
2141 unsigned long mask;
2142
2143 ret = -EFAULT;
2144 memset(buffer, 0, sizeof(buffer));
2145 if (count > sizeof(buffer) - 1)
2146 count = sizeof(buffer) - 1;
2147 if (copy_from_user(buffer, buf, count))
2148 goto out_no_task;
2149
2150 ret = -EINVAL;
2151 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2152 if (*end == '\n')
2153 end++;
2154 if (end - buffer == 0)
2155 goto out_no_task;
2156
2157 ret = -ESRCH;
2158 task = get_proc_task(file->f_dentry->d_inode);
2159 if (!task)
2160 goto out_no_task;
2161
2162 ret = end - buffer;
2163 mm = get_task_mm(task);
2164 if (!mm)
2165 goto out_no_mm;
2166
2167 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2168 if (val & mask)
2169 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2170 else
2171 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2172 }
2173
2174 mmput(mm);
2175 out_no_mm:
2176 put_task_struct(task);
2177 out_no_task:
2178 return ret;
2179}
2180
2181static const struct file_operations proc_coredump_filter_operations = {
2182 .read = proc_coredump_filter_read,
2183 .write = proc_coredump_filter_write,
2184};
2185#endif
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/*
2188 * /proc/self:
2189 */
2190static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2191 int buflen)
2192{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002193 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002194 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002195 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002196 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002197 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002198 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return vfs_readlink(dentry,buffer,buflen,tmp);
2200}
2201
Al Viro008b1502005-08-20 00:17:39 +01002202static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002204 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002205 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002206 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002207 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002208 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002209 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002210 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002213static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 .readlink = proc_self_readlink,
2215 .follow_link = proc_self_follow_link,
2216};
2217
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002218/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002219 * proc base
2220 *
2221 * These are the directory entries in the root directory of /proc
2222 * that properly belong to the /proc filesystem, as they describe
2223 * describe something that is process related.
2224 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002225static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002226 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002227 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002228};
2229
2230/*
2231 * Exceptional case: normally we are not allowed to unhash a busy
2232 * directory. In this case, however, we can do it - no aliasing problems
2233 * due to the way we treat inodes.
2234 */
2235static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2236{
2237 struct inode *inode = dentry->d_inode;
2238 struct task_struct *task = get_proc_task(inode);
2239 if (task) {
2240 put_task_struct(task);
2241 return 1;
2242 }
2243 d_drop(dentry);
2244 return 0;
2245}
2246
2247static struct dentry_operations proc_base_dentry_operations =
2248{
2249 .d_revalidate = proc_base_revalidate,
2250 .d_delete = pid_delete_dentry,
2251};
2252
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002253static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002254 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002255{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002256 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002257 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002258 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002259 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002260
2261 /* Allocate the inode */
2262 error = ERR_PTR(-ENOMEM);
2263 inode = new_inode(dir->i_sb);
2264 if (!inode)
2265 goto out;
2266
2267 /* Initialize the inode */
2268 ei = PROC_I(inode);
2269 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002270
2271 /*
2272 * grab the reference to the task.
2273 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002274 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002275 if (!ei->pid)
2276 goto out_iput;
2277
2278 inode->i_uid = 0;
2279 inode->i_gid = 0;
2280 inode->i_mode = p->mode;
2281 if (S_ISDIR(inode->i_mode))
2282 inode->i_nlink = 2;
2283 if (S_ISLNK(inode->i_mode))
2284 inode->i_size = 64;
2285 if (p->iop)
2286 inode->i_op = p->iop;
2287 if (p->fop)
2288 inode->i_fop = p->fop;
2289 ei->op = p->op;
2290 dentry->d_op = &proc_base_dentry_operations;
2291 d_add(dentry, inode);
2292 error = NULL;
2293out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002294 return error;
2295out_iput:
2296 iput(inode);
2297 goto out;
2298}
2299
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002300static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2301{
2302 struct dentry *error;
2303 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002304 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002305
2306 error = ERR_PTR(-ENOENT);
2307
2308 if (!task)
2309 goto out_no_task;
2310
2311 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002312 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2313 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002314 if (p->len != dentry->d_name.len)
2315 continue;
2316 if (!memcmp(dentry->d_name.name, p->name, p->len))
2317 break;
2318 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002319 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002320 goto out;
2321
2322 error = proc_base_instantiate(dir, dentry, task, p);
2323
2324out:
2325 put_task_struct(task);
2326out_no_task:
2327 return error;
2328}
2329
Eric Dumazetc5141e62007-05-08 00:26:15 -07002330static int proc_base_fill_cache(struct file *filp, void *dirent,
2331 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002332{
2333 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2334 proc_base_instantiate, task, p);
2335}
2336
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002337#ifdef CONFIG_TASK_IO_ACCOUNTING
2338static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2339{
2340 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002341#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002342 "rchar: %llu\n"
2343 "wchar: %llu\n"
2344 "syscr: %llu\n"
2345 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002346#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002347 "read_bytes: %llu\n"
2348 "write_bytes: %llu\n"
2349 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002350#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002351 (unsigned long long)task->rchar,
2352 (unsigned long long)task->wchar,
2353 (unsigned long long)task->syscr,
2354 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002355#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002356 (unsigned long long)task->ioac.read_bytes,
2357 (unsigned long long)task->ioac.write_bytes,
2358 (unsigned long long)task->ioac.cancelled_write_bytes);
2359}
2360#endif
2361
Eric W. Biederman801199c2006-10-02 02:18:48 -07002362/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002363 * Thread groups
2364 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002365static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002366static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002367
Eric Dumazetc5141e62007-05-08 00:26:15 -07002368static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002369 DIR("task", S_IRUGO|S_IXUGO, task),
2370 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002371 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002372#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002373 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002374#endif
James Pearson315e28c2007-10-16 23:30:17 -07002375 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002376 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002377 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002378 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002379#ifdef CONFIG_SCHED_DEBUG
2380 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2381#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002382 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002383 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002384 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002385 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002386#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002387 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002388#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002389 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002390 LNK("cwd", cwd),
2391 LNK("root", root),
2392 LNK("exe", exe),
2393 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002394 REG("mountinfo", S_IRUGO, mountinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002395 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002396#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002397 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002398 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002399 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002400#endif
2401#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002402 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002403#endif
2404#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002405 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002406#endif
2407#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002408 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002409#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002410#ifdef CONFIG_LATENCYTOP
2411 REG("latency", S_IRUGO, lstats),
2412#endif
Paul Menage8793d852007-10-18 23:39:39 -07002413#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002414 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002415#endif
Paul Menagea4243162007-10-18 23:39:35 -07002416#ifdef CONFIG_CGROUPS
2417 REG("cgroup", S_IRUGO, cgroup),
2418#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002419 INF("oom_score", S_IRUGO, oom_score),
2420 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002421#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002422 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002423 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002424#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002425#ifdef CONFIG_FAULT_INJECTION
2426 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2427#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002428#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2429 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2430#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002431#ifdef CONFIG_TASK_IO_ACCOUNTING
2432 INF("io", S_IRUGO, pid_io_accounting),
2433#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002434};
2435
2436static int proc_tgid_base_readdir(struct file * filp,
2437 void * dirent, filldir_t filldir)
2438{
2439 return proc_pident_readdir(filp,dirent,filldir,
2440 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2441}
2442
Arjan van de Ven00977a52007-02-12 00:55:34 -08002443static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002444 .read = generic_read_dir,
2445 .readdir = proc_tgid_base_readdir,
2446};
2447
2448static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002449 return proc_pident_lookup(dir, dentry,
2450 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002451}
2452
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002453static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002454 .lookup = proc_tgid_base_lookup,
2455 .getattr = pid_getattr,
2456 .setattr = proc_setattr,
2457};
2458
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002459static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002461 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002462 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002463 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Eric W. Biederman48e64842006-06-26 00:25:48 -07002465 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002466 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2467 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002468 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002469 if (!(current->flags & PF_EXITING))
2470 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002471 d_drop(dentry);
2472 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002475 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002476 goto out;
2477
2478 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002479 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2480 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002481 if (!leader)
2482 goto out;
2483
2484 name.name = "task";
2485 name.len = strlen(name.name);
2486 dir = d_hash_and_lookup(leader, &name);
2487 if (!dir)
2488 goto out_put_leader;
2489
2490 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002491 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002492 dentry = d_hash_and_lookup(dir, &name);
2493 if (dentry) {
2494 shrink_dcache_parent(dentry);
2495 d_drop(dentry);
2496 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002498
2499 dput(dir);
2500out_put_leader:
2501 dput(leader);
2502out:
2503 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Randy Dunlap0895e912007-10-21 21:00:10 -07002506/**
2507 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2508 * @task: task that should be flushed.
2509 *
2510 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002511 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002512 * in. This call is supposed to do all of this job.
2513 *
2514 * Looks in the dcache for
2515 * /proc/@pid
2516 * /proc/@tgid/task/@pid
2517 * if either directory is present flushes it and all of it'ts children
2518 * from the dcache.
2519 *
2520 * It is safe and reasonable to cache /proc entries for a task until
2521 * that task exits. After that they just clog up the dcache with
2522 * useless entries, possibly causing useful dcache entries to be
2523 * flushed instead. This routine is proved to flush those useless
2524 * dcache entries at process exit time.
2525 *
2526 * NOTE: This routine is just an optimization so it does not guarantee
2527 * that no dcache entries will exist at process exit time it
2528 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002529 */
2530
2531void proc_flush_task(struct task_struct *task)
2532{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002533 int i;
2534 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002535 struct upid *upid;
2536
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002537 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002538 if (thread_group_leader(task))
2539 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002540
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002541 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002542 upid = &pid->numbers[i];
2543 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002544 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002545 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002546
2547 upid = &pid->numbers[pid->level];
2548 if (upid->nr == 1)
2549 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002550}
2551
Adrian Bunk9711ef92006-12-06 20:38:31 -08002552static struct dentry *proc_pid_instantiate(struct inode *dir,
2553 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002554 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002555{
2556 struct dentry *error = ERR_PTR(-ENOENT);
2557 struct inode *inode;
2558
Eric W. Biederman61a28782006-10-02 02:18:49 -07002559 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002560 if (!inode)
2561 goto out;
2562
2563 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2564 inode->i_op = &proc_tgid_base_inode_operations;
2565 inode->i_fop = &proc_tgid_base_operations;
2566 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002567 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002568#ifdef CONFIG_SECURITY
2569 inode->i_nlink += 1;
2570#endif
2571
2572 dentry->d_op = &pid_dentry_operations;
2573
2574 d_add(dentry, inode);
2575 /* Close the race of the process dying before we return the dentry */
2576 if (pid_revalidate(dentry, NULL))
2577 error = NULL;
2578out:
2579 return error;
2580}
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2583{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002584 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002587 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Eric W. Biederman801199c2006-10-02 02:18:48 -07002589 result = proc_base_lookup(dir, dentry);
2590 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2591 goto out;
2592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 tgid = name_to_int(dentry);
2594 if (tgid == ~0U)
2595 goto out;
2596
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002597 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002598 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002599 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (task)
2601 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002602 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (!task)
2604 goto out;
2605
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002606 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002607 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002609 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002613 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002614 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002616struct tgid_iter {
2617 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002618 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002619};
2620static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2621{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002622 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002624 if (iter.task)
2625 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002626 rcu_read_lock();
2627retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002628 iter.task = NULL;
2629 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002630 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002631 iter.tgid = pid_nr_ns(pid, ns);
2632 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002633 /* What we to know is if the pid we have find is the
2634 * pid of a thread_group_leader. Testing for task
2635 * being a thread_group_leader is the obvious thing
2636 * todo but there is a window when it fails, due to
2637 * the pid transfer logic in de_thread.
2638 *
2639 * So we perform the straight forward test of seeing
2640 * if the pid we have found is the pid of a thread
2641 * group leader, and don't worry if the task we have
2642 * found doesn't happen to be a thread group leader.
2643 * As we don't care in the case of readdir.
2644 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002645 if (!iter.task || !has_group_leader_pid(iter.task)) {
2646 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002647 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002648 }
2649 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002651 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002652 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653}
2654
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002655#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Eric W. Biederman61a28782006-10-02 02:18:49 -07002657static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002658 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002659{
2660 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002661 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002662 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002663 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002664}
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666/* for the /proc/ directory itself, after non-process stuff has been done */
2667int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2668{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002670 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002671 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002672 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Eric W. Biederman61a28782006-10-02 02:18:49 -07002674 if (!reaper)
2675 goto out_no_task;
2676
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002677 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002678 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002679 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002680 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
2682
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002683 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002684 iter.task = NULL;
2685 iter.tgid = filp->f_pos - TGID_OFFSET;
2686 for (iter = next_tgid(ns, iter);
2687 iter.task;
2688 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2689 filp->f_pos = iter.tgid + TGID_OFFSET;
2690 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2691 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002692 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002695 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2696out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002697 put_task_struct(reaper);
2698out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 return 0;
2700}
2701
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002702/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002703 * Tasks
2704 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002705static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002706 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002707 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002708 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002709 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002710 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002711 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002712#ifdef CONFIG_SCHED_DEBUG
2713 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2714#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002715 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002716 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002717 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002718 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002719#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002720 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002721#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002722 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002723 LNK("cwd", cwd),
2724 LNK("root", root),
2725 LNK("exe", exe),
2726 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002727 REG("mountinfo", S_IRUGO, mountinfo),
Matt Mackall1e883282008-02-04 22:29:07 -08002728#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002729 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002730 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002731 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002732#endif
2733#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002734 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002735#endif
2736#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002737 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002738#endif
2739#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002740 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002741#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002742#ifdef CONFIG_LATENCYTOP
2743 REG("latency", S_IRUGO, lstats),
2744#endif
Paul Menage8793d852007-10-18 23:39:39 -07002745#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002746 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002747#endif
Paul Menagea4243162007-10-18 23:39:35 -07002748#ifdef CONFIG_CGROUPS
2749 REG("cgroup", S_IRUGO, cgroup),
2750#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002751 INF("oom_score", S_IRUGO, oom_score),
2752 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002753#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002754 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002755 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002756#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002757#ifdef CONFIG_FAULT_INJECTION
2758 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2759#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002760};
2761
2762static int proc_tid_base_readdir(struct file * filp,
2763 void * dirent, filldir_t filldir)
2764{
2765 return proc_pident_readdir(filp,dirent,filldir,
2766 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2767}
2768
2769static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002770 return proc_pident_lookup(dir, dentry,
2771 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002772}
2773
Arjan van de Ven00977a52007-02-12 00:55:34 -08002774static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002775 .read = generic_read_dir,
2776 .readdir = proc_tid_base_readdir,
2777};
2778
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002779static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002780 .lookup = proc_tid_base_lookup,
2781 .getattr = pid_getattr,
2782 .setattr = proc_setattr,
2783};
2784
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002785static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002786 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002787{
2788 struct dentry *error = ERR_PTR(-ENOENT);
2789 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002790 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002791
2792 if (!inode)
2793 goto out;
2794 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2795 inode->i_op = &proc_tid_base_inode_operations;
2796 inode->i_fop = &proc_tid_base_operations;
2797 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002798 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002799#ifdef CONFIG_SECURITY
2800 inode->i_nlink += 1;
2801#endif
2802
2803 dentry->d_op = &pid_dentry_operations;
2804
2805 d_add(dentry, inode);
2806 /* Close the race of the process dying before we return the dentry */
2807 if (pid_revalidate(dentry, NULL))
2808 error = NULL;
2809out:
2810 return error;
2811}
2812
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002813static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2814{
2815 struct dentry *result = ERR_PTR(-ENOENT);
2816 struct task_struct *task;
2817 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002818 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002819 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002820
2821 if (!leader)
2822 goto out_no_task;
2823
2824 tid = name_to_int(dentry);
2825 if (tid == ~0U)
2826 goto out;
2827
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002828 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002829 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002830 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002831 if (task)
2832 get_task_struct(task);
2833 rcu_read_unlock();
2834 if (!task)
2835 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002836 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002837 goto out_drop_task;
2838
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002839 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002840out_drop_task:
2841 put_task_struct(task);
2842out:
2843 put_task_struct(leader);
2844out_no_task:
2845 return result;
2846}
2847
2848/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002849 * Find the first tid of a thread group to return to user space.
2850 *
2851 * Usually this is just the thread group leader, but if the users
2852 * buffer was too small or there was a seek into the middle of the
2853 * directory we have more work todo.
2854 *
2855 * In the case of a short read we start with find_task_by_pid.
2856 *
2857 * In the case of a seek we start with the leader and walk nr
2858 * threads past it.
2859 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002860static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002861 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002862{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002863 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002864
Eric W. Biedermancc288732006-06-26 00:26:01 -07002865 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002866 /* Attempt to start with the pid of a thread */
2867 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002868 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002869 if (pos && (pos->group_leader == leader))
2870 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002871 }
2872
2873 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002874 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002875 if (nr && nr >= get_nr_threads(leader))
2876 goto out;
2877
2878 /* If we haven't found our starting place yet start
2879 * with the leader and walk nr threads forward.
2880 */
2881 for (pos = leader; nr > 0; --nr) {
2882 pos = next_thread(pos);
2883 if (pos == leader) {
2884 pos = NULL;
2885 goto out;
2886 }
2887 }
2888found:
2889 get_task_struct(pos);
2890out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002891 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002892 return pos;
2893}
2894
2895/*
2896 * Find the next thread in the thread list.
2897 * Return NULL if there is an error or no next thread.
2898 *
2899 * The reference to the input task_struct is released.
2900 */
2901static struct task_struct *next_tid(struct task_struct *start)
2902{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002903 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002904 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002905 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002906 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002907 if (thread_group_leader(pos))
2908 pos = NULL;
2909 else
2910 get_task_struct(pos);
2911 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002912 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002913 put_task_struct(start);
2914 return pos;
2915}
2916
Eric W. Biederman61a28782006-10-02 02:18:49 -07002917static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2918 struct task_struct *task, int tid)
2919{
2920 char name[PROC_NUMBUF];
2921 int len = snprintf(name, sizeof(name), "%d", tid);
2922 return proc_fill_cache(filp, dirent, filldir, name, len,
2923 proc_task_instantiate, task, NULL);
2924}
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926/* for the /proc/TGID/task/ directories */
2927static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2928{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002929 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002931 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002932 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 int retval = -ENOENT;
2934 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002935 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002937 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002939 task = get_proc_task(inode);
2940 if (!task)
2941 goto out_no_task;
2942 rcu_read_lock();
2943 if (pid_alive(task)) {
2944 leader = task->group_leader;
2945 get_task_struct(leader);
2946 }
2947 rcu_read_unlock();
2948 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002949 if (!leader)
2950 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 retval = 0;
2952
2953 switch (pos) {
2954 case 0:
2955 ino = inode->i_ino;
2956 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2957 goto out;
2958 pos++;
2959 /* fall through */
2960 case 1:
2961 ino = parent_ino(dentry);
2962 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2963 goto out;
2964 pos++;
2965 /* fall through */
2966 }
2967
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002968 /* f_version caches the tgid value that the last readdir call couldn't
2969 * return. lseek aka telldir automagically resets f_version to 0.
2970 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002971 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002972 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002973 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002974 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002975 task;
2976 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002977 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002978 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002979 /* returning this tgid failed, save it as the first
2980 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002981 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002982 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
2986out:
2987 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002988 put_task_struct(leader);
2989out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return retval;
2991}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002992
2993static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2994{
2995 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002996 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002997 generic_fillattr(inode, stat);
2998
Eric W. Biederman99f89552006-06-26 00:25:55 -07002999 if (p) {
3000 rcu_read_lock();
3001 stat->nlink += get_nr_threads(p);
3002 rcu_read_unlock();
3003 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003004 }
3005
3006 return 0;
3007}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003008
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003009static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003010 .lookup = proc_task_lookup,
3011 .getattr = proc_task_getattr,
3012 .setattr = proc_setattr,
3013};
3014
Arjan van de Ven00977a52007-02-12 00:55:34 -08003015static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003016 .read = generic_read_dir,
3017 .readdir = proc_task_readdir,
3018};