blob: 17f7a7ee6c5e14aa00e2b9b8a999a8cfa89d21d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
64#include <linux/smp_lock.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070065#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kallsyms.h>
Kees Cook5096add2007-05-08 00:26:04 -070067#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/mount.h>
69#include <linux/security.h>
70#include <linux/ptrace.h>
71#include <linux/seccomp.h>
72#include <linux/cpuset.h>
73#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050074#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070075#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070076#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include "internal.h"
78
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070079/* NOTE:
80 * Implementing inode permission operations in /proc is almost
81 * certainly an error. Permission checks need to happen during
82 * each system call not at open time. The reason is that most of
83 * what we wish to check for permissions in /proc varies at runtime.
84 *
85 * The classic example of a problem is opening file descriptors
86 * in /proc for a task before it execs a suid executable.
87 */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Eric W. Biederman8578cea2006-06-26 00:25:54 -070090/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070091#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int len;
95 char *name;
96 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080097 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080098 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070099 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Eric W. Biederman61a28782006-10-02 02:18:49 -0700102#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .len = sizeof(NAME) - 1, \
104 .name = (NAME), \
105 .mode = MODE, \
106 .iop = IOP, \
107 .fop = FOP, \
108 .op = OP, \
109}
110
Eric W. Biederman61a28782006-10-02 02:18:49 -0700111#define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700115#define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119#define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700121 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700122#define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Kees Cook5096add2007-05-08 00:26:04 -0700127int maps_protect;
128EXPORT_SYMBOL(maps_protect);
129
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700130static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700133 task_lock(task);
134 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if(fs)
136 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700137 task_unlock(task);
138 return fs;
139}
140
Eric W. Biederman99f89552006-06-26 00:25:55 -0700141static int get_nr_threads(struct task_struct *tsk)
142{
143 /* Must be called with the rcu_read_lock held */
144 unsigned long flags;
145 int count = 0;
146
147 if (lock_task_sighand(tsk, &flags)) {
148 count = atomic_read(&tsk->signal->count);
149 unlock_task_sighand(tsk, &flags);
150 }
151 return count;
152}
153
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700154static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
155{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700156 struct task_struct *task = get_proc_task(inode);
157 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700158 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700159
160 if (task) {
161 fs = get_fs_struct(task);
162 put_task_struct(task);
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (fs) {
165 read_lock(&fs->lock);
166 *mnt = mntget(fs->pwdmnt);
167 *dentry = dget(fs->pwd);
168 read_unlock(&fs->lock);
169 result = 0;
170 put_fs_struct(fs);
171 }
172 return result;
173}
174
175static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
176{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177 struct task_struct *task = get_proc_task(inode);
178 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700180
181 if (task) {
182 fs = get_fs_struct(task);
183 put_task_struct(task);
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (fs) {
186 read_lock(&fs->lock);
187 *mnt = mntget(fs->rootmnt);
188 *dentry = dget(fs->root);
189 read_unlock(&fs->lock);
190 result = 0;
191 put_fs_struct(fs);
192 }
193 return result;
194}
195
196#define MAY_PTRACE(task) \
197 (task == current || \
198 (task->parent == current && \
199 (task->ptrace & PT_PTRACED) && \
200 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
201 security_ptrace(current,task) == 0))
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static int proc_pid_environ(struct task_struct *task, char * buffer)
204{
205 int res = 0;
206 struct mm_struct *mm = get_task_mm(task);
207 if (mm) {
208 unsigned int len = mm->env_end - mm->env_start;
209 if (len > PAGE_SIZE)
210 len = PAGE_SIZE;
211 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700212 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 res = -ESRCH;
214 mmput(mm);
215 }
216 return res;
217}
218
219static int proc_pid_cmdline(struct task_struct *task, char * buffer)
220{
221 int res = 0;
222 unsigned int len;
223 struct mm_struct *mm = get_task_mm(task);
224 if (!mm)
225 goto out;
226 if (!mm->arg_end)
227 goto out_mm; /* Shh! No looking before we're done */
228
229 len = mm->arg_end - mm->arg_start;
230
231 if (len > PAGE_SIZE)
232 len = PAGE_SIZE;
233
234 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
235
236 // If the nul at the end of args has been overwritten, then
237 // assume application is using setproctitle(3).
238 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
239 len = strnlen(buffer, res);
240 if (len < res) {
241 res = len;
242 } else {
243 len = mm->env_end - mm->env_start;
244 if (len > PAGE_SIZE - res)
245 len = PAGE_SIZE - res;
246 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
247 res = strnlen(buffer, res);
248 }
249 }
250out_mm:
251 mmput(mm);
252out:
253 return res;
254}
255
256static int proc_pid_auxv(struct task_struct *task, char *buffer)
257{
258 int res = 0;
259 struct mm_struct *mm = get_task_mm(task);
260 if (mm) {
261 unsigned int nwords = 0;
262 do
263 nwords += 2;
264 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
265 res = nwords * sizeof(mm->saved_auxv[0]);
266 if (res > PAGE_SIZE)
267 res = PAGE_SIZE;
268 memcpy(buffer, mm->saved_auxv, res);
269 mmput(mm);
270 }
271 return res;
272}
273
274
275#ifdef CONFIG_KALLSYMS
276/*
277 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
278 * Returns the resolved symbol. If that fails, simply return the address.
279 */
280static int proc_pid_wchan(struct task_struct *task, char *buffer)
281{
282 char *modname;
283 const char *sym_name;
284 unsigned long wchan, size, offset;
285 char namebuf[KSYM_NAME_LEN+1];
286
287 wchan = get_wchan(task);
288
289 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
290 if (sym_name)
291 return sprintf(buffer, "%s", sym_name);
292 return sprintf(buffer, "%lu", wchan);
293}
294#endif /* CONFIG_KALLSYMS */
295
296#ifdef CONFIG_SCHEDSTATS
297/*
298 * Provides /proc/PID/schedstat
299 */
300static int proc_pid_schedstat(struct task_struct *task, char *buffer)
301{
302 return sprintf(buffer, "%lu %lu %lu\n",
303 task->sched_info.cpu_time,
304 task->sched_info.run_delay,
305 task->sched_info.pcnt);
306}
307#endif
308
309/* The badness from the OOM killer */
310unsigned long badness(struct task_struct *p, unsigned long uptime);
311static int proc_oom_score(struct task_struct *task, char *buffer)
312{
313 unsigned long points;
314 struct timespec uptime;
315
316 do_posix_clock_monotonic_gettime(&uptime);
317 points = badness(task, uptime.tv_sec);
318 return sprintf(buffer, "%lu\n", points);
319}
320
321/************************************************************************/
322/* Here the fs part begins */
323/************************************************************************/
324
325/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700326static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700328 struct task_struct *task;
329 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700330 /* Allow access to a task's file descriptors if it is us or we
331 * may use ptrace attach to the process and find out that
332 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700333 */
334 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700335 if (task) {
336 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700337 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700338 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700339 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700342static int proc_setattr(struct dentry *dentry, struct iattr *attr)
343{
344 int error;
345 struct inode *inode = dentry->d_inode;
346
347 if (attr->ia_valid & ATTR_MODE)
348 return -EPERM;
349
350 error = inode_change_ok(inode, attr);
351 if (!error) {
352 error = security_inode_setattr(dentry, attr);
353 if (!error)
354 error = inode_setattr(inode, attr);
355 }
356 return error;
357}
358
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800359static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700360 .setattr = proc_setattr,
361};
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500364struct proc_mounts {
365 struct seq_file m;
366 int event;
367};
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static int mounts_open(struct inode *inode, struct file *file)
370{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700371 struct task_struct *task = get_proc_task(inode);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800372 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500373 struct proc_mounts *p;
374 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Eric W. Biederman99f89552006-06-26 00:25:55 -0700376 if (task) {
377 task_lock(task);
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800378 if (task->nsproxy) {
379 ns = task->nsproxy->mnt_ns;
380 if (ns)
381 get_mnt_ns(ns);
382 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700383 task_unlock(task);
384 put_task_struct(task);
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800387 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500388 ret = -ENOMEM;
389 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
390 if (p) {
391 file->private_data = &p->m;
392 ret = seq_open(file, &mounts_op);
393 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800394 p->m.private = ns;
395 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500396 return 0;
397 }
398 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800400 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 return ret;
403}
404
405static int mounts_release(struct inode *inode, struct file *file)
406{
407 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800408 struct mnt_namespace *ns = m->private;
409 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return seq_release(inode, file);
411}
412
Al Viro5addc5d2005-11-07 17:15:49 -0500413static unsigned mounts_poll(struct file *file, poll_table *wait)
414{
415 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800416 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500417 unsigned res = 0;
418
419 poll_wait(file, &ns->poll, wait);
420
421 spin_lock(&vfsmount_lock);
422 if (p->event != ns->event) {
423 p->event = ns->event;
424 res = POLLERR;
425 }
426 spin_unlock(&vfsmount_lock);
427
428 return res;
429}
430
Arjan van de Ven00977a52007-02-12 00:55:34 -0800431static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 .open = mounts_open,
433 .read = seq_read,
434 .llseek = seq_lseek,
435 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500436 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437};
438
Chuck Leverb4629fe2006-03-20 13:44:12 -0500439extern struct seq_operations mountstats_op;
440static int mountstats_open(struct inode *inode, struct file *file)
441{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500442 int ret = seq_open(file, &mountstats_op);
443
444 if (!ret) {
445 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800446 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700447 struct task_struct *task = get_proc_task(inode);
448
449 if (task) {
450 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800451 if (task->nsproxy)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800452 mnt_ns = task->nsproxy->mnt_ns;
453 if (mnt_ns)
454 get_mnt_ns(mnt_ns);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700455 task_unlock(task);
456 put_task_struct(task);
457 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500458
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800459 if (mnt_ns)
460 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500461 else {
462 seq_release(inode, file);
463 ret = -EINVAL;
464 }
465 }
466 return ret;
467}
468
Arjan van de Ven00977a52007-02-12 00:55:34 -0800469static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500470 .open = mountstats_open,
471 .read = seq_read,
472 .llseek = seq_lseek,
473 .release = mounts_release,
474};
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
477
478static ssize_t proc_info_read(struct file * file, char __user * buf,
479 size_t count, loff_t *ppos)
480{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800481 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 unsigned long page;
483 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700484 struct task_struct *task = get_proc_task(inode);
485
486 length = -ESRCH;
487 if (!task)
488 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (count > PROC_BLOCK_SIZE)
491 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700492
493 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700495 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 length = PROC_I(inode)->op.proc_read(task, (char*)page);
498
499 if (length >= 0)
500 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
501 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700502out:
503 put_task_struct(task);
504out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return length;
506}
507
Arjan van de Ven00977a52007-02-12 00:55:34 -0800508static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 .read = proc_info_read,
510};
511
512static int mem_open(struct inode* inode, struct file* file)
513{
514 file->private_data = (void*)((long)current->self_exec_id);
515 return 0;
516}
517
518static ssize_t mem_read(struct file * file, char __user * buf,
519 size_t count, loff_t *ppos)
520{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800521 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 char *page;
523 unsigned long src = *ppos;
524 int ret = -ESRCH;
525 struct mm_struct *mm;
526
Eric W. Biederman99f89552006-06-26 00:25:55 -0700527 if (!task)
528 goto out_no_task;
529
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700530 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto out;
532
533 ret = -ENOMEM;
534 page = (char *)__get_free_page(GFP_USER);
535 if (!page)
536 goto out;
537
538 ret = 0;
539
540 mm = get_task_mm(task);
541 if (!mm)
542 goto out_free;
543
544 ret = -EIO;
545
546 if (file->private_data != (void*)((long)current->self_exec_id))
547 goto out_put;
548
549 ret = 0;
550
551 while (count > 0) {
552 int this_len, retval;
553
554 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
555 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700556 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (!ret)
558 ret = -EIO;
559 break;
560 }
561
562 if (copy_to_user(buf, page, retval)) {
563 ret = -EFAULT;
564 break;
565 }
566
567 ret += retval;
568 src += retval;
569 buf += retval;
570 count -= retval;
571 }
572 *ppos = src;
573
574out_put:
575 mmput(mm);
576out_free:
577 free_page((unsigned long) page);
578out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700579 put_task_struct(task);
580out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return ret;
582}
583
584#define mem_write NULL
585
586#ifndef mem_write
587/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800588static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 size_t count, loff_t *ppos)
590{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700591 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800593 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 unsigned long dst = *ppos;
595
Eric W. Biederman99f89552006-06-26 00:25:55 -0700596 copied = -ESRCH;
597 if (!task)
598 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Eric W. Biederman99f89552006-06-26 00:25:55 -0700600 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
601 goto out;
602
603 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 page = (char *)__get_free_page(GFP_USER);
605 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700606 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700608 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 while (count > 0) {
610 int this_len, retval;
611
612 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
613 if (copy_from_user(page, buf, this_len)) {
614 copied = -EFAULT;
615 break;
616 }
617 retval = access_process_vm(task, dst, page, this_len, 1);
618 if (!retval) {
619 if (!copied)
620 copied = -EIO;
621 break;
622 }
623 copied += retval;
624 buf += retval;
625 dst += retval;
626 count -= retval;
627 }
628 *ppos = dst;
629 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700630out:
631 put_task_struct(task);
632out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return copied;
634}
635#endif
636
637static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
638{
639 switch (orig) {
640 case 0:
641 file->f_pos = offset;
642 break;
643 case 1:
644 file->f_pos += offset;
645 break;
646 default:
647 return -EINVAL;
648 }
649 force_successful_syscall_return();
650 return file->f_pos;
651}
652
Arjan van de Ven00977a52007-02-12 00:55:34 -0800653static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 .llseek = mem_lseek,
655 .read = mem_read,
656 .write = mem_write,
657 .open = mem_open,
658};
659
660static ssize_t oom_adjust_read(struct file *file, char __user *buf,
661 size_t count, loff_t *ppos)
662{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800663 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700664 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700666 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 loff_t __ppos = *ppos;
668
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (__ppos >= len)
676 return 0;
677 if (count > len-__ppos)
678 count = len-__ppos;
679 if (copy_to_user(buf, buffer + __ppos, count))
680 return -EFAULT;
681 *ppos = __ppos + count;
682 return count;
683}
684
685static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
686 size_t count, loff_t *ppos)
687{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700688 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700689 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int oom_adjust;
691
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700692 memset(buffer, 0, sizeof(buffer));
693 if (count > sizeof(buffer) - 1)
694 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (copy_from_user(buffer, buf, count))
696 return -EFAULT;
697 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700698 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
699 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -EINVAL;
701 if (*end == '\n')
702 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800703 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700704 if (!task)
705 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800706 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
707 put_task_struct(task);
708 return -EACCES;
709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700711 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (end - buffer == 0)
713 return -EIO;
714 return end - buffer;
715}
716
Arjan van de Ven00977a52007-02-12 00:55:34 -0800717static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .read = oom_adjust_read,
719 .write = oom_adjust_write,
720};
721
David Rientjesb813e932007-05-06 14:49:24 -0700722static ssize_t clear_refs_write(struct file *file, const char __user *buf,
723 size_t count, loff_t *ppos)
724{
725 struct task_struct *task;
726 char buffer[PROC_NUMBUF], *end;
727 struct mm_struct *mm;
728
729 memset(buffer, 0, sizeof(buffer));
730 if (count > sizeof(buffer) - 1)
731 count = sizeof(buffer) - 1;
732 if (copy_from_user(buffer, buf, count))
733 return -EFAULT;
734 if (!simple_strtol(buffer, &end, 0))
735 return -EINVAL;
736 if (*end == '\n')
737 end++;
738 task = get_proc_task(file->f_path.dentry->d_inode);
739 if (!task)
740 return -ESRCH;
741 mm = get_task_mm(task);
742 if (mm) {
743 clear_refs_smap(mm);
744 mmput(mm);
745 }
746 put_task_struct(task);
747 if (end - buffer == 0)
748 return -EIO;
749 return end - buffer;
750}
751
752static struct file_operations proc_clear_refs_operations = {
753 .write = clear_refs_write,
754};
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756#ifdef CONFIG_AUDITSYSCALL
757#define TMPBUFLEN 21
758static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
759 size_t count, loff_t *ppos)
760{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800761 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700762 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ssize_t length;
764 char tmpbuf[TMPBUFLEN];
765
Eric W. Biederman99f89552006-06-26 00:25:55 -0700766 if (!task)
767 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
769 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700770 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
772}
773
774static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
775 size_t count, loff_t *ppos)
776{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800777 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 char *page, *tmp;
779 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 uid_t loginuid;
781
782 if (!capable(CAP_AUDIT_CONTROL))
783 return -EPERM;
784
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700785 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -EPERM;
787
Al Viroe0182902006-05-18 08:28:02 -0400788 if (count >= PAGE_SIZE)
789 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (*ppos != 0) {
792 /* No partial writes. */
793 return -EINVAL;
794 }
795 page = (char*)__get_free_page(GFP_USER);
796 if (!page)
797 return -ENOMEM;
798 length = -EFAULT;
799 if (copy_from_user(page, buf, count))
800 goto out_free_page;
801
Al Viroe0182902006-05-18 08:28:02 -0400802 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 loginuid = simple_strtoul(page, &tmp, 10);
804 if (tmp == page) {
805 length = -EINVAL;
806 goto out_free_page;
807
808 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700809 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (likely(length == 0))
811 length = count;
812
813out_free_page:
814 free_page((unsigned long) page);
815 return length;
816}
817
Arjan van de Ven00977a52007-02-12 00:55:34 -0800818static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 .read = proc_loginuid_read,
820 .write = proc_loginuid_write,
821};
822#endif
823
824#ifdef CONFIG_SECCOMP
825static ssize_t seccomp_read(struct file *file, char __user *buf,
826 size_t count, loff_t *ppos)
827{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700828 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 char __buf[20];
830 loff_t __ppos = *ppos;
831 size_t len;
832
Eric W. Biederman99f89552006-06-26 00:25:55 -0700833 if (!tsk)
834 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* no need to print the trailing zero, so use only len */
836 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700837 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (__ppos >= len)
839 return 0;
840 if (count > len - __ppos)
841 count = len - __ppos;
842 if (copy_to_user(buf, __buf + __ppos, count))
843 return -EFAULT;
844 *ppos = __ppos + count;
845 return count;
846}
847
848static ssize_t seccomp_write(struct file *file, const char __user *buf,
849 size_t count, loff_t *ppos)
850{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700851 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 char __buf[20], *end;
853 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700854 ssize_t result;
855
856 result = -ESRCH;
857 if (!tsk)
858 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700861 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Eric W. Biederman99f89552006-06-26 00:25:55 -0700865 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 memset(__buf, 0, sizeof(__buf));
867 count = min(count, sizeof(__buf) - 1);
868 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700869 goto out;
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 seccomp_mode = simple_strtoul(__buf, &end, 0);
872 if (*end == '\n')
873 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700874 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
876 tsk->seccomp.mode = seccomp_mode;
877 set_tsk_thread_flag(tsk, TIF_SECCOMP);
878 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700879 goto out;
880 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700882 goto out;
883 result = end - __buf;
884out:
885 put_task_struct(tsk);
886out_no_task:
887 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Arjan van de Ven00977a52007-02-12 00:55:34 -0800890static const struct file_operations proc_seccomp_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 .read = seccomp_read,
892 .write = seccomp_write,
893};
894#endif /* CONFIG_SECCOMP */
895
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800896#ifdef CONFIG_FAULT_INJECTION
897static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
898 size_t count, loff_t *ppos)
899{
900 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
901 char buffer[PROC_NUMBUF];
902 size_t len;
903 int make_it_fail;
904 loff_t __ppos = *ppos;
905
906 if (!task)
907 return -ESRCH;
908 make_it_fail = task->make_it_fail;
909 put_task_struct(task);
910
911 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
912 if (__ppos >= len)
913 return 0;
914 if (count > len-__ppos)
915 count = len-__ppos;
916 if (copy_to_user(buf, buffer + __ppos, count))
917 return -EFAULT;
918 *ppos = __ppos + count;
919 return count;
920}
921
922static ssize_t proc_fault_inject_write(struct file * file,
923 const char __user * buf, size_t count, loff_t *ppos)
924{
925 struct task_struct *task;
926 char buffer[PROC_NUMBUF], *end;
927 int make_it_fail;
928
929 if (!capable(CAP_SYS_RESOURCE))
930 return -EPERM;
931 memset(buffer, 0, sizeof(buffer));
932 if (count > sizeof(buffer) - 1)
933 count = sizeof(buffer) - 1;
934 if (copy_from_user(buffer, buf, count))
935 return -EFAULT;
936 make_it_fail = simple_strtol(buffer, &end, 0);
937 if (*end == '\n')
938 end++;
939 task = get_proc_task(file->f_dentry->d_inode);
940 if (!task)
941 return -ESRCH;
942 task->make_it_fail = make_it_fail;
943 put_task_struct(task);
944 if (end - buffer == 0)
945 return -EIO;
946 return end - buffer;
947}
948
Arjan van de Ven00977a52007-02-12 00:55:34 -0800949static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800950 .read = proc_fault_inject_read,
951 .write = proc_fault_inject_write,
952};
953#endif
954
Al Viro008b1502005-08-20 00:17:39 +0100955static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 struct inode *inode = dentry->d_inode;
958 int error = -EACCES;
959
960 /* We don't need a base pointer in the /proc filesystem */
961 path_release(nd);
962
Eric W. Biederman778c1142006-06-26 00:25:58 -0700963 /* Are we allowed to snoop on the tasks file descriptors? */
964 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
968 nd->last_type = LAST_BIND;
969out:
Al Viro008b1502005-08-20 00:17:39 +0100970 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
974 char __user *buffer, int buflen)
975{
976 struct inode * inode;
977 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
978 int len;
979
980 if (!tmp)
981 return -ENOMEM;
982
983 inode = dentry->d_inode;
984 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
985 len = PTR_ERR(path);
986 if (IS_ERR(path))
987 goto out;
988 len = tmp + PAGE_SIZE - 1 - path;
989
990 if (len > buflen)
991 len = buflen;
992 if (copy_to_user(buffer, path, len))
993 len = -EFAULT;
994 out:
995 free_page((unsigned long)tmp);
996 return len;
997}
998
999static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1000{
1001 int error = -EACCES;
1002 struct inode *inode = dentry->d_inode;
1003 struct dentry *de;
1004 struct vfsmount *mnt = NULL;
1005
Eric W. Biederman778c1142006-06-26 00:25:58 -07001006 /* Are we allowed to snoop on the tasks file descriptors? */
1007 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1011 if (error)
1012 goto out;
1013
1014 error = do_proc_readlink(de, mnt, buffer, buflen);
1015 dput(de);
1016 mntput(mnt);
1017out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return error;
1019}
1020
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001021static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001023 .follow_link = proc_pid_follow_link,
1024 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025};
1026
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001027
1028/* building an inode */
1029
1030static int task_dumpable(struct task_struct *task)
1031{
1032 int dumpable = 0;
1033 struct mm_struct *mm;
1034
1035 task_lock(task);
1036 mm = task->mm;
1037 if (mm)
1038 dumpable = mm->dumpable;
1039 task_unlock(task);
1040 if(dumpable == 1)
1041 return 1;
1042 return 0;
1043}
1044
1045
Eric W. Biederman61a28782006-10-02 02:18:49 -07001046static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001047{
1048 struct inode * inode;
1049 struct proc_inode *ei;
1050
1051 /* We need a new inode */
1052
1053 inode = new_inode(sb);
1054 if (!inode)
1055 goto out;
1056
1057 /* Common stuff */
1058 ei = PROC_I(inode);
1059 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001060 inode->i_op = &proc_def_inode_operations;
1061
1062 /*
1063 * grab the reference to task.
1064 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001065 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001066 if (!ei->pid)
1067 goto out_unlock;
1068
1069 inode->i_uid = 0;
1070 inode->i_gid = 0;
1071 if (task_dumpable(task)) {
1072 inode->i_uid = task->euid;
1073 inode->i_gid = task->egid;
1074 }
1075 security_task_to_inode(task, inode);
1076
1077out:
1078 return inode;
1079
1080out_unlock:
1081 iput(inode);
1082 return NULL;
1083}
1084
1085static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1086{
1087 struct inode *inode = dentry->d_inode;
1088 struct task_struct *task;
1089 generic_fillattr(inode, stat);
1090
1091 rcu_read_lock();
1092 stat->uid = 0;
1093 stat->gid = 0;
1094 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1095 if (task) {
1096 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1097 task_dumpable(task)) {
1098 stat->uid = task->euid;
1099 stat->gid = task->egid;
1100 }
1101 }
1102 rcu_read_unlock();
1103 return 0;
1104}
1105
1106/* dentry stuff */
1107
1108/*
1109 * Exceptional case: normally we are not allowed to unhash a busy
1110 * directory. In this case, however, we can do it - no aliasing problems
1111 * due to the way we treat inodes.
1112 *
1113 * Rewrite the inode's ownerships here because the owning task may have
1114 * performed a setuid(), etc.
1115 *
1116 * Before the /proc/pid/status file was created the only way to read
1117 * the effective uid of a /process was to stat /proc/pid. Reading
1118 * /proc/pid/status is slow enough that procps and other packages
1119 * kept stating /proc/pid. To keep the rules in /proc simple I have
1120 * made this apply to all per process world readable and executable
1121 * directories.
1122 */
1123static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1124{
1125 struct inode *inode = dentry->d_inode;
1126 struct task_struct *task = get_proc_task(inode);
1127 if (task) {
1128 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1129 task_dumpable(task)) {
1130 inode->i_uid = task->euid;
1131 inode->i_gid = task->egid;
1132 } else {
1133 inode->i_uid = 0;
1134 inode->i_gid = 0;
1135 }
1136 inode->i_mode &= ~(S_ISUID | S_ISGID);
1137 security_task_to_inode(task, inode);
1138 put_task_struct(task);
1139 return 1;
1140 }
1141 d_drop(dentry);
1142 return 0;
1143}
1144
1145static int pid_delete_dentry(struct dentry * dentry)
1146{
1147 /* Is the task we represent dead?
1148 * If so, then don't put the dentry on the lru list,
1149 * kill it immediately.
1150 */
1151 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1152}
1153
1154static struct dentry_operations pid_dentry_operations =
1155{
1156 .d_revalidate = pid_revalidate,
1157 .d_delete = pid_delete_dentry,
1158};
1159
1160/* Lookups */
1161
Eric W. Biederman61a28782006-10-02 02:18:49 -07001162typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1163
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001164/*
1165 * Fill a directory entry.
1166 *
1167 * If possible create the dcache entry and derive our inode number and
1168 * file type from dcache entry.
1169 *
1170 * Since all of the proc inode numbers are dynamically generated, the inode
1171 * numbers do not exist until the inode is cache. This means creating the
1172 * the dcache entry in readdir is necessary to keep the inode numbers
1173 * reported by readdir in sync with the inode numbers reported
1174 * by stat.
1175 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001176static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1177 char *name, int len,
1178 instantiate_t instantiate, struct task_struct *task, void *ptr)
1179{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001180 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001181 struct inode *inode;
1182 struct qstr qname;
1183 ino_t ino = 0;
1184 unsigned type = DT_UNKNOWN;
1185
1186 qname.name = name;
1187 qname.len = len;
1188 qname.hash = full_name_hash(name, len);
1189
1190 child = d_lookup(dir, &qname);
1191 if (!child) {
1192 struct dentry *new;
1193 new = d_alloc(dir, &qname);
1194 if (new) {
1195 child = instantiate(dir->d_inode, new, task, ptr);
1196 if (child)
1197 dput(new);
1198 else
1199 child = new;
1200 }
1201 }
1202 if (!child || IS_ERR(child) || !child->d_inode)
1203 goto end_instantiate;
1204 inode = child->d_inode;
1205 if (inode) {
1206 ino = inode->i_ino;
1207 type = inode->i_mode >> 12;
1208 }
1209 dput(child);
1210end_instantiate:
1211 if (!ino)
1212 ino = find_inode_number(dir, &qname);
1213 if (!ino)
1214 ino = 1;
1215 return filldir(dirent, name, len, filp->f_pos, ino, type);
1216}
1217
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001218static unsigned name_to_int(struct dentry *dentry)
1219{
1220 const char *name = dentry->d_name.name;
1221 int len = dentry->d_name.len;
1222 unsigned n = 0;
1223
1224 if (len > 1 && *name == '0')
1225 goto out;
1226 while (len-- > 0) {
1227 unsigned c = *name++ - '0';
1228 if (c > 9)
1229 goto out;
1230 if (n >= (~0U-9)/10)
1231 goto out;
1232 n *= 10;
1233 n += c;
1234 }
1235 return n;
1236out:
1237 return ~0U;
1238}
1239
1240static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1241{
1242 struct task_struct *task = get_proc_task(inode);
1243 struct files_struct *files = NULL;
1244 struct file *file;
1245 int fd = proc_fd(inode);
1246
1247 if (task) {
1248 files = get_files_struct(task);
1249 put_task_struct(task);
1250 }
1251 if (files) {
1252 /*
1253 * We are not taking a ref to the file structure, so we must
1254 * hold ->file_lock.
1255 */
1256 spin_lock(&files->file_lock);
1257 file = fcheck_files(files, fd);
1258 if (file) {
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001259 *mnt = mntget(file->f_path.mnt);
1260 *dentry = dget(file->f_path.dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001261 spin_unlock(&files->file_lock);
1262 put_files_struct(files);
1263 return 0;
1264 }
1265 spin_unlock(&files->file_lock);
1266 put_files_struct(files);
1267 }
1268 return -ENOENT;
1269}
1270
1271static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1272{
1273 struct inode *inode = dentry->d_inode;
1274 struct task_struct *task = get_proc_task(inode);
1275 int fd = proc_fd(inode);
1276 struct files_struct *files;
1277
1278 if (task) {
1279 files = get_files_struct(task);
1280 if (files) {
1281 rcu_read_lock();
1282 if (fcheck_files(files, fd)) {
1283 rcu_read_unlock();
1284 put_files_struct(files);
1285 if (task_dumpable(task)) {
1286 inode->i_uid = task->euid;
1287 inode->i_gid = task->egid;
1288 } else {
1289 inode->i_uid = 0;
1290 inode->i_gid = 0;
1291 }
1292 inode->i_mode &= ~(S_ISUID | S_ISGID);
1293 security_task_to_inode(task, inode);
1294 put_task_struct(task);
1295 return 1;
1296 }
1297 rcu_read_unlock();
1298 put_files_struct(files);
1299 }
1300 put_task_struct(task);
1301 }
1302 d_drop(dentry);
1303 return 0;
1304}
1305
1306static struct dentry_operations tid_fd_dentry_operations =
1307{
1308 .d_revalidate = tid_fd_revalidate,
1309 .d_delete = pid_delete_dentry,
1310};
1311
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001312static struct dentry *proc_fd_instantiate(struct inode *dir,
1313 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001314{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001315 unsigned fd = *(unsigned *)ptr;
1316 struct file *file;
1317 struct files_struct *files;
1318 struct inode *inode;
1319 struct proc_inode *ei;
1320 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001321
Eric W. Biederman61a28782006-10-02 02:18:49 -07001322 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001323 if (!inode)
1324 goto out;
1325 ei = PROC_I(inode);
1326 ei->fd = fd;
1327 files = get_files_struct(task);
1328 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001329 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001330 inode->i_mode = S_IFLNK;
1331
1332 /*
1333 * We are not taking a ref to the file structure, so we must
1334 * hold ->file_lock.
1335 */
1336 spin_lock(&files->file_lock);
1337 file = fcheck_files(files, fd);
1338 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001339 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001340 if (file->f_mode & 1)
1341 inode->i_mode |= S_IRUSR | S_IXUSR;
1342 if (file->f_mode & 2)
1343 inode->i_mode |= S_IWUSR | S_IXUSR;
1344 spin_unlock(&files->file_lock);
1345 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001346
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001347 inode->i_op = &proc_pid_link_inode_operations;
1348 inode->i_size = 64;
1349 ei->op.proc_get_link = proc_fd_link;
1350 dentry->d_op = &tid_fd_dentry_operations;
1351 d_add(dentry, inode);
1352 /* Close the race of the process dying before we return the dentry */
1353 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001354 error = NULL;
1355
1356 out:
1357 return error;
1358out_unlock:
1359 spin_unlock(&files->file_lock);
1360 put_files_struct(files);
1361out_iput:
1362 iput(inode);
1363 goto out;
1364}
1365
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001366static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1367{
1368 struct task_struct *task = get_proc_task(dir);
1369 unsigned fd = name_to_int(dentry);
1370 struct dentry *result = ERR_PTR(-ENOENT);
1371
1372 if (!task)
1373 goto out_no_task;
1374 if (fd == ~0U)
1375 goto out;
1376
1377 result = proc_fd_instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001378out:
1379 put_task_struct(task);
1380out_no_task:
1381 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001382}
1383
Eric W. Biederman61a28782006-10-02 02:18:49 -07001384static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1385 struct task_struct *task, int fd)
1386{
1387 char name[PROC_NUMBUF];
1388 int len = snprintf(name, sizeof(name), "%d", fd);
1389 return proc_fill_cache(filp, dirent, filldir, name, len,
1390 proc_fd_instantiate, task, &fd);
1391}
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1394{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001395 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001396 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001397 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 unsigned int fd, tid, ino;
1399 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001401 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001404 if (!p)
1405 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 retval = 0;
1407 tid = p->pid;
1408
1409 fd = filp->f_pos;
1410 switch (fd) {
1411 case 0:
1412 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1413 goto out;
1414 filp->f_pos++;
1415 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001416 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1418 goto out;
1419 filp->f_pos++;
1420 default:
1421 files = get_files_struct(p);
1422 if (!files)
1423 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001424 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001425 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001427 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 fd++, filp->f_pos++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 if (!fcheck_files(files, fd))
1431 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001432 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Eric W. Biederman61a28782006-10-02 02:18:49 -07001434 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001435 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 break;
1437 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001438 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001440 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 put_files_struct(files);
1442 }
1443out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001444 put_task_struct(p);
1445out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return retval;
1447}
1448
Arjan van de Ven00977a52007-02-12 00:55:34 -08001449static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 .read = generic_read_dir,
1451 .readdir = proc_readfd,
1452};
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001455 * /proc/pid/fd needs a special permission handler so that a process can still
1456 * access /proc/self/fd after it has executed a setuid().
1457 */
1458static int proc_fd_permission(struct inode *inode, int mask,
1459 struct nameidata *nd)
1460{
1461 int rv;
1462
1463 rv = generic_permission(inode, mask, NULL);
1464 if (rv == 0)
1465 return 0;
1466 if (task_pid(current) == proc_pid(inode))
1467 rv = 0;
1468 return rv;
1469}
1470
1471/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 * proc directories can do almost nothing..
1473 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001474static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001476 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001477 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478};
1479
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001480static struct dentry *proc_pident_instantiate(struct inode *dir,
1481 struct dentry *dentry, struct task_struct *task, void *ptr)
1482{
1483 struct pid_entry *p = ptr;
1484 struct inode *inode;
1485 struct proc_inode *ei;
1486 struct dentry *error = ERR_PTR(-EINVAL);
1487
Eric W. Biederman61a28782006-10-02 02:18:49 -07001488 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001489 if (!inode)
1490 goto out;
1491
1492 ei = PROC_I(inode);
1493 inode->i_mode = p->mode;
1494 if (S_ISDIR(inode->i_mode))
1495 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1496 if (p->iop)
1497 inode->i_op = p->iop;
1498 if (p->fop)
1499 inode->i_fop = p->fop;
1500 ei->op = p->op;
1501 dentry->d_op = &pid_dentry_operations;
1502 d_add(dentry, inode);
1503 /* Close the race of the process dying before we return the dentry */
1504 if (pid_revalidate(dentry, NULL))
1505 error = NULL;
1506out:
1507 return error;
1508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510static struct dentry *proc_pident_lookup(struct inode *dir,
1511 struct dentry *dentry,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001512 struct pid_entry *ents,
1513 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001516 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001517 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001518 struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001520 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 inode = NULL;
1522
Eric W. Biederman99f89552006-06-26 00:25:55 -07001523 if (!task)
1524 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001526 /*
1527 * Yes, it does not scale. And it should not. Don't add
1528 * new entries into /proc/<tgid>/ without very good reasons.
1529 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001530 last = &ents[nents - 1];
1531 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (p->len != dentry->d_name.len)
1533 continue;
1534 if (!memcmp(dentry->d_name.name, p->name, p->len))
1535 break;
1536 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001537 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 goto out;
1539
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001540 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001542 put_task_struct(task);
1543out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001544 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
Eric W. Biederman61a28782006-10-02 02:18:49 -07001547static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1548 struct task_struct *task, struct pid_entry *p)
1549{
1550 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1551 proc_pident_instantiate, task, p);
1552}
1553
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001554static int proc_pident_readdir(struct file *filp,
1555 void *dirent, filldir_t filldir,
1556 struct pid_entry *ents, unsigned int nents)
1557{
1558 int i;
1559 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001560 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001561 struct inode *inode = dentry->d_inode;
1562 struct task_struct *task = get_proc_task(inode);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001563 struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001564 ino_t ino;
1565 int ret;
1566
1567 ret = -ENOENT;
1568 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001569 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001570
1571 ret = 0;
1572 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001573 i = filp->f_pos;
1574 switch (i) {
1575 case 0:
1576 ino = inode->i_ino;
1577 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1578 goto out;
1579 i++;
1580 filp->f_pos++;
1581 /* fall through */
1582 case 1:
1583 ino = parent_ino(dentry);
1584 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1585 goto out;
1586 i++;
1587 filp->f_pos++;
1588 /* fall through */
1589 default:
1590 i -= 2;
1591 if (i >= nents) {
1592 ret = 1;
1593 goto out;
1594 }
1595 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001596 last = &ents[nents - 1];
1597 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001598 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001599 goto out;
1600 filp->f_pos++;
1601 p++;
1602 }
1603 }
1604
1605 ret = 1;
1606out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001607 put_task_struct(task);
1608out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001609 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001613static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1614 size_t count, loff_t *ppos)
1615{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001616 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001617 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001618 ssize_t length;
1619 struct task_struct *task = get_proc_task(inode);
1620
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001621 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001622 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001623
1624 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001625 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001626 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001627 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001628 if (length > 0)
1629 length = simple_read_from_buffer(buf, count, ppos, p, length);
1630 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001631 return length;
1632}
1633
1634static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1635 size_t count, loff_t *ppos)
1636{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001637 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001638 char *page;
1639 ssize_t length;
1640 struct task_struct *task = get_proc_task(inode);
1641
1642 length = -ESRCH;
1643 if (!task)
1644 goto out_no_task;
1645 if (count > PAGE_SIZE)
1646 count = PAGE_SIZE;
1647
1648 /* No partial writes. */
1649 length = -EINVAL;
1650 if (*ppos != 0)
1651 goto out;
1652
1653 length = -ENOMEM;
1654 page = (char*)__get_free_page(GFP_USER);
1655 if (!page)
1656 goto out;
1657
1658 length = -EFAULT;
1659 if (copy_from_user(page, buf, count))
1660 goto out_free;
1661
1662 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001663 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001664 (void*)page, count);
1665out_free:
1666 free_page((unsigned long) page);
1667out:
1668 put_task_struct(task);
1669out_no_task:
1670 return length;
1671}
1672
Arjan van de Ven00977a52007-02-12 00:55:34 -08001673static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001674 .read = proc_pid_attr_read,
1675 .write = proc_pid_attr_write,
1676};
1677
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001678static struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001679 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1680 REG("prev", S_IRUGO, pid_attr),
1681 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1682 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1683 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1684 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001685};
1686
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001687static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 void * dirent, filldir_t filldir)
1689{
1690 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001691 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
Arjan van de Ven00977a52007-02-12 00:55:34 -08001694static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001696 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697};
1698
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001699static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 struct dentry *dentry, struct nameidata *nd)
1701{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001702 return proc_pident_lookup(dir, dentry,
1703 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001706static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001707 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001708 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001709 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710};
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712#endif
1713
1714/*
1715 * /proc/self:
1716 */
1717static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1718 int buflen)
1719{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001720 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 sprintf(tmp, "%d", current->tgid);
1722 return vfs_readlink(dentry,buffer,buflen,tmp);
1723}
1724
Al Viro008b1502005-08-20 00:17:39 +01001725static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001727 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001729 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001732static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 .readlink = proc_self_readlink,
1734 .follow_link = proc_self_follow_link,
1735};
1736
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001737/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001738 * proc base
1739 *
1740 * These are the directory entries in the root directory of /proc
1741 * that properly belong to the /proc filesystem, as they describe
1742 * describe something that is process related.
1743 */
1744static struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001745 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001746 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001747};
1748
1749/*
1750 * Exceptional case: normally we are not allowed to unhash a busy
1751 * directory. In this case, however, we can do it - no aliasing problems
1752 * due to the way we treat inodes.
1753 */
1754static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1755{
1756 struct inode *inode = dentry->d_inode;
1757 struct task_struct *task = get_proc_task(inode);
1758 if (task) {
1759 put_task_struct(task);
1760 return 1;
1761 }
1762 d_drop(dentry);
1763 return 0;
1764}
1765
1766static struct dentry_operations proc_base_dentry_operations =
1767{
1768 .d_revalidate = proc_base_revalidate,
1769 .d_delete = pid_delete_dentry,
1770};
1771
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001772static struct dentry *proc_base_instantiate(struct inode *dir,
1773 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001774{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001775 struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001776 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001777 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001778 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001779
1780 /* Allocate the inode */
1781 error = ERR_PTR(-ENOMEM);
1782 inode = new_inode(dir->i_sb);
1783 if (!inode)
1784 goto out;
1785
1786 /* Initialize the inode */
1787 ei = PROC_I(inode);
1788 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001789
1790 /*
1791 * grab the reference to the task.
1792 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001793 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001794 if (!ei->pid)
1795 goto out_iput;
1796
1797 inode->i_uid = 0;
1798 inode->i_gid = 0;
1799 inode->i_mode = p->mode;
1800 if (S_ISDIR(inode->i_mode))
1801 inode->i_nlink = 2;
1802 if (S_ISLNK(inode->i_mode))
1803 inode->i_size = 64;
1804 if (p->iop)
1805 inode->i_op = p->iop;
1806 if (p->fop)
1807 inode->i_fop = p->fop;
1808 ei->op = p->op;
1809 dentry->d_op = &proc_base_dentry_operations;
1810 d_add(dentry, inode);
1811 error = NULL;
1812out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001813 return error;
1814out_iput:
1815 iput(inode);
1816 goto out;
1817}
1818
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001819static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1820{
1821 struct dentry *error;
1822 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001823 struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001824
1825 error = ERR_PTR(-ENOENT);
1826
1827 if (!task)
1828 goto out_no_task;
1829
1830 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001831 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1832 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001833 if (p->len != dentry->d_name.len)
1834 continue;
1835 if (!memcmp(dentry->d_name.name, p->name, p->len))
1836 break;
1837 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001838 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001839 goto out;
1840
1841 error = proc_base_instantiate(dir, dentry, task, p);
1842
1843out:
1844 put_task_struct(task);
1845out_no_task:
1846 return error;
1847}
1848
Eric W. Biederman61a28782006-10-02 02:18:49 -07001849static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1850 struct task_struct *task, struct pid_entry *p)
1851{
1852 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1853 proc_base_instantiate, task, p);
1854}
1855
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001856#ifdef CONFIG_TASK_IO_ACCOUNTING
1857static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1858{
1859 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001860#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001861 "rchar: %llu\n"
1862 "wchar: %llu\n"
1863 "syscr: %llu\n"
1864 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001865#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001866 "read_bytes: %llu\n"
1867 "write_bytes: %llu\n"
1868 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001869#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001870 (unsigned long long)task->rchar,
1871 (unsigned long long)task->wchar,
1872 (unsigned long long)task->syscr,
1873 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001874#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001875 (unsigned long long)task->ioac.read_bytes,
1876 (unsigned long long)task->ioac.write_bytes,
1877 (unsigned long long)task->ioac.cancelled_write_bytes);
1878}
1879#endif
1880
Eric W. Biederman801199c2006-10-02 02:18:48 -07001881/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001882 * Thread groups
1883 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08001884static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001885static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001886
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001887static struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001888 DIR("task", S_IRUGO|S_IXUGO, task),
1889 DIR("fd", S_IRUSR|S_IXUSR, fd),
1890 INF("environ", S_IRUSR, pid_environ),
1891 INF("auxv", S_IRUSR, pid_auxv),
1892 INF("status", S_IRUGO, pid_status),
1893 INF("cmdline", S_IRUGO, pid_cmdline),
1894 INF("stat", S_IRUGO, tgid_stat),
1895 INF("statm", S_IRUGO, pid_statm),
1896 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001897#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001898 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001899#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001900 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001901#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07001902 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001903#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001904 LNK("cwd", cwd),
1905 LNK("root", root),
1906 LNK("exe", exe),
1907 REG("mounts", S_IRUGO, mounts),
1908 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001909#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07001910 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07001911 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001912#endif
1913#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001914 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001915#endif
1916#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001917 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001918#endif
1919#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001920 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001921#endif
1922#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001923 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001924#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001925 INF("oom_score", S_IRUGO, oom_score),
1926 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001927#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07001928 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001929#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001930#ifdef CONFIG_FAULT_INJECTION
1931 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1932#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001933#ifdef CONFIG_TASK_IO_ACCOUNTING
1934 INF("io", S_IRUGO, pid_io_accounting),
1935#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001936};
1937
1938static int proc_tgid_base_readdir(struct file * filp,
1939 void * dirent, filldir_t filldir)
1940{
1941 return proc_pident_readdir(filp,dirent,filldir,
1942 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1943}
1944
Arjan van de Ven00977a52007-02-12 00:55:34 -08001945static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001946 .read = generic_read_dir,
1947 .readdir = proc_tgid_base_readdir,
1948};
1949
1950static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001951 return proc_pident_lookup(dir, dentry,
1952 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001953}
1954
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001955static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001956 .lookup = proc_tgid_base_lookup,
1957 .getattr = pid_getattr,
1958 .setattr = proc_setattr,
1959};
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001962 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001964 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001966 * Looks in the dcache for
1967 * /proc/@pid
1968 * /proc/@tgid/task/@pid
1969 * if either directory is present flushes it and all of it'ts children
1970 * from the dcache.
1971 *
1972 * It is safe and reasonable to cache /proc entries for a task until
1973 * that task exits. After that they just clog up the dcache with
1974 * useless entries, possibly causing useful dcache entries to be
1975 * flushed instead. This routine is proved to flush those useless
1976 * dcache entries at process exit time.
1977 *
1978 * NOTE: This routine is just an optimization so it does not guarantee
1979 * that no dcache entries will exist at process exit time it
1980 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001982void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001984 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001985 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001986 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Eric W. Biederman48e64842006-06-26 00:25:48 -07001988 name.name = buf;
1989 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1990 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1991 if (dentry) {
1992 shrink_dcache_parent(dentry);
1993 d_drop(dentry);
1994 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Eric W. Biederman48e64842006-06-26 00:25:48 -07001997 if (thread_group_leader(task))
1998 goto out;
1999
2000 name.name = buf;
2001 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2002 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2003 if (!leader)
2004 goto out;
2005
2006 name.name = "task";
2007 name.len = strlen(name.name);
2008 dir = d_hash_and_lookup(leader, &name);
2009 if (!dir)
2010 goto out_put_leader;
2011
2012 name.name = buf;
2013 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2014 dentry = d_hash_and_lookup(dir, &name);
2015 if (dentry) {
2016 shrink_dcache_parent(dentry);
2017 d_drop(dentry);
2018 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002020
2021 dput(dir);
2022out_put_leader:
2023 dput(leader);
2024out:
2025 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Adrian Bunk9711ef92006-12-06 20:38:31 -08002028static struct dentry *proc_pid_instantiate(struct inode *dir,
2029 struct dentry * dentry,
2030 struct task_struct *task, void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002031{
2032 struct dentry *error = ERR_PTR(-ENOENT);
2033 struct inode *inode;
2034
Eric W. Biederman61a28782006-10-02 02:18:49 -07002035 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002036 if (!inode)
2037 goto out;
2038
2039 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2040 inode->i_op = &proc_tgid_base_inode_operations;
2041 inode->i_fop = &proc_tgid_base_operations;
2042 inode->i_flags|=S_IMMUTABLE;
2043 inode->i_nlink = 4;
2044#ifdef CONFIG_SECURITY
2045 inode->i_nlink += 1;
2046#endif
2047
2048 dentry->d_op = &pid_dentry_operations;
2049
2050 d_add(dentry, inode);
2051 /* Close the race of the process dying before we return the dentry */
2052 if (pid_revalidate(dentry, NULL))
2053 error = NULL;
2054out:
2055 return error;
2056}
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2059{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002060 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Eric W. Biederman801199c2006-10-02 02:18:48 -07002064 result = proc_base_lookup(dir, dentry);
2065 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2066 goto out;
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 tgid = name_to_int(dentry);
2069 if (tgid == ~0U)
2070 goto out;
2071
Eric W. Biedermande758732006-06-26 00:25:51 -07002072 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 task = find_task_by_pid(tgid);
2074 if (task)
2075 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002076 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (!task)
2078 goto out;
2079
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002080 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002081 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002083 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002087 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002088 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002090static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002092 struct task_struct *task;
2093 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002095 rcu_read_lock();
2096retry:
2097 task = NULL;
2098 pid = find_ge_pid(tgid);
2099 if (pid) {
2100 tgid = pid->nr + 1;
2101 task = pid_task(pid, PIDTYPE_PID);
2102 /* What we to know is if the pid we have find is the
2103 * pid of a thread_group_leader. Testing for task
2104 * being a thread_group_leader is the obvious thing
2105 * todo but there is a window when it fails, due to
2106 * the pid transfer logic in de_thread.
2107 *
2108 * So we perform the straight forward test of seeing
2109 * if the pid we have found is the pid of a thread
2110 * group leader, and don't worry if the task we have
2111 * found doesn't happen to be a thread group leader.
2112 * As we don't care in the case of readdir.
2113 */
2114 if (!task || !has_group_leader_pid(task))
2115 goto retry;
2116 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002118 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002119 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002122#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Eric W. Biederman61a28782006-10-02 02:18:49 -07002124static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2125 struct task_struct *task, int tgid)
2126{
2127 char name[PROC_NUMBUF];
2128 int len = snprintf(name, sizeof(name), "%d", tgid);
2129 return proc_fill_cache(filp, dirent, filldir, name, len,
2130 proc_pid_instantiate, task, NULL);
2131}
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133/* for the /proc/ directory itself, after non-process stuff has been done */
2134int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002137 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002138 struct task_struct *task;
2139 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Eric W. Biederman61a28782006-10-02 02:18:49 -07002141 if (!reaper)
2142 goto out_no_task;
2143
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002144 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric W. Biederman801199c2006-10-02 02:18:48 -07002145 struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002146 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002147 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002150 tgid = filp->f_pos - TGID_OFFSET;
2151 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002152 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002153 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002154 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002155 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002156 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002157 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002158 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002161 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2162out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002163 put_task_struct(reaper);
2164out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return 0;
2166}
2167
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002168/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002169 * Tasks
2170 */
2171static struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002172 DIR("fd", S_IRUSR|S_IXUSR, fd),
2173 INF("environ", S_IRUSR, pid_environ),
2174 INF("auxv", S_IRUSR, pid_auxv),
2175 INF("status", S_IRUGO, pid_status),
2176 INF("cmdline", S_IRUGO, pid_cmdline),
2177 INF("stat", S_IRUGO, tid_stat),
2178 INF("statm", S_IRUGO, pid_statm),
2179 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002180#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002181 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002182#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002183 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002184#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002185 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002186#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002187 LNK("cwd", cwd),
2188 LNK("root", root),
2189 LNK("exe", exe),
2190 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002191#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002192 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002193 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002194#endif
2195#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002196 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002197#endif
2198#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002199 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002200#endif
2201#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002202 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002203#endif
2204#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002205 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002206#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002207 INF("oom_score", S_IRUGO, oom_score),
2208 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002209#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002210 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002211#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002212#ifdef CONFIG_FAULT_INJECTION
2213 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2214#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002215};
2216
2217static int proc_tid_base_readdir(struct file * filp,
2218 void * dirent, filldir_t filldir)
2219{
2220 return proc_pident_readdir(filp,dirent,filldir,
2221 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2222}
2223
2224static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002225 return proc_pident_lookup(dir, dentry,
2226 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002227}
2228
Arjan van de Ven00977a52007-02-12 00:55:34 -08002229static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002230 .read = generic_read_dir,
2231 .readdir = proc_tid_base_readdir,
2232};
2233
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002234static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002235 .lookup = proc_tid_base_lookup,
2236 .getattr = pid_getattr,
2237 .setattr = proc_setattr,
2238};
2239
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002240static struct dentry *proc_task_instantiate(struct inode *dir,
2241 struct dentry *dentry, struct task_struct *task, void *ptr)
2242{
2243 struct dentry *error = ERR_PTR(-ENOENT);
2244 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002245 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002246
2247 if (!inode)
2248 goto out;
2249 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2250 inode->i_op = &proc_tid_base_inode_operations;
2251 inode->i_fop = &proc_tid_base_operations;
2252 inode->i_flags|=S_IMMUTABLE;
2253 inode->i_nlink = 3;
2254#ifdef CONFIG_SECURITY
2255 inode->i_nlink += 1;
2256#endif
2257
2258 dentry->d_op = &pid_dentry_operations;
2259
2260 d_add(dentry, inode);
2261 /* Close the race of the process dying before we return the dentry */
2262 if (pid_revalidate(dentry, NULL))
2263 error = NULL;
2264out:
2265 return error;
2266}
2267
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002268static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2269{
2270 struct dentry *result = ERR_PTR(-ENOENT);
2271 struct task_struct *task;
2272 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002273 unsigned tid;
2274
2275 if (!leader)
2276 goto out_no_task;
2277
2278 tid = name_to_int(dentry);
2279 if (tid == ~0U)
2280 goto out;
2281
2282 rcu_read_lock();
2283 task = find_task_by_pid(tid);
2284 if (task)
2285 get_task_struct(task);
2286 rcu_read_unlock();
2287 if (!task)
2288 goto out;
2289 if (leader->tgid != task->tgid)
2290 goto out_drop_task;
2291
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002292 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002293out_drop_task:
2294 put_task_struct(task);
2295out:
2296 put_task_struct(leader);
2297out_no_task:
2298 return result;
2299}
2300
2301/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002302 * Find the first tid of a thread group to return to user space.
2303 *
2304 * Usually this is just the thread group leader, but if the users
2305 * buffer was too small or there was a seek into the middle of the
2306 * directory we have more work todo.
2307 *
2308 * In the case of a short read we start with find_task_by_pid.
2309 *
2310 * In the case of a seek we start with the leader and walk nr
2311 * threads past it.
2312 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002313static struct task_struct *first_tid(struct task_struct *leader,
2314 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002315{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002316 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002317
Eric W. Biedermancc288732006-06-26 00:26:01 -07002318 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002319 /* Attempt to start with the pid of a thread */
2320 if (tid && (nr > 0)) {
2321 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002322 if (pos && (pos->group_leader == leader))
2323 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002324 }
2325
2326 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002327 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002328 if (nr && nr >= get_nr_threads(leader))
2329 goto out;
2330
2331 /* If we haven't found our starting place yet start
2332 * with the leader and walk nr threads forward.
2333 */
2334 for (pos = leader; nr > 0; --nr) {
2335 pos = next_thread(pos);
2336 if (pos == leader) {
2337 pos = NULL;
2338 goto out;
2339 }
2340 }
2341found:
2342 get_task_struct(pos);
2343out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002344 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002345 return pos;
2346}
2347
2348/*
2349 * Find the next thread in the thread list.
2350 * Return NULL if there is an error or no next thread.
2351 *
2352 * The reference to the input task_struct is released.
2353 */
2354static struct task_struct *next_tid(struct task_struct *start)
2355{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002356 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002357 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002358 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002359 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002360 if (thread_group_leader(pos))
2361 pos = NULL;
2362 else
2363 get_task_struct(pos);
2364 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002365 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002366 put_task_struct(start);
2367 return pos;
2368}
2369
Eric W. Biederman61a28782006-10-02 02:18:49 -07002370static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2371 struct task_struct *task, int tid)
2372{
2373 char name[PROC_NUMBUF];
2374 int len = snprintf(name, sizeof(name), "%d", tid);
2375 return proc_fill_cache(filp, dirent, filldir, name, len,
2376 proc_task_instantiate, task, NULL);
2377}
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379/* for the /proc/TGID/task/ directories */
2380static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2381{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002382 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002384 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002385 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 int retval = -ENOENT;
2387 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002388 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2390
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002391 task = get_proc_task(inode);
2392 if (!task)
2393 goto out_no_task;
2394 rcu_read_lock();
2395 if (pid_alive(task)) {
2396 leader = task->group_leader;
2397 get_task_struct(leader);
2398 }
2399 rcu_read_unlock();
2400 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002401 if (!leader)
2402 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 retval = 0;
2404
2405 switch (pos) {
2406 case 0:
2407 ino = inode->i_ino;
2408 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2409 goto out;
2410 pos++;
2411 /* fall through */
2412 case 1:
2413 ino = parent_ino(dentry);
2414 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2415 goto out;
2416 pos++;
2417 /* fall through */
2418 }
2419
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002420 /* f_version caches the tgid value that the last readdir call couldn't
2421 * return. lseek aka telldir automagically resets f_version to 0.
2422 */
2423 tid = filp->f_version;
2424 filp->f_version = 0;
2425 for (task = first_tid(leader, tid, pos - 2);
2426 task;
2427 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002428 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002429 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002430 /* returning this tgid failed, save it as the first
2431 * pid for the next readir call */
2432 filp->f_version = tid;
2433 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437out:
2438 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002439 put_task_struct(leader);
2440out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return retval;
2442}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002443
2444static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2445{
2446 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002447 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002448 generic_fillattr(inode, stat);
2449
Eric W. Biederman99f89552006-06-26 00:25:55 -07002450 if (p) {
2451 rcu_read_lock();
2452 stat->nlink += get_nr_threads(p);
2453 rcu_read_unlock();
2454 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002455 }
2456
2457 return 0;
2458}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002459
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002460static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002461 .lookup = proc_task_lookup,
2462 .getattr = proc_task_getattr,
2463 .setattr = proc_setattr,
2464};
2465
Arjan van de Ven00977a52007-02-12 00:55:34 -08002466static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002467 .read = generic_read_dir,
2468 .readdir = proc_task_readdir,
2469};