blob: d0921944e68c70e851197c86fb205c69b2fe51ff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070064#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/kallsyms.h>
Kees Cook5096add2007-05-08 00:26:04 -070066#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/mount.h>
68#include <linux/security.h>
69#include <linux/ptrace.h>
70#include <linux/seccomp.h>
71#include <linux/cpuset.h>
72#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050073#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070074#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070075#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include "internal.h"
77
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070078/* NOTE:
79 * Implementing inode permission operations in /proc is almost
80 * certainly an error. Permission checks need to happen during
81 * each system call not at open time. The reason is that most of
82 * what we wish to check for permissions in /proc varies at runtime.
83 *
84 * The classic example of a problem is opening file descriptors
85 * in /proc for a task before it execs a suid executable.
86 */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Eric W. Biederman8578cea2006-06-26 00:25:54 -070089/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070090#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070094 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080096 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080097 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070098 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Eric W. Biederman61a28782006-10-02 02:18:49 -0700101#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700102 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700103 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 .mode = MODE, \
105 .iop = IOP, \
106 .fop = FOP, \
107 .op = OP, \
108}
109
Eric W. Biederman61a28782006-10-02 02:18:49 -0700110#define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700114#define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700118#define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700121#define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Kees Cook5096add2007-05-08 00:26:04 -0700126int maps_protect;
127EXPORT_SYMBOL(maps_protect);
128
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700129static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700132 task_lock(task);
133 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if(fs)
135 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700136 task_unlock(task);
137 return fs;
138}
139
Eric W. Biederman99f89552006-06-26 00:25:55 -0700140static int get_nr_threads(struct task_struct *tsk)
141{
142 /* Must be called with the rcu_read_lock held */
143 unsigned long flags;
144 int count = 0;
145
146 if (lock_task_sighand(tsk, &flags)) {
147 count = atomic_read(&tsk->signal->count);
148 unlock_task_sighand(tsk, &flags);
149 }
150 return count;
151}
152
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700153static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
154{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700155 struct task_struct *task = get_proc_task(inode);
156 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700158
159 if (task) {
160 fs = get_fs_struct(task);
161 put_task_struct(task);
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (fs) {
164 read_lock(&fs->lock);
165 *mnt = mntget(fs->pwdmnt);
166 *dentry = dget(fs->pwd);
167 read_unlock(&fs->lock);
168 result = 0;
169 put_fs_struct(fs);
170 }
171 return result;
172}
173
174static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
175{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176 struct task_struct *task = get_proc_task(inode);
177 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179
180 if (task) {
181 fs = get_fs_struct(task);
182 put_task_struct(task);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (fs) {
185 read_lock(&fs->lock);
186 *mnt = mntget(fs->rootmnt);
187 *dentry = dget(fs->root);
188 read_unlock(&fs->lock);
189 result = 0;
190 put_fs_struct(fs);
191 }
192 return result;
193}
194
195#define MAY_PTRACE(task) \
196 (task == current || \
197 (task->parent == current && \
198 (task->ptrace & PT_PTRACED) && \
199 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
200 security_ptrace(current,task) == 0))
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int proc_pid_environ(struct task_struct *task, char * buffer)
203{
204 int res = 0;
205 struct mm_struct *mm = get_task_mm(task);
206 if (mm) {
Alexey Dobriyanda58a162007-07-15 23:40:21 -0700207 unsigned int len;
208
209 res = -ESRCH;
210 if (!ptrace_may_attach(task))
211 goto out;
212
213 len = mm->env_end - mm->env_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (len > PAGE_SIZE)
215 len = PAGE_SIZE;
216 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Alexey Dobriyanda58a162007-07-15 23:40:21 -0700217out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 mmput(mm);
219 }
220 return res;
221}
222
223static int proc_pid_cmdline(struct task_struct *task, char * buffer)
224{
225 int res = 0;
226 unsigned int len;
227 struct mm_struct *mm = get_task_mm(task);
228 if (!mm)
229 goto out;
230 if (!mm->arg_end)
231 goto out_mm; /* Shh! No looking before we're done */
232
233 len = mm->arg_end - mm->arg_start;
234
235 if (len > PAGE_SIZE)
236 len = PAGE_SIZE;
237
238 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
239
240 // If the nul at the end of args has been overwritten, then
241 // assume application is using setproctitle(3).
242 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
243 len = strnlen(buffer, res);
244 if (len < res) {
245 res = len;
246 } else {
247 len = mm->env_end - mm->env_start;
248 if (len > PAGE_SIZE - res)
249 len = PAGE_SIZE - res;
250 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
251 res = strnlen(buffer, res);
252 }
253 }
254out_mm:
255 mmput(mm);
256out:
257 return res;
258}
259
260static int proc_pid_auxv(struct task_struct *task, char *buffer)
261{
262 int res = 0;
263 struct mm_struct *mm = get_task_mm(task);
264 if (mm) {
265 unsigned int nwords = 0;
266 do
267 nwords += 2;
268 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
269 res = nwords * sizeof(mm->saved_auxv[0]);
270 if (res > PAGE_SIZE)
271 res = PAGE_SIZE;
272 memcpy(buffer, mm->saved_auxv, res);
273 mmput(mm);
274 }
275 return res;
276}
277
278
279#ifdef CONFIG_KALLSYMS
280/*
281 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
282 * Returns the resolved symbol. If that fails, simply return the address.
283 */
284static int proc_pid_wchan(struct task_struct *task, char *buffer)
285{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700286 unsigned long wchan;
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700287 char symname[KSYM_NAME_LEN+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 wchan = get_wchan(task);
290
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700291 if (lookup_symbol_name(wchan, symname) < 0)
292 return sprintf(buffer, "%lu", wchan);
293 else
294 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296#endif /* CONFIG_KALLSYMS */
297
298#ifdef CONFIG_SCHEDSTATS
299/*
300 * Provides /proc/PID/schedstat
301 */
302static int proc_pid_schedstat(struct task_struct *task, char *buffer)
303{
Balbir Singh172ba842007-07-09 18:52:00 +0200304 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 task->sched_info.cpu_time,
306 task->sched_info.run_delay,
307 task->sched_info.pcnt);
308}
309#endif
310
311/* The badness from the OOM killer */
312unsigned long badness(struct task_struct *p, unsigned long uptime);
313static int proc_oom_score(struct task_struct *task, char *buffer)
314{
315 unsigned long points;
316 struct timespec uptime;
317
318 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700319 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700321 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return sprintf(buffer, "%lu\n", points);
323}
324
325/************************************************************************/
326/* Here the fs part begins */
327/************************************************************************/
328
329/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700330static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700332 struct task_struct *task;
333 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700334 /* Allow access to a task's file descriptors if it is us or we
335 * may use ptrace attach to the process and find out that
336 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700337 */
338 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700339 if (task) {
340 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700341 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700342 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700343 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700346static int proc_setattr(struct dentry *dentry, struct iattr *attr)
347{
348 int error;
349 struct inode *inode = dentry->d_inode;
350
351 if (attr->ia_valid & ATTR_MODE)
352 return -EPERM;
353
354 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700355 if (!error)
356 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700357 return error;
358}
359
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800360static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700361 .setattr = proc_setattr,
362};
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500365struct proc_mounts {
366 struct seq_file m;
367 int event;
368};
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static int mounts_open(struct inode *inode, struct file *file)
371{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700372 struct task_struct *task = get_proc_task(inode);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800373 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500374 struct proc_mounts *p;
375 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Eric W. Biederman99f89552006-06-26 00:25:55 -0700377 if (task) {
378 task_lock(task);
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800379 if (task->nsproxy) {
380 ns = task->nsproxy->mnt_ns;
381 if (ns)
382 get_mnt_ns(ns);
383 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700384 task_unlock(task);
385 put_task_struct(task);
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800388 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500389 ret = -ENOMEM;
390 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
391 if (p) {
392 file->private_data = &p->m;
393 ret = seq_open(file, &mounts_op);
394 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800395 p->m.private = ns;
396 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500397 return 0;
398 }
399 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800401 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 return ret;
404}
405
406static int mounts_release(struct inode *inode, struct file *file)
407{
408 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800409 struct mnt_namespace *ns = m->private;
410 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return seq_release(inode, file);
412}
413
Al Viro5addc5d2005-11-07 17:15:49 -0500414static unsigned mounts_poll(struct file *file, poll_table *wait)
415{
416 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800417 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500418 unsigned res = 0;
419
420 poll_wait(file, &ns->poll, wait);
421
422 spin_lock(&vfsmount_lock);
423 if (p->event != ns->event) {
424 p->event = ns->event;
425 res = POLLERR;
426 }
427 spin_unlock(&vfsmount_lock);
428
429 return res;
430}
431
Arjan van de Ven00977a52007-02-12 00:55:34 -0800432static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 .open = mounts_open,
434 .read = seq_read,
435 .llseek = seq_lseek,
436 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500437 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438};
439
Chuck Leverb4629fe2006-03-20 13:44:12 -0500440extern struct seq_operations mountstats_op;
441static int mountstats_open(struct inode *inode, struct file *file)
442{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500443 int ret = seq_open(file, &mountstats_op);
444
445 if (!ret) {
446 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800447 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700448 struct task_struct *task = get_proc_task(inode);
449
450 if (task) {
451 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800452 if (task->nsproxy)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800453 mnt_ns = task->nsproxy->mnt_ns;
454 if (mnt_ns)
455 get_mnt_ns(mnt_ns);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700456 task_unlock(task);
457 put_task_struct(task);
458 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500459
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800460 if (mnt_ns)
461 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500462 else {
463 seq_release(inode, file);
464 ret = -EINVAL;
465 }
466 }
467 return ret;
468}
469
Arjan van de Ven00977a52007-02-12 00:55:34 -0800470static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500471 .open = mountstats_open,
472 .read = seq_read,
473 .llseek = seq_lseek,
474 .release = mounts_release,
475};
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
478
479static ssize_t proc_info_read(struct file * file, char __user * buf,
480 size_t count, loff_t *ppos)
481{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800482 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned long page;
484 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700485 struct task_struct *task = get_proc_task(inode);
486
487 length = -ESRCH;
488 if (!task)
489 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (count > PROC_BLOCK_SIZE)
492 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700493
494 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 length = PROC_I(inode)->op.proc_read(task, (char*)page);
499
500 if (length >= 0)
501 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
502 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700503out:
504 put_task_struct(task);
505out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return length;
507}
508
Arjan van de Ven00977a52007-02-12 00:55:34 -0800509static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .read = proc_info_read,
511};
512
513static int mem_open(struct inode* inode, struct file* file)
514{
515 file->private_data = (void*)((long)current->self_exec_id);
516 return 0;
517}
518
519static ssize_t mem_read(struct file * file, char __user * buf,
520 size_t count, loff_t *ppos)
521{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800522 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 char *page;
524 unsigned long src = *ppos;
525 int ret = -ESRCH;
526 struct mm_struct *mm;
527
Eric W. Biederman99f89552006-06-26 00:25:55 -0700528 if (!task)
529 goto out_no_task;
530
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700531 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto out;
533
534 ret = -ENOMEM;
535 page = (char *)__get_free_page(GFP_USER);
536 if (!page)
537 goto out;
538
539 ret = 0;
540
541 mm = get_task_mm(task);
542 if (!mm)
543 goto out_free;
544
545 ret = -EIO;
546
547 if (file->private_data != (void*)((long)current->self_exec_id))
548 goto out_put;
549
550 ret = 0;
551
552 while (count > 0) {
553 int this_len, retval;
554
555 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
556 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700557 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (!ret)
559 ret = -EIO;
560 break;
561 }
562
563 if (copy_to_user(buf, page, retval)) {
564 ret = -EFAULT;
565 break;
566 }
567
568 ret += retval;
569 src += retval;
570 buf += retval;
571 count -= retval;
572 }
573 *ppos = src;
574
575out_put:
576 mmput(mm);
577out_free:
578 free_page((unsigned long) page);
579out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700580 put_task_struct(task);
581out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return ret;
583}
584
585#define mem_write NULL
586
587#ifndef mem_write
588/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800589static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 size_t count, loff_t *ppos)
591{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700592 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800594 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 unsigned long dst = *ppos;
596
Eric W. Biederman99f89552006-06-26 00:25:55 -0700597 copied = -ESRCH;
598 if (!task)
599 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Eric W. Biederman99f89552006-06-26 00:25:55 -0700601 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
602 goto out;
603
604 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 page = (char *)__get_free_page(GFP_USER);
606 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700607 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700609 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 while (count > 0) {
611 int this_len, retval;
612
613 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
614 if (copy_from_user(page, buf, this_len)) {
615 copied = -EFAULT;
616 break;
617 }
618 retval = access_process_vm(task, dst, page, this_len, 1);
619 if (!retval) {
620 if (!copied)
621 copied = -EIO;
622 break;
623 }
624 copied += retval;
625 buf += retval;
626 dst += retval;
627 count -= retval;
628 }
629 *ppos = dst;
630 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700631out:
632 put_task_struct(task);
633out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return copied;
635}
636#endif
637
638static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
639{
640 switch (orig) {
641 case 0:
642 file->f_pos = offset;
643 break;
644 case 1:
645 file->f_pos += offset;
646 break;
647 default:
648 return -EINVAL;
649 }
650 force_successful_syscall_return();
651 return file->f_pos;
652}
653
Arjan van de Ven00977a52007-02-12 00:55:34 -0800654static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 .llseek = mem_lseek,
656 .read = mem_read,
657 .write = mem_write,
658 .open = mem_open,
659};
660
661static ssize_t oom_adjust_read(struct file *file, char __user *buf,
662 size_t count, loff_t *ppos)
663{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800664 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700665 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700667 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Eric W. Biederman99f89552006-06-26 00:25:55 -0700669 if (!task)
670 return -ESRCH;
671 oom_adjust = task->oomkilladj;
672 put_task_struct(task);
673
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700674 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700675
676 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
680 size_t count, loff_t *ppos)
681{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700682 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700683 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int oom_adjust;
685
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700686 memset(buffer, 0, sizeof(buffer));
687 if (count > sizeof(buffer) - 1)
688 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (copy_from_user(buffer, buf, count))
690 return -EFAULT;
691 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700692 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
693 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EINVAL;
695 if (*end == '\n')
696 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800697 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700698 if (!task)
699 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800700 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
701 put_task_struct(task);
702 return -EACCES;
703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700705 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (end - buffer == 0)
707 return -EIO;
708 return end - buffer;
709}
710
Arjan van de Ven00977a52007-02-12 00:55:34 -0800711static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 .read = oom_adjust_read,
713 .write = oom_adjust_write,
714};
715
David Rientjes4b8df892007-05-08 20:27:01 -0700716#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -0700717static ssize_t clear_refs_write(struct file *file, const char __user *buf,
718 size_t count, loff_t *ppos)
719{
720 struct task_struct *task;
721 char buffer[PROC_NUMBUF], *end;
722 struct mm_struct *mm;
723
724 memset(buffer, 0, sizeof(buffer));
725 if (count > sizeof(buffer) - 1)
726 count = sizeof(buffer) - 1;
727 if (copy_from_user(buffer, buf, count))
728 return -EFAULT;
729 if (!simple_strtol(buffer, &end, 0))
730 return -EINVAL;
731 if (*end == '\n')
732 end++;
733 task = get_proc_task(file->f_path.dentry->d_inode);
734 if (!task)
735 return -ESRCH;
736 mm = get_task_mm(task);
737 if (mm) {
738 clear_refs_smap(mm);
739 mmput(mm);
740 }
741 put_task_struct(task);
742 if (end - buffer == 0)
743 return -EIO;
744 return end - buffer;
745}
746
747static struct file_operations proc_clear_refs_operations = {
748 .write = clear_refs_write,
749};
David Rientjes4b8df892007-05-08 20:27:01 -0700750#endif
David Rientjesb813e932007-05-06 14:49:24 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752#ifdef CONFIG_AUDITSYSCALL
753#define TMPBUFLEN 21
754static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
755 size_t count, loff_t *ppos)
756{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800757 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700758 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ssize_t length;
760 char tmpbuf[TMPBUFLEN];
761
Eric W. Biederman99f89552006-06-26 00:25:55 -0700762 if (!task)
763 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
765 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700766 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
768}
769
770static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
771 size_t count, loff_t *ppos)
772{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800773 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 char *page, *tmp;
775 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 uid_t loginuid;
777
778 if (!capable(CAP_AUDIT_CONTROL))
779 return -EPERM;
780
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700781 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EPERM;
783
Al Viroe0182902006-05-18 08:28:02 -0400784 if (count >= PAGE_SIZE)
785 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (*ppos != 0) {
788 /* No partial writes. */
789 return -EINVAL;
790 }
791 page = (char*)__get_free_page(GFP_USER);
792 if (!page)
793 return -ENOMEM;
794 length = -EFAULT;
795 if (copy_from_user(page, buf, count))
796 goto out_free_page;
797
Al Viroe0182902006-05-18 08:28:02 -0400798 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 loginuid = simple_strtoul(page, &tmp, 10);
800 if (tmp == page) {
801 length = -EINVAL;
802 goto out_free_page;
803
804 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700805 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (likely(length == 0))
807 length = count;
808
809out_free_page:
810 free_page((unsigned long) page);
811 return length;
812}
813
Arjan van de Ven00977a52007-02-12 00:55:34 -0800814static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 .read = proc_loginuid_read,
816 .write = proc_loginuid_write,
817};
818#endif
819
820#ifdef CONFIG_SECCOMP
821static ssize_t seccomp_read(struct file *file, char __user *buf,
822 size_t count, loff_t *ppos)
823{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700824 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 char __buf[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 size_t len;
827
Eric W. Biederman99f89552006-06-26 00:25:55 -0700828 if (!tsk)
829 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* no need to print the trailing zero, so use only len */
831 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700832 put_task_struct(tsk);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700833
834 return simple_read_from_buffer(buf, count, ppos, __buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837static ssize_t seccomp_write(struct file *file, const char __user *buf,
838 size_t count, loff_t *ppos)
839{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700840 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 char __buf[20], *end;
842 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700843 ssize_t result;
844
845 result = -ESRCH;
846 if (!tsk)
847 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700850 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700852 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Eric W. Biederman99f89552006-06-26 00:25:55 -0700854 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 memset(__buf, 0, sizeof(__buf));
856 count = min(count, sizeof(__buf) - 1);
857 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700858 goto out;
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 seccomp_mode = simple_strtoul(__buf, &end, 0);
861 if (*end == '\n')
862 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700863 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
865 tsk->seccomp.mode = seccomp_mode;
866 set_tsk_thread_flag(tsk, TIF_SECCOMP);
867 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700868 goto out;
869 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700871 goto out;
872 result = end - __buf;
873out:
874 put_task_struct(tsk);
875out_no_task:
876 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Arjan van de Ven00977a52007-02-12 00:55:34 -0800879static const struct file_operations proc_seccomp_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 .read = seccomp_read,
881 .write = seccomp_write,
882};
883#endif /* CONFIG_SECCOMP */
884
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800885#ifdef CONFIG_FAULT_INJECTION
886static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
887 size_t count, loff_t *ppos)
888{
889 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
890 char buffer[PROC_NUMBUF];
891 size_t len;
892 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800893
894 if (!task)
895 return -ESRCH;
896 make_it_fail = task->make_it_fail;
897 put_task_struct(task);
898
899 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700900
901 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800902}
903
904static ssize_t proc_fault_inject_write(struct file * file,
905 const char __user * buf, size_t count, loff_t *ppos)
906{
907 struct task_struct *task;
908 char buffer[PROC_NUMBUF], *end;
909 int make_it_fail;
910
911 if (!capable(CAP_SYS_RESOURCE))
912 return -EPERM;
913 memset(buffer, 0, sizeof(buffer));
914 if (count > sizeof(buffer) - 1)
915 count = sizeof(buffer) - 1;
916 if (copy_from_user(buffer, buf, count))
917 return -EFAULT;
918 make_it_fail = simple_strtol(buffer, &end, 0);
919 if (*end == '\n')
920 end++;
921 task = get_proc_task(file->f_dentry->d_inode);
922 if (!task)
923 return -ESRCH;
924 task->make_it_fail = make_it_fail;
925 put_task_struct(task);
926 if (end - buffer == 0)
927 return -EIO;
928 return end - buffer;
929}
930
Arjan van de Ven00977a52007-02-12 00:55:34 -0800931static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800932 .read = proc_fault_inject_read,
933 .write = proc_fault_inject_write,
934};
935#endif
936
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200937#ifdef CONFIG_SCHED_DEBUG
938/*
939 * Print out various scheduling related per-task fields:
940 */
941static int sched_show(struct seq_file *m, void *v)
942{
943 struct inode *inode = m->private;
944 struct task_struct *p;
945
946 WARN_ON(!inode);
947
948 p = get_proc_task(inode);
949 if (!p)
950 return -ESRCH;
951 proc_sched_show_task(p, m);
952
953 put_task_struct(p);
954
955 return 0;
956}
957
958static ssize_t
959sched_write(struct file *file, const char __user *buf,
960 size_t count, loff_t *offset)
961{
962 struct inode *inode = file->f_path.dentry->d_inode;
963 struct task_struct *p;
964
965 WARN_ON(!inode);
966
967 p = get_proc_task(inode);
968 if (!p)
969 return -ESRCH;
970 proc_sched_set_task(p);
971
972 put_task_struct(p);
973
974 return count;
975}
976
977static int sched_open(struct inode *inode, struct file *filp)
978{
979 int ret;
980
981 ret = single_open(filp, sched_show, NULL);
982 if (!ret) {
983 struct seq_file *m = filp->private_data;
984
985 m->private = inode;
986 }
987 return ret;
988}
989
990static const struct file_operations proc_pid_sched_operations = {
991 .open = sched_open,
992 .read = seq_read,
993 .write = sched_write,
994 .llseek = seq_lseek,
995 .release = seq_release,
996};
997
998#endif
999
Al Viro008b1502005-08-20 00:17:39 +01001000static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct inode *inode = dentry->d_inode;
1003 int error = -EACCES;
1004
1005 /* We don't need a base pointer in the /proc filesystem */
1006 path_release(nd);
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, &nd->dentry, &nd->mnt);
1013 nd->last_type = LAST_BIND;
1014out:
Al Viro008b1502005-08-20 00:17:39 +01001015 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1019 char __user *buffer, int buflen)
1020{
1021 struct inode * inode;
1022 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1023 int len;
1024
1025 if (!tmp)
1026 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 inode = dentry->d_inode;
1029 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1030 len = PTR_ERR(path);
1031 if (IS_ERR(path))
1032 goto out;
1033 len = tmp + PAGE_SIZE - 1 - path;
1034
1035 if (len > buflen)
1036 len = buflen;
1037 if (copy_to_user(buffer, path, len))
1038 len = -EFAULT;
1039 out:
1040 free_page((unsigned long)tmp);
1041 return len;
1042}
1043
1044static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1045{
1046 int error = -EACCES;
1047 struct inode *inode = dentry->d_inode;
1048 struct dentry *de;
1049 struct vfsmount *mnt = NULL;
1050
Eric W. Biederman778c1142006-06-26 00:25:58 -07001051 /* Are we allowed to snoop on the tasks file descriptors? */
1052 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1056 if (error)
1057 goto out;
1058
1059 error = do_proc_readlink(de, mnt, buffer, buflen);
1060 dput(de);
1061 mntput(mnt);
1062out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return error;
1064}
1065
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001066static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001068 .follow_link = proc_pid_follow_link,
1069 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070};
1071
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001072
1073/* building an inode */
1074
1075static int task_dumpable(struct task_struct *task)
1076{
1077 int dumpable = 0;
1078 struct mm_struct *mm;
1079
1080 task_lock(task);
1081 mm = task->mm;
1082 if (mm)
1083 dumpable = mm->dumpable;
1084 task_unlock(task);
1085 if(dumpable == 1)
1086 return 1;
1087 return 0;
1088}
1089
1090
Eric W. Biederman61a28782006-10-02 02:18:49 -07001091static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001092{
1093 struct inode * inode;
1094 struct proc_inode *ei;
1095
1096 /* We need a new inode */
1097
1098 inode = new_inode(sb);
1099 if (!inode)
1100 goto out;
1101
1102 /* Common stuff */
1103 ei = PROC_I(inode);
1104 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001105 inode->i_op = &proc_def_inode_operations;
1106
1107 /*
1108 * grab the reference to task.
1109 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001110 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001111 if (!ei->pid)
1112 goto out_unlock;
1113
1114 inode->i_uid = 0;
1115 inode->i_gid = 0;
1116 if (task_dumpable(task)) {
1117 inode->i_uid = task->euid;
1118 inode->i_gid = task->egid;
1119 }
1120 security_task_to_inode(task, inode);
1121
1122out:
1123 return inode;
1124
1125out_unlock:
1126 iput(inode);
1127 return NULL;
1128}
1129
1130static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1131{
1132 struct inode *inode = dentry->d_inode;
1133 struct task_struct *task;
1134 generic_fillattr(inode, stat);
1135
1136 rcu_read_lock();
1137 stat->uid = 0;
1138 stat->gid = 0;
1139 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1140 if (task) {
1141 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1142 task_dumpable(task)) {
1143 stat->uid = task->euid;
1144 stat->gid = task->egid;
1145 }
1146 }
1147 rcu_read_unlock();
1148 return 0;
1149}
1150
1151/* dentry stuff */
1152
1153/*
1154 * Exceptional case: normally we are not allowed to unhash a busy
1155 * directory. In this case, however, we can do it - no aliasing problems
1156 * due to the way we treat inodes.
1157 *
1158 * Rewrite the inode's ownerships here because the owning task may have
1159 * performed a setuid(), etc.
1160 *
1161 * Before the /proc/pid/status file was created the only way to read
1162 * the effective uid of a /process was to stat /proc/pid. Reading
1163 * /proc/pid/status is slow enough that procps and other packages
1164 * kept stating /proc/pid. To keep the rules in /proc simple I have
1165 * made this apply to all per process world readable and executable
1166 * directories.
1167 */
1168static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1169{
1170 struct inode *inode = dentry->d_inode;
1171 struct task_struct *task = get_proc_task(inode);
1172 if (task) {
1173 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1174 task_dumpable(task)) {
1175 inode->i_uid = task->euid;
1176 inode->i_gid = task->egid;
1177 } else {
1178 inode->i_uid = 0;
1179 inode->i_gid = 0;
1180 }
1181 inode->i_mode &= ~(S_ISUID | S_ISGID);
1182 security_task_to_inode(task, inode);
1183 put_task_struct(task);
1184 return 1;
1185 }
1186 d_drop(dentry);
1187 return 0;
1188}
1189
1190static int pid_delete_dentry(struct dentry * dentry)
1191{
1192 /* Is the task we represent dead?
1193 * If so, then don't put the dentry on the lru list,
1194 * kill it immediately.
1195 */
1196 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1197}
1198
1199static struct dentry_operations pid_dentry_operations =
1200{
1201 .d_revalidate = pid_revalidate,
1202 .d_delete = pid_delete_dentry,
1203};
1204
1205/* Lookups */
1206
Eric Dumazetc5141e62007-05-08 00:26:15 -07001207typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1208 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001209
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001210/*
1211 * Fill a directory entry.
1212 *
1213 * If possible create the dcache entry and derive our inode number and
1214 * file type from dcache entry.
1215 *
1216 * Since all of the proc inode numbers are dynamically generated, the inode
1217 * numbers do not exist until the inode is cache. This means creating the
1218 * the dcache entry in readdir is necessary to keep the inode numbers
1219 * reported by readdir in sync with the inode numbers reported
1220 * by stat.
1221 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001222static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1223 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001224 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001225{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001226 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001227 struct inode *inode;
1228 struct qstr qname;
1229 ino_t ino = 0;
1230 unsigned type = DT_UNKNOWN;
1231
1232 qname.name = name;
1233 qname.len = len;
1234 qname.hash = full_name_hash(name, len);
1235
1236 child = d_lookup(dir, &qname);
1237 if (!child) {
1238 struct dentry *new;
1239 new = d_alloc(dir, &qname);
1240 if (new) {
1241 child = instantiate(dir->d_inode, new, task, ptr);
1242 if (child)
1243 dput(new);
1244 else
1245 child = new;
1246 }
1247 }
1248 if (!child || IS_ERR(child) || !child->d_inode)
1249 goto end_instantiate;
1250 inode = child->d_inode;
1251 if (inode) {
1252 ino = inode->i_ino;
1253 type = inode->i_mode >> 12;
1254 }
1255 dput(child);
1256end_instantiate:
1257 if (!ino)
1258 ino = find_inode_number(dir, &qname);
1259 if (!ino)
1260 ino = 1;
1261 return filldir(dirent, name, len, filp->f_pos, ino, type);
1262}
1263
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001264static unsigned name_to_int(struct dentry *dentry)
1265{
1266 const char *name = dentry->d_name.name;
1267 int len = dentry->d_name.len;
1268 unsigned n = 0;
1269
1270 if (len > 1 && *name == '0')
1271 goto out;
1272 while (len-- > 0) {
1273 unsigned c = *name++ - '0';
1274 if (c > 9)
1275 goto out;
1276 if (n >= (~0U-9)/10)
1277 goto out;
1278 n *= 10;
1279 n += c;
1280 }
1281 return n;
1282out:
1283 return ~0U;
1284}
1285
Miklos Szeredi27932742007-05-08 00:26:17 -07001286#define PROC_FDINFO_MAX 64
1287
1288static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1289 struct vfsmount **mnt, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001290{
1291 struct task_struct *task = get_proc_task(inode);
1292 struct files_struct *files = NULL;
1293 struct file *file;
1294 int fd = proc_fd(inode);
1295
1296 if (task) {
1297 files = get_files_struct(task);
1298 put_task_struct(task);
1299 }
1300 if (files) {
1301 /*
1302 * We are not taking a ref to the file structure, so we must
1303 * hold ->file_lock.
1304 */
1305 spin_lock(&files->file_lock);
1306 file = fcheck_files(files, fd);
1307 if (file) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001308 if (mnt)
1309 *mnt = mntget(file->f_path.mnt);
1310 if (dentry)
1311 *dentry = dget(file->f_path.dentry);
1312 if (info)
1313 snprintf(info, PROC_FDINFO_MAX,
1314 "pos:\t%lli\n"
1315 "flags:\t0%o\n",
1316 (long long) file->f_pos,
1317 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001318 spin_unlock(&files->file_lock);
1319 put_files_struct(files);
1320 return 0;
1321 }
1322 spin_unlock(&files->file_lock);
1323 put_files_struct(files);
1324 }
1325 return -ENOENT;
1326}
1327
Miklos Szeredi27932742007-05-08 00:26:17 -07001328static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1329 struct vfsmount **mnt)
1330{
1331 return proc_fd_info(inode, dentry, mnt, NULL);
1332}
1333
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001334static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1335{
1336 struct inode *inode = dentry->d_inode;
1337 struct task_struct *task = get_proc_task(inode);
1338 int fd = proc_fd(inode);
1339 struct files_struct *files;
1340
1341 if (task) {
1342 files = get_files_struct(task);
1343 if (files) {
1344 rcu_read_lock();
1345 if (fcheck_files(files, fd)) {
1346 rcu_read_unlock();
1347 put_files_struct(files);
1348 if (task_dumpable(task)) {
1349 inode->i_uid = task->euid;
1350 inode->i_gid = task->egid;
1351 } else {
1352 inode->i_uid = 0;
1353 inode->i_gid = 0;
1354 }
1355 inode->i_mode &= ~(S_ISUID | S_ISGID);
1356 security_task_to_inode(task, inode);
1357 put_task_struct(task);
1358 return 1;
1359 }
1360 rcu_read_unlock();
1361 put_files_struct(files);
1362 }
1363 put_task_struct(task);
1364 }
1365 d_drop(dentry);
1366 return 0;
1367}
1368
1369static struct dentry_operations tid_fd_dentry_operations =
1370{
1371 .d_revalidate = tid_fd_revalidate,
1372 .d_delete = pid_delete_dentry,
1373};
1374
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001375static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001376 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001377{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001378 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001379 struct file *file;
1380 struct files_struct *files;
1381 struct inode *inode;
1382 struct proc_inode *ei;
1383 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001384
Eric W. Biederman61a28782006-10-02 02:18:49 -07001385 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001386 if (!inode)
1387 goto out;
1388 ei = PROC_I(inode);
1389 ei->fd = fd;
1390 files = get_files_struct(task);
1391 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001392 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001393 inode->i_mode = S_IFLNK;
1394
1395 /*
1396 * We are not taking a ref to the file structure, so we must
1397 * hold ->file_lock.
1398 */
1399 spin_lock(&files->file_lock);
1400 file = fcheck_files(files, fd);
1401 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001402 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001403 if (file->f_mode & 1)
1404 inode->i_mode |= S_IRUSR | S_IXUSR;
1405 if (file->f_mode & 2)
1406 inode->i_mode |= S_IWUSR | S_IXUSR;
1407 spin_unlock(&files->file_lock);
1408 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001409
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001410 inode->i_op = &proc_pid_link_inode_operations;
1411 inode->i_size = 64;
1412 ei->op.proc_get_link = proc_fd_link;
1413 dentry->d_op = &tid_fd_dentry_operations;
1414 d_add(dentry, inode);
1415 /* Close the race of the process dying before we return the dentry */
1416 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001417 error = NULL;
1418
1419 out:
1420 return error;
1421out_unlock:
1422 spin_unlock(&files->file_lock);
1423 put_files_struct(files);
1424out_iput:
1425 iput(inode);
1426 goto out;
1427}
1428
Miklos Szeredi27932742007-05-08 00:26:17 -07001429static struct dentry *proc_lookupfd_common(struct inode *dir,
1430 struct dentry *dentry,
1431 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001432{
1433 struct task_struct *task = get_proc_task(dir);
1434 unsigned fd = name_to_int(dentry);
1435 struct dentry *result = ERR_PTR(-ENOENT);
1436
1437 if (!task)
1438 goto out_no_task;
1439 if (fd == ~0U)
1440 goto out;
1441
Miklos Szeredi27932742007-05-08 00:26:17 -07001442 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001443out:
1444 put_task_struct(task);
1445out_no_task:
1446 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001447}
1448
Miklos Szeredi27932742007-05-08 00:26:17 -07001449static int proc_readfd_common(struct file * filp, void * dirent,
1450 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001452 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001453 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001454 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 unsigned int fd, tid, ino;
1456 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001458 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001461 if (!p)
1462 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 retval = 0;
1464 tid = p->pid;
1465
1466 fd = filp->f_pos;
1467 switch (fd) {
1468 case 0:
1469 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1470 goto out;
1471 filp->f_pos++;
1472 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001473 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1475 goto out;
1476 filp->f_pos++;
1477 default:
1478 files = get_files_struct(p);
1479 if (!files)
1480 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001481 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001482 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001484 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001486 char name[PROC_NUMBUF];
1487 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (!fcheck_files(files, fd))
1490 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001491 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Miklos Szeredi27932742007-05-08 00:26:17 -07001493 len = snprintf(name, sizeof(name), "%d", fd);
1494 if (proc_fill_cache(filp, dirent, filldir,
1495 name, len, instantiate,
1496 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001497 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 break;
1499 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001500 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001502 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 put_files_struct(files);
1504 }
1505out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001506 put_task_struct(p);
1507out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return retval;
1509}
1510
Miklos Szeredi27932742007-05-08 00:26:17 -07001511static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1512 struct nameidata *nd)
1513{
1514 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1515}
1516
1517static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1518{
1519 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1520}
1521
1522static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1523 size_t len, loff_t *ppos)
1524{
1525 char tmp[PROC_FDINFO_MAX];
1526 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1527 if (!err)
1528 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1529 return err;
1530}
1531
1532static const struct file_operations proc_fdinfo_file_operations = {
1533 .open = nonseekable_open,
1534 .read = proc_fdinfo_read,
1535};
1536
Arjan van de Ven00977a52007-02-12 00:55:34 -08001537static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 .read = generic_read_dir,
1539 .readdir = proc_readfd,
1540};
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001543 * /proc/pid/fd needs a special permission handler so that a process can still
1544 * access /proc/self/fd after it has executed a setuid().
1545 */
1546static int proc_fd_permission(struct inode *inode, int mask,
1547 struct nameidata *nd)
1548{
1549 int rv;
1550
1551 rv = generic_permission(inode, mask, NULL);
1552 if (rv == 0)
1553 return 0;
1554 if (task_pid(current) == proc_pid(inode))
1555 rv = 0;
1556 return rv;
1557}
1558
1559/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 * proc directories can do almost nothing..
1561 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001562static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001564 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001565 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566};
1567
Miklos Szeredi27932742007-05-08 00:26:17 -07001568static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1569 struct dentry *dentry, struct task_struct *task, const void *ptr)
1570{
1571 unsigned fd = *(unsigned *)ptr;
1572 struct inode *inode;
1573 struct proc_inode *ei;
1574 struct dentry *error = ERR_PTR(-ENOENT);
1575
1576 inode = proc_pid_make_inode(dir->i_sb, task);
1577 if (!inode)
1578 goto out;
1579 ei = PROC_I(inode);
1580 ei->fd = fd;
1581 inode->i_mode = S_IFREG | S_IRUSR;
1582 inode->i_fop = &proc_fdinfo_file_operations;
1583 dentry->d_op = &tid_fd_dentry_operations;
1584 d_add(dentry, inode);
1585 /* Close the race of the process dying before we return the dentry */
1586 if (tid_fd_revalidate(dentry, NULL))
1587 error = NULL;
1588
1589 out:
1590 return error;
1591}
1592
1593static struct dentry *proc_lookupfdinfo(struct inode *dir,
1594 struct dentry *dentry,
1595 struct nameidata *nd)
1596{
1597 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1598}
1599
1600static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1601{
1602 return proc_readfd_common(filp, dirent, filldir,
1603 proc_fdinfo_instantiate);
1604}
1605
1606static const struct file_operations proc_fdinfo_operations = {
1607 .read = generic_read_dir,
1608 .readdir = proc_readfdinfo,
1609};
1610
1611/*
1612 * proc directories can do almost nothing..
1613 */
1614static const struct inode_operations proc_fdinfo_inode_operations = {
1615 .lookup = proc_lookupfdinfo,
1616 .setattr = proc_setattr,
1617};
1618
1619
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001620static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001621 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001622{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001623 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001624 struct inode *inode;
1625 struct proc_inode *ei;
1626 struct dentry *error = ERR_PTR(-EINVAL);
1627
Eric W. Biederman61a28782006-10-02 02:18:49 -07001628 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001629 if (!inode)
1630 goto out;
1631
1632 ei = PROC_I(inode);
1633 inode->i_mode = p->mode;
1634 if (S_ISDIR(inode->i_mode))
1635 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1636 if (p->iop)
1637 inode->i_op = p->iop;
1638 if (p->fop)
1639 inode->i_fop = p->fop;
1640 ei->op = p->op;
1641 dentry->d_op = &pid_dentry_operations;
1642 d_add(dentry, inode);
1643 /* Close the race of the process dying before we return the dentry */
1644 if (pid_revalidate(dentry, NULL))
1645 error = NULL;
1646out:
1647 return error;
1648}
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650static struct dentry *proc_pident_lookup(struct inode *dir,
1651 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001652 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001653 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001656 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001657 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001658 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001660 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 inode = NULL;
1662
Eric W. Biederman99f89552006-06-26 00:25:55 -07001663 if (!task)
1664 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001666 /*
1667 * Yes, it does not scale. And it should not. Don't add
1668 * new entries into /proc/<tgid>/ without very good reasons.
1669 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001670 last = &ents[nents - 1];
1671 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (p->len != dentry->d_name.len)
1673 continue;
1674 if (!memcmp(dentry->d_name.name, p->name, p->len))
1675 break;
1676 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001677 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 goto out;
1679
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001680 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001682 put_task_struct(task);
1683out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001684 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Eric Dumazetc5141e62007-05-08 00:26:15 -07001687static int proc_pident_fill_cache(struct file *filp, void *dirent,
1688 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001689{
1690 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1691 proc_pident_instantiate, task, p);
1692}
1693
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001694static int proc_pident_readdir(struct file *filp,
1695 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001696 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001697{
1698 int i;
1699 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001700 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001701 struct inode *inode = dentry->d_inode;
1702 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001703 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001704 ino_t ino;
1705 int ret;
1706
1707 ret = -ENOENT;
1708 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001709 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001710
1711 ret = 0;
1712 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001713 i = filp->f_pos;
1714 switch (i) {
1715 case 0:
1716 ino = inode->i_ino;
1717 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1718 goto out;
1719 i++;
1720 filp->f_pos++;
1721 /* fall through */
1722 case 1:
1723 ino = parent_ino(dentry);
1724 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1725 goto out;
1726 i++;
1727 filp->f_pos++;
1728 /* fall through */
1729 default:
1730 i -= 2;
1731 if (i >= nents) {
1732 ret = 1;
1733 goto out;
1734 }
1735 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001736 last = &ents[nents - 1];
1737 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001738 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001739 goto out;
1740 filp->f_pos++;
1741 p++;
1742 }
1743 }
1744
1745 ret = 1;
1746out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001747 put_task_struct(task);
1748out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001749 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001753static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1754 size_t count, loff_t *ppos)
1755{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001756 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001757 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001758 ssize_t length;
1759 struct task_struct *task = get_proc_task(inode);
1760
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001762 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001763
1764 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001765 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001766 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001767 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001768 if (length > 0)
1769 length = simple_read_from_buffer(buf, count, ppos, p, length);
1770 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001771 return length;
1772}
1773
1774static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1775 size_t count, loff_t *ppos)
1776{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001777 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778 char *page;
1779 ssize_t length;
1780 struct task_struct *task = get_proc_task(inode);
1781
1782 length = -ESRCH;
1783 if (!task)
1784 goto out_no_task;
1785 if (count > PAGE_SIZE)
1786 count = PAGE_SIZE;
1787
1788 /* No partial writes. */
1789 length = -EINVAL;
1790 if (*ppos != 0)
1791 goto out;
1792
1793 length = -ENOMEM;
1794 page = (char*)__get_free_page(GFP_USER);
1795 if (!page)
1796 goto out;
1797
1798 length = -EFAULT;
1799 if (copy_from_user(page, buf, count))
1800 goto out_free;
1801
1802 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001803 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001804 (void*)page, count);
1805out_free:
1806 free_page((unsigned long) page);
1807out:
1808 put_task_struct(task);
1809out_no_task:
1810 return length;
1811}
1812
Arjan van de Ven00977a52007-02-12 00:55:34 -08001813static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001814 .read = proc_pid_attr_read,
1815 .write = proc_pid_attr_write,
1816};
1817
Eric Dumazetc5141e62007-05-08 00:26:15 -07001818static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001819 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1820 REG("prev", S_IRUGO, pid_attr),
1821 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1822 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1823 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1824 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001825};
1826
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001827static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 void * dirent, filldir_t filldir)
1829{
1830 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001831 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Arjan van de Ven00977a52007-02-12 00:55:34 -08001834static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001836 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837};
1838
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001839static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 struct dentry *dentry, struct nameidata *nd)
1841{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001842 return proc_pident_lookup(dir, dentry,
1843 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001846static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001847 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001848 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001849 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850};
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852#endif
1853
1854/*
1855 * /proc/self:
1856 */
1857static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1858 int buflen)
1859{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001860 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 sprintf(tmp, "%d", current->tgid);
1862 return vfs_readlink(dentry,buffer,buflen,tmp);
1863}
1864
Al Viro008b1502005-08-20 00:17:39 +01001865static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001867 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001869 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001870}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001872static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 .readlink = proc_self_readlink,
1874 .follow_link = proc_self_follow_link,
1875};
1876
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001877/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001878 * proc base
1879 *
1880 * These are the directory entries in the root directory of /proc
1881 * that properly belong to the /proc filesystem, as they describe
1882 * describe something that is process related.
1883 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07001884static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001885 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001886 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001887};
1888
1889/*
1890 * Exceptional case: normally we are not allowed to unhash a busy
1891 * directory. In this case, however, we can do it - no aliasing problems
1892 * due to the way we treat inodes.
1893 */
1894static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1895{
1896 struct inode *inode = dentry->d_inode;
1897 struct task_struct *task = get_proc_task(inode);
1898 if (task) {
1899 put_task_struct(task);
1900 return 1;
1901 }
1902 d_drop(dentry);
1903 return 0;
1904}
1905
1906static struct dentry_operations proc_base_dentry_operations =
1907{
1908 .d_revalidate = proc_base_revalidate,
1909 .d_delete = pid_delete_dentry,
1910};
1911
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001912static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001913 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001914{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001915 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001916 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001917 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001918 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001919
1920 /* Allocate the inode */
1921 error = ERR_PTR(-ENOMEM);
1922 inode = new_inode(dir->i_sb);
1923 if (!inode)
1924 goto out;
1925
1926 /* Initialize the inode */
1927 ei = PROC_I(inode);
1928 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001929
1930 /*
1931 * grab the reference to the task.
1932 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001933 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001934 if (!ei->pid)
1935 goto out_iput;
1936
1937 inode->i_uid = 0;
1938 inode->i_gid = 0;
1939 inode->i_mode = p->mode;
1940 if (S_ISDIR(inode->i_mode))
1941 inode->i_nlink = 2;
1942 if (S_ISLNK(inode->i_mode))
1943 inode->i_size = 64;
1944 if (p->iop)
1945 inode->i_op = p->iop;
1946 if (p->fop)
1947 inode->i_fop = p->fop;
1948 ei->op = p->op;
1949 dentry->d_op = &proc_base_dentry_operations;
1950 d_add(dentry, inode);
1951 error = NULL;
1952out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001953 return error;
1954out_iput:
1955 iput(inode);
1956 goto out;
1957}
1958
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001959static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1960{
1961 struct dentry *error;
1962 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001963 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001964
1965 error = ERR_PTR(-ENOENT);
1966
1967 if (!task)
1968 goto out_no_task;
1969
1970 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001971 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1972 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001973 if (p->len != dentry->d_name.len)
1974 continue;
1975 if (!memcmp(dentry->d_name.name, p->name, p->len))
1976 break;
1977 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001978 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001979 goto out;
1980
1981 error = proc_base_instantiate(dir, dentry, task, p);
1982
1983out:
1984 put_task_struct(task);
1985out_no_task:
1986 return error;
1987}
1988
Eric Dumazetc5141e62007-05-08 00:26:15 -07001989static int proc_base_fill_cache(struct file *filp, void *dirent,
1990 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001991{
1992 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1993 proc_base_instantiate, task, p);
1994}
1995
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001996#ifdef CONFIG_TASK_IO_ACCOUNTING
1997static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1998{
1999 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002000#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002001 "rchar: %llu\n"
2002 "wchar: %llu\n"
2003 "syscr: %llu\n"
2004 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002005#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002006 "read_bytes: %llu\n"
2007 "write_bytes: %llu\n"
2008 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002009#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002010 (unsigned long long)task->rchar,
2011 (unsigned long long)task->wchar,
2012 (unsigned long long)task->syscr,
2013 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002014#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002015 (unsigned long long)task->ioac.read_bytes,
2016 (unsigned long long)task->ioac.write_bytes,
2017 (unsigned long long)task->ioac.cancelled_write_bytes);
2018}
2019#endif
2020
Eric W. Biederman801199c2006-10-02 02:18:48 -07002021/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002022 * Thread groups
2023 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002024static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002025static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002026
Eric Dumazetc5141e62007-05-08 00:26:15 -07002027static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002028 DIR("task", S_IRUGO|S_IXUGO, task),
2029 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002030 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002031 INF("environ", S_IRUSR, pid_environ),
2032 INF("auxv", S_IRUSR, pid_auxv),
2033 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002034#ifdef CONFIG_SCHED_DEBUG
2035 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2036#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002037 INF("cmdline", S_IRUGO, pid_cmdline),
2038 INF("stat", S_IRUGO, tgid_stat),
2039 INF("statm", S_IRUGO, pid_statm),
2040 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002041#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002042 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002043#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002044 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002045#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002046 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002047#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002048 LNK("cwd", cwd),
2049 LNK("root", root),
2050 LNK("exe", exe),
2051 REG("mounts", S_IRUGO, mounts),
2052 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002053#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002054 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002055 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002056#endif
2057#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002058 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002059#endif
2060#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002061 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002062#endif
2063#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002064 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002065#endif
2066#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002067 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002068#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002069 INF("oom_score", S_IRUGO, oom_score),
2070 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002071#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002072 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002073#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002074#ifdef CONFIG_FAULT_INJECTION
2075 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2076#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002077#ifdef CONFIG_TASK_IO_ACCOUNTING
2078 INF("io", S_IRUGO, pid_io_accounting),
2079#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002080};
2081
2082static int proc_tgid_base_readdir(struct file * filp,
2083 void * dirent, filldir_t filldir)
2084{
2085 return proc_pident_readdir(filp,dirent,filldir,
2086 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2087}
2088
Arjan van de Ven00977a52007-02-12 00:55:34 -08002089static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002090 .read = generic_read_dir,
2091 .readdir = proc_tgid_base_readdir,
2092};
2093
2094static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002095 return proc_pident_lookup(dir, dentry,
2096 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002097}
2098
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002099static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002100 .lookup = proc_tgid_base_lookup,
2101 .getattr = pid_getattr,
2102 .setattr = proc_setattr,
2103};
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07002106 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002108 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002110 * Looks in the dcache for
2111 * /proc/@pid
2112 * /proc/@tgid/task/@pid
2113 * if either directory is present flushes it and all of it'ts children
2114 * from the dcache.
2115 *
2116 * It is safe and reasonable to cache /proc entries for a task until
2117 * that task exits. After that they just clog up the dcache with
2118 * useless entries, possibly causing useful dcache entries to be
2119 * flushed instead. This routine is proved to flush those useless
2120 * dcache entries at process exit time.
2121 *
2122 * NOTE: This routine is just an optimization so it does not guarantee
2123 * that no dcache entries will exist at process exit time it
2124 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07002126void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002128 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002129 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002130 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Eric W. Biederman48e64842006-06-26 00:25:48 -07002132 name.name = buf;
2133 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2134 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2135 if (dentry) {
2136 shrink_dcache_parent(dentry);
2137 d_drop(dentry);
2138 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Eric W. Biederman48e64842006-06-26 00:25:48 -07002141 if (thread_group_leader(task))
2142 goto out;
2143
2144 name.name = buf;
2145 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2146 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2147 if (!leader)
2148 goto out;
2149
2150 name.name = "task";
2151 name.len = strlen(name.name);
2152 dir = d_hash_and_lookup(leader, &name);
2153 if (!dir)
2154 goto out_put_leader;
2155
2156 name.name = buf;
2157 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2158 dentry = d_hash_and_lookup(dir, &name);
2159 if (dentry) {
2160 shrink_dcache_parent(dentry);
2161 d_drop(dentry);
2162 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002164
2165 dput(dir);
2166out_put_leader:
2167 dput(leader);
2168out:
2169 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Adrian Bunk9711ef92006-12-06 20:38:31 -08002172static struct dentry *proc_pid_instantiate(struct inode *dir,
2173 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002174 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002175{
2176 struct dentry *error = ERR_PTR(-ENOENT);
2177 struct inode *inode;
2178
Eric W. Biederman61a28782006-10-02 02:18:49 -07002179 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002180 if (!inode)
2181 goto out;
2182
2183 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2184 inode->i_op = &proc_tgid_base_inode_operations;
2185 inode->i_fop = &proc_tgid_base_operations;
2186 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002187 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002188#ifdef CONFIG_SECURITY
2189 inode->i_nlink += 1;
2190#endif
2191
2192 dentry->d_op = &pid_dentry_operations;
2193
2194 d_add(dentry, inode);
2195 /* Close the race of the process dying before we return the dentry */
2196 if (pid_revalidate(dentry, NULL))
2197 error = NULL;
2198out:
2199 return error;
2200}
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2203{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002204 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Eric W. Biederman801199c2006-10-02 02:18:48 -07002208 result = proc_base_lookup(dir, dentry);
2209 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2210 goto out;
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 tgid = name_to_int(dentry);
2213 if (tgid == ~0U)
2214 goto out;
2215
Eric W. Biedermande758732006-06-26 00:25:51 -07002216 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 task = find_task_by_pid(tgid);
2218 if (task)
2219 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002220 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (!task)
2222 goto out;
2223
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002224 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002225 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002227 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002231 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002232 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002234static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002236 struct task_struct *task;
2237 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002239 rcu_read_lock();
2240retry:
2241 task = NULL;
2242 pid = find_ge_pid(tgid);
2243 if (pid) {
2244 tgid = pid->nr + 1;
2245 task = pid_task(pid, PIDTYPE_PID);
2246 /* What we to know is if the pid we have find is the
2247 * pid of a thread_group_leader. Testing for task
2248 * being a thread_group_leader is the obvious thing
2249 * todo but there is a window when it fails, due to
2250 * the pid transfer logic in de_thread.
2251 *
2252 * So we perform the straight forward test of seeing
2253 * if the pid we have found is the pid of a thread
2254 * group leader, and don't worry if the task we have
2255 * found doesn't happen to be a thread group leader.
2256 * As we don't care in the case of readdir.
2257 */
2258 if (!task || !has_group_leader_pid(task))
2259 goto retry;
2260 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002262 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002263 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002266#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Eric W. Biederman61a28782006-10-02 02:18:49 -07002268static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2269 struct task_struct *task, int tgid)
2270{
2271 char name[PROC_NUMBUF];
2272 int len = snprintf(name, sizeof(name), "%d", tgid);
2273 return proc_fill_cache(filp, dirent, filldir, name, len,
2274 proc_pid_instantiate, task, NULL);
2275}
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277/* for the /proc/ directory itself, after non-process stuff has been done */
2278int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2279{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002281 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002282 struct task_struct *task;
2283 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Eric W. Biederman61a28782006-10-02 02:18:49 -07002285 if (!reaper)
2286 goto out_no_task;
2287
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002288 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002289 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002290 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
2293
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002294 tgid = filp->f_pos - TGID_OFFSET;
2295 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002296 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002297 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002298 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002299 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002300 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002301 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002302 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002305 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2306out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002307 put_task_struct(reaper);
2308out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
2310}
2311
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002312/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002313 * Tasks
2314 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002315static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002316 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002317 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002318 INF("environ", S_IRUSR, pid_environ),
2319 INF("auxv", S_IRUSR, pid_auxv),
2320 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002321#ifdef CONFIG_SCHED_DEBUG
2322 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2323#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002324 INF("cmdline", S_IRUGO, pid_cmdline),
2325 INF("stat", S_IRUGO, tid_stat),
2326 INF("statm", S_IRUGO, pid_statm),
2327 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002328#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002329 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002330#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002331 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002332#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002333 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002334#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002335 LNK("cwd", cwd),
2336 LNK("root", root),
2337 LNK("exe", exe),
2338 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002339#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002340 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002341 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002342#endif
2343#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002344 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002345#endif
2346#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002347 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002348#endif
2349#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002350 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002351#endif
2352#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002353 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002354#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002355 INF("oom_score", S_IRUGO, oom_score),
2356 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002357#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002358 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002359#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002360#ifdef CONFIG_FAULT_INJECTION
2361 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2362#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002363};
2364
2365static int proc_tid_base_readdir(struct file * filp,
2366 void * dirent, filldir_t filldir)
2367{
2368 return proc_pident_readdir(filp,dirent,filldir,
2369 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2370}
2371
2372static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002373 return proc_pident_lookup(dir, dentry,
2374 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002375}
2376
Arjan van de Ven00977a52007-02-12 00:55:34 -08002377static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002378 .read = generic_read_dir,
2379 .readdir = proc_tid_base_readdir,
2380};
2381
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002382static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002383 .lookup = proc_tid_base_lookup,
2384 .getattr = pid_getattr,
2385 .setattr = proc_setattr,
2386};
2387
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002388static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002389 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002390{
2391 struct dentry *error = ERR_PTR(-ENOENT);
2392 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002393 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002394
2395 if (!inode)
2396 goto out;
2397 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2398 inode->i_op = &proc_tid_base_inode_operations;
2399 inode->i_fop = &proc_tid_base_operations;
2400 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002401 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002402#ifdef CONFIG_SECURITY
2403 inode->i_nlink += 1;
2404#endif
2405
2406 dentry->d_op = &pid_dentry_operations;
2407
2408 d_add(dentry, inode);
2409 /* Close the race of the process dying before we return the dentry */
2410 if (pid_revalidate(dentry, NULL))
2411 error = NULL;
2412out:
2413 return error;
2414}
2415
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002416static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2417{
2418 struct dentry *result = ERR_PTR(-ENOENT);
2419 struct task_struct *task;
2420 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002421 unsigned tid;
2422
2423 if (!leader)
2424 goto out_no_task;
2425
2426 tid = name_to_int(dentry);
2427 if (tid == ~0U)
2428 goto out;
2429
2430 rcu_read_lock();
2431 task = find_task_by_pid(tid);
2432 if (task)
2433 get_task_struct(task);
2434 rcu_read_unlock();
2435 if (!task)
2436 goto out;
2437 if (leader->tgid != task->tgid)
2438 goto out_drop_task;
2439
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002440 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002441out_drop_task:
2442 put_task_struct(task);
2443out:
2444 put_task_struct(leader);
2445out_no_task:
2446 return result;
2447}
2448
2449/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002450 * Find the first tid of a thread group to return to user space.
2451 *
2452 * Usually this is just the thread group leader, but if the users
2453 * buffer was too small or there was a seek into the middle of the
2454 * directory we have more work todo.
2455 *
2456 * In the case of a short read we start with find_task_by_pid.
2457 *
2458 * In the case of a seek we start with the leader and walk nr
2459 * threads past it.
2460 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002461static struct task_struct *first_tid(struct task_struct *leader,
2462 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002463{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002464 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002465
Eric W. Biedermancc288732006-06-26 00:26:01 -07002466 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002467 /* Attempt to start with the pid of a thread */
2468 if (tid && (nr > 0)) {
2469 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002470 if (pos && (pos->group_leader == leader))
2471 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002472 }
2473
2474 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002475 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002476 if (nr && nr >= get_nr_threads(leader))
2477 goto out;
2478
2479 /* If we haven't found our starting place yet start
2480 * with the leader and walk nr threads forward.
2481 */
2482 for (pos = leader; nr > 0; --nr) {
2483 pos = next_thread(pos);
2484 if (pos == leader) {
2485 pos = NULL;
2486 goto out;
2487 }
2488 }
2489found:
2490 get_task_struct(pos);
2491out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002492 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002493 return pos;
2494}
2495
2496/*
2497 * Find the next thread in the thread list.
2498 * Return NULL if there is an error or no next thread.
2499 *
2500 * The reference to the input task_struct is released.
2501 */
2502static struct task_struct *next_tid(struct task_struct *start)
2503{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002504 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002505 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002506 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002507 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002508 if (thread_group_leader(pos))
2509 pos = NULL;
2510 else
2511 get_task_struct(pos);
2512 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002513 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002514 put_task_struct(start);
2515 return pos;
2516}
2517
Eric W. Biederman61a28782006-10-02 02:18:49 -07002518static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2519 struct task_struct *task, int tid)
2520{
2521 char name[PROC_NUMBUF];
2522 int len = snprintf(name, sizeof(name), "%d", tid);
2523 return proc_fill_cache(filp, dirent, filldir, name, len,
2524 proc_task_instantiate, task, NULL);
2525}
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527/* for the /proc/TGID/task/ directories */
2528static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2529{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002530 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002532 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002533 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 int retval = -ENOENT;
2535 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002536 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2538
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002539 task = get_proc_task(inode);
2540 if (!task)
2541 goto out_no_task;
2542 rcu_read_lock();
2543 if (pid_alive(task)) {
2544 leader = task->group_leader;
2545 get_task_struct(leader);
2546 }
2547 rcu_read_unlock();
2548 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002549 if (!leader)
2550 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 retval = 0;
2552
2553 switch (pos) {
2554 case 0:
2555 ino = inode->i_ino;
2556 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2557 goto out;
2558 pos++;
2559 /* fall through */
2560 case 1:
2561 ino = parent_ino(dentry);
2562 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2563 goto out;
2564 pos++;
2565 /* fall through */
2566 }
2567
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002568 /* f_version caches the tgid value that the last readdir call couldn't
2569 * return. lseek aka telldir automagically resets f_version to 0.
2570 */
2571 tid = filp->f_version;
2572 filp->f_version = 0;
2573 for (task = first_tid(leader, tid, pos - 2);
2574 task;
2575 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002576 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002577 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002578 /* returning this tgid failed, save it as the first
2579 * pid for the next readir call */
2580 filp->f_version = tid;
2581 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
2585out:
2586 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002587 put_task_struct(leader);
2588out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return retval;
2590}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002591
2592static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2593{
2594 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002595 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002596 generic_fillattr(inode, stat);
2597
Eric W. Biederman99f89552006-06-26 00:25:55 -07002598 if (p) {
2599 rcu_read_lock();
2600 stat->nlink += get_nr_threads(p);
2601 rcu_read_unlock();
2602 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002603 }
2604
2605 return 0;
2606}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002607
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002608static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002609 .lookup = proc_task_lookup,
2610 .getattr = proc_task_getattr,
2611 .setattr = proc_setattr,
2612};
2613
Arjan van de Ven00977a52007-02-12 00:55:34 -08002614static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002615 .read = generic_read_dir,
2616 .readdir = proc_task_readdir,
2617};