blob: be94ddebb413273309b9c111986b2ec8979daeae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
64#include <linux/smp_lock.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070065#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kallsyms.h>
Kees Cook5096add2007-05-08 00:26:04 -070067#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/mount.h>
69#include <linux/security.h>
70#include <linux/ptrace.h>
71#include <linux/seccomp.h>
72#include <linux/cpuset.h>
73#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050074#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070075#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070076#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include "internal.h"
78
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070079/* NOTE:
80 * Implementing inode permission operations in /proc is almost
81 * certainly an error. Permission checks need to happen during
82 * each system call not at open time. The reason is that most of
83 * what we wish to check for permissions in /proc varies at runtime.
84 *
85 * The classic example of a problem is opening file descriptors
86 * in /proc for a task before it execs a suid executable.
87 */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Eric W. Biederman8578cea2006-06-26 00:25:54 -070090/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070091#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070095 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080097 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080098 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070099 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Eric W. Biederman61a28782006-10-02 02:18:49 -0700102#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700104 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700105 .mode = MODE, \
106 .iop = IOP, \
107 .fop = FOP, \
108 .op = OP, \
109}
110
Eric W. Biederman61a28782006-10-02 02:18:49 -0700111#define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700115#define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119#define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700121 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700122#define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Kees Cook5096add2007-05-08 00:26:04 -0700127int maps_protect;
128EXPORT_SYMBOL(maps_protect);
129
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700130static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700133 task_lock(task);
134 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if(fs)
136 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700137 task_unlock(task);
138 return fs;
139}
140
Eric W. Biederman99f89552006-06-26 00:25:55 -0700141static int get_nr_threads(struct task_struct *tsk)
142{
143 /* Must be called with the rcu_read_lock held */
144 unsigned long flags;
145 int count = 0;
146
147 if (lock_task_sighand(tsk, &flags)) {
148 count = atomic_read(&tsk->signal->count);
149 unlock_task_sighand(tsk, &flags);
150 }
151 return count;
152}
153
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700154static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
155{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700156 struct task_struct *task = get_proc_task(inode);
157 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700158 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700159
160 if (task) {
161 fs = get_fs_struct(task);
162 put_task_struct(task);
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (fs) {
165 read_lock(&fs->lock);
166 *mnt = mntget(fs->pwdmnt);
167 *dentry = dget(fs->pwd);
168 read_unlock(&fs->lock);
169 result = 0;
170 put_fs_struct(fs);
171 }
172 return result;
173}
174
175static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
176{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177 struct task_struct *task = get_proc_task(inode);
178 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700180
181 if (task) {
182 fs = get_fs_struct(task);
183 put_task_struct(task);
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (fs) {
186 read_lock(&fs->lock);
187 *mnt = mntget(fs->rootmnt);
188 *dentry = dget(fs->root);
189 read_unlock(&fs->lock);
190 result = 0;
191 put_fs_struct(fs);
192 }
193 return result;
194}
195
196#define MAY_PTRACE(task) \
197 (task == current || \
198 (task->parent == current && \
199 (task->ptrace & PT_PTRACED) && \
200 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
201 security_ptrace(current,task) == 0))
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static int proc_pid_environ(struct task_struct *task, char * buffer)
204{
205 int res = 0;
206 struct mm_struct *mm = get_task_mm(task);
207 if (mm) {
208 unsigned int len = mm->env_end - mm->env_start;
209 if (len > PAGE_SIZE)
210 len = PAGE_SIZE;
211 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700212 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 res = -ESRCH;
214 mmput(mm);
215 }
216 return res;
217}
218
219static int proc_pid_cmdline(struct task_struct *task, char * buffer)
220{
221 int res = 0;
222 unsigned int len;
223 struct mm_struct *mm = get_task_mm(task);
224 if (!mm)
225 goto out;
226 if (!mm->arg_end)
227 goto out_mm; /* Shh! No looking before we're done */
228
229 len = mm->arg_end - mm->arg_start;
230
231 if (len > PAGE_SIZE)
232 len = PAGE_SIZE;
233
234 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
235
236 // If the nul at the end of args has been overwritten, then
237 // assume application is using setproctitle(3).
238 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
239 len = strnlen(buffer, res);
240 if (len < res) {
241 res = len;
242 } else {
243 len = mm->env_end - mm->env_start;
244 if (len > PAGE_SIZE - res)
245 len = PAGE_SIZE - res;
246 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
247 res = strnlen(buffer, res);
248 }
249 }
250out_mm:
251 mmput(mm);
252out:
253 return res;
254}
255
256static int proc_pid_auxv(struct task_struct *task, char *buffer)
257{
258 int res = 0;
259 struct mm_struct *mm = get_task_mm(task);
260 if (mm) {
261 unsigned int nwords = 0;
262 do
263 nwords += 2;
264 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
265 res = nwords * sizeof(mm->saved_auxv[0]);
266 if (res > PAGE_SIZE)
267 res = PAGE_SIZE;
268 memcpy(buffer, mm->saved_auxv, res);
269 mmput(mm);
270 }
271 return res;
272}
273
274
275#ifdef CONFIG_KALLSYMS
276/*
277 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
278 * Returns the resolved symbol. If that fails, simply return the address.
279 */
280static int proc_pid_wchan(struct task_struct *task, char *buffer)
281{
282 char *modname;
283 const char *sym_name;
284 unsigned long wchan, size, offset;
285 char namebuf[KSYM_NAME_LEN+1];
286
287 wchan = get_wchan(task);
288
289 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
290 if (sym_name)
291 return sprintf(buffer, "%s", sym_name);
292 return sprintf(buffer, "%lu", wchan);
293}
294#endif /* CONFIG_KALLSYMS */
295
296#ifdef CONFIG_SCHEDSTATS
297/*
298 * Provides /proc/PID/schedstat
299 */
300static int proc_pid_schedstat(struct task_struct *task, char *buffer)
301{
302 return sprintf(buffer, "%lu %lu %lu\n",
303 task->sched_info.cpu_time,
304 task->sched_info.run_delay,
305 task->sched_info.pcnt);
306}
307#endif
308
309/* The badness from the OOM killer */
310unsigned long badness(struct task_struct *p, unsigned long uptime);
311static int proc_oom_score(struct task_struct *task, char *buffer)
312{
313 unsigned long points;
314 struct timespec uptime;
315
316 do_posix_clock_monotonic_gettime(&uptime);
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 Dumazetc5141e62007-05-08 00:26:15 -07001162typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1163 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001164
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001165/*
1166 * Fill a directory entry.
1167 *
1168 * If possible create the dcache entry and derive our inode number and
1169 * file type from dcache entry.
1170 *
1171 * Since all of the proc inode numbers are dynamically generated, the inode
1172 * numbers do not exist until the inode is cache. This means creating the
1173 * the dcache entry in readdir is necessary to keep the inode numbers
1174 * reported by readdir in sync with the inode numbers reported
1175 * by stat.
1176 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001177static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1178 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001179 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001180{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001181 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001182 struct inode *inode;
1183 struct qstr qname;
1184 ino_t ino = 0;
1185 unsigned type = DT_UNKNOWN;
1186
1187 qname.name = name;
1188 qname.len = len;
1189 qname.hash = full_name_hash(name, len);
1190
1191 child = d_lookup(dir, &qname);
1192 if (!child) {
1193 struct dentry *new;
1194 new = d_alloc(dir, &qname);
1195 if (new) {
1196 child = instantiate(dir->d_inode, new, task, ptr);
1197 if (child)
1198 dput(new);
1199 else
1200 child = new;
1201 }
1202 }
1203 if (!child || IS_ERR(child) || !child->d_inode)
1204 goto end_instantiate;
1205 inode = child->d_inode;
1206 if (inode) {
1207 ino = inode->i_ino;
1208 type = inode->i_mode >> 12;
1209 }
1210 dput(child);
1211end_instantiate:
1212 if (!ino)
1213 ino = find_inode_number(dir, &qname);
1214 if (!ino)
1215 ino = 1;
1216 return filldir(dirent, name, len, filp->f_pos, ino, type);
1217}
1218
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001219static unsigned name_to_int(struct dentry *dentry)
1220{
1221 const char *name = dentry->d_name.name;
1222 int len = dentry->d_name.len;
1223 unsigned n = 0;
1224
1225 if (len > 1 && *name == '0')
1226 goto out;
1227 while (len-- > 0) {
1228 unsigned c = *name++ - '0';
1229 if (c > 9)
1230 goto out;
1231 if (n >= (~0U-9)/10)
1232 goto out;
1233 n *= 10;
1234 n += c;
1235 }
1236 return n;
1237out:
1238 return ~0U;
1239}
1240
1241static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1242{
1243 struct task_struct *task = get_proc_task(inode);
1244 struct files_struct *files = NULL;
1245 struct file *file;
1246 int fd = proc_fd(inode);
1247
1248 if (task) {
1249 files = get_files_struct(task);
1250 put_task_struct(task);
1251 }
1252 if (files) {
1253 /*
1254 * We are not taking a ref to the file structure, so we must
1255 * hold ->file_lock.
1256 */
1257 spin_lock(&files->file_lock);
1258 file = fcheck_files(files, fd);
1259 if (file) {
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001260 *mnt = mntget(file->f_path.mnt);
1261 *dentry = dget(file->f_path.dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001262 spin_unlock(&files->file_lock);
1263 put_files_struct(files);
1264 return 0;
1265 }
1266 spin_unlock(&files->file_lock);
1267 put_files_struct(files);
1268 }
1269 return -ENOENT;
1270}
1271
1272static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1273{
1274 struct inode *inode = dentry->d_inode;
1275 struct task_struct *task = get_proc_task(inode);
1276 int fd = proc_fd(inode);
1277 struct files_struct *files;
1278
1279 if (task) {
1280 files = get_files_struct(task);
1281 if (files) {
1282 rcu_read_lock();
1283 if (fcheck_files(files, fd)) {
1284 rcu_read_unlock();
1285 put_files_struct(files);
1286 if (task_dumpable(task)) {
1287 inode->i_uid = task->euid;
1288 inode->i_gid = task->egid;
1289 } else {
1290 inode->i_uid = 0;
1291 inode->i_gid = 0;
1292 }
1293 inode->i_mode &= ~(S_ISUID | S_ISGID);
1294 security_task_to_inode(task, inode);
1295 put_task_struct(task);
1296 return 1;
1297 }
1298 rcu_read_unlock();
1299 put_files_struct(files);
1300 }
1301 put_task_struct(task);
1302 }
1303 d_drop(dentry);
1304 return 0;
1305}
1306
1307static struct dentry_operations tid_fd_dentry_operations =
1308{
1309 .d_revalidate = tid_fd_revalidate,
1310 .d_delete = pid_delete_dentry,
1311};
1312
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001313static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001314 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001315{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001316 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001317 struct file *file;
1318 struct files_struct *files;
1319 struct inode *inode;
1320 struct proc_inode *ei;
1321 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001322
Eric W. Biederman61a28782006-10-02 02:18:49 -07001323 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001324 if (!inode)
1325 goto out;
1326 ei = PROC_I(inode);
1327 ei->fd = fd;
1328 files = get_files_struct(task);
1329 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001330 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001331 inode->i_mode = S_IFLNK;
1332
1333 /*
1334 * We are not taking a ref to the file structure, so we must
1335 * hold ->file_lock.
1336 */
1337 spin_lock(&files->file_lock);
1338 file = fcheck_files(files, fd);
1339 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001340 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001341 if (file->f_mode & 1)
1342 inode->i_mode |= S_IRUSR | S_IXUSR;
1343 if (file->f_mode & 2)
1344 inode->i_mode |= S_IWUSR | S_IXUSR;
1345 spin_unlock(&files->file_lock);
1346 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001347
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001348 inode->i_op = &proc_pid_link_inode_operations;
1349 inode->i_size = 64;
1350 ei->op.proc_get_link = proc_fd_link;
1351 dentry->d_op = &tid_fd_dentry_operations;
1352 d_add(dentry, inode);
1353 /* Close the race of the process dying before we return the dentry */
1354 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001355 error = NULL;
1356
1357 out:
1358 return error;
1359out_unlock:
1360 spin_unlock(&files->file_lock);
1361 put_files_struct(files);
1362out_iput:
1363 iput(inode);
1364 goto out;
1365}
1366
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001367static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1368{
1369 struct task_struct *task = get_proc_task(dir);
1370 unsigned fd = name_to_int(dentry);
1371 struct dentry *result = ERR_PTR(-ENOENT);
1372
1373 if (!task)
1374 goto out_no_task;
1375 if (fd == ~0U)
1376 goto out;
1377
1378 result = proc_fd_instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001379out:
1380 put_task_struct(task);
1381out_no_task:
1382 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001383}
1384
Eric W. Biederman61a28782006-10-02 02:18:49 -07001385static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1386 struct task_struct *task, int fd)
1387{
1388 char name[PROC_NUMBUF];
1389 int len = snprintf(name, sizeof(name), "%d", fd);
1390 return proc_fill_cache(filp, dirent, filldir, name, len,
1391 proc_fd_instantiate, task, &fd);
1392}
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1395{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001396 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001397 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001398 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 unsigned int fd, tid, ino;
1400 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001402 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001405 if (!p)
1406 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 retval = 0;
1408 tid = p->pid;
1409
1410 fd = filp->f_pos;
1411 switch (fd) {
1412 case 0:
1413 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1414 goto out;
1415 filp->f_pos++;
1416 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001417 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1419 goto out;
1420 filp->f_pos++;
1421 default:
1422 files = get_files_struct(p);
1423 if (!files)
1424 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001425 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001426 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001428 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 fd++, filp->f_pos++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 if (!fcheck_files(files, fd))
1432 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001433 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Eric W. Biederman61a28782006-10-02 02:18:49 -07001435 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001436 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 break;
1438 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001439 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001441 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 put_files_struct(files);
1443 }
1444out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001445 put_task_struct(p);
1446out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return retval;
1448}
1449
Arjan van de Ven00977a52007-02-12 00:55:34 -08001450static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 .read = generic_read_dir,
1452 .readdir = proc_readfd,
1453};
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001456 * /proc/pid/fd needs a special permission handler so that a process can still
1457 * access /proc/self/fd after it has executed a setuid().
1458 */
1459static int proc_fd_permission(struct inode *inode, int mask,
1460 struct nameidata *nd)
1461{
1462 int rv;
1463
1464 rv = generic_permission(inode, mask, NULL);
1465 if (rv == 0)
1466 return 0;
1467 if (task_pid(current) == proc_pid(inode))
1468 rv = 0;
1469 return rv;
1470}
1471
1472/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * proc directories can do almost nothing..
1474 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001475static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001477 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001478 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479};
1480
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001481static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001482 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001483{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001484 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001485 struct inode *inode;
1486 struct proc_inode *ei;
1487 struct dentry *error = ERR_PTR(-EINVAL);
1488
Eric W. Biederman61a28782006-10-02 02:18:49 -07001489 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001490 if (!inode)
1491 goto out;
1492
1493 ei = PROC_I(inode);
1494 inode->i_mode = p->mode;
1495 if (S_ISDIR(inode->i_mode))
1496 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1497 if (p->iop)
1498 inode->i_op = p->iop;
1499 if (p->fop)
1500 inode->i_fop = p->fop;
1501 ei->op = p->op;
1502 dentry->d_op = &pid_dentry_operations;
1503 d_add(dentry, inode);
1504 /* Close the race of the process dying before we return the dentry */
1505 if (pid_revalidate(dentry, NULL))
1506 error = NULL;
1507out:
1508 return error;
1509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511static struct dentry *proc_pident_lookup(struct inode *dir,
1512 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001513 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001514 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001517 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001518 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001519 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001521 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 inode = NULL;
1523
Eric W. Biederman99f89552006-06-26 00:25:55 -07001524 if (!task)
1525 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001527 /*
1528 * Yes, it does not scale. And it should not. Don't add
1529 * new entries into /proc/<tgid>/ without very good reasons.
1530 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001531 last = &ents[nents - 1];
1532 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (p->len != dentry->d_name.len)
1534 continue;
1535 if (!memcmp(dentry->d_name.name, p->name, p->len))
1536 break;
1537 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001538 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 goto out;
1540
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001541 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001543 put_task_struct(task);
1544out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001545 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Eric Dumazetc5141e62007-05-08 00:26:15 -07001548static int proc_pident_fill_cache(struct file *filp, void *dirent,
1549 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001550{
1551 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1552 proc_pident_instantiate, task, p);
1553}
1554
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001555static int proc_pident_readdir(struct file *filp,
1556 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001557 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001558{
1559 int i;
1560 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001561 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001562 struct inode *inode = dentry->d_inode;
1563 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001564 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001565 ino_t ino;
1566 int ret;
1567
1568 ret = -ENOENT;
1569 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001570 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001571
1572 ret = 0;
1573 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001574 i = filp->f_pos;
1575 switch (i) {
1576 case 0:
1577 ino = inode->i_ino;
1578 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1579 goto out;
1580 i++;
1581 filp->f_pos++;
1582 /* fall through */
1583 case 1:
1584 ino = parent_ino(dentry);
1585 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1586 goto out;
1587 i++;
1588 filp->f_pos++;
1589 /* fall through */
1590 default:
1591 i -= 2;
1592 if (i >= nents) {
1593 ret = 1;
1594 goto out;
1595 }
1596 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001597 last = &ents[nents - 1];
1598 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001599 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001600 goto out;
1601 filp->f_pos++;
1602 p++;
1603 }
1604 }
1605
1606 ret = 1;
1607out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001608 put_task_struct(task);
1609out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001610 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001614static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1615 size_t count, loff_t *ppos)
1616{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001617 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001618 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001619 ssize_t length;
1620 struct task_struct *task = get_proc_task(inode);
1621
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001622 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001623 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001624
1625 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001626 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001627 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001628 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001629 if (length > 0)
1630 length = simple_read_from_buffer(buf, count, ppos, p, length);
1631 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001632 return length;
1633}
1634
1635static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1636 size_t count, loff_t *ppos)
1637{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001638 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001639 char *page;
1640 ssize_t length;
1641 struct task_struct *task = get_proc_task(inode);
1642
1643 length = -ESRCH;
1644 if (!task)
1645 goto out_no_task;
1646 if (count > PAGE_SIZE)
1647 count = PAGE_SIZE;
1648
1649 /* No partial writes. */
1650 length = -EINVAL;
1651 if (*ppos != 0)
1652 goto out;
1653
1654 length = -ENOMEM;
1655 page = (char*)__get_free_page(GFP_USER);
1656 if (!page)
1657 goto out;
1658
1659 length = -EFAULT;
1660 if (copy_from_user(page, buf, count))
1661 goto out_free;
1662
1663 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001664 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001665 (void*)page, count);
1666out_free:
1667 free_page((unsigned long) page);
1668out:
1669 put_task_struct(task);
1670out_no_task:
1671 return length;
1672}
1673
Arjan van de Ven00977a52007-02-12 00:55:34 -08001674static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001675 .read = proc_pid_attr_read,
1676 .write = proc_pid_attr_write,
1677};
1678
Eric Dumazetc5141e62007-05-08 00:26:15 -07001679static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001680 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1681 REG("prev", S_IRUGO, pid_attr),
1682 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1683 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1684 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1685 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001686};
1687
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001688static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 void * dirent, filldir_t filldir)
1690{
1691 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001692 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Arjan van de Ven00977a52007-02-12 00:55:34 -08001695static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001697 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698};
1699
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001700static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 struct dentry *dentry, struct nameidata *nd)
1702{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001703 return proc_pident_lookup(dir, dentry,
1704 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001707static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001708 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001709 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001710 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711};
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713#endif
1714
1715/*
1716 * /proc/self:
1717 */
1718static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1719 int buflen)
1720{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001721 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 sprintf(tmp, "%d", current->tgid);
1723 return vfs_readlink(dentry,buffer,buflen,tmp);
1724}
1725
Al Viro008b1502005-08-20 00:17:39 +01001726static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001728 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001730 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001733static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 .readlink = proc_self_readlink,
1735 .follow_link = proc_self_follow_link,
1736};
1737
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001738/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001739 * proc base
1740 *
1741 * These are the directory entries in the root directory of /proc
1742 * that properly belong to the /proc filesystem, as they describe
1743 * describe something that is process related.
1744 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07001745static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001746 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001747 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001748};
1749
1750/*
1751 * Exceptional case: normally we are not allowed to unhash a busy
1752 * directory. In this case, however, we can do it - no aliasing problems
1753 * due to the way we treat inodes.
1754 */
1755static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1756{
1757 struct inode *inode = dentry->d_inode;
1758 struct task_struct *task = get_proc_task(inode);
1759 if (task) {
1760 put_task_struct(task);
1761 return 1;
1762 }
1763 d_drop(dentry);
1764 return 0;
1765}
1766
1767static struct dentry_operations proc_base_dentry_operations =
1768{
1769 .d_revalidate = proc_base_revalidate,
1770 .d_delete = pid_delete_dentry,
1771};
1772
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001773static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001774 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001775{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001776 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001777 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001778 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001779 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001780
1781 /* Allocate the inode */
1782 error = ERR_PTR(-ENOMEM);
1783 inode = new_inode(dir->i_sb);
1784 if (!inode)
1785 goto out;
1786
1787 /* Initialize the inode */
1788 ei = PROC_I(inode);
1789 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001790
1791 /*
1792 * grab the reference to the task.
1793 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001794 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001795 if (!ei->pid)
1796 goto out_iput;
1797
1798 inode->i_uid = 0;
1799 inode->i_gid = 0;
1800 inode->i_mode = p->mode;
1801 if (S_ISDIR(inode->i_mode))
1802 inode->i_nlink = 2;
1803 if (S_ISLNK(inode->i_mode))
1804 inode->i_size = 64;
1805 if (p->iop)
1806 inode->i_op = p->iop;
1807 if (p->fop)
1808 inode->i_fop = p->fop;
1809 ei->op = p->op;
1810 dentry->d_op = &proc_base_dentry_operations;
1811 d_add(dentry, inode);
1812 error = NULL;
1813out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001814 return error;
1815out_iput:
1816 iput(inode);
1817 goto out;
1818}
1819
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001820static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1821{
1822 struct dentry *error;
1823 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001824 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001825
1826 error = ERR_PTR(-ENOENT);
1827
1828 if (!task)
1829 goto out_no_task;
1830
1831 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001832 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1833 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001834 if (p->len != dentry->d_name.len)
1835 continue;
1836 if (!memcmp(dentry->d_name.name, p->name, p->len))
1837 break;
1838 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001839 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001840 goto out;
1841
1842 error = proc_base_instantiate(dir, dentry, task, p);
1843
1844out:
1845 put_task_struct(task);
1846out_no_task:
1847 return error;
1848}
1849
Eric Dumazetc5141e62007-05-08 00:26:15 -07001850static int proc_base_fill_cache(struct file *filp, void *dirent,
1851 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001852{
1853 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1854 proc_base_instantiate, task, p);
1855}
1856
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001857#ifdef CONFIG_TASK_IO_ACCOUNTING
1858static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1859{
1860 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001861#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001862 "rchar: %llu\n"
1863 "wchar: %llu\n"
1864 "syscr: %llu\n"
1865 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001866#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001867 "read_bytes: %llu\n"
1868 "write_bytes: %llu\n"
1869 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001870#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001871 (unsigned long long)task->rchar,
1872 (unsigned long long)task->wchar,
1873 (unsigned long long)task->syscr,
1874 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001875#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001876 (unsigned long long)task->ioac.read_bytes,
1877 (unsigned long long)task->ioac.write_bytes,
1878 (unsigned long long)task->ioac.cancelled_write_bytes);
1879}
1880#endif
1881
Eric W. Biederman801199c2006-10-02 02:18:48 -07001882/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001883 * Thread groups
1884 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08001885static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001886static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001887
Eric Dumazetc5141e62007-05-08 00:26:15 -07001888static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001889 DIR("task", S_IRUGO|S_IXUGO, task),
1890 DIR("fd", S_IRUSR|S_IXUSR, fd),
1891 INF("environ", S_IRUSR, pid_environ),
1892 INF("auxv", S_IRUSR, pid_auxv),
1893 INF("status", S_IRUGO, pid_status),
1894 INF("cmdline", S_IRUGO, pid_cmdline),
1895 INF("stat", S_IRUGO, tgid_stat),
1896 INF("statm", S_IRUGO, pid_statm),
1897 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001898#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001899 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001900#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001901 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001902#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07001903 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001904#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001905 LNK("cwd", cwd),
1906 LNK("root", root),
1907 LNK("exe", exe),
1908 REG("mounts", S_IRUGO, mounts),
1909 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001910#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07001911 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07001912 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001913#endif
1914#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001915 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001916#endif
1917#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001918 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001919#endif
1920#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001921 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001922#endif
1923#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001924 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001925#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001926 INF("oom_score", S_IRUGO, oom_score),
1927 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001928#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07001929 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001930#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001931#ifdef CONFIG_FAULT_INJECTION
1932 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1933#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001934#ifdef CONFIG_TASK_IO_ACCOUNTING
1935 INF("io", S_IRUGO, pid_io_accounting),
1936#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001937};
1938
1939static int proc_tgid_base_readdir(struct file * filp,
1940 void * dirent, filldir_t filldir)
1941{
1942 return proc_pident_readdir(filp,dirent,filldir,
1943 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1944}
1945
Arjan van de Ven00977a52007-02-12 00:55:34 -08001946static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001947 .read = generic_read_dir,
1948 .readdir = proc_tgid_base_readdir,
1949};
1950
1951static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001952 return proc_pident_lookup(dir, dentry,
1953 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001954}
1955
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001956static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001957 .lookup = proc_tgid_base_lookup,
1958 .getattr = pid_getattr,
1959 .setattr = proc_setattr,
1960};
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001963 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001965 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001967 * Looks in the dcache for
1968 * /proc/@pid
1969 * /proc/@tgid/task/@pid
1970 * if either directory is present flushes it and all of it'ts children
1971 * from the dcache.
1972 *
1973 * It is safe and reasonable to cache /proc entries for a task until
1974 * that task exits. After that they just clog up the dcache with
1975 * useless entries, possibly causing useful dcache entries to be
1976 * flushed instead. This routine is proved to flush those useless
1977 * dcache entries at process exit time.
1978 *
1979 * NOTE: This routine is just an optimization so it does not guarantee
1980 * that no dcache entries will exist at process exit time it
1981 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001983void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001985 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001986 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001987 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Eric W. Biederman48e64842006-06-26 00:25:48 -07001989 name.name = buf;
1990 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1991 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1992 if (dentry) {
1993 shrink_dcache_parent(dentry);
1994 d_drop(dentry);
1995 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Eric W. Biederman48e64842006-06-26 00:25:48 -07001998 if (thread_group_leader(task))
1999 goto out;
2000
2001 name.name = buf;
2002 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2003 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2004 if (!leader)
2005 goto out;
2006
2007 name.name = "task";
2008 name.len = strlen(name.name);
2009 dir = d_hash_and_lookup(leader, &name);
2010 if (!dir)
2011 goto out_put_leader;
2012
2013 name.name = buf;
2014 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2015 dentry = d_hash_and_lookup(dir, &name);
2016 if (dentry) {
2017 shrink_dcache_parent(dentry);
2018 d_drop(dentry);
2019 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002021
2022 dput(dir);
2023out_put_leader:
2024 dput(leader);
2025out:
2026 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
Adrian Bunk9711ef92006-12-06 20:38:31 -08002029static struct dentry *proc_pid_instantiate(struct inode *dir,
2030 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002031 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002032{
2033 struct dentry *error = ERR_PTR(-ENOENT);
2034 struct inode *inode;
2035
Eric W. Biederman61a28782006-10-02 02:18:49 -07002036 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002037 if (!inode)
2038 goto out;
2039
2040 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2041 inode->i_op = &proc_tgid_base_inode_operations;
2042 inode->i_fop = &proc_tgid_base_operations;
2043 inode->i_flags|=S_IMMUTABLE;
2044 inode->i_nlink = 4;
2045#ifdef CONFIG_SECURITY
2046 inode->i_nlink += 1;
2047#endif
2048
2049 dentry->d_op = &pid_dentry_operations;
2050
2051 d_add(dentry, inode);
2052 /* Close the race of the process dying before we return the dentry */
2053 if (pid_revalidate(dentry, NULL))
2054 error = NULL;
2055out:
2056 return error;
2057}
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2060{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002061 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Eric W. Biederman801199c2006-10-02 02:18:48 -07002065 result = proc_base_lookup(dir, dentry);
2066 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2067 goto out;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 tgid = name_to_int(dentry);
2070 if (tgid == ~0U)
2071 goto out;
2072
Eric W. Biedermande758732006-06-26 00:25:51 -07002073 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 task = find_task_by_pid(tgid);
2075 if (task)
2076 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002077 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (!task)
2079 goto out;
2080
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002081 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002082 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002084 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002088 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002089 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002091static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002093 struct task_struct *task;
2094 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002096 rcu_read_lock();
2097retry:
2098 task = NULL;
2099 pid = find_ge_pid(tgid);
2100 if (pid) {
2101 tgid = pid->nr + 1;
2102 task = pid_task(pid, PIDTYPE_PID);
2103 /* What we to know is if the pid we have find is the
2104 * pid of a thread_group_leader. Testing for task
2105 * being a thread_group_leader is the obvious thing
2106 * todo but there is a window when it fails, due to
2107 * the pid transfer logic in de_thread.
2108 *
2109 * So we perform the straight forward test of seeing
2110 * if the pid we have found is the pid of a thread
2111 * group leader, and don't worry if the task we have
2112 * found doesn't happen to be a thread group leader.
2113 * As we don't care in the case of readdir.
2114 */
2115 if (!task || !has_group_leader_pid(task))
2116 goto retry;
2117 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002119 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002120 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002123#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Eric W. Biederman61a28782006-10-02 02:18:49 -07002125static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2126 struct task_struct *task, int tgid)
2127{
2128 char name[PROC_NUMBUF];
2129 int len = snprintf(name, sizeof(name), "%d", tgid);
2130 return proc_fill_cache(filp, dirent, filldir, name, len,
2131 proc_pid_instantiate, task, NULL);
2132}
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134/* for the /proc/ directory itself, after non-process stuff has been done */
2135int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002138 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002139 struct task_struct *task;
2140 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Eric W. Biederman61a28782006-10-02 02:18:49 -07002142 if (!reaper)
2143 goto out_no_task;
2144
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002145 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002146 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002147 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002148 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002151 tgid = filp->f_pos - TGID_OFFSET;
2152 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002153 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002154 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002155 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002156 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002157 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002158 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002162 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2163out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002164 put_task_struct(reaper);
2165out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return 0;
2167}
2168
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002169/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002170 * Tasks
2171 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002172static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002173 DIR("fd", S_IRUSR|S_IXUSR, fd),
2174 INF("environ", S_IRUSR, pid_environ),
2175 INF("auxv", S_IRUSR, pid_auxv),
2176 INF("status", S_IRUGO, pid_status),
2177 INF("cmdline", S_IRUGO, pid_cmdline),
2178 INF("stat", S_IRUGO, tid_stat),
2179 INF("statm", S_IRUGO, pid_statm),
2180 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002181#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002182 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002183#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002184 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002185#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002186 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002187#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002188 LNK("cwd", cwd),
2189 LNK("root", root),
2190 LNK("exe", exe),
2191 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002192#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002193 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002194 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002195#endif
2196#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002197 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002198#endif
2199#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002200 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002201#endif
2202#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002203 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002204#endif
2205#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002206 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002207#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002208 INF("oom_score", S_IRUGO, oom_score),
2209 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002210#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002211 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002212#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002213#ifdef CONFIG_FAULT_INJECTION
2214 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2215#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002216};
2217
2218static int proc_tid_base_readdir(struct file * filp,
2219 void * dirent, filldir_t filldir)
2220{
2221 return proc_pident_readdir(filp,dirent,filldir,
2222 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2223}
2224
2225static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002226 return proc_pident_lookup(dir, dentry,
2227 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002228}
2229
Arjan van de Ven00977a52007-02-12 00:55:34 -08002230static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002231 .read = generic_read_dir,
2232 .readdir = proc_tid_base_readdir,
2233};
2234
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002235static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002236 .lookup = proc_tid_base_lookup,
2237 .getattr = pid_getattr,
2238 .setattr = proc_setattr,
2239};
2240
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002241static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002242 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002243{
2244 struct dentry *error = ERR_PTR(-ENOENT);
2245 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002246 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002247
2248 if (!inode)
2249 goto out;
2250 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2251 inode->i_op = &proc_tid_base_inode_operations;
2252 inode->i_fop = &proc_tid_base_operations;
2253 inode->i_flags|=S_IMMUTABLE;
2254 inode->i_nlink = 3;
2255#ifdef CONFIG_SECURITY
2256 inode->i_nlink += 1;
2257#endif
2258
2259 dentry->d_op = &pid_dentry_operations;
2260
2261 d_add(dentry, inode);
2262 /* Close the race of the process dying before we return the dentry */
2263 if (pid_revalidate(dentry, NULL))
2264 error = NULL;
2265out:
2266 return error;
2267}
2268
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002269static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2270{
2271 struct dentry *result = ERR_PTR(-ENOENT);
2272 struct task_struct *task;
2273 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002274 unsigned tid;
2275
2276 if (!leader)
2277 goto out_no_task;
2278
2279 tid = name_to_int(dentry);
2280 if (tid == ~0U)
2281 goto out;
2282
2283 rcu_read_lock();
2284 task = find_task_by_pid(tid);
2285 if (task)
2286 get_task_struct(task);
2287 rcu_read_unlock();
2288 if (!task)
2289 goto out;
2290 if (leader->tgid != task->tgid)
2291 goto out_drop_task;
2292
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002293 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002294out_drop_task:
2295 put_task_struct(task);
2296out:
2297 put_task_struct(leader);
2298out_no_task:
2299 return result;
2300}
2301
2302/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002303 * Find the first tid of a thread group to return to user space.
2304 *
2305 * Usually this is just the thread group leader, but if the users
2306 * buffer was too small or there was a seek into the middle of the
2307 * directory we have more work todo.
2308 *
2309 * In the case of a short read we start with find_task_by_pid.
2310 *
2311 * In the case of a seek we start with the leader and walk nr
2312 * threads past it.
2313 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002314static struct task_struct *first_tid(struct task_struct *leader,
2315 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002316{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002317 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002318
Eric W. Biedermancc288732006-06-26 00:26:01 -07002319 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002320 /* Attempt to start with the pid of a thread */
2321 if (tid && (nr > 0)) {
2322 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002323 if (pos && (pos->group_leader == leader))
2324 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002325 }
2326
2327 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002328 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002329 if (nr && nr >= get_nr_threads(leader))
2330 goto out;
2331
2332 /* If we haven't found our starting place yet start
2333 * with the leader and walk nr threads forward.
2334 */
2335 for (pos = leader; nr > 0; --nr) {
2336 pos = next_thread(pos);
2337 if (pos == leader) {
2338 pos = NULL;
2339 goto out;
2340 }
2341 }
2342found:
2343 get_task_struct(pos);
2344out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002345 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002346 return pos;
2347}
2348
2349/*
2350 * Find the next thread in the thread list.
2351 * Return NULL if there is an error or no next thread.
2352 *
2353 * The reference to the input task_struct is released.
2354 */
2355static struct task_struct *next_tid(struct task_struct *start)
2356{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002357 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002358 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002359 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002360 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002361 if (thread_group_leader(pos))
2362 pos = NULL;
2363 else
2364 get_task_struct(pos);
2365 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002366 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002367 put_task_struct(start);
2368 return pos;
2369}
2370
Eric W. Biederman61a28782006-10-02 02:18:49 -07002371static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2372 struct task_struct *task, int tid)
2373{
2374 char name[PROC_NUMBUF];
2375 int len = snprintf(name, sizeof(name), "%d", tid);
2376 return proc_fill_cache(filp, dirent, filldir, name, len,
2377 proc_task_instantiate, task, NULL);
2378}
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380/* for the /proc/TGID/task/ directories */
2381static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2382{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002383 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002385 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002386 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 int retval = -ENOENT;
2388 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002389 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2391
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002392 task = get_proc_task(inode);
2393 if (!task)
2394 goto out_no_task;
2395 rcu_read_lock();
2396 if (pid_alive(task)) {
2397 leader = task->group_leader;
2398 get_task_struct(leader);
2399 }
2400 rcu_read_unlock();
2401 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002402 if (!leader)
2403 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 retval = 0;
2405
2406 switch (pos) {
2407 case 0:
2408 ino = inode->i_ino;
2409 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2410 goto out;
2411 pos++;
2412 /* fall through */
2413 case 1:
2414 ino = parent_ino(dentry);
2415 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2416 goto out;
2417 pos++;
2418 /* fall through */
2419 }
2420
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002421 /* f_version caches the tgid value that the last readdir call couldn't
2422 * return. lseek aka telldir automagically resets f_version to 0.
2423 */
2424 tid = filp->f_version;
2425 filp->f_version = 0;
2426 for (task = first_tid(leader, tid, pos - 2);
2427 task;
2428 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002429 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002430 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002431 /* returning this tgid failed, save it as the first
2432 * pid for the next readir call */
2433 filp->f_version = tid;
2434 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
2438out:
2439 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002440 put_task_struct(leader);
2441out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return retval;
2443}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002444
2445static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2446{
2447 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002448 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002449 generic_fillattr(inode, stat);
2450
Eric W. Biederman99f89552006-06-26 00:25:55 -07002451 if (p) {
2452 rcu_read_lock();
2453 stat->nlink += get_nr_threads(p);
2454 rcu_read_unlock();
2455 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002456 }
2457
2458 return 0;
2459}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002460
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002461static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002462 .lookup = proc_task_lookup,
2463 .getattr = proc_task_getattr,
2464 .setattr = proc_setattr,
2465};
2466
Arjan van de Ven00977a52007-02-12 00:55:34 -08002467static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002468 .read = generic_read_dir,
2469 .readdir = proc_task_readdir,
2470};