blob: 33537487f5abe4dadb7c90068dfb961528bdaaaf [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 -070091
Eric W. Biederman8578cea2006-06-26 00:25:54 -070092/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070093#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070097 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080099 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800100 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700101 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Eric W. Biederman61a28782006-10-02 02:18:49 -0700104#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700105 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700106 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700107 .mode = MODE, \
108 .iop = IOP, \
109 .fop = FOP, \
110 .op = OP, \
111}
112
Eric W. Biederman61a28782006-10-02 02:18:49 -0700113#define DIR(NAME, MODE, OTYPE) \
114 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
116 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700117#define LNK(NAME, OTYPE) \
118 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 &proc_pid_link_inode_operations, NULL, \
120 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700121#define REG(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700123 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700124#define INF(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700126 NULL, &proc_info_file_operations, \
127 { .proc_read = &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
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700156static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
157{
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);
168 *mnt = mntget(fs->pwdmnt);
169 *dentry = dget(fs->pwd);
170 read_unlock(&fs->lock);
171 result = 0;
172 put_fs_struct(fs);
173 }
174 return result;
175}
176
177static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
178{
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);
189 *mnt = mntget(fs->rootmnt);
190 *dentry = dget(fs->root);
191 read_unlock(&fs->lock);
192 result = 0;
193 put_fs_struct(fs);
194 }
195 return result;
196}
197
198#define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
Matthew Wilcox6d8982d2007-12-06 11:04:01 -0500202 (task_is_stopped_or_traced(task)) && \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 security_ptrace(current,task) == 0))
204
Al Viro831830b2008-01-02 14:09:57 +0000205struct mm_struct *mm_for_maps(struct task_struct *task)
206{
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 return NULL;
210 down_read(&mm->mmap_sem);
211 task_lock(task);
212 if (task->mm != mm)
213 goto out;
214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
215 goto out;
216 task_unlock(task);
217 return mm;
218out:
219 task_unlock(task);
220 up_read(&mm->mmap_sem);
221 mmput(mm);
222 return NULL;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int proc_pid_cmdline(struct task_struct *task, char * buffer)
226{
227 int res = 0;
228 unsigned int len;
229 struct mm_struct *mm = get_task_mm(task);
230 if (!mm)
231 goto out;
232 if (!mm->arg_end)
233 goto out_mm; /* Shh! No looking before we're done */
234
235 len = mm->arg_end - mm->arg_start;
236
237 if (len > PAGE_SIZE)
238 len = PAGE_SIZE;
239
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
241
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
246 if (len < res) {
247 res = len;
248 } else {
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
254 }
255 }
256out_mm:
257 mmput(mm);
258out:
259 return res;
260}
261
262static int proc_pid_auxv(struct task_struct *task, char *buffer)
263{
264 int res = 0;
265 struct mm_struct *mm = get_task_mm(task);
266 if (mm) {
267 unsigned int nwords = 0;
268 do
269 nwords += 2;
270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
272 if (res > PAGE_SIZE)
273 res = PAGE_SIZE;
274 memcpy(buffer, mm->saved_auxv, res);
275 mmput(mm);
276 }
277 return res;
278}
279
280
281#ifdef CONFIG_KALLSYMS
282/*
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
285 */
286static int proc_pid_wchan(struct task_struct *task, char *buffer)
287{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700288 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700289 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 wchan = get_wchan(task);
292
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700293 if (lookup_symbol_name(wchan, symname) < 0)
294 return sprintf(buffer, "%lu", wchan);
295 else
296 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298#endif /* CONFIG_KALLSYMS */
299
300#ifdef CONFIG_SCHEDSTATS
301/*
302 * Provides /proc/PID/schedstat
303 */
304static int proc_pid_schedstat(struct task_struct *task, char *buffer)
305{
Balbir Singh172ba842007-07-09 18:52:00 +0200306 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 task->sched_info.cpu_time,
308 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200309 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311#endif
312
Arjan van de Ven97455122008-01-25 21:08:34 +0100313#ifdef CONFIG_LATENCYTOP
314static int lstats_show_proc(struct seq_file *m, void *v)
315{
316 int i;
317 struct task_struct *task = m->private;
318 seq_puts(m, "Latency Top version : v0.1\n");
319
320 for (i = 0; i < 32; i++) {
321 if (task->latency_record[i].backtrace[0]) {
322 int q;
323 seq_printf(m, "%i %li %li ",
324 task->latency_record[i].count,
325 task->latency_record[i].time,
326 task->latency_record[i].max);
327 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
328 char sym[KSYM_NAME_LEN];
329 char *c;
330 if (!task->latency_record[i].backtrace[q])
331 break;
332 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
333 break;
334 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
335 c = strchr(sym, '+');
336 if (c)
337 *c = 0;
338 seq_printf(m, "%s ", sym);
339 }
340 seq_printf(m, "\n");
341 }
342
343 }
344 return 0;
345}
346
347static int lstats_open(struct inode *inode, struct file *file)
348{
349 int ret;
350 struct seq_file *m;
351 struct task_struct *task = get_proc_task(inode);
352
353 ret = single_open(file, lstats_show_proc, NULL);
354 if (!ret) {
355 m = file->private_data;
356 m->private = task;
357 }
358 return ret;
359}
360
361static ssize_t lstats_write(struct file *file, const char __user *buf,
362 size_t count, loff_t *offs)
363{
364 struct seq_file *m;
365 struct task_struct *task;
366
367 m = file->private_data;
368 task = m->private;
369 clear_all_latency_tracing(task);
370
371 return count;
372}
373
374static const struct file_operations proc_lstats_operations = {
375 .open = lstats_open,
376 .read = seq_read,
377 .write = lstats_write,
378 .llseek = seq_lseek,
379 .release = single_release,
380};
381
382#endif
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/* The badness from the OOM killer */
385unsigned long badness(struct task_struct *p, unsigned long uptime);
386static int proc_oom_score(struct task_struct *task, char *buffer)
387{
388 unsigned long points;
389 struct timespec uptime;
390
391 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700392 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700394 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return sprintf(buffer, "%lu\n", points);
396}
397
Neil Hormand85f50d2007-10-18 23:40:37 -0700398struct limit_names {
399 char *name;
400 char *unit;
401};
402
403static const struct limit_names lnames[RLIM_NLIMITS] = {
404 [RLIMIT_CPU] = {"Max cpu time", "ms"},
405 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
406 [RLIMIT_DATA] = {"Max data size", "bytes"},
407 [RLIMIT_STACK] = {"Max stack size", "bytes"},
408 [RLIMIT_CORE] = {"Max core file size", "bytes"},
409 [RLIMIT_RSS] = {"Max resident set", "bytes"},
410 [RLIMIT_NPROC] = {"Max processes", "processes"},
411 [RLIMIT_NOFILE] = {"Max open files", "files"},
412 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
413 [RLIMIT_AS] = {"Max address space", "bytes"},
414 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
415 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
416 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
417 [RLIMIT_NICE] = {"Max nice priority", NULL},
418 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
419};
420
421/* Display limits for a process */
422static int proc_pid_limits(struct task_struct *task, char *buffer)
423{
424 unsigned int i;
425 int count = 0;
426 unsigned long flags;
427 char *bufptr = buffer;
428
429 struct rlimit rlim[RLIM_NLIMITS];
430
431 rcu_read_lock();
432 if (!lock_task_sighand(task,&flags)) {
433 rcu_read_unlock();
434 return 0;
435 }
436 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
437 unlock_task_sighand(task, &flags);
438 rcu_read_unlock();
439
440 /*
441 * print the file header
442 */
443 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
444 "Limit", "Soft Limit", "Hard Limit", "Units");
445
446 for (i = 0; i < RLIM_NLIMITS; i++) {
447 if (rlim[i].rlim_cur == RLIM_INFINITY)
448 count += sprintf(&bufptr[count], "%-25s %-20s ",
449 lnames[i].name, "unlimited");
450 else
451 count += sprintf(&bufptr[count], "%-25s %-20lu ",
452 lnames[i].name, rlim[i].rlim_cur);
453
454 if (rlim[i].rlim_max == RLIM_INFINITY)
455 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
456 else
457 count += sprintf(&bufptr[count], "%-20lu ",
458 rlim[i].rlim_max);
459
460 if (lnames[i].unit)
461 count += sprintf(&bufptr[count], "%-10s\n",
462 lnames[i].unit);
463 else
464 count += sprintf(&bufptr[count], "\n");
465 }
466
467 return count;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/************************************************************************/
471/* Here the fs part begins */
472/************************************************************************/
473
474/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700475static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700477 struct task_struct *task;
478 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700479 /* Allow access to a task's file descriptors if it is us or we
480 * may use ptrace attach to the process and find out that
481 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700482 */
483 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700484 if (task) {
485 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700486 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700487 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700488 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700491static int proc_setattr(struct dentry *dentry, struct iattr *attr)
492{
493 int error;
494 struct inode *inode = dentry->d_inode;
495
496 if (attr->ia_valid & ATTR_MODE)
497 return -EPERM;
498
499 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700500 if (!error)
501 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700502 return error;
503}
504
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800505static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700506 .setattr = proc_setattr,
507};
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500510struct proc_mounts {
511 struct seq_file m;
512 int event;
513};
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static int mounts_open(struct inode *inode, struct file *file)
516{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700517 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700518 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800519 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500520 struct proc_mounts *p;
521 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Eric W. Biederman99f89552006-06-26 00:25:55 -0700523 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700524 rcu_read_lock();
525 nsp = task_nsproxy(task);
526 if (nsp) {
527 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800528 if (ns)
529 get_mnt_ns(ns);
530 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700531 rcu_read_unlock();
532
Eric W. Biederman99f89552006-06-26 00:25:55 -0700533 put_task_struct(task);
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800536 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500537 ret = -ENOMEM;
538 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
539 if (p) {
540 file->private_data = &p->m;
541 ret = seq_open(file, &mounts_op);
542 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800543 p->m.private = ns;
544 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500545 return 0;
546 }
547 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800549 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 return ret;
552}
553
554static int mounts_release(struct inode *inode, struct file *file)
555{
556 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800557 struct mnt_namespace *ns = m->private;
558 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return seq_release(inode, file);
560}
561
Al Viro5addc5d2005-11-07 17:15:49 -0500562static unsigned mounts_poll(struct file *file, poll_table *wait)
563{
564 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800565 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500566 unsigned res = 0;
567
568 poll_wait(file, &ns->poll, wait);
569
570 spin_lock(&vfsmount_lock);
571 if (p->event != ns->event) {
572 p->event = ns->event;
573 res = POLLERR;
574 }
575 spin_unlock(&vfsmount_lock);
576
577 return res;
578}
579
Arjan van de Ven00977a52007-02-12 00:55:34 -0800580static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 .open = mounts_open,
582 .read = seq_read,
583 .llseek = seq_lseek,
584 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500585 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586};
587
Chuck Leverb4629fe2006-03-20 13:44:12 -0500588extern struct seq_operations mountstats_op;
589static int mountstats_open(struct inode *inode, struct file *file)
590{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500591 int ret = seq_open(file, &mountstats_op);
592
593 if (!ret) {
594 struct seq_file *m = file->private_data;
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700595 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800596 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700597 struct task_struct *task = get_proc_task(inode);
598
599 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700600 rcu_read_lock();
601 nsp = task_nsproxy(task);
602 if (nsp) {
603 mnt_ns = nsp->mnt_ns;
604 if (mnt_ns)
605 get_mnt_ns(mnt_ns);
606 }
607 rcu_read_unlock();
608
Eric W. Biederman99f89552006-06-26 00:25:55 -0700609 put_task_struct(task);
610 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500611
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800612 if (mnt_ns)
613 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500614 else {
615 seq_release(inode, file);
616 ret = -EINVAL;
617 }
618 }
619 return ret;
620}
621
Arjan van de Ven00977a52007-02-12 00:55:34 -0800622static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500623 .open = mountstats_open,
624 .read = seq_read,
625 .llseek = seq_lseek,
626 .release = mounts_release,
627};
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
630
631static ssize_t proc_info_read(struct file * file, char __user * buf,
632 size_t count, loff_t *ppos)
633{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800634 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 unsigned long page;
636 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700637 struct task_struct *task = get_proc_task(inode);
638
639 length = -ESRCH;
640 if (!task)
641 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (count > PROC_BLOCK_SIZE)
644 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700645
646 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700647 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 length = PROC_I(inode)->op.proc_read(task, (char*)page);
651
652 if (length >= 0)
653 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
654 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700655out:
656 put_task_struct(task);
657out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return length;
659}
660
Arjan van de Ven00977a52007-02-12 00:55:34 -0800661static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .read = proc_info_read,
663};
664
665static int mem_open(struct inode* inode, struct file* file)
666{
667 file->private_data = (void*)((long)current->self_exec_id);
668 return 0;
669}
670
671static ssize_t mem_read(struct file * file, char __user * buf,
672 size_t count, loff_t *ppos)
673{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800674 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 char *page;
676 unsigned long src = *ppos;
677 int ret = -ESRCH;
678 struct mm_struct *mm;
679
Eric W. Biederman99f89552006-06-26 00:25:55 -0700680 if (!task)
681 goto out_no_task;
682
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700683 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 goto out;
685
686 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700687 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (!page)
689 goto out;
690
691 ret = 0;
692
693 mm = get_task_mm(task);
694 if (!mm)
695 goto out_free;
696
697 ret = -EIO;
698
699 if (file->private_data != (void*)((long)current->self_exec_id))
700 goto out_put;
701
702 ret = 0;
703
704 while (count > 0) {
705 int this_len, retval;
706
707 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
708 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700709 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!ret)
711 ret = -EIO;
712 break;
713 }
714
715 if (copy_to_user(buf, page, retval)) {
716 ret = -EFAULT;
717 break;
718 }
719
720 ret += retval;
721 src += retval;
722 buf += retval;
723 count -= retval;
724 }
725 *ppos = src;
726
727out_put:
728 mmput(mm);
729out_free:
730 free_page((unsigned long) page);
731out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700732 put_task_struct(task);
733out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return ret;
735}
736
737#define mem_write NULL
738
739#ifndef mem_write
740/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800741static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 size_t count, loff_t *ppos)
743{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700744 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800746 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 unsigned long dst = *ppos;
748
Eric W. Biederman99f89552006-06-26 00:25:55 -0700749 copied = -ESRCH;
750 if (!task)
751 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Eric W. Biederman99f89552006-06-26 00:25:55 -0700753 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
754 goto out;
755
756 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700757 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700761 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 while (count > 0) {
763 int this_len, retval;
764
765 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
766 if (copy_from_user(page, buf, this_len)) {
767 copied = -EFAULT;
768 break;
769 }
770 retval = access_process_vm(task, dst, page, this_len, 1);
771 if (!retval) {
772 if (!copied)
773 copied = -EIO;
774 break;
775 }
776 copied += retval;
777 buf += retval;
778 dst += retval;
779 count -= retval;
780 }
781 *ppos = dst;
782 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700783out:
784 put_task_struct(task);
785out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return copied;
787}
788#endif
789
790static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
791{
792 switch (orig) {
793 case 0:
794 file->f_pos = offset;
795 break;
796 case 1:
797 file->f_pos += offset;
798 break;
799 default:
800 return -EINVAL;
801 }
802 force_successful_syscall_return();
803 return file->f_pos;
804}
805
Arjan van de Ven00977a52007-02-12 00:55:34 -0800806static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .llseek = mem_lseek,
808 .read = mem_read,
809 .write = mem_write,
810 .open = mem_open,
811};
812
James Pearson315e28c2007-10-16 23:30:17 -0700813static ssize_t environ_read(struct file *file, char __user *buf,
814 size_t count, loff_t *ppos)
815{
816 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
817 char *page;
818 unsigned long src = *ppos;
819 int ret = -ESRCH;
820 struct mm_struct *mm;
821
822 if (!task)
823 goto out_no_task;
824
825 if (!ptrace_may_attach(task))
826 goto out;
827
828 ret = -ENOMEM;
829 page = (char *)__get_free_page(GFP_TEMPORARY);
830 if (!page)
831 goto out;
832
833 ret = 0;
834
835 mm = get_task_mm(task);
836 if (!mm)
837 goto out_free;
838
839 while (count > 0) {
840 int this_len, retval, max_len;
841
842 this_len = mm->env_end - (mm->env_start + src);
843
844 if (this_len <= 0)
845 break;
846
847 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
848 this_len = (this_len > max_len) ? max_len : this_len;
849
850 retval = access_process_vm(task, (mm->env_start + src),
851 page, this_len, 0);
852
853 if (retval <= 0) {
854 ret = retval;
855 break;
856 }
857
858 if (copy_to_user(buf, page, retval)) {
859 ret = -EFAULT;
860 break;
861 }
862
863 ret += retval;
864 src += retval;
865 buf += retval;
866 count -= retval;
867 }
868 *ppos = src;
869
870 mmput(mm);
871out_free:
872 free_page((unsigned long) page);
873out:
874 put_task_struct(task);
875out_no_task:
876 return ret;
877}
878
879static const struct file_operations proc_environ_operations = {
880 .read = environ_read,
881};
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static ssize_t oom_adjust_read(struct file *file, char __user *buf,
884 size_t count, loff_t *ppos)
885{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800886 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700887 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700889 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Eric W. Biederman99f89552006-06-26 00:25:55 -0700891 if (!task)
892 return -ESRCH;
893 oom_adjust = task->oomkilladj;
894 put_task_struct(task);
895
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700896 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700897
898 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
902 size_t count, loff_t *ppos)
903{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700904 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700905 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int oom_adjust;
907
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700908 memset(buffer, 0, sizeof(buffer));
909 if (count > sizeof(buffer) - 1)
910 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (copy_from_user(buffer, buf, count))
912 return -EFAULT;
913 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700914 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
915 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -EINVAL;
917 if (*end == '\n')
918 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800919 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700920 if (!task)
921 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800922 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
923 put_task_struct(task);
924 return -EACCES;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700927 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (end - buffer == 0)
929 return -EIO;
930 return end - buffer;
931}
932
Arjan van de Ven00977a52007-02-12 00:55:34 -0800933static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 .read = oom_adjust_read,
935 .write = oom_adjust_write,
936};
937
David Rientjes4b8df892007-05-08 20:27:01 -0700938#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -0700939static ssize_t clear_refs_write(struct file *file, const char __user *buf,
940 size_t count, loff_t *ppos)
941{
942 struct task_struct *task;
943 char buffer[PROC_NUMBUF], *end;
944 struct mm_struct *mm;
945
946 memset(buffer, 0, sizeof(buffer));
947 if (count > sizeof(buffer) - 1)
948 count = sizeof(buffer) - 1;
949 if (copy_from_user(buffer, buf, count))
950 return -EFAULT;
951 if (!simple_strtol(buffer, &end, 0))
952 return -EINVAL;
953 if (*end == '\n')
954 end++;
955 task = get_proc_task(file->f_path.dentry->d_inode);
956 if (!task)
957 return -ESRCH;
958 mm = get_task_mm(task);
959 if (mm) {
960 clear_refs_smap(mm);
961 mmput(mm);
962 }
963 put_task_struct(task);
964 if (end - buffer == 0)
965 return -EIO;
966 return end - buffer;
967}
968
969static struct file_operations proc_clear_refs_operations = {
970 .write = clear_refs_write,
971};
David Rientjes4b8df892007-05-08 20:27:01 -0700972#endif
David Rientjesb813e932007-05-06 14:49:24 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#ifdef CONFIG_AUDITSYSCALL
975#define TMPBUFLEN 21
976static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
977 size_t count, loff_t *ppos)
978{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800979 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700980 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 ssize_t length;
982 char tmpbuf[TMPBUFLEN];
983
Eric W. Biederman99f89552006-06-26 00:25:55 -0700984 if (!task)
985 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -0500987 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700988 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
990}
991
992static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
993 size_t count, loff_t *ppos)
994{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800995 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 char *page, *tmp;
997 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 uid_t loginuid;
999
1000 if (!capable(CAP_AUDIT_CONTROL))
1001 return -EPERM;
1002
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001003 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return -EPERM;
1005
Al Viroe0182902006-05-18 08:28:02 -04001006 if (count >= PAGE_SIZE)
1007 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (*ppos != 0) {
1010 /* No partial writes. */
1011 return -EINVAL;
1012 }
Mel Gormane12ba742007-10-16 01:25:52 -07001013 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (!page)
1015 return -ENOMEM;
1016 length = -EFAULT;
1017 if (copy_from_user(page, buf, count))
1018 goto out_free_page;
1019
Al Viroe0182902006-05-18 08:28:02 -04001020 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 loginuid = simple_strtoul(page, &tmp, 10);
1022 if (tmp == page) {
1023 length = -EINVAL;
1024 goto out_free_page;
1025
1026 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001027 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (likely(length == 0))
1029 length = count;
1030
1031out_free_page:
1032 free_page((unsigned long) page);
1033 return length;
1034}
1035
Arjan van de Ven00977a52007-02-12 00:55:34 -08001036static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 .read = proc_loginuid_read,
1038 .write = proc_loginuid_write,
1039};
1040#endif
1041
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001042#ifdef CONFIG_FAULT_INJECTION
1043static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1044 size_t count, loff_t *ppos)
1045{
1046 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1047 char buffer[PROC_NUMBUF];
1048 size_t len;
1049 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001050
1051 if (!task)
1052 return -ESRCH;
1053 make_it_fail = task->make_it_fail;
1054 put_task_struct(task);
1055
1056 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001057
1058 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001059}
1060
1061static ssize_t proc_fault_inject_write(struct file * file,
1062 const char __user * buf, size_t count, loff_t *ppos)
1063{
1064 struct task_struct *task;
1065 char buffer[PROC_NUMBUF], *end;
1066 int make_it_fail;
1067
1068 if (!capable(CAP_SYS_RESOURCE))
1069 return -EPERM;
1070 memset(buffer, 0, sizeof(buffer));
1071 if (count > sizeof(buffer) - 1)
1072 count = sizeof(buffer) - 1;
1073 if (copy_from_user(buffer, buf, count))
1074 return -EFAULT;
1075 make_it_fail = simple_strtol(buffer, &end, 0);
1076 if (*end == '\n')
1077 end++;
1078 task = get_proc_task(file->f_dentry->d_inode);
1079 if (!task)
1080 return -ESRCH;
1081 task->make_it_fail = make_it_fail;
1082 put_task_struct(task);
1083 if (end - buffer == 0)
1084 return -EIO;
1085 return end - buffer;
1086}
1087
Arjan van de Ven00977a52007-02-12 00:55:34 -08001088static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001089 .read = proc_fault_inject_read,
1090 .write = proc_fault_inject_write,
1091};
1092#endif
1093
Arjan van de Ven97455122008-01-25 21:08:34 +01001094
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001095#ifdef CONFIG_SCHED_DEBUG
1096/*
1097 * Print out various scheduling related per-task fields:
1098 */
1099static int sched_show(struct seq_file *m, void *v)
1100{
1101 struct inode *inode = m->private;
1102 struct task_struct *p;
1103
1104 WARN_ON(!inode);
1105
1106 p = get_proc_task(inode);
1107 if (!p)
1108 return -ESRCH;
1109 proc_sched_show_task(p, m);
1110
1111 put_task_struct(p);
1112
1113 return 0;
1114}
1115
1116static ssize_t
1117sched_write(struct file *file, const char __user *buf,
1118 size_t count, loff_t *offset)
1119{
1120 struct inode *inode = file->f_path.dentry->d_inode;
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_set_task(p);
1129
1130 put_task_struct(p);
1131
1132 return count;
1133}
1134
1135static int sched_open(struct inode *inode, struct file *filp)
1136{
1137 int ret;
1138
1139 ret = single_open(filp, sched_show, NULL);
1140 if (!ret) {
1141 struct seq_file *m = filp->private_data;
1142
1143 m->private = inode;
1144 }
1145 return ret;
1146}
1147
1148static const struct file_operations proc_pid_sched_operations = {
1149 .open = sched_open,
1150 .read = seq_read,
1151 .write = sched_write,
1152 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001153 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001154};
1155
1156#endif
1157
Al Viro008b1502005-08-20 00:17:39 +01001158static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 struct inode *inode = dentry->d_inode;
1161 int error = -EACCES;
1162
1163 /* We don't need a base pointer in the /proc filesystem */
1164 path_release(nd);
1165
Eric W. Biederman778c1142006-06-26 00:25:58 -07001166 /* Are we allowed to snoop on the tasks file descriptors? */
1167 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1171 nd->last_type = LAST_BIND;
1172out:
Al Viro008b1502005-08-20 00:17:39 +01001173 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1177 char __user *buffer, int buflen)
1178{
1179 struct inode * inode;
Mel Gormane12ba742007-10-16 01:25:52 -07001180 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1181 char *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int len;
1183
1184 if (!tmp)
1185 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 inode = dentry->d_inode;
1188 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1189 len = PTR_ERR(path);
1190 if (IS_ERR(path))
1191 goto out;
1192 len = tmp + PAGE_SIZE - 1 - path;
1193
1194 if (len > buflen)
1195 len = buflen;
1196 if (copy_to_user(buffer, path, len))
1197 len = -EFAULT;
1198 out:
1199 free_page((unsigned long)tmp);
1200 return len;
1201}
1202
1203static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1204{
1205 int error = -EACCES;
1206 struct inode *inode = dentry->d_inode;
1207 struct dentry *de;
1208 struct vfsmount *mnt = NULL;
1209
Eric W. Biederman778c1142006-06-26 00:25:58 -07001210 /* Are we allowed to snoop on the tasks file descriptors? */
1211 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1215 if (error)
1216 goto out;
1217
1218 error = do_proc_readlink(de, mnt, buffer, buflen);
1219 dput(de);
1220 mntput(mnt);
1221out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return error;
1223}
1224
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001225static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001227 .follow_link = proc_pid_follow_link,
1228 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229};
1230
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001231
1232/* building an inode */
1233
1234static int task_dumpable(struct task_struct *task)
1235{
1236 int dumpable = 0;
1237 struct mm_struct *mm;
1238
1239 task_lock(task);
1240 mm = task->mm;
1241 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001242 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001243 task_unlock(task);
1244 if(dumpable == 1)
1245 return 1;
1246 return 0;
1247}
1248
1249
Eric W. Biederman61a28782006-10-02 02:18:49 -07001250static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001251{
1252 struct inode * inode;
1253 struct proc_inode *ei;
1254
1255 /* We need a new inode */
1256
1257 inode = new_inode(sb);
1258 if (!inode)
1259 goto out;
1260
1261 /* Common stuff */
1262 ei = PROC_I(inode);
1263 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001264 inode->i_op = &proc_def_inode_operations;
1265
1266 /*
1267 * grab the reference to task.
1268 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001269 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001270 if (!ei->pid)
1271 goto out_unlock;
1272
1273 inode->i_uid = 0;
1274 inode->i_gid = 0;
1275 if (task_dumpable(task)) {
1276 inode->i_uid = task->euid;
1277 inode->i_gid = task->egid;
1278 }
1279 security_task_to_inode(task, inode);
1280
1281out:
1282 return inode;
1283
1284out_unlock:
1285 iput(inode);
1286 return NULL;
1287}
1288
1289static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1290{
1291 struct inode *inode = dentry->d_inode;
1292 struct task_struct *task;
1293 generic_fillattr(inode, stat);
1294
1295 rcu_read_lock();
1296 stat->uid = 0;
1297 stat->gid = 0;
1298 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1299 if (task) {
1300 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1301 task_dumpable(task)) {
1302 stat->uid = task->euid;
1303 stat->gid = task->egid;
1304 }
1305 }
1306 rcu_read_unlock();
1307 return 0;
1308}
1309
1310/* dentry stuff */
1311
1312/*
1313 * Exceptional case: normally we are not allowed to unhash a busy
1314 * directory. In this case, however, we can do it - no aliasing problems
1315 * due to the way we treat inodes.
1316 *
1317 * Rewrite the inode's ownerships here because the owning task may have
1318 * performed a setuid(), etc.
1319 *
1320 * Before the /proc/pid/status file was created the only way to read
1321 * the effective uid of a /process was to stat /proc/pid. Reading
1322 * /proc/pid/status is slow enough that procps and other packages
1323 * kept stating /proc/pid. To keep the rules in /proc simple I have
1324 * made this apply to all per process world readable and executable
1325 * directories.
1326 */
1327static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1328{
1329 struct inode *inode = dentry->d_inode;
1330 struct task_struct *task = get_proc_task(inode);
1331 if (task) {
1332 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1333 task_dumpable(task)) {
1334 inode->i_uid = task->euid;
1335 inode->i_gid = task->egid;
1336 } else {
1337 inode->i_uid = 0;
1338 inode->i_gid = 0;
1339 }
1340 inode->i_mode &= ~(S_ISUID | S_ISGID);
1341 security_task_to_inode(task, inode);
1342 put_task_struct(task);
1343 return 1;
1344 }
1345 d_drop(dentry);
1346 return 0;
1347}
1348
1349static int pid_delete_dentry(struct dentry * dentry)
1350{
1351 /* Is the task we represent dead?
1352 * If so, then don't put the dentry on the lru list,
1353 * kill it immediately.
1354 */
1355 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1356}
1357
1358static struct dentry_operations pid_dentry_operations =
1359{
1360 .d_revalidate = pid_revalidate,
1361 .d_delete = pid_delete_dentry,
1362};
1363
1364/* Lookups */
1365
Eric Dumazetc5141e62007-05-08 00:26:15 -07001366typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1367 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001368
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001369/*
1370 * Fill a directory entry.
1371 *
1372 * If possible create the dcache entry and derive our inode number and
1373 * file type from dcache entry.
1374 *
1375 * Since all of the proc inode numbers are dynamically generated, the inode
1376 * numbers do not exist until the inode is cache. This means creating the
1377 * the dcache entry in readdir is necessary to keep the inode numbers
1378 * reported by readdir in sync with the inode numbers reported
1379 * by stat.
1380 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001381static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1382 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001383 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001384{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001385 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001386 struct inode *inode;
1387 struct qstr qname;
1388 ino_t ino = 0;
1389 unsigned type = DT_UNKNOWN;
1390
1391 qname.name = name;
1392 qname.len = len;
1393 qname.hash = full_name_hash(name, len);
1394
1395 child = d_lookup(dir, &qname);
1396 if (!child) {
1397 struct dentry *new;
1398 new = d_alloc(dir, &qname);
1399 if (new) {
1400 child = instantiate(dir->d_inode, new, task, ptr);
1401 if (child)
1402 dput(new);
1403 else
1404 child = new;
1405 }
1406 }
1407 if (!child || IS_ERR(child) || !child->d_inode)
1408 goto end_instantiate;
1409 inode = child->d_inode;
1410 if (inode) {
1411 ino = inode->i_ino;
1412 type = inode->i_mode >> 12;
1413 }
1414 dput(child);
1415end_instantiate:
1416 if (!ino)
1417 ino = find_inode_number(dir, &qname);
1418 if (!ino)
1419 ino = 1;
1420 return filldir(dirent, name, len, filp->f_pos, ino, type);
1421}
1422
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001423static unsigned name_to_int(struct dentry *dentry)
1424{
1425 const char *name = dentry->d_name.name;
1426 int len = dentry->d_name.len;
1427 unsigned n = 0;
1428
1429 if (len > 1 && *name == '0')
1430 goto out;
1431 while (len-- > 0) {
1432 unsigned c = *name++ - '0';
1433 if (c > 9)
1434 goto out;
1435 if (n >= (~0U-9)/10)
1436 goto out;
1437 n *= 10;
1438 n += c;
1439 }
1440 return n;
1441out:
1442 return ~0U;
1443}
1444
Miklos Szeredi27932742007-05-08 00:26:17 -07001445#define PROC_FDINFO_MAX 64
1446
1447static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1448 struct vfsmount **mnt, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001449{
1450 struct task_struct *task = get_proc_task(inode);
1451 struct files_struct *files = NULL;
1452 struct file *file;
1453 int fd = proc_fd(inode);
1454
1455 if (task) {
1456 files = get_files_struct(task);
1457 put_task_struct(task);
1458 }
1459 if (files) {
1460 /*
1461 * We are not taking a ref to the file structure, so we must
1462 * hold ->file_lock.
1463 */
1464 spin_lock(&files->file_lock);
1465 file = fcheck_files(files, fd);
1466 if (file) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001467 if (mnt)
1468 *mnt = mntget(file->f_path.mnt);
1469 if (dentry)
1470 *dentry = dget(file->f_path.dentry);
1471 if (info)
1472 snprintf(info, PROC_FDINFO_MAX,
1473 "pos:\t%lli\n"
1474 "flags:\t0%o\n",
1475 (long long) file->f_pos,
1476 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001477 spin_unlock(&files->file_lock);
1478 put_files_struct(files);
1479 return 0;
1480 }
1481 spin_unlock(&files->file_lock);
1482 put_files_struct(files);
1483 }
1484 return -ENOENT;
1485}
1486
Miklos Szeredi27932742007-05-08 00:26:17 -07001487static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1488 struct vfsmount **mnt)
1489{
1490 return proc_fd_info(inode, dentry, mnt, NULL);
1491}
1492
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001493static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1494{
1495 struct inode *inode = dentry->d_inode;
1496 struct task_struct *task = get_proc_task(inode);
1497 int fd = proc_fd(inode);
1498 struct files_struct *files;
1499
1500 if (task) {
1501 files = get_files_struct(task);
1502 if (files) {
1503 rcu_read_lock();
1504 if (fcheck_files(files, fd)) {
1505 rcu_read_unlock();
1506 put_files_struct(files);
1507 if (task_dumpable(task)) {
1508 inode->i_uid = task->euid;
1509 inode->i_gid = task->egid;
1510 } else {
1511 inode->i_uid = 0;
1512 inode->i_gid = 0;
1513 }
1514 inode->i_mode &= ~(S_ISUID | S_ISGID);
1515 security_task_to_inode(task, inode);
1516 put_task_struct(task);
1517 return 1;
1518 }
1519 rcu_read_unlock();
1520 put_files_struct(files);
1521 }
1522 put_task_struct(task);
1523 }
1524 d_drop(dentry);
1525 return 0;
1526}
1527
1528static struct dentry_operations tid_fd_dentry_operations =
1529{
1530 .d_revalidate = tid_fd_revalidate,
1531 .d_delete = pid_delete_dentry,
1532};
1533
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001534static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001535 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001536{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001537 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001538 struct file *file;
1539 struct files_struct *files;
1540 struct inode *inode;
1541 struct proc_inode *ei;
1542 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001543
Eric W. Biederman61a28782006-10-02 02:18:49 -07001544 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001545 if (!inode)
1546 goto out;
1547 ei = PROC_I(inode);
1548 ei->fd = fd;
1549 files = get_files_struct(task);
1550 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001551 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001552 inode->i_mode = S_IFLNK;
1553
1554 /*
1555 * We are not taking a ref to the file structure, so we must
1556 * hold ->file_lock.
1557 */
1558 spin_lock(&files->file_lock);
1559 file = fcheck_files(files, fd);
1560 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001561 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001562 if (file->f_mode & 1)
1563 inode->i_mode |= S_IRUSR | S_IXUSR;
1564 if (file->f_mode & 2)
1565 inode->i_mode |= S_IWUSR | S_IXUSR;
1566 spin_unlock(&files->file_lock);
1567 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001568
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001569 inode->i_op = &proc_pid_link_inode_operations;
1570 inode->i_size = 64;
1571 ei->op.proc_get_link = proc_fd_link;
1572 dentry->d_op = &tid_fd_dentry_operations;
1573 d_add(dentry, inode);
1574 /* Close the race of the process dying before we return the dentry */
1575 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001576 error = NULL;
1577
1578 out:
1579 return error;
1580out_unlock:
1581 spin_unlock(&files->file_lock);
1582 put_files_struct(files);
1583out_iput:
1584 iput(inode);
1585 goto out;
1586}
1587
Miklos Szeredi27932742007-05-08 00:26:17 -07001588static struct dentry *proc_lookupfd_common(struct inode *dir,
1589 struct dentry *dentry,
1590 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001591{
1592 struct task_struct *task = get_proc_task(dir);
1593 unsigned fd = name_to_int(dentry);
1594 struct dentry *result = ERR_PTR(-ENOENT);
1595
1596 if (!task)
1597 goto out_no_task;
1598 if (fd == ~0U)
1599 goto out;
1600
Miklos Szeredi27932742007-05-08 00:26:17 -07001601 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001602out:
1603 put_task_struct(task);
1604out_no_task:
1605 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001606}
1607
Miklos Szeredi27932742007-05-08 00:26:17 -07001608static int proc_readfd_common(struct file * filp, void * dirent,
1609 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001611 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001612 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001613 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001614 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001617 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001620 if (!p)
1621 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 fd = filp->f_pos;
1625 switch (fd) {
1626 case 0:
1627 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1628 goto out;
1629 filp->f_pos++;
1630 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001631 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1633 goto out;
1634 filp->f_pos++;
1635 default:
1636 files = get_files_struct(p);
1637 if (!files)
1638 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001639 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001640 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001642 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001644 char name[PROC_NUMBUF];
1645 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 if (!fcheck_files(files, fd))
1648 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001649 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Miklos Szeredi27932742007-05-08 00:26:17 -07001651 len = snprintf(name, sizeof(name), "%d", fd);
1652 if (proc_fill_cache(filp, dirent, filldir,
1653 name, len, instantiate,
1654 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001655 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 break;
1657 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001658 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001660 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 put_files_struct(files);
1662 }
1663out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001664 put_task_struct(p);
1665out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return retval;
1667}
1668
Miklos Szeredi27932742007-05-08 00:26:17 -07001669static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1670 struct nameidata *nd)
1671{
1672 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1673}
1674
1675static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1676{
1677 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1678}
1679
1680static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1681 size_t len, loff_t *ppos)
1682{
1683 char tmp[PROC_FDINFO_MAX];
1684 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1685 if (!err)
1686 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1687 return err;
1688}
1689
1690static const struct file_operations proc_fdinfo_file_operations = {
1691 .open = nonseekable_open,
1692 .read = proc_fdinfo_read,
1693};
1694
Arjan van de Ven00977a52007-02-12 00:55:34 -08001695static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 .read = generic_read_dir,
1697 .readdir = proc_readfd,
1698};
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001701 * /proc/pid/fd needs a special permission handler so that a process can still
1702 * access /proc/self/fd after it has executed a setuid().
1703 */
1704static int proc_fd_permission(struct inode *inode, int mask,
1705 struct nameidata *nd)
1706{
1707 int rv;
1708
1709 rv = generic_permission(inode, mask, NULL);
1710 if (rv == 0)
1711 return 0;
1712 if (task_pid(current) == proc_pid(inode))
1713 rv = 0;
1714 return rv;
1715}
1716
1717/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 * proc directories can do almost nothing..
1719 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001720static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001722 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001723 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724};
1725
Miklos Szeredi27932742007-05-08 00:26:17 -07001726static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1727 struct dentry *dentry, struct task_struct *task, const void *ptr)
1728{
1729 unsigned fd = *(unsigned *)ptr;
1730 struct inode *inode;
1731 struct proc_inode *ei;
1732 struct dentry *error = ERR_PTR(-ENOENT);
1733
1734 inode = proc_pid_make_inode(dir->i_sb, task);
1735 if (!inode)
1736 goto out;
1737 ei = PROC_I(inode);
1738 ei->fd = fd;
1739 inode->i_mode = S_IFREG | S_IRUSR;
1740 inode->i_fop = &proc_fdinfo_file_operations;
1741 dentry->d_op = &tid_fd_dentry_operations;
1742 d_add(dentry, inode);
1743 /* Close the race of the process dying before we return the dentry */
1744 if (tid_fd_revalidate(dentry, NULL))
1745 error = NULL;
1746
1747 out:
1748 return error;
1749}
1750
1751static struct dentry *proc_lookupfdinfo(struct inode *dir,
1752 struct dentry *dentry,
1753 struct nameidata *nd)
1754{
1755 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1756}
1757
1758static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1759{
1760 return proc_readfd_common(filp, dirent, filldir,
1761 proc_fdinfo_instantiate);
1762}
1763
1764static const struct file_operations proc_fdinfo_operations = {
1765 .read = generic_read_dir,
1766 .readdir = proc_readfdinfo,
1767};
1768
1769/*
1770 * proc directories can do almost nothing..
1771 */
1772static const struct inode_operations proc_fdinfo_inode_operations = {
1773 .lookup = proc_lookupfdinfo,
1774 .setattr = proc_setattr,
1775};
1776
1777
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001778static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001779 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001780{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001781 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001782 struct inode *inode;
1783 struct proc_inode *ei;
1784 struct dentry *error = ERR_PTR(-EINVAL);
1785
Eric W. Biederman61a28782006-10-02 02:18:49 -07001786 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001787 if (!inode)
1788 goto out;
1789
1790 ei = PROC_I(inode);
1791 inode->i_mode = p->mode;
1792 if (S_ISDIR(inode->i_mode))
1793 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1794 if (p->iop)
1795 inode->i_op = p->iop;
1796 if (p->fop)
1797 inode->i_fop = p->fop;
1798 ei->op = p->op;
1799 dentry->d_op = &pid_dentry_operations;
1800 d_add(dentry, inode);
1801 /* Close the race of the process dying before we return the dentry */
1802 if (pid_revalidate(dentry, NULL))
1803 error = NULL;
1804out:
1805 return error;
1806}
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808static struct dentry *proc_pident_lookup(struct inode *dir,
1809 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001810 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001811 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001814 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001815 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001816 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001818 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 inode = NULL;
1820
Eric W. Biederman99f89552006-06-26 00:25:55 -07001821 if (!task)
1822 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001824 /*
1825 * Yes, it does not scale. And it should not. Don't add
1826 * new entries into /proc/<tgid>/ without very good reasons.
1827 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001828 last = &ents[nents - 1];
1829 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (p->len != dentry->d_name.len)
1831 continue;
1832 if (!memcmp(dentry->d_name.name, p->name, p->len))
1833 break;
1834 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001835 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto out;
1837
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001838 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001840 put_task_struct(task);
1841out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001842 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Eric Dumazetc5141e62007-05-08 00:26:15 -07001845static int proc_pident_fill_cache(struct file *filp, void *dirent,
1846 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001847{
1848 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1849 proc_pident_instantiate, task, p);
1850}
1851
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001852static int proc_pident_readdir(struct file *filp,
1853 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001854 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001855{
1856 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001857 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001858 struct inode *inode = dentry->d_inode;
1859 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001860 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001861 ino_t ino;
1862 int ret;
1863
1864 ret = -ENOENT;
1865 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001866 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001867
1868 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001869 i = filp->f_pos;
1870 switch (i) {
1871 case 0:
1872 ino = inode->i_ino;
1873 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1874 goto out;
1875 i++;
1876 filp->f_pos++;
1877 /* fall through */
1878 case 1:
1879 ino = parent_ino(dentry);
1880 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1881 goto out;
1882 i++;
1883 filp->f_pos++;
1884 /* fall through */
1885 default:
1886 i -= 2;
1887 if (i >= nents) {
1888 ret = 1;
1889 goto out;
1890 }
1891 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001892 last = &ents[nents - 1];
1893 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001894 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001895 goto out;
1896 filp->f_pos++;
1897 p++;
1898 }
1899 }
1900
1901 ret = 1;
1902out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001903 put_task_struct(task);
1904out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001905 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001909static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1910 size_t count, loff_t *ppos)
1911{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001912 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001913 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001914 ssize_t length;
1915 struct task_struct *task = get_proc_task(inode);
1916
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001917 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001918 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001919
1920 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001921 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001922 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001923 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001924 if (length > 0)
1925 length = simple_read_from_buffer(buf, count, ppos, p, length);
1926 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001927 return length;
1928}
1929
1930static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1931 size_t count, loff_t *ppos)
1932{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001933 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001934 char *page;
1935 ssize_t length;
1936 struct task_struct *task = get_proc_task(inode);
1937
1938 length = -ESRCH;
1939 if (!task)
1940 goto out_no_task;
1941 if (count > PAGE_SIZE)
1942 count = PAGE_SIZE;
1943
1944 /* No partial writes. */
1945 length = -EINVAL;
1946 if (*ppos != 0)
1947 goto out;
1948
1949 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001950 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001951 if (!page)
1952 goto out;
1953
1954 length = -EFAULT;
1955 if (copy_from_user(page, buf, count))
1956 goto out_free;
1957
1958 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001959 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001960 (void*)page, count);
1961out_free:
1962 free_page((unsigned long) page);
1963out:
1964 put_task_struct(task);
1965out_no_task:
1966 return length;
1967}
1968
Arjan van de Ven00977a52007-02-12 00:55:34 -08001969static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001970 .read = proc_pid_attr_read,
1971 .write = proc_pid_attr_write,
1972};
1973
Eric Dumazetc5141e62007-05-08 00:26:15 -07001974static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001975 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1976 REG("prev", S_IRUGO, pid_attr),
1977 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1978 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1979 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1980 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001981};
1982
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001983static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 void * dirent, filldir_t filldir)
1985{
1986 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001987 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
Arjan van de Ven00977a52007-02-12 00:55:34 -08001990static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001992 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993};
1994
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001995static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 struct dentry *dentry, struct nameidata *nd)
1997{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001998 return proc_pident_lookup(dir, dentry,
1999 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002002static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002003 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002004 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002005 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006};
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008#endif
2009
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002010#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2011static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2012 size_t count, loff_t *ppos)
2013{
2014 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2015 struct mm_struct *mm;
2016 char buffer[PROC_NUMBUF];
2017 size_t len;
2018 int ret;
2019
2020 if (!task)
2021 return -ESRCH;
2022
2023 ret = 0;
2024 mm = get_task_mm(task);
2025 if (mm) {
2026 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2027 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2028 MMF_DUMP_FILTER_SHIFT));
2029 mmput(mm);
2030 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2031 }
2032
2033 put_task_struct(task);
2034
2035 return ret;
2036}
2037
2038static ssize_t proc_coredump_filter_write(struct file *file,
2039 const char __user *buf,
2040 size_t count,
2041 loff_t *ppos)
2042{
2043 struct task_struct *task;
2044 struct mm_struct *mm;
2045 char buffer[PROC_NUMBUF], *end;
2046 unsigned int val;
2047 int ret;
2048 int i;
2049 unsigned long mask;
2050
2051 ret = -EFAULT;
2052 memset(buffer, 0, sizeof(buffer));
2053 if (count > sizeof(buffer) - 1)
2054 count = sizeof(buffer) - 1;
2055 if (copy_from_user(buffer, buf, count))
2056 goto out_no_task;
2057
2058 ret = -EINVAL;
2059 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2060 if (*end == '\n')
2061 end++;
2062 if (end - buffer == 0)
2063 goto out_no_task;
2064
2065 ret = -ESRCH;
2066 task = get_proc_task(file->f_dentry->d_inode);
2067 if (!task)
2068 goto out_no_task;
2069
2070 ret = end - buffer;
2071 mm = get_task_mm(task);
2072 if (!mm)
2073 goto out_no_mm;
2074
2075 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2076 if (val & mask)
2077 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2078 else
2079 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2080 }
2081
2082 mmput(mm);
2083 out_no_mm:
2084 put_task_struct(task);
2085 out_no_task:
2086 return ret;
2087}
2088
2089static const struct file_operations proc_coredump_filter_operations = {
2090 .read = proc_coredump_filter_read,
2091 .write = proc_coredump_filter_write,
2092};
2093#endif
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095/*
2096 * /proc/self:
2097 */
2098static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2099 int buflen)
2100{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002101 char tmp[PROC_NUMBUF];
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002102 sprintf(tmp, "%d", task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return vfs_readlink(dentry,buffer,buflen,tmp);
2104}
2105
Al Viro008b1502005-08-20 00:17:39 +01002106static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002108 char tmp[PROC_NUMBUF];
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002109 sprintf(tmp, "%d", task_tgid_vnr(current));
Al Viro008b1502005-08-20 00:17:39 +01002110 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002113static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 .readlink = proc_self_readlink,
2115 .follow_link = proc_self_follow_link,
2116};
2117
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002118/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002119 * proc base
2120 *
2121 * These are the directory entries in the root directory of /proc
2122 * that properly belong to the /proc filesystem, as they describe
2123 * describe something that is process related.
2124 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002125static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002126 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002127 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002128};
2129
2130/*
2131 * Exceptional case: normally we are not allowed to unhash a busy
2132 * directory. In this case, however, we can do it - no aliasing problems
2133 * due to the way we treat inodes.
2134 */
2135static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2136{
2137 struct inode *inode = dentry->d_inode;
2138 struct task_struct *task = get_proc_task(inode);
2139 if (task) {
2140 put_task_struct(task);
2141 return 1;
2142 }
2143 d_drop(dentry);
2144 return 0;
2145}
2146
2147static struct dentry_operations proc_base_dentry_operations =
2148{
2149 .d_revalidate = proc_base_revalidate,
2150 .d_delete = pid_delete_dentry,
2151};
2152
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002153static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002154 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002155{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002156 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002157 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002158 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002159 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002160
2161 /* Allocate the inode */
2162 error = ERR_PTR(-ENOMEM);
2163 inode = new_inode(dir->i_sb);
2164 if (!inode)
2165 goto out;
2166
2167 /* Initialize the inode */
2168 ei = PROC_I(inode);
2169 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002170
2171 /*
2172 * grab the reference to the task.
2173 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002174 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002175 if (!ei->pid)
2176 goto out_iput;
2177
2178 inode->i_uid = 0;
2179 inode->i_gid = 0;
2180 inode->i_mode = p->mode;
2181 if (S_ISDIR(inode->i_mode))
2182 inode->i_nlink = 2;
2183 if (S_ISLNK(inode->i_mode))
2184 inode->i_size = 64;
2185 if (p->iop)
2186 inode->i_op = p->iop;
2187 if (p->fop)
2188 inode->i_fop = p->fop;
2189 ei->op = p->op;
2190 dentry->d_op = &proc_base_dentry_operations;
2191 d_add(dentry, inode);
2192 error = NULL;
2193out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002194 return error;
2195out_iput:
2196 iput(inode);
2197 goto out;
2198}
2199
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002200static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2201{
2202 struct dentry *error;
2203 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002204 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002205
2206 error = ERR_PTR(-ENOENT);
2207
2208 if (!task)
2209 goto out_no_task;
2210
2211 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002212 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2213 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002214 if (p->len != dentry->d_name.len)
2215 continue;
2216 if (!memcmp(dentry->d_name.name, p->name, p->len))
2217 break;
2218 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002219 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002220 goto out;
2221
2222 error = proc_base_instantiate(dir, dentry, task, p);
2223
2224out:
2225 put_task_struct(task);
2226out_no_task:
2227 return error;
2228}
2229
Eric Dumazetc5141e62007-05-08 00:26:15 -07002230static int proc_base_fill_cache(struct file *filp, void *dirent,
2231 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002232{
2233 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2234 proc_base_instantiate, task, p);
2235}
2236
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002237#ifdef CONFIG_TASK_IO_ACCOUNTING
2238static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2239{
2240 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002241#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002242 "rchar: %llu\n"
2243 "wchar: %llu\n"
2244 "syscr: %llu\n"
2245 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002246#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002247 "read_bytes: %llu\n"
2248 "write_bytes: %llu\n"
2249 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002250#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002251 (unsigned long long)task->rchar,
2252 (unsigned long long)task->wchar,
2253 (unsigned long long)task->syscr,
2254 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002255#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002256 (unsigned long long)task->ioac.read_bytes,
2257 (unsigned long long)task->ioac.write_bytes,
2258 (unsigned long long)task->ioac.cancelled_write_bytes);
2259}
2260#endif
2261
Eric W. Biederman801199c2006-10-02 02:18:48 -07002262/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002263 * Thread groups
2264 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002265static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002266static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002267
Eric Dumazetc5141e62007-05-08 00:26:15 -07002268static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002269 DIR("task", S_IRUGO|S_IXUGO, task),
2270 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002271 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002272 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002273 INF("auxv", S_IRUSR, pid_auxv),
2274 INF("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002275 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002276#ifdef CONFIG_SCHED_DEBUG
2277 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2278#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002279 INF("cmdline", S_IRUGO, pid_cmdline),
2280 INF("stat", S_IRUGO, tgid_stat),
2281 INF("statm", S_IRUGO, pid_statm),
2282 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002283#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002284 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002285#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002286 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002287 LNK("cwd", cwd),
2288 LNK("root", root),
2289 LNK("exe", exe),
2290 REG("mounts", S_IRUGO, mounts),
2291 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002292#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002293 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002294 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002295#endif
2296#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002297 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002298#endif
2299#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002300 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002301#endif
2302#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002303 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002304#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002305#ifdef CONFIG_LATENCYTOP
2306 REG("latency", S_IRUGO, lstats),
2307#endif
Paul Menage8793d852007-10-18 23:39:39 -07002308#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002309 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002310#endif
Paul Menagea4243162007-10-18 23:39:35 -07002311#ifdef CONFIG_CGROUPS
2312 REG("cgroup", S_IRUGO, cgroup),
2313#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002314 INF("oom_score", S_IRUGO, oom_score),
2315 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002316#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002317 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002318#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002319#ifdef CONFIG_FAULT_INJECTION
2320 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2321#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002322#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2323 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2324#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002325#ifdef CONFIG_TASK_IO_ACCOUNTING
2326 INF("io", S_IRUGO, pid_io_accounting),
2327#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002328};
2329
2330static int proc_tgid_base_readdir(struct file * filp,
2331 void * dirent, filldir_t filldir)
2332{
2333 return proc_pident_readdir(filp,dirent,filldir,
2334 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2335}
2336
Arjan van de Ven00977a52007-02-12 00:55:34 -08002337static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002338 .read = generic_read_dir,
2339 .readdir = proc_tgid_base_readdir,
2340};
2341
2342static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002343 return proc_pident_lookup(dir, dentry,
2344 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002345}
2346
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002347static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002348 .lookup = proc_tgid_base_lookup,
2349 .getattr = pid_getattr,
2350 .setattr = proc_setattr,
2351};
2352
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002353static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002355 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002356 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002357 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Eric W. Biederman48e64842006-06-26 00:25:48 -07002359 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002360 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2361 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002362 if (dentry) {
2363 shrink_dcache_parent(dentry);
2364 d_drop(dentry);
2365 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002368 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002369 goto out;
2370
2371 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002372 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2373 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002374 if (!leader)
2375 goto out;
2376
2377 name.name = "task";
2378 name.len = strlen(name.name);
2379 dir = d_hash_and_lookup(leader, &name);
2380 if (!dir)
2381 goto out_put_leader;
2382
2383 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002384 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002385 dentry = d_hash_and_lookup(dir, &name);
2386 if (dentry) {
2387 shrink_dcache_parent(dentry);
2388 d_drop(dentry);
2389 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002391
2392 dput(dir);
2393out_put_leader:
2394 dput(leader);
2395out:
2396 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Randy Dunlap0895e912007-10-21 21:00:10 -07002399/**
2400 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2401 * @task: task that should be flushed.
2402 *
2403 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002404 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002405 * in. This call is supposed to do all of this job.
2406 *
2407 * Looks in the dcache for
2408 * /proc/@pid
2409 * /proc/@tgid/task/@pid
2410 * if either directory is present flushes it and all of it'ts children
2411 * from the dcache.
2412 *
2413 * It is safe and reasonable to cache /proc entries for a task until
2414 * that task exits. After that they just clog up the dcache with
2415 * useless entries, possibly causing useful dcache entries to be
2416 * flushed instead. This routine is proved to flush those useless
2417 * dcache entries at process exit time.
2418 *
2419 * NOTE: This routine is just an optimization so it does not guarantee
2420 * that no dcache entries will exist at process exit time it
2421 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002422 */
2423
2424void proc_flush_task(struct task_struct *task)
2425{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002426 int i;
2427 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002428 struct upid *upid;
2429
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002430 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002431 if (thread_group_leader(task))
2432 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002433
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002434 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002435 upid = &pid->numbers[i];
2436 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002437 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002438 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002439
2440 upid = &pid->numbers[pid->level];
2441 if (upid->nr == 1)
2442 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002443}
2444
Adrian Bunk9711ef92006-12-06 20:38:31 -08002445static struct dentry *proc_pid_instantiate(struct inode *dir,
2446 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002447 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002448{
2449 struct dentry *error = ERR_PTR(-ENOENT);
2450 struct inode *inode;
2451
Eric W. Biederman61a28782006-10-02 02:18:49 -07002452 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002453 if (!inode)
2454 goto out;
2455
2456 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2457 inode->i_op = &proc_tgid_base_inode_operations;
2458 inode->i_fop = &proc_tgid_base_operations;
2459 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002460 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002461#ifdef CONFIG_SECURITY
2462 inode->i_nlink += 1;
2463#endif
2464
2465 dentry->d_op = &pid_dentry_operations;
2466
2467 d_add(dentry, inode);
2468 /* Close the race of the process dying before we return the dentry */
2469 if (pid_revalidate(dentry, NULL))
2470 error = NULL;
2471out:
2472 return error;
2473}
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2476{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002477 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002480 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Eric W. Biederman801199c2006-10-02 02:18:48 -07002482 result = proc_base_lookup(dir, dentry);
2483 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2484 goto out;
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 tgid = name_to_int(dentry);
2487 if (tgid == ~0U)
2488 goto out;
2489
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002490 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002491 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002492 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (task)
2494 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002495 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (!task)
2497 goto out;
2498
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002499 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002500 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002502 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002506 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002507 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002509struct tgid_iter {
2510 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002511 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002512};
2513static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2514{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002515 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002517 if (iter.task)
2518 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002519 rcu_read_lock();
2520retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002521 iter.task = NULL;
2522 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002523 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002524 iter.tgid = pid_nr_ns(pid, ns);
2525 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002526 /* What we to know is if the pid we have find is the
2527 * pid of a thread_group_leader. Testing for task
2528 * being a thread_group_leader is the obvious thing
2529 * todo but there is a window when it fails, due to
2530 * the pid transfer logic in de_thread.
2531 *
2532 * So we perform the straight forward test of seeing
2533 * if the pid we have found is the pid of a thread
2534 * group leader, and don't worry if the task we have
2535 * found doesn't happen to be a thread group leader.
2536 * As we don't care in the case of readdir.
2537 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002538 if (!iter.task || !has_group_leader_pid(iter.task)) {
2539 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002540 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002541 }
2542 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002544 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002545 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002548#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Eric W. Biederman61a28782006-10-02 02:18:49 -07002550static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002551 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002552{
2553 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002554 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002555 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002556 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002557}
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559/* for the /proc/ directory itself, after non-process stuff has been done */
2560int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2561{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002563 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002564 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002565 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Eric W. Biederman61a28782006-10-02 02:18:49 -07002567 if (!reaper)
2568 goto out_no_task;
2569
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002570 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002571 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002572 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002573 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002576 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002577 iter.task = NULL;
2578 iter.tgid = filp->f_pos - TGID_OFFSET;
2579 for (iter = next_tgid(ns, iter);
2580 iter.task;
2581 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2582 filp->f_pos = iter.tgid + TGID_OFFSET;
2583 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2584 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002585 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002588 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2589out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002590 put_task_struct(reaper);
2591out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 return 0;
2593}
2594
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002595/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002596 * Tasks
2597 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002598static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002599 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002600 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002601 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002602 INF("auxv", S_IRUSR, pid_auxv),
2603 INF("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002604 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002605#ifdef CONFIG_SCHED_DEBUG
2606 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2607#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002608 INF("cmdline", S_IRUGO, pid_cmdline),
2609 INF("stat", S_IRUGO, tid_stat),
2610 INF("statm", S_IRUGO, pid_statm),
2611 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002612#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002613 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002614#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002615 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002616 LNK("cwd", cwd),
2617 LNK("root", root),
2618 LNK("exe", exe),
2619 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002620#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002621 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002622 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002623#endif
2624#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002625 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002626#endif
2627#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002628 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002629#endif
2630#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002631 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002632#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002633#ifdef CONFIG_LATENCYTOP
2634 REG("latency", S_IRUGO, lstats),
2635#endif
Paul Menage8793d852007-10-18 23:39:39 -07002636#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002637 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002638#endif
Paul Menagea4243162007-10-18 23:39:35 -07002639#ifdef CONFIG_CGROUPS
2640 REG("cgroup", S_IRUGO, cgroup),
2641#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002642 INF("oom_score", S_IRUGO, oom_score),
2643 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002644#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002645 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002646#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002647#ifdef CONFIG_FAULT_INJECTION
2648 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2649#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002650};
2651
2652static int proc_tid_base_readdir(struct file * filp,
2653 void * dirent, filldir_t filldir)
2654{
2655 return proc_pident_readdir(filp,dirent,filldir,
2656 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2657}
2658
2659static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002660 return proc_pident_lookup(dir, dentry,
2661 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002662}
2663
Arjan van de Ven00977a52007-02-12 00:55:34 -08002664static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002665 .read = generic_read_dir,
2666 .readdir = proc_tid_base_readdir,
2667};
2668
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002669static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002670 .lookup = proc_tid_base_lookup,
2671 .getattr = pid_getattr,
2672 .setattr = proc_setattr,
2673};
2674
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002675static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002676 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002677{
2678 struct dentry *error = ERR_PTR(-ENOENT);
2679 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002680 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002681
2682 if (!inode)
2683 goto out;
2684 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2685 inode->i_op = &proc_tid_base_inode_operations;
2686 inode->i_fop = &proc_tid_base_operations;
2687 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002688 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002689#ifdef CONFIG_SECURITY
2690 inode->i_nlink += 1;
2691#endif
2692
2693 dentry->d_op = &pid_dentry_operations;
2694
2695 d_add(dentry, inode);
2696 /* Close the race of the process dying before we return the dentry */
2697 if (pid_revalidate(dentry, NULL))
2698 error = NULL;
2699out:
2700 return error;
2701}
2702
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002703static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2704{
2705 struct dentry *result = ERR_PTR(-ENOENT);
2706 struct task_struct *task;
2707 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002708 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002709 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002710
2711 if (!leader)
2712 goto out_no_task;
2713
2714 tid = name_to_int(dentry);
2715 if (tid == ~0U)
2716 goto out;
2717
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002718 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002719 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002720 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002721 if (task)
2722 get_task_struct(task);
2723 rcu_read_unlock();
2724 if (!task)
2725 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002726 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002727 goto out_drop_task;
2728
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002729 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002730out_drop_task:
2731 put_task_struct(task);
2732out:
2733 put_task_struct(leader);
2734out_no_task:
2735 return result;
2736}
2737
2738/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002739 * Find the first tid of a thread group to return to user space.
2740 *
2741 * Usually this is just the thread group leader, but if the users
2742 * buffer was too small or there was a seek into the middle of the
2743 * directory we have more work todo.
2744 *
2745 * In the case of a short read we start with find_task_by_pid.
2746 *
2747 * In the case of a seek we start with the leader and walk nr
2748 * threads past it.
2749 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002750static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002751 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002752{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002753 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002754
Eric W. Biedermancc288732006-06-26 00:26:01 -07002755 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002756 /* Attempt to start with the pid of a thread */
2757 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002758 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002759 if (pos && (pos->group_leader == leader))
2760 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002761 }
2762
2763 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002764 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002765 if (nr && nr >= get_nr_threads(leader))
2766 goto out;
2767
2768 /* If we haven't found our starting place yet start
2769 * with the leader and walk nr threads forward.
2770 */
2771 for (pos = leader; nr > 0; --nr) {
2772 pos = next_thread(pos);
2773 if (pos == leader) {
2774 pos = NULL;
2775 goto out;
2776 }
2777 }
2778found:
2779 get_task_struct(pos);
2780out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002781 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002782 return pos;
2783}
2784
2785/*
2786 * Find the next thread in the thread list.
2787 * Return NULL if there is an error or no next thread.
2788 *
2789 * The reference to the input task_struct is released.
2790 */
2791static struct task_struct *next_tid(struct task_struct *start)
2792{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002793 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002794 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002795 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002796 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002797 if (thread_group_leader(pos))
2798 pos = NULL;
2799 else
2800 get_task_struct(pos);
2801 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002802 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002803 put_task_struct(start);
2804 return pos;
2805}
2806
Eric W. Biederman61a28782006-10-02 02:18:49 -07002807static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2808 struct task_struct *task, int tid)
2809{
2810 char name[PROC_NUMBUF];
2811 int len = snprintf(name, sizeof(name), "%d", tid);
2812 return proc_fill_cache(filp, dirent, filldir, name, len,
2813 proc_task_instantiate, task, NULL);
2814}
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816/* for the /proc/TGID/task/ directories */
2817static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2818{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002819 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002821 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002822 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 int retval = -ENOENT;
2824 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002825 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002827 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002829 task = get_proc_task(inode);
2830 if (!task)
2831 goto out_no_task;
2832 rcu_read_lock();
2833 if (pid_alive(task)) {
2834 leader = task->group_leader;
2835 get_task_struct(leader);
2836 }
2837 rcu_read_unlock();
2838 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002839 if (!leader)
2840 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 retval = 0;
2842
2843 switch (pos) {
2844 case 0:
2845 ino = inode->i_ino;
2846 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2847 goto out;
2848 pos++;
2849 /* fall through */
2850 case 1:
2851 ino = parent_ino(dentry);
2852 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2853 goto out;
2854 pos++;
2855 /* fall through */
2856 }
2857
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002858 /* f_version caches the tgid value that the last readdir call couldn't
2859 * return. lseek aka telldir automagically resets f_version to 0.
2860 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002861 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002862 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002863 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002864 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002865 task;
2866 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002867 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002868 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002869 /* returning this tgid failed, save it as the first
2870 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002871 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002872 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
2876out:
2877 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002878 put_task_struct(leader);
2879out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return retval;
2881}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002882
2883static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2884{
2885 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002886 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002887 generic_fillattr(inode, stat);
2888
Eric W. Biederman99f89552006-06-26 00:25:55 -07002889 if (p) {
2890 rcu_read_lock();
2891 stat->nlink += get_nr_threads(p);
2892 rcu_read_unlock();
2893 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002894 }
2895
2896 return 0;
2897}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002898
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002899static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002900 .lookup = proc_task_lookup,
2901 .getattr = proc_task_getattr,
2902 .setattr = proc_setattr,
2903};
2904
Arjan van de Ven00977a52007-02-12 00:55:34 -08002905static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002906 .read = generic_read_dir,
2907 .readdir = proc_task_readdir,
2908};