blob: ab1b439923e6c622d1627486e9dae80aa0ae9cd5 [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>
64#include <linux/smp_lock.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070065#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kallsyms.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>
71#include <linux/seccomp.h>
72#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include "internal.h"
78
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070079/* NOTE:
80 * Implementing inode permission operations in /proc is almost
81 * certainly an error. Permission checks need to happen during
82 * each system call not at open time. The reason is that most of
83 * what we wish to check for permissions in /proc varies at runtime.
84 *
85 * The classic example of a problem is opening file descriptors
86 * in /proc for a task before it execs a suid executable.
87 */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Eric W. Biederman8578cea2006-06-26 00:25:54 -070090/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070091#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070095 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080097 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080098 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070099 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Eric W. Biederman61a28782006-10-02 02:18:49 -0700102#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700104 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700105 .mode = MODE, \
106 .iop = IOP, \
107 .fop = FOP, \
108 .op = OP, \
109}
110
Eric W. Biederman61a28782006-10-02 02:18:49 -0700111#define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700115#define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119#define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700121 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700122#define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Kees Cook5096add2007-05-08 00:26:04 -0700127int maps_protect;
128EXPORT_SYMBOL(maps_protect);
129
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700130static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700133 task_lock(task);
134 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if(fs)
136 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700137 task_unlock(task);
138 return fs;
139}
140
Eric W. Biederman99f89552006-06-26 00:25:55 -0700141static int get_nr_threads(struct task_struct *tsk)
142{
143 /* Must be called with the rcu_read_lock held */
144 unsigned long flags;
145 int count = 0;
146
147 if (lock_task_sighand(tsk, &flags)) {
148 count = atomic_read(&tsk->signal->count);
149 unlock_task_sighand(tsk, &flags);
150 }
151 return count;
152}
153
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700154static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
155{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700156 struct task_struct *task = get_proc_task(inode);
157 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700158 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700159
160 if (task) {
161 fs = get_fs_struct(task);
162 put_task_struct(task);
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (fs) {
165 read_lock(&fs->lock);
166 *mnt = mntget(fs->pwdmnt);
167 *dentry = dget(fs->pwd);
168 read_unlock(&fs->lock);
169 result = 0;
170 put_fs_struct(fs);
171 }
172 return result;
173}
174
175static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
176{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177 struct task_struct *task = get_proc_task(inode);
178 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700180
181 if (task) {
182 fs = get_fs_struct(task);
183 put_task_struct(task);
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (fs) {
186 read_lock(&fs->lock);
187 *mnt = mntget(fs->rootmnt);
188 *dentry = dget(fs->root);
189 read_unlock(&fs->lock);
190 result = 0;
191 put_fs_struct(fs);
192 }
193 return result;
194}
195
196#define MAY_PTRACE(task) \
197 (task == current || \
198 (task->parent == current && \
199 (task->ptrace & PT_PTRACED) && \
200 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
201 security_ptrace(current,task) == 0))
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static int proc_pid_environ(struct task_struct *task, char * buffer)
204{
205 int res = 0;
206 struct mm_struct *mm = get_task_mm(task);
207 if (mm) {
208 unsigned int len = mm->env_end - mm->env_start;
209 if (len > PAGE_SIZE)
210 len = PAGE_SIZE;
211 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700212 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 res = -ESRCH;
214 mmput(mm);
215 }
216 return res;
217}
218
219static int proc_pid_cmdline(struct task_struct *task, char * buffer)
220{
221 int res = 0;
222 unsigned int len;
223 struct mm_struct *mm = get_task_mm(task);
224 if (!mm)
225 goto out;
226 if (!mm->arg_end)
227 goto out_mm; /* Shh! No looking before we're done */
228
229 len = mm->arg_end - mm->arg_start;
230
231 if (len > PAGE_SIZE)
232 len = PAGE_SIZE;
233
234 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
235
236 // If the nul at the end of args has been overwritten, then
237 // assume application is using setproctitle(3).
238 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
239 len = strnlen(buffer, res);
240 if (len < res) {
241 res = len;
242 } else {
243 len = mm->env_end - mm->env_start;
244 if (len > PAGE_SIZE - res)
245 len = PAGE_SIZE - res;
246 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
247 res = strnlen(buffer, res);
248 }
249 }
250out_mm:
251 mmput(mm);
252out:
253 return res;
254}
255
256static int proc_pid_auxv(struct task_struct *task, char *buffer)
257{
258 int res = 0;
259 struct mm_struct *mm = get_task_mm(task);
260 if (mm) {
261 unsigned int nwords = 0;
262 do
263 nwords += 2;
264 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
265 res = nwords * sizeof(mm->saved_auxv[0]);
266 if (res > PAGE_SIZE)
267 res = PAGE_SIZE;
268 memcpy(buffer, mm->saved_auxv, res);
269 mmput(mm);
270 }
271 return res;
272}
273
274
275#ifdef CONFIG_KALLSYMS
276/*
277 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
278 * Returns the resolved symbol. If that fails, simply return the address.
279 */
280static int proc_pid_wchan(struct task_struct *task, char *buffer)
281{
282 char *modname;
283 const char *sym_name;
284 unsigned long wchan, size, offset;
285 char namebuf[KSYM_NAME_LEN+1];
286
287 wchan = get_wchan(task);
288
289 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
290 if (sym_name)
291 return sprintf(buffer, "%s", sym_name);
292 return sprintf(buffer, "%lu", wchan);
293}
294#endif /* CONFIG_KALLSYMS */
295
296#ifdef CONFIG_SCHEDSTATS
297/*
298 * Provides /proc/PID/schedstat
299 */
300static int proc_pid_schedstat(struct task_struct *task, char *buffer)
301{
302 return sprintf(buffer, "%lu %lu %lu\n",
303 task->sched_info.cpu_time,
304 task->sched_info.run_delay,
305 task->sched_info.pcnt);
306}
307#endif
308
309/* The badness from the OOM killer */
310unsigned long badness(struct task_struct *p, unsigned long uptime);
311static int proc_oom_score(struct task_struct *task, char *buffer)
312{
313 unsigned long points;
314 struct timespec uptime;
315
316 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700317 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700319 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return sprintf(buffer, "%lu\n", points);
321}
322
323/************************************************************************/
324/* Here the fs part begins */
325/************************************************************************/
326
327/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700328static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700330 struct task_struct *task;
331 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700332 /* Allow access to a task's file descriptors if it is us or we
333 * may use ptrace attach to the process and find out that
334 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700335 */
336 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700337 if (task) {
338 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700339 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700340 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700341 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700344static int proc_setattr(struct dentry *dentry, struct iattr *attr)
345{
346 int error;
347 struct inode *inode = dentry->d_inode;
348
349 if (attr->ia_valid & ATTR_MODE)
350 return -EPERM;
351
352 error = inode_change_ok(inode, attr);
353 if (!error) {
354 error = security_inode_setattr(dentry, attr);
355 if (!error)
356 error = inode_setattr(inode, attr);
357 }
358 return error;
359}
360
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800361static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700362 .setattr = proc_setattr,
363};
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500366struct proc_mounts {
367 struct seq_file m;
368 int event;
369};
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static int mounts_open(struct inode *inode, struct file *file)
372{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700373 struct task_struct *task = get_proc_task(inode);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800374 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500375 struct proc_mounts *p;
376 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Eric W. Biederman99f89552006-06-26 00:25:55 -0700378 if (task) {
379 task_lock(task);
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800380 if (task->nsproxy) {
381 ns = task->nsproxy->mnt_ns;
382 if (ns)
383 get_mnt_ns(ns);
384 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700385 task_unlock(task);
386 put_task_struct(task);
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800389 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500390 ret = -ENOMEM;
391 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
392 if (p) {
393 file->private_data = &p->m;
394 ret = seq_open(file, &mounts_op);
395 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800396 p->m.private = ns;
397 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500398 return 0;
399 }
400 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800402 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 return ret;
405}
406
407static int mounts_release(struct inode *inode, struct file *file)
408{
409 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800410 struct mnt_namespace *ns = m->private;
411 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return seq_release(inode, file);
413}
414
Al Viro5addc5d2005-11-07 17:15:49 -0500415static unsigned mounts_poll(struct file *file, poll_table *wait)
416{
417 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800418 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500419 unsigned res = 0;
420
421 poll_wait(file, &ns->poll, wait);
422
423 spin_lock(&vfsmount_lock);
424 if (p->event != ns->event) {
425 p->event = ns->event;
426 res = POLLERR;
427 }
428 spin_unlock(&vfsmount_lock);
429
430 return res;
431}
432
Arjan van de Ven00977a52007-02-12 00:55:34 -0800433static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .open = mounts_open,
435 .read = seq_read,
436 .llseek = seq_lseek,
437 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500438 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439};
440
Chuck Leverb4629fe2006-03-20 13:44:12 -0500441extern struct seq_operations mountstats_op;
442static int mountstats_open(struct inode *inode, struct file *file)
443{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500444 int ret = seq_open(file, &mountstats_op);
445
446 if (!ret) {
447 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800448 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700449 struct task_struct *task = get_proc_task(inode);
450
451 if (task) {
452 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800453 if (task->nsproxy)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800454 mnt_ns = task->nsproxy->mnt_ns;
455 if (mnt_ns)
456 get_mnt_ns(mnt_ns);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700457 task_unlock(task);
458 put_task_struct(task);
459 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500460
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800461 if (mnt_ns)
462 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500463 else {
464 seq_release(inode, file);
465 ret = -EINVAL;
466 }
467 }
468 return ret;
469}
470
Arjan van de Ven00977a52007-02-12 00:55:34 -0800471static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500472 .open = mountstats_open,
473 .read = seq_read,
474 .llseek = seq_lseek,
475 .release = mounts_release,
476};
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
479
480static ssize_t proc_info_read(struct file * file, char __user * buf,
481 size_t count, loff_t *ppos)
482{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800483 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 unsigned long page;
485 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700486 struct task_struct *task = get_proc_task(inode);
487
488 length = -ESRCH;
489 if (!task)
490 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (count > PROC_BLOCK_SIZE)
493 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700494
495 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700497 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 length = PROC_I(inode)->op.proc_read(task, (char*)page);
500
501 if (length >= 0)
502 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
503 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700504out:
505 put_task_struct(task);
506out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return length;
508}
509
Arjan van de Ven00977a52007-02-12 00:55:34 -0800510static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .read = proc_info_read,
512};
513
514static int mem_open(struct inode* inode, struct file* file)
515{
516 file->private_data = (void*)((long)current->self_exec_id);
517 return 0;
518}
519
520static ssize_t mem_read(struct file * file, char __user * buf,
521 size_t count, loff_t *ppos)
522{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800523 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 char *page;
525 unsigned long src = *ppos;
526 int ret = -ESRCH;
527 struct mm_struct *mm;
528
Eric W. Biederman99f89552006-06-26 00:25:55 -0700529 if (!task)
530 goto out_no_task;
531
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700532 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto out;
534
535 ret = -ENOMEM;
536 page = (char *)__get_free_page(GFP_USER);
537 if (!page)
538 goto out;
539
540 ret = 0;
541
542 mm = get_task_mm(task);
543 if (!mm)
544 goto out_free;
545
546 ret = -EIO;
547
548 if (file->private_data != (void*)((long)current->self_exec_id))
549 goto out_put;
550
551 ret = 0;
552
553 while (count > 0) {
554 int this_len, retval;
555
556 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
557 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700558 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!ret)
560 ret = -EIO;
561 break;
562 }
563
564 if (copy_to_user(buf, page, retval)) {
565 ret = -EFAULT;
566 break;
567 }
568
569 ret += retval;
570 src += retval;
571 buf += retval;
572 count -= retval;
573 }
574 *ppos = src;
575
576out_put:
577 mmput(mm);
578out_free:
579 free_page((unsigned long) page);
580out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700581 put_task_struct(task);
582out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return ret;
584}
585
586#define mem_write NULL
587
588#ifndef mem_write
589/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800590static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 size_t count, loff_t *ppos)
592{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700593 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800595 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 unsigned long dst = *ppos;
597
Eric W. Biederman99f89552006-06-26 00:25:55 -0700598 copied = -ESRCH;
599 if (!task)
600 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Eric W. Biederman99f89552006-06-26 00:25:55 -0700602 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
603 goto out;
604
605 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 page = (char *)__get_free_page(GFP_USER);
607 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700608 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700610 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 while (count > 0) {
612 int this_len, retval;
613
614 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
615 if (copy_from_user(page, buf, this_len)) {
616 copied = -EFAULT;
617 break;
618 }
619 retval = access_process_vm(task, dst, page, this_len, 1);
620 if (!retval) {
621 if (!copied)
622 copied = -EIO;
623 break;
624 }
625 copied += retval;
626 buf += retval;
627 dst += retval;
628 count -= retval;
629 }
630 *ppos = dst;
631 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700632out:
633 put_task_struct(task);
634out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return copied;
636}
637#endif
638
639static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
640{
641 switch (orig) {
642 case 0:
643 file->f_pos = offset;
644 break;
645 case 1:
646 file->f_pos += offset;
647 break;
648 default:
649 return -EINVAL;
650 }
651 force_successful_syscall_return();
652 return file->f_pos;
653}
654
Arjan van de Ven00977a52007-02-12 00:55:34 -0800655static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 .llseek = mem_lseek,
657 .read = mem_read,
658 .write = mem_write,
659 .open = mem_open,
660};
661
662static ssize_t oom_adjust_read(struct file *file, char __user *buf,
663 size_t count, loff_t *ppos)
664{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800665 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700666 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700668 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 loff_t __ppos = *ppos;
670
Eric W. Biederman99f89552006-06-26 00:25:55 -0700671 if (!task)
672 return -ESRCH;
673 oom_adjust = task->oomkilladj;
674 put_task_struct(task);
675
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700676 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (__ppos >= len)
678 return 0;
679 if (count > len-__ppos)
680 count = len-__ppos;
681 if (copy_to_user(buf, buffer + __ppos, count))
682 return -EFAULT;
683 *ppos = __ppos + count;
684 return count;
685}
686
687static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
688 size_t count, loff_t *ppos)
689{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700690 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700691 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int oom_adjust;
693
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700694 memset(buffer, 0, sizeof(buffer));
695 if (count > sizeof(buffer) - 1)
696 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (copy_from_user(buffer, buf, count))
698 return -EFAULT;
699 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700700 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
701 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EINVAL;
703 if (*end == '\n')
704 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800705 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700706 if (!task)
707 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800708 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
709 put_task_struct(task);
710 return -EACCES;
711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700713 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (end - buffer == 0)
715 return -EIO;
716 return end - buffer;
717}
718
Arjan van de Ven00977a52007-02-12 00:55:34 -0800719static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 .read = oom_adjust_read,
721 .write = oom_adjust_write,
722};
723
David Rientjesb813e932007-05-06 14:49:24 -0700724static ssize_t clear_refs_write(struct file *file, const char __user *buf,
725 size_t count, loff_t *ppos)
726{
727 struct task_struct *task;
728 char buffer[PROC_NUMBUF], *end;
729 struct mm_struct *mm;
730
731 memset(buffer, 0, sizeof(buffer));
732 if (count > sizeof(buffer) - 1)
733 count = sizeof(buffer) - 1;
734 if (copy_from_user(buffer, buf, count))
735 return -EFAULT;
736 if (!simple_strtol(buffer, &end, 0))
737 return -EINVAL;
738 if (*end == '\n')
739 end++;
740 task = get_proc_task(file->f_path.dentry->d_inode);
741 if (!task)
742 return -ESRCH;
743 mm = get_task_mm(task);
744 if (mm) {
745 clear_refs_smap(mm);
746 mmput(mm);
747 }
748 put_task_struct(task);
749 if (end - buffer == 0)
750 return -EIO;
751 return end - buffer;
752}
753
754static struct file_operations proc_clear_refs_operations = {
755 .write = clear_refs_write,
756};
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#ifdef CONFIG_AUDITSYSCALL
759#define TMPBUFLEN 21
760static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
761 size_t count, loff_t *ppos)
762{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800763 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700764 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 ssize_t length;
766 char tmpbuf[TMPBUFLEN];
767
Eric W. Biederman99f89552006-06-26 00:25:55 -0700768 if (!task)
769 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
771 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700772 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
774}
775
776static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
777 size_t count, loff_t *ppos)
778{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800779 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 char *page, *tmp;
781 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 uid_t loginuid;
783
784 if (!capable(CAP_AUDIT_CONTROL))
785 return -EPERM;
786
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700787 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -EPERM;
789
Al Viroe0182902006-05-18 08:28:02 -0400790 if (count >= PAGE_SIZE)
791 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (*ppos != 0) {
794 /* No partial writes. */
795 return -EINVAL;
796 }
797 page = (char*)__get_free_page(GFP_USER);
798 if (!page)
799 return -ENOMEM;
800 length = -EFAULT;
801 if (copy_from_user(page, buf, count))
802 goto out_free_page;
803
Al Viroe0182902006-05-18 08:28:02 -0400804 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 loginuid = simple_strtoul(page, &tmp, 10);
806 if (tmp == page) {
807 length = -EINVAL;
808 goto out_free_page;
809
810 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700811 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (likely(length == 0))
813 length = count;
814
815out_free_page:
816 free_page((unsigned long) page);
817 return length;
818}
819
Arjan van de Ven00977a52007-02-12 00:55:34 -0800820static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 .read = proc_loginuid_read,
822 .write = proc_loginuid_write,
823};
824#endif
825
826#ifdef CONFIG_SECCOMP
827static ssize_t seccomp_read(struct file *file, char __user *buf,
828 size_t count, loff_t *ppos)
829{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700830 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 char __buf[20];
832 loff_t __ppos = *ppos;
833 size_t len;
834
Eric W. Biederman99f89552006-06-26 00:25:55 -0700835 if (!tsk)
836 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* no need to print the trailing zero, so use only len */
838 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700839 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (__ppos >= len)
841 return 0;
842 if (count > len - __ppos)
843 count = len - __ppos;
844 if (copy_to_user(buf, __buf + __ppos, count))
845 return -EFAULT;
846 *ppos = __ppos + count;
847 return count;
848}
849
850static ssize_t seccomp_write(struct file *file, const char __user *buf,
851 size_t count, loff_t *ppos)
852{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700853 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 char __buf[20], *end;
855 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700856 ssize_t result;
857
858 result = -ESRCH;
859 if (!tsk)
860 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700863 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700865 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Eric W. Biederman99f89552006-06-26 00:25:55 -0700867 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 memset(__buf, 0, sizeof(__buf));
869 count = min(count, sizeof(__buf) - 1);
870 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700871 goto out;
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 seccomp_mode = simple_strtoul(__buf, &end, 0);
874 if (*end == '\n')
875 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700876 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
878 tsk->seccomp.mode = seccomp_mode;
879 set_tsk_thread_flag(tsk, TIF_SECCOMP);
880 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700881 goto out;
882 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700884 goto out;
885 result = end - __buf;
886out:
887 put_task_struct(tsk);
888out_no_task:
889 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Arjan van de Ven00977a52007-02-12 00:55:34 -0800892static const struct file_operations proc_seccomp_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 .read = seccomp_read,
894 .write = seccomp_write,
895};
896#endif /* CONFIG_SECCOMP */
897
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800898#ifdef CONFIG_FAULT_INJECTION
899static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
900 size_t count, loff_t *ppos)
901{
902 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
903 char buffer[PROC_NUMBUF];
904 size_t len;
905 int make_it_fail;
906 loff_t __ppos = *ppos;
907
908 if (!task)
909 return -ESRCH;
910 make_it_fail = task->make_it_fail;
911 put_task_struct(task);
912
913 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
914 if (__ppos >= len)
915 return 0;
916 if (count > len-__ppos)
917 count = len-__ppos;
918 if (copy_to_user(buf, buffer + __ppos, count))
919 return -EFAULT;
920 *ppos = __ppos + count;
921 return count;
922}
923
924static ssize_t proc_fault_inject_write(struct file * file,
925 const char __user * buf, size_t count, loff_t *ppos)
926{
927 struct task_struct *task;
928 char buffer[PROC_NUMBUF], *end;
929 int make_it_fail;
930
931 if (!capable(CAP_SYS_RESOURCE))
932 return -EPERM;
933 memset(buffer, 0, sizeof(buffer));
934 if (count > sizeof(buffer) - 1)
935 count = sizeof(buffer) - 1;
936 if (copy_from_user(buffer, buf, count))
937 return -EFAULT;
938 make_it_fail = simple_strtol(buffer, &end, 0);
939 if (*end == '\n')
940 end++;
941 task = get_proc_task(file->f_dentry->d_inode);
942 if (!task)
943 return -ESRCH;
944 task->make_it_fail = make_it_fail;
945 put_task_struct(task);
946 if (end - buffer == 0)
947 return -EIO;
948 return end - buffer;
949}
950
Arjan van de Ven00977a52007-02-12 00:55:34 -0800951static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800952 .read = proc_fault_inject_read,
953 .write = proc_fault_inject_write,
954};
955#endif
956
Al Viro008b1502005-08-20 00:17:39 +0100957static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 struct inode *inode = dentry->d_inode;
960 int error = -EACCES;
961
962 /* We don't need a base pointer in the /proc filesystem */
963 path_release(nd);
964
Eric W. Biederman778c1142006-06-26 00:25:58 -0700965 /* Are we allowed to snoop on the tasks file descriptors? */
966 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
970 nd->last_type = LAST_BIND;
971out:
Al Viro008b1502005-08-20 00:17:39 +0100972 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
976 char __user *buffer, int buflen)
977{
978 struct inode * inode;
979 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
980 int len;
981
982 if (!tmp)
983 return -ENOMEM;
984
985 inode = dentry->d_inode;
986 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
987 len = PTR_ERR(path);
988 if (IS_ERR(path))
989 goto out;
990 len = tmp + PAGE_SIZE - 1 - path;
991
992 if (len > buflen)
993 len = buflen;
994 if (copy_to_user(buffer, path, len))
995 len = -EFAULT;
996 out:
997 free_page((unsigned long)tmp);
998 return len;
999}
1000
1001static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1002{
1003 int error = -EACCES;
1004 struct inode *inode = dentry->d_inode;
1005 struct dentry *de;
1006 struct vfsmount *mnt = NULL;
1007
Eric W. Biederman778c1142006-06-26 00:25:58 -07001008 /* Are we allowed to snoop on the tasks file descriptors? */
1009 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1013 if (error)
1014 goto out;
1015
1016 error = do_proc_readlink(de, mnt, buffer, buflen);
1017 dput(de);
1018 mntput(mnt);
1019out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return error;
1021}
1022
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001023static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001025 .follow_link = proc_pid_follow_link,
1026 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027};
1028
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001029
1030/* building an inode */
1031
1032static int task_dumpable(struct task_struct *task)
1033{
1034 int dumpable = 0;
1035 struct mm_struct *mm;
1036
1037 task_lock(task);
1038 mm = task->mm;
1039 if (mm)
1040 dumpable = mm->dumpable;
1041 task_unlock(task);
1042 if(dumpable == 1)
1043 return 1;
1044 return 0;
1045}
1046
1047
Eric W. Biederman61a28782006-10-02 02:18:49 -07001048static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001049{
1050 struct inode * inode;
1051 struct proc_inode *ei;
1052
1053 /* We need a new inode */
1054
1055 inode = new_inode(sb);
1056 if (!inode)
1057 goto out;
1058
1059 /* Common stuff */
1060 ei = PROC_I(inode);
1061 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001062 inode->i_op = &proc_def_inode_operations;
1063
1064 /*
1065 * grab the reference to task.
1066 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001067 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001068 if (!ei->pid)
1069 goto out_unlock;
1070
1071 inode->i_uid = 0;
1072 inode->i_gid = 0;
1073 if (task_dumpable(task)) {
1074 inode->i_uid = task->euid;
1075 inode->i_gid = task->egid;
1076 }
1077 security_task_to_inode(task, inode);
1078
1079out:
1080 return inode;
1081
1082out_unlock:
1083 iput(inode);
1084 return NULL;
1085}
1086
1087static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1088{
1089 struct inode *inode = dentry->d_inode;
1090 struct task_struct *task;
1091 generic_fillattr(inode, stat);
1092
1093 rcu_read_lock();
1094 stat->uid = 0;
1095 stat->gid = 0;
1096 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1097 if (task) {
1098 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1099 task_dumpable(task)) {
1100 stat->uid = task->euid;
1101 stat->gid = task->egid;
1102 }
1103 }
1104 rcu_read_unlock();
1105 return 0;
1106}
1107
1108/* dentry stuff */
1109
1110/*
1111 * Exceptional case: normally we are not allowed to unhash a busy
1112 * directory. In this case, however, we can do it - no aliasing problems
1113 * due to the way we treat inodes.
1114 *
1115 * Rewrite the inode's ownerships here because the owning task may have
1116 * performed a setuid(), etc.
1117 *
1118 * Before the /proc/pid/status file was created the only way to read
1119 * the effective uid of a /process was to stat /proc/pid. Reading
1120 * /proc/pid/status is slow enough that procps and other packages
1121 * kept stating /proc/pid. To keep the rules in /proc simple I have
1122 * made this apply to all per process world readable and executable
1123 * directories.
1124 */
1125static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1126{
1127 struct inode *inode = dentry->d_inode;
1128 struct task_struct *task = get_proc_task(inode);
1129 if (task) {
1130 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1131 task_dumpable(task)) {
1132 inode->i_uid = task->euid;
1133 inode->i_gid = task->egid;
1134 } else {
1135 inode->i_uid = 0;
1136 inode->i_gid = 0;
1137 }
1138 inode->i_mode &= ~(S_ISUID | S_ISGID);
1139 security_task_to_inode(task, inode);
1140 put_task_struct(task);
1141 return 1;
1142 }
1143 d_drop(dentry);
1144 return 0;
1145}
1146
1147static int pid_delete_dentry(struct dentry * dentry)
1148{
1149 /* Is the task we represent dead?
1150 * If so, then don't put the dentry on the lru list,
1151 * kill it immediately.
1152 */
1153 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1154}
1155
1156static struct dentry_operations pid_dentry_operations =
1157{
1158 .d_revalidate = pid_revalidate,
1159 .d_delete = pid_delete_dentry,
1160};
1161
1162/* Lookups */
1163
Eric Dumazetc5141e62007-05-08 00:26:15 -07001164typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1165 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001166
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001167/*
1168 * Fill a directory entry.
1169 *
1170 * If possible create the dcache entry and derive our inode number and
1171 * file type from dcache entry.
1172 *
1173 * Since all of the proc inode numbers are dynamically generated, the inode
1174 * numbers do not exist until the inode is cache. This means creating the
1175 * the dcache entry in readdir is necessary to keep the inode numbers
1176 * reported by readdir in sync with the inode numbers reported
1177 * by stat.
1178 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001179static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1180 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001181 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001182{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001183 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001184 struct inode *inode;
1185 struct qstr qname;
1186 ino_t ino = 0;
1187 unsigned type = DT_UNKNOWN;
1188
1189 qname.name = name;
1190 qname.len = len;
1191 qname.hash = full_name_hash(name, len);
1192
1193 child = d_lookup(dir, &qname);
1194 if (!child) {
1195 struct dentry *new;
1196 new = d_alloc(dir, &qname);
1197 if (new) {
1198 child = instantiate(dir->d_inode, new, task, ptr);
1199 if (child)
1200 dput(new);
1201 else
1202 child = new;
1203 }
1204 }
1205 if (!child || IS_ERR(child) || !child->d_inode)
1206 goto end_instantiate;
1207 inode = child->d_inode;
1208 if (inode) {
1209 ino = inode->i_ino;
1210 type = inode->i_mode >> 12;
1211 }
1212 dput(child);
1213end_instantiate:
1214 if (!ino)
1215 ino = find_inode_number(dir, &qname);
1216 if (!ino)
1217 ino = 1;
1218 return filldir(dirent, name, len, filp->f_pos, ino, type);
1219}
1220
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001221static unsigned name_to_int(struct dentry *dentry)
1222{
1223 const char *name = dentry->d_name.name;
1224 int len = dentry->d_name.len;
1225 unsigned n = 0;
1226
1227 if (len > 1 && *name == '0')
1228 goto out;
1229 while (len-- > 0) {
1230 unsigned c = *name++ - '0';
1231 if (c > 9)
1232 goto out;
1233 if (n >= (~0U-9)/10)
1234 goto out;
1235 n *= 10;
1236 n += c;
1237 }
1238 return n;
1239out:
1240 return ~0U;
1241}
1242
Miklos Szeredi27932742007-05-08 00:26:17 -07001243#define PROC_FDINFO_MAX 64
1244
1245static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1246 struct vfsmount **mnt, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001247{
1248 struct task_struct *task = get_proc_task(inode);
1249 struct files_struct *files = NULL;
1250 struct file *file;
1251 int fd = proc_fd(inode);
1252
1253 if (task) {
1254 files = get_files_struct(task);
1255 put_task_struct(task);
1256 }
1257 if (files) {
1258 /*
1259 * We are not taking a ref to the file structure, so we must
1260 * hold ->file_lock.
1261 */
1262 spin_lock(&files->file_lock);
1263 file = fcheck_files(files, fd);
1264 if (file) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001265 if (mnt)
1266 *mnt = mntget(file->f_path.mnt);
1267 if (dentry)
1268 *dentry = dget(file->f_path.dentry);
1269 if (info)
1270 snprintf(info, PROC_FDINFO_MAX,
1271 "pos:\t%lli\n"
1272 "flags:\t0%o\n",
1273 (long long) file->f_pos,
1274 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001275 spin_unlock(&files->file_lock);
1276 put_files_struct(files);
1277 return 0;
1278 }
1279 spin_unlock(&files->file_lock);
1280 put_files_struct(files);
1281 }
1282 return -ENOENT;
1283}
1284
Miklos Szeredi27932742007-05-08 00:26:17 -07001285static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1286 struct vfsmount **mnt)
1287{
1288 return proc_fd_info(inode, dentry, mnt, NULL);
1289}
1290
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001291static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1292{
1293 struct inode *inode = dentry->d_inode;
1294 struct task_struct *task = get_proc_task(inode);
1295 int fd = proc_fd(inode);
1296 struct files_struct *files;
1297
1298 if (task) {
1299 files = get_files_struct(task);
1300 if (files) {
1301 rcu_read_lock();
1302 if (fcheck_files(files, fd)) {
1303 rcu_read_unlock();
1304 put_files_struct(files);
1305 if (task_dumpable(task)) {
1306 inode->i_uid = task->euid;
1307 inode->i_gid = task->egid;
1308 } else {
1309 inode->i_uid = 0;
1310 inode->i_gid = 0;
1311 }
1312 inode->i_mode &= ~(S_ISUID | S_ISGID);
1313 security_task_to_inode(task, inode);
1314 put_task_struct(task);
1315 return 1;
1316 }
1317 rcu_read_unlock();
1318 put_files_struct(files);
1319 }
1320 put_task_struct(task);
1321 }
1322 d_drop(dentry);
1323 return 0;
1324}
1325
1326static struct dentry_operations tid_fd_dentry_operations =
1327{
1328 .d_revalidate = tid_fd_revalidate,
1329 .d_delete = pid_delete_dentry,
1330};
1331
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001332static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001333 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001334{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001335 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001336 struct file *file;
1337 struct files_struct *files;
1338 struct inode *inode;
1339 struct proc_inode *ei;
1340 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001341
Eric W. Biederman61a28782006-10-02 02:18:49 -07001342 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001343 if (!inode)
1344 goto out;
1345 ei = PROC_I(inode);
1346 ei->fd = fd;
1347 files = get_files_struct(task);
1348 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001349 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001350 inode->i_mode = S_IFLNK;
1351
1352 /*
1353 * We are not taking a ref to the file structure, so we must
1354 * hold ->file_lock.
1355 */
1356 spin_lock(&files->file_lock);
1357 file = fcheck_files(files, fd);
1358 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001359 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001360 if (file->f_mode & 1)
1361 inode->i_mode |= S_IRUSR | S_IXUSR;
1362 if (file->f_mode & 2)
1363 inode->i_mode |= S_IWUSR | S_IXUSR;
1364 spin_unlock(&files->file_lock);
1365 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001366
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001367 inode->i_op = &proc_pid_link_inode_operations;
1368 inode->i_size = 64;
1369 ei->op.proc_get_link = proc_fd_link;
1370 dentry->d_op = &tid_fd_dentry_operations;
1371 d_add(dentry, inode);
1372 /* Close the race of the process dying before we return the dentry */
1373 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001374 error = NULL;
1375
1376 out:
1377 return error;
1378out_unlock:
1379 spin_unlock(&files->file_lock);
1380 put_files_struct(files);
1381out_iput:
1382 iput(inode);
1383 goto out;
1384}
1385
Miklos Szeredi27932742007-05-08 00:26:17 -07001386static struct dentry *proc_lookupfd_common(struct inode *dir,
1387 struct dentry *dentry,
1388 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001389{
1390 struct task_struct *task = get_proc_task(dir);
1391 unsigned fd = name_to_int(dentry);
1392 struct dentry *result = ERR_PTR(-ENOENT);
1393
1394 if (!task)
1395 goto out_no_task;
1396 if (fd == ~0U)
1397 goto out;
1398
Miklos Szeredi27932742007-05-08 00:26:17 -07001399 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001400out:
1401 put_task_struct(task);
1402out_no_task:
1403 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001404}
1405
Miklos Szeredi27932742007-05-08 00:26:17 -07001406static int proc_readfd_common(struct file * filp, void * dirent,
1407 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001409 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001410 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001411 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 unsigned int fd, tid, ino;
1413 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001415 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001418 if (!p)
1419 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 retval = 0;
1421 tid = p->pid;
1422
1423 fd = filp->f_pos;
1424 switch (fd) {
1425 case 0:
1426 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1427 goto out;
1428 filp->f_pos++;
1429 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001430 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1432 goto out;
1433 filp->f_pos++;
1434 default:
1435 files = get_files_struct(p);
1436 if (!files)
1437 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001438 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001439 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001441 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001443 char name[PROC_NUMBUF];
1444 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 if (!fcheck_files(files, fd))
1447 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001448 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Miklos Szeredi27932742007-05-08 00:26:17 -07001450 len = snprintf(name, sizeof(name), "%d", fd);
1451 if (proc_fill_cache(filp, dirent, filldir,
1452 name, len, instantiate,
1453 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001454 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
1456 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001457 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001459 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 put_files_struct(files);
1461 }
1462out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001463 put_task_struct(p);
1464out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return retval;
1466}
1467
Miklos Szeredi27932742007-05-08 00:26:17 -07001468static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1469 struct nameidata *nd)
1470{
1471 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1472}
1473
1474static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1475{
1476 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1477}
1478
1479static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1480 size_t len, loff_t *ppos)
1481{
1482 char tmp[PROC_FDINFO_MAX];
1483 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1484 if (!err)
1485 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1486 return err;
1487}
1488
1489static const struct file_operations proc_fdinfo_file_operations = {
1490 .open = nonseekable_open,
1491 .read = proc_fdinfo_read,
1492};
1493
Arjan van de Ven00977a52007-02-12 00:55:34 -08001494static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 .read = generic_read_dir,
1496 .readdir = proc_readfd,
1497};
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001500 * /proc/pid/fd needs a special permission handler so that a process can still
1501 * access /proc/self/fd after it has executed a setuid().
1502 */
1503static int proc_fd_permission(struct inode *inode, int mask,
1504 struct nameidata *nd)
1505{
1506 int rv;
1507
1508 rv = generic_permission(inode, mask, NULL);
1509 if (rv == 0)
1510 return 0;
1511 if (task_pid(current) == proc_pid(inode))
1512 rv = 0;
1513 return rv;
1514}
1515
1516/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 * proc directories can do almost nothing..
1518 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001519static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001521 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001522 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523};
1524
Miklos Szeredi27932742007-05-08 00:26:17 -07001525static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1526 struct dentry *dentry, struct task_struct *task, const void *ptr)
1527{
1528 unsigned fd = *(unsigned *)ptr;
1529 struct inode *inode;
1530 struct proc_inode *ei;
1531 struct dentry *error = ERR_PTR(-ENOENT);
1532
1533 inode = proc_pid_make_inode(dir->i_sb, task);
1534 if (!inode)
1535 goto out;
1536 ei = PROC_I(inode);
1537 ei->fd = fd;
1538 inode->i_mode = S_IFREG | S_IRUSR;
1539 inode->i_fop = &proc_fdinfo_file_operations;
1540 dentry->d_op = &tid_fd_dentry_operations;
1541 d_add(dentry, inode);
1542 /* Close the race of the process dying before we return the dentry */
1543 if (tid_fd_revalidate(dentry, NULL))
1544 error = NULL;
1545
1546 out:
1547 return error;
1548}
1549
1550static struct dentry *proc_lookupfdinfo(struct inode *dir,
1551 struct dentry *dentry,
1552 struct nameidata *nd)
1553{
1554 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1555}
1556
1557static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1558{
1559 return proc_readfd_common(filp, dirent, filldir,
1560 proc_fdinfo_instantiate);
1561}
1562
1563static const struct file_operations proc_fdinfo_operations = {
1564 .read = generic_read_dir,
1565 .readdir = proc_readfdinfo,
1566};
1567
1568/*
1569 * proc directories can do almost nothing..
1570 */
1571static const struct inode_operations proc_fdinfo_inode_operations = {
1572 .lookup = proc_lookupfdinfo,
1573 .setattr = proc_setattr,
1574};
1575
1576
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001577static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001578 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001579{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001580 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001581 struct inode *inode;
1582 struct proc_inode *ei;
1583 struct dentry *error = ERR_PTR(-EINVAL);
1584
Eric W. Biederman61a28782006-10-02 02:18:49 -07001585 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001586 if (!inode)
1587 goto out;
1588
1589 ei = PROC_I(inode);
1590 inode->i_mode = p->mode;
1591 if (S_ISDIR(inode->i_mode))
1592 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1593 if (p->iop)
1594 inode->i_op = p->iop;
1595 if (p->fop)
1596 inode->i_fop = p->fop;
1597 ei->op = p->op;
1598 dentry->d_op = &pid_dentry_operations;
1599 d_add(dentry, inode);
1600 /* Close the race of the process dying before we return the dentry */
1601 if (pid_revalidate(dentry, NULL))
1602 error = NULL;
1603out:
1604 return error;
1605}
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607static struct dentry *proc_pident_lookup(struct inode *dir,
1608 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001609 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001610 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001613 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001614 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001615 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001617 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 inode = NULL;
1619
Eric W. Biederman99f89552006-06-26 00:25:55 -07001620 if (!task)
1621 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001623 /*
1624 * Yes, it does not scale. And it should not. Don't add
1625 * new entries into /proc/<tgid>/ without very good reasons.
1626 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001627 last = &ents[nents - 1];
1628 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (p->len != dentry->d_name.len)
1630 continue;
1631 if (!memcmp(dentry->d_name.name, p->name, p->len))
1632 break;
1633 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001634 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 goto out;
1636
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001637 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001639 put_task_struct(task);
1640out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001641 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Eric Dumazetc5141e62007-05-08 00:26:15 -07001644static int proc_pident_fill_cache(struct file *filp, void *dirent,
1645 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001646{
1647 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1648 proc_pident_instantiate, task, p);
1649}
1650
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001651static int proc_pident_readdir(struct file *filp,
1652 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001653 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001654{
1655 int i;
1656 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001657 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001658 struct inode *inode = dentry->d_inode;
1659 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001660 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001661 ino_t ino;
1662 int ret;
1663
1664 ret = -ENOENT;
1665 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001666 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001667
1668 ret = 0;
1669 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001670 i = filp->f_pos;
1671 switch (i) {
1672 case 0:
1673 ino = inode->i_ino;
1674 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1675 goto out;
1676 i++;
1677 filp->f_pos++;
1678 /* fall through */
1679 case 1:
1680 ino = parent_ino(dentry);
1681 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1682 goto out;
1683 i++;
1684 filp->f_pos++;
1685 /* fall through */
1686 default:
1687 i -= 2;
1688 if (i >= nents) {
1689 ret = 1;
1690 goto out;
1691 }
1692 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001693 last = &ents[nents - 1];
1694 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001695 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001696 goto out;
1697 filp->f_pos++;
1698 p++;
1699 }
1700 }
1701
1702 ret = 1;
1703out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001704 put_task_struct(task);
1705out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001706 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001710static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1711 size_t count, loff_t *ppos)
1712{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001713 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001714 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001715 ssize_t length;
1716 struct task_struct *task = get_proc_task(inode);
1717
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001718 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001719 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001720
1721 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001722 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001723 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001724 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001725 if (length > 0)
1726 length = simple_read_from_buffer(buf, count, ppos, p, length);
1727 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001728 return length;
1729}
1730
1731static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1732 size_t count, loff_t *ppos)
1733{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001734 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001735 char *page;
1736 ssize_t length;
1737 struct task_struct *task = get_proc_task(inode);
1738
1739 length = -ESRCH;
1740 if (!task)
1741 goto out_no_task;
1742 if (count > PAGE_SIZE)
1743 count = PAGE_SIZE;
1744
1745 /* No partial writes. */
1746 length = -EINVAL;
1747 if (*ppos != 0)
1748 goto out;
1749
1750 length = -ENOMEM;
1751 page = (char*)__get_free_page(GFP_USER);
1752 if (!page)
1753 goto out;
1754
1755 length = -EFAULT;
1756 if (copy_from_user(page, buf, count))
1757 goto out_free;
1758
1759 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001760 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761 (void*)page, count);
1762out_free:
1763 free_page((unsigned long) page);
1764out:
1765 put_task_struct(task);
1766out_no_task:
1767 return length;
1768}
1769
Arjan van de Ven00977a52007-02-12 00:55:34 -08001770static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001771 .read = proc_pid_attr_read,
1772 .write = proc_pid_attr_write,
1773};
1774
Eric Dumazetc5141e62007-05-08 00:26:15 -07001775static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001776 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1777 REG("prev", S_IRUGO, pid_attr),
1778 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1779 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1780 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1781 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001782};
1783
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001784static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 void * dirent, filldir_t filldir)
1786{
1787 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001788 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Arjan van de Ven00977a52007-02-12 00:55:34 -08001791static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001793 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794};
1795
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001796static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 struct dentry *dentry, struct nameidata *nd)
1798{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001799 return proc_pident_lookup(dir, dentry,
1800 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001803static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001804 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001805 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001806 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807};
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809#endif
1810
1811/*
1812 * /proc/self:
1813 */
1814static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1815 int buflen)
1816{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001817 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 sprintf(tmp, "%d", current->tgid);
1819 return vfs_readlink(dentry,buffer,buflen,tmp);
1820}
1821
Al Viro008b1502005-08-20 00:17:39 +01001822static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001824 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001826 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001827}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001829static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 .readlink = proc_self_readlink,
1831 .follow_link = proc_self_follow_link,
1832};
1833
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001834/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001835 * proc base
1836 *
1837 * These are the directory entries in the root directory of /proc
1838 * that properly belong to the /proc filesystem, as they describe
1839 * describe something that is process related.
1840 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07001841static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001842 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001843 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001844};
1845
1846/*
1847 * Exceptional case: normally we are not allowed to unhash a busy
1848 * directory. In this case, however, we can do it - no aliasing problems
1849 * due to the way we treat inodes.
1850 */
1851static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1852{
1853 struct inode *inode = dentry->d_inode;
1854 struct task_struct *task = get_proc_task(inode);
1855 if (task) {
1856 put_task_struct(task);
1857 return 1;
1858 }
1859 d_drop(dentry);
1860 return 0;
1861}
1862
1863static struct dentry_operations proc_base_dentry_operations =
1864{
1865 .d_revalidate = proc_base_revalidate,
1866 .d_delete = pid_delete_dentry,
1867};
1868
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001869static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001870 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001871{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001872 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001873 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001874 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001875 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001876
1877 /* Allocate the inode */
1878 error = ERR_PTR(-ENOMEM);
1879 inode = new_inode(dir->i_sb);
1880 if (!inode)
1881 goto out;
1882
1883 /* Initialize the inode */
1884 ei = PROC_I(inode);
1885 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001886
1887 /*
1888 * grab the reference to the task.
1889 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001890 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001891 if (!ei->pid)
1892 goto out_iput;
1893
1894 inode->i_uid = 0;
1895 inode->i_gid = 0;
1896 inode->i_mode = p->mode;
1897 if (S_ISDIR(inode->i_mode))
1898 inode->i_nlink = 2;
1899 if (S_ISLNK(inode->i_mode))
1900 inode->i_size = 64;
1901 if (p->iop)
1902 inode->i_op = p->iop;
1903 if (p->fop)
1904 inode->i_fop = p->fop;
1905 ei->op = p->op;
1906 dentry->d_op = &proc_base_dentry_operations;
1907 d_add(dentry, inode);
1908 error = NULL;
1909out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001910 return error;
1911out_iput:
1912 iput(inode);
1913 goto out;
1914}
1915
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001916static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1917{
1918 struct dentry *error;
1919 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001920 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001921
1922 error = ERR_PTR(-ENOENT);
1923
1924 if (!task)
1925 goto out_no_task;
1926
1927 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001928 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1929 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001930 if (p->len != dentry->d_name.len)
1931 continue;
1932 if (!memcmp(dentry->d_name.name, p->name, p->len))
1933 break;
1934 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001935 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001936 goto out;
1937
1938 error = proc_base_instantiate(dir, dentry, task, p);
1939
1940out:
1941 put_task_struct(task);
1942out_no_task:
1943 return error;
1944}
1945
Eric Dumazetc5141e62007-05-08 00:26:15 -07001946static int proc_base_fill_cache(struct file *filp, void *dirent,
1947 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001948{
1949 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1950 proc_base_instantiate, task, p);
1951}
1952
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001953#ifdef CONFIG_TASK_IO_ACCOUNTING
1954static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1955{
1956 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001957#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001958 "rchar: %llu\n"
1959 "wchar: %llu\n"
1960 "syscr: %llu\n"
1961 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001962#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001963 "read_bytes: %llu\n"
1964 "write_bytes: %llu\n"
1965 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001966#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001967 (unsigned long long)task->rchar,
1968 (unsigned long long)task->wchar,
1969 (unsigned long long)task->syscr,
1970 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001971#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001972 (unsigned long long)task->ioac.read_bytes,
1973 (unsigned long long)task->ioac.write_bytes,
1974 (unsigned long long)task->ioac.cancelled_write_bytes);
1975}
1976#endif
1977
Eric W. Biederman801199c2006-10-02 02:18:48 -07001978/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001979 * Thread groups
1980 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08001981static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001982static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001983
Eric Dumazetc5141e62007-05-08 00:26:15 -07001984static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001985 DIR("task", S_IRUGO|S_IXUGO, task),
1986 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07001987 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07001988 INF("environ", S_IRUSR, pid_environ),
1989 INF("auxv", S_IRUSR, pid_auxv),
1990 INF("status", S_IRUGO, pid_status),
1991 INF("cmdline", S_IRUGO, pid_cmdline),
1992 INF("stat", S_IRUGO, tgid_stat),
1993 INF("statm", S_IRUGO, pid_statm),
1994 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001995#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001996 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001997#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001998 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001999#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002000 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002001#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002002 LNK("cwd", cwd),
2003 LNK("root", root),
2004 LNK("exe", exe),
2005 REG("mounts", S_IRUGO, mounts),
2006 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002007#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002008 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002009 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002010#endif
2011#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002012 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002013#endif
2014#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002015 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002016#endif
2017#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002018 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002019#endif
2020#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002021 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002022#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002023 INF("oom_score", S_IRUGO, oom_score),
2024 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002025#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002026 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002027#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002028#ifdef CONFIG_FAULT_INJECTION
2029 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2030#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002031#ifdef CONFIG_TASK_IO_ACCOUNTING
2032 INF("io", S_IRUGO, pid_io_accounting),
2033#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002034};
2035
2036static int proc_tgid_base_readdir(struct file * filp,
2037 void * dirent, filldir_t filldir)
2038{
2039 return proc_pident_readdir(filp,dirent,filldir,
2040 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2041}
2042
Arjan van de Ven00977a52007-02-12 00:55:34 -08002043static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002044 .read = generic_read_dir,
2045 .readdir = proc_tgid_base_readdir,
2046};
2047
2048static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002049 return proc_pident_lookup(dir, dentry,
2050 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002051}
2052
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002053static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002054 .lookup = proc_tgid_base_lookup,
2055 .getattr = pid_getattr,
2056 .setattr = proc_setattr,
2057};
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07002060 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002062 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002064 * Looks in the dcache for
2065 * /proc/@pid
2066 * /proc/@tgid/task/@pid
2067 * if either directory is present flushes it and all of it'ts children
2068 * from the dcache.
2069 *
2070 * It is safe and reasonable to cache /proc entries for a task until
2071 * that task exits. After that they just clog up the dcache with
2072 * useless entries, possibly causing useful dcache entries to be
2073 * flushed instead. This routine is proved to flush those useless
2074 * dcache entries at process exit time.
2075 *
2076 * NOTE: This routine is just an optimization so it does not guarantee
2077 * that no dcache entries will exist at process exit time it
2078 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07002080void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002082 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002083 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002084 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Eric W. Biederman48e64842006-06-26 00:25:48 -07002086 name.name = buf;
2087 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2088 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2089 if (dentry) {
2090 shrink_dcache_parent(dentry);
2091 d_drop(dentry);
2092 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Eric W. Biederman48e64842006-06-26 00:25:48 -07002095 if (thread_group_leader(task))
2096 goto out;
2097
2098 name.name = buf;
2099 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2100 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2101 if (!leader)
2102 goto out;
2103
2104 name.name = "task";
2105 name.len = strlen(name.name);
2106 dir = d_hash_and_lookup(leader, &name);
2107 if (!dir)
2108 goto out_put_leader;
2109
2110 name.name = buf;
2111 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2112 dentry = d_hash_and_lookup(dir, &name);
2113 if (dentry) {
2114 shrink_dcache_parent(dentry);
2115 d_drop(dentry);
2116 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002118
2119 dput(dir);
2120out_put_leader:
2121 dput(leader);
2122out:
2123 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
Adrian Bunk9711ef92006-12-06 20:38:31 -08002126static struct dentry *proc_pid_instantiate(struct inode *dir,
2127 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002128 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002129{
2130 struct dentry *error = ERR_PTR(-ENOENT);
2131 struct inode *inode;
2132
Eric W. Biederman61a28782006-10-02 02:18:49 -07002133 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002134 if (!inode)
2135 goto out;
2136
2137 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2138 inode->i_op = &proc_tgid_base_inode_operations;
2139 inode->i_fop = &proc_tgid_base_operations;
2140 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002141 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002142#ifdef CONFIG_SECURITY
2143 inode->i_nlink += 1;
2144#endif
2145
2146 dentry->d_op = &pid_dentry_operations;
2147
2148 d_add(dentry, inode);
2149 /* Close the race of the process dying before we return the dentry */
2150 if (pid_revalidate(dentry, NULL))
2151 error = NULL;
2152out:
2153 return error;
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2157{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002158 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Eric W. Biederman801199c2006-10-02 02:18:48 -07002162 result = proc_base_lookup(dir, dentry);
2163 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2164 goto out;
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 tgid = name_to_int(dentry);
2167 if (tgid == ~0U)
2168 goto out;
2169
Eric W. Biedermande758732006-06-26 00:25:51 -07002170 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 task = find_task_by_pid(tgid);
2172 if (task)
2173 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002174 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (!task)
2176 goto out;
2177
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002178 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002179 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002181 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002185 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002186 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002188static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002190 struct task_struct *task;
2191 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002193 rcu_read_lock();
2194retry:
2195 task = NULL;
2196 pid = find_ge_pid(tgid);
2197 if (pid) {
2198 tgid = pid->nr + 1;
2199 task = pid_task(pid, PIDTYPE_PID);
2200 /* What we to know is if the pid we have find is the
2201 * pid of a thread_group_leader. Testing for task
2202 * being a thread_group_leader is the obvious thing
2203 * todo but there is a window when it fails, due to
2204 * the pid transfer logic in de_thread.
2205 *
2206 * So we perform the straight forward test of seeing
2207 * if the pid we have found is the pid of a thread
2208 * group leader, and don't worry if the task we have
2209 * found doesn't happen to be a thread group leader.
2210 * As we don't care in the case of readdir.
2211 */
2212 if (!task || !has_group_leader_pid(task))
2213 goto retry;
2214 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002216 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002217 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002220#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Eric W. Biederman61a28782006-10-02 02:18:49 -07002222static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2223 struct task_struct *task, int tgid)
2224{
2225 char name[PROC_NUMBUF];
2226 int len = snprintf(name, sizeof(name), "%d", tgid);
2227 return proc_fill_cache(filp, dirent, filldir, name, len,
2228 proc_pid_instantiate, task, NULL);
2229}
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231/* for the /proc/ directory itself, after non-process stuff has been done */
2232int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002235 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002236 struct task_struct *task;
2237 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Eric W. Biederman61a28782006-10-02 02:18:49 -07002239 if (!reaper)
2240 goto out_no_task;
2241
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002242 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002243 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002244 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002248 tgid = filp->f_pos - TGID_OFFSET;
2249 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002250 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002251 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002252 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002253 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002254 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002255 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002259 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2260out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002261 put_task_struct(reaper);
2262out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return 0;
2264}
2265
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002266/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002267 * Tasks
2268 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002269static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002270 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002271 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002272 INF("environ", S_IRUSR, pid_environ),
2273 INF("auxv", S_IRUSR, pid_auxv),
2274 INF("status", S_IRUGO, pid_status),
2275 INF("cmdline", S_IRUGO, pid_cmdline),
2276 INF("stat", S_IRUGO, tid_stat),
2277 INF("statm", S_IRUGO, pid_statm),
2278 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002279#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002280 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002281#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002282 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002283#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002284 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002285#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002286 LNK("cwd", cwd),
2287 LNK("root", root),
2288 LNK("exe", exe),
2289 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002290#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002291 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002292 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002293#endif
2294#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002295 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002296#endif
2297#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002298 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002299#endif
2300#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002301 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002302#endif
2303#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002304 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002305#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002306 INF("oom_score", S_IRUGO, oom_score),
2307 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002308#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002309 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002310#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002311#ifdef CONFIG_FAULT_INJECTION
2312 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2313#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002314};
2315
2316static int proc_tid_base_readdir(struct file * filp,
2317 void * dirent, filldir_t filldir)
2318{
2319 return proc_pident_readdir(filp,dirent,filldir,
2320 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2321}
2322
2323static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002324 return proc_pident_lookup(dir, dentry,
2325 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002326}
2327
Arjan van de Ven00977a52007-02-12 00:55:34 -08002328static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002329 .read = generic_read_dir,
2330 .readdir = proc_tid_base_readdir,
2331};
2332
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002333static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002334 .lookup = proc_tid_base_lookup,
2335 .getattr = pid_getattr,
2336 .setattr = proc_setattr,
2337};
2338
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002339static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002340 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002341{
2342 struct dentry *error = ERR_PTR(-ENOENT);
2343 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002344 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002345
2346 if (!inode)
2347 goto out;
2348 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2349 inode->i_op = &proc_tid_base_inode_operations;
2350 inode->i_fop = &proc_tid_base_operations;
2351 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002352 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002353#ifdef CONFIG_SECURITY
2354 inode->i_nlink += 1;
2355#endif
2356
2357 dentry->d_op = &pid_dentry_operations;
2358
2359 d_add(dentry, inode);
2360 /* Close the race of the process dying before we return the dentry */
2361 if (pid_revalidate(dentry, NULL))
2362 error = NULL;
2363out:
2364 return error;
2365}
2366
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002367static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2368{
2369 struct dentry *result = ERR_PTR(-ENOENT);
2370 struct task_struct *task;
2371 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002372 unsigned tid;
2373
2374 if (!leader)
2375 goto out_no_task;
2376
2377 tid = name_to_int(dentry);
2378 if (tid == ~0U)
2379 goto out;
2380
2381 rcu_read_lock();
2382 task = find_task_by_pid(tid);
2383 if (task)
2384 get_task_struct(task);
2385 rcu_read_unlock();
2386 if (!task)
2387 goto out;
2388 if (leader->tgid != task->tgid)
2389 goto out_drop_task;
2390
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002391 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002392out_drop_task:
2393 put_task_struct(task);
2394out:
2395 put_task_struct(leader);
2396out_no_task:
2397 return result;
2398}
2399
2400/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002401 * Find the first tid of a thread group to return to user space.
2402 *
2403 * Usually this is just the thread group leader, but if the users
2404 * buffer was too small or there was a seek into the middle of the
2405 * directory we have more work todo.
2406 *
2407 * In the case of a short read we start with find_task_by_pid.
2408 *
2409 * In the case of a seek we start with the leader and walk nr
2410 * threads past it.
2411 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002412static struct task_struct *first_tid(struct task_struct *leader,
2413 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002414{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002415 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002416
Eric W. Biedermancc288732006-06-26 00:26:01 -07002417 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002418 /* Attempt to start with the pid of a thread */
2419 if (tid && (nr > 0)) {
2420 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002421 if (pos && (pos->group_leader == leader))
2422 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002423 }
2424
2425 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002426 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002427 if (nr && nr >= get_nr_threads(leader))
2428 goto out;
2429
2430 /* If we haven't found our starting place yet start
2431 * with the leader and walk nr threads forward.
2432 */
2433 for (pos = leader; nr > 0; --nr) {
2434 pos = next_thread(pos);
2435 if (pos == leader) {
2436 pos = NULL;
2437 goto out;
2438 }
2439 }
2440found:
2441 get_task_struct(pos);
2442out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002443 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002444 return pos;
2445}
2446
2447/*
2448 * Find the next thread in the thread list.
2449 * Return NULL if there is an error or no next thread.
2450 *
2451 * The reference to the input task_struct is released.
2452 */
2453static struct task_struct *next_tid(struct task_struct *start)
2454{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002455 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002456 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002457 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002458 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002459 if (thread_group_leader(pos))
2460 pos = NULL;
2461 else
2462 get_task_struct(pos);
2463 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002464 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002465 put_task_struct(start);
2466 return pos;
2467}
2468
Eric W. Biederman61a28782006-10-02 02:18:49 -07002469static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2470 struct task_struct *task, int tid)
2471{
2472 char name[PROC_NUMBUF];
2473 int len = snprintf(name, sizeof(name), "%d", tid);
2474 return proc_fill_cache(filp, dirent, filldir, name, len,
2475 proc_task_instantiate, task, NULL);
2476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/* for the /proc/TGID/task/ directories */
2479static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2480{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002481 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002483 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002484 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 int retval = -ENOENT;
2486 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002487 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2489
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002490 task = get_proc_task(inode);
2491 if (!task)
2492 goto out_no_task;
2493 rcu_read_lock();
2494 if (pid_alive(task)) {
2495 leader = task->group_leader;
2496 get_task_struct(leader);
2497 }
2498 rcu_read_unlock();
2499 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002500 if (!leader)
2501 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 retval = 0;
2503
2504 switch (pos) {
2505 case 0:
2506 ino = inode->i_ino;
2507 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2508 goto out;
2509 pos++;
2510 /* fall through */
2511 case 1:
2512 ino = parent_ino(dentry);
2513 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2514 goto out;
2515 pos++;
2516 /* fall through */
2517 }
2518
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002519 /* f_version caches the tgid value that the last readdir call couldn't
2520 * return. lseek aka telldir automagically resets f_version to 0.
2521 */
2522 tid = filp->f_version;
2523 filp->f_version = 0;
2524 for (task = first_tid(leader, tid, pos - 2);
2525 task;
2526 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002527 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002528 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002529 /* returning this tgid failed, save it as the first
2530 * pid for the next readir call */
2531 filp->f_version = tid;
2532 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536out:
2537 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002538 put_task_struct(leader);
2539out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return retval;
2541}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002542
2543static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2544{
2545 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002546 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002547 generic_fillattr(inode, stat);
2548
Eric W. Biederman99f89552006-06-26 00:25:55 -07002549 if (p) {
2550 rcu_read_lock();
2551 stat->nlink += get_nr_threads(p);
2552 rcu_read_unlock();
2553 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002554 }
2555
2556 return 0;
2557}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002558
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002559static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002560 .lookup = proc_task_lookup,
2561 .getattr = proc_task_getattr,
2562 .setattr = proc_setattr,
2563};
2564
Arjan van de Ven00977a52007-02-12 00:55:34 -08002565static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002566 .read = generic_read_dir,
2567 .readdir = proc_task_readdir,
2568};