blob: 88f8edf18258ac50681474ffc9542c75de30ecda [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},
419};
420
421/* Display limits for a process */
422static int proc_pid_limits(struct task_struct *task, char *buffer)
423{
424 unsigned int i;
425 int count = 0;
426 unsigned long flags;
427 char *bufptr = buffer;
428
429 struct rlimit rlim[RLIM_NLIMITS];
430
431 rcu_read_lock();
432 if (!lock_task_sighand(task,&flags)) {
433 rcu_read_unlock();
434 return 0;
435 }
436 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
437 unlock_task_sighand(task, &flags);
438 rcu_read_unlock();
439
440 /*
441 * print the file header
442 */
443 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
444 "Limit", "Soft Limit", "Hard Limit", "Units");
445
446 for (i = 0; i < RLIM_NLIMITS; i++) {
447 if (rlim[i].rlim_cur == RLIM_INFINITY)
448 count += sprintf(&bufptr[count], "%-25s %-20s ",
449 lnames[i].name, "unlimited");
450 else
451 count += sprintf(&bufptr[count], "%-25s %-20lu ",
452 lnames[i].name, rlim[i].rlim_cur);
453
454 if (rlim[i].rlim_max == RLIM_INFINITY)
455 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
456 else
457 count += sprintf(&bufptr[count], "%-20lu ",
458 rlim[i].rlim_max);
459
460 if (lnames[i].unit)
461 count += sprintf(&bufptr[count], "%-10s\n",
462 lnames[i].unit);
463 else
464 count += sprintf(&bufptr[count], "\n");
465 }
466
467 return count;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/************************************************************************/
471/* Here the fs part begins */
472/************************************************************************/
473
474/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700475static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700477 struct task_struct *task;
478 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700479 /* Allow access to a task's file descriptors if it is us or we
480 * may use ptrace attach to the process and find out that
481 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700482 */
483 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700484 if (task) {
485 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700486 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700487 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700488 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700491static int proc_setattr(struct dentry *dentry, struct iattr *attr)
492{
493 int error;
494 struct inode *inode = dentry->d_inode;
495
496 if (attr->ia_valid & ATTR_MODE)
497 return -EPERM;
498
499 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700500 if (!error)
501 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700502 return error;
503}
504
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800505static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700506 .setattr = proc_setattr,
507};
508
Jan Engelhardt03a44822008-02-08 04:21:19 -0800509extern const struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500510struct proc_mounts {
511 struct seq_file m;
512 int event;
513};
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static int mounts_open(struct inode *inode, struct file *file)
516{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700517 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700518 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800519 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500520 struct proc_mounts *p;
521 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Eric W. Biederman99f89552006-06-26 00:25:55 -0700523 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700524 rcu_read_lock();
525 nsp = task_nsproxy(task);
526 if (nsp) {
527 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800528 if (ns)
529 get_mnt_ns(ns);
530 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700531 rcu_read_unlock();
532
Eric W. Biederman99f89552006-06-26 00:25:55 -0700533 put_task_struct(task);
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800536 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500537 ret = -ENOMEM;
538 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
539 if (p) {
540 file->private_data = &p->m;
541 ret = seq_open(file, &mounts_op);
542 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800543 p->m.private = ns;
544 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500545 return 0;
546 }
547 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800549 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 return ret;
552}
553
554static int mounts_release(struct inode *inode, struct file *file)
555{
556 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800557 struct mnt_namespace *ns = m->private;
558 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return seq_release(inode, file);
560}
561
Al Viro5addc5d2005-11-07 17:15:49 -0500562static unsigned mounts_poll(struct file *file, poll_table *wait)
563{
564 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800565 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500566 unsigned res = 0;
567
568 poll_wait(file, &ns->poll, wait);
569
570 spin_lock(&vfsmount_lock);
571 if (p->event != ns->event) {
572 p->event = ns->event;
573 res = POLLERR;
574 }
575 spin_unlock(&vfsmount_lock);
576
577 return res;
578}
579
Arjan van de Ven00977a52007-02-12 00:55:34 -0800580static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 .open = mounts_open,
582 .read = seq_read,
583 .llseek = seq_lseek,
584 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500585 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586};
587
Jan Engelhardt03a44822008-02-08 04:21:19 -0800588extern const struct seq_operations mountstats_op;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500589static int mountstats_open(struct inode *inode, struct file *file)
590{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500591 int ret = seq_open(file, &mountstats_op);
592
593 if (!ret) {
594 struct seq_file *m = file->private_data;
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700595 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800596 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700597 struct task_struct *task = get_proc_task(inode);
598
599 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700600 rcu_read_lock();
601 nsp = task_nsproxy(task);
602 if (nsp) {
603 mnt_ns = nsp->mnt_ns;
604 if (mnt_ns)
605 get_mnt_ns(mnt_ns);
606 }
607 rcu_read_unlock();
608
Eric W. Biederman99f89552006-06-26 00:25:55 -0700609 put_task_struct(task);
610 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500611
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800612 if (mnt_ns)
613 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500614 else {
615 seq_release(inode, file);
616 ret = -EINVAL;
617 }
618 }
619 return ret;
620}
621
Arjan van de Ven00977a52007-02-12 00:55:34 -0800622static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500623 .open = mountstats_open,
624 .read = seq_read,
625 .llseek = seq_lseek,
626 .release = mounts_release,
627};
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
630
631static ssize_t proc_info_read(struct file * file, char __user * buf,
632 size_t count, loff_t *ppos)
633{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800634 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 unsigned long page;
636 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700637 struct task_struct *task = get_proc_task(inode);
638
639 length = -ESRCH;
640 if (!task)
641 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (count > PROC_BLOCK_SIZE)
644 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700645
646 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700647 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 length = PROC_I(inode)->op.proc_read(task, (char*)page);
651
652 if (length >= 0)
653 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
654 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700655out:
656 put_task_struct(task);
657out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return length;
659}
660
Arjan van de Ven00977a52007-02-12 00:55:34 -0800661static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .read = proc_info_read,
663};
664
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800665static int proc_single_show(struct seq_file *m, void *v)
666{
667 struct inode *inode = m->private;
668 struct pid_namespace *ns;
669 struct pid *pid;
670 struct task_struct *task;
671 int ret;
672
673 ns = inode->i_sb->s_fs_info;
674 pid = proc_pid(inode);
675 task = get_pid_task(pid, PIDTYPE_PID);
676 if (!task)
677 return -ESRCH;
678
679 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
680
681 put_task_struct(task);
682 return ret;
683}
684
685static int proc_single_open(struct inode *inode, struct file *filp)
686{
687 int ret;
688 ret = single_open(filp, proc_single_show, NULL);
689 if (!ret) {
690 struct seq_file *m = filp->private_data;
691
692 m->private = inode;
693 }
694 return ret;
695}
696
697static const struct file_operations proc_single_file_operations = {
698 .open = proc_single_open,
699 .read = seq_read,
700 .llseek = seq_lseek,
701 .release = single_release,
702};
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static int mem_open(struct inode* inode, struct file* file)
705{
706 file->private_data = (void*)((long)current->self_exec_id);
707 return 0;
708}
709
710static ssize_t mem_read(struct file * file, char __user * buf,
711 size_t count, loff_t *ppos)
712{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800713 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 char *page;
715 unsigned long src = *ppos;
716 int ret = -ESRCH;
717 struct mm_struct *mm;
718
Eric W. Biederman99f89552006-06-26 00:25:55 -0700719 if (!task)
720 goto out_no_task;
721
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700722 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto out;
724
725 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700726 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!page)
728 goto out;
729
730 ret = 0;
731
732 mm = get_task_mm(task);
733 if (!mm)
734 goto out_free;
735
736 ret = -EIO;
737
738 if (file->private_data != (void*)((long)current->self_exec_id))
739 goto out_put;
740
741 ret = 0;
742
743 while (count > 0) {
744 int this_len, retval;
745
746 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
747 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700748 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (!ret)
750 ret = -EIO;
751 break;
752 }
753
754 if (copy_to_user(buf, page, retval)) {
755 ret = -EFAULT;
756 break;
757 }
758
759 ret += retval;
760 src += retval;
761 buf += retval;
762 count -= retval;
763 }
764 *ppos = src;
765
766out_put:
767 mmput(mm);
768out_free:
769 free_page((unsigned long) page);
770out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700771 put_task_struct(task);
772out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return ret;
774}
775
776#define mem_write NULL
777
778#ifndef mem_write
779/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800780static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 size_t count, loff_t *ppos)
782{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700783 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800785 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 unsigned long dst = *ppos;
787
Eric W. Biederman99f89552006-06-26 00:25:55 -0700788 copied = -ESRCH;
789 if (!task)
790 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Eric W. Biederman99f89552006-06-26 00:25:55 -0700792 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
793 goto out;
794
795 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700796 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700798 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700800 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 while (count > 0) {
802 int this_len, retval;
803
804 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
805 if (copy_from_user(page, buf, this_len)) {
806 copied = -EFAULT;
807 break;
808 }
809 retval = access_process_vm(task, dst, page, this_len, 1);
810 if (!retval) {
811 if (!copied)
812 copied = -EIO;
813 break;
814 }
815 copied += retval;
816 buf += retval;
817 dst += retval;
818 count -= retval;
819 }
820 *ppos = dst;
821 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700822out:
823 put_task_struct(task);
824out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return copied;
826}
827#endif
828
Matt Mackall85863e42008-02-04 22:29:04 -0800829loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 switch (orig) {
832 case 0:
833 file->f_pos = offset;
834 break;
835 case 1:
836 file->f_pos += offset;
837 break;
838 default:
839 return -EINVAL;
840 }
841 force_successful_syscall_return();
842 return file->f_pos;
843}
844
Arjan van de Ven00977a52007-02-12 00:55:34 -0800845static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 .llseek = mem_lseek,
847 .read = mem_read,
848 .write = mem_write,
849 .open = mem_open,
850};
851
James Pearson315e28c2007-10-16 23:30:17 -0700852static ssize_t environ_read(struct file *file, char __user *buf,
853 size_t count, loff_t *ppos)
854{
855 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
856 char *page;
857 unsigned long src = *ppos;
858 int ret = -ESRCH;
859 struct mm_struct *mm;
860
861 if (!task)
862 goto out_no_task;
863
864 if (!ptrace_may_attach(task))
865 goto out;
866
867 ret = -ENOMEM;
868 page = (char *)__get_free_page(GFP_TEMPORARY);
869 if (!page)
870 goto out;
871
872 ret = 0;
873
874 mm = get_task_mm(task);
875 if (!mm)
876 goto out_free;
877
878 while (count > 0) {
879 int this_len, retval, max_len;
880
881 this_len = mm->env_end - (mm->env_start + src);
882
883 if (this_len <= 0)
884 break;
885
886 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
887 this_len = (this_len > max_len) ? max_len : this_len;
888
889 retval = access_process_vm(task, (mm->env_start + src),
890 page, this_len, 0);
891
892 if (retval <= 0) {
893 ret = retval;
894 break;
895 }
896
897 if (copy_to_user(buf, page, retval)) {
898 ret = -EFAULT;
899 break;
900 }
901
902 ret += retval;
903 src += retval;
904 buf += retval;
905 count -= retval;
906 }
907 *ppos = src;
908
909 mmput(mm);
910out_free:
911 free_page((unsigned long) page);
912out:
913 put_task_struct(task);
914out_no_task:
915 return ret;
916}
917
918static const struct file_operations proc_environ_operations = {
919 .read = environ_read,
920};
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922static ssize_t oom_adjust_read(struct file *file, char __user *buf,
923 size_t count, loff_t *ppos)
924{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800925 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700926 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700928 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Eric W. Biederman99f89552006-06-26 00:25:55 -0700930 if (!task)
931 return -ESRCH;
932 oom_adjust = task->oomkilladj;
933 put_task_struct(task);
934
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700935 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700936
937 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
941 size_t count, loff_t *ppos)
942{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700943 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700944 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int oom_adjust;
946
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700947 memset(buffer, 0, sizeof(buffer));
948 if (count > sizeof(buffer) - 1)
949 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (copy_from_user(buffer, buf, count))
951 return -EFAULT;
952 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700953 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
954 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -EINVAL;
956 if (*end == '\n')
957 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800958 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700959 if (!task)
960 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800961 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
962 put_task_struct(task);
963 return -EACCES;
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700966 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (end - buffer == 0)
968 return -EIO;
969 return end - buffer;
970}
971
Arjan van de Ven00977a52007-02-12 00:55:34 -0800972static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 .read = oom_adjust_read,
974 .write = oom_adjust_write,
975};
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#ifdef CONFIG_AUDITSYSCALL
978#define TMPBUFLEN 21
979static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
980 size_t count, loff_t *ppos)
981{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800982 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700983 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 ssize_t length;
985 char tmpbuf[TMPBUFLEN];
986
Eric W. Biederman99f89552006-06-26 00:25:55 -0700987 if (!task)
988 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -0500990 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700991 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
993}
994
995static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
996 size_t count, loff_t *ppos)
997{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800998 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 char *page, *tmp;
1000 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 uid_t loginuid;
1002
1003 if (!capable(CAP_AUDIT_CONTROL))
1004 return -EPERM;
1005
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001006 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return -EPERM;
1008
Al Viroe0182902006-05-18 08:28:02 -04001009 if (count >= PAGE_SIZE)
1010 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (*ppos != 0) {
1013 /* No partial writes. */
1014 return -EINVAL;
1015 }
Mel Gormane12ba742007-10-16 01:25:52 -07001016 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!page)
1018 return -ENOMEM;
1019 length = -EFAULT;
1020 if (copy_from_user(page, buf, count))
1021 goto out_free_page;
1022
Al Viroe0182902006-05-18 08:28:02 -04001023 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 loginuid = simple_strtoul(page, &tmp, 10);
1025 if (tmp == page) {
1026 length = -EINVAL;
1027 goto out_free_page;
1028
1029 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001030 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (likely(length == 0))
1032 length = count;
1033
1034out_free_page:
1035 free_page((unsigned long) page);
1036 return length;
1037}
1038
Arjan van de Ven00977a52007-02-12 00:55:34 -08001039static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 .read = proc_loginuid_read,
1041 .write = proc_loginuid_write,
1042};
1043#endif
1044
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001045#ifdef CONFIG_FAULT_INJECTION
1046static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1047 size_t count, loff_t *ppos)
1048{
1049 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1050 char buffer[PROC_NUMBUF];
1051 size_t len;
1052 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001053
1054 if (!task)
1055 return -ESRCH;
1056 make_it_fail = task->make_it_fail;
1057 put_task_struct(task);
1058
1059 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001060
1061 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001062}
1063
1064static ssize_t proc_fault_inject_write(struct file * file,
1065 const char __user * buf, size_t count, loff_t *ppos)
1066{
1067 struct task_struct *task;
1068 char buffer[PROC_NUMBUF], *end;
1069 int make_it_fail;
1070
1071 if (!capable(CAP_SYS_RESOURCE))
1072 return -EPERM;
1073 memset(buffer, 0, sizeof(buffer));
1074 if (count > sizeof(buffer) - 1)
1075 count = sizeof(buffer) - 1;
1076 if (copy_from_user(buffer, buf, count))
1077 return -EFAULT;
1078 make_it_fail = simple_strtol(buffer, &end, 0);
1079 if (*end == '\n')
1080 end++;
1081 task = get_proc_task(file->f_dentry->d_inode);
1082 if (!task)
1083 return -ESRCH;
1084 task->make_it_fail = make_it_fail;
1085 put_task_struct(task);
1086 if (end - buffer == 0)
1087 return -EIO;
1088 return end - buffer;
1089}
1090
Arjan van de Ven00977a52007-02-12 00:55:34 -08001091static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001092 .read = proc_fault_inject_read,
1093 .write = proc_fault_inject_write,
1094};
1095#endif
1096
Arjan van de Ven97455122008-01-25 21:08:34 +01001097
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001098#ifdef CONFIG_SCHED_DEBUG
1099/*
1100 * Print out various scheduling related per-task fields:
1101 */
1102static int sched_show(struct seq_file *m, void *v)
1103{
1104 struct inode *inode = m->private;
1105 struct task_struct *p;
1106
1107 WARN_ON(!inode);
1108
1109 p = get_proc_task(inode);
1110 if (!p)
1111 return -ESRCH;
1112 proc_sched_show_task(p, m);
1113
1114 put_task_struct(p);
1115
1116 return 0;
1117}
1118
1119static ssize_t
1120sched_write(struct file *file, const char __user *buf,
1121 size_t count, loff_t *offset)
1122{
1123 struct inode *inode = file->f_path.dentry->d_inode;
1124 struct task_struct *p;
1125
1126 WARN_ON(!inode);
1127
1128 p = get_proc_task(inode);
1129 if (!p)
1130 return -ESRCH;
1131 proc_sched_set_task(p);
1132
1133 put_task_struct(p);
1134
1135 return count;
1136}
1137
1138static int sched_open(struct inode *inode, struct file *filp)
1139{
1140 int ret;
1141
1142 ret = single_open(filp, sched_show, NULL);
1143 if (!ret) {
1144 struct seq_file *m = filp->private_data;
1145
1146 m->private = inode;
1147 }
1148 return ret;
1149}
1150
1151static const struct file_operations proc_pid_sched_operations = {
1152 .open = sched_open,
1153 .read = seq_read,
1154 .write = sched_write,
1155 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001156 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001157};
1158
1159#endif
1160
Al Viro008b1502005-08-20 00:17:39 +01001161static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct inode *inode = dentry->d_inode;
1164 int error = -EACCES;
1165
1166 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001167 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Eric W. Biederman778c1142006-06-26 00:25:58 -07001169 /* Are we allowed to snoop on the tasks file descriptors? */
1170 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001173 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 nd->last_type = LAST_BIND;
1175out:
Al Viro008b1502005-08-20 00:17:39 +01001176 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001179static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Mel Gormane12ba742007-10-16 01:25:52 -07001181 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001182 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int len;
1184
1185 if (!tmp)
1186 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001187
Jan Blunckcf28b482008-02-14 19:38:44 -08001188 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001189 len = PTR_ERR(pathname);
1190 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001192 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 if (len > buflen)
1195 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001196 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 len = -EFAULT;
1198 out:
1199 free_page((unsigned long)tmp);
1200 return len;
1201}
1202
1203static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1204{
1205 int error = -EACCES;
1206 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001207 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Eric W. Biederman778c1142006-06-26 00:25:58 -07001209 /* Are we allowed to snoop on the tasks file descriptors? */
1210 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001213 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (error)
1215 goto out;
1216
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001217 error = do_proc_readlink(&path, buffer, buflen);
1218 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return error;
1221}
1222
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001223static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001225 .follow_link = proc_pid_follow_link,
1226 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227};
1228
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001229
1230/* building an inode */
1231
1232static int task_dumpable(struct task_struct *task)
1233{
1234 int dumpable = 0;
1235 struct mm_struct *mm;
1236
1237 task_lock(task);
1238 mm = task->mm;
1239 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001240 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001241 task_unlock(task);
1242 if(dumpable == 1)
1243 return 1;
1244 return 0;
1245}
1246
1247
Eric W. Biederman61a28782006-10-02 02:18:49 -07001248static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001249{
1250 struct inode * inode;
1251 struct proc_inode *ei;
1252
1253 /* We need a new inode */
1254
1255 inode = new_inode(sb);
1256 if (!inode)
1257 goto out;
1258
1259 /* Common stuff */
1260 ei = PROC_I(inode);
1261 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001262 inode->i_op = &proc_def_inode_operations;
1263
1264 /*
1265 * grab the reference to task.
1266 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001267 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001268 if (!ei->pid)
1269 goto out_unlock;
1270
1271 inode->i_uid = 0;
1272 inode->i_gid = 0;
1273 if (task_dumpable(task)) {
1274 inode->i_uid = task->euid;
1275 inode->i_gid = task->egid;
1276 }
1277 security_task_to_inode(task, inode);
1278
1279out:
1280 return inode;
1281
1282out_unlock:
1283 iput(inode);
1284 return NULL;
1285}
1286
1287static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1288{
1289 struct inode *inode = dentry->d_inode;
1290 struct task_struct *task;
1291 generic_fillattr(inode, stat);
1292
1293 rcu_read_lock();
1294 stat->uid = 0;
1295 stat->gid = 0;
1296 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1297 if (task) {
1298 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1299 task_dumpable(task)) {
1300 stat->uid = task->euid;
1301 stat->gid = task->egid;
1302 }
1303 }
1304 rcu_read_unlock();
1305 return 0;
1306}
1307
1308/* dentry stuff */
1309
1310/*
1311 * Exceptional case: normally we are not allowed to unhash a busy
1312 * directory. In this case, however, we can do it - no aliasing problems
1313 * due to the way we treat inodes.
1314 *
1315 * Rewrite the inode's ownerships here because the owning task may have
1316 * performed a setuid(), etc.
1317 *
1318 * Before the /proc/pid/status file was created the only way to read
1319 * the effective uid of a /process was to stat /proc/pid. Reading
1320 * /proc/pid/status is slow enough that procps and other packages
1321 * kept stating /proc/pid. To keep the rules in /proc simple I have
1322 * made this apply to all per process world readable and executable
1323 * directories.
1324 */
1325static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1326{
1327 struct inode *inode = dentry->d_inode;
1328 struct task_struct *task = get_proc_task(inode);
1329 if (task) {
1330 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1331 task_dumpable(task)) {
1332 inode->i_uid = task->euid;
1333 inode->i_gid = task->egid;
1334 } else {
1335 inode->i_uid = 0;
1336 inode->i_gid = 0;
1337 }
1338 inode->i_mode &= ~(S_ISUID | S_ISGID);
1339 security_task_to_inode(task, inode);
1340 put_task_struct(task);
1341 return 1;
1342 }
1343 d_drop(dentry);
1344 return 0;
1345}
1346
1347static int pid_delete_dentry(struct dentry * dentry)
1348{
1349 /* Is the task we represent dead?
1350 * If so, then don't put the dentry on the lru list,
1351 * kill it immediately.
1352 */
1353 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1354}
1355
1356static struct dentry_operations pid_dentry_operations =
1357{
1358 .d_revalidate = pid_revalidate,
1359 .d_delete = pid_delete_dentry,
1360};
1361
1362/* Lookups */
1363
Eric Dumazetc5141e62007-05-08 00:26:15 -07001364typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1365 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001366
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001367/*
1368 * Fill a directory entry.
1369 *
1370 * If possible create the dcache entry and derive our inode number and
1371 * file type from dcache entry.
1372 *
1373 * Since all of the proc inode numbers are dynamically generated, the inode
1374 * numbers do not exist until the inode is cache. This means creating the
1375 * the dcache entry in readdir is necessary to keep the inode numbers
1376 * reported by readdir in sync with the inode numbers reported
1377 * by stat.
1378 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001379static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1380 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001381 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001382{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001383 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001384 struct inode *inode;
1385 struct qstr qname;
1386 ino_t ino = 0;
1387 unsigned type = DT_UNKNOWN;
1388
1389 qname.name = name;
1390 qname.len = len;
1391 qname.hash = full_name_hash(name, len);
1392
1393 child = d_lookup(dir, &qname);
1394 if (!child) {
1395 struct dentry *new;
1396 new = d_alloc(dir, &qname);
1397 if (new) {
1398 child = instantiate(dir->d_inode, new, task, ptr);
1399 if (child)
1400 dput(new);
1401 else
1402 child = new;
1403 }
1404 }
1405 if (!child || IS_ERR(child) || !child->d_inode)
1406 goto end_instantiate;
1407 inode = child->d_inode;
1408 if (inode) {
1409 ino = inode->i_ino;
1410 type = inode->i_mode >> 12;
1411 }
1412 dput(child);
1413end_instantiate:
1414 if (!ino)
1415 ino = find_inode_number(dir, &qname);
1416 if (!ino)
1417 ino = 1;
1418 return filldir(dirent, name, len, filp->f_pos, ino, type);
1419}
1420
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001421static unsigned name_to_int(struct dentry *dentry)
1422{
1423 const char *name = dentry->d_name.name;
1424 int len = dentry->d_name.len;
1425 unsigned n = 0;
1426
1427 if (len > 1 && *name == '0')
1428 goto out;
1429 while (len-- > 0) {
1430 unsigned c = *name++ - '0';
1431 if (c > 9)
1432 goto out;
1433 if (n >= (~0U-9)/10)
1434 goto out;
1435 n *= 10;
1436 n += c;
1437 }
1438 return n;
1439out:
1440 return ~0U;
1441}
1442
Miklos Szeredi27932742007-05-08 00:26:17 -07001443#define PROC_FDINFO_MAX 64
1444
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001445static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001446{
1447 struct task_struct *task = get_proc_task(inode);
1448 struct files_struct *files = NULL;
1449 struct file *file;
1450 int fd = proc_fd(inode);
1451
1452 if (task) {
1453 files = get_files_struct(task);
1454 put_task_struct(task);
1455 }
1456 if (files) {
1457 /*
1458 * We are not taking a ref to the file structure, so we must
1459 * hold ->file_lock.
1460 */
1461 spin_lock(&files->file_lock);
1462 file = fcheck_files(files, fd);
1463 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001464 if (path) {
1465 *path = file->f_path;
1466 path_get(&file->f_path);
1467 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001468 if (info)
1469 snprintf(info, PROC_FDINFO_MAX,
1470 "pos:\t%lli\n"
1471 "flags:\t0%o\n",
1472 (long long) file->f_pos,
1473 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001474 spin_unlock(&files->file_lock);
1475 put_files_struct(files);
1476 return 0;
1477 }
1478 spin_unlock(&files->file_lock);
1479 put_files_struct(files);
1480 }
1481 return -ENOENT;
1482}
1483
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001484static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001485{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001486 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001487}
1488
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001489static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1490{
1491 struct inode *inode = dentry->d_inode;
1492 struct task_struct *task = get_proc_task(inode);
1493 int fd = proc_fd(inode);
1494 struct files_struct *files;
1495
1496 if (task) {
1497 files = get_files_struct(task);
1498 if (files) {
1499 rcu_read_lock();
1500 if (fcheck_files(files, fd)) {
1501 rcu_read_unlock();
1502 put_files_struct(files);
1503 if (task_dumpable(task)) {
1504 inode->i_uid = task->euid;
1505 inode->i_gid = task->egid;
1506 } else {
1507 inode->i_uid = 0;
1508 inode->i_gid = 0;
1509 }
1510 inode->i_mode &= ~(S_ISUID | S_ISGID);
1511 security_task_to_inode(task, inode);
1512 put_task_struct(task);
1513 return 1;
1514 }
1515 rcu_read_unlock();
1516 put_files_struct(files);
1517 }
1518 put_task_struct(task);
1519 }
1520 d_drop(dentry);
1521 return 0;
1522}
1523
1524static struct dentry_operations tid_fd_dentry_operations =
1525{
1526 .d_revalidate = tid_fd_revalidate,
1527 .d_delete = pid_delete_dentry,
1528};
1529
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001530static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001531 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001532{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001533 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001534 struct file *file;
1535 struct files_struct *files;
1536 struct inode *inode;
1537 struct proc_inode *ei;
1538 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001539
Eric W. Biederman61a28782006-10-02 02:18:49 -07001540 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001541 if (!inode)
1542 goto out;
1543 ei = PROC_I(inode);
1544 ei->fd = fd;
1545 files = get_files_struct(task);
1546 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001547 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001548 inode->i_mode = S_IFLNK;
1549
1550 /*
1551 * We are not taking a ref to the file structure, so we must
1552 * hold ->file_lock.
1553 */
1554 spin_lock(&files->file_lock);
1555 file = fcheck_files(files, fd);
1556 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001557 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001558 if (file->f_mode & 1)
1559 inode->i_mode |= S_IRUSR | S_IXUSR;
1560 if (file->f_mode & 2)
1561 inode->i_mode |= S_IWUSR | S_IXUSR;
1562 spin_unlock(&files->file_lock);
1563 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001564
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001565 inode->i_op = &proc_pid_link_inode_operations;
1566 inode->i_size = 64;
1567 ei->op.proc_get_link = proc_fd_link;
1568 dentry->d_op = &tid_fd_dentry_operations;
1569 d_add(dentry, inode);
1570 /* Close the race of the process dying before we return the dentry */
1571 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001572 error = NULL;
1573
1574 out:
1575 return error;
1576out_unlock:
1577 spin_unlock(&files->file_lock);
1578 put_files_struct(files);
1579out_iput:
1580 iput(inode);
1581 goto out;
1582}
1583
Miklos Szeredi27932742007-05-08 00:26:17 -07001584static struct dentry *proc_lookupfd_common(struct inode *dir,
1585 struct dentry *dentry,
1586 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001587{
1588 struct task_struct *task = get_proc_task(dir);
1589 unsigned fd = name_to_int(dentry);
1590 struct dentry *result = ERR_PTR(-ENOENT);
1591
1592 if (!task)
1593 goto out_no_task;
1594 if (fd == ~0U)
1595 goto out;
1596
Miklos Szeredi27932742007-05-08 00:26:17 -07001597 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001598out:
1599 put_task_struct(task);
1600out_no_task:
1601 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001602}
1603
Miklos Szeredi27932742007-05-08 00:26:17 -07001604static int proc_readfd_common(struct file * filp, void * dirent,
1605 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001607 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001608 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001609 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001610 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001613 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001616 if (!p)
1617 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 fd = filp->f_pos;
1621 switch (fd) {
1622 case 0:
1623 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1624 goto out;
1625 filp->f_pos++;
1626 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001627 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1629 goto out;
1630 filp->f_pos++;
1631 default:
1632 files = get_files_struct(p);
1633 if (!files)
1634 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001635 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001636 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001638 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001640 char name[PROC_NUMBUF];
1641 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 if (!fcheck_files(files, fd))
1644 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001645 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Miklos Szeredi27932742007-05-08 00:26:17 -07001647 len = snprintf(name, sizeof(name), "%d", fd);
1648 if (proc_fill_cache(filp, dirent, filldir,
1649 name, len, instantiate,
1650 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001651 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 break;
1653 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001654 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001656 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 put_files_struct(files);
1658 }
1659out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001660 put_task_struct(p);
1661out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return retval;
1663}
1664
Miklos Szeredi27932742007-05-08 00:26:17 -07001665static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1666 struct nameidata *nd)
1667{
1668 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1669}
1670
1671static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1672{
1673 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1674}
1675
1676static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1677 size_t len, loff_t *ppos)
1678{
1679 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001680 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001681 if (!err)
1682 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1683 return err;
1684}
1685
1686static const struct file_operations proc_fdinfo_file_operations = {
1687 .open = nonseekable_open,
1688 .read = proc_fdinfo_read,
1689};
1690
Arjan van de Ven00977a52007-02-12 00:55:34 -08001691static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 .read = generic_read_dir,
1693 .readdir = proc_readfd,
1694};
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001697 * /proc/pid/fd needs a special permission handler so that a process can still
1698 * access /proc/self/fd after it has executed a setuid().
1699 */
1700static int proc_fd_permission(struct inode *inode, int mask,
1701 struct nameidata *nd)
1702{
1703 int rv;
1704
1705 rv = generic_permission(inode, mask, NULL);
1706 if (rv == 0)
1707 return 0;
1708 if (task_pid(current) == proc_pid(inode))
1709 rv = 0;
1710 return rv;
1711}
1712
1713/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 * proc directories can do almost nothing..
1715 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001716static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001718 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001719 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720};
1721
Miklos Szeredi27932742007-05-08 00:26:17 -07001722static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1723 struct dentry *dentry, struct task_struct *task, const void *ptr)
1724{
1725 unsigned fd = *(unsigned *)ptr;
1726 struct inode *inode;
1727 struct proc_inode *ei;
1728 struct dentry *error = ERR_PTR(-ENOENT);
1729
1730 inode = proc_pid_make_inode(dir->i_sb, task);
1731 if (!inode)
1732 goto out;
1733 ei = PROC_I(inode);
1734 ei->fd = fd;
1735 inode->i_mode = S_IFREG | S_IRUSR;
1736 inode->i_fop = &proc_fdinfo_file_operations;
1737 dentry->d_op = &tid_fd_dentry_operations;
1738 d_add(dentry, inode);
1739 /* Close the race of the process dying before we return the dentry */
1740 if (tid_fd_revalidate(dentry, NULL))
1741 error = NULL;
1742
1743 out:
1744 return error;
1745}
1746
1747static struct dentry *proc_lookupfdinfo(struct inode *dir,
1748 struct dentry *dentry,
1749 struct nameidata *nd)
1750{
1751 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1752}
1753
1754static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1755{
1756 return proc_readfd_common(filp, dirent, filldir,
1757 proc_fdinfo_instantiate);
1758}
1759
1760static const struct file_operations proc_fdinfo_operations = {
1761 .read = generic_read_dir,
1762 .readdir = proc_readfdinfo,
1763};
1764
1765/*
1766 * proc directories can do almost nothing..
1767 */
1768static const struct inode_operations proc_fdinfo_inode_operations = {
1769 .lookup = proc_lookupfdinfo,
1770 .setattr = proc_setattr,
1771};
1772
1773
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001774static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001775 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001776{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001777 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001778 struct inode *inode;
1779 struct proc_inode *ei;
1780 struct dentry *error = ERR_PTR(-EINVAL);
1781
Eric W. Biederman61a28782006-10-02 02:18:49 -07001782 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001783 if (!inode)
1784 goto out;
1785
1786 ei = PROC_I(inode);
1787 inode->i_mode = p->mode;
1788 if (S_ISDIR(inode->i_mode))
1789 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1790 if (p->iop)
1791 inode->i_op = p->iop;
1792 if (p->fop)
1793 inode->i_fop = p->fop;
1794 ei->op = p->op;
1795 dentry->d_op = &pid_dentry_operations;
1796 d_add(dentry, inode);
1797 /* Close the race of the process dying before we return the dentry */
1798 if (pid_revalidate(dentry, NULL))
1799 error = NULL;
1800out:
1801 return error;
1802}
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804static struct dentry *proc_pident_lookup(struct inode *dir,
1805 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001806 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001807 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001810 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001811 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001812 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001814 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 inode = NULL;
1816
Eric W. Biederman99f89552006-06-26 00:25:55 -07001817 if (!task)
1818 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001820 /*
1821 * Yes, it does not scale. And it should not. Don't add
1822 * new entries into /proc/<tgid>/ without very good reasons.
1823 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001824 last = &ents[nents - 1];
1825 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (p->len != dentry->d_name.len)
1827 continue;
1828 if (!memcmp(dentry->d_name.name, p->name, p->len))
1829 break;
1830 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001831 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 goto out;
1833
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001834 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001836 put_task_struct(task);
1837out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001838 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
Eric Dumazetc5141e62007-05-08 00:26:15 -07001841static int proc_pident_fill_cache(struct file *filp, void *dirent,
1842 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001843{
1844 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1845 proc_pident_instantiate, task, p);
1846}
1847
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001848static int proc_pident_readdir(struct file *filp,
1849 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001850 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001851{
1852 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001853 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001854 struct inode *inode = dentry->d_inode;
1855 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001856 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001857 ino_t ino;
1858 int ret;
1859
1860 ret = -ENOENT;
1861 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001862 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001863
1864 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001865 i = filp->f_pos;
1866 switch (i) {
1867 case 0:
1868 ino = inode->i_ino;
1869 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1870 goto out;
1871 i++;
1872 filp->f_pos++;
1873 /* fall through */
1874 case 1:
1875 ino = parent_ino(dentry);
1876 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1877 goto out;
1878 i++;
1879 filp->f_pos++;
1880 /* fall through */
1881 default:
1882 i -= 2;
1883 if (i >= nents) {
1884 ret = 1;
1885 goto out;
1886 }
1887 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001888 last = &ents[nents - 1];
1889 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001890 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001891 goto out;
1892 filp->f_pos++;
1893 p++;
1894 }
1895 }
1896
1897 ret = 1;
1898out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001899 put_task_struct(task);
1900out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001905static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1906 size_t count, loff_t *ppos)
1907{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001908 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001909 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001910 ssize_t length;
1911 struct task_struct *task = get_proc_task(inode);
1912
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001913 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001914 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001915
1916 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001917 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001918 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001919 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001920 if (length > 0)
1921 length = simple_read_from_buffer(buf, count, ppos, p, length);
1922 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001923 return length;
1924}
1925
1926static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1927 size_t count, loff_t *ppos)
1928{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001929 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001930 char *page;
1931 ssize_t length;
1932 struct task_struct *task = get_proc_task(inode);
1933
1934 length = -ESRCH;
1935 if (!task)
1936 goto out_no_task;
1937 if (count > PAGE_SIZE)
1938 count = PAGE_SIZE;
1939
1940 /* No partial writes. */
1941 length = -EINVAL;
1942 if (*ppos != 0)
1943 goto out;
1944
1945 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001946 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001947 if (!page)
1948 goto out;
1949
1950 length = -EFAULT;
1951 if (copy_from_user(page, buf, count))
1952 goto out_free;
1953
1954 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001955 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001956 (void*)page, count);
1957out_free:
1958 free_page((unsigned long) page);
1959out:
1960 put_task_struct(task);
1961out_no_task:
1962 return length;
1963}
1964
Arjan van de Ven00977a52007-02-12 00:55:34 -08001965static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001966 .read = proc_pid_attr_read,
1967 .write = proc_pid_attr_write,
1968};
1969
Eric Dumazetc5141e62007-05-08 00:26:15 -07001970static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001971 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1972 REG("prev", S_IRUGO, pid_attr),
1973 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1974 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1975 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1976 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001977};
1978
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001979static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 void * dirent, filldir_t filldir)
1981{
1982 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001983 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
Arjan van de Ven00977a52007-02-12 00:55:34 -08001986static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001988 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989};
1990
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001991static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 struct dentry *dentry, struct nameidata *nd)
1993{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001994 return proc_pident_lookup(dir, dentry,
1995 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001998static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001999 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002000 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002001 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002};
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004#endif
2005
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002006#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2007static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2008 size_t count, loff_t *ppos)
2009{
2010 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2011 struct mm_struct *mm;
2012 char buffer[PROC_NUMBUF];
2013 size_t len;
2014 int ret;
2015
2016 if (!task)
2017 return -ESRCH;
2018
2019 ret = 0;
2020 mm = get_task_mm(task);
2021 if (mm) {
2022 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2023 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2024 MMF_DUMP_FILTER_SHIFT));
2025 mmput(mm);
2026 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2027 }
2028
2029 put_task_struct(task);
2030
2031 return ret;
2032}
2033
2034static ssize_t proc_coredump_filter_write(struct file *file,
2035 const char __user *buf,
2036 size_t count,
2037 loff_t *ppos)
2038{
2039 struct task_struct *task;
2040 struct mm_struct *mm;
2041 char buffer[PROC_NUMBUF], *end;
2042 unsigned int val;
2043 int ret;
2044 int i;
2045 unsigned long mask;
2046
2047 ret = -EFAULT;
2048 memset(buffer, 0, sizeof(buffer));
2049 if (count > sizeof(buffer) - 1)
2050 count = sizeof(buffer) - 1;
2051 if (copy_from_user(buffer, buf, count))
2052 goto out_no_task;
2053
2054 ret = -EINVAL;
2055 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2056 if (*end == '\n')
2057 end++;
2058 if (end - buffer == 0)
2059 goto out_no_task;
2060
2061 ret = -ESRCH;
2062 task = get_proc_task(file->f_dentry->d_inode);
2063 if (!task)
2064 goto out_no_task;
2065
2066 ret = end - buffer;
2067 mm = get_task_mm(task);
2068 if (!mm)
2069 goto out_no_mm;
2070
2071 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2072 if (val & mask)
2073 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2074 else
2075 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2076 }
2077
2078 mmput(mm);
2079 out_no_mm:
2080 put_task_struct(task);
2081 out_no_task:
2082 return ret;
2083}
2084
2085static const struct file_operations proc_coredump_filter_operations = {
2086 .read = proc_coredump_filter_read,
2087 .write = proc_coredump_filter_write,
2088};
2089#endif
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091/*
2092 * /proc/self:
2093 */
2094static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2095 int buflen)
2096{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002097 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002098 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002099 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002100 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002101 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002102 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return vfs_readlink(dentry,buffer,buflen,tmp);
2104}
2105
Al Viro008b1502005-08-20 00:17:39 +01002106static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002108 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002109 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002110 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002111 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002112 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002113 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002114 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002117static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 .readlink = proc_self_readlink,
2119 .follow_link = proc_self_follow_link,
2120};
2121
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002122/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002123 * proc base
2124 *
2125 * These are the directory entries in the root directory of /proc
2126 * that properly belong to the /proc filesystem, as they describe
2127 * describe something that is process related.
2128 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002129static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002130 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002131 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002132};
2133
2134/*
2135 * Exceptional case: normally we are not allowed to unhash a busy
2136 * directory. In this case, however, we can do it - no aliasing problems
2137 * due to the way we treat inodes.
2138 */
2139static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2140{
2141 struct inode *inode = dentry->d_inode;
2142 struct task_struct *task = get_proc_task(inode);
2143 if (task) {
2144 put_task_struct(task);
2145 return 1;
2146 }
2147 d_drop(dentry);
2148 return 0;
2149}
2150
2151static struct dentry_operations proc_base_dentry_operations =
2152{
2153 .d_revalidate = proc_base_revalidate,
2154 .d_delete = pid_delete_dentry,
2155};
2156
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002157static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002158 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002159{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002160 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002161 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002162 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002163 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002164
2165 /* Allocate the inode */
2166 error = ERR_PTR(-ENOMEM);
2167 inode = new_inode(dir->i_sb);
2168 if (!inode)
2169 goto out;
2170
2171 /* Initialize the inode */
2172 ei = PROC_I(inode);
2173 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002174
2175 /*
2176 * grab the reference to the task.
2177 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002178 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002179 if (!ei->pid)
2180 goto out_iput;
2181
2182 inode->i_uid = 0;
2183 inode->i_gid = 0;
2184 inode->i_mode = p->mode;
2185 if (S_ISDIR(inode->i_mode))
2186 inode->i_nlink = 2;
2187 if (S_ISLNK(inode->i_mode))
2188 inode->i_size = 64;
2189 if (p->iop)
2190 inode->i_op = p->iop;
2191 if (p->fop)
2192 inode->i_fop = p->fop;
2193 ei->op = p->op;
2194 dentry->d_op = &proc_base_dentry_operations;
2195 d_add(dentry, inode);
2196 error = NULL;
2197out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002198 return error;
2199out_iput:
2200 iput(inode);
2201 goto out;
2202}
2203
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002204static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2205{
2206 struct dentry *error;
2207 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002208 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002209
2210 error = ERR_PTR(-ENOENT);
2211
2212 if (!task)
2213 goto out_no_task;
2214
2215 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002216 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2217 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002218 if (p->len != dentry->d_name.len)
2219 continue;
2220 if (!memcmp(dentry->d_name.name, p->name, p->len))
2221 break;
2222 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002223 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002224 goto out;
2225
2226 error = proc_base_instantiate(dir, dentry, task, p);
2227
2228out:
2229 put_task_struct(task);
2230out_no_task:
2231 return error;
2232}
2233
Eric Dumazetc5141e62007-05-08 00:26:15 -07002234static int proc_base_fill_cache(struct file *filp, void *dirent,
2235 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002236{
2237 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2238 proc_base_instantiate, task, p);
2239}
2240
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002241#ifdef CONFIG_TASK_IO_ACCOUNTING
2242static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2243{
2244 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002245#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002246 "rchar: %llu\n"
2247 "wchar: %llu\n"
2248 "syscr: %llu\n"
2249 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002250#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002251 "read_bytes: %llu\n"
2252 "write_bytes: %llu\n"
2253 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002254#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002255 (unsigned long long)task->rchar,
2256 (unsigned long long)task->wchar,
2257 (unsigned long long)task->syscr,
2258 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002259#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002260 (unsigned long long)task->ioac.read_bytes,
2261 (unsigned long long)task->ioac.write_bytes,
2262 (unsigned long long)task->ioac.cancelled_write_bytes);
2263}
2264#endif
2265
Eric W. Biederman801199c2006-10-02 02:18:48 -07002266/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002267 * Thread groups
2268 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002269static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002270static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002271
Eric Dumazetc5141e62007-05-08 00:26:15 -07002272static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002273 DIR("task", S_IRUGO|S_IXUGO, task),
2274 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002275 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002276 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002277 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002278 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002279 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002280#ifdef CONFIG_SCHED_DEBUG
2281 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2282#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002283 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002284 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002285 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002286 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002287#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002288 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002289#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002290 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002291 LNK("cwd", cwd),
2292 LNK("root", root),
2293 LNK("exe", exe),
2294 REG("mounts", S_IRUGO, mounts),
2295 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002296#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002297 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002298 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002299 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002300#endif
2301#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002302 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002303#endif
2304#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002305 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002306#endif
2307#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002308 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002309#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002310#ifdef CONFIG_LATENCYTOP
2311 REG("latency", S_IRUGO, lstats),
2312#endif
Paul Menage8793d852007-10-18 23:39:39 -07002313#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002314 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002315#endif
Paul Menagea4243162007-10-18 23:39:35 -07002316#ifdef CONFIG_CGROUPS
2317 REG("cgroup", S_IRUGO, cgroup),
2318#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002319 INF("oom_score", S_IRUGO, oom_score),
2320 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002321#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002322 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002323#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002324#ifdef CONFIG_FAULT_INJECTION
2325 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2326#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002327#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2328 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2329#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002330#ifdef CONFIG_TASK_IO_ACCOUNTING
2331 INF("io", S_IRUGO, pid_io_accounting),
2332#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002333};
2334
2335static int proc_tgid_base_readdir(struct file * filp,
2336 void * dirent, filldir_t filldir)
2337{
2338 return proc_pident_readdir(filp,dirent,filldir,
2339 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2340}
2341
Arjan van de Ven00977a52007-02-12 00:55:34 -08002342static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002343 .read = generic_read_dir,
2344 .readdir = proc_tgid_base_readdir,
2345};
2346
2347static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002348 return proc_pident_lookup(dir, dentry,
2349 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002350}
2351
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002352static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002353 .lookup = proc_tgid_base_lookup,
2354 .getattr = pid_getattr,
2355 .setattr = proc_setattr,
2356};
2357
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002358static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002360 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002361 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002362 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Eric W. Biederman48e64842006-06-26 00:25:48 -07002364 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002365 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2366 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002367 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002368 if (!(current->flags & PF_EXITING))
2369 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002370 d_drop(dentry);
2371 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002374 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002375 goto out;
2376
2377 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002378 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2379 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002380 if (!leader)
2381 goto out;
2382
2383 name.name = "task";
2384 name.len = strlen(name.name);
2385 dir = d_hash_and_lookup(leader, &name);
2386 if (!dir)
2387 goto out_put_leader;
2388
2389 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002390 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002391 dentry = d_hash_and_lookup(dir, &name);
2392 if (dentry) {
2393 shrink_dcache_parent(dentry);
2394 d_drop(dentry);
2395 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002397
2398 dput(dir);
2399out_put_leader:
2400 dput(leader);
2401out:
2402 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404
Randy Dunlap0895e912007-10-21 21:00:10 -07002405/**
2406 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2407 * @task: task that should be flushed.
2408 *
2409 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002410 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002411 * in. This call is supposed to do all of this job.
2412 *
2413 * Looks in the dcache for
2414 * /proc/@pid
2415 * /proc/@tgid/task/@pid
2416 * if either directory is present flushes it and all of it'ts children
2417 * from the dcache.
2418 *
2419 * It is safe and reasonable to cache /proc entries for a task until
2420 * that task exits. After that they just clog up the dcache with
2421 * useless entries, possibly causing useful dcache entries to be
2422 * flushed instead. This routine is proved to flush those useless
2423 * dcache entries at process exit time.
2424 *
2425 * NOTE: This routine is just an optimization so it does not guarantee
2426 * that no dcache entries will exist at process exit time it
2427 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002428 */
2429
2430void proc_flush_task(struct task_struct *task)
2431{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002432 int i;
2433 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002434 struct upid *upid;
2435
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002436 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002437 if (thread_group_leader(task))
2438 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002439
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002440 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002441 upid = &pid->numbers[i];
2442 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002443 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002444 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002445
2446 upid = &pid->numbers[pid->level];
2447 if (upid->nr == 1)
2448 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002449}
2450
Adrian Bunk9711ef92006-12-06 20:38:31 -08002451static struct dentry *proc_pid_instantiate(struct inode *dir,
2452 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002453 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002454{
2455 struct dentry *error = ERR_PTR(-ENOENT);
2456 struct inode *inode;
2457
Eric W. Biederman61a28782006-10-02 02:18:49 -07002458 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002459 if (!inode)
2460 goto out;
2461
2462 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2463 inode->i_op = &proc_tgid_base_inode_operations;
2464 inode->i_fop = &proc_tgid_base_operations;
2465 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002466 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002467#ifdef CONFIG_SECURITY
2468 inode->i_nlink += 1;
2469#endif
2470
2471 dentry->d_op = &pid_dentry_operations;
2472
2473 d_add(dentry, inode);
2474 /* Close the race of the process dying before we return the dentry */
2475 if (pid_revalidate(dentry, NULL))
2476 error = NULL;
2477out:
2478 return error;
2479}
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2482{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002483 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002486 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Eric W. Biederman801199c2006-10-02 02:18:48 -07002488 result = proc_base_lookup(dir, dentry);
2489 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2490 goto out;
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 tgid = name_to_int(dentry);
2493 if (tgid == ~0U)
2494 goto out;
2495
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002496 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002497 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002498 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (task)
2500 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002501 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (!task)
2503 goto out;
2504
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002505 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002506 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002508 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002512 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002513 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002515struct tgid_iter {
2516 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002517 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002518};
2519static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2520{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002521 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002523 if (iter.task)
2524 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002525 rcu_read_lock();
2526retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002527 iter.task = NULL;
2528 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002529 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002530 iter.tgid = pid_nr_ns(pid, ns);
2531 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002532 /* What we to know is if the pid we have find is the
2533 * pid of a thread_group_leader. Testing for task
2534 * being a thread_group_leader is the obvious thing
2535 * todo but there is a window when it fails, due to
2536 * the pid transfer logic in de_thread.
2537 *
2538 * So we perform the straight forward test of seeing
2539 * if the pid we have found is the pid of a thread
2540 * group leader, and don't worry if the task we have
2541 * found doesn't happen to be a thread group leader.
2542 * As we don't care in the case of readdir.
2543 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002544 if (!iter.task || !has_group_leader_pid(iter.task)) {
2545 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002546 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002547 }
2548 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002550 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002551 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002554#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Eric W. Biederman61a28782006-10-02 02:18:49 -07002556static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002557 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002558{
2559 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002560 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002561 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002562 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002563}
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565/* for the /proc/ directory itself, after non-process stuff has been done */
2566int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2567{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002569 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002570 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002571 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Eric W. Biederman61a28782006-10-02 02:18:49 -07002573 if (!reaper)
2574 goto out_no_task;
2575
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002576 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002577 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002578 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002579 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002582 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002583 iter.task = NULL;
2584 iter.tgid = filp->f_pos - TGID_OFFSET;
2585 for (iter = next_tgid(ns, iter);
2586 iter.task;
2587 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2588 filp->f_pos = iter.tgid + TGID_OFFSET;
2589 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2590 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002594 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2595out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002596 put_task_struct(reaper);
2597out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return 0;
2599}
2600
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002601/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002602 * Tasks
2603 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002604static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002605 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002606 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002607 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002608 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002609 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002610 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002611#ifdef CONFIG_SCHED_DEBUG
2612 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2613#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002614 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002615 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002616 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002617 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002618#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002619 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002620#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002621 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002622 LNK("cwd", cwd),
2623 LNK("root", root),
2624 LNK("exe", exe),
2625 REG("mounts", S_IRUGO, mounts),
Matt Mackall1e883282008-02-04 22:29:07 -08002626#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002627 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002628 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002629 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002630#endif
2631#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002632 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002633#endif
2634#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002635 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002636#endif
2637#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002638 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002639#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002640#ifdef CONFIG_LATENCYTOP
2641 REG("latency", S_IRUGO, lstats),
2642#endif
Paul Menage8793d852007-10-18 23:39:39 -07002643#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002644 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002645#endif
Paul Menagea4243162007-10-18 23:39:35 -07002646#ifdef CONFIG_CGROUPS
2647 REG("cgroup", S_IRUGO, cgroup),
2648#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002649 INF("oom_score", S_IRUGO, oom_score),
2650 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002651#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002652 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002653#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002654#ifdef CONFIG_FAULT_INJECTION
2655 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2656#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657};
2658
2659static int proc_tid_base_readdir(struct file * filp,
2660 void * dirent, filldir_t filldir)
2661{
2662 return proc_pident_readdir(filp,dirent,filldir,
2663 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2664}
2665
2666static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002667 return proc_pident_lookup(dir, dentry,
2668 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002669}
2670
Arjan van de Ven00977a52007-02-12 00:55:34 -08002671static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002672 .read = generic_read_dir,
2673 .readdir = proc_tid_base_readdir,
2674};
2675
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002676static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002677 .lookup = proc_tid_base_lookup,
2678 .getattr = pid_getattr,
2679 .setattr = proc_setattr,
2680};
2681
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002682static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002683 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002684{
2685 struct dentry *error = ERR_PTR(-ENOENT);
2686 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002687 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002688
2689 if (!inode)
2690 goto out;
2691 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2692 inode->i_op = &proc_tid_base_inode_operations;
2693 inode->i_fop = &proc_tid_base_operations;
2694 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002695 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002696#ifdef CONFIG_SECURITY
2697 inode->i_nlink += 1;
2698#endif
2699
2700 dentry->d_op = &pid_dentry_operations;
2701
2702 d_add(dentry, inode);
2703 /* Close the race of the process dying before we return the dentry */
2704 if (pid_revalidate(dentry, NULL))
2705 error = NULL;
2706out:
2707 return error;
2708}
2709
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002710static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2711{
2712 struct dentry *result = ERR_PTR(-ENOENT);
2713 struct task_struct *task;
2714 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002715 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002716 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002717
2718 if (!leader)
2719 goto out_no_task;
2720
2721 tid = name_to_int(dentry);
2722 if (tid == ~0U)
2723 goto out;
2724
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002725 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002726 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002727 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002728 if (task)
2729 get_task_struct(task);
2730 rcu_read_unlock();
2731 if (!task)
2732 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002733 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002734 goto out_drop_task;
2735
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002736 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002737out_drop_task:
2738 put_task_struct(task);
2739out:
2740 put_task_struct(leader);
2741out_no_task:
2742 return result;
2743}
2744
2745/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002746 * Find the first tid of a thread group to return to user space.
2747 *
2748 * Usually this is just the thread group leader, but if the users
2749 * buffer was too small or there was a seek into the middle of the
2750 * directory we have more work todo.
2751 *
2752 * In the case of a short read we start with find_task_by_pid.
2753 *
2754 * In the case of a seek we start with the leader and walk nr
2755 * threads past it.
2756 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002757static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002758 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002759{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002760 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002761
Eric W. Biedermancc288732006-06-26 00:26:01 -07002762 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002763 /* Attempt to start with the pid of a thread */
2764 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002765 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002766 if (pos && (pos->group_leader == leader))
2767 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002768 }
2769
2770 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002771 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002772 if (nr && nr >= get_nr_threads(leader))
2773 goto out;
2774
2775 /* If we haven't found our starting place yet start
2776 * with the leader and walk nr threads forward.
2777 */
2778 for (pos = leader; nr > 0; --nr) {
2779 pos = next_thread(pos);
2780 if (pos == leader) {
2781 pos = NULL;
2782 goto out;
2783 }
2784 }
2785found:
2786 get_task_struct(pos);
2787out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002788 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002789 return pos;
2790}
2791
2792/*
2793 * Find the next thread in the thread list.
2794 * Return NULL if there is an error or no next thread.
2795 *
2796 * The reference to the input task_struct is released.
2797 */
2798static struct task_struct *next_tid(struct task_struct *start)
2799{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002800 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002801 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002802 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002803 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002804 if (thread_group_leader(pos))
2805 pos = NULL;
2806 else
2807 get_task_struct(pos);
2808 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002809 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002810 put_task_struct(start);
2811 return pos;
2812}
2813
Eric W. Biederman61a28782006-10-02 02:18:49 -07002814static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2815 struct task_struct *task, int tid)
2816{
2817 char name[PROC_NUMBUF];
2818 int len = snprintf(name, sizeof(name), "%d", tid);
2819 return proc_fill_cache(filp, dirent, filldir, name, len,
2820 proc_task_instantiate, task, NULL);
2821}
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823/* for the /proc/TGID/task/ directories */
2824static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2825{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002826 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002828 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002829 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 int retval = -ENOENT;
2831 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002832 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002834 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002836 task = get_proc_task(inode);
2837 if (!task)
2838 goto out_no_task;
2839 rcu_read_lock();
2840 if (pid_alive(task)) {
2841 leader = task->group_leader;
2842 get_task_struct(leader);
2843 }
2844 rcu_read_unlock();
2845 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002846 if (!leader)
2847 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 retval = 0;
2849
2850 switch (pos) {
2851 case 0:
2852 ino = inode->i_ino;
2853 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2854 goto out;
2855 pos++;
2856 /* fall through */
2857 case 1:
2858 ino = parent_ino(dentry);
2859 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2860 goto out;
2861 pos++;
2862 /* fall through */
2863 }
2864
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002865 /* f_version caches the tgid value that the last readdir call couldn't
2866 * return. lseek aka telldir automagically resets f_version to 0.
2867 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002868 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002869 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002870 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002871 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002872 task;
2873 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002874 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002875 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002876 /* returning this tgid failed, save it as the first
2877 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002878 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002879 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
2883out:
2884 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002885 put_task_struct(leader);
2886out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return retval;
2888}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002889
2890static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2891{
2892 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002893 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002894 generic_fillattr(inode, stat);
2895
Eric W. Biederman99f89552006-06-26 00:25:55 -07002896 if (p) {
2897 rcu_read_lock();
2898 stat->nlink += get_nr_threads(p);
2899 rcu_read_unlock();
2900 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002901 }
2902
2903 return 0;
2904}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002905
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002906static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002907 .lookup = proc_task_lookup,
2908 .getattr = proc_task_getattr,
2909 .setattr = proc_setattr,
2910};
2911
Arjan van de Ven00977a52007-02-12 00:55:34 -08002912static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002913 .read = generic_read_dir,
2914 .readdir = proc_task_readdir,
2915};