blob: 7313c62e3e9dd47f103bd1cf5e0e32f3e3356356 [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
Jan Engelhardt03a44822008-02-08 04:21:19 -0800505extern const struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500506struct proc_mounts {
507 struct seq_file m;
508 int event;
509};
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static int mounts_open(struct inode *inode, struct file *file)
512{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700513 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700514 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800515 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500516 struct proc_mounts *p;
517 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Eric W. Biederman99f89552006-06-26 00:25:55 -0700519 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700520 rcu_read_lock();
521 nsp = task_nsproxy(task);
522 if (nsp) {
523 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800524 if (ns)
525 get_mnt_ns(ns);
526 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700527 rcu_read_unlock();
528
Eric W. Biederman99f89552006-06-26 00:25:55 -0700529 put_task_struct(task);
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800532 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500533 ret = -ENOMEM;
534 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
535 if (p) {
536 file->private_data = &p->m;
537 ret = seq_open(file, &mounts_op);
538 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800539 p->m.private = ns;
540 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500541 return 0;
542 }
543 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800545 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 return ret;
548}
549
550static int mounts_release(struct inode *inode, struct file *file)
551{
552 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800553 struct mnt_namespace *ns = m->private;
554 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return seq_release(inode, file);
556}
557
Al Viro5addc5d2005-11-07 17:15:49 -0500558static unsigned mounts_poll(struct file *file, poll_table *wait)
559{
560 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800561 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500562 unsigned res = 0;
563
564 poll_wait(file, &ns->poll, wait);
565
566 spin_lock(&vfsmount_lock);
567 if (p->event != ns->event) {
568 p->event = ns->event;
569 res = POLLERR;
570 }
571 spin_unlock(&vfsmount_lock);
572
573 return res;
574}
575
Arjan van de Ven00977a52007-02-12 00:55:34 -0800576static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 .open = mounts_open,
578 .read = seq_read,
579 .llseek = seq_lseek,
580 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500581 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582};
583
Jan Engelhardt03a44822008-02-08 04:21:19 -0800584extern const struct seq_operations mountstats_op;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500585static int mountstats_open(struct inode *inode, struct file *file)
586{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500587 int ret = seq_open(file, &mountstats_op);
588
589 if (!ret) {
590 struct seq_file *m = file->private_data;
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700591 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800592 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700593 struct task_struct *task = get_proc_task(inode);
594
595 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700596 rcu_read_lock();
597 nsp = task_nsproxy(task);
598 if (nsp) {
599 mnt_ns = nsp->mnt_ns;
600 if (mnt_ns)
601 get_mnt_ns(mnt_ns);
602 }
603 rcu_read_unlock();
604
Eric W. Biederman99f89552006-06-26 00:25:55 -0700605 put_task_struct(task);
606 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500607
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800608 if (mnt_ns)
609 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500610 else {
611 seq_release(inode, file);
612 ret = -EINVAL;
613 }
614 }
615 return ret;
616}
617
Arjan van de Ven00977a52007-02-12 00:55:34 -0800618static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500619 .open = mountstats_open,
620 .read = seq_read,
621 .llseek = seq_lseek,
622 .release = mounts_release,
623};
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
626
627static ssize_t proc_info_read(struct file * file, char __user * buf,
628 size_t count, loff_t *ppos)
629{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800630 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 unsigned long page;
632 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700633 struct task_struct *task = get_proc_task(inode);
634
635 length = -ESRCH;
636 if (!task)
637 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (count > PROC_BLOCK_SIZE)
640 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700641
642 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700643 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700644 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 length = PROC_I(inode)->op.proc_read(task, (char*)page);
647
648 if (length >= 0)
649 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
650 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700651out:
652 put_task_struct(task);
653out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return length;
655}
656
Arjan van de Ven00977a52007-02-12 00:55:34 -0800657static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 .read = proc_info_read,
659};
660
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800661static int proc_single_show(struct seq_file *m, void *v)
662{
663 struct inode *inode = m->private;
664 struct pid_namespace *ns;
665 struct pid *pid;
666 struct task_struct *task;
667 int ret;
668
669 ns = inode->i_sb->s_fs_info;
670 pid = proc_pid(inode);
671 task = get_pid_task(pid, PIDTYPE_PID);
672 if (!task)
673 return -ESRCH;
674
675 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
676
677 put_task_struct(task);
678 return ret;
679}
680
681static int proc_single_open(struct inode *inode, struct file *filp)
682{
683 int ret;
684 ret = single_open(filp, proc_single_show, NULL);
685 if (!ret) {
686 struct seq_file *m = filp->private_data;
687
688 m->private = inode;
689 }
690 return ret;
691}
692
693static const struct file_operations proc_single_file_operations = {
694 .open = proc_single_open,
695 .read = seq_read,
696 .llseek = seq_lseek,
697 .release = single_release,
698};
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static int mem_open(struct inode* inode, struct file* file)
701{
702 file->private_data = (void*)((long)current->self_exec_id);
703 return 0;
704}
705
706static ssize_t mem_read(struct file * file, char __user * buf,
707 size_t count, loff_t *ppos)
708{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800709 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 char *page;
711 unsigned long src = *ppos;
712 int ret = -ESRCH;
713 struct mm_struct *mm;
714
Eric W. Biederman99f89552006-06-26 00:25:55 -0700715 if (!task)
716 goto out_no_task;
717
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700718 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto out;
720
721 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700722 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!page)
724 goto out;
725
726 ret = 0;
727
728 mm = get_task_mm(task);
729 if (!mm)
730 goto out_free;
731
732 ret = -EIO;
733
734 if (file->private_data != (void*)((long)current->self_exec_id))
735 goto out_put;
736
737 ret = 0;
738
739 while (count > 0) {
740 int this_len, retval;
741
742 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
743 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700744 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!ret)
746 ret = -EIO;
747 break;
748 }
749
750 if (copy_to_user(buf, page, retval)) {
751 ret = -EFAULT;
752 break;
753 }
754
755 ret += retval;
756 src += retval;
757 buf += retval;
758 count -= retval;
759 }
760 *ppos = src;
761
762out_put:
763 mmput(mm);
764out_free:
765 free_page((unsigned long) page);
766out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700767 put_task_struct(task);
768out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return ret;
770}
771
772#define mem_write NULL
773
774#ifndef mem_write
775/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800776static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 size_t count, loff_t *ppos)
778{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700779 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800781 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 unsigned long dst = *ppos;
783
Eric W. Biederman99f89552006-06-26 00:25:55 -0700784 copied = -ESRCH;
785 if (!task)
786 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Eric W. Biederman99f89552006-06-26 00:25:55 -0700788 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
789 goto out;
790
791 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700792 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700794 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700796 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 while (count > 0) {
798 int this_len, retval;
799
800 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
801 if (copy_from_user(page, buf, this_len)) {
802 copied = -EFAULT;
803 break;
804 }
805 retval = access_process_vm(task, dst, page, this_len, 1);
806 if (!retval) {
807 if (!copied)
808 copied = -EIO;
809 break;
810 }
811 copied += retval;
812 buf += retval;
813 dst += retval;
814 count -= retval;
815 }
816 *ppos = dst;
817 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700818out:
819 put_task_struct(task);
820out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return copied;
822}
823#endif
824
Matt Mackall85863e42008-02-04 22:29:04 -0800825loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 switch (orig) {
828 case 0:
829 file->f_pos = offset;
830 break;
831 case 1:
832 file->f_pos += offset;
833 break;
834 default:
835 return -EINVAL;
836 }
837 force_successful_syscall_return();
838 return file->f_pos;
839}
840
Arjan van de Ven00977a52007-02-12 00:55:34 -0800841static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 .llseek = mem_lseek,
843 .read = mem_read,
844 .write = mem_write,
845 .open = mem_open,
846};
847
James Pearson315e28c2007-10-16 23:30:17 -0700848static ssize_t environ_read(struct file *file, char __user *buf,
849 size_t count, loff_t *ppos)
850{
851 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
852 char *page;
853 unsigned long src = *ppos;
854 int ret = -ESRCH;
855 struct mm_struct *mm;
856
857 if (!task)
858 goto out_no_task;
859
860 if (!ptrace_may_attach(task))
861 goto out;
862
863 ret = -ENOMEM;
864 page = (char *)__get_free_page(GFP_TEMPORARY);
865 if (!page)
866 goto out;
867
868 ret = 0;
869
870 mm = get_task_mm(task);
871 if (!mm)
872 goto out_free;
873
874 while (count > 0) {
875 int this_len, retval, max_len;
876
877 this_len = mm->env_end - (mm->env_start + src);
878
879 if (this_len <= 0)
880 break;
881
882 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
883 this_len = (this_len > max_len) ? max_len : this_len;
884
885 retval = access_process_vm(task, (mm->env_start + src),
886 page, this_len, 0);
887
888 if (retval <= 0) {
889 ret = retval;
890 break;
891 }
892
893 if (copy_to_user(buf, page, retval)) {
894 ret = -EFAULT;
895 break;
896 }
897
898 ret += retval;
899 src += retval;
900 buf += retval;
901 count -= retval;
902 }
903 *ppos = src;
904
905 mmput(mm);
906out_free:
907 free_page((unsigned long) page);
908out:
909 put_task_struct(task);
910out_no_task:
911 return ret;
912}
913
914static const struct file_operations proc_environ_operations = {
915 .read = environ_read,
916};
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918static ssize_t oom_adjust_read(struct file *file, char __user *buf,
919 size_t count, loff_t *ppos)
920{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800921 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700922 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700924 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Eric W. Biederman99f89552006-06-26 00:25:55 -0700926 if (!task)
927 return -ESRCH;
928 oom_adjust = task->oomkilladj;
929 put_task_struct(task);
930
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700931 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700932
933 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
937 size_t count, loff_t *ppos)
938{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700939 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700940 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int oom_adjust;
942
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700943 memset(buffer, 0, sizeof(buffer));
944 if (count > sizeof(buffer) - 1)
945 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (copy_from_user(buffer, buf, count))
947 return -EFAULT;
948 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700949 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
950 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return -EINVAL;
952 if (*end == '\n')
953 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800954 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700955 if (!task)
956 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800957 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
958 put_task_struct(task);
959 return -EACCES;
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700962 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (end - buffer == 0)
964 return -EIO;
965 return end - buffer;
966}
967
Arjan van de Ven00977a52007-02-12 00:55:34 -0800968static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 .read = oom_adjust_read,
970 .write = oom_adjust_write,
971};
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973#ifdef CONFIG_AUDITSYSCALL
974#define TMPBUFLEN 21
975static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
976 size_t count, loff_t *ppos)
977{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800978 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700979 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 ssize_t length;
981 char tmpbuf[TMPBUFLEN];
982
Eric W. Biederman99f89552006-06-26 00:25:55 -0700983 if (!task)
984 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -0500986 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700987 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
989}
990
991static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
992 size_t count, loff_t *ppos)
993{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800994 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 char *page, *tmp;
996 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 uid_t loginuid;
998
999 if (!capable(CAP_AUDIT_CONTROL))
1000 return -EPERM;
1001
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001002 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return -EPERM;
1004
Al Viroe0182902006-05-18 08:28:02 -04001005 if (count >= PAGE_SIZE)
1006 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 if (*ppos != 0) {
1009 /* No partial writes. */
1010 return -EINVAL;
1011 }
Mel Gormane12ba742007-10-16 01:25:52 -07001012 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (!page)
1014 return -ENOMEM;
1015 length = -EFAULT;
1016 if (copy_from_user(page, buf, count))
1017 goto out_free_page;
1018
Al Viroe0182902006-05-18 08:28:02 -04001019 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 loginuid = simple_strtoul(page, &tmp, 10);
1021 if (tmp == page) {
1022 length = -EINVAL;
1023 goto out_free_page;
1024
1025 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001026 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (likely(length == 0))
1028 length = count;
1029
1030out_free_page:
1031 free_page((unsigned long) page);
1032 return length;
1033}
1034
Arjan van de Ven00977a52007-02-12 00:55:34 -08001035static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 .read = proc_loginuid_read,
1037 .write = proc_loginuid_write,
1038};
Eric Paris1e0bd752008-03-13 08:15:31 -04001039
1040static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1041 size_t count, loff_t *ppos)
1042{
1043 struct inode * inode = file->f_path.dentry->d_inode;
1044 struct task_struct *task = get_proc_task(inode);
1045 ssize_t length;
1046 char tmpbuf[TMPBUFLEN];
1047
1048 if (!task)
1049 return -ESRCH;
1050 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1051 audit_get_sessionid(task));
1052 put_task_struct(task);
1053 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1054}
1055
1056static const struct file_operations proc_sessionid_operations = {
1057 .read = proc_sessionid_read,
1058};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059#endif
1060
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001061#ifdef CONFIG_FAULT_INJECTION
1062static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1063 size_t count, loff_t *ppos)
1064{
1065 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1066 char buffer[PROC_NUMBUF];
1067 size_t len;
1068 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001069
1070 if (!task)
1071 return -ESRCH;
1072 make_it_fail = task->make_it_fail;
1073 put_task_struct(task);
1074
1075 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001076
1077 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001078}
1079
1080static ssize_t proc_fault_inject_write(struct file * file,
1081 const char __user * buf, size_t count, loff_t *ppos)
1082{
1083 struct task_struct *task;
1084 char buffer[PROC_NUMBUF], *end;
1085 int make_it_fail;
1086
1087 if (!capable(CAP_SYS_RESOURCE))
1088 return -EPERM;
1089 memset(buffer, 0, sizeof(buffer));
1090 if (count > sizeof(buffer) - 1)
1091 count = sizeof(buffer) - 1;
1092 if (copy_from_user(buffer, buf, count))
1093 return -EFAULT;
1094 make_it_fail = simple_strtol(buffer, &end, 0);
1095 if (*end == '\n')
1096 end++;
1097 task = get_proc_task(file->f_dentry->d_inode);
1098 if (!task)
1099 return -ESRCH;
1100 task->make_it_fail = make_it_fail;
1101 put_task_struct(task);
1102 if (end - buffer == 0)
1103 return -EIO;
1104 return end - buffer;
1105}
1106
Arjan van de Ven00977a52007-02-12 00:55:34 -08001107static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001108 .read = proc_fault_inject_read,
1109 .write = proc_fault_inject_write,
1110};
1111#endif
1112
Arjan van de Ven97455122008-01-25 21:08:34 +01001113
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001114#ifdef CONFIG_SCHED_DEBUG
1115/*
1116 * Print out various scheduling related per-task fields:
1117 */
1118static int sched_show(struct seq_file *m, void *v)
1119{
1120 struct inode *inode = m->private;
1121 struct task_struct *p;
1122
1123 WARN_ON(!inode);
1124
1125 p = get_proc_task(inode);
1126 if (!p)
1127 return -ESRCH;
1128 proc_sched_show_task(p, m);
1129
1130 put_task_struct(p);
1131
1132 return 0;
1133}
1134
1135static ssize_t
1136sched_write(struct file *file, const char __user *buf,
1137 size_t count, loff_t *offset)
1138{
1139 struct inode *inode = file->f_path.dentry->d_inode;
1140 struct task_struct *p;
1141
1142 WARN_ON(!inode);
1143
1144 p = get_proc_task(inode);
1145 if (!p)
1146 return -ESRCH;
1147 proc_sched_set_task(p);
1148
1149 put_task_struct(p);
1150
1151 return count;
1152}
1153
1154static int sched_open(struct inode *inode, struct file *filp)
1155{
1156 int ret;
1157
1158 ret = single_open(filp, sched_show, NULL);
1159 if (!ret) {
1160 struct seq_file *m = filp->private_data;
1161
1162 m->private = inode;
1163 }
1164 return ret;
1165}
1166
1167static const struct file_operations proc_pid_sched_operations = {
1168 .open = sched_open,
1169 .read = seq_read,
1170 .write = sched_write,
1171 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001172 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001173};
1174
1175#endif
1176
Al Viro008b1502005-08-20 00:17:39 +01001177static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 struct inode *inode = dentry->d_inode;
1180 int error = -EACCES;
1181
1182 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001183 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Eric W. Biederman778c1142006-06-26 00:25:58 -07001185 /* Are we allowed to snoop on the tasks file descriptors? */
1186 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001189 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 nd->last_type = LAST_BIND;
1191out:
Al Viro008b1502005-08-20 00:17:39 +01001192 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001195static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Mel Gormane12ba742007-10-16 01:25:52 -07001197 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001198 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int len;
1200
1201 if (!tmp)
1202 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001203
Jan Blunckcf28b482008-02-14 19:38:44 -08001204 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001205 len = PTR_ERR(pathname);
1206 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001208 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (len > buflen)
1211 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001212 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 len = -EFAULT;
1214 out:
1215 free_page((unsigned long)tmp);
1216 return len;
1217}
1218
1219static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1220{
1221 int error = -EACCES;
1222 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001223 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Eric W. Biederman778c1142006-06-26 00:25:58 -07001225 /* Are we allowed to snoop on the tasks file descriptors? */
1226 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001229 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (error)
1231 goto out;
1232
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001233 error = do_proc_readlink(&path, buffer, buflen);
1234 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return error;
1237}
1238
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001239static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001241 .follow_link = proc_pid_follow_link,
1242 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243};
1244
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001245
1246/* building an inode */
1247
1248static int task_dumpable(struct task_struct *task)
1249{
1250 int dumpable = 0;
1251 struct mm_struct *mm;
1252
1253 task_lock(task);
1254 mm = task->mm;
1255 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001256 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001257 task_unlock(task);
1258 if(dumpable == 1)
1259 return 1;
1260 return 0;
1261}
1262
1263
Eric W. Biederman61a28782006-10-02 02:18:49 -07001264static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001265{
1266 struct inode * inode;
1267 struct proc_inode *ei;
1268
1269 /* We need a new inode */
1270
1271 inode = new_inode(sb);
1272 if (!inode)
1273 goto out;
1274
1275 /* Common stuff */
1276 ei = PROC_I(inode);
1277 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001278 inode->i_op = &proc_def_inode_operations;
1279
1280 /*
1281 * grab the reference to task.
1282 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001283 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001284 if (!ei->pid)
1285 goto out_unlock;
1286
1287 inode->i_uid = 0;
1288 inode->i_gid = 0;
1289 if (task_dumpable(task)) {
1290 inode->i_uid = task->euid;
1291 inode->i_gid = task->egid;
1292 }
1293 security_task_to_inode(task, inode);
1294
1295out:
1296 return inode;
1297
1298out_unlock:
1299 iput(inode);
1300 return NULL;
1301}
1302
1303static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1304{
1305 struct inode *inode = dentry->d_inode;
1306 struct task_struct *task;
1307 generic_fillattr(inode, stat);
1308
1309 rcu_read_lock();
1310 stat->uid = 0;
1311 stat->gid = 0;
1312 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1313 if (task) {
1314 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1315 task_dumpable(task)) {
1316 stat->uid = task->euid;
1317 stat->gid = task->egid;
1318 }
1319 }
1320 rcu_read_unlock();
1321 return 0;
1322}
1323
1324/* dentry stuff */
1325
1326/*
1327 * Exceptional case: normally we are not allowed to unhash a busy
1328 * directory. In this case, however, we can do it - no aliasing problems
1329 * due to the way we treat inodes.
1330 *
1331 * Rewrite the inode's ownerships here because the owning task may have
1332 * performed a setuid(), etc.
1333 *
1334 * Before the /proc/pid/status file was created the only way to read
1335 * the effective uid of a /process was to stat /proc/pid. Reading
1336 * /proc/pid/status is slow enough that procps and other packages
1337 * kept stating /proc/pid. To keep the rules in /proc simple I have
1338 * made this apply to all per process world readable and executable
1339 * directories.
1340 */
1341static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1342{
1343 struct inode *inode = dentry->d_inode;
1344 struct task_struct *task = get_proc_task(inode);
1345 if (task) {
1346 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1347 task_dumpable(task)) {
1348 inode->i_uid = task->euid;
1349 inode->i_gid = task->egid;
1350 } else {
1351 inode->i_uid = 0;
1352 inode->i_gid = 0;
1353 }
1354 inode->i_mode &= ~(S_ISUID | S_ISGID);
1355 security_task_to_inode(task, inode);
1356 put_task_struct(task);
1357 return 1;
1358 }
1359 d_drop(dentry);
1360 return 0;
1361}
1362
1363static int pid_delete_dentry(struct dentry * dentry)
1364{
1365 /* Is the task we represent dead?
1366 * If so, then don't put the dentry on the lru list,
1367 * kill it immediately.
1368 */
1369 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1370}
1371
1372static struct dentry_operations pid_dentry_operations =
1373{
1374 .d_revalidate = pid_revalidate,
1375 .d_delete = pid_delete_dentry,
1376};
1377
1378/* Lookups */
1379
Eric Dumazetc5141e62007-05-08 00:26:15 -07001380typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1381 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001382
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001383/*
1384 * Fill a directory entry.
1385 *
1386 * If possible create the dcache entry and derive our inode number and
1387 * file type from dcache entry.
1388 *
1389 * Since all of the proc inode numbers are dynamically generated, the inode
1390 * numbers do not exist until the inode is cache. This means creating the
1391 * the dcache entry in readdir is necessary to keep the inode numbers
1392 * reported by readdir in sync with the inode numbers reported
1393 * by stat.
1394 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001395static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1396 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001397 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001398{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001399 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001400 struct inode *inode;
1401 struct qstr qname;
1402 ino_t ino = 0;
1403 unsigned type = DT_UNKNOWN;
1404
1405 qname.name = name;
1406 qname.len = len;
1407 qname.hash = full_name_hash(name, len);
1408
1409 child = d_lookup(dir, &qname);
1410 if (!child) {
1411 struct dentry *new;
1412 new = d_alloc(dir, &qname);
1413 if (new) {
1414 child = instantiate(dir->d_inode, new, task, ptr);
1415 if (child)
1416 dput(new);
1417 else
1418 child = new;
1419 }
1420 }
1421 if (!child || IS_ERR(child) || !child->d_inode)
1422 goto end_instantiate;
1423 inode = child->d_inode;
1424 if (inode) {
1425 ino = inode->i_ino;
1426 type = inode->i_mode >> 12;
1427 }
1428 dput(child);
1429end_instantiate:
1430 if (!ino)
1431 ino = find_inode_number(dir, &qname);
1432 if (!ino)
1433 ino = 1;
1434 return filldir(dirent, name, len, filp->f_pos, ino, type);
1435}
1436
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001437static unsigned name_to_int(struct dentry *dentry)
1438{
1439 const char *name = dentry->d_name.name;
1440 int len = dentry->d_name.len;
1441 unsigned n = 0;
1442
1443 if (len > 1 && *name == '0')
1444 goto out;
1445 while (len-- > 0) {
1446 unsigned c = *name++ - '0';
1447 if (c > 9)
1448 goto out;
1449 if (n >= (~0U-9)/10)
1450 goto out;
1451 n *= 10;
1452 n += c;
1453 }
1454 return n;
1455out:
1456 return ~0U;
1457}
1458
Miklos Szeredi27932742007-05-08 00:26:17 -07001459#define PROC_FDINFO_MAX 64
1460
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001461static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001462{
1463 struct task_struct *task = get_proc_task(inode);
1464 struct files_struct *files = NULL;
1465 struct file *file;
1466 int fd = proc_fd(inode);
1467
1468 if (task) {
1469 files = get_files_struct(task);
1470 put_task_struct(task);
1471 }
1472 if (files) {
1473 /*
1474 * We are not taking a ref to the file structure, so we must
1475 * hold ->file_lock.
1476 */
1477 spin_lock(&files->file_lock);
1478 file = fcheck_files(files, fd);
1479 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001480 if (path) {
1481 *path = file->f_path;
1482 path_get(&file->f_path);
1483 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001484 if (info)
1485 snprintf(info, PROC_FDINFO_MAX,
1486 "pos:\t%lli\n"
1487 "flags:\t0%o\n",
1488 (long long) file->f_pos,
1489 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001490 spin_unlock(&files->file_lock);
1491 put_files_struct(files);
1492 return 0;
1493 }
1494 spin_unlock(&files->file_lock);
1495 put_files_struct(files);
1496 }
1497 return -ENOENT;
1498}
1499
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001500static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001501{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001502 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001503}
1504
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001505static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1506{
1507 struct inode *inode = dentry->d_inode;
1508 struct task_struct *task = get_proc_task(inode);
1509 int fd = proc_fd(inode);
1510 struct files_struct *files;
1511
1512 if (task) {
1513 files = get_files_struct(task);
1514 if (files) {
1515 rcu_read_lock();
1516 if (fcheck_files(files, fd)) {
1517 rcu_read_unlock();
1518 put_files_struct(files);
1519 if (task_dumpable(task)) {
1520 inode->i_uid = task->euid;
1521 inode->i_gid = task->egid;
1522 } else {
1523 inode->i_uid = 0;
1524 inode->i_gid = 0;
1525 }
1526 inode->i_mode &= ~(S_ISUID | S_ISGID);
1527 security_task_to_inode(task, inode);
1528 put_task_struct(task);
1529 return 1;
1530 }
1531 rcu_read_unlock();
1532 put_files_struct(files);
1533 }
1534 put_task_struct(task);
1535 }
1536 d_drop(dentry);
1537 return 0;
1538}
1539
1540static struct dentry_operations tid_fd_dentry_operations =
1541{
1542 .d_revalidate = tid_fd_revalidate,
1543 .d_delete = pid_delete_dentry,
1544};
1545
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001546static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001547 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001548{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001549 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001550 struct file *file;
1551 struct files_struct *files;
1552 struct inode *inode;
1553 struct proc_inode *ei;
1554 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001555
Eric W. Biederman61a28782006-10-02 02:18:49 -07001556 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001557 if (!inode)
1558 goto out;
1559 ei = PROC_I(inode);
1560 ei->fd = fd;
1561 files = get_files_struct(task);
1562 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001563 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001564 inode->i_mode = S_IFLNK;
1565
1566 /*
1567 * We are not taking a ref to the file structure, so we must
1568 * hold ->file_lock.
1569 */
1570 spin_lock(&files->file_lock);
1571 file = fcheck_files(files, fd);
1572 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001573 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001574 if (file->f_mode & 1)
1575 inode->i_mode |= S_IRUSR | S_IXUSR;
1576 if (file->f_mode & 2)
1577 inode->i_mode |= S_IWUSR | S_IXUSR;
1578 spin_unlock(&files->file_lock);
1579 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001580
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001581 inode->i_op = &proc_pid_link_inode_operations;
1582 inode->i_size = 64;
1583 ei->op.proc_get_link = proc_fd_link;
1584 dentry->d_op = &tid_fd_dentry_operations;
1585 d_add(dentry, inode);
1586 /* Close the race of the process dying before we return the dentry */
1587 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001588 error = NULL;
1589
1590 out:
1591 return error;
1592out_unlock:
1593 spin_unlock(&files->file_lock);
1594 put_files_struct(files);
1595out_iput:
1596 iput(inode);
1597 goto out;
1598}
1599
Miklos Szeredi27932742007-05-08 00:26:17 -07001600static struct dentry *proc_lookupfd_common(struct inode *dir,
1601 struct dentry *dentry,
1602 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001603{
1604 struct task_struct *task = get_proc_task(dir);
1605 unsigned fd = name_to_int(dentry);
1606 struct dentry *result = ERR_PTR(-ENOENT);
1607
1608 if (!task)
1609 goto out_no_task;
1610 if (fd == ~0U)
1611 goto out;
1612
Miklos Szeredi27932742007-05-08 00:26:17 -07001613 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001614out:
1615 put_task_struct(task);
1616out_no_task:
1617 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001618}
1619
Miklos Szeredi27932742007-05-08 00:26:17 -07001620static int proc_readfd_common(struct file * filp, void * dirent,
1621 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001623 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001624 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001625 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001626 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct files_struct * files;
1629
1630 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001631 if (!p)
1632 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 fd = filp->f_pos;
1636 switch (fd) {
1637 case 0:
1638 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1639 goto out;
1640 filp->f_pos++;
1641 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001642 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1644 goto out;
1645 filp->f_pos++;
1646 default:
1647 files = get_files_struct(p);
1648 if (!files)
1649 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001650 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001652 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001654 char name[PROC_NUMBUF];
1655 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 if (!fcheck_files(files, fd))
1658 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001659 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Miklos Szeredi27932742007-05-08 00:26:17 -07001661 len = snprintf(name, sizeof(name), "%d", fd);
1662 if (proc_fill_cache(filp, dirent, filldir,
1663 name, len, instantiate,
1664 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001665 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001668 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001670 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 put_files_struct(files);
1672 }
1673out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001674 put_task_struct(p);
1675out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return retval;
1677}
1678
Miklos Szeredi27932742007-05-08 00:26:17 -07001679static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1680 struct nameidata *nd)
1681{
1682 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1683}
1684
1685static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1686{
1687 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1688}
1689
1690static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1691 size_t len, loff_t *ppos)
1692{
1693 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001694 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001695 if (!err)
1696 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1697 return err;
1698}
1699
1700static const struct file_operations proc_fdinfo_file_operations = {
1701 .open = nonseekable_open,
1702 .read = proc_fdinfo_read,
1703};
1704
Arjan van de Ven00977a52007-02-12 00:55:34 -08001705static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 .read = generic_read_dir,
1707 .readdir = proc_readfd,
1708};
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001711 * /proc/pid/fd needs a special permission handler so that a process can still
1712 * access /proc/self/fd after it has executed a setuid().
1713 */
1714static int proc_fd_permission(struct inode *inode, int mask,
1715 struct nameidata *nd)
1716{
1717 int rv;
1718
1719 rv = generic_permission(inode, mask, NULL);
1720 if (rv == 0)
1721 return 0;
1722 if (task_pid(current) == proc_pid(inode))
1723 rv = 0;
1724 return rv;
1725}
1726
1727/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 * proc directories can do almost nothing..
1729 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001730static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001732 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001733 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734};
1735
Miklos Szeredi27932742007-05-08 00:26:17 -07001736static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1737 struct dentry *dentry, struct task_struct *task, const void *ptr)
1738{
1739 unsigned fd = *(unsigned *)ptr;
1740 struct inode *inode;
1741 struct proc_inode *ei;
1742 struct dentry *error = ERR_PTR(-ENOENT);
1743
1744 inode = proc_pid_make_inode(dir->i_sb, task);
1745 if (!inode)
1746 goto out;
1747 ei = PROC_I(inode);
1748 ei->fd = fd;
1749 inode->i_mode = S_IFREG | S_IRUSR;
1750 inode->i_fop = &proc_fdinfo_file_operations;
1751 dentry->d_op = &tid_fd_dentry_operations;
1752 d_add(dentry, inode);
1753 /* Close the race of the process dying before we return the dentry */
1754 if (tid_fd_revalidate(dentry, NULL))
1755 error = NULL;
1756
1757 out:
1758 return error;
1759}
1760
1761static struct dentry *proc_lookupfdinfo(struct inode *dir,
1762 struct dentry *dentry,
1763 struct nameidata *nd)
1764{
1765 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1766}
1767
1768static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1769{
1770 return proc_readfd_common(filp, dirent, filldir,
1771 proc_fdinfo_instantiate);
1772}
1773
1774static const struct file_operations proc_fdinfo_operations = {
1775 .read = generic_read_dir,
1776 .readdir = proc_readfdinfo,
1777};
1778
1779/*
1780 * proc directories can do almost nothing..
1781 */
1782static const struct inode_operations proc_fdinfo_inode_operations = {
1783 .lookup = proc_lookupfdinfo,
1784 .setattr = proc_setattr,
1785};
1786
1787
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001788static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001789 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001790{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001791 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001792 struct inode *inode;
1793 struct proc_inode *ei;
1794 struct dentry *error = ERR_PTR(-EINVAL);
1795
Eric W. Biederman61a28782006-10-02 02:18:49 -07001796 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001797 if (!inode)
1798 goto out;
1799
1800 ei = PROC_I(inode);
1801 inode->i_mode = p->mode;
1802 if (S_ISDIR(inode->i_mode))
1803 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1804 if (p->iop)
1805 inode->i_op = p->iop;
1806 if (p->fop)
1807 inode->i_fop = p->fop;
1808 ei->op = p->op;
1809 dentry->d_op = &pid_dentry_operations;
1810 d_add(dentry, inode);
1811 /* Close the race of the process dying before we return the dentry */
1812 if (pid_revalidate(dentry, NULL))
1813 error = NULL;
1814out:
1815 return error;
1816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818static struct dentry *proc_pident_lookup(struct inode *dir,
1819 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001820 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001821 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001824 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001825 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001826 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001828 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 inode = NULL;
1830
Eric W. Biederman99f89552006-06-26 00:25:55 -07001831 if (!task)
1832 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001834 /*
1835 * Yes, it does not scale. And it should not. Don't add
1836 * new entries into /proc/<tgid>/ without very good reasons.
1837 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001838 last = &ents[nents - 1];
1839 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (p->len != dentry->d_name.len)
1841 continue;
1842 if (!memcmp(dentry->d_name.name, p->name, p->len))
1843 break;
1844 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001845 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto out;
1847
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001848 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001850 put_task_struct(task);
1851out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001852 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Eric Dumazetc5141e62007-05-08 00:26:15 -07001855static int proc_pident_fill_cache(struct file *filp, void *dirent,
1856 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001857{
1858 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1859 proc_pident_instantiate, task, p);
1860}
1861
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001862static int proc_pident_readdir(struct file *filp,
1863 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001864 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001865{
1866 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001867 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001868 struct inode *inode = dentry->d_inode;
1869 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001870 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001871 ino_t ino;
1872 int ret;
1873
1874 ret = -ENOENT;
1875 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001876 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001877
1878 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001879 i = filp->f_pos;
1880 switch (i) {
1881 case 0:
1882 ino = inode->i_ino;
1883 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1884 goto out;
1885 i++;
1886 filp->f_pos++;
1887 /* fall through */
1888 case 1:
1889 ino = parent_ino(dentry);
1890 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1891 goto out;
1892 i++;
1893 filp->f_pos++;
1894 /* fall through */
1895 default:
1896 i -= 2;
1897 if (i >= nents) {
1898 ret = 1;
1899 goto out;
1900 }
1901 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001902 last = &ents[nents - 1];
1903 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001904 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001905 goto out;
1906 filp->f_pos++;
1907 p++;
1908 }
1909 }
1910
1911 ret = 1;
1912out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001913 put_task_struct(task);
1914out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001915 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001919static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1920 size_t count, loff_t *ppos)
1921{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001922 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001923 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001924 ssize_t length;
1925 struct task_struct *task = get_proc_task(inode);
1926
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001927 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001928 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001929
1930 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001931 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001932 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001933 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001934 if (length > 0)
1935 length = simple_read_from_buffer(buf, count, ppos, p, length);
1936 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001937 return length;
1938}
1939
1940static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1941 size_t count, loff_t *ppos)
1942{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001943 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001944 char *page;
1945 ssize_t length;
1946 struct task_struct *task = get_proc_task(inode);
1947
1948 length = -ESRCH;
1949 if (!task)
1950 goto out_no_task;
1951 if (count > PAGE_SIZE)
1952 count = PAGE_SIZE;
1953
1954 /* No partial writes. */
1955 length = -EINVAL;
1956 if (*ppos != 0)
1957 goto out;
1958
1959 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001960 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001961 if (!page)
1962 goto out;
1963
1964 length = -EFAULT;
1965 if (copy_from_user(page, buf, count))
1966 goto out_free;
1967
1968 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001969 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001970 (void*)page, count);
1971out_free:
1972 free_page((unsigned long) page);
1973out:
1974 put_task_struct(task);
1975out_no_task:
1976 return length;
1977}
1978
Arjan van de Ven00977a52007-02-12 00:55:34 -08001979static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001980 .read = proc_pid_attr_read,
1981 .write = proc_pid_attr_write,
1982};
1983
Eric Dumazetc5141e62007-05-08 00:26:15 -07001984static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001985 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1986 REG("prev", S_IRUGO, pid_attr),
1987 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1988 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1989 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1990 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001991};
1992
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001993static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 void * dirent, filldir_t filldir)
1995{
1996 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001997 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Arjan van de Ven00977a52007-02-12 00:55:34 -08002000static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002002 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003};
2004
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002005static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct dentry *dentry, struct nameidata *nd)
2007{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002008 return proc_pident_lookup(dir, dentry,
2009 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002012static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002013 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002014 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002015 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016};
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018#endif
2019
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002020#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2021static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2022 size_t count, loff_t *ppos)
2023{
2024 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2025 struct mm_struct *mm;
2026 char buffer[PROC_NUMBUF];
2027 size_t len;
2028 int ret;
2029
2030 if (!task)
2031 return -ESRCH;
2032
2033 ret = 0;
2034 mm = get_task_mm(task);
2035 if (mm) {
2036 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2037 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2038 MMF_DUMP_FILTER_SHIFT));
2039 mmput(mm);
2040 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2041 }
2042
2043 put_task_struct(task);
2044
2045 return ret;
2046}
2047
2048static ssize_t proc_coredump_filter_write(struct file *file,
2049 const char __user *buf,
2050 size_t count,
2051 loff_t *ppos)
2052{
2053 struct task_struct *task;
2054 struct mm_struct *mm;
2055 char buffer[PROC_NUMBUF], *end;
2056 unsigned int val;
2057 int ret;
2058 int i;
2059 unsigned long mask;
2060
2061 ret = -EFAULT;
2062 memset(buffer, 0, sizeof(buffer));
2063 if (count > sizeof(buffer) - 1)
2064 count = sizeof(buffer) - 1;
2065 if (copy_from_user(buffer, buf, count))
2066 goto out_no_task;
2067
2068 ret = -EINVAL;
2069 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2070 if (*end == '\n')
2071 end++;
2072 if (end - buffer == 0)
2073 goto out_no_task;
2074
2075 ret = -ESRCH;
2076 task = get_proc_task(file->f_dentry->d_inode);
2077 if (!task)
2078 goto out_no_task;
2079
2080 ret = end - buffer;
2081 mm = get_task_mm(task);
2082 if (!mm)
2083 goto out_no_mm;
2084
2085 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2086 if (val & mask)
2087 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2088 else
2089 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2090 }
2091
2092 mmput(mm);
2093 out_no_mm:
2094 put_task_struct(task);
2095 out_no_task:
2096 return ret;
2097}
2098
2099static const struct file_operations proc_coredump_filter_operations = {
2100 .read = proc_coredump_filter_read,
2101 .write = proc_coredump_filter_write,
2102};
2103#endif
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105/*
2106 * /proc/self:
2107 */
2108static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2109 int buflen)
2110{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002111 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002112 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002113 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002114 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002115 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002116 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return vfs_readlink(dentry,buffer,buflen,tmp);
2118}
2119
Al Viro008b1502005-08-20 00:17:39 +01002120static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002122 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002123 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002124 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002125 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002126 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002127 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002128 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002131static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 .readlink = proc_self_readlink,
2133 .follow_link = proc_self_follow_link,
2134};
2135
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002136/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002137 * proc base
2138 *
2139 * These are the directory entries in the root directory of /proc
2140 * that properly belong to the /proc filesystem, as they describe
2141 * describe something that is process related.
2142 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002143static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002144 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002145 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002146};
2147
2148/*
2149 * Exceptional case: normally we are not allowed to unhash a busy
2150 * directory. In this case, however, we can do it - no aliasing problems
2151 * due to the way we treat inodes.
2152 */
2153static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2154{
2155 struct inode *inode = dentry->d_inode;
2156 struct task_struct *task = get_proc_task(inode);
2157 if (task) {
2158 put_task_struct(task);
2159 return 1;
2160 }
2161 d_drop(dentry);
2162 return 0;
2163}
2164
2165static struct dentry_operations proc_base_dentry_operations =
2166{
2167 .d_revalidate = proc_base_revalidate,
2168 .d_delete = pid_delete_dentry,
2169};
2170
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002171static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002172 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002173{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002174 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002175 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002176 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002177 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002178
2179 /* Allocate the inode */
2180 error = ERR_PTR(-ENOMEM);
2181 inode = new_inode(dir->i_sb);
2182 if (!inode)
2183 goto out;
2184
2185 /* Initialize the inode */
2186 ei = PROC_I(inode);
2187 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002188
2189 /*
2190 * grab the reference to the task.
2191 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002192 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002193 if (!ei->pid)
2194 goto out_iput;
2195
2196 inode->i_uid = 0;
2197 inode->i_gid = 0;
2198 inode->i_mode = p->mode;
2199 if (S_ISDIR(inode->i_mode))
2200 inode->i_nlink = 2;
2201 if (S_ISLNK(inode->i_mode))
2202 inode->i_size = 64;
2203 if (p->iop)
2204 inode->i_op = p->iop;
2205 if (p->fop)
2206 inode->i_fop = p->fop;
2207 ei->op = p->op;
2208 dentry->d_op = &proc_base_dentry_operations;
2209 d_add(dentry, inode);
2210 error = NULL;
2211out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002212 return error;
2213out_iput:
2214 iput(inode);
2215 goto out;
2216}
2217
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002218static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2219{
2220 struct dentry *error;
2221 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002222 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002223
2224 error = ERR_PTR(-ENOENT);
2225
2226 if (!task)
2227 goto out_no_task;
2228
2229 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002230 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2231 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002232 if (p->len != dentry->d_name.len)
2233 continue;
2234 if (!memcmp(dentry->d_name.name, p->name, p->len))
2235 break;
2236 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002237 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002238 goto out;
2239
2240 error = proc_base_instantiate(dir, dentry, task, p);
2241
2242out:
2243 put_task_struct(task);
2244out_no_task:
2245 return error;
2246}
2247
Eric Dumazetc5141e62007-05-08 00:26:15 -07002248static int proc_base_fill_cache(struct file *filp, void *dirent,
2249 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002250{
2251 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2252 proc_base_instantiate, task, p);
2253}
2254
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002255#ifdef CONFIG_TASK_IO_ACCOUNTING
2256static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2257{
2258 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002259#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002260 "rchar: %llu\n"
2261 "wchar: %llu\n"
2262 "syscr: %llu\n"
2263 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002264#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002265 "read_bytes: %llu\n"
2266 "write_bytes: %llu\n"
2267 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002268#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002269 (unsigned long long)task->rchar,
2270 (unsigned long long)task->wchar,
2271 (unsigned long long)task->syscr,
2272 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002273#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002274 (unsigned long long)task->ioac.read_bytes,
2275 (unsigned long long)task->ioac.write_bytes,
2276 (unsigned long long)task->ioac.cancelled_write_bytes);
2277}
2278#endif
2279
Eric W. Biederman801199c2006-10-02 02:18:48 -07002280/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002281 * Thread groups
2282 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002283static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002284static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002285
Eric Dumazetc5141e62007-05-08 00:26:15 -07002286static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002287 DIR("task", S_IRUGO|S_IXUGO, task),
2288 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002289 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002290#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002291 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002292#endif
James Pearson315e28c2007-10-16 23:30:17 -07002293 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002294 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002295 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002296 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002297#ifdef CONFIG_SCHED_DEBUG
2298 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2299#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002300 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002301 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002302 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002303 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002304#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002305 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002306#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002307 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002308 LNK("cwd", cwd),
2309 LNK("root", root),
2310 LNK("exe", exe),
2311 REG("mounts", S_IRUGO, mounts),
2312 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002313#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002314 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002315 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002316 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002317#endif
2318#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002319 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002320#endif
2321#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002322 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002323#endif
2324#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002325 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002326#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002327#ifdef CONFIG_LATENCYTOP
2328 REG("latency", S_IRUGO, lstats),
2329#endif
Paul Menage8793d852007-10-18 23:39:39 -07002330#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002331 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002332#endif
Paul Menagea4243162007-10-18 23:39:35 -07002333#ifdef CONFIG_CGROUPS
2334 REG("cgroup", S_IRUGO, cgroup),
2335#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002336 INF("oom_score", S_IRUGO, oom_score),
2337 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002338#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002339 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002340 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002341#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002342#ifdef CONFIG_FAULT_INJECTION
2343 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2344#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002345#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2346 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2347#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002348#ifdef CONFIG_TASK_IO_ACCOUNTING
2349 INF("io", S_IRUGO, pid_io_accounting),
2350#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002351};
2352
2353static int proc_tgid_base_readdir(struct file * filp,
2354 void * dirent, filldir_t filldir)
2355{
2356 return proc_pident_readdir(filp,dirent,filldir,
2357 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2358}
2359
Arjan van de Ven00977a52007-02-12 00:55:34 -08002360static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002361 .read = generic_read_dir,
2362 .readdir = proc_tgid_base_readdir,
2363};
2364
2365static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002366 return proc_pident_lookup(dir, dentry,
2367 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002368}
2369
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002370static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002371 .lookup = proc_tgid_base_lookup,
2372 .getattr = pid_getattr,
2373 .setattr = proc_setattr,
2374};
2375
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002376static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002378 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002379 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002380 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Eric W. Biederman48e64842006-06-26 00:25:48 -07002382 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002383 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2384 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002385 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002386 if (!(current->flags & PF_EXITING))
2387 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002388 d_drop(dentry);
2389 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002392 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002393 goto out;
2394
2395 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002396 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2397 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002398 if (!leader)
2399 goto out;
2400
2401 name.name = "task";
2402 name.len = strlen(name.name);
2403 dir = d_hash_and_lookup(leader, &name);
2404 if (!dir)
2405 goto out_put_leader;
2406
2407 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002408 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002409 dentry = d_hash_and_lookup(dir, &name);
2410 if (dentry) {
2411 shrink_dcache_parent(dentry);
2412 d_drop(dentry);
2413 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002415
2416 dput(dir);
2417out_put_leader:
2418 dput(leader);
2419out:
2420 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Randy Dunlap0895e912007-10-21 21:00:10 -07002423/**
2424 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2425 * @task: task that should be flushed.
2426 *
2427 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002428 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002429 * in. This call is supposed to do all of this job.
2430 *
2431 * Looks in the dcache for
2432 * /proc/@pid
2433 * /proc/@tgid/task/@pid
2434 * if either directory is present flushes it and all of it'ts children
2435 * from the dcache.
2436 *
2437 * It is safe and reasonable to cache /proc entries for a task until
2438 * that task exits. After that they just clog up the dcache with
2439 * useless entries, possibly causing useful dcache entries to be
2440 * flushed instead. This routine is proved to flush those useless
2441 * dcache entries at process exit time.
2442 *
2443 * NOTE: This routine is just an optimization so it does not guarantee
2444 * that no dcache entries will exist at process exit time it
2445 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002446 */
2447
2448void proc_flush_task(struct task_struct *task)
2449{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002450 int i;
2451 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002452 struct upid *upid;
2453
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002454 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002455 if (thread_group_leader(task))
2456 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002457
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002458 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002459 upid = &pid->numbers[i];
2460 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002461 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002462 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002463
2464 upid = &pid->numbers[pid->level];
2465 if (upid->nr == 1)
2466 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002467}
2468
Adrian Bunk9711ef92006-12-06 20:38:31 -08002469static struct dentry *proc_pid_instantiate(struct inode *dir,
2470 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002471 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002472{
2473 struct dentry *error = ERR_PTR(-ENOENT);
2474 struct inode *inode;
2475
Eric W. Biederman61a28782006-10-02 02:18:49 -07002476 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002477 if (!inode)
2478 goto out;
2479
2480 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2481 inode->i_op = &proc_tgid_base_inode_operations;
2482 inode->i_fop = &proc_tgid_base_operations;
2483 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002484 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002485#ifdef CONFIG_SECURITY
2486 inode->i_nlink += 1;
2487#endif
2488
2489 dentry->d_op = &pid_dentry_operations;
2490
2491 d_add(dentry, inode);
2492 /* Close the race of the process dying before we return the dentry */
2493 if (pid_revalidate(dentry, NULL))
2494 error = NULL;
2495out:
2496 return error;
2497}
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2500{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002501 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002504 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Eric W. Biederman801199c2006-10-02 02:18:48 -07002506 result = proc_base_lookup(dir, dentry);
2507 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2508 goto out;
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 tgid = name_to_int(dentry);
2511 if (tgid == ~0U)
2512 goto out;
2513
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002514 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002515 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002516 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (task)
2518 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002519 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (!task)
2521 goto out;
2522
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002523 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002524 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002526 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002530 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002531 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002533struct tgid_iter {
2534 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002535 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002536};
2537static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2538{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002539 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002541 if (iter.task)
2542 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002543 rcu_read_lock();
2544retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002545 iter.task = NULL;
2546 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002547 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002548 iter.tgid = pid_nr_ns(pid, ns);
2549 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002550 /* What we to know is if the pid we have find is the
2551 * pid of a thread_group_leader. Testing for task
2552 * being a thread_group_leader is the obvious thing
2553 * todo but there is a window when it fails, due to
2554 * the pid transfer logic in de_thread.
2555 *
2556 * So we perform the straight forward test of seeing
2557 * if the pid we have found is the pid of a thread
2558 * group leader, and don't worry if the task we have
2559 * found doesn't happen to be a thread group leader.
2560 * As we don't care in the case of readdir.
2561 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002562 if (!iter.task || !has_group_leader_pid(iter.task)) {
2563 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002564 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002565 }
2566 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002568 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002569 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002572#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Eric W. Biederman61a28782006-10-02 02:18:49 -07002574static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002575 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002576{
2577 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002578 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002579 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002580 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002581}
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583/* for the /proc/ directory itself, after non-process stuff has been done */
2584int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002587 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002588 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002589 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Eric W. Biederman61a28782006-10-02 02:18:49 -07002591 if (!reaper)
2592 goto out_no_task;
2593
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002594 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002595 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002596 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002597 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002600 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002601 iter.task = NULL;
2602 iter.tgid = filp->f_pos - TGID_OFFSET;
2603 for (iter = next_tgid(ns, iter);
2604 iter.task;
2605 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2606 filp->f_pos = iter.tgid + TGID_OFFSET;
2607 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2608 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002609 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002612 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2613out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002614 put_task_struct(reaper);
2615out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return 0;
2617}
2618
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002619/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002620 * Tasks
2621 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002622static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002623 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002624 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002625 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002626 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002627 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002628 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002629#ifdef CONFIG_SCHED_DEBUG
2630 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2631#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002632 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002633 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002634 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002635 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002636#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002637 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002638#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002639 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002640 LNK("cwd", cwd),
2641 LNK("root", root),
2642 LNK("exe", exe),
2643 REG("mounts", S_IRUGO, mounts),
Matt Mackall1e883282008-02-04 22:29:07 -08002644#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002645 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002646 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002647 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002648#endif
2649#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002650 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002651#endif
2652#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002653 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002654#endif
2655#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002656 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002658#ifdef CONFIG_LATENCYTOP
2659 REG("latency", S_IRUGO, lstats),
2660#endif
Paul Menage8793d852007-10-18 23:39:39 -07002661#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002662 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002663#endif
Paul Menagea4243162007-10-18 23:39:35 -07002664#ifdef CONFIG_CGROUPS
2665 REG("cgroup", S_IRUGO, cgroup),
2666#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002667 INF("oom_score", S_IRUGO, oom_score),
2668 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002669#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002670 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002671 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002672#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002673#ifdef CONFIG_FAULT_INJECTION
2674 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2675#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002676};
2677
2678static int proc_tid_base_readdir(struct file * filp,
2679 void * dirent, filldir_t filldir)
2680{
2681 return proc_pident_readdir(filp,dirent,filldir,
2682 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2683}
2684
2685static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002686 return proc_pident_lookup(dir, dentry,
2687 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002688}
2689
Arjan van de Ven00977a52007-02-12 00:55:34 -08002690static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002691 .read = generic_read_dir,
2692 .readdir = proc_tid_base_readdir,
2693};
2694
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002695static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002696 .lookup = proc_tid_base_lookup,
2697 .getattr = pid_getattr,
2698 .setattr = proc_setattr,
2699};
2700
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002701static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002702 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002703{
2704 struct dentry *error = ERR_PTR(-ENOENT);
2705 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002706 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002707
2708 if (!inode)
2709 goto out;
2710 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2711 inode->i_op = &proc_tid_base_inode_operations;
2712 inode->i_fop = &proc_tid_base_operations;
2713 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002714 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002715#ifdef CONFIG_SECURITY
2716 inode->i_nlink += 1;
2717#endif
2718
2719 dentry->d_op = &pid_dentry_operations;
2720
2721 d_add(dentry, inode);
2722 /* Close the race of the process dying before we return the dentry */
2723 if (pid_revalidate(dentry, NULL))
2724 error = NULL;
2725out:
2726 return error;
2727}
2728
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002729static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2730{
2731 struct dentry *result = ERR_PTR(-ENOENT);
2732 struct task_struct *task;
2733 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002734 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002735 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002736
2737 if (!leader)
2738 goto out_no_task;
2739
2740 tid = name_to_int(dentry);
2741 if (tid == ~0U)
2742 goto out;
2743
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002744 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002745 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002746 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002747 if (task)
2748 get_task_struct(task);
2749 rcu_read_unlock();
2750 if (!task)
2751 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002752 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002753 goto out_drop_task;
2754
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002755 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002756out_drop_task:
2757 put_task_struct(task);
2758out:
2759 put_task_struct(leader);
2760out_no_task:
2761 return result;
2762}
2763
2764/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002765 * Find the first tid of a thread group to return to user space.
2766 *
2767 * Usually this is just the thread group leader, but if the users
2768 * buffer was too small or there was a seek into the middle of the
2769 * directory we have more work todo.
2770 *
2771 * In the case of a short read we start with find_task_by_pid.
2772 *
2773 * In the case of a seek we start with the leader and walk nr
2774 * threads past it.
2775 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002776static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002777 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002778{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002779 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002780
Eric W. Biedermancc288732006-06-26 00:26:01 -07002781 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002782 /* Attempt to start with the pid of a thread */
2783 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002784 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002785 if (pos && (pos->group_leader == leader))
2786 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002787 }
2788
2789 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002790 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002791 if (nr && nr >= get_nr_threads(leader))
2792 goto out;
2793
2794 /* If we haven't found our starting place yet start
2795 * with the leader and walk nr threads forward.
2796 */
2797 for (pos = leader; nr > 0; --nr) {
2798 pos = next_thread(pos);
2799 if (pos == leader) {
2800 pos = NULL;
2801 goto out;
2802 }
2803 }
2804found:
2805 get_task_struct(pos);
2806out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002807 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002808 return pos;
2809}
2810
2811/*
2812 * Find the next thread in the thread list.
2813 * Return NULL if there is an error or no next thread.
2814 *
2815 * The reference to the input task_struct is released.
2816 */
2817static struct task_struct *next_tid(struct task_struct *start)
2818{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002819 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002820 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002821 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002822 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002823 if (thread_group_leader(pos))
2824 pos = NULL;
2825 else
2826 get_task_struct(pos);
2827 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002828 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002829 put_task_struct(start);
2830 return pos;
2831}
2832
Eric W. Biederman61a28782006-10-02 02:18:49 -07002833static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2834 struct task_struct *task, int tid)
2835{
2836 char name[PROC_NUMBUF];
2837 int len = snprintf(name, sizeof(name), "%d", tid);
2838 return proc_fill_cache(filp, dirent, filldir, name, len,
2839 proc_task_instantiate, task, NULL);
2840}
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842/* for the /proc/TGID/task/ directories */
2843static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2844{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002845 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002847 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002848 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 int retval = -ENOENT;
2850 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002851 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002853 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002855 task = get_proc_task(inode);
2856 if (!task)
2857 goto out_no_task;
2858 rcu_read_lock();
2859 if (pid_alive(task)) {
2860 leader = task->group_leader;
2861 get_task_struct(leader);
2862 }
2863 rcu_read_unlock();
2864 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002865 if (!leader)
2866 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 retval = 0;
2868
2869 switch (pos) {
2870 case 0:
2871 ino = inode->i_ino;
2872 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2873 goto out;
2874 pos++;
2875 /* fall through */
2876 case 1:
2877 ino = parent_ino(dentry);
2878 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2879 goto out;
2880 pos++;
2881 /* fall through */
2882 }
2883
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002884 /* f_version caches the tgid value that the last readdir call couldn't
2885 * return. lseek aka telldir automagically resets f_version to 0.
2886 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002887 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002888 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002889 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002890 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002891 task;
2892 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002893 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002894 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002895 /* returning this tgid failed, save it as the first
2896 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002897 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002898 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902out:
2903 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002904 put_task_struct(leader);
2905out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return retval;
2907}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002908
2909static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2910{
2911 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002912 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002913 generic_fillattr(inode, stat);
2914
Eric W. Biederman99f89552006-06-26 00:25:55 -07002915 if (p) {
2916 rcu_read_lock();
2917 stat->nlink += get_nr_threads(p);
2918 rcu_read_unlock();
2919 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002920 }
2921
2922 return 0;
2923}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002924
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002925static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002926 .lookup = proc_task_lookup,
2927 .getattr = proc_task_getattr,
2928 .setattr = proc_setattr,
2929};
2930
Arjan van de Ven00977a52007-02-12 00:55:34 -08002931static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002932 .read = generic_read_dir,
2933 .readdir = proc_task_readdir,
2934};