blob: 3217774d269f6773d5fa9742ee7094572c41ee24 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070064#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/kallsyms.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070066#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070067#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/mount.h>
69#include <linux/security.h>
70#include <linux/ptrace.h>
Paul Menagea4243162007-10-18 23:39:35 -070071#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/cpuset.h>
73#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050074#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070075#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070076#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070077#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070078#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include "internal.h"
80
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070081/* NOTE:
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
86 *
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
89 */
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070093 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080095 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080096 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070097 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Eric W. Biederman61a28782006-10-02 02:18:49 -0700100#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700101 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700102 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .mode = MODE, \
104 .iop = IOP, \
105 .fop = FOP, \
106 .op = OP, \
107}
108
Eric W. Biederman61a28782006-10-02 02:18:49 -0700109#define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
112 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700113#define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700117#define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800124#define ONE(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
127 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Kees Cook5096add2007-05-08 00:26:04 -0700129int maps_protect;
130EXPORT_SYMBOL(maps_protect);
131
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700132static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700135 task_lock(task);
136 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if(fs)
138 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700139 task_unlock(task);
140 return fs;
141}
142
Eric W. Biederman99f89552006-06-26 00:25:55 -0700143static int get_nr_threads(struct task_struct *tsk)
144{
145 /* Must be called with the rcu_read_lock held */
146 unsigned long flags;
147 int count = 0;
148
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
152 }
153 return count;
154}
155
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800156static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700160 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700161
162 if (task) {
163 fs = get_fs_struct(task);
164 put_task_struct(task);
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (fs) {
167 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800168 *path = fs->pwd;
169 path_get(&fs->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 read_unlock(&fs->lock);
171 result = 0;
172 put_fs_struct(fs);
173 }
174 return result;
175}
176
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800177static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182
183 if (task) {
184 fs = get_fs_struct(task);
185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (fs) {
188 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800189 *path = fs->root;
190 path_get(&fs->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 read_unlock(&fs->lock);
192 result = 0;
193 put_fs_struct(fs);
194 }
195 return result;
196}
197
198#define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
Matthew Wilcox6d8982d2007-12-06 11:04:01 -0500202 (task_is_stopped_or_traced(task)) && \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 security_ptrace(current,task) == 0))
204
Al Viro831830b2008-01-02 14:09:57 +0000205struct mm_struct *mm_for_maps(struct task_struct *task)
206{
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 return NULL;
210 down_read(&mm->mmap_sem);
211 task_lock(task);
212 if (task->mm != mm)
213 goto out;
214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
215 goto out;
216 task_unlock(task);
217 return mm;
218out:
219 task_unlock(task);
220 up_read(&mm->mmap_sem);
221 mmput(mm);
222 return NULL;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int proc_pid_cmdline(struct task_struct *task, char * buffer)
226{
227 int res = 0;
228 unsigned int len;
229 struct mm_struct *mm = get_task_mm(task);
230 if (!mm)
231 goto out;
232 if (!mm->arg_end)
233 goto out_mm; /* Shh! No looking before we're done */
234
235 len = mm->arg_end - mm->arg_start;
236
237 if (len > PAGE_SIZE)
238 len = PAGE_SIZE;
239
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
241
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
246 if (len < res) {
247 res = len;
248 } else {
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
254 }
255 }
256out_mm:
257 mmput(mm);
258out:
259 return res;
260}
261
262static int proc_pid_auxv(struct task_struct *task, char *buffer)
263{
264 int res = 0;
265 struct mm_struct *mm = get_task_mm(task);
266 if (mm) {
267 unsigned int nwords = 0;
268 do
269 nwords += 2;
270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
272 if (res > PAGE_SIZE)
273 res = PAGE_SIZE;
274 memcpy(buffer, mm->saved_auxv, res);
275 mmput(mm);
276 }
277 return res;
278}
279
280
281#ifdef CONFIG_KALLSYMS
282/*
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
285 */
286static int proc_pid_wchan(struct task_struct *task, char *buffer)
287{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700288 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700289 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 wchan = get_wchan(task);
292
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700293 if (lookup_symbol_name(wchan, symname) < 0)
294 return sprintf(buffer, "%lu", wchan);
295 else
296 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298#endif /* CONFIG_KALLSYMS */
299
300#ifdef CONFIG_SCHEDSTATS
301/*
302 * Provides /proc/PID/schedstat
303 */
304static int proc_pid_schedstat(struct task_struct *task, char *buffer)
305{
Balbir Singh172ba842007-07-09 18:52:00 +0200306 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 task->sched_info.cpu_time,
308 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200309 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311#endif
312
Arjan van de Ven97455122008-01-25 21:08:34 +0100313#ifdef CONFIG_LATENCYTOP
314static int lstats_show_proc(struct seq_file *m, void *v)
315{
316 int i;
317 struct task_struct *task = m->private;
318 seq_puts(m, "Latency Top version : v0.1\n");
319
320 for (i = 0; i < 32; i++) {
321 if (task->latency_record[i].backtrace[0]) {
322 int q;
323 seq_printf(m, "%i %li %li ",
324 task->latency_record[i].count,
325 task->latency_record[i].time,
326 task->latency_record[i].max);
327 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
328 char sym[KSYM_NAME_LEN];
329 char *c;
330 if (!task->latency_record[i].backtrace[q])
331 break;
332 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
333 break;
334 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
335 c = strchr(sym, '+');
336 if (c)
337 *c = 0;
338 seq_printf(m, "%s ", sym);
339 }
340 seq_printf(m, "\n");
341 }
342
343 }
344 return 0;
345}
346
347static int lstats_open(struct inode *inode, struct file *file)
348{
349 int ret;
350 struct seq_file *m;
351 struct task_struct *task = get_proc_task(inode);
352
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},
Eugene Teo88081172008-02-23 15:23:52 -0800419 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700420};
421
422/* Display limits for a process */
423static int proc_pid_limits(struct task_struct *task, char *buffer)
424{
425 unsigned int i;
426 int count = 0;
427 unsigned long flags;
428 char *bufptr = buffer;
429
430 struct rlimit rlim[RLIM_NLIMITS];
431
432 rcu_read_lock();
433 if (!lock_task_sighand(task,&flags)) {
434 rcu_read_unlock();
435 return 0;
436 }
437 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
438 unlock_task_sighand(task, &flags);
439 rcu_read_unlock();
440
441 /*
442 * print the file header
443 */
444 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
445 "Limit", "Soft Limit", "Hard Limit", "Units");
446
447 for (i = 0; i < RLIM_NLIMITS; i++) {
448 if (rlim[i].rlim_cur == RLIM_INFINITY)
449 count += sprintf(&bufptr[count], "%-25s %-20s ",
450 lnames[i].name, "unlimited");
451 else
452 count += sprintf(&bufptr[count], "%-25s %-20lu ",
453 lnames[i].name, rlim[i].rlim_cur);
454
455 if (rlim[i].rlim_max == RLIM_INFINITY)
456 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
457 else
458 count += sprintf(&bufptr[count], "%-20lu ",
459 rlim[i].rlim_max);
460
461 if (lnames[i].unit)
462 count += sprintf(&bufptr[count], "%-10s\n",
463 lnames[i].unit);
464 else
465 count += sprintf(&bufptr[count], "\n");
466 }
467
468 return count;
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/************************************************************************/
472/* Here the fs part begins */
473/************************************************************************/
474
475/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700476static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700478 struct task_struct *task;
479 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700480 /* Allow access to a task's file descriptors if it is us or we
481 * may use ptrace attach to the process and find out that
482 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700483 */
484 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700485 if (task) {
486 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700487 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700488 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700489 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700492static int proc_setattr(struct dentry *dentry, struct iattr *attr)
493{
494 int error;
495 struct inode *inode = dentry->d_inode;
496
497 if (attr->ia_valid & ATTR_MODE)
498 return -EPERM;
499
500 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700501 if (!error)
502 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700503 return error;
504}
505
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800506static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700507 .setattr = proc_setattr,
508};
509
Jan Engelhardt03a44822008-02-08 04:21:19 -0800510extern const struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500511struct proc_mounts {
512 struct seq_file m;
513 int event;
514};
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static int mounts_open(struct inode *inode, struct file *file)
517{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700518 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700519 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800520 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500521 struct proc_mounts *p;
522 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Eric W. Biederman99f89552006-06-26 00:25:55 -0700524 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700525 rcu_read_lock();
526 nsp = task_nsproxy(task);
527 if (nsp) {
528 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800529 if (ns)
530 get_mnt_ns(ns);
531 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700532 rcu_read_unlock();
533
Eric W. Biederman99f89552006-06-26 00:25:55 -0700534 put_task_struct(task);
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800537 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500538 ret = -ENOMEM;
539 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
540 if (p) {
541 file->private_data = &p->m;
542 ret = seq_open(file, &mounts_op);
543 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800544 p->m.private = ns;
545 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500546 return 0;
547 }
548 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800550 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 return ret;
553}
554
555static int mounts_release(struct inode *inode, struct file *file)
556{
557 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800558 struct mnt_namespace *ns = m->private;
559 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return seq_release(inode, file);
561}
562
Al Viro5addc5d2005-11-07 17:15:49 -0500563static unsigned mounts_poll(struct file *file, poll_table *wait)
564{
565 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800566 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500567 unsigned res = 0;
568
569 poll_wait(file, &ns->poll, wait);
570
571 spin_lock(&vfsmount_lock);
572 if (p->event != ns->event) {
573 p->event = ns->event;
574 res = POLLERR;
575 }
576 spin_unlock(&vfsmount_lock);
577
578 return res;
579}
580
Arjan van de Ven00977a52007-02-12 00:55:34 -0800581static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .open = mounts_open,
583 .read = seq_read,
584 .llseek = seq_lseek,
585 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500586 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587};
588
Jan Engelhardt03a44822008-02-08 04:21:19 -0800589extern const struct seq_operations mountstats_op;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500590static int mountstats_open(struct inode *inode, struct file *file)
591{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500592 int ret = seq_open(file, &mountstats_op);
593
594 if (!ret) {
595 struct seq_file *m = file->private_data;
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700596 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800597 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700598 struct task_struct *task = get_proc_task(inode);
599
600 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700601 rcu_read_lock();
602 nsp = task_nsproxy(task);
603 if (nsp) {
604 mnt_ns = nsp->mnt_ns;
605 if (mnt_ns)
606 get_mnt_ns(mnt_ns);
607 }
608 rcu_read_unlock();
609
Eric W. Biederman99f89552006-06-26 00:25:55 -0700610 put_task_struct(task);
611 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500612
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800613 if (mnt_ns)
614 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500615 else {
616 seq_release(inode, file);
617 ret = -EINVAL;
618 }
619 }
620 return ret;
621}
622
Arjan van de Ven00977a52007-02-12 00:55:34 -0800623static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500624 .open = mountstats_open,
625 .read = seq_read,
626 .llseek = seq_lseek,
627 .release = mounts_release,
628};
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
631
632static ssize_t proc_info_read(struct file * file, char __user * buf,
633 size_t count, loff_t *ppos)
634{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800635 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 unsigned long page;
637 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700638 struct task_struct *task = get_proc_task(inode);
639
640 length = -ESRCH;
641 if (!task)
642 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (count > PROC_BLOCK_SIZE)
645 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700646
647 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700648 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700649 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 length = PROC_I(inode)->op.proc_read(task, (char*)page);
652
653 if (length >= 0)
654 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
655 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700656out:
657 put_task_struct(task);
658out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return length;
660}
661
Arjan van de Ven00977a52007-02-12 00:55:34 -0800662static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .read = proc_info_read,
664};
665
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800666static int proc_single_show(struct seq_file *m, void *v)
667{
668 struct inode *inode = m->private;
669 struct pid_namespace *ns;
670 struct pid *pid;
671 struct task_struct *task;
672 int ret;
673
674 ns = inode->i_sb->s_fs_info;
675 pid = proc_pid(inode);
676 task = get_pid_task(pid, PIDTYPE_PID);
677 if (!task)
678 return -ESRCH;
679
680 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
681
682 put_task_struct(task);
683 return ret;
684}
685
686static int proc_single_open(struct inode *inode, struct file *filp)
687{
688 int ret;
689 ret = single_open(filp, proc_single_show, NULL);
690 if (!ret) {
691 struct seq_file *m = filp->private_data;
692
693 m->private = inode;
694 }
695 return ret;
696}
697
698static const struct file_operations proc_single_file_operations = {
699 .open = proc_single_open,
700 .read = seq_read,
701 .llseek = seq_lseek,
702 .release = single_release,
703};
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static int mem_open(struct inode* inode, struct file* file)
706{
707 file->private_data = (void*)((long)current->self_exec_id);
708 return 0;
709}
710
711static ssize_t mem_read(struct file * file, char __user * buf,
712 size_t count, loff_t *ppos)
713{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800714 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 char *page;
716 unsigned long src = *ppos;
717 int ret = -ESRCH;
718 struct mm_struct *mm;
719
Eric W. Biederman99f89552006-06-26 00:25:55 -0700720 if (!task)
721 goto out_no_task;
722
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700723 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 goto out;
725
726 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700727 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!page)
729 goto out;
730
731 ret = 0;
732
733 mm = get_task_mm(task);
734 if (!mm)
735 goto out_free;
736
737 ret = -EIO;
738
739 if (file->private_data != (void*)((long)current->self_exec_id))
740 goto out_put;
741
742 ret = 0;
743
744 while (count > 0) {
745 int this_len, retval;
746
747 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
748 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700749 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!ret)
751 ret = -EIO;
752 break;
753 }
754
755 if (copy_to_user(buf, page, retval)) {
756 ret = -EFAULT;
757 break;
758 }
759
760 ret += retval;
761 src += retval;
762 buf += retval;
763 count -= retval;
764 }
765 *ppos = src;
766
767out_put:
768 mmput(mm);
769out_free:
770 free_page((unsigned long) page);
771out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700772 put_task_struct(task);
773out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return ret;
775}
776
777#define mem_write NULL
778
779#ifndef mem_write
780/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800781static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 size_t count, loff_t *ppos)
783{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700784 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800786 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 unsigned long dst = *ppos;
788
Eric W. Biederman99f89552006-06-26 00:25:55 -0700789 copied = -ESRCH;
790 if (!task)
791 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Eric W. Biederman99f89552006-06-26 00:25:55 -0700793 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
794 goto out;
795
796 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700797 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700801 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 while (count > 0) {
803 int this_len, retval;
804
805 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
806 if (copy_from_user(page, buf, this_len)) {
807 copied = -EFAULT;
808 break;
809 }
810 retval = access_process_vm(task, dst, page, this_len, 1);
811 if (!retval) {
812 if (!copied)
813 copied = -EIO;
814 break;
815 }
816 copied += retval;
817 buf += retval;
818 dst += retval;
819 count -= retval;
820 }
821 *ppos = dst;
822 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700823out:
824 put_task_struct(task);
825out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return copied;
827}
828#endif
829
Matt Mackall85863e42008-02-04 22:29:04 -0800830loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 switch (orig) {
833 case 0:
834 file->f_pos = offset;
835 break;
836 case 1:
837 file->f_pos += offset;
838 break;
839 default:
840 return -EINVAL;
841 }
842 force_successful_syscall_return();
843 return file->f_pos;
844}
845
Arjan van de Ven00977a52007-02-12 00:55:34 -0800846static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 .llseek = mem_lseek,
848 .read = mem_read,
849 .write = mem_write,
850 .open = mem_open,
851};
852
James Pearson315e28c2007-10-16 23:30:17 -0700853static ssize_t environ_read(struct file *file, char __user *buf,
854 size_t count, loff_t *ppos)
855{
856 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
857 char *page;
858 unsigned long src = *ppos;
859 int ret = -ESRCH;
860 struct mm_struct *mm;
861
862 if (!task)
863 goto out_no_task;
864
865 if (!ptrace_may_attach(task))
866 goto out;
867
868 ret = -ENOMEM;
869 page = (char *)__get_free_page(GFP_TEMPORARY);
870 if (!page)
871 goto out;
872
873 ret = 0;
874
875 mm = get_task_mm(task);
876 if (!mm)
877 goto out_free;
878
879 while (count > 0) {
880 int this_len, retval, max_len;
881
882 this_len = mm->env_end - (mm->env_start + src);
883
884 if (this_len <= 0)
885 break;
886
887 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
888 this_len = (this_len > max_len) ? max_len : this_len;
889
890 retval = access_process_vm(task, (mm->env_start + src),
891 page, this_len, 0);
892
893 if (retval <= 0) {
894 ret = retval;
895 break;
896 }
897
898 if (copy_to_user(buf, page, retval)) {
899 ret = -EFAULT;
900 break;
901 }
902
903 ret += retval;
904 src += retval;
905 buf += retval;
906 count -= retval;
907 }
908 *ppos = src;
909
910 mmput(mm);
911out_free:
912 free_page((unsigned long) page);
913out:
914 put_task_struct(task);
915out_no_task:
916 return ret;
917}
918
919static const struct file_operations proc_environ_operations = {
920 .read = environ_read,
921};
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static ssize_t oom_adjust_read(struct file *file, char __user *buf,
924 size_t count, loff_t *ppos)
925{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800926 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700927 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700929 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Eric W. Biederman99f89552006-06-26 00:25:55 -0700931 if (!task)
932 return -ESRCH;
933 oom_adjust = task->oomkilladj;
934 put_task_struct(task);
935
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700936 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700937
938 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
942 size_t count, loff_t *ppos)
943{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700944 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700945 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int oom_adjust;
947
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700948 memset(buffer, 0, sizeof(buffer));
949 if (count > sizeof(buffer) - 1)
950 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (copy_from_user(buffer, buf, count))
952 return -EFAULT;
953 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700954 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
955 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -EINVAL;
957 if (*end == '\n')
958 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800959 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700960 if (!task)
961 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800962 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
963 put_task_struct(task);
964 return -EACCES;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700967 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (end - buffer == 0)
969 return -EIO;
970 return end - buffer;
971}
972
Arjan van de Ven00977a52007-02-12 00:55:34 -0800973static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 .read = oom_adjust_read,
975 .write = oom_adjust_write,
976};
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978#ifdef CONFIG_AUDITSYSCALL
979#define TMPBUFLEN 21
980static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
981 size_t count, loff_t *ppos)
982{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800983 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700984 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 ssize_t length;
986 char tmpbuf[TMPBUFLEN];
987
Eric W. Biederman99f89552006-06-26 00:25:55 -0700988 if (!task)
989 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -0500991 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700992 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
994}
995
996static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
997 size_t count, loff_t *ppos)
998{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800999 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 char *page, *tmp;
1001 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 uid_t loginuid;
1003
1004 if (!capable(CAP_AUDIT_CONTROL))
1005 return -EPERM;
1006
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001007 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -EPERM;
1009
Al Viroe0182902006-05-18 08:28:02 -04001010 if (count >= PAGE_SIZE)
1011 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (*ppos != 0) {
1014 /* No partial writes. */
1015 return -EINVAL;
1016 }
Mel Gormane12ba742007-10-16 01:25:52 -07001017 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!page)
1019 return -ENOMEM;
1020 length = -EFAULT;
1021 if (copy_from_user(page, buf, count))
1022 goto out_free_page;
1023
Al Viroe0182902006-05-18 08:28:02 -04001024 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 loginuid = simple_strtoul(page, &tmp, 10);
1026 if (tmp == page) {
1027 length = -EINVAL;
1028 goto out_free_page;
1029
1030 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001031 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (likely(length == 0))
1033 length = count;
1034
1035out_free_page:
1036 free_page((unsigned long) page);
1037 return length;
1038}
1039
Arjan van de Ven00977a52007-02-12 00:55:34 -08001040static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 .read = proc_loginuid_read,
1042 .write = proc_loginuid_write,
1043};
1044#endif
1045
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001046#ifdef CONFIG_FAULT_INJECTION
1047static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1048 size_t count, loff_t *ppos)
1049{
1050 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1051 char buffer[PROC_NUMBUF];
1052 size_t len;
1053 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001054
1055 if (!task)
1056 return -ESRCH;
1057 make_it_fail = task->make_it_fail;
1058 put_task_struct(task);
1059
1060 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001061
1062 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001063}
1064
1065static ssize_t proc_fault_inject_write(struct file * file,
1066 const char __user * buf, size_t count, loff_t *ppos)
1067{
1068 struct task_struct *task;
1069 char buffer[PROC_NUMBUF], *end;
1070 int make_it_fail;
1071
1072 if (!capable(CAP_SYS_RESOURCE))
1073 return -EPERM;
1074 memset(buffer, 0, sizeof(buffer));
1075 if (count > sizeof(buffer) - 1)
1076 count = sizeof(buffer) - 1;
1077 if (copy_from_user(buffer, buf, count))
1078 return -EFAULT;
1079 make_it_fail = simple_strtol(buffer, &end, 0);
1080 if (*end == '\n')
1081 end++;
1082 task = get_proc_task(file->f_dentry->d_inode);
1083 if (!task)
1084 return -ESRCH;
1085 task->make_it_fail = make_it_fail;
1086 put_task_struct(task);
1087 if (end - buffer == 0)
1088 return -EIO;
1089 return end - buffer;
1090}
1091
Arjan van de Ven00977a52007-02-12 00:55:34 -08001092static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001093 .read = proc_fault_inject_read,
1094 .write = proc_fault_inject_write,
1095};
1096#endif
1097
Arjan van de Ven97455122008-01-25 21:08:34 +01001098
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001099#ifdef CONFIG_SCHED_DEBUG
1100/*
1101 * Print out various scheduling related per-task fields:
1102 */
1103static int sched_show(struct seq_file *m, void *v)
1104{
1105 struct inode *inode = m->private;
1106 struct task_struct *p;
1107
1108 WARN_ON(!inode);
1109
1110 p = get_proc_task(inode);
1111 if (!p)
1112 return -ESRCH;
1113 proc_sched_show_task(p, m);
1114
1115 put_task_struct(p);
1116
1117 return 0;
1118}
1119
1120static ssize_t
1121sched_write(struct file *file, const char __user *buf,
1122 size_t count, loff_t *offset)
1123{
1124 struct inode *inode = file->f_path.dentry->d_inode;
1125 struct task_struct *p;
1126
1127 WARN_ON(!inode);
1128
1129 p = get_proc_task(inode);
1130 if (!p)
1131 return -ESRCH;
1132 proc_sched_set_task(p);
1133
1134 put_task_struct(p);
1135
1136 return count;
1137}
1138
1139static int sched_open(struct inode *inode, struct file *filp)
1140{
1141 int ret;
1142
1143 ret = single_open(filp, sched_show, NULL);
1144 if (!ret) {
1145 struct seq_file *m = filp->private_data;
1146
1147 m->private = inode;
1148 }
1149 return ret;
1150}
1151
1152static const struct file_operations proc_pid_sched_operations = {
1153 .open = sched_open,
1154 .read = seq_read,
1155 .write = sched_write,
1156 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001157 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001158};
1159
1160#endif
1161
Al Viro008b1502005-08-20 00:17:39 +01001162static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct inode *inode = dentry->d_inode;
1165 int error = -EACCES;
1166
1167 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001168 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Eric W. Biederman778c1142006-06-26 00:25:58 -07001170 /* Are we allowed to snoop on the tasks file descriptors? */
1171 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001174 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 nd->last_type = LAST_BIND;
1176out:
Al Viro008b1502005-08-20 00:17:39 +01001177 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001180static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Mel Gormane12ba742007-10-16 01:25:52 -07001182 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001183 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int len;
1185
1186 if (!tmp)
1187 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001188
Jan Blunckcf28b482008-02-14 19:38:44 -08001189 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001190 len = PTR_ERR(pathname);
1191 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001193 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 if (len > buflen)
1196 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001197 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 len = -EFAULT;
1199 out:
1200 free_page((unsigned long)tmp);
1201 return len;
1202}
1203
1204static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1205{
1206 int error = -EACCES;
1207 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001208 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
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
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001214 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (error)
1216 goto out;
1217
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001218 error = do_proc_readlink(&path, buffer, buflen);
1219 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return error;
1222}
1223
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001224static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001226 .follow_link = proc_pid_follow_link,
1227 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228};
1229
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001230
1231/* building an inode */
1232
1233static int task_dumpable(struct task_struct *task)
1234{
1235 int dumpable = 0;
1236 struct mm_struct *mm;
1237
1238 task_lock(task);
1239 mm = task->mm;
1240 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001241 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001242 task_unlock(task);
1243 if(dumpable == 1)
1244 return 1;
1245 return 0;
1246}
1247
1248
Eric W. Biederman61a28782006-10-02 02:18:49 -07001249static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001250{
1251 struct inode * inode;
1252 struct proc_inode *ei;
1253
1254 /* We need a new inode */
1255
1256 inode = new_inode(sb);
1257 if (!inode)
1258 goto out;
1259
1260 /* Common stuff */
1261 ei = PROC_I(inode);
1262 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001263 inode->i_op = &proc_def_inode_operations;
1264
1265 /*
1266 * grab the reference to task.
1267 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001268 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001269 if (!ei->pid)
1270 goto out_unlock;
1271
1272 inode->i_uid = 0;
1273 inode->i_gid = 0;
1274 if (task_dumpable(task)) {
1275 inode->i_uid = task->euid;
1276 inode->i_gid = task->egid;
1277 }
1278 security_task_to_inode(task, inode);
1279
1280out:
1281 return inode;
1282
1283out_unlock:
1284 iput(inode);
1285 return NULL;
1286}
1287
1288static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1289{
1290 struct inode *inode = dentry->d_inode;
1291 struct task_struct *task;
1292 generic_fillattr(inode, stat);
1293
1294 rcu_read_lock();
1295 stat->uid = 0;
1296 stat->gid = 0;
1297 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1298 if (task) {
1299 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1300 task_dumpable(task)) {
1301 stat->uid = task->euid;
1302 stat->gid = task->egid;
1303 }
1304 }
1305 rcu_read_unlock();
1306 return 0;
1307}
1308
1309/* dentry stuff */
1310
1311/*
1312 * Exceptional case: normally we are not allowed to unhash a busy
1313 * directory. In this case, however, we can do it - no aliasing problems
1314 * due to the way we treat inodes.
1315 *
1316 * Rewrite the inode's ownerships here because the owning task may have
1317 * performed a setuid(), etc.
1318 *
1319 * Before the /proc/pid/status file was created the only way to read
1320 * the effective uid of a /process was to stat /proc/pid. Reading
1321 * /proc/pid/status is slow enough that procps and other packages
1322 * kept stating /proc/pid. To keep the rules in /proc simple I have
1323 * made this apply to all per process world readable and executable
1324 * directories.
1325 */
1326static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1327{
1328 struct inode *inode = dentry->d_inode;
1329 struct task_struct *task = get_proc_task(inode);
1330 if (task) {
1331 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1332 task_dumpable(task)) {
1333 inode->i_uid = task->euid;
1334 inode->i_gid = task->egid;
1335 } else {
1336 inode->i_uid = 0;
1337 inode->i_gid = 0;
1338 }
1339 inode->i_mode &= ~(S_ISUID | S_ISGID);
1340 security_task_to_inode(task, inode);
1341 put_task_struct(task);
1342 return 1;
1343 }
1344 d_drop(dentry);
1345 return 0;
1346}
1347
1348static int pid_delete_dentry(struct dentry * dentry)
1349{
1350 /* Is the task we represent dead?
1351 * If so, then don't put the dentry on the lru list,
1352 * kill it immediately.
1353 */
1354 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1355}
1356
1357static struct dentry_operations pid_dentry_operations =
1358{
1359 .d_revalidate = pid_revalidate,
1360 .d_delete = pid_delete_dentry,
1361};
1362
1363/* Lookups */
1364
Eric Dumazetc5141e62007-05-08 00:26:15 -07001365typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1366 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001367
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001368/*
1369 * Fill a directory entry.
1370 *
1371 * If possible create the dcache entry and derive our inode number and
1372 * file type from dcache entry.
1373 *
1374 * Since all of the proc inode numbers are dynamically generated, the inode
1375 * numbers do not exist until the inode is cache. This means creating the
1376 * the dcache entry in readdir is necessary to keep the inode numbers
1377 * reported by readdir in sync with the inode numbers reported
1378 * by stat.
1379 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001380static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1381 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001382 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001383{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001384 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001385 struct inode *inode;
1386 struct qstr qname;
1387 ino_t ino = 0;
1388 unsigned type = DT_UNKNOWN;
1389
1390 qname.name = name;
1391 qname.len = len;
1392 qname.hash = full_name_hash(name, len);
1393
1394 child = d_lookup(dir, &qname);
1395 if (!child) {
1396 struct dentry *new;
1397 new = d_alloc(dir, &qname);
1398 if (new) {
1399 child = instantiate(dir->d_inode, new, task, ptr);
1400 if (child)
1401 dput(new);
1402 else
1403 child = new;
1404 }
1405 }
1406 if (!child || IS_ERR(child) || !child->d_inode)
1407 goto end_instantiate;
1408 inode = child->d_inode;
1409 if (inode) {
1410 ino = inode->i_ino;
1411 type = inode->i_mode >> 12;
1412 }
1413 dput(child);
1414end_instantiate:
1415 if (!ino)
1416 ino = find_inode_number(dir, &qname);
1417 if (!ino)
1418 ino = 1;
1419 return filldir(dirent, name, len, filp->f_pos, ino, type);
1420}
1421
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001422static unsigned name_to_int(struct dentry *dentry)
1423{
1424 const char *name = dentry->d_name.name;
1425 int len = dentry->d_name.len;
1426 unsigned n = 0;
1427
1428 if (len > 1 && *name == '0')
1429 goto out;
1430 while (len-- > 0) {
1431 unsigned c = *name++ - '0';
1432 if (c > 9)
1433 goto out;
1434 if (n >= (~0U-9)/10)
1435 goto out;
1436 n *= 10;
1437 n += c;
1438 }
1439 return n;
1440out:
1441 return ~0U;
1442}
1443
Miklos Szeredi27932742007-05-08 00:26:17 -07001444#define PROC_FDINFO_MAX 64
1445
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001446static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001447{
1448 struct task_struct *task = get_proc_task(inode);
1449 struct files_struct *files = NULL;
1450 struct file *file;
1451 int fd = proc_fd(inode);
1452
1453 if (task) {
1454 files = get_files_struct(task);
1455 put_task_struct(task);
1456 }
1457 if (files) {
1458 /*
1459 * We are not taking a ref to the file structure, so we must
1460 * hold ->file_lock.
1461 */
1462 spin_lock(&files->file_lock);
1463 file = fcheck_files(files, fd);
1464 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001465 if (path) {
1466 *path = file->f_path;
1467 path_get(&file->f_path);
1468 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001469 if (info)
1470 snprintf(info, PROC_FDINFO_MAX,
1471 "pos:\t%lli\n"
1472 "flags:\t0%o\n",
1473 (long long) file->f_pos,
1474 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001475 spin_unlock(&files->file_lock);
1476 put_files_struct(files);
1477 return 0;
1478 }
1479 spin_unlock(&files->file_lock);
1480 put_files_struct(files);
1481 }
1482 return -ENOENT;
1483}
1484
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001485static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001486{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001487 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001488}
1489
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001490static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1491{
1492 struct inode *inode = dentry->d_inode;
1493 struct task_struct *task = get_proc_task(inode);
1494 int fd = proc_fd(inode);
1495 struct files_struct *files;
1496
1497 if (task) {
1498 files = get_files_struct(task);
1499 if (files) {
1500 rcu_read_lock();
1501 if (fcheck_files(files, fd)) {
1502 rcu_read_unlock();
1503 put_files_struct(files);
1504 if (task_dumpable(task)) {
1505 inode->i_uid = task->euid;
1506 inode->i_gid = task->egid;
1507 } else {
1508 inode->i_uid = 0;
1509 inode->i_gid = 0;
1510 }
1511 inode->i_mode &= ~(S_ISUID | S_ISGID);
1512 security_task_to_inode(task, inode);
1513 put_task_struct(task);
1514 return 1;
1515 }
1516 rcu_read_unlock();
1517 put_files_struct(files);
1518 }
1519 put_task_struct(task);
1520 }
1521 d_drop(dentry);
1522 return 0;
1523}
1524
1525static struct dentry_operations tid_fd_dentry_operations =
1526{
1527 .d_revalidate = tid_fd_revalidate,
1528 .d_delete = pid_delete_dentry,
1529};
1530
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001531static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001532 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001533{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001534 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001535 struct file *file;
1536 struct files_struct *files;
1537 struct inode *inode;
1538 struct proc_inode *ei;
1539 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001540
Eric W. Biederman61a28782006-10-02 02:18:49 -07001541 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001542 if (!inode)
1543 goto out;
1544 ei = PROC_I(inode);
1545 ei->fd = fd;
1546 files = get_files_struct(task);
1547 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001548 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001549 inode->i_mode = S_IFLNK;
1550
1551 /*
1552 * We are not taking a ref to the file structure, so we must
1553 * hold ->file_lock.
1554 */
1555 spin_lock(&files->file_lock);
1556 file = fcheck_files(files, fd);
1557 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001558 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001559 if (file->f_mode & 1)
1560 inode->i_mode |= S_IRUSR | S_IXUSR;
1561 if (file->f_mode & 2)
1562 inode->i_mode |= S_IWUSR | S_IXUSR;
1563 spin_unlock(&files->file_lock);
1564 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001565
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001566 inode->i_op = &proc_pid_link_inode_operations;
1567 inode->i_size = 64;
1568 ei->op.proc_get_link = proc_fd_link;
1569 dentry->d_op = &tid_fd_dentry_operations;
1570 d_add(dentry, inode);
1571 /* Close the race of the process dying before we return the dentry */
1572 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001573 error = NULL;
1574
1575 out:
1576 return error;
1577out_unlock:
1578 spin_unlock(&files->file_lock);
1579 put_files_struct(files);
1580out_iput:
1581 iput(inode);
1582 goto out;
1583}
1584
Miklos Szeredi27932742007-05-08 00:26:17 -07001585static struct dentry *proc_lookupfd_common(struct inode *dir,
1586 struct dentry *dentry,
1587 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001588{
1589 struct task_struct *task = get_proc_task(dir);
1590 unsigned fd = name_to_int(dentry);
1591 struct dentry *result = ERR_PTR(-ENOENT);
1592
1593 if (!task)
1594 goto out_no_task;
1595 if (fd == ~0U)
1596 goto out;
1597
Miklos Szeredi27932742007-05-08 00:26:17 -07001598 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001599out:
1600 put_task_struct(task);
1601out_no_task:
1602 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001603}
1604
Miklos Szeredi27932742007-05-08 00:26:17 -07001605static int proc_readfd_common(struct file * filp, void * dirent,
1606 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001608 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001609 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001610 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001611 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001614 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001617 if (!p)
1618 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 fd = filp->f_pos;
1622 switch (fd) {
1623 case 0:
1624 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1625 goto out;
1626 filp->f_pos++;
1627 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001628 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1630 goto out;
1631 filp->f_pos++;
1632 default:
1633 files = get_files_struct(p);
1634 if (!files)
1635 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001636 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001637 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001639 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001641 char name[PROC_NUMBUF];
1642 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 if (!fcheck_files(files, fd))
1645 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001646 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Miklos Szeredi27932742007-05-08 00:26:17 -07001648 len = snprintf(name, sizeof(name), "%d", fd);
1649 if (proc_fill_cache(filp, dirent, filldir,
1650 name, len, instantiate,
1651 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001652 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001655 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001657 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 put_files_struct(files);
1659 }
1660out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001661 put_task_struct(p);
1662out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return retval;
1664}
1665
Miklos Szeredi27932742007-05-08 00:26:17 -07001666static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1667 struct nameidata *nd)
1668{
1669 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1670}
1671
1672static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1673{
1674 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1675}
1676
1677static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1678 size_t len, loff_t *ppos)
1679{
1680 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001681 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001682 if (!err)
1683 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1684 return err;
1685}
1686
1687static const struct file_operations proc_fdinfo_file_operations = {
1688 .open = nonseekable_open,
1689 .read = proc_fdinfo_read,
1690};
1691
Arjan van de Ven00977a52007-02-12 00:55:34 -08001692static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 .read = generic_read_dir,
1694 .readdir = proc_readfd,
1695};
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001698 * /proc/pid/fd needs a special permission handler so that a process can still
1699 * access /proc/self/fd after it has executed a setuid().
1700 */
1701static int proc_fd_permission(struct inode *inode, int mask,
1702 struct nameidata *nd)
1703{
1704 int rv;
1705
1706 rv = generic_permission(inode, mask, NULL);
1707 if (rv == 0)
1708 return 0;
1709 if (task_pid(current) == proc_pid(inode))
1710 rv = 0;
1711 return rv;
1712}
1713
1714/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 * proc directories can do almost nothing..
1716 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001717static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001719 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001720 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721};
1722
Miklos Szeredi27932742007-05-08 00:26:17 -07001723static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1724 struct dentry *dentry, struct task_struct *task, const void *ptr)
1725{
1726 unsigned fd = *(unsigned *)ptr;
1727 struct inode *inode;
1728 struct proc_inode *ei;
1729 struct dentry *error = ERR_PTR(-ENOENT);
1730
1731 inode = proc_pid_make_inode(dir->i_sb, task);
1732 if (!inode)
1733 goto out;
1734 ei = PROC_I(inode);
1735 ei->fd = fd;
1736 inode->i_mode = S_IFREG | S_IRUSR;
1737 inode->i_fop = &proc_fdinfo_file_operations;
1738 dentry->d_op = &tid_fd_dentry_operations;
1739 d_add(dentry, inode);
1740 /* Close the race of the process dying before we return the dentry */
1741 if (tid_fd_revalidate(dentry, NULL))
1742 error = NULL;
1743
1744 out:
1745 return error;
1746}
1747
1748static struct dentry *proc_lookupfdinfo(struct inode *dir,
1749 struct dentry *dentry,
1750 struct nameidata *nd)
1751{
1752 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1753}
1754
1755static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1756{
1757 return proc_readfd_common(filp, dirent, filldir,
1758 proc_fdinfo_instantiate);
1759}
1760
1761static const struct file_operations proc_fdinfo_operations = {
1762 .read = generic_read_dir,
1763 .readdir = proc_readfdinfo,
1764};
1765
1766/*
1767 * proc directories can do almost nothing..
1768 */
1769static const struct inode_operations proc_fdinfo_inode_operations = {
1770 .lookup = proc_lookupfdinfo,
1771 .setattr = proc_setattr,
1772};
1773
1774
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001775static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001776 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001777{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001778 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001779 struct inode *inode;
1780 struct proc_inode *ei;
1781 struct dentry *error = ERR_PTR(-EINVAL);
1782
Eric W. Biederman61a28782006-10-02 02:18:49 -07001783 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001784 if (!inode)
1785 goto out;
1786
1787 ei = PROC_I(inode);
1788 inode->i_mode = p->mode;
1789 if (S_ISDIR(inode->i_mode))
1790 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1791 if (p->iop)
1792 inode->i_op = p->iop;
1793 if (p->fop)
1794 inode->i_fop = p->fop;
1795 ei->op = p->op;
1796 dentry->d_op = &pid_dentry_operations;
1797 d_add(dentry, inode);
1798 /* Close the race of the process dying before we return the dentry */
1799 if (pid_revalidate(dentry, NULL))
1800 error = NULL;
1801out:
1802 return error;
1803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805static struct dentry *proc_pident_lookup(struct inode *dir,
1806 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001807 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001808 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001811 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001812 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001813 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001815 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 inode = NULL;
1817
Eric W. Biederman99f89552006-06-26 00:25:55 -07001818 if (!task)
1819 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001821 /*
1822 * Yes, it does not scale. And it should not. Don't add
1823 * new entries into /proc/<tgid>/ without very good reasons.
1824 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001825 last = &ents[nents - 1];
1826 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (p->len != dentry->d_name.len)
1828 continue;
1829 if (!memcmp(dentry->d_name.name, p->name, p->len))
1830 break;
1831 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001832 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 goto out;
1834
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001835 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001837 put_task_struct(task);
1838out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001839 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Eric Dumazetc5141e62007-05-08 00:26:15 -07001842static int proc_pident_fill_cache(struct file *filp, void *dirent,
1843 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001844{
1845 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1846 proc_pident_instantiate, task, p);
1847}
1848
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001849static int proc_pident_readdir(struct file *filp,
1850 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001851 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001852{
1853 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001854 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001855 struct inode *inode = dentry->d_inode;
1856 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001857 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001858 ino_t ino;
1859 int ret;
1860
1861 ret = -ENOENT;
1862 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001863 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001864
1865 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001866 i = filp->f_pos;
1867 switch (i) {
1868 case 0:
1869 ino = inode->i_ino;
1870 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1871 goto out;
1872 i++;
1873 filp->f_pos++;
1874 /* fall through */
1875 case 1:
1876 ino = parent_ino(dentry);
1877 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1878 goto out;
1879 i++;
1880 filp->f_pos++;
1881 /* fall through */
1882 default:
1883 i -= 2;
1884 if (i >= nents) {
1885 ret = 1;
1886 goto out;
1887 }
1888 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001889 last = &ents[nents - 1];
1890 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001891 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001892 goto out;
1893 filp->f_pos++;
1894 p++;
1895 }
1896 }
1897
1898 ret = 1;
1899out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001900 put_task_struct(task);
1901out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001902 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001906static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1907 size_t count, loff_t *ppos)
1908{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001909 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001910 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001911 ssize_t length;
1912 struct task_struct *task = get_proc_task(inode);
1913
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001914 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001915 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001916
1917 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001918 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001919 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001920 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001921 if (length > 0)
1922 length = simple_read_from_buffer(buf, count, ppos, p, length);
1923 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001924 return length;
1925}
1926
1927static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1928 size_t count, loff_t *ppos)
1929{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001930 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001931 char *page;
1932 ssize_t length;
1933 struct task_struct *task = get_proc_task(inode);
1934
1935 length = -ESRCH;
1936 if (!task)
1937 goto out_no_task;
1938 if (count > PAGE_SIZE)
1939 count = PAGE_SIZE;
1940
1941 /* No partial writes. */
1942 length = -EINVAL;
1943 if (*ppos != 0)
1944 goto out;
1945
1946 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001947 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001948 if (!page)
1949 goto out;
1950
1951 length = -EFAULT;
1952 if (copy_from_user(page, buf, count))
1953 goto out_free;
1954
1955 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001956 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001957 (void*)page, count);
1958out_free:
1959 free_page((unsigned long) page);
1960out:
1961 put_task_struct(task);
1962out_no_task:
1963 return length;
1964}
1965
Arjan van de Ven00977a52007-02-12 00:55:34 -08001966static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001967 .read = proc_pid_attr_read,
1968 .write = proc_pid_attr_write,
1969};
1970
Eric Dumazetc5141e62007-05-08 00:26:15 -07001971static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001972 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1973 REG("prev", S_IRUGO, pid_attr),
1974 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1975 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1976 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1977 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001978};
1979
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001980static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 void * dirent, filldir_t filldir)
1982{
1983 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001984 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Arjan van de Ven00977a52007-02-12 00:55:34 -08001987static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001989 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990};
1991
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001992static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 struct dentry *dentry, struct nameidata *nd)
1994{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001995 return proc_pident_lookup(dir, dentry,
1996 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001999static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002000 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002001 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002002 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003};
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005#endif
2006
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002007#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2008static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2009 size_t count, loff_t *ppos)
2010{
2011 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2012 struct mm_struct *mm;
2013 char buffer[PROC_NUMBUF];
2014 size_t len;
2015 int ret;
2016
2017 if (!task)
2018 return -ESRCH;
2019
2020 ret = 0;
2021 mm = get_task_mm(task);
2022 if (mm) {
2023 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2024 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2025 MMF_DUMP_FILTER_SHIFT));
2026 mmput(mm);
2027 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2028 }
2029
2030 put_task_struct(task);
2031
2032 return ret;
2033}
2034
2035static ssize_t proc_coredump_filter_write(struct file *file,
2036 const char __user *buf,
2037 size_t count,
2038 loff_t *ppos)
2039{
2040 struct task_struct *task;
2041 struct mm_struct *mm;
2042 char buffer[PROC_NUMBUF], *end;
2043 unsigned int val;
2044 int ret;
2045 int i;
2046 unsigned long mask;
2047
2048 ret = -EFAULT;
2049 memset(buffer, 0, sizeof(buffer));
2050 if (count > sizeof(buffer) - 1)
2051 count = sizeof(buffer) - 1;
2052 if (copy_from_user(buffer, buf, count))
2053 goto out_no_task;
2054
2055 ret = -EINVAL;
2056 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2057 if (*end == '\n')
2058 end++;
2059 if (end - buffer == 0)
2060 goto out_no_task;
2061
2062 ret = -ESRCH;
2063 task = get_proc_task(file->f_dentry->d_inode);
2064 if (!task)
2065 goto out_no_task;
2066
2067 ret = end - buffer;
2068 mm = get_task_mm(task);
2069 if (!mm)
2070 goto out_no_mm;
2071
2072 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2073 if (val & mask)
2074 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2075 else
2076 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2077 }
2078
2079 mmput(mm);
2080 out_no_mm:
2081 put_task_struct(task);
2082 out_no_task:
2083 return ret;
2084}
2085
2086static const struct file_operations proc_coredump_filter_operations = {
2087 .read = proc_coredump_filter_read,
2088 .write = proc_coredump_filter_write,
2089};
2090#endif
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092/*
2093 * /proc/self:
2094 */
2095static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2096 int buflen)
2097{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002098 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002099 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002100 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002101 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002102 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002103 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return vfs_readlink(dentry,buffer,buflen,tmp);
2105}
2106
Al Viro008b1502005-08-20 00:17:39 +01002107static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002109 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002110 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002111 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002112 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002113 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002114 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002115 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002118static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 .readlink = proc_self_readlink,
2120 .follow_link = proc_self_follow_link,
2121};
2122
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002123/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002124 * proc base
2125 *
2126 * These are the directory entries in the root directory of /proc
2127 * that properly belong to the /proc filesystem, as they describe
2128 * describe something that is process related.
2129 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002130static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002131 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002132 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002133};
2134
2135/*
2136 * Exceptional case: normally we are not allowed to unhash a busy
2137 * directory. In this case, however, we can do it - no aliasing problems
2138 * due to the way we treat inodes.
2139 */
2140static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2141{
2142 struct inode *inode = dentry->d_inode;
2143 struct task_struct *task = get_proc_task(inode);
2144 if (task) {
2145 put_task_struct(task);
2146 return 1;
2147 }
2148 d_drop(dentry);
2149 return 0;
2150}
2151
2152static struct dentry_operations proc_base_dentry_operations =
2153{
2154 .d_revalidate = proc_base_revalidate,
2155 .d_delete = pid_delete_dentry,
2156};
2157
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002158static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002159 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002160{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002161 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002162 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002163 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002164 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002165
2166 /* Allocate the inode */
2167 error = ERR_PTR(-ENOMEM);
2168 inode = new_inode(dir->i_sb);
2169 if (!inode)
2170 goto out;
2171
2172 /* Initialize the inode */
2173 ei = PROC_I(inode);
2174 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002175
2176 /*
2177 * grab the reference to the task.
2178 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002179 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002180 if (!ei->pid)
2181 goto out_iput;
2182
2183 inode->i_uid = 0;
2184 inode->i_gid = 0;
2185 inode->i_mode = p->mode;
2186 if (S_ISDIR(inode->i_mode))
2187 inode->i_nlink = 2;
2188 if (S_ISLNK(inode->i_mode))
2189 inode->i_size = 64;
2190 if (p->iop)
2191 inode->i_op = p->iop;
2192 if (p->fop)
2193 inode->i_fop = p->fop;
2194 ei->op = p->op;
2195 dentry->d_op = &proc_base_dentry_operations;
2196 d_add(dentry, inode);
2197 error = NULL;
2198out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002199 return error;
2200out_iput:
2201 iput(inode);
2202 goto out;
2203}
2204
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002205static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2206{
2207 struct dentry *error;
2208 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002209 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002210
2211 error = ERR_PTR(-ENOENT);
2212
2213 if (!task)
2214 goto out_no_task;
2215
2216 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002217 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2218 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002219 if (p->len != dentry->d_name.len)
2220 continue;
2221 if (!memcmp(dentry->d_name.name, p->name, p->len))
2222 break;
2223 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002224 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002225 goto out;
2226
2227 error = proc_base_instantiate(dir, dentry, task, p);
2228
2229out:
2230 put_task_struct(task);
2231out_no_task:
2232 return error;
2233}
2234
Eric Dumazetc5141e62007-05-08 00:26:15 -07002235static int proc_base_fill_cache(struct file *filp, void *dirent,
2236 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002237{
2238 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2239 proc_base_instantiate, task, p);
2240}
2241
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002242#ifdef CONFIG_TASK_IO_ACCOUNTING
2243static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2244{
2245 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002246#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002247 "rchar: %llu\n"
2248 "wchar: %llu\n"
2249 "syscr: %llu\n"
2250 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002251#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002252 "read_bytes: %llu\n"
2253 "write_bytes: %llu\n"
2254 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002255#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002256 (unsigned long long)task->rchar,
2257 (unsigned long long)task->wchar,
2258 (unsigned long long)task->syscr,
2259 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002260#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002261 (unsigned long long)task->ioac.read_bytes,
2262 (unsigned long long)task->ioac.write_bytes,
2263 (unsigned long long)task->ioac.cancelled_write_bytes);
2264}
2265#endif
2266
Eric W. Biederman801199c2006-10-02 02:18:48 -07002267/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002268 * Thread groups
2269 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002270static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002271static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002272
Eric Dumazetc5141e62007-05-08 00:26:15 -07002273static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002274 DIR("task", S_IRUGO|S_IXUGO, task),
2275 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002276 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002277#ifdef CONFIG_NET
Pavel Emelyanove9720ac2008-03-07 11:08:40 -08002278 DIR("net", S_IRUGO|S_IXUSR, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002279#endif
James Pearson315e28c2007-10-16 23:30:17 -07002280 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002281 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002282 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002283 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002284#ifdef CONFIG_SCHED_DEBUG
2285 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2286#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002287 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002288 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002289 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002290 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002291#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002292 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002293#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002294 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002295 LNK("cwd", cwd),
2296 LNK("root", root),
2297 LNK("exe", exe),
2298 REG("mounts", S_IRUGO, mounts),
2299 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002300#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002301 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002302 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002303 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002304#endif
2305#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002306 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002307#endif
2308#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002309 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002310#endif
2311#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002312 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002313#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002314#ifdef CONFIG_LATENCYTOP
2315 REG("latency", S_IRUGO, lstats),
2316#endif
Paul Menage8793d852007-10-18 23:39:39 -07002317#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002318 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002319#endif
Paul Menagea4243162007-10-18 23:39:35 -07002320#ifdef CONFIG_CGROUPS
2321 REG("cgroup", S_IRUGO, cgroup),
2322#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002323 INF("oom_score", S_IRUGO, oom_score),
2324 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002325#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002326 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002327#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002328#ifdef CONFIG_FAULT_INJECTION
2329 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2330#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002331#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2332 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2333#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002334#ifdef CONFIG_TASK_IO_ACCOUNTING
2335 INF("io", S_IRUGO, pid_io_accounting),
2336#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002337};
2338
2339static int proc_tgid_base_readdir(struct file * filp,
2340 void * dirent, filldir_t filldir)
2341{
2342 return proc_pident_readdir(filp,dirent,filldir,
2343 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2344}
2345
Arjan van de Ven00977a52007-02-12 00:55:34 -08002346static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002347 .read = generic_read_dir,
2348 .readdir = proc_tgid_base_readdir,
2349};
2350
2351static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002352 return proc_pident_lookup(dir, dentry,
2353 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002354}
2355
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002356static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002357 .lookup = proc_tgid_base_lookup,
2358 .getattr = pid_getattr,
2359 .setattr = proc_setattr,
2360};
2361
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002362static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002364 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002365 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002366 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Eric W. Biederman48e64842006-06-26 00:25:48 -07002368 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002369 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2370 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002371 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002372 if (!(current->flags & PF_EXITING))
2373 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002374 d_drop(dentry);
2375 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002378 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002379 goto out;
2380
2381 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002382 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2383 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002384 if (!leader)
2385 goto out;
2386
2387 name.name = "task";
2388 name.len = strlen(name.name);
2389 dir = d_hash_and_lookup(leader, &name);
2390 if (!dir)
2391 goto out_put_leader;
2392
2393 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002394 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002395 dentry = d_hash_and_lookup(dir, &name);
2396 if (dentry) {
2397 shrink_dcache_parent(dentry);
2398 d_drop(dentry);
2399 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002401
2402 dput(dir);
2403out_put_leader:
2404 dput(leader);
2405out:
2406 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Randy Dunlap0895e912007-10-21 21:00:10 -07002409/**
2410 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2411 * @task: task that should be flushed.
2412 *
2413 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002414 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002415 * in. This call is supposed to do all of this job.
2416 *
2417 * Looks in the dcache for
2418 * /proc/@pid
2419 * /proc/@tgid/task/@pid
2420 * if either directory is present flushes it and all of it'ts children
2421 * from the dcache.
2422 *
2423 * It is safe and reasonable to cache /proc entries for a task until
2424 * that task exits. After that they just clog up the dcache with
2425 * useless entries, possibly causing useful dcache entries to be
2426 * flushed instead. This routine is proved to flush those useless
2427 * dcache entries at process exit time.
2428 *
2429 * NOTE: This routine is just an optimization so it does not guarantee
2430 * that no dcache entries will exist at process exit time it
2431 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002432 */
2433
2434void proc_flush_task(struct task_struct *task)
2435{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002436 int i;
2437 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002438 struct upid *upid;
2439
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002440 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002441 if (thread_group_leader(task))
2442 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002443
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002444 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002445 upid = &pid->numbers[i];
2446 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002447 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002448 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002449
2450 upid = &pid->numbers[pid->level];
2451 if (upid->nr == 1)
2452 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002453}
2454
Adrian Bunk9711ef92006-12-06 20:38:31 -08002455static struct dentry *proc_pid_instantiate(struct inode *dir,
2456 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002457 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002458{
2459 struct dentry *error = ERR_PTR(-ENOENT);
2460 struct inode *inode;
2461
Eric W. Biederman61a28782006-10-02 02:18:49 -07002462 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002463 if (!inode)
2464 goto out;
2465
2466 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2467 inode->i_op = &proc_tgid_base_inode_operations;
2468 inode->i_fop = &proc_tgid_base_operations;
2469 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002470 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002471#ifdef CONFIG_SECURITY
2472 inode->i_nlink += 1;
2473#endif
2474
2475 dentry->d_op = &pid_dentry_operations;
2476
2477 d_add(dentry, inode);
2478 /* Close the race of the process dying before we return the dentry */
2479 if (pid_revalidate(dentry, NULL))
2480 error = NULL;
2481out:
2482 return error;
2483}
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2486{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002487 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002490 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Eric W. Biederman801199c2006-10-02 02:18:48 -07002492 result = proc_base_lookup(dir, dentry);
2493 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2494 goto out;
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 tgid = name_to_int(dentry);
2497 if (tgid == ~0U)
2498 goto out;
2499
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002500 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002501 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002502 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (task)
2504 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002505 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 if (!task)
2507 goto out;
2508
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002509 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002510 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002512 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002516 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002517 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002519struct tgid_iter {
2520 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002521 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002522};
2523static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2524{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002525 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002527 if (iter.task)
2528 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002529 rcu_read_lock();
2530retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002531 iter.task = NULL;
2532 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002533 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002534 iter.tgid = pid_nr_ns(pid, ns);
2535 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002536 /* What we to know is if the pid we have find is the
2537 * pid of a thread_group_leader. Testing for task
2538 * being a thread_group_leader is the obvious thing
2539 * todo but there is a window when it fails, due to
2540 * the pid transfer logic in de_thread.
2541 *
2542 * So we perform the straight forward test of seeing
2543 * if the pid we have found is the pid of a thread
2544 * group leader, and don't worry if the task we have
2545 * found doesn't happen to be a thread group leader.
2546 * As we don't care in the case of readdir.
2547 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002548 if (!iter.task || !has_group_leader_pid(iter.task)) {
2549 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002550 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002551 }
2552 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002554 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002555 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002558#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Eric W. Biederman61a28782006-10-02 02:18:49 -07002560static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002561 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002562{
2563 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002564 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002565 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002566 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002567}
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569/* for the /proc/ directory itself, after non-process stuff has been done */
2570int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2571{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002573 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002574 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002575 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Eric W. Biederman61a28782006-10-02 02:18:49 -07002577 if (!reaper)
2578 goto out_no_task;
2579
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002580 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002581 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002582 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002583 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
2585
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002586 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002587 iter.task = NULL;
2588 iter.tgid = filp->f_pos - TGID_OFFSET;
2589 for (iter = next_tgid(ns, iter);
2590 iter.task;
2591 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2592 filp->f_pos = iter.tgid + TGID_OFFSET;
2593 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2594 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002598 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2599out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002600 put_task_struct(reaper);
2601out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return 0;
2603}
2604
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002605/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002606 * Tasks
2607 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002608static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002609 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002610 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002611 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002612 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002613 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002614 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002615#ifdef CONFIG_SCHED_DEBUG
2616 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2617#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002618 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002619 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002620 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002621 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002622#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002623 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002624#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002625 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002626 LNK("cwd", cwd),
2627 LNK("root", root),
2628 LNK("exe", exe),
2629 REG("mounts", S_IRUGO, mounts),
Matt Mackall1e883282008-02-04 22:29:07 -08002630#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002631 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002632 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002633 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002634#endif
2635#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002636 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002637#endif
2638#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002639 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002640#endif
2641#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002642 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002643#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002644#ifdef CONFIG_LATENCYTOP
2645 REG("latency", S_IRUGO, lstats),
2646#endif
Paul Menage8793d852007-10-18 23:39:39 -07002647#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002648 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002649#endif
Paul Menagea4243162007-10-18 23:39:35 -07002650#ifdef CONFIG_CGROUPS
2651 REG("cgroup", S_IRUGO, cgroup),
2652#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002653 INF("oom_score", S_IRUGO, oom_score),
2654 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002655#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002656 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002658#ifdef CONFIG_FAULT_INJECTION
2659 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2660#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002661};
2662
2663static int proc_tid_base_readdir(struct file * filp,
2664 void * dirent, filldir_t filldir)
2665{
2666 return proc_pident_readdir(filp,dirent,filldir,
2667 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2668}
2669
2670static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002671 return proc_pident_lookup(dir, dentry,
2672 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002673}
2674
Arjan van de Ven00977a52007-02-12 00:55:34 -08002675static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002676 .read = generic_read_dir,
2677 .readdir = proc_tid_base_readdir,
2678};
2679
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002680static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002681 .lookup = proc_tid_base_lookup,
2682 .getattr = pid_getattr,
2683 .setattr = proc_setattr,
2684};
2685
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002686static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002687 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002688{
2689 struct dentry *error = ERR_PTR(-ENOENT);
2690 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002691 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002692
2693 if (!inode)
2694 goto out;
2695 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2696 inode->i_op = &proc_tid_base_inode_operations;
2697 inode->i_fop = &proc_tid_base_operations;
2698 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002699 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002700#ifdef CONFIG_SECURITY
2701 inode->i_nlink += 1;
2702#endif
2703
2704 dentry->d_op = &pid_dentry_operations;
2705
2706 d_add(dentry, inode);
2707 /* Close the race of the process dying before we return the dentry */
2708 if (pid_revalidate(dentry, NULL))
2709 error = NULL;
2710out:
2711 return error;
2712}
2713
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002714static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2715{
2716 struct dentry *result = ERR_PTR(-ENOENT);
2717 struct task_struct *task;
2718 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002719 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002720 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002721
2722 if (!leader)
2723 goto out_no_task;
2724
2725 tid = name_to_int(dentry);
2726 if (tid == ~0U)
2727 goto out;
2728
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002729 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002730 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002731 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002732 if (task)
2733 get_task_struct(task);
2734 rcu_read_unlock();
2735 if (!task)
2736 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002737 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002738 goto out_drop_task;
2739
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002740 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002741out_drop_task:
2742 put_task_struct(task);
2743out:
2744 put_task_struct(leader);
2745out_no_task:
2746 return result;
2747}
2748
2749/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002750 * Find the first tid of a thread group to return to user space.
2751 *
2752 * Usually this is just the thread group leader, but if the users
2753 * buffer was too small or there was a seek into the middle of the
2754 * directory we have more work todo.
2755 *
2756 * In the case of a short read we start with find_task_by_pid.
2757 *
2758 * In the case of a seek we start with the leader and walk nr
2759 * threads past it.
2760 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002761static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002762 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002763{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002764 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002765
Eric W. Biedermancc288732006-06-26 00:26:01 -07002766 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002767 /* Attempt to start with the pid of a thread */
2768 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002769 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002770 if (pos && (pos->group_leader == leader))
2771 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002772 }
2773
2774 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002775 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002776 if (nr && nr >= get_nr_threads(leader))
2777 goto out;
2778
2779 /* If we haven't found our starting place yet start
2780 * with the leader and walk nr threads forward.
2781 */
2782 for (pos = leader; nr > 0; --nr) {
2783 pos = next_thread(pos);
2784 if (pos == leader) {
2785 pos = NULL;
2786 goto out;
2787 }
2788 }
2789found:
2790 get_task_struct(pos);
2791out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002792 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002793 return pos;
2794}
2795
2796/*
2797 * Find the next thread in the thread list.
2798 * Return NULL if there is an error or no next thread.
2799 *
2800 * The reference to the input task_struct is released.
2801 */
2802static struct task_struct *next_tid(struct task_struct *start)
2803{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002804 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002805 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002806 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002807 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002808 if (thread_group_leader(pos))
2809 pos = NULL;
2810 else
2811 get_task_struct(pos);
2812 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002813 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002814 put_task_struct(start);
2815 return pos;
2816}
2817
Eric W. Biederman61a28782006-10-02 02:18:49 -07002818static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2819 struct task_struct *task, int tid)
2820{
2821 char name[PROC_NUMBUF];
2822 int len = snprintf(name, sizeof(name), "%d", tid);
2823 return proc_fill_cache(filp, dirent, filldir, name, len,
2824 proc_task_instantiate, task, NULL);
2825}
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827/* for the /proc/TGID/task/ directories */
2828static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2829{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002830 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002832 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002833 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 int retval = -ENOENT;
2835 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002836 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002838 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002840 task = get_proc_task(inode);
2841 if (!task)
2842 goto out_no_task;
2843 rcu_read_lock();
2844 if (pid_alive(task)) {
2845 leader = task->group_leader;
2846 get_task_struct(leader);
2847 }
2848 rcu_read_unlock();
2849 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002850 if (!leader)
2851 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 retval = 0;
2853
2854 switch (pos) {
2855 case 0:
2856 ino = inode->i_ino;
2857 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2858 goto out;
2859 pos++;
2860 /* fall through */
2861 case 1:
2862 ino = parent_ino(dentry);
2863 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2864 goto out;
2865 pos++;
2866 /* fall through */
2867 }
2868
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002869 /* f_version caches the tgid value that the last readdir call couldn't
2870 * return. lseek aka telldir automagically resets f_version to 0.
2871 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002872 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002873 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002874 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002875 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002876 task;
2877 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002878 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002879 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002880 /* returning this tgid failed, save it as the first
2881 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002882 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002883 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887out:
2888 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002889 put_task_struct(leader);
2890out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return retval;
2892}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002893
2894static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2895{
2896 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002897 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002898 generic_fillattr(inode, stat);
2899
Eric W. Biederman99f89552006-06-26 00:25:55 -07002900 if (p) {
2901 rcu_read_lock();
2902 stat->nlink += get_nr_threads(p);
2903 rcu_read_unlock();
2904 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002905 }
2906
2907 return 0;
2908}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002909
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002910static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002911 .lookup = proc_task_lookup,
2912 .getattr = proc_task_getattr,
2913 .setattr = proc_setattr,
2914};
2915
Arjan van de Ven00977a52007-02-12 00:55:34 -08002916static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002917 .read = generic_read_dir,
2918 .readdir = proc_task_readdir,
2919};