blob: c5e412a00b170e5294964505726b85f6cff93309 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070064#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/kallsyms.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070066#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070067#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/mount.h>
69#include <linux/security.h>
70#include <linux/ptrace.h>
Paul Menagea4243162007-10-18 23:39:35 -070071#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/cpuset.h>
73#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050074#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070075#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070076#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070077#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070078#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include "internal.h"
80
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070081/* NOTE:
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
86 *
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
89 */
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070093 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080095 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080096 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070097 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Eric W. Biederman61a28782006-10-02 02:18:49 -0700100#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700101 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700102 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .mode = MODE, \
104 .iop = IOP, \
105 .fop = FOP, \
106 .op = OP, \
107}
108
Eric W. Biederman61a28782006-10-02 02:18:49 -0700109#define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
112 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700113#define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700117#define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800124#define ONE(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
127 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Kees Cook5096add2007-05-08 00:26:04 -0700129int maps_protect;
130EXPORT_SYMBOL(maps_protect);
131
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700132static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700135 task_lock(task);
136 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if(fs)
138 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700139 task_unlock(task);
140 return fs;
141}
142
Eric W. Biederman99f89552006-06-26 00:25:55 -0700143static int get_nr_threads(struct task_struct *tsk)
144{
145 /* Must be called with the rcu_read_lock held */
146 unsigned long flags;
147 int count = 0;
148
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
152 }
153 return count;
154}
155
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800156static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700160 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700161
162 if (task) {
163 fs = get_fs_struct(task);
164 put_task_struct(task);
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (fs) {
167 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800168 *path = fs->pwd;
169 path_get(&fs->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 read_unlock(&fs->lock);
171 result = 0;
172 put_fs_struct(fs);
173 }
174 return result;
175}
176
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800177static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182
183 if (task) {
184 fs = get_fs_struct(task);
185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (fs) {
188 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800189 *path = fs->root;
190 path_get(&fs->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 read_unlock(&fs->lock);
192 result = 0;
193 put_fs_struct(fs);
194 }
195 return result;
196}
197
198#define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
Matthew Wilcox6d8982d2007-12-06 11:04:01 -0500202 (task_is_stopped_or_traced(task)) && \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 security_ptrace(current,task) == 0))
204
Al Viro831830b2008-01-02 14:09:57 +0000205struct mm_struct *mm_for_maps(struct task_struct *task)
206{
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 return NULL;
210 down_read(&mm->mmap_sem);
211 task_lock(task);
212 if (task->mm != mm)
213 goto out;
214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
215 goto out;
216 task_unlock(task);
217 return mm;
218out:
219 task_unlock(task);
220 up_read(&mm->mmap_sem);
221 mmput(mm);
222 return NULL;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int proc_pid_cmdline(struct task_struct *task, char * buffer)
226{
227 int res = 0;
228 unsigned int len;
229 struct mm_struct *mm = get_task_mm(task);
230 if (!mm)
231 goto out;
232 if (!mm->arg_end)
233 goto out_mm; /* Shh! No looking before we're done */
234
235 len = mm->arg_end - mm->arg_start;
236
237 if (len > PAGE_SIZE)
238 len = PAGE_SIZE;
239
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
241
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
246 if (len < res) {
247 res = len;
248 } else {
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
254 }
255 }
256out_mm:
257 mmput(mm);
258out:
259 return res;
260}
261
262static int proc_pid_auxv(struct task_struct *task, char *buffer)
263{
264 int res = 0;
265 struct mm_struct *mm = get_task_mm(task);
266 if (mm) {
267 unsigned int nwords = 0;
268 do
269 nwords += 2;
270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
272 if (res > PAGE_SIZE)
273 res = PAGE_SIZE;
274 memcpy(buffer, mm->saved_auxv, res);
275 mmput(mm);
276 }
277 return res;
278}
279
280
281#ifdef CONFIG_KALLSYMS
282/*
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
285 */
286static int proc_pid_wchan(struct task_struct *task, char *buffer)
287{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700288 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700289 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 wchan = get_wchan(task);
292
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700293 if (lookup_symbol_name(wchan, symname) < 0)
294 return sprintf(buffer, "%lu", wchan);
295 else
296 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298#endif /* CONFIG_KALLSYMS */
299
300#ifdef CONFIG_SCHEDSTATS
301/*
302 * Provides /proc/PID/schedstat
303 */
304static int proc_pid_schedstat(struct task_struct *task, char *buffer)
305{
Balbir Singh172ba842007-07-09 18:52:00 +0200306 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 task->sched_info.cpu_time,
308 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200309 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311#endif
312
Arjan van de Ven97455122008-01-25 21:08:34 +0100313#ifdef CONFIG_LATENCYTOP
314static int lstats_show_proc(struct seq_file *m, void *v)
315{
316 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800317 struct inode *inode = m->private;
318 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100319
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800320 if (!task)
321 return -ESRCH;
322 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100323 for (i = 0; i < 32; i++) {
324 if (task->latency_record[i].backtrace[0]) {
325 int q;
326 seq_printf(m, "%i %li %li ",
327 task->latency_record[i].count,
328 task->latency_record[i].time,
329 task->latency_record[i].max);
330 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
331 char sym[KSYM_NAME_LEN];
332 char *c;
333 if (!task->latency_record[i].backtrace[q])
334 break;
335 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
336 break;
337 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
338 c = strchr(sym, '+');
339 if (c)
340 *c = 0;
341 seq_printf(m, "%s ", sym);
342 }
343 seq_printf(m, "\n");
344 }
345
346 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800347 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100348 return 0;
349}
350
351static int lstats_open(struct inode *inode, struct file *file)
352{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800353 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800354}
355
Arjan van de Ven97455122008-01-25 21:08:34 +0100356static ssize_t lstats_write(struct file *file, const char __user *buf,
357 size_t count, loff_t *offs)
358{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800359 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100360
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800361 if (!task)
362 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100363 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800364 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100365
366 return count;
367}
368
369static const struct file_operations proc_lstats_operations = {
370 .open = lstats_open,
371 .read = seq_read,
372 .write = lstats_write,
373 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800374 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100375};
376
377#endif
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/* The badness from the OOM killer */
380unsigned long badness(struct task_struct *p, unsigned long uptime);
381static int proc_oom_score(struct task_struct *task, char *buffer)
382{
383 unsigned long points;
384 struct timespec uptime;
385
386 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700387 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700389 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return sprintf(buffer, "%lu\n", points);
391}
392
Neil Hormand85f50d2007-10-18 23:40:37 -0700393struct limit_names {
394 char *name;
395 char *unit;
396};
397
398static const struct limit_names lnames[RLIM_NLIMITS] = {
399 [RLIMIT_CPU] = {"Max cpu time", "ms"},
400 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
401 [RLIMIT_DATA] = {"Max data size", "bytes"},
402 [RLIMIT_STACK] = {"Max stack size", "bytes"},
403 [RLIMIT_CORE] = {"Max core file size", "bytes"},
404 [RLIMIT_RSS] = {"Max resident set", "bytes"},
405 [RLIMIT_NPROC] = {"Max processes", "processes"},
406 [RLIMIT_NOFILE] = {"Max open files", "files"},
407 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
408 [RLIMIT_AS] = {"Max address space", "bytes"},
409 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
410 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
411 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
412 [RLIMIT_NICE] = {"Max nice priority", NULL},
413 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800414 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700415};
416
417/* Display limits for a process */
418static int proc_pid_limits(struct task_struct *task, char *buffer)
419{
420 unsigned int i;
421 int count = 0;
422 unsigned long flags;
423 char *bufptr = buffer;
424
425 struct rlimit rlim[RLIM_NLIMITS];
426
427 rcu_read_lock();
428 if (!lock_task_sighand(task,&flags)) {
429 rcu_read_unlock();
430 return 0;
431 }
432 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
433 unlock_task_sighand(task, &flags);
434 rcu_read_unlock();
435
436 /*
437 * print the file header
438 */
439 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
440 "Limit", "Soft Limit", "Hard Limit", "Units");
441
442 for (i = 0; i < RLIM_NLIMITS; i++) {
443 if (rlim[i].rlim_cur == RLIM_INFINITY)
444 count += sprintf(&bufptr[count], "%-25s %-20s ",
445 lnames[i].name, "unlimited");
446 else
447 count += sprintf(&bufptr[count], "%-25s %-20lu ",
448 lnames[i].name, rlim[i].rlim_cur);
449
450 if (rlim[i].rlim_max == RLIM_INFINITY)
451 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
452 else
453 count += sprintf(&bufptr[count], "%-20lu ",
454 rlim[i].rlim_max);
455
456 if (lnames[i].unit)
457 count += sprintf(&bufptr[count], "%-10s\n",
458 lnames[i].unit);
459 else
460 count += sprintf(&bufptr[count], "\n");
461 }
462
463 return count;
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/************************************************************************/
467/* Here the fs part begins */
468/************************************************************************/
469
470/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700471static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700473 struct task_struct *task;
474 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700475 /* Allow access to a task's file descriptors if it is us or we
476 * may use ptrace attach to the process and find out that
477 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700478 */
479 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700480 if (task) {
481 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700482 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700483 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700484 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700487static int proc_setattr(struct dentry *dentry, struct iattr *attr)
488{
489 int error;
490 struct inode *inode = dentry->d_inode;
491
492 if (attr->ia_valid & ATTR_MODE)
493 return -EPERM;
494
495 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700496 if (!error)
497 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700498 return error;
499}
500
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800501static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700502 .setattr = proc_setattr,
503};
504
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100505static int mounts_open_common(struct inode *inode, struct file *file,
506 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700508 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700509 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800510 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100511 struct fs_struct *fs = NULL;
512 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500513 struct proc_mounts *p;
514 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Eric W. Biederman99f89552006-06-26 00:25:55 -0700516 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700517 rcu_read_lock();
518 nsp = task_nsproxy(task);
519 if (nsp) {
520 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800521 if (ns)
522 get_mnt_ns(ns);
523 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700524 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100525 if (ns)
526 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700527 put_task_struct(task);
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100530 if (!ns)
531 goto err;
532 if (!fs)
533 goto err_put_ns;
534
535 read_lock(&fs->lock);
536 root = fs->root;
537 path_get(&root);
538 read_unlock(&fs->lock);
539 put_fs_struct(fs);
540
541 ret = -ENOMEM;
542 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
543 if (!p)
544 goto err_put_path;
545
546 file->private_data = &p->m;
547 ret = seq_open(file, op);
548 if (ret)
549 goto err_free;
550
551 p->m.private = p;
552 p->ns = ns;
553 p->root = root;
554 p->event = ns->event;
555
556 return 0;
557
558 err_free:
559 kfree(p);
560 err_put_path:
561 path_put(&root);
562 err_put_ns:
563 put_mnt_ns(ns);
564 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return ret;
566}
567
568static int mounts_release(struct inode *inode, struct file *file)
569{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100570 struct proc_mounts *p = file->private_data;
571 path_put(&p->root);
572 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return seq_release(inode, file);
574}
575
Al Viro5addc5d2005-11-07 17:15:49 -0500576static unsigned mounts_poll(struct file *file, poll_table *wait)
577{
578 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100579 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500580 unsigned res = 0;
581
582 poll_wait(file, &ns->poll, wait);
583
584 spin_lock(&vfsmount_lock);
585 if (p->event != ns->event) {
586 p->event = ns->event;
587 res = POLLERR;
588 }
589 spin_unlock(&vfsmount_lock);
590
591 return res;
592}
593
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100594static int mounts_open(struct inode *inode, struct file *file)
595{
596 return mounts_open_common(inode, file, &mounts_op);
597}
598
Arjan van de Ven00977a52007-02-12 00:55:34 -0800599static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .open = mounts_open,
601 .read = seq_read,
602 .llseek = seq_lseek,
603 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500604 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605};
606
Ram Pai2d4d4862008-03-27 13:06:25 +0100607static int mountinfo_open(struct inode *inode, struct file *file)
608{
609 return mounts_open_common(inode, file, &mountinfo_op);
610}
611
612static const struct file_operations proc_mountinfo_operations = {
613 .open = mountinfo_open,
614 .read = seq_read,
615 .llseek = seq_lseek,
616 .release = mounts_release,
617 .poll = mounts_poll,
618};
619
Chuck Leverb4629fe2006-03-20 13:44:12 -0500620static int mountstats_open(struct inode *inode, struct file *file)
621{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100622 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500623}
624
Arjan van de Ven00977a52007-02-12 00:55:34 -0800625static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500626 .open = mountstats_open,
627 .read = seq_read,
628 .llseek = seq_lseek,
629 .release = mounts_release,
630};
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
633
634static ssize_t proc_info_read(struct file * file, char __user * buf,
635 size_t count, loff_t *ppos)
636{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800637 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned long page;
639 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700640 struct task_struct *task = get_proc_task(inode);
641
642 length = -ESRCH;
643 if (!task)
644 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 if (count > PROC_BLOCK_SIZE)
647 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700648
649 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700650 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700651 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 length = PROC_I(inode)->op.proc_read(task, (char*)page);
654
655 if (length >= 0)
656 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
657 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700658out:
659 put_task_struct(task);
660out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return length;
662}
663
Arjan van de Ven00977a52007-02-12 00:55:34 -0800664static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 .read = proc_info_read,
666};
667
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800668static int proc_single_show(struct seq_file *m, void *v)
669{
670 struct inode *inode = m->private;
671 struct pid_namespace *ns;
672 struct pid *pid;
673 struct task_struct *task;
674 int ret;
675
676 ns = inode->i_sb->s_fs_info;
677 pid = proc_pid(inode);
678 task = get_pid_task(pid, PIDTYPE_PID);
679 if (!task)
680 return -ESRCH;
681
682 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
683
684 put_task_struct(task);
685 return ret;
686}
687
688static int proc_single_open(struct inode *inode, struct file *filp)
689{
690 int ret;
691 ret = single_open(filp, proc_single_show, NULL);
692 if (!ret) {
693 struct seq_file *m = filp->private_data;
694
695 m->private = inode;
696 }
697 return ret;
698}
699
700static const struct file_operations proc_single_file_operations = {
701 .open = proc_single_open,
702 .read = seq_read,
703 .llseek = seq_lseek,
704 .release = single_release,
705};
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707static int mem_open(struct inode* inode, struct file* file)
708{
709 file->private_data = (void*)((long)current->self_exec_id);
710 return 0;
711}
712
713static ssize_t mem_read(struct file * file, char __user * buf,
714 size_t count, loff_t *ppos)
715{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800716 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 char *page;
718 unsigned long src = *ppos;
719 int ret = -ESRCH;
720 struct mm_struct *mm;
721
Eric W. Biederman99f89552006-06-26 00:25:55 -0700722 if (!task)
723 goto out_no_task;
724
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700725 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto out;
727
728 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700729 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!page)
731 goto out;
732
733 ret = 0;
734
735 mm = get_task_mm(task);
736 if (!mm)
737 goto out_free;
738
739 ret = -EIO;
740
741 if (file->private_data != (void*)((long)current->self_exec_id))
742 goto out_put;
743
744 ret = 0;
745
746 while (count > 0) {
747 int this_len, retval;
748
749 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
750 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700751 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!ret)
753 ret = -EIO;
754 break;
755 }
756
757 if (copy_to_user(buf, page, retval)) {
758 ret = -EFAULT;
759 break;
760 }
761
762 ret += retval;
763 src += retval;
764 buf += retval;
765 count -= retval;
766 }
767 *ppos = src;
768
769out_put:
770 mmput(mm);
771out_free:
772 free_page((unsigned long) page);
773out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700774 put_task_struct(task);
775out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return ret;
777}
778
779#define mem_write NULL
780
781#ifndef mem_write
782/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800783static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 size_t count, loff_t *ppos)
785{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700786 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800788 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 unsigned long dst = *ppos;
790
Eric W. Biederman99f89552006-06-26 00:25:55 -0700791 copied = -ESRCH;
792 if (!task)
793 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Eric W. Biederman99f89552006-06-26 00:25:55 -0700795 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
796 goto out;
797
798 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700799 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700801 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700803 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 while (count > 0) {
805 int this_len, retval;
806
807 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
808 if (copy_from_user(page, buf, this_len)) {
809 copied = -EFAULT;
810 break;
811 }
812 retval = access_process_vm(task, dst, page, this_len, 1);
813 if (!retval) {
814 if (!copied)
815 copied = -EIO;
816 break;
817 }
818 copied += retval;
819 buf += retval;
820 dst += retval;
821 count -= retval;
822 }
823 *ppos = dst;
824 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700825out:
826 put_task_struct(task);
827out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return copied;
829}
830#endif
831
Matt Mackall85863e42008-02-04 22:29:04 -0800832loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 switch (orig) {
835 case 0:
836 file->f_pos = offset;
837 break;
838 case 1:
839 file->f_pos += offset;
840 break;
841 default:
842 return -EINVAL;
843 }
844 force_successful_syscall_return();
845 return file->f_pos;
846}
847
Arjan van de Ven00977a52007-02-12 00:55:34 -0800848static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .llseek = mem_lseek,
850 .read = mem_read,
851 .write = mem_write,
852 .open = mem_open,
853};
854
James Pearson315e28c2007-10-16 23:30:17 -0700855static ssize_t environ_read(struct file *file, char __user *buf,
856 size_t count, loff_t *ppos)
857{
858 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
859 char *page;
860 unsigned long src = *ppos;
861 int ret = -ESRCH;
862 struct mm_struct *mm;
863
864 if (!task)
865 goto out_no_task;
866
867 if (!ptrace_may_attach(task))
868 goto out;
869
870 ret = -ENOMEM;
871 page = (char *)__get_free_page(GFP_TEMPORARY);
872 if (!page)
873 goto out;
874
875 ret = 0;
876
877 mm = get_task_mm(task);
878 if (!mm)
879 goto out_free;
880
881 while (count > 0) {
882 int this_len, retval, max_len;
883
884 this_len = mm->env_end - (mm->env_start + src);
885
886 if (this_len <= 0)
887 break;
888
889 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
890 this_len = (this_len > max_len) ? max_len : this_len;
891
892 retval = access_process_vm(task, (mm->env_start + src),
893 page, this_len, 0);
894
895 if (retval <= 0) {
896 ret = retval;
897 break;
898 }
899
900 if (copy_to_user(buf, page, retval)) {
901 ret = -EFAULT;
902 break;
903 }
904
905 ret += retval;
906 src += retval;
907 buf += retval;
908 count -= retval;
909 }
910 *ppos = src;
911
912 mmput(mm);
913out_free:
914 free_page((unsigned long) page);
915out:
916 put_task_struct(task);
917out_no_task:
918 return ret;
919}
920
921static const struct file_operations proc_environ_operations = {
922 .read = environ_read,
923};
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925static ssize_t oom_adjust_read(struct file *file, char __user *buf,
926 size_t count, loff_t *ppos)
927{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800928 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700929 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700931 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Eric W. Biederman99f89552006-06-26 00:25:55 -0700933 if (!task)
934 return -ESRCH;
935 oom_adjust = task->oomkilladj;
936 put_task_struct(task);
937
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700938 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700939
940 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
944 size_t count, loff_t *ppos)
945{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700946 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700947 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 int oom_adjust;
949
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700950 memset(buffer, 0, sizeof(buffer));
951 if (count > sizeof(buffer) - 1)
952 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (copy_from_user(buffer, buf, count))
954 return -EFAULT;
955 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700956 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
957 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return -EINVAL;
959 if (*end == '\n')
960 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800961 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700962 if (!task)
963 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800964 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
965 put_task_struct(task);
966 return -EACCES;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700969 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (end - buffer == 0)
971 return -EIO;
972 return end - buffer;
973}
974
Arjan van de Ven00977a52007-02-12 00:55:34 -0800975static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 .read = oom_adjust_read,
977 .write = oom_adjust_write,
978};
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#ifdef CONFIG_AUDITSYSCALL
981#define TMPBUFLEN 21
982static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
983 size_t count, loff_t *ppos)
984{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800985 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700986 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ssize_t length;
988 char tmpbuf[TMPBUFLEN];
989
Eric W. Biederman99f89552006-06-26 00:25:55 -0700990 if (!task)
991 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -0500993 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700994 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
996}
997
998static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
999 size_t count, loff_t *ppos)
1000{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001001 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 char *page, *tmp;
1003 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 uid_t loginuid;
1005
1006 if (!capable(CAP_AUDIT_CONTROL))
1007 return -EPERM;
1008
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001009 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -EPERM;
1011
Al Viroe0182902006-05-18 08:28:02 -04001012 if (count >= PAGE_SIZE)
1013 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (*ppos != 0) {
1016 /* No partial writes. */
1017 return -EINVAL;
1018 }
Mel Gormane12ba742007-10-16 01:25:52 -07001019 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (!page)
1021 return -ENOMEM;
1022 length = -EFAULT;
1023 if (copy_from_user(page, buf, count))
1024 goto out_free_page;
1025
Al Viroe0182902006-05-18 08:28:02 -04001026 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 loginuid = simple_strtoul(page, &tmp, 10);
1028 if (tmp == page) {
1029 length = -EINVAL;
1030 goto out_free_page;
1031
1032 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001033 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (likely(length == 0))
1035 length = count;
1036
1037out_free_page:
1038 free_page((unsigned long) page);
1039 return length;
1040}
1041
Arjan van de Ven00977a52007-02-12 00:55:34 -08001042static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .read = proc_loginuid_read,
1044 .write = proc_loginuid_write,
1045};
Eric Paris1e0bd752008-03-13 08:15:31 -04001046
1047static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1048 size_t count, loff_t *ppos)
1049{
1050 struct inode * inode = file->f_path.dentry->d_inode;
1051 struct task_struct *task = get_proc_task(inode);
1052 ssize_t length;
1053 char tmpbuf[TMPBUFLEN];
1054
1055 if (!task)
1056 return -ESRCH;
1057 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1058 audit_get_sessionid(task));
1059 put_task_struct(task);
1060 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1061}
1062
1063static const struct file_operations proc_sessionid_operations = {
1064 .read = proc_sessionid_read,
1065};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066#endif
1067
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001068#ifdef CONFIG_FAULT_INJECTION
1069static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1070 size_t count, loff_t *ppos)
1071{
1072 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1073 char buffer[PROC_NUMBUF];
1074 size_t len;
1075 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001076
1077 if (!task)
1078 return -ESRCH;
1079 make_it_fail = task->make_it_fail;
1080 put_task_struct(task);
1081
1082 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001083
1084 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001085}
1086
1087static ssize_t proc_fault_inject_write(struct file * file,
1088 const char __user * buf, size_t count, loff_t *ppos)
1089{
1090 struct task_struct *task;
1091 char buffer[PROC_NUMBUF], *end;
1092 int make_it_fail;
1093
1094 if (!capable(CAP_SYS_RESOURCE))
1095 return -EPERM;
1096 memset(buffer, 0, sizeof(buffer));
1097 if (count > sizeof(buffer) - 1)
1098 count = sizeof(buffer) - 1;
1099 if (copy_from_user(buffer, buf, count))
1100 return -EFAULT;
1101 make_it_fail = simple_strtol(buffer, &end, 0);
1102 if (*end == '\n')
1103 end++;
1104 task = get_proc_task(file->f_dentry->d_inode);
1105 if (!task)
1106 return -ESRCH;
1107 task->make_it_fail = make_it_fail;
1108 put_task_struct(task);
1109 if (end - buffer == 0)
1110 return -EIO;
1111 return end - buffer;
1112}
1113
Arjan van de Ven00977a52007-02-12 00:55:34 -08001114static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001115 .read = proc_fault_inject_read,
1116 .write = proc_fault_inject_write,
1117};
1118#endif
1119
Arjan van de Ven97455122008-01-25 21:08:34 +01001120
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001121#ifdef CONFIG_SCHED_DEBUG
1122/*
1123 * Print out various scheduling related per-task fields:
1124 */
1125static int sched_show(struct seq_file *m, void *v)
1126{
1127 struct inode *inode = m->private;
1128 struct task_struct *p;
1129
1130 WARN_ON(!inode);
1131
1132 p = get_proc_task(inode);
1133 if (!p)
1134 return -ESRCH;
1135 proc_sched_show_task(p, m);
1136
1137 put_task_struct(p);
1138
1139 return 0;
1140}
1141
1142static ssize_t
1143sched_write(struct file *file, const char __user *buf,
1144 size_t count, loff_t *offset)
1145{
1146 struct inode *inode = file->f_path.dentry->d_inode;
1147 struct task_struct *p;
1148
1149 WARN_ON(!inode);
1150
1151 p = get_proc_task(inode);
1152 if (!p)
1153 return -ESRCH;
1154 proc_sched_set_task(p);
1155
1156 put_task_struct(p);
1157
1158 return count;
1159}
1160
1161static int sched_open(struct inode *inode, struct file *filp)
1162{
1163 int ret;
1164
1165 ret = single_open(filp, sched_show, NULL);
1166 if (!ret) {
1167 struct seq_file *m = filp->private_data;
1168
1169 m->private = inode;
1170 }
1171 return ret;
1172}
1173
1174static const struct file_operations proc_pid_sched_operations = {
1175 .open = sched_open,
1176 .read = seq_read,
1177 .write = sched_write,
1178 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001179 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001180};
1181
1182#endif
1183
Al Viro008b1502005-08-20 00:17:39 +01001184static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 struct inode *inode = dentry->d_inode;
1187 int error = -EACCES;
1188
1189 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001190 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Eric W. Biederman778c1142006-06-26 00:25:58 -07001192 /* Are we allowed to snoop on the tasks file descriptors? */
1193 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001196 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 nd->last_type = LAST_BIND;
1198out:
Al Viro008b1502005-08-20 00:17:39 +01001199 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001202static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Mel Gormane12ba742007-10-16 01:25:52 -07001204 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001205 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int len;
1207
1208 if (!tmp)
1209 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001210
Jan Blunckcf28b482008-02-14 19:38:44 -08001211 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001212 len = PTR_ERR(pathname);
1213 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001215 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (len > buflen)
1218 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001219 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 len = -EFAULT;
1221 out:
1222 free_page((unsigned long)tmp);
1223 return len;
1224}
1225
1226static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1227{
1228 int error = -EACCES;
1229 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001230 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Eric W. Biederman778c1142006-06-26 00:25:58 -07001232 /* Are we allowed to snoop on the tasks file descriptors? */
1233 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001236 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (error)
1238 goto out;
1239
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001240 error = do_proc_readlink(&path, buffer, buflen);
1241 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return error;
1244}
1245
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001246static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001248 .follow_link = proc_pid_follow_link,
1249 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250};
1251
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001252
1253/* building an inode */
1254
1255static int task_dumpable(struct task_struct *task)
1256{
1257 int dumpable = 0;
1258 struct mm_struct *mm;
1259
1260 task_lock(task);
1261 mm = task->mm;
1262 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001263 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001264 task_unlock(task);
1265 if(dumpable == 1)
1266 return 1;
1267 return 0;
1268}
1269
1270
Eric W. Biederman61a28782006-10-02 02:18:49 -07001271static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001272{
1273 struct inode * inode;
1274 struct proc_inode *ei;
1275
1276 /* We need a new inode */
1277
1278 inode = new_inode(sb);
1279 if (!inode)
1280 goto out;
1281
1282 /* Common stuff */
1283 ei = PROC_I(inode);
1284 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001285 inode->i_op = &proc_def_inode_operations;
1286
1287 /*
1288 * grab the reference to task.
1289 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001290 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001291 if (!ei->pid)
1292 goto out_unlock;
1293
1294 inode->i_uid = 0;
1295 inode->i_gid = 0;
1296 if (task_dumpable(task)) {
1297 inode->i_uid = task->euid;
1298 inode->i_gid = task->egid;
1299 }
1300 security_task_to_inode(task, inode);
1301
1302out:
1303 return inode;
1304
1305out_unlock:
1306 iput(inode);
1307 return NULL;
1308}
1309
1310static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1311{
1312 struct inode *inode = dentry->d_inode;
1313 struct task_struct *task;
1314 generic_fillattr(inode, stat);
1315
1316 rcu_read_lock();
1317 stat->uid = 0;
1318 stat->gid = 0;
1319 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1320 if (task) {
1321 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1322 task_dumpable(task)) {
1323 stat->uid = task->euid;
1324 stat->gid = task->egid;
1325 }
1326 }
1327 rcu_read_unlock();
1328 return 0;
1329}
1330
1331/* dentry stuff */
1332
1333/*
1334 * Exceptional case: normally we are not allowed to unhash a busy
1335 * directory. In this case, however, we can do it - no aliasing problems
1336 * due to the way we treat inodes.
1337 *
1338 * Rewrite the inode's ownerships here because the owning task may have
1339 * performed a setuid(), etc.
1340 *
1341 * Before the /proc/pid/status file was created the only way to read
1342 * the effective uid of a /process was to stat /proc/pid. Reading
1343 * /proc/pid/status is slow enough that procps and other packages
1344 * kept stating /proc/pid. To keep the rules in /proc simple I have
1345 * made this apply to all per process world readable and executable
1346 * directories.
1347 */
1348static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1349{
1350 struct inode *inode = dentry->d_inode;
1351 struct task_struct *task = get_proc_task(inode);
1352 if (task) {
1353 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1354 task_dumpable(task)) {
1355 inode->i_uid = task->euid;
1356 inode->i_gid = task->egid;
1357 } else {
1358 inode->i_uid = 0;
1359 inode->i_gid = 0;
1360 }
1361 inode->i_mode &= ~(S_ISUID | S_ISGID);
1362 security_task_to_inode(task, inode);
1363 put_task_struct(task);
1364 return 1;
1365 }
1366 d_drop(dentry);
1367 return 0;
1368}
1369
1370static int pid_delete_dentry(struct dentry * dentry)
1371{
1372 /* Is the task we represent dead?
1373 * If so, then don't put the dentry on the lru list,
1374 * kill it immediately.
1375 */
1376 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1377}
1378
1379static struct dentry_operations pid_dentry_operations =
1380{
1381 .d_revalidate = pid_revalidate,
1382 .d_delete = pid_delete_dentry,
1383};
1384
1385/* Lookups */
1386
Eric Dumazetc5141e62007-05-08 00:26:15 -07001387typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1388 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001389
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001390/*
1391 * Fill a directory entry.
1392 *
1393 * If possible create the dcache entry and derive our inode number and
1394 * file type from dcache entry.
1395 *
1396 * Since all of the proc inode numbers are dynamically generated, the inode
1397 * numbers do not exist until the inode is cache. This means creating the
1398 * the dcache entry in readdir is necessary to keep the inode numbers
1399 * reported by readdir in sync with the inode numbers reported
1400 * by stat.
1401 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001402static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1403 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001404 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001405{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001406 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001407 struct inode *inode;
1408 struct qstr qname;
1409 ino_t ino = 0;
1410 unsigned type = DT_UNKNOWN;
1411
1412 qname.name = name;
1413 qname.len = len;
1414 qname.hash = full_name_hash(name, len);
1415
1416 child = d_lookup(dir, &qname);
1417 if (!child) {
1418 struct dentry *new;
1419 new = d_alloc(dir, &qname);
1420 if (new) {
1421 child = instantiate(dir->d_inode, new, task, ptr);
1422 if (child)
1423 dput(new);
1424 else
1425 child = new;
1426 }
1427 }
1428 if (!child || IS_ERR(child) || !child->d_inode)
1429 goto end_instantiate;
1430 inode = child->d_inode;
1431 if (inode) {
1432 ino = inode->i_ino;
1433 type = inode->i_mode >> 12;
1434 }
1435 dput(child);
1436end_instantiate:
1437 if (!ino)
1438 ino = find_inode_number(dir, &qname);
1439 if (!ino)
1440 ino = 1;
1441 return filldir(dirent, name, len, filp->f_pos, ino, type);
1442}
1443
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001444static unsigned name_to_int(struct dentry *dentry)
1445{
1446 const char *name = dentry->d_name.name;
1447 int len = dentry->d_name.len;
1448 unsigned n = 0;
1449
1450 if (len > 1 && *name == '0')
1451 goto out;
1452 while (len-- > 0) {
1453 unsigned c = *name++ - '0';
1454 if (c > 9)
1455 goto out;
1456 if (n >= (~0U-9)/10)
1457 goto out;
1458 n *= 10;
1459 n += c;
1460 }
1461 return n;
1462out:
1463 return ~0U;
1464}
1465
Miklos Szeredi27932742007-05-08 00:26:17 -07001466#define PROC_FDINFO_MAX 64
1467
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001468static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001469{
1470 struct task_struct *task = get_proc_task(inode);
1471 struct files_struct *files = NULL;
1472 struct file *file;
1473 int fd = proc_fd(inode);
1474
1475 if (task) {
1476 files = get_files_struct(task);
1477 put_task_struct(task);
1478 }
1479 if (files) {
1480 /*
1481 * We are not taking a ref to the file structure, so we must
1482 * hold ->file_lock.
1483 */
1484 spin_lock(&files->file_lock);
1485 file = fcheck_files(files, fd);
1486 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001487 if (path) {
1488 *path = file->f_path;
1489 path_get(&file->f_path);
1490 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001491 if (info)
1492 snprintf(info, PROC_FDINFO_MAX,
1493 "pos:\t%lli\n"
1494 "flags:\t0%o\n",
1495 (long long) file->f_pos,
1496 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001497 spin_unlock(&files->file_lock);
1498 put_files_struct(files);
1499 return 0;
1500 }
1501 spin_unlock(&files->file_lock);
1502 put_files_struct(files);
1503 }
1504 return -ENOENT;
1505}
1506
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001507static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001508{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001509 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001510}
1511
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001512static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1513{
1514 struct inode *inode = dentry->d_inode;
1515 struct task_struct *task = get_proc_task(inode);
1516 int fd = proc_fd(inode);
1517 struct files_struct *files;
1518
1519 if (task) {
1520 files = get_files_struct(task);
1521 if (files) {
1522 rcu_read_lock();
1523 if (fcheck_files(files, fd)) {
1524 rcu_read_unlock();
1525 put_files_struct(files);
1526 if (task_dumpable(task)) {
1527 inode->i_uid = task->euid;
1528 inode->i_gid = task->egid;
1529 } else {
1530 inode->i_uid = 0;
1531 inode->i_gid = 0;
1532 }
1533 inode->i_mode &= ~(S_ISUID | S_ISGID);
1534 security_task_to_inode(task, inode);
1535 put_task_struct(task);
1536 return 1;
1537 }
1538 rcu_read_unlock();
1539 put_files_struct(files);
1540 }
1541 put_task_struct(task);
1542 }
1543 d_drop(dentry);
1544 return 0;
1545}
1546
1547static struct dentry_operations tid_fd_dentry_operations =
1548{
1549 .d_revalidate = tid_fd_revalidate,
1550 .d_delete = pid_delete_dentry,
1551};
1552
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001553static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001554 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001555{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001556 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001557 struct file *file;
1558 struct files_struct *files;
1559 struct inode *inode;
1560 struct proc_inode *ei;
1561 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001562
Eric W. Biederman61a28782006-10-02 02:18:49 -07001563 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001564 if (!inode)
1565 goto out;
1566 ei = PROC_I(inode);
1567 ei->fd = fd;
1568 files = get_files_struct(task);
1569 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001570 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001571 inode->i_mode = S_IFLNK;
1572
1573 /*
1574 * We are not taking a ref to the file structure, so we must
1575 * hold ->file_lock.
1576 */
1577 spin_lock(&files->file_lock);
1578 file = fcheck_files(files, fd);
1579 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001580 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001581 if (file->f_mode & 1)
1582 inode->i_mode |= S_IRUSR | S_IXUSR;
1583 if (file->f_mode & 2)
1584 inode->i_mode |= S_IWUSR | S_IXUSR;
1585 spin_unlock(&files->file_lock);
1586 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001587
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001588 inode->i_op = &proc_pid_link_inode_operations;
1589 inode->i_size = 64;
1590 ei->op.proc_get_link = proc_fd_link;
1591 dentry->d_op = &tid_fd_dentry_operations;
1592 d_add(dentry, inode);
1593 /* Close the race of the process dying before we return the dentry */
1594 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001595 error = NULL;
1596
1597 out:
1598 return error;
1599out_unlock:
1600 spin_unlock(&files->file_lock);
1601 put_files_struct(files);
1602out_iput:
1603 iput(inode);
1604 goto out;
1605}
1606
Miklos Szeredi27932742007-05-08 00:26:17 -07001607static struct dentry *proc_lookupfd_common(struct inode *dir,
1608 struct dentry *dentry,
1609 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001610{
1611 struct task_struct *task = get_proc_task(dir);
1612 unsigned fd = name_to_int(dentry);
1613 struct dentry *result = ERR_PTR(-ENOENT);
1614
1615 if (!task)
1616 goto out_no_task;
1617 if (fd == ~0U)
1618 goto out;
1619
Miklos Szeredi27932742007-05-08 00:26:17 -07001620 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001621out:
1622 put_task_struct(task);
1623out_no_task:
1624 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001625}
1626
Miklos Szeredi27932742007-05-08 00:26:17 -07001627static int proc_readfd_common(struct file * filp, void * dirent,
1628 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001630 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001631 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001632 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001633 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct files_struct * files;
1636
1637 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001638 if (!p)
1639 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 fd = filp->f_pos;
1643 switch (fd) {
1644 case 0:
1645 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1646 goto out;
1647 filp->f_pos++;
1648 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001649 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1651 goto out;
1652 filp->f_pos++;
1653 default:
1654 files = get_files_struct(p);
1655 if (!files)
1656 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001657 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001659 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001661 char name[PROC_NUMBUF];
1662 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (!fcheck_files(files, fd))
1665 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001666 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Miklos Szeredi27932742007-05-08 00:26:17 -07001668 len = snprintf(name, sizeof(name), "%d", fd);
1669 if (proc_fill_cache(filp, dirent, filldir,
1670 name, len, instantiate,
1671 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001672 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 break;
1674 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001675 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001677 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 put_files_struct(files);
1679 }
1680out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001681 put_task_struct(p);
1682out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return retval;
1684}
1685
Miklos Szeredi27932742007-05-08 00:26:17 -07001686static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1687 struct nameidata *nd)
1688{
1689 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1690}
1691
1692static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1693{
1694 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1695}
1696
1697static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1698 size_t len, loff_t *ppos)
1699{
1700 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001701 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001702 if (!err)
1703 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1704 return err;
1705}
1706
1707static const struct file_operations proc_fdinfo_file_operations = {
1708 .open = nonseekable_open,
1709 .read = proc_fdinfo_read,
1710};
1711
Arjan van de Ven00977a52007-02-12 00:55:34 -08001712static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 .read = generic_read_dir,
1714 .readdir = proc_readfd,
1715};
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001718 * /proc/pid/fd needs a special permission handler so that a process can still
1719 * access /proc/self/fd after it has executed a setuid().
1720 */
1721static int proc_fd_permission(struct inode *inode, int mask,
1722 struct nameidata *nd)
1723{
1724 int rv;
1725
1726 rv = generic_permission(inode, mask, NULL);
1727 if (rv == 0)
1728 return 0;
1729 if (task_pid(current) == proc_pid(inode))
1730 rv = 0;
1731 return rv;
1732}
1733
1734/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 * proc directories can do almost nothing..
1736 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001737static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001739 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001740 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741};
1742
Miklos Szeredi27932742007-05-08 00:26:17 -07001743static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1744 struct dentry *dentry, struct task_struct *task, const void *ptr)
1745{
1746 unsigned fd = *(unsigned *)ptr;
1747 struct inode *inode;
1748 struct proc_inode *ei;
1749 struct dentry *error = ERR_PTR(-ENOENT);
1750
1751 inode = proc_pid_make_inode(dir->i_sb, task);
1752 if (!inode)
1753 goto out;
1754 ei = PROC_I(inode);
1755 ei->fd = fd;
1756 inode->i_mode = S_IFREG | S_IRUSR;
1757 inode->i_fop = &proc_fdinfo_file_operations;
1758 dentry->d_op = &tid_fd_dentry_operations;
1759 d_add(dentry, inode);
1760 /* Close the race of the process dying before we return the dentry */
1761 if (tid_fd_revalidate(dentry, NULL))
1762 error = NULL;
1763
1764 out:
1765 return error;
1766}
1767
1768static struct dentry *proc_lookupfdinfo(struct inode *dir,
1769 struct dentry *dentry,
1770 struct nameidata *nd)
1771{
1772 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1773}
1774
1775static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1776{
1777 return proc_readfd_common(filp, dirent, filldir,
1778 proc_fdinfo_instantiate);
1779}
1780
1781static const struct file_operations proc_fdinfo_operations = {
1782 .read = generic_read_dir,
1783 .readdir = proc_readfdinfo,
1784};
1785
1786/*
1787 * proc directories can do almost nothing..
1788 */
1789static const struct inode_operations proc_fdinfo_inode_operations = {
1790 .lookup = proc_lookupfdinfo,
1791 .setattr = proc_setattr,
1792};
1793
1794
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001795static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001796 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001797{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001798 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001799 struct inode *inode;
1800 struct proc_inode *ei;
1801 struct dentry *error = ERR_PTR(-EINVAL);
1802
Eric W. Biederman61a28782006-10-02 02:18:49 -07001803 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001804 if (!inode)
1805 goto out;
1806
1807 ei = PROC_I(inode);
1808 inode->i_mode = p->mode;
1809 if (S_ISDIR(inode->i_mode))
1810 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1811 if (p->iop)
1812 inode->i_op = p->iop;
1813 if (p->fop)
1814 inode->i_fop = p->fop;
1815 ei->op = p->op;
1816 dentry->d_op = &pid_dentry_operations;
1817 d_add(dentry, inode);
1818 /* Close the race of the process dying before we return the dentry */
1819 if (pid_revalidate(dentry, NULL))
1820 error = NULL;
1821out:
1822 return error;
1823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825static struct dentry *proc_pident_lookup(struct inode *dir,
1826 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001827 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001828 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001831 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001832 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001833 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001835 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 inode = NULL;
1837
Eric W. Biederman99f89552006-06-26 00:25:55 -07001838 if (!task)
1839 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001841 /*
1842 * Yes, it does not scale. And it should not. Don't add
1843 * new entries into /proc/<tgid>/ without very good reasons.
1844 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001845 last = &ents[nents - 1];
1846 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (p->len != dentry->d_name.len)
1848 continue;
1849 if (!memcmp(dentry->d_name.name, p->name, p->len))
1850 break;
1851 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001852 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 goto out;
1854
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001855 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001857 put_task_struct(task);
1858out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001859 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Eric Dumazetc5141e62007-05-08 00:26:15 -07001862static int proc_pident_fill_cache(struct file *filp, void *dirent,
1863 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001864{
1865 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1866 proc_pident_instantiate, task, p);
1867}
1868
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001869static int proc_pident_readdir(struct file *filp,
1870 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001871 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001872{
1873 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001874 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001875 struct inode *inode = dentry->d_inode;
1876 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001877 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001878 ino_t ino;
1879 int ret;
1880
1881 ret = -ENOENT;
1882 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001883 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001884
1885 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001886 i = filp->f_pos;
1887 switch (i) {
1888 case 0:
1889 ino = inode->i_ino;
1890 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1891 goto out;
1892 i++;
1893 filp->f_pos++;
1894 /* fall through */
1895 case 1:
1896 ino = parent_ino(dentry);
1897 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1898 goto out;
1899 i++;
1900 filp->f_pos++;
1901 /* fall through */
1902 default:
1903 i -= 2;
1904 if (i >= nents) {
1905 ret = 1;
1906 goto out;
1907 }
1908 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001909 last = &ents[nents - 1];
1910 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001911 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001912 goto out;
1913 filp->f_pos++;
1914 p++;
1915 }
1916 }
1917
1918 ret = 1;
1919out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001920 put_task_struct(task);
1921out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001922 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001926static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1927 size_t count, loff_t *ppos)
1928{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001929 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001930 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001931 ssize_t length;
1932 struct task_struct *task = get_proc_task(inode);
1933
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001934 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001935 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001936
1937 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001938 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001939 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001940 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001941 if (length > 0)
1942 length = simple_read_from_buffer(buf, count, ppos, p, length);
1943 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001944 return length;
1945}
1946
1947static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1948 size_t count, loff_t *ppos)
1949{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001950 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001951 char *page;
1952 ssize_t length;
1953 struct task_struct *task = get_proc_task(inode);
1954
1955 length = -ESRCH;
1956 if (!task)
1957 goto out_no_task;
1958 if (count > PAGE_SIZE)
1959 count = PAGE_SIZE;
1960
1961 /* No partial writes. */
1962 length = -EINVAL;
1963 if (*ppos != 0)
1964 goto out;
1965
1966 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001967 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001968 if (!page)
1969 goto out;
1970
1971 length = -EFAULT;
1972 if (copy_from_user(page, buf, count))
1973 goto out_free;
1974
1975 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001976 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001977 (void*)page, count);
1978out_free:
1979 free_page((unsigned long) page);
1980out:
1981 put_task_struct(task);
1982out_no_task:
1983 return length;
1984}
1985
Arjan van de Ven00977a52007-02-12 00:55:34 -08001986static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001987 .read = proc_pid_attr_read,
1988 .write = proc_pid_attr_write,
1989};
1990
Eric Dumazetc5141e62007-05-08 00:26:15 -07001991static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001992 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1993 REG("prev", S_IRUGO, pid_attr),
1994 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1995 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1996 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1997 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001998};
1999
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002000static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 void * dirent, filldir_t filldir)
2002{
2003 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002004 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Arjan van de Ven00977a52007-02-12 00:55:34 -08002007static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002009 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010};
2011
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002012static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 struct dentry *dentry, struct nameidata *nd)
2014{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002015 return proc_pident_lookup(dir, dentry,
2016 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002019static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002020 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002021 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002022 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023};
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025#endif
2026
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002027#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2028static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2029 size_t count, loff_t *ppos)
2030{
2031 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2032 struct mm_struct *mm;
2033 char buffer[PROC_NUMBUF];
2034 size_t len;
2035 int ret;
2036
2037 if (!task)
2038 return -ESRCH;
2039
2040 ret = 0;
2041 mm = get_task_mm(task);
2042 if (mm) {
2043 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2044 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2045 MMF_DUMP_FILTER_SHIFT));
2046 mmput(mm);
2047 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2048 }
2049
2050 put_task_struct(task);
2051
2052 return ret;
2053}
2054
2055static ssize_t proc_coredump_filter_write(struct file *file,
2056 const char __user *buf,
2057 size_t count,
2058 loff_t *ppos)
2059{
2060 struct task_struct *task;
2061 struct mm_struct *mm;
2062 char buffer[PROC_NUMBUF], *end;
2063 unsigned int val;
2064 int ret;
2065 int i;
2066 unsigned long mask;
2067
2068 ret = -EFAULT;
2069 memset(buffer, 0, sizeof(buffer));
2070 if (count > sizeof(buffer) - 1)
2071 count = sizeof(buffer) - 1;
2072 if (copy_from_user(buffer, buf, count))
2073 goto out_no_task;
2074
2075 ret = -EINVAL;
2076 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2077 if (*end == '\n')
2078 end++;
2079 if (end - buffer == 0)
2080 goto out_no_task;
2081
2082 ret = -ESRCH;
2083 task = get_proc_task(file->f_dentry->d_inode);
2084 if (!task)
2085 goto out_no_task;
2086
2087 ret = end - buffer;
2088 mm = get_task_mm(task);
2089 if (!mm)
2090 goto out_no_mm;
2091
2092 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2093 if (val & mask)
2094 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2095 else
2096 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2097 }
2098
2099 mmput(mm);
2100 out_no_mm:
2101 put_task_struct(task);
2102 out_no_task:
2103 return ret;
2104}
2105
2106static const struct file_operations proc_coredump_filter_operations = {
2107 .read = proc_coredump_filter_read,
2108 .write = proc_coredump_filter_write,
2109};
2110#endif
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112/*
2113 * /proc/self:
2114 */
2115static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2116 int buflen)
2117{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002118 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002119 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002120 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002121 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002122 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002123 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return vfs_readlink(dentry,buffer,buflen,tmp);
2125}
2126
Al Viro008b1502005-08-20 00:17:39 +01002127static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002129 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002130 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002131 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002132 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002133 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002134 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002135 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002138static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 .readlink = proc_self_readlink,
2140 .follow_link = proc_self_follow_link,
2141};
2142
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002143/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002144 * proc base
2145 *
2146 * These are the directory entries in the root directory of /proc
2147 * that properly belong to the /proc filesystem, as they describe
2148 * describe something that is process related.
2149 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002150static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002151 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002152 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002153};
2154
2155/*
2156 * Exceptional case: normally we are not allowed to unhash a busy
2157 * directory. In this case, however, we can do it - no aliasing problems
2158 * due to the way we treat inodes.
2159 */
2160static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2161{
2162 struct inode *inode = dentry->d_inode;
2163 struct task_struct *task = get_proc_task(inode);
2164 if (task) {
2165 put_task_struct(task);
2166 return 1;
2167 }
2168 d_drop(dentry);
2169 return 0;
2170}
2171
2172static struct dentry_operations proc_base_dentry_operations =
2173{
2174 .d_revalidate = proc_base_revalidate,
2175 .d_delete = pid_delete_dentry,
2176};
2177
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002178static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002179 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002180{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002181 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002182 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002183 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002184 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002185
2186 /* Allocate the inode */
2187 error = ERR_PTR(-ENOMEM);
2188 inode = new_inode(dir->i_sb);
2189 if (!inode)
2190 goto out;
2191
2192 /* Initialize the inode */
2193 ei = PROC_I(inode);
2194 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002195
2196 /*
2197 * grab the reference to the task.
2198 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002199 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002200 if (!ei->pid)
2201 goto out_iput;
2202
2203 inode->i_uid = 0;
2204 inode->i_gid = 0;
2205 inode->i_mode = p->mode;
2206 if (S_ISDIR(inode->i_mode))
2207 inode->i_nlink = 2;
2208 if (S_ISLNK(inode->i_mode))
2209 inode->i_size = 64;
2210 if (p->iop)
2211 inode->i_op = p->iop;
2212 if (p->fop)
2213 inode->i_fop = p->fop;
2214 ei->op = p->op;
2215 dentry->d_op = &proc_base_dentry_operations;
2216 d_add(dentry, inode);
2217 error = NULL;
2218out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002219 return error;
2220out_iput:
2221 iput(inode);
2222 goto out;
2223}
2224
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002225static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2226{
2227 struct dentry *error;
2228 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002229 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002230
2231 error = ERR_PTR(-ENOENT);
2232
2233 if (!task)
2234 goto out_no_task;
2235
2236 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002237 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2238 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002239 if (p->len != dentry->d_name.len)
2240 continue;
2241 if (!memcmp(dentry->d_name.name, p->name, p->len))
2242 break;
2243 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002244 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002245 goto out;
2246
2247 error = proc_base_instantiate(dir, dentry, task, p);
2248
2249out:
2250 put_task_struct(task);
2251out_no_task:
2252 return error;
2253}
2254
Eric Dumazetc5141e62007-05-08 00:26:15 -07002255static int proc_base_fill_cache(struct file *filp, void *dirent,
2256 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002257{
2258 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2259 proc_base_instantiate, task, p);
2260}
2261
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002262#ifdef CONFIG_TASK_IO_ACCOUNTING
2263static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2264{
2265 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002266#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002267 "rchar: %llu\n"
2268 "wchar: %llu\n"
2269 "syscr: %llu\n"
2270 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002271#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002272 "read_bytes: %llu\n"
2273 "write_bytes: %llu\n"
2274 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002275#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002276 (unsigned long long)task->rchar,
2277 (unsigned long long)task->wchar,
2278 (unsigned long long)task->syscr,
2279 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002280#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002281 (unsigned long long)task->ioac.read_bytes,
2282 (unsigned long long)task->ioac.write_bytes,
2283 (unsigned long long)task->ioac.cancelled_write_bytes);
2284}
2285#endif
2286
Eric W. Biederman801199c2006-10-02 02:18:48 -07002287/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002288 * Thread groups
2289 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002290static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002291static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002292
Eric Dumazetc5141e62007-05-08 00:26:15 -07002293static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002294 DIR("task", S_IRUGO|S_IXUGO, task),
2295 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002296 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002297#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002298 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002299#endif
James Pearson315e28c2007-10-16 23:30:17 -07002300 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002301 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002302 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002303 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002304#ifdef CONFIG_SCHED_DEBUG
2305 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2306#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002307 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002308 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002309 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002310 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002311#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002312 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002313#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002314 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002315 LNK("cwd", cwd),
2316 LNK("root", root),
2317 LNK("exe", exe),
2318 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002319 REG("mountinfo", S_IRUGO, mountinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002320 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002321#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002322 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002323 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002324 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002325#endif
2326#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002327 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002328#endif
2329#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002330 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002331#endif
2332#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002333 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002334#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002335#ifdef CONFIG_LATENCYTOP
2336 REG("latency", S_IRUGO, lstats),
2337#endif
Paul Menage8793d852007-10-18 23:39:39 -07002338#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002339 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002340#endif
Paul Menagea4243162007-10-18 23:39:35 -07002341#ifdef CONFIG_CGROUPS
2342 REG("cgroup", S_IRUGO, cgroup),
2343#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002344 INF("oom_score", S_IRUGO, oom_score),
2345 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002346#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002347 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002348 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002349#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002350#ifdef CONFIG_FAULT_INJECTION
2351 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2352#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002353#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2354 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2355#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002356#ifdef CONFIG_TASK_IO_ACCOUNTING
2357 INF("io", S_IRUGO, pid_io_accounting),
2358#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002359};
2360
2361static int proc_tgid_base_readdir(struct file * filp,
2362 void * dirent, filldir_t filldir)
2363{
2364 return proc_pident_readdir(filp,dirent,filldir,
2365 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2366}
2367
Arjan van de Ven00977a52007-02-12 00:55:34 -08002368static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002369 .read = generic_read_dir,
2370 .readdir = proc_tgid_base_readdir,
2371};
2372
2373static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002374 return proc_pident_lookup(dir, dentry,
2375 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002376}
2377
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002378static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002379 .lookup = proc_tgid_base_lookup,
2380 .getattr = pid_getattr,
2381 .setattr = proc_setattr,
2382};
2383
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002384static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002386 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002387 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002388 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Eric W. Biederman48e64842006-06-26 00:25:48 -07002390 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002391 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2392 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002393 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002394 if (!(current->flags & PF_EXITING))
2395 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002396 d_drop(dentry);
2397 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002400 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002401 goto out;
2402
2403 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002404 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2405 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002406 if (!leader)
2407 goto out;
2408
2409 name.name = "task";
2410 name.len = strlen(name.name);
2411 dir = d_hash_and_lookup(leader, &name);
2412 if (!dir)
2413 goto out_put_leader;
2414
2415 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002416 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002417 dentry = d_hash_and_lookup(dir, &name);
2418 if (dentry) {
2419 shrink_dcache_parent(dentry);
2420 d_drop(dentry);
2421 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002423
2424 dput(dir);
2425out_put_leader:
2426 dput(leader);
2427out:
2428 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Randy Dunlap0895e912007-10-21 21:00:10 -07002431/**
2432 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2433 * @task: task that should be flushed.
2434 *
2435 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002436 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002437 * in. This call is supposed to do all of this job.
2438 *
2439 * Looks in the dcache for
2440 * /proc/@pid
2441 * /proc/@tgid/task/@pid
2442 * if either directory is present flushes it and all of it'ts children
2443 * from the dcache.
2444 *
2445 * It is safe and reasonable to cache /proc entries for a task until
2446 * that task exits. After that they just clog up the dcache with
2447 * useless entries, possibly causing useful dcache entries to be
2448 * flushed instead. This routine is proved to flush those useless
2449 * dcache entries at process exit time.
2450 *
2451 * NOTE: This routine is just an optimization so it does not guarantee
2452 * that no dcache entries will exist at process exit time it
2453 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002454 */
2455
2456void proc_flush_task(struct task_struct *task)
2457{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002458 int i;
2459 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002460 struct upid *upid;
2461
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002462 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002463 if (thread_group_leader(task))
2464 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002465
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002466 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002467 upid = &pid->numbers[i];
2468 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002469 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002470 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002471
2472 upid = &pid->numbers[pid->level];
2473 if (upid->nr == 1)
2474 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002475}
2476
Adrian Bunk9711ef92006-12-06 20:38:31 -08002477static struct dentry *proc_pid_instantiate(struct inode *dir,
2478 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002479 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002480{
2481 struct dentry *error = ERR_PTR(-ENOENT);
2482 struct inode *inode;
2483
Eric W. Biederman61a28782006-10-02 02:18:49 -07002484 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002485 if (!inode)
2486 goto out;
2487
2488 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2489 inode->i_op = &proc_tgid_base_inode_operations;
2490 inode->i_fop = &proc_tgid_base_operations;
2491 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002492 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002493#ifdef CONFIG_SECURITY
2494 inode->i_nlink += 1;
2495#endif
2496
2497 dentry->d_op = &pid_dentry_operations;
2498
2499 d_add(dentry, inode);
2500 /* Close the race of the process dying before we return the dentry */
2501 if (pid_revalidate(dentry, NULL))
2502 error = NULL;
2503out:
2504 return error;
2505}
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2508{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002509 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002512 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Eric W. Biederman801199c2006-10-02 02:18:48 -07002514 result = proc_base_lookup(dir, dentry);
2515 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2516 goto out;
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 tgid = name_to_int(dentry);
2519 if (tgid == ~0U)
2520 goto out;
2521
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002522 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002523 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002524 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (task)
2526 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002527 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (!task)
2529 goto out;
2530
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002531 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002532 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002534 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002538 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002539 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002541struct tgid_iter {
2542 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002543 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002544};
2545static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2546{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002547 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002549 if (iter.task)
2550 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002551 rcu_read_lock();
2552retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002553 iter.task = NULL;
2554 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002555 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002556 iter.tgid = pid_nr_ns(pid, ns);
2557 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002558 /* What we to know is if the pid we have find is the
2559 * pid of a thread_group_leader. Testing for task
2560 * being a thread_group_leader is the obvious thing
2561 * todo but there is a window when it fails, due to
2562 * the pid transfer logic in de_thread.
2563 *
2564 * So we perform the straight forward test of seeing
2565 * if the pid we have found is the pid of a thread
2566 * group leader, and don't worry if the task we have
2567 * found doesn't happen to be a thread group leader.
2568 * As we don't care in the case of readdir.
2569 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002570 if (!iter.task || !has_group_leader_pid(iter.task)) {
2571 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002572 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002573 }
2574 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002576 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002577 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002580#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Eric W. Biederman61a28782006-10-02 02:18:49 -07002582static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002583 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002584{
2585 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002586 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002587 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002588 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002589}
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591/* for the /proc/ directory itself, after non-process stuff has been done */
2592int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002595 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002596 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002597 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Eric W. Biederman61a28782006-10-02 02:18:49 -07002599 if (!reaper)
2600 goto out_no_task;
2601
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002602 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002603 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002604 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002608 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002609 iter.task = NULL;
2610 iter.tgid = filp->f_pos - TGID_OFFSET;
2611 for (iter = next_tgid(ns, iter);
2612 iter.task;
2613 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2614 filp->f_pos = iter.tgid + TGID_OFFSET;
2615 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2616 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002617 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002620 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2621out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002622 put_task_struct(reaper);
2623out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return 0;
2625}
2626
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002627/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002628 * Tasks
2629 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002630static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002631 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002632 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002633 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002634 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002635 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002636 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002637#ifdef CONFIG_SCHED_DEBUG
2638 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2639#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002640 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002641 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002642 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002643 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002644#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002645 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002646#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002647 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002648 LNK("cwd", cwd),
2649 LNK("root", root),
2650 LNK("exe", exe),
2651 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002652 REG("mountinfo", S_IRUGO, mountinfo),
Matt Mackall1e883282008-02-04 22:29:07 -08002653#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002654 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002655 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002656 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657#endif
2658#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002659 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002660#endif
2661#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002662 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002663#endif
2664#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002665 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002666#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002667#ifdef CONFIG_LATENCYTOP
2668 REG("latency", S_IRUGO, lstats),
2669#endif
Paul Menage8793d852007-10-18 23:39:39 -07002670#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002671 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002672#endif
Paul Menagea4243162007-10-18 23:39:35 -07002673#ifdef CONFIG_CGROUPS
2674 REG("cgroup", S_IRUGO, cgroup),
2675#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002676 INF("oom_score", S_IRUGO, oom_score),
2677 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002678#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002679 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002680 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002681#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002682#ifdef CONFIG_FAULT_INJECTION
2683 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2684#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002685};
2686
2687static int proc_tid_base_readdir(struct file * filp,
2688 void * dirent, filldir_t filldir)
2689{
2690 return proc_pident_readdir(filp,dirent,filldir,
2691 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2692}
2693
2694static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002695 return proc_pident_lookup(dir, dentry,
2696 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002697}
2698
Arjan van de Ven00977a52007-02-12 00:55:34 -08002699static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002700 .read = generic_read_dir,
2701 .readdir = proc_tid_base_readdir,
2702};
2703
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002704static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002705 .lookup = proc_tid_base_lookup,
2706 .getattr = pid_getattr,
2707 .setattr = proc_setattr,
2708};
2709
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002710static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002711 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002712{
2713 struct dentry *error = ERR_PTR(-ENOENT);
2714 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002715 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002716
2717 if (!inode)
2718 goto out;
2719 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2720 inode->i_op = &proc_tid_base_inode_operations;
2721 inode->i_fop = &proc_tid_base_operations;
2722 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002723 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002724#ifdef CONFIG_SECURITY
2725 inode->i_nlink += 1;
2726#endif
2727
2728 dentry->d_op = &pid_dentry_operations;
2729
2730 d_add(dentry, inode);
2731 /* Close the race of the process dying before we return the dentry */
2732 if (pid_revalidate(dentry, NULL))
2733 error = NULL;
2734out:
2735 return error;
2736}
2737
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002738static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2739{
2740 struct dentry *result = ERR_PTR(-ENOENT);
2741 struct task_struct *task;
2742 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002743 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002744 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002745
2746 if (!leader)
2747 goto out_no_task;
2748
2749 tid = name_to_int(dentry);
2750 if (tid == ~0U)
2751 goto out;
2752
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002753 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002754 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002755 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002756 if (task)
2757 get_task_struct(task);
2758 rcu_read_unlock();
2759 if (!task)
2760 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002761 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002762 goto out_drop_task;
2763
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002764 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002765out_drop_task:
2766 put_task_struct(task);
2767out:
2768 put_task_struct(leader);
2769out_no_task:
2770 return result;
2771}
2772
2773/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002774 * Find the first tid of a thread group to return to user space.
2775 *
2776 * Usually this is just the thread group leader, but if the users
2777 * buffer was too small or there was a seek into the middle of the
2778 * directory we have more work todo.
2779 *
2780 * In the case of a short read we start with find_task_by_pid.
2781 *
2782 * In the case of a seek we start with the leader and walk nr
2783 * threads past it.
2784 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002785static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002786 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002787{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002788 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002789
Eric W. Biedermancc288732006-06-26 00:26:01 -07002790 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002791 /* Attempt to start with the pid of a thread */
2792 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002793 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002794 if (pos && (pos->group_leader == leader))
2795 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002796 }
2797
2798 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002799 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002800 if (nr && nr >= get_nr_threads(leader))
2801 goto out;
2802
2803 /* If we haven't found our starting place yet start
2804 * with the leader and walk nr threads forward.
2805 */
2806 for (pos = leader; nr > 0; --nr) {
2807 pos = next_thread(pos);
2808 if (pos == leader) {
2809 pos = NULL;
2810 goto out;
2811 }
2812 }
2813found:
2814 get_task_struct(pos);
2815out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002816 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002817 return pos;
2818}
2819
2820/*
2821 * Find the next thread in the thread list.
2822 * Return NULL if there is an error or no next thread.
2823 *
2824 * The reference to the input task_struct is released.
2825 */
2826static struct task_struct *next_tid(struct task_struct *start)
2827{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002828 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002829 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002830 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002831 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002832 if (thread_group_leader(pos))
2833 pos = NULL;
2834 else
2835 get_task_struct(pos);
2836 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002837 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002838 put_task_struct(start);
2839 return pos;
2840}
2841
Eric W. Biederman61a28782006-10-02 02:18:49 -07002842static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2843 struct task_struct *task, int tid)
2844{
2845 char name[PROC_NUMBUF];
2846 int len = snprintf(name, sizeof(name), "%d", tid);
2847 return proc_fill_cache(filp, dirent, filldir, name, len,
2848 proc_task_instantiate, task, NULL);
2849}
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851/* for the /proc/TGID/task/ directories */
2852static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2853{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002854 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002856 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002857 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 int retval = -ENOENT;
2859 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002860 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002862 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002864 task = get_proc_task(inode);
2865 if (!task)
2866 goto out_no_task;
2867 rcu_read_lock();
2868 if (pid_alive(task)) {
2869 leader = task->group_leader;
2870 get_task_struct(leader);
2871 }
2872 rcu_read_unlock();
2873 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002874 if (!leader)
2875 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 retval = 0;
2877
2878 switch (pos) {
2879 case 0:
2880 ino = inode->i_ino;
2881 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2882 goto out;
2883 pos++;
2884 /* fall through */
2885 case 1:
2886 ino = parent_ino(dentry);
2887 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2888 goto out;
2889 pos++;
2890 /* fall through */
2891 }
2892
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002893 /* f_version caches the tgid value that the last readdir call couldn't
2894 * return. lseek aka telldir automagically resets f_version to 0.
2895 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002896 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002897 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002898 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002899 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002900 task;
2901 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002902 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002903 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002904 /* returning this tgid failed, save it as the first
2905 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002906 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002907 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911out:
2912 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002913 put_task_struct(leader);
2914out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return retval;
2916}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002917
2918static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2919{
2920 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002921 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002922 generic_fillattr(inode, stat);
2923
Eric W. Biederman99f89552006-06-26 00:25:55 -07002924 if (p) {
2925 rcu_read_lock();
2926 stat->nlink += get_nr_threads(p);
2927 rcu_read_unlock();
2928 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002929 }
2930
2931 return 0;
2932}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002934static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002935 .lookup = proc_task_lookup,
2936 .getattr = proc_task_getattr,
2937 .setattr = proc_setattr,
2938};
2939
Arjan van de Ven00977a52007-02-12 00:55:34 -08002940static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002941 .read = generic_read_dir,
2942 .readdir = proc_task_readdir,
2943};