blob: 989e3078d7af5344fcbd2e65dd09b2d46e4fee63 [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;
317 struct task_struct *task = m->private;
318 seq_puts(m, "Latency Top version : v0.1\n");
319
320 for (i = 0; i < 32; i++) {
321 if (task->latency_record[i].backtrace[0]) {
322 int q;
323 seq_printf(m, "%i %li %li ",
324 task->latency_record[i].count,
325 task->latency_record[i].time,
326 task->latency_record[i].max);
327 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
328 char sym[KSYM_NAME_LEN];
329 char *c;
330 if (!task->latency_record[i].backtrace[q])
331 break;
332 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
333 break;
334 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
335 c = strchr(sym, '+');
336 if (c)
337 *c = 0;
338 seq_printf(m, "%s ", sym);
339 }
340 seq_printf(m, "\n");
341 }
342
343 }
344 return 0;
345}
346
347static int lstats_open(struct inode *inode, struct file *file)
348{
349 int ret;
350 struct seq_file *m;
351 struct task_struct *task = get_proc_task(inode);
352
Hiroshi Shimamotoae002782008-02-14 10:26:24 -0800353 if (!task)
354 return -ENOENT;
Arjan van de Ven97455122008-01-25 21:08:34 +0100355 ret = single_open(file, lstats_show_proc, NULL);
356 if (!ret) {
357 m = file->private_data;
358 m->private = task;
359 }
360 return ret;
361}
362
363static ssize_t lstats_write(struct file *file, const char __user *buf,
364 size_t count, loff_t *offs)
365{
366 struct seq_file *m;
367 struct task_struct *task;
368
369 m = file->private_data;
370 task = m->private;
371 clear_all_latency_tracing(task);
372
373 return count;
374}
375
376static const struct file_operations proc_lstats_operations = {
377 .open = lstats_open,
378 .read = seq_read,
379 .write = lstats_write,
380 .llseek = seq_lseek,
381 .release = single_release,
382};
383
384#endif
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/* The badness from the OOM killer */
387unsigned long badness(struct task_struct *p, unsigned long uptime);
388static int proc_oom_score(struct task_struct *task, char *buffer)
389{
390 unsigned long points;
391 struct timespec uptime;
392
393 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700394 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700396 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return sprintf(buffer, "%lu\n", points);
398}
399
Neil Hormand85f50d2007-10-18 23:40:37 -0700400struct limit_names {
401 char *name;
402 char *unit;
403};
404
405static const struct limit_names lnames[RLIM_NLIMITS] = {
406 [RLIMIT_CPU] = {"Max cpu time", "ms"},
407 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
408 [RLIMIT_DATA] = {"Max data size", "bytes"},
409 [RLIMIT_STACK] = {"Max stack size", "bytes"},
410 [RLIMIT_CORE] = {"Max core file size", "bytes"},
411 [RLIMIT_RSS] = {"Max resident set", "bytes"},
412 [RLIMIT_NPROC] = {"Max processes", "processes"},
413 [RLIMIT_NOFILE] = {"Max open files", "files"},
414 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
415 [RLIMIT_AS] = {"Max address space", "bytes"},
416 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
417 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
418 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
419 [RLIMIT_NICE] = {"Max nice priority", NULL},
420 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800421 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700422};
423
424/* Display limits for a process */
425static int proc_pid_limits(struct task_struct *task, char *buffer)
426{
427 unsigned int i;
428 int count = 0;
429 unsigned long flags;
430 char *bufptr = buffer;
431
432 struct rlimit rlim[RLIM_NLIMITS];
433
434 rcu_read_lock();
435 if (!lock_task_sighand(task,&flags)) {
436 rcu_read_unlock();
437 return 0;
438 }
439 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
440 unlock_task_sighand(task, &flags);
441 rcu_read_unlock();
442
443 /*
444 * print the file header
445 */
446 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
447 "Limit", "Soft Limit", "Hard Limit", "Units");
448
449 for (i = 0; i < RLIM_NLIMITS; i++) {
450 if (rlim[i].rlim_cur == RLIM_INFINITY)
451 count += sprintf(&bufptr[count], "%-25s %-20s ",
452 lnames[i].name, "unlimited");
453 else
454 count += sprintf(&bufptr[count], "%-25s %-20lu ",
455 lnames[i].name, rlim[i].rlim_cur);
456
457 if (rlim[i].rlim_max == RLIM_INFINITY)
458 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
459 else
460 count += sprintf(&bufptr[count], "%-20lu ",
461 rlim[i].rlim_max);
462
463 if (lnames[i].unit)
464 count += sprintf(&bufptr[count], "%-10s\n",
465 lnames[i].unit);
466 else
467 count += sprintf(&bufptr[count], "\n");
468 }
469
470 return count;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/************************************************************************/
474/* Here the fs part begins */
475/************************************************************************/
476
477/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700478static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700480 struct task_struct *task;
481 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700482 /* Allow access to a task's file descriptors if it is us or we
483 * may use ptrace attach to the process and find out that
484 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700485 */
486 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700487 if (task) {
488 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700489 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700490 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700491 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700494static int proc_setattr(struct dentry *dentry, struct iattr *attr)
495{
496 int error;
497 struct inode *inode = dentry->d_inode;
498
499 if (attr->ia_valid & ATTR_MODE)
500 return -EPERM;
501
502 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700503 if (!error)
504 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700505 return error;
506}
507
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800508static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700509 .setattr = proc_setattr,
510};
511
Jan Engelhardt03a44822008-02-08 04:21:19 -0800512extern const struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500513struct proc_mounts {
514 struct seq_file m;
515 int event;
516};
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static int mounts_open(struct inode *inode, struct file *file)
519{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700520 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700521 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800522 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500523 struct proc_mounts *p;
524 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Eric W. Biederman99f89552006-06-26 00:25:55 -0700526 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700527 rcu_read_lock();
528 nsp = task_nsproxy(task);
529 if (nsp) {
530 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800531 if (ns)
532 get_mnt_ns(ns);
533 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700534 rcu_read_unlock();
535
Eric W. Biederman99f89552006-06-26 00:25:55 -0700536 put_task_struct(task);
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800539 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500540 ret = -ENOMEM;
541 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
542 if (p) {
543 file->private_data = &p->m;
544 ret = seq_open(file, &mounts_op);
545 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800546 p->m.private = ns;
547 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500548 return 0;
549 }
550 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800552 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 return ret;
555}
556
557static int mounts_release(struct inode *inode, struct file *file)
558{
559 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800560 struct mnt_namespace *ns = m->private;
561 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return seq_release(inode, file);
563}
564
Al Viro5addc5d2005-11-07 17:15:49 -0500565static unsigned mounts_poll(struct file *file, poll_table *wait)
566{
567 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800568 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500569 unsigned res = 0;
570
571 poll_wait(file, &ns->poll, wait);
572
573 spin_lock(&vfsmount_lock);
574 if (p->event != ns->event) {
575 p->event = ns->event;
576 res = POLLERR;
577 }
578 spin_unlock(&vfsmount_lock);
579
580 return res;
581}
582
Arjan van de Ven00977a52007-02-12 00:55:34 -0800583static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 .open = mounts_open,
585 .read = seq_read,
586 .llseek = seq_lseek,
587 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500588 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589};
590
Jan Engelhardt03a44822008-02-08 04:21:19 -0800591extern const struct seq_operations mountstats_op;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500592static int mountstats_open(struct inode *inode, struct file *file)
593{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500594 int ret = seq_open(file, &mountstats_op);
595
596 if (!ret) {
597 struct seq_file *m = file->private_data;
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700598 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800599 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700600 struct task_struct *task = get_proc_task(inode);
601
602 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700603 rcu_read_lock();
604 nsp = task_nsproxy(task);
605 if (nsp) {
606 mnt_ns = nsp->mnt_ns;
607 if (mnt_ns)
608 get_mnt_ns(mnt_ns);
609 }
610 rcu_read_unlock();
611
Eric W. Biederman99f89552006-06-26 00:25:55 -0700612 put_task_struct(task);
613 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500614
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800615 if (mnt_ns)
616 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500617 else {
618 seq_release(inode, file);
619 ret = -EINVAL;
620 }
621 }
622 return ret;
623}
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};
1046#endif
1047
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001048#ifdef CONFIG_FAULT_INJECTION
1049static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1050 size_t count, loff_t *ppos)
1051{
1052 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1053 char buffer[PROC_NUMBUF];
1054 size_t len;
1055 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001056
1057 if (!task)
1058 return -ESRCH;
1059 make_it_fail = task->make_it_fail;
1060 put_task_struct(task);
1061
1062 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001063
1064 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001065}
1066
1067static ssize_t proc_fault_inject_write(struct file * file,
1068 const char __user * buf, size_t count, loff_t *ppos)
1069{
1070 struct task_struct *task;
1071 char buffer[PROC_NUMBUF], *end;
1072 int make_it_fail;
1073
1074 if (!capable(CAP_SYS_RESOURCE))
1075 return -EPERM;
1076 memset(buffer, 0, sizeof(buffer));
1077 if (count > sizeof(buffer) - 1)
1078 count = sizeof(buffer) - 1;
1079 if (copy_from_user(buffer, buf, count))
1080 return -EFAULT;
1081 make_it_fail = simple_strtol(buffer, &end, 0);
1082 if (*end == '\n')
1083 end++;
1084 task = get_proc_task(file->f_dentry->d_inode);
1085 if (!task)
1086 return -ESRCH;
1087 task->make_it_fail = make_it_fail;
1088 put_task_struct(task);
1089 if (end - buffer == 0)
1090 return -EIO;
1091 return end - buffer;
1092}
1093
Arjan van de Ven00977a52007-02-12 00:55:34 -08001094static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001095 .read = proc_fault_inject_read,
1096 .write = proc_fault_inject_write,
1097};
1098#endif
1099
Arjan van de Ven97455122008-01-25 21:08:34 +01001100
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001101#ifdef CONFIG_SCHED_DEBUG
1102/*
1103 * Print out various scheduling related per-task fields:
1104 */
1105static int sched_show(struct seq_file *m, void *v)
1106{
1107 struct inode *inode = m->private;
1108 struct task_struct *p;
1109
1110 WARN_ON(!inode);
1111
1112 p = get_proc_task(inode);
1113 if (!p)
1114 return -ESRCH;
1115 proc_sched_show_task(p, m);
1116
1117 put_task_struct(p);
1118
1119 return 0;
1120}
1121
1122static ssize_t
1123sched_write(struct file *file, const char __user *buf,
1124 size_t count, loff_t *offset)
1125{
1126 struct inode *inode = file->f_path.dentry->d_inode;
1127 struct task_struct *p;
1128
1129 WARN_ON(!inode);
1130
1131 p = get_proc_task(inode);
1132 if (!p)
1133 return -ESRCH;
1134 proc_sched_set_task(p);
1135
1136 put_task_struct(p);
1137
1138 return count;
1139}
1140
1141static int sched_open(struct inode *inode, struct file *filp)
1142{
1143 int ret;
1144
1145 ret = single_open(filp, sched_show, NULL);
1146 if (!ret) {
1147 struct seq_file *m = filp->private_data;
1148
1149 m->private = inode;
1150 }
1151 return ret;
1152}
1153
1154static const struct file_operations proc_pid_sched_operations = {
1155 .open = sched_open,
1156 .read = seq_read,
1157 .write = sched_write,
1158 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001159 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001160};
1161
1162#endif
1163
Al Viro008b1502005-08-20 00:17:39 +01001164static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 struct inode *inode = dentry->d_inode;
1167 int error = -EACCES;
1168
1169 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001170 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Eric W. Biederman778c1142006-06-26 00:25:58 -07001172 /* Are we allowed to snoop on the tasks file descriptors? */
1173 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001176 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 nd->last_type = LAST_BIND;
1178out:
Al Viro008b1502005-08-20 00:17:39 +01001179 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001182static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Mel Gormane12ba742007-10-16 01:25:52 -07001184 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001185 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 int len;
1187
1188 if (!tmp)
1189 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001190
Jan Blunckcf28b482008-02-14 19:38:44 -08001191 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001192 len = PTR_ERR(pathname);
1193 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001195 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (len > buflen)
1198 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001199 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 len = -EFAULT;
1201 out:
1202 free_page((unsigned long)tmp);
1203 return len;
1204}
1205
1206static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1207{
1208 int error = -EACCES;
1209 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001210 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Eric W. Biederman778c1142006-06-26 00:25:58 -07001212 /* Are we allowed to snoop on the tasks file descriptors? */
1213 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001216 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (error)
1218 goto out;
1219
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001220 error = do_proc_readlink(&path, buffer, buflen);
1221 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return error;
1224}
1225
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001226static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001228 .follow_link = proc_pid_follow_link,
1229 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230};
1231
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001232
1233/* building an inode */
1234
1235static int task_dumpable(struct task_struct *task)
1236{
1237 int dumpable = 0;
1238 struct mm_struct *mm;
1239
1240 task_lock(task);
1241 mm = task->mm;
1242 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001243 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001244 task_unlock(task);
1245 if(dumpable == 1)
1246 return 1;
1247 return 0;
1248}
1249
1250
Eric W. Biederman61a28782006-10-02 02:18:49 -07001251static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001252{
1253 struct inode * inode;
1254 struct proc_inode *ei;
1255
1256 /* We need a new inode */
1257
1258 inode = new_inode(sb);
1259 if (!inode)
1260 goto out;
1261
1262 /* Common stuff */
1263 ei = PROC_I(inode);
1264 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001265 inode->i_op = &proc_def_inode_operations;
1266
1267 /*
1268 * grab the reference to task.
1269 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001270 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001271 if (!ei->pid)
1272 goto out_unlock;
1273
1274 inode->i_uid = 0;
1275 inode->i_gid = 0;
1276 if (task_dumpable(task)) {
1277 inode->i_uid = task->euid;
1278 inode->i_gid = task->egid;
1279 }
1280 security_task_to_inode(task, inode);
1281
1282out:
1283 return inode;
1284
1285out_unlock:
1286 iput(inode);
1287 return NULL;
1288}
1289
1290static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1291{
1292 struct inode *inode = dentry->d_inode;
1293 struct task_struct *task;
1294 generic_fillattr(inode, stat);
1295
1296 rcu_read_lock();
1297 stat->uid = 0;
1298 stat->gid = 0;
1299 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1300 if (task) {
1301 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1302 task_dumpable(task)) {
1303 stat->uid = task->euid;
1304 stat->gid = task->egid;
1305 }
1306 }
1307 rcu_read_unlock();
1308 return 0;
1309}
1310
1311/* dentry stuff */
1312
1313/*
1314 * Exceptional case: normally we are not allowed to unhash a busy
1315 * directory. In this case, however, we can do it - no aliasing problems
1316 * due to the way we treat inodes.
1317 *
1318 * Rewrite the inode's ownerships here because the owning task may have
1319 * performed a setuid(), etc.
1320 *
1321 * Before the /proc/pid/status file was created the only way to read
1322 * the effective uid of a /process was to stat /proc/pid. Reading
1323 * /proc/pid/status is slow enough that procps and other packages
1324 * kept stating /proc/pid. To keep the rules in /proc simple I have
1325 * made this apply to all per process world readable and executable
1326 * directories.
1327 */
1328static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1329{
1330 struct inode *inode = dentry->d_inode;
1331 struct task_struct *task = get_proc_task(inode);
1332 if (task) {
1333 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1334 task_dumpable(task)) {
1335 inode->i_uid = task->euid;
1336 inode->i_gid = task->egid;
1337 } else {
1338 inode->i_uid = 0;
1339 inode->i_gid = 0;
1340 }
1341 inode->i_mode &= ~(S_ISUID | S_ISGID);
1342 security_task_to_inode(task, inode);
1343 put_task_struct(task);
1344 return 1;
1345 }
1346 d_drop(dentry);
1347 return 0;
1348}
1349
1350static int pid_delete_dentry(struct dentry * dentry)
1351{
1352 /* Is the task we represent dead?
1353 * If so, then don't put the dentry on the lru list,
1354 * kill it immediately.
1355 */
1356 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1357}
1358
1359static struct dentry_operations pid_dentry_operations =
1360{
1361 .d_revalidate = pid_revalidate,
1362 .d_delete = pid_delete_dentry,
1363};
1364
1365/* Lookups */
1366
Eric Dumazetc5141e62007-05-08 00:26:15 -07001367typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1368 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001369
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001370/*
1371 * Fill a directory entry.
1372 *
1373 * If possible create the dcache entry and derive our inode number and
1374 * file type from dcache entry.
1375 *
1376 * Since all of the proc inode numbers are dynamically generated, the inode
1377 * numbers do not exist until the inode is cache. This means creating the
1378 * the dcache entry in readdir is necessary to keep the inode numbers
1379 * reported by readdir in sync with the inode numbers reported
1380 * by stat.
1381 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001382static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1383 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001384 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001385{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001386 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001387 struct inode *inode;
1388 struct qstr qname;
1389 ino_t ino = 0;
1390 unsigned type = DT_UNKNOWN;
1391
1392 qname.name = name;
1393 qname.len = len;
1394 qname.hash = full_name_hash(name, len);
1395
1396 child = d_lookup(dir, &qname);
1397 if (!child) {
1398 struct dentry *new;
1399 new = d_alloc(dir, &qname);
1400 if (new) {
1401 child = instantiate(dir->d_inode, new, task, ptr);
1402 if (child)
1403 dput(new);
1404 else
1405 child = new;
1406 }
1407 }
1408 if (!child || IS_ERR(child) || !child->d_inode)
1409 goto end_instantiate;
1410 inode = child->d_inode;
1411 if (inode) {
1412 ino = inode->i_ino;
1413 type = inode->i_mode >> 12;
1414 }
1415 dput(child);
1416end_instantiate:
1417 if (!ino)
1418 ino = find_inode_number(dir, &qname);
1419 if (!ino)
1420 ino = 1;
1421 return filldir(dirent, name, len, filp->f_pos, ino, type);
1422}
1423
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001424static unsigned name_to_int(struct dentry *dentry)
1425{
1426 const char *name = dentry->d_name.name;
1427 int len = dentry->d_name.len;
1428 unsigned n = 0;
1429
1430 if (len > 1 && *name == '0')
1431 goto out;
1432 while (len-- > 0) {
1433 unsigned c = *name++ - '0';
1434 if (c > 9)
1435 goto out;
1436 if (n >= (~0U-9)/10)
1437 goto out;
1438 n *= 10;
1439 n += c;
1440 }
1441 return n;
1442out:
1443 return ~0U;
1444}
1445
Miklos Szeredi27932742007-05-08 00:26:17 -07001446#define PROC_FDINFO_MAX 64
1447
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001448static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001449{
1450 struct task_struct *task = get_proc_task(inode);
1451 struct files_struct *files = NULL;
1452 struct file *file;
1453 int fd = proc_fd(inode);
1454
1455 if (task) {
1456 files = get_files_struct(task);
1457 put_task_struct(task);
1458 }
1459 if (files) {
1460 /*
1461 * We are not taking a ref to the file structure, so we must
1462 * hold ->file_lock.
1463 */
1464 spin_lock(&files->file_lock);
1465 file = fcheck_files(files, fd);
1466 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001467 if (path) {
1468 *path = file->f_path;
1469 path_get(&file->f_path);
1470 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001471 if (info)
1472 snprintf(info, PROC_FDINFO_MAX,
1473 "pos:\t%lli\n"
1474 "flags:\t0%o\n",
1475 (long long) file->f_pos,
1476 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001477 spin_unlock(&files->file_lock);
1478 put_files_struct(files);
1479 return 0;
1480 }
1481 spin_unlock(&files->file_lock);
1482 put_files_struct(files);
1483 }
1484 return -ENOENT;
1485}
1486
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001487static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001488{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001489 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001490}
1491
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001492static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1493{
1494 struct inode *inode = dentry->d_inode;
1495 struct task_struct *task = get_proc_task(inode);
1496 int fd = proc_fd(inode);
1497 struct files_struct *files;
1498
1499 if (task) {
1500 files = get_files_struct(task);
1501 if (files) {
1502 rcu_read_lock();
1503 if (fcheck_files(files, fd)) {
1504 rcu_read_unlock();
1505 put_files_struct(files);
1506 if (task_dumpable(task)) {
1507 inode->i_uid = task->euid;
1508 inode->i_gid = task->egid;
1509 } else {
1510 inode->i_uid = 0;
1511 inode->i_gid = 0;
1512 }
1513 inode->i_mode &= ~(S_ISUID | S_ISGID);
1514 security_task_to_inode(task, inode);
1515 put_task_struct(task);
1516 return 1;
1517 }
1518 rcu_read_unlock();
1519 put_files_struct(files);
1520 }
1521 put_task_struct(task);
1522 }
1523 d_drop(dentry);
1524 return 0;
1525}
1526
1527static struct dentry_operations tid_fd_dentry_operations =
1528{
1529 .d_revalidate = tid_fd_revalidate,
1530 .d_delete = pid_delete_dentry,
1531};
1532
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001533static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001534 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001535{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001536 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001537 struct file *file;
1538 struct files_struct *files;
1539 struct inode *inode;
1540 struct proc_inode *ei;
1541 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001542
Eric W. Biederman61a28782006-10-02 02:18:49 -07001543 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001544 if (!inode)
1545 goto out;
1546 ei = PROC_I(inode);
1547 ei->fd = fd;
1548 files = get_files_struct(task);
1549 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001550 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001551 inode->i_mode = S_IFLNK;
1552
1553 /*
1554 * We are not taking a ref to the file structure, so we must
1555 * hold ->file_lock.
1556 */
1557 spin_lock(&files->file_lock);
1558 file = fcheck_files(files, fd);
1559 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001560 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001561 if (file->f_mode & 1)
1562 inode->i_mode |= S_IRUSR | S_IXUSR;
1563 if (file->f_mode & 2)
1564 inode->i_mode |= S_IWUSR | S_IXUSR;
1565 spin_unlock(&files->file_lock);
1566 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001567
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001568 inode->i_op = &proc_pid_link_inode_operations;
1569 inode->i_size = 64;
1570 ei->op.proc_get_link = proc_fd_link;
1571 dentry->d_op = &tid_fd_dentry_operations;
1572 d_add(dentry, inode);
1573 /* Close the race of the process dying before we return the dentry */
1574 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001575 error = NULL;
1576
1577 out:
1578 return error;
1579out_unlock:
1580 spin_unlock(&files->file_lock);
1581 put_files_struct(files);
1582out_iput:
1583 iput(inode);
1584 goto out;
1585}
1586
Miklos Szeredi27932742007-05-08 00:26:17 -07001587static struct dentry *proc_lookupfd_common(struct inode *dir,
1588 struct dentry *dentry,
1589 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001590{
1591 struct task_struct *task = get_proc_task(dir);
1592 unsigned fd = name_to_int(dentry);
1593 struct dentry *result = ERR_PTR(-ENOENT);
1594
1595 if (!task)
1596 goto out_no_task;
1597 if (fd == ~0U)
1598 goto out;
1599
Miklos Szeredi27932742007-05-08 00:26:17 -07001600 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001601out:
1602 put_task_struct(task);
1603out_no_task:
1604 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001605}
1606
Miklos Szeredi27932742007-05-08 00:26:17 -07001607static int proc_readfd_common(struct file * filp, void * dirent,
1608 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001610 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001611 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001612 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001613 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001616 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001619 if (!p)
1620 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 fd = filp->f_pos;
1624 switch (fd) {
1625 case 0:
1626 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1627 goto out;
1628 filp->f_pos++;
1629 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001630 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1632 goto out;
1633 filp->f_pos++;
1634 default:
1635 files = get_files_struct(p);
1636 if (!files)
1637 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001638 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001639 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001641 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001643 char name[PROC_NUMBUF];
1644 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 if (!fcheck_files(files, fd))
1647 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001648 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Miklos Szeredi27932742007-05-08 00:26:17 -07001650 len = snprintf(name, sizeof(name), "%d", fd);
1651 if (proc_fill_cache(filp, dirent, filldir,
1652 name, len, instantiate,
1653 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001654 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 break;
1656 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001657 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001659 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 put_files_struct(files);
1661 }
1662out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001663 put_task_struct(p);
1664out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return retval;
1666}
1667
Miklos Szeredi27932742007-05-08 00:26:17 -07001668static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1669 struct nameidata *nd)
1670{
1671 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1672}
1673
1674static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1675{
1676 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1677}
1678
1679static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1680 size_t len, loff_t *ppos)
1681{
1682 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001683 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001684 if (!err)
1685 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1686 return err;
1687}
1688
1689static const struct file_operations proc_fdinfo_file_operations = {
1690 .open = nonseekable_open,
1691 .read = proc_fdinfo_read,
1692};
1693
Arjan van de Ven00977a52007-02-12 00:55:34 -08001694static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 .read = generic_read_dir,
1696 .readdir = proc_readfd,
1697};
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001700 * /proc/pid/fd needs a special permission handler so that a process can still
1701 * access /proc/self/fd after it has executed a setuid().
1702 */
1703static int proc_fd_permission(struct inode *inode, int mask,
1704 struct nameidata *nd)
1705{
1706 int rv;
1707
1708 rv = generic_permission(inode, mask, NULL);
1709 if (rv == 0)
1710 return 0;
1711 if (task_pid(current) == proc_pid(inode))
1712 rv = 0;
1713 return rv;
1714}
1715
1716/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * proc directories can do almost nothing..
1718 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001719static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001721 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001722 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723};
1724
Miklos Szeredi27932742007-05-08 00:26:17 -07001725static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1726 struct dentry *dentry, struct task_struct *task, const void *ptr)
1727{
1728 unsigned fd = *(unsigned *)ptr;
1729 struct inode *inode;
1730 struct proc_inode *ei;
1731 struct dentry *error = ERR_PTR(-ENOENT);
1732
1733 inode = proc_pid_make_inode(dir->i_sb, task);
1734 if (!inode)
1735 goto out;
1736 ei = PROC_I(inode);
1737 ei->fd = fd;
1738 inode->i_mode = S_IFREG | S_IRUSR;
1739 inode->i_fop = &proc_fdinfo_file_operations;
1740 dentry->d_op = &tid_fd_dentry_operations;
1741 d_add(dentry, inode);
1742 /* Close the race of the process dying before we return the dentry */
1743 if (tid_fd_revalidate(dentry, NULL))
1744 error = NULL;
1745
1746 out:
1747 return error;
1748}
1749
1750static struct dentry *proc_lookupfdinfo(struct inode *dir,
1751 struct dentry *dentry,
1752 struct nameidata *nd)
1753{
1754 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1755}
1756
1757static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1758{
1759 return proc_readfd_common(filp, dirent, filldir,
1760 proc_fdinfo_instantiate);
1761}
1762
1763static const struct file_operations proc_fdinfo_operations = {
1764 .read = generic_read_dir,
1765 .readdir = proc_readfdinfo,
1766};
1767
1768/*
1769 * proc directories can do almost nothing..
1770 */
1771static const struct inode_operations proc_fdinfo_inode_operations = {
1772 .lookup = proc_lookupfdinfo,
1773 .setattr = proc_setattr,
1774};
1775
1776
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001777static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001778 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001779{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001780 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001781 struct inode *inode;
1782 struct proc_inode *ei;
1783 struct dentry *error = ERR_PTR(-EINVAL);
1784
Eric W. Biederman61a28782006-10-02 02:18:49 -07001785 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001786 if (!inode)
1787 goto out;
1788
1789 ei = PROC_I(inode);
1790 inode->i_mode = p->mode;
1791 if (S_ISDIR(inode->i_mode))
1792 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1793 if (p->iop)
1794 inode->i_op = p->iop;
1795 if (p->fop)
1796 inode->i_fop = p->fop;
1797 ei->op = p->op;
1798 dentry->d_op = &pid_dentry_operations;
1799 d_add(dentry, inode);
1800 /* Close the race of the process dying before we return the dentry */
1801 if (pid_revalidate(dentry, NULL))
1802 error = NULL;
1803out:
1804 return error;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807static struct dentry *proc_pident_lookup(struct inode *dir,
1808 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001809 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001810 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001813 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001814 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001815 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001817 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 inode = NULL;
1819
Eric W. Biederman99f89552006-06-26 00:25:55 -07001820 if (!task)
1821 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001823 /*
1824 * Yes, it does not scale. And it should not. Don't add
1825 * new entries into /proc/<tgid>/ without very good reasons.
1826 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001827 last = &ents[nents - 1];
1828 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (p->len != dentry->d_name.len)
1830 continue;
1831 if (!memcmp(dentry->d_name.name, p->name, p->len))
1832 break;
1833 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001834 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 goto out;
1836
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001837 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001839 put_task_struct(task);
1840out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001841 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
Eric Dumazetc5141e62007-05-08 00:26:15 -07001844static int proc_pident_fill_cache(struct file *filp, void *dirent,
1845 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001846{
1847 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1848 proc_pident_instantiate, task, p);
1849}
1850
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001851static int proc_pident_readdir(struct file *filp,
1852 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001853 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001854{
1855 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001856 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001857 struct inode *inode = dentry->d_inode;
1858 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001859 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001860 ino_t ino;
1861 int ret;
1862
1863 ret = -ENOENT;
1864 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001865 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001866
1867 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001868 i = filp->f_pos;
1869 switch (i) {
1870 case 0:
1871 ino = inode->i_ino;
1872 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1873 goto out;
1874 i++;
1875 filp->f_pos++;
1876 /* fall through */
1877 case 1:
1878 ino = parent_ino(dentry);
1879 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1880 goto out;
1881 i++;
1882 filp->f_pos++;
1883 /* fall through */
1884 default:
1885 i -= 2;
1886 if (i >= nents) {
1887 ret = 1;
1888 goto out;
1889 }
1890 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001891 last = &ents[nents - 1];
1892 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001893 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001894 goto out;
1895 filp->f_pos++;
1896 p++;
1897 }
1898 }
1899
1900 ret = 1;
1901out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001902 put_task_struct(task);
1903out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001904 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001908static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1909 size_t count, loff_t *ppos)
1910{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001911 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001912 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001913 ssize_t length;
1914 struct task_struct *task = get_proc_task(inode);
1915
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001916 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001917 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001918
1919 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001920 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001921 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001922 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001923 if (length > 0)
1924 length = simple_read_from_buffer(buf, count, ppos, p, length);
1925 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001926 return length;
1927}
1928
1929static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1930 size_t count, loff_t *ppos)
1931{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001932 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001933 char *page;
1934 ssize_t length;
1935 struct task_struct *task = get_proc_task(inode);
1936
1937 length = -ESRCH;
1938 if (!task)
1939 goto out_no_task;
1940 if (count > PAGE_SIZE)
1941 count = PAGE_SIZE;
1942
1943 /* No partial writes. */
1944 length = -EINVAL;
1945 if (*ppos != 0)
1946 goto out;
1947
1948 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001949 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001950 if (!page)
1951 goto out;
1952
1953 length = -EFAULT;
1954 if (copy_from_user(page, buf, count))
1955 goto out_free;
1956
1957 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001958 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001959 (void*)page, count);
1960out_free:
1961 free_page((unsigned long) page);
1962out:
1963 put_task_struct(task);
1964out_no_task:
1965 return length;
1966}
1967
Arjan van de Ven00977a52007-02-12 00:55:34 -08001968static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001969 .read = proc_pid_attr_read,
1970 .write = proc_pid_attr_write,
1971};
1972
Eric Dumazetc5141e62007-05-08 00:26:15 -07001973static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001974 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1975 REG("prev", S_IRUGO, pid_attr),
1976 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1977 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1978 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1979 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001980};
1981
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001982static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 void * dirent, filldir_t filldir)
1984{
1985 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001986 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Arjan van de Ven00977a52007-02-12 00:55:34 -08001989static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001991 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992};
1993
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001994static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct dentry *dentry, struct nameidata *nd)
1996{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001997 return proc_pident_lookup(dir, dentry,
1998 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002001static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002002 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002003 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002004 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005};
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007#endif
2008
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002009#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2010static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2011 size_t count, loff_t *ppos)
2012{
2013 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2014 struct mm_struct *mm;
2015 char buffer[PROC_NUMBUF];
2016 size_t len;
2017 int ret;
2018
2019 if (!task)
2020 return -ESRCH;
2021
2022 ret = 0;
2023 mm = get_task_mm(task);
2024 if (mm) {
2025 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2026 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2027 MMF_DUMP_FILTER_SHIFT));
2028 mmput(mm);
2029 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2030 }
2031
2032 put_task_struct(task);
2033
2034 return ret;
2035}
2036
2037static ssize_t proc_coredump_filter_write(struct file *file,
2038 const char __user *buf,
2039 size_t count,
2040 loff_t *ppos)
2041{
2042 struct task_struct *task;
2043 struct mm_struct *mm;
2044 char buffer[PROC_NUMBUF], *end;
2045 unsigned int val;
2046 int ret;
2047 int i;
2048 unsigned long mask;
2049
2050 ret = -EFAULT;
2051 memset(buffer, 0, sizeof(buffer));
2052 if (count > sizeof(buffer) - 1)
2053 count = sizeof(buffer) - 1;
2054 if (copy_from_user(buffer, buf, count))
2055 goto out_no_task;
2056
2057 ret = -EINVAL;
2058 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2059 if (*end == '\n')
2060 end++;
2061 if (end - buffer == 0)
2062 goto out_no_task;
2063
2064 ret = -ESRCH;
2065 task = get_proc_task(file->f_dentry->d_inode);
2066 if (!task)
2067 goto out_no_task;
2068
2069 ret = end - buffer;
2070 mm = get_task_mm(task);
2071 if (!mm)
2072 goto out_no_mm;
2073
2074 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2075 if (val & mask)
2076 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2077 else
2078 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2079 }
2080
2081 mmput(mm);
2082 out_no_mm:
2083 put_task_struct(task);
2084 out_no_task:
2085 return ret;
2086}
2087
2088static const struct file_operations proc_coredump_filter_operations = {
2089 .read = proc_coredump_filter_read,
2090 .write = proc_coredump_filter_write,
2091};
2092#endif
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094/*
2095 * /proc/self:
2096 */
2097static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2098 int buflen)
2099{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002100 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002101 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002102 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002103 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002104 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002105 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return vfs_readlink(dentry,buffer,buflen,tmp);
2107}
2108
Al Viro008b1502005-08-20 00:17:39 +01002109static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002111 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002112 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002113 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002114 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002115 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002116 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002117 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002120static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 .readlink = proc_self_readlink,
2122 .follow_link = proc_self_follow_link,
2123};
2124
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002125/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002126 * proc base
2127 *
2128 * These are the directory entries in the root directory of /proc
2129 * that properly belong to the /proc filesystem, as they describe
2130 * describe something that is process related.
2131 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002132static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002133 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002134 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002135};
2136
2137/*
2138 * Exceptional case: normally we are not allowed to unhash a busy
2139 * directory. In this case, however, we can do it - no aliasing problems
2140 * due to the way we treat inodes.
2141 */
2142static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2143{
2144 struct inode *inode = dentry->d_inode;
2145 struct task_struct *task = get_proc_task(inode);
2146 if (task) {
2147 put_task_struct(task);
2148 return 1;
2149 }
2150 d_drop(dentry);
2151 return 0;
2152}
2153
2154static struct dentry_operations proc_base_dentry_operations =
2155{
2156 .d_revalidate = proc_base_revalidate,
2157 .d_delete = pid_delete_dentry,
2158};
2159
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002160static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002161 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002162{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002163 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002164 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002165 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002166 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002167
2168 /* Allocate the inode */
2169 error = ERR_PTR(-ENOMEM);
2170 inode = new_inode(dir->i_sb);
2171 if (!inode)
2172 goto out;
2173
2174 /* Initialize the inode */
2175 ei = PROC_I(inode);
2176 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002177
2178 /*
2179 * grab the reference to the task.
2180 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002181 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002182 if (!ei->pid)
2183 goto out_iput;
2184
2185 inode->i_uid = 0;
2186 inode->i_gid = 0;
2187 inode->i_mode = p->mode;
2188 if (S_ISDIR(inode->i_mode))
2189 inode->i_nlink = 2;
2190 if (S_ISLNK(inode->i_mode))
2191 inode->i_size = 64;
2192 if (p->iop)
2193 inode->i_op = p->iop;
2194 if (p->fop)
2195 inode->i_fop = p->fop;
2196 ei->op = p->op;
2197 dentry->d_op = &proc_base_dentry_operations;
2198 d_add(dentry, inode);
2199 error = NULL;
2200out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002201 return error;
2202out_iput:
2203 iput(inode);
2204 goto out;
2205}
2206
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002207static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2208{
2209 struct dentry *error;
2210 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002211 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002212
2213 error = ERR_PTR(-ENOENT);
2214
2215 if (!task)
2216 goto out_no_task;
2217
2218 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002219 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2220 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002221 if (p->len != dentry->d_name.len)
2222 continue;
2223 if (!memcmp(dentry->d_name.name, p->name, p->len))
2224 break;
2225 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002226 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002227 goto out;
2228
2229 error = proc_base_instantiate(dir, dentry, task, p);
2230
2231out:
2232 put_task_struct(task);
2233out_no_task:
2234 return error;
2235}
2236
Eric Dumazetc5141e62007-05-08 00:26:15 -07002237static int proc_base_fill_cache(struct file *filp, void *dirent,
2238 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002239{
2240 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2241 proc_base_instantiate, task, p);
2242}
2243
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002244#ifdef CONFIG_TASK_IO_ACCOUNTING
2245static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2246{
2247 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002248#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002249 "rchar: %llu\n"
2250 "wchar: %llu\n"
2251 "syscr: %llu\n"
2252 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002253#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002254 "read_bytes: %llu\n"
2255 "write_bytes: %llu\n"
2256 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002257#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002258 (unsigned long long)task->rchar,
2259 (unsigned long long)task->wchar,
2260 (unsigned long long)task->syscr,
2261 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002262#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002263 (unsigned long long)task->ioac.read_bytes,
2264 (unsigned long long)task->ioac.write_bytes,
2265 (unsigned long long)task->ioac.cancelled_write_bytes);
2266}
2267#endif
2268
Eric W. Biederman801199c2006-10-02 02:18:48 -07002269/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002270 * Thread groups
2271 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002272static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002273static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002274
Eric Dumazetc5141e62007-05-08 00:26:15 -07002275static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002276 DIR("task", S_IRUGO|S_IXUGO, task),
2277 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002278 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002279 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002280 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002281 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002282 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002283#ifdef CONFIG_SCHED_DEBUG
2284 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2285#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002286 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002287 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002288 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002289 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002290#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002291 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002292#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002293 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002294 LNK("cwd", cwd),
2295 LNK("root", root),
2296 LNK("exe", exe),
2297 REG("mounts", S_IRUGO, mounts),
2298 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002299#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002300 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002301 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002302 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002303#endif
2304#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002305 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002306#endif
2307#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002308 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002309#endif
2310#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002311 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002312#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002313#ifdef CONFIG_LATENCYTOP
2314 REG("latency", S_IRUGO, lstats),
2315#endif
Paul Menage8793d852007-10-18 23:39:39 -07002316#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002317 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002318#endif
Paul Menagea4243162007-10-18 23:39:35 -07002319#ifdef CONFIG_CGROUPS
2320 REG("cgroup", S_IRUGO, cgroup),
2321#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002322 INF("oom_score", S_IRUGO, oom_score),
2323 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002324#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002325 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002326#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002327#ifdef CONFIG_FAULT_INJECTION
2328 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2329#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002330#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2331 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2332#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002333#ifdef CONFIG_TASK_IO_ACCOUNTING
2334 INF("io", S_IRUGO, pid_io_accounting),
2335#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002336};
2337
2338static int proc_tgid_base_readdir(struct file * filp,
2339 void * dirent, filldir_t filldir)
2340{
2341 return proc_pident_readdir(filp,dirent,filldir,
2342 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2343}
2344
Arjan van de Ven00977a52007-02-12 00:55:34 -08002345static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002346 .read = generic_read_dir,
2347 .readdir = proc_tgid_base_readdir,
2348};
2349
2350static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002351 return proc_pident_lookup(dir, dentry,
2352 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002353}
2354
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002355static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002356 .lookup = proc_tgid_base_lookup,
2357 .getattr = pid_getattr,
2358 .setattr = proc_setattr,
2359};
2360
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002361static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002363 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002364 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002365 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Eric W. Biederman48e64842006-06-26 00:25:48 -07002367 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002368 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2369 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002370 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002371 if (!(current->flags & PF_EXITING))
2372 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002373 d_drop(dentry);
2374 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002377 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002378 goto out;
2379
2380 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002381 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2382 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002383 if (!leader)
2384 goto out;
2385
2386 name.name = "task";
2387 name.len = strlen(name.name);
2388 dir = d_hash_and_lookup(leader, &name);
2389 if (!dir)
2390 goto out_put_leader;
2391
2392 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002393 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002394 dentry = d_hash_and_lookup(dir, &name);
2395 if (dentry) {
2396 shrink_dcache_parent(dentry);
2397 d_drop(dentry);
2398 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002400
2401 dput(dir);
2402out_put_leader:
2403 dput(leader);
2404out:
2405 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
Randy Dunlap0895e912007-10-21 21:00:10 -07002408/**
2409 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2410 * @task: task that should be flushed.
2411 *
2412 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002413 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002414 * in. This call is supposed to do all of this job.
2415 *
2416 * Looks in the dcache for
2417 * /proc/@pid
2418 * /proc/@tgid/task/@pid
2419 * if either directory is present flushes it and all of it'ts children
2420 * from the dcache.
2421 *
2422 * It is safe and reasonable to cache /proc entries for a task until
2423 * that task exits. After that they just clog up the dcache with
2424 * useless entries, possibly causing useful dcache entries to be
2425 * flushed instead. This routine is proved to flush those useless
2426 * dcache entries at process exit time.
2427 *
2428 * NOTE: This routine is just an optimization so it does not guarantee
2429 * that no dcache entries will exist at process exit time it
2430 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002431 */
2432
2433void proc_flush_task(struct task_struct *task)
2434{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002435 int i;
2436 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002437 struct upid *upid;
2438
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002439 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002440 if (thread_group_leader(task))
2441 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002442
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002443 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002444 upid = &pid->numbers[i];
2445 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002446 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002447 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002448
2449 upid = &pid->numbers[pid->level];
2450 if (upid->nr == 1)
2451 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002452}
2453
Adrian Bunk9711ef92006-12-06 20:38:31 -08002454static struct dentry *proc_pid_instantiate(struct inode *dir,
2455 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002456 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002457{
2458 struct dentry *error = ERR_PTR(-ENOENT);
2459 struct inode *inode;
2460
Eric W. Biederman61a28782006-10-02 02:18:49 -07002461 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002462 if (!inode)
2463 goto out;
2464
2465 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2466 inode->i_op = &proc_tgid_base_inode_operations;
2467 inode->i_fop = &proc_tgid_base_operations;
2468 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002469 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002470#ifdef CONFIG_SECURITY
2471 inode->i_nlink += 1;
2472#endif
2473
2474 dentry->d_op = &pid_dentry_operations;
2475
2476 d_add(dentry, inode);
2477 /* Close the race of the process dying before we return the dentry */
2478 if (pid_revalidate(dentry, NULL))
2479 error = NULL;
2480out:
2481 return error;
2482}
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2485{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002486 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002489 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Eric W. Biederman801199c2006-10-02 02:18:48 -07002491 result = proc_base_lookup(dir, dentry);
2492 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2493 goto out;
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 tgid = name_to_int(dentry);
2496 if (tgid == ~0U)
2497 goto out;
2498
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002499 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002500 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002501 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (task)
2503 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002504 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (!task)
2506 goto out;
2507
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002508 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002509 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002511 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002515 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002516 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002518struct tgid_iter {
2519 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002520 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002521};
2522static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2523{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002524 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002526 if (iter.task)
2527 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002528 rcu_read_lock();
2529retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002530 iter.task = NULL;
2531 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002532 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002533 iter.tgid = pid_nr_ns(pid, ns);
2534 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002535 /* What we to know is if the pid we have find is the
2536 * pid of a thread_group_leader. Testing for task
2537 * being a thread_group_leader is the obvious thing
2538 * todo but there is a window when it fails, due to
2539 * the pid transfer logic in de_thread.
2540 *
2541 * So we perform the straight forward test of seeing
2542 * if the pid we have found is the pid of a thread
2543 * group leader, and don't worry if the task we have
2544 * found doesn't happen to be a thread group leader.
2545 * As we don't care in the case of readdir.
2546 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002547 if (!iter.task || !has_group_leader_pid(iter.task)) {
2548 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002549 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002550 }
2551 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002553 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002554 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002557#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Eric W. Biederman61a28782006-10-02 02:18:49 -07002559static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002560 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002561{
2562 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002563 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002564 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002565 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002566}
2567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568/* for the /proc/ directory itself, after non-process stuff has been done */
2569int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2570{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002572 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002573 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002574 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Eric W. Biederman61a28782006-10-02 02:18:49 -07002576 if (!reaper)
2577 goto out_no_task;
2578
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002579 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002580 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002581 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
2584
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002585 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002586 iter.task = NULL;
2587 iter.tgid = filp->f_pos - TGID_OFFSET;
2588 for (iter = next_tgid(ns, iter);
2589 iter.task;
2590 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2591 filp->f_pos = iter.tgid + TGID_OFFSET;
2592 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2593 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002594 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002597 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2598out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002599 put_task_struct(reaper);
2600out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return 0;
2602}
2603
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002604/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002605 * Tasks
2606 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002607static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002608 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002609 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002610 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002611 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002612 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002613 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002614#ifdef CONFIG_SCHED_DEBUG
2615 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2616#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002617 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002618 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002619 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002620 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002621#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002622 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002623#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002624 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002625 LNK("cwd", cwd),
2626 LNK("root", root),
2627 LNK("exe", exe),
2628 REG("mounts", S_IRUGO, mounts),
Matt Mackall1e883282008-02-04 22:29:07 -08002629#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002630 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002631 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002632 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002633#endif
2634#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002635 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002636#endif
2637#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002638 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002639#endif
2640#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002641 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002642#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002643#ifdef CONFIG_LATENCYTOP
2644 REG("latency", S_IRUGO, lstats),
2645#endif
Paul Menage8793d852007-10-18 23:39:39 -07002646#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002647 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002648#endif
Paul Menagea4243162007-10-18 23:39:35 -07002649#ifdef CONFIG_CGROUPS
2650 REG("cgroup", S_IRUGO, cgroup),
2651#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002652 INF("oom_score", S_IRUGO, oom_score),
2653 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002654#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002655 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002656#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002657#ifdef CONFIG_FAULT_INJECTION
2658 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2659#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002660};
2661
2662static int proc_tid_base_readdir(struct file * filp,
2663 void * dirent, filldir_t filldir)
2664{
2665 return proc_pident_readdir(filp,dirent,filldir,
2666 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2667}
2668
2669static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002670 return proc_pident_lookup(dir, dentry,
2671 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002672}
2673
Arjan van de Ven00977a52007-02-12 00:55:34 -08002674static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002675 .read = generic_read_dir,
2676 .readdir = proc_tid_base_readdir,
2677};
2678
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002679static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002680 .lookup = proc_tid_base_lookup,
2681 .getattr = pid_getattr,
2682 .setattr = proc_setattr,
2683};
2684
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002685static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002686 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002687{
2688 struct dentry *error = ERR_PTR(-ENOENT);
2689 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002690 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002691
2692 if (!inode)
2693 goto out;
2694 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2695 inode->i_op = &proc_tid_base_inode_operations;
2696 inode->i_fop = &proc_tid_base_operations;
2697 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002698 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002699#ifdef CONFIG_SECURITY
2700 inode->i_nlink += 1;
2701#endif
2702
2703 dentry->d_op = &pid_dentry_operations;
2704
2705 d_add(dentry, inode);
2706 /* Close the race of the process dying before we return the dentry */
2707 if (pid_revalidate(dentry, NULL))
2708 error = NULL;
2709out:
2710 return error;
2711}
2712
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002713static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2714{
2715 struct dentry *result = ERR_PTR(-ENOENT);
2716 struct task_struct *task;
2717 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002718 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002719 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002720
2721 if (!leader)
2722 goto out_no_task;
2723
2724 tid = name_to_int(dentry);
2725 if (tid == ~0U)
2726 goto out;
2727
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002728 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002729 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002730 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002731 if (task)
2732 get_task_struct(task);
2733 rcu_read_unlock();
2734 if (!task)
2735 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002736 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002737 goto out_drop_task;
2738
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002739 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002740out_drop_task:
2741 put_task_struct(task);
2742out:
2743 put_task_struct(leader);
2744out_no_task:
2745 return result;
2746}
2747
2748/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002749 * Find the first tid of a thread group to return to user space.
2750 *
2751 * Usually this is just the thread group leader, but if the users
2752 * buffer was too small or there was a seek into the middle of the
2753 * directory we have more work todo.
2754 *
2755 * In the case of a short read we start with find_task_by_pid.
2756 *
2757 * In the case of a seek we start with the leader and walk nr
2758 * threads past it.
2759 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002760static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002761 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002762{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002763 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002764
Eric W. Biedermancc288732006-06-26 00:26:01 -07002765 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002766 /* Attempt to start with the pid of a thread */
2767 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002768 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002769 if (pos && (pos->group_leader == leader))
2770 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002771 }
2772
2773 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002774 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002775 if (nr && nr >= get_nr_threads(leader))
2776 goto out;
2777
2778 /* If we haven't found our starting place yet start
2779 * with the leader and walk nr threads forward.
2780 */
2781 for (pos = leader; nr > 0; --nr) {
2782 pos = next_thread(pos);
2783 if (pos == leader) {
2784 pos = NULL;
2785 goto out;
2786 }
2787 }
2788found:
2789 get_task_struct(pos);
2790out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002791 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002792 return pos;
2793}
2794
2795/*
2796 * Find the next thread in the thread list.
2797 * Return NULL if there is an error or no next thread.
2798 *
2799 * The reference to the input task_struct is released.
2800 */
2801static struct task_struct *next_tid(struct task_struct *start)
2802{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002803 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002804 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002805 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002806 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002807 if (thread_group_leader(pos))
2808 pos = NULL;
2809 else
2810 get_task_struct(pos);
2811 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002812 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002813 put_task_struct(start);
2814 return pos;
2815}
2816
Eric W. Biederman61a28782006-10-02 02:18:49 -07002817static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2818 struct task_struct *task, int tid)
2819{
2820 char name[PROC_NUMBUF];
2821 int len = snprintf(name, sizeof(name), "%d", tid);
2822 return proc_fill_cache(filp, dirent, filldir, name, len,
2823 proc_task_instantiate, task, NULL);
2824}
2825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826/* for the /proc/TGID/task/ directories */
2827static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2828{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002829 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002831 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002832 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 int retval = -ENOENT;
2834 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002835 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002837 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002839 task = get_proc_task(inode);
2840 if (!task)
2841 goto out_no_task;
2842 rcu_read_lock();
2843 if (pid_alive(task)) {
2844 leader = task->group_leader;
2845 get_task_struct(leader);
2846 }
2847 rcu_read_unlock();
2848 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002849 if (!leader)
2850 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 retval = 0;
2852
2853 switch (pos) {
2854 case 0:
2855 ino = inode->i_ino;
2856 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2857 goto out;
2858 pos++;
2859 /* fall through */
2860 case 1:
2861 ino = parent_ino(dentry);
2862 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2863 goto out;
2864 pos++;
2865 /* fall through */
2866 }
2867
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002868 /* f_version caches the tgid value that the last readdir call couldn't
2869 * return. lseek aka telldir automagically resets f_version to 0.
2870 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002871 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002872 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002873 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002874 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002875 task;
2876 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002877 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002878 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002879 /* returning this tgid failed, save it as the first
2880 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002881 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002882 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
2886out:
2887 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002888 put_task_struct(leader);
2889out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return retval;
2891}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002892
2893static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2894{
2895 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002896 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002897 generic_fillattr(inode, stat);
2898
Eric W. Biederman99f89552006-06-26 00:25:55 -07002899 if (p) {
2900 rcu_read_lock();
2901 stat->nlink += get_nr_threads(p);
2902 rcu_read_unlock();
2903 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002904 }
2905
2906 return 0;
2907}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002908
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002909static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002910 .lookup = proc_task_lookup,
2911 .getattr = proc_task_getattr,
2912 .setattr = proc_setattr,
2913};
2914
Arjan van de Ven00977a52007-02-12 00:55:34 -08002915static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002916 .read = generic_read_dir,
2917 .readdir = proc_task_readdir,
2918};