blob: fbff900fd5ad3c690003935033318bda66802c17 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070064#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/kallsyms.h>
Kees Cook5096add2007-05-08 00:26:04 -070066#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/mount.h>
68#include <linux/security.h>
69#include <linux/ptrace.h>
Paul Menagea4243162007-10-18 23:39:35 -070070#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/cpuset.h>
72#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050073#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070074#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070075#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070076#include <linux/elf.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_cmdline(struct task_struct *task, char * buffer)
204{
205 int res = 0;
206 unsigned int len;
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 goto out;
210 if (!mm->arg_end)
211 goto out_mm; /* Shh! No looking before we're done */
212
213 len = mm->arg_end - mm->arg_start;
214
215 if (len > PAGE_SIZE)
216 len = PAGE_SIZE;
217
218 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
219
220 // If the nul at the end of args has been overwritten, then
221 // assume application is using setproctitle(3).
222 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
223 len = strnlen(buffer, res);
224 if (len < res) {
225 res = len;
226 } else {
227 len = mm->env_end - mm->env_start;
228 if (len > PAGE_SIZE - res)
229 len = PAGE_SIZE - res;
230 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
231 res = strnlen(buffer, res);
232 }
233 }
234out_mm:
235 mmput(mm);
236out:
237 return res;
238}
239
240static int proc_pid_auxv(struct task_struct *task, char *buffer)
241{
242 int res = 0;
243 struct mm_struct *mm = get_task_mm(task);
244 if (mm) {
245 unsigned int nwords = 0;
246 do
247 nwords += 2;
248 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
249 res = nwords * sizeof(mm->saved_auxv[0]);
250 if (res > PAGE_SIZE)
251 res = PAGE_SIZE;
252 memcpy(buffer, mm->saved_auxv, res);
253 mmput(mm);
254 }
255 return res;
256}
257
258
259#ifdef CONFIG_KALLSYMS
260/*
261 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
262 * Returns the resolved symbol. If that fails, simply return the address.
263 */
264static int proc_pid_wchan(struct task_struct *task, char *buffer)
265{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700266 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700267 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 wchan = get_wchan(task);
270
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700271 if (lookup_symbol_name(wchan, symname) < 0)
272 return sprintf(buffer, "%lu", wchan);
273 else
274 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276#endif /* CONFIG_KALLSYMS */
277
278#ifdef CONFIG_SCHEDSTATS
279/*
280 * Provides /proc/PID/schedstat
281 */
282static int proc_pid_schedstat(struct task_struct *task, char *buffer)
283{
Balbir Singh172ba842007-07-09 18:52:00 +0200284 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 task->sched_info.cpu_time,
286 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200287 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289#endif
290
291/* The badness from the OOM killer */
292unsigned long badness(struct task_struct *p, unsigned long uptime);
293static int proc_oom_score(struct task_struct *task, char *buffer)
294{
295 unsigned long points;
296 struct timespec uptime;
297
298 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700299 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700301 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return sprintf(buffer, "%lu\n", points);
303}
304
305/************************************************************************/
306/* Here the fs part begins */
307/************************************************************************/
308
309/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700310static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700312 struct task_struct *task;
313 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700314 /* Allow access to a task's file descriptors if it is us or we
315 * may use ptrace attach to the process and find out that
316 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700317 */
318 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700319 if (task) {
320 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700321 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700322 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700323 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700326static int proc_setattr(struct dentry *dentry, struct iattr *attr)
327{
328 int error;
329 struct inode *inode = dentry->d_inode;
330
331 if (attr->ia_valid & ATTR_MODE)
332 return -EPERM;
333
334 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700335 if (!error)
336 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700337 return error;
338}
339
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800340static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700341 .setattr = proc_setattr,
342};
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500345struct proc_mounts {
346 struct seq_file m;
347 int event;
348};
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static int mounts_open(struct inode *inode, struct file *file)
351{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700352 struct task_struct *task = get_proc_task(inode);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800353 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500354 struct proc_mounts *p;
355 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Eric W. Biederman99f89552006-06-26 00:25:55 -0700357 if (task) {
358 task_lock(task);
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800359 if (task->nsproxy) {
360 ns = task->nsproxy->mnt_ns;
361 if (ns)
362 get_mnt_ns(ns);
363 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700364 task_unlock(task);
365 put_task_struct(task);
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800368 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500369 ret = -ENOMEM;
370 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
371 if (p) {
372 file->private_data = &p->m;
373 ret = seq_open(file, &mounts_op);
374 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800375 p->m.private = ns;
376 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500377 return 0;
378 }
379 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800381 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383 return ret;
384}
385
386static int mounts_release(struct inode *inode, struct file *file)
387{
388 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800389 struct mnt_namespace *ns = m->private;
390 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return seq_release(inode, file);
392}
393
Al Viro5addc5d2005-11-07 17:15:49 -0500394static unsigned mounts_poll(struct file *file, poll_table *wait)
395{
396 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800397 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500398 unsigned res = 0;
399
400 poll_wait(file, &ns->poll, wait);
401
402 spin_lock(&vfsmount_lock);
403 if (p->event != ns->event) {
404 p->event = ns->event;
405 res = POLLERR;
406 }
407 spin_unlock(&vfsmount_lock);
408
409 return res;
410}
411
Arjan van de Ven00977a52007-02-12 00:55:34 -0800412static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 .open = mounts_open,
414 .read = seq_read,
415 .llseek = seq_lseek,
416 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500417 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418};
419
Chuck Leverb4629fe2006-03-20 13:44:12 -0500420extern struct seq_operations mountstats_op;
421static int mountstats_open(struct inode *inode, struct file *file)
422{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500423 int ret = seq_open(file, &mountstats_op);
424
425 if (!ret) {
426 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800427 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700428 struct task_struct *task = get_proc_task(inode);
429
430 if (task) {
431 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800432 if (task->nsproxy)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800433 mnt_ns = task->nsproxy->mnt_ns;
434 if (mnt_ns)
435 get_mnt_ns(mnt_ns);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700436 task_unlock(task);
437 put_task_struct(task);
438 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500439
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800440 if (mnt_ns)
441 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500442 else {
443 seq_release(inode, file);
444 ret = -EINVAL;
445 }
446 }
447 return ret;
448}
449
Arjan van de Ven00977a52007-02-12 00:55:34 -0800450static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500451 .open = mountstats_open,
452 .read = seq_read,
453 .llseek = seq_lseek,
454 .release = mounts_release,
455};
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
458
459static ssize_t proc_info_read(struct file * file, char __user * buf,
460 size_t count, loff_t *ppos)
461{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800462 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 unsigned long page;
464 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700465 struct task_struct *task = get_proc_task(inode);
466
467 length = -ESRCH;
468 if (!task)
469 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if (count > PROC_BLOCK_SIZE)
472 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700473
474 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700475 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700476 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 length = PROC_I(inode)->op.proc_read(task, (char*)page);
479
480 if (length >= 0)
481 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
482 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700483out:
484 put_task_struct(task);
485out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return length;
487}
488
Arjan van de Ven00977a52007-02-12 00:55:34 -0800489static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 .read = proc_info_read,
491};
492
493static int mem_open(struct inode* inode, struct file* file)
494{
495 file->private_data = (void*)((long)current->self_exec_id);
496 return 0;
497}
498
499static ssize_t mem_read(struct file * file, char __user * buf,
500 size_t count, loff_t *ppos)
501{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800502 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 char *page;
504 unsigned long src = *ppos;
505 int ret = -ESRCH;
506 struct mm_struct *mm;
507
Eric W. Biederman99f89552006-06-26 00:25:55 -0700508 if (!task)
509 goto out_no_task;
510
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700511 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto out;
513
514 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700515 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (!page)
517 goto out;
518
519 ret = 0;
520
521 mm = get_task_mm(task);
522 if (!mm)
523 goto out_free;
524
525 ret = -EIO;
526
527 if (file->private_data != (void*)((long)current->self_exec_id))
528 goto out_put;
529
530 ret = 0;
531
532 while (count > 0) {
533 int this_len, retval;
534
535 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
536 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700537 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (!ret)
539 ret = -EIO;
540 break;
541 }
542
543 if (copy_to_user(buf, page, retval)) {
544 ret = -EFAULT;
545 break;
546 }
547
548 ret += retval;
549 src += retval;
550 buf += retval;
551 count -= retval;
552 }
553 *ppos = src;
554
555out_put:
556 mmput(mm);
557out_free:
558 free_page((unsigned long) page);
559out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700560 put_task_struct(task);
561out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return ret;
563}
564
565#define mem_write NULL
566
567#ifndef mem_write
568/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800569static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 size_t count, loff_t *ppos)
571{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700572 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800574 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unsigned long dst = *ppos;
576
Eric W. Biederman99f89552006-06-26 00:25:55 -0700577 copied = -ESRCH;
578 if (!task)
579 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Eric W. Biederman99f89552006-06-26 00:25:55 -0700581 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
582 goto out;
583
584 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700585 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700587 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700589 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 while (count > 0) {
591 int this_len, retval;
592
593 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
594 if (copy_from_user(page, buf, this_len)) {
595 copied = -EFAULT;
596 break;
597 }
598 retval = access_process_vm(task, dst, page, this_len, 1);
599 if (!retval) {
600 if (!copied)
601 copied = -EIO;
602 break;
603 }
604 copied += retval;
605 buf += retval;
606 dst += retval;
607 count -= retval;
608 }
609 *ppos = dst;
610 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700611out:
612 put_task_struct(task);
613out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return copied;
615}
616#endif
617
618static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
619{
620 switch (orig) {
621 case 0:
622 file->f_pos = offset;
623 break;
624 case 1:
625 file->f_pos += offset;
626 break;
627 default:
628 return -EINVAL;
629 }
630 force_successful_syscall_return();
631 return file->f_pos;
632}
633
Arjan van de Ven00977a52007-02-12 00:55:34 -0800634static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 .llseek = mem_lseek,
636 .read = mem_read,
637 .write = mem_write,
638 .open = mem_open,
639};
640
James Pearson315e28c2007-10-16 23:30:17 -0700641static ssize_t environ_read(struct file *file, char __user *buf,
642 size_t count, loff_t *ppos)
643{
644 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
645 char *page;
646 unsigned long src = *ppos;
647 int ret = -ESRCH;
648 struct mm_struct *mm;
649
650 if (!task)
651 goto out_no_task;
652
653 if (!ptrace_may_attach(task))
654 goto out;
655
656 ret = -ENOMEM;
657 page = (char *)__get_free_page(GFP_TEMPORARY);
658 if (!page)
659 goto out;
660
661 ret = 0;
662
663 mm = get_task_mm(task);
664 if (!mm)
665 goto out_free;
666
667 while (count > 0) {
668 int this_len, retval, max_len;
669
670 this_len = mm->env_end - (mm->env_start + src);
671
672 if (this_len <= 0)
673 break;
674
675 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
676 this_len = (this_len > max_len) ? max_len : this_len;
677
678 retval = access_process_vm(task, (mm->env_start + src),
679 page, this_len, 0);
680
681 if (retval <= 0) {
682 ret = retval;
683 break;
684 }
685
686 if (copy_to_user(buf, page, retval)) {
687 ret = -EFAULT;
688 break;
689 }
690
691 ret += retval;
692 src += retval;
693 buf += retval;
694 count -= retval;
695 }
696 *ppos = src;
697
698 mmput(mm);
699out_free:
700 free_page((unsigned long) page);
701out:
702 put_task_struct(task);
703out_no_task:
704 return ret;
705}
706
707static const struct file_operations proc_environ_operations = {
708 .read = environ_read,
709};
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static ssize_t oom_adjust_read(struct file *file, char __user *buf,
712 size_t count, loff_t *ppos)
713{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800714 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700715 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700717 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Eric W. Biederman99f89552006-06-26 00:25:55 -0700719 if (!task)
720 return -ESRCH;
721 oom_adjust = task->oomkilladj;
722 put_task_struct(task);
723
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700724 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700725
726 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
730 size_t count, loff_t *ppos)
731{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700732 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700733 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int oom_adjust;
735
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700736 memset(buffer, 0, sizeof(buffer));
737 if (count > sizeof(buffer) - 1)
738 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (copy_from_user(buffer, buf, count))
740 return -EFAULT;
741 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700742 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
743 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -EINVAL;
745 if (*end == '\n')
746 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800747 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700748 if (!task)
749 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800750 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
751 put_task_struct(task);
752 return -EACCES;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700755 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (end - buffer == 0)
757 return -EIO;
758 return end - buffer;
759}
760
Arjan van de Ven00977a52007-02-12 00:55:34 -0800761static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 .read = oom_adjust_read,
763 .write = oom_adjust_write,
764};
765
David Rientjes4b8df892007-05-08 20:27:01 -0700766#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -0700767static ssize_t clear_refs_write(struct file *file, const char __user *buf,
768 size_t count, loff_t *ppos)
769{
770 struct task_struct *task;
771 char buffer[PROC_NUMBUF], *end;
772 struct mm_struct *mm;
773
774 memset(buffer, 0, sizeof(buffer));
775 if (count > sizeof(buffer) - 1)
776 count = sizeof(buffer) - 1;
777 if (copy_from_user(buffer, buf, count))
778 return -EFAULT;
779 if (!simple_strtol(buffer, &end, 0))
780 return -EINVAL;
781 if (*end == '\n')
782 end++;
783 task = get_proc_task(file->f_path.dentry->d_inode);
784 if (!task)
785 return -ESRCH;
786 mm = get_task_mm(task);
787 if (mm) {
788 clear_refs_smap(mm);
789 mmput(mm);
790 }
791 put_task_struct(task);
792 if (end - buffer == 0)
793 return -EIO;
794 return end - buffer;
795}
796
797static struct file_operations proc_clear_refs_operations = {
798 .write = clear_refs_write,
799};
David Rientjes4b8df892007-05-08 20:27:01 -0700800#endif
David Rientjesb813e932007-05-06 14:49:24 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#ifdef CONFIG_AUDITSYSCALL
803#define TMPBUFLEN 21
804static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
805 size_t count, loff_t *ppos)
806{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800807 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700808 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 ssize_t length;
810 char tmpbuf[TMPBUFLEN];
811
Eric W. Biederman99f89552006-06-26 00:25:55 -0700812 if (!task)
813 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
815 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700816 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
818}
819
820static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
821 size_t count, loff_t *ppos)
822{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800823 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 char *page, *tmp;
825 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 uid_t loginuid;
827
828 if (!capable(CAP_AUDIT_CONTROL))
829 return -EPERM;
830
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700831 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return -EPERM;
833
Al Viroe0182902006-05-18 08:28:02 -0400834 if (count >= PAGE_SIZE)
835 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (*ppos != 0) {
838 /* No partial writes. */
839 return -EINVAL;
840 }
Mel Gormane12ba742007-10-16 01:25:52 -0700841 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (!page)
843 return -ENOMEM;
844 length = -EFAULT;
845 if (copy_from_user(page, buf, count))
846 goto out_free_page;
847
Al Viroe0182902006-05-18 08:28:02 -0400848 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 loginuid = simple_strtoul(page, &tmp, 10);
850 if (tmp == page) {
851 length = -EINVAL;
852 goto out_free_page;
853
854 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700855 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (likely(length == 0))
857 length = count;
858
859out_free_page:
860 free_page((unsigned long) page);
861 return length;
862}
863
Arjan van de Ven00977a52007-02-12 00:55:34 -0800864static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 .read = proc_loginuid_read,
866 .write = proc_loginuid_write,
867};
868#endif
869
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800870#ifdef CONFIG_FAULT_INJECTION
871static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
872 size_t count, loff_t *ppos)
873{
874 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
875 char buffer[PROC_NUMBUF];
876 size_t len;
877 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800878
879 if (!task)
880 return -ESRCH;
881 make_it_fail = task->make_it_fail;
882 put_task_struct(task);
883
884 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700885
886 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800887}
888
889static ssize_t proc_fault_inject_write(struct file * file,
890 const char __user * buf, size_t count, loff_t *ppos)
891{
892 struct task_struct *task;
893 char buffer[PROC_NUMBUF], *end;
894 int make_it_fail;
895
896 if (!capable(CAP_SYS_RESOURCE))
897 return -EPERM;
898 memset(buffer, 0, sizeof(buffer));
899 if (count > sizeof(buffer) - 1)
900 count = sizeof(buffer) - 1;
901 if (copy_from_user(buffer, buf, count))
902 return -EFAULT;
903 make_it_fail = simple_strtol(buffer, &end, 0);
904 if (*end == '\n')
905 end++;
906 task = get_proc_task(file->f_dentry->d_inode);
907 if (!task)
908 return -ESRCH;
909 task->make_it_fail = make_it_fail;
910 put_task_struct(task);
911 if (end - buffer == 0)
912 return -EIO;
913 return end - buffer;
914}
915
Arjan van de Ven00977a52007-02-12 00:55:34 -0800916static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800917 .read = proc_fault_inject_read,
918 .write = proc_fault_inject_write,
919};
920#endif
921
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200922#ifdef CONFIG_SCHED_DEBUG
923/*
924 * Print out various scheduling related per-task fields:
925 */
926static int sched_show(struct seq_file *m, void *v)
927{
928 struct inode *inode = m->private;
929 struct task_struct *p;
930
931 WARN_ON(!inode);
932
933 p = get_proc_task(inode);
934 if (!p)
935 return -ESRCH;
936 proc_sched_show_task(p, m);
937
938 put_task_struct(p);
939
940 return 0;
941}
942
943static ssize_t
944sched_write(struct file *file, const char __user *buf,
945 size_t count, loff_t *offset)
946{
947 struct inode *inode = file->f_path.dentry->d_inode;
948 struct task_struct *p;
949
950 WARN_ON(!inode);
951
952 p = get_proc_task(inode);
953 if (!p)
954 return -ESRCH;
955 proc_sched_set_task(p);
956
957 put_task_struct(p);
958
959 return count;
960}
961
962static int sched_open(struct inode *inode, struct file *filp)
963{
964 int ret;
965
966 ret = single_open(filp, sched_show, NULL);
967 if (!ret) {
968 struct seq_file *m = filp->private_data;
969
970 m->private = inode;
971 }
972 return ret;
973}
974
975static const struct file_operations proc_pid_sched_operations = {
976 .open = sched_open,
977 .read = seq_read,
978 .write = sched_write,
979 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -0700980 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200981};
982
983#endif
984
Al Viro008b1502005-08-20 00:17:39 +0100985static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct inode *inode = dentry->d_inode;
988 int error = -EACCES;
989
990 /* We don't need a base pointer in the /proc filesystem */
991 path_release(nd);
992
Eric W. Biederman778c1142006-06-26 00:25:58 -0700993 /* Are we allowed to snoop on the tasks file descriptors? */
994 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
998 nd->last_type = LAST_BIND;
999out:
Al Viro008b1502005-08-20 00:17:39 +01001000 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1004 char __user *buffer, int buflen)
1005{
1006 struct inode * inode;
Mel Gormane12ba742007-10-16 01:25:52 -07001007 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1008 char *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int len;
1010
1011 if (!tmp)
1012 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 inode = dentry->d_inode;
1015 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1016 len = PTR_ERR(path);
1017 if (IS_ERR(path))
1018 goto out;
1019 len = tmp + PAGE_SIZE - 1 - path;
1020
1021 if (len > buflen)
1022 len = buflen;
1023 if (copy_to_user(buffer, path, len))
1024 len = -EFAULT;
1025 out:
1026 free_page((unsigned long)tmp);
1027 return len;
1028}
1029
1030static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1031{
1032 int error = -EACCES;
1033 struct inode *inode = dentry->d_inode;
1034 struct dentry *de;
1035 struct vfsmount *mnt = NULL;
1036
Eric W. Biederman778c1142006-06-26 00:25:58 -07001037 /* Are we allowed to snoop on the tasks file descriptors? */
1038 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1042 if (error)
1043 goto out;
1044
1045 error = do_proc_readlink(de, mnt, buffer, buflen);
1046 dput(de);
1047 mntput(mnt);
1048out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return error;
1050}
1051
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001052static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001054 .follow_link = proc_pid_follow_link,
1055 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056};
1057
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001058
1059/* building an inode */
1060
1061static int task_dumpable(struct task_struct *task)
1062{
1063 int dumpable = 0;
1064 struct mm_struct *mm;
1065
1066 task_lock(task);
1067 mm = task->mm;
1068 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001069 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001070 task_unlock(task);
1071 if(dumpable == 1)
1072 return 1;
1073 return 0;
1074}
1075
1076
Eric W. Biederman61a28782006-10-02 02:18:49 -07001077static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001078{
1079 struct inode * inode;
1080 struct proc_inode *ei;
1081
1082 /* We need a new inode */
1083
1084 inode = new_inode(sb);
1085 if (!inode)
1086 goto out;
1087
1088 /* Common stuff */
1089 ei = PROC_I(inode);
1090 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001091 inode->i_op = &proc_def_inode_operations;
1092
1093 /*
1094 * grab the reference to task.
1095 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001096 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001097 if (!ei->pid)
1098 goto out_unlock;
1099
1100 inode->i_uid = 0;
1101 inode->i_gid = 0;
1102 if (task_dumpable(task)) {
1103 inode->i_uid = task->euid;
1104 inode->i_gid = task->egid;
1105 }
1106 security_task_to_inode(task, inode);
1107
1108out:
1109 return inode;
1110
1111out_unlock:
1112 iput(inode);
1113 return NULL;
1114}
1115
1116static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1117{
1118 struct inode *inode = dentry->d_inode;
1119 struct task_struct *task;
1120 generic_fillattr(inode, stat);
1121
1122 rcu_read_lock();
1123 stat->uid = 0;
1124 stat->gid = 0;
1125 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1126 if (task) {
1127 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1128 task_dumpable(task)) {
1129 stat->uid = task->euid;
1130 stat->gid = task->egid;
1131 }
1132 }
1133 rcu_read_unlock();
1134 return 0;
1135}
1136
1137/* dentry stuff */
1138
1139/*
1140 * Exceptional case: normally we are not allowed to unhash a busy
1141 * directory. In this case, however, we can do it - no aliasing problems
1142 * due to the way we treat inodes.
1143 *
1144 * Rewrite the inode's ownerships here because the owning task may have
1145 * performed a setuid(), etc.
1146 *
1147 * Before the /proc/pid/status file was created the only way to read
1148 * the effective uid of a /process was to stat /proc/pid. Reading
1149 * /proc/pid/status is slow enough that procps and other packages
1150 * kept stating /proc/pid. To keep the rules in /proc simple I have
1151 * made this apply to all per process world readable and executable
1152 * directories.
1153 */
1154static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1155{
1156 struct inode *inode = dentry->d_inode;
1157 struct task_struct *task = get_proc_task(inode);
1158 if (task) {
1159 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1160 task_dumpable(task)) {
1161 inode->i_uid = task->euid;
1162 inode->i_gid = task->egid;
1163 } else {
1164 inode->i_uid = 0;
1165 inode->i_gid = 0;
1166 }
1167 inode->i_mode &= ~(S_ISUID | S_ISGID);
1168 security_task_to_inode(task, inode);
1169 put_task_struct(task);
1170 return 1;
1171 }
1172 d_drop(dentry);
1173 return 0;
1174}
1175
1176static int pid_delete_dentry(struct dentry * dentry)
1177{
1178 /* Is the task we represent dead?
1179 * If so, then don't put the dentry on the lru list,
1180 * kill it immediately.
1181 */
1182 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1183}
1184
1185static struct dentry_operations pid_dentry_operations =
1186{
1187 .d_revalidate = pid_revalidate,
1188 .d_delete = pid_delete_dentry,
1189};
1190
1191/* Lookups */
1192
Eric Dumazetc5141e62007-05-08 00:26:15 -07001193typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1194 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001195
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001196/*
1197 * Fill a directory entry.
1198 *
1199 * If possible create the dcache entry and derive our inode number and
1200 * file type from dcache entry.
1201 *
1202 * Since all of the proc inode numbers are dynamically generated, the inode
1203 * numbers do not exist until the inode is cache. This means creating the
1204 * the dcache entry in readdir is necessary to keep the inode numbers
1205 * reported by readdir in sync with the inode numbers reported
1206 * by stat.
1207 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001208static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1209 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001210 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001211{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001212 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001213 struct inode *inode;
1214 struct qstr qname;
1215 ino_t ino = 0;
1216 unsigned type = DT_UNKNOWN;
1217
1218 qname.name = name;
1219 qname.len = len;
1220 qname.hash = full_name_hash(name, len);
1221
1222 child = d_lookup(dir, &qname);
1223 if (!child) {
1224 struct dentry *new;
1225 new = d_alloc(dir, &qname);
1226 if (new) {
1227 child = instantiate(dir->d_inode, new, task, ptr);
1228 if (child)
1229 dput(new);
1230 else
1231 child = new;
1232 }
1233 }
1234 if (!child || IS_ERR(child) || !child->d_inode)
1235 goto end_instantiate;
1236 inode = child->d_inode;
1237 if (inode) {
1238 ino = inode->i_ino;
1239 type = inode->i_mode >> 12;
1240 }
1241 dput(child);
1242end_instantiate:
1243 if (!ino)
1244 ino = find_inode_number(dir, &qname);
1245 if (!ino)
1246 ino = 1;
1247 return filldir(dirent, name, len, filp->f_pos, ino, type);
1248}
1249
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001250static unsigned name_to_int(struct dentry *dentry)
1251{
1252 const char *name = dentry->d_name.name;
1253 int len = dentry->d_name.len;
1254 unsigned n = 0;
1255
1256 if (len > 1 && *name == '0')
1257 goto out;
1258 while (len-- > 0) {
1259 unsigned c = *name++ - '0';
1260 if (c > 9)
1261 goto out;
1262 if (n >= (~0U-9)/10)
1263 goto out;
1264 n *= 10;
1265 n += c;
1266 }
1267 return n;
1268out:
1269 return ~0U;
1270}
1271
Miklos Szeredi27932742007-05-08 00:26:17 -07001272#define PROC_FDINFO_MAX 64
1273
1274static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1275 struct vfsmount **mnt, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001276{
1277 struct task_struct *task = get_proc_task(inode);
1278 struct files_struct *files = NULL;
1279 struct file *file;
1280 int fd = proc_fd(inode);
1281
1282 if (task) {
1283 files = get_files_struct(task);
1284 put_task_struct(task);
1285 }
1286 if (files) {
1287 /*
1288 * We are not taking a ref to the file structure, so we must
1289 * hold ->file_lock.
1290 */
1291 spin_lock(&files->file_lock);
1292 file = fcheck_files(files, fd);
1293 if (file) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001294 if (mnt)
1295 *mnt = mntget(file->f_path.mnt);
1296 if (dentry)
1297 *dentry = dget(file->f_path.dentry);
1298 if (info)
1299 snprintf(info, PROC_FDINFO_MAX,
1300 "pos:\t%lli\n"
1301 "flags:\t0%o\n",
1302 (long long) file->f_pos,
1303 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001304 spin_unlock(&files->file_lock);
1305 put_files_struct(files);
1306 return 0;
1307 }
1308 spin_unlock(&files->file_lock);
1309 put_files_struct(files);
1310 }
1311 return -ENOENT;
1312}
1313
Miklos Szeredi27932742007-05-08 00:26:17 -07001314static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1315 struct vfsmount **mnt)
1316{
1317 return proc_fd_info(inode, dentry, mnt, NULL);
1318}
1319
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001320static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1321{
1322 struct inode *inode = dentry->d_inode;
1323 struct task_struct *task = get_proc_task(inode);
1324 int fd = proc_fd(inode);
1325 struct files_struct *files;
1326
1327 if (task) {
1328 files = get_files_struct(task);
1329 if (files) {
1330 rcu_read_lock();
1331 if (fcheck_files(files, fd)) {
1332 rcu_read_unlock();
1333 put_files_struct(files);
1334 if (task_dumpable(task)) {
1335 inode->i_uid = task->euid;
1336 inode->i_gid = task->egid;
1337 } else {
1338 inode->i_uid = 0;
1339 inode->i_gid = 0;
1340 }
1341 inode->i_mode &= ~(S_ISUID | S_ISGID);
1342 security_task_to_inode(task, inode);
1343 put_task_struct(task);
1344 return 1;
1345 }
1346 rcu_read_unlock();
1347 put_files_struct(files);
1348 }
1349 put_task_struct(task);
1350 }
1351 d_drop(dentry);
1352 return 0;
1353}
1354
1355static struct dentry_operations tid_fd_dentry_operations =
1356{
1357 .d_revalidate = tid_fd_revalidate,
1358 .d_delete = pid_delete_dentry,
1359};
1360
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001361static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001362 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001363{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001364 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001365 struct file *file;
1366 struct files_struct *files;
1367 struct inode *inode;
1368 struct proc_inode *ei;
1369 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001370
Eric W. Biederman61a28782006-10-02 02:18:49 -07001371 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001372 if (!inode)
1373 goto out;
1374 ei = PROC_I(inode);
1375 ei->fd = fd;
1376 files = get_files_struct(task);
1377 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001378 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001379 inode->i_mode = S_IFLNK;
1380
1381 /*
1382 * We are not taking a ref to the file structure, so we must
1383 * hold ->file_lock.
1384 */
1385 spin_lock(&files->file_lock);
1386 file = fcheck_files(files, fd);
1387 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001388 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001389 if (file->f_mode & 1)
1390 inode->i_mode |= S_IRUSR | S_IXUSR;
1391 if (file->f_mode & 2)
1392 inode->i_mode |= S_IWUSR | S_IXUSR;
1393 spin_unlock(&files->file_lock);
1394 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001395
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001396 inode->i_op = &proc_pid_link_inode_operations;
1397 inode->i_size = 64;
1398 ei->op.proc_get_link = proc_fd_link;
1399 dentry->d_op = &tid_fd_dentry_operations;
1400 d_add(dentry, inode);
1401 /* Close the race of the process dying before we return the dentry */
1402 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001403 error = NULL;
1404
1405 out:
1406 return error;
1407out_unlock:
1408 spin_unlock(&files->file_lock);
1409 put_files_struct(files);
1410out_iput:
1411 iput(inode);
1412 goto out;
1413}
1414
Miklos Szeredi27932742007-05-08 00:26:17 -07001415static struct dentry *proc_lookupfd_common(struct inode *dir,
1416 struct dentry *dentry,
1417 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001418{
1419 struct task_struct *task = get_proc_task(dir);
1420 unsigned fd = name_to_int(dentry);
1421 struct dentry *result = ERR_PTR(-ENOENT);
1422
1423 if (!task)
1424 goto out_no_task;
1425 if (fd == ~0U)
1426 goto out;
1427
Miklos Szeredi27932742007-05-08 00:26:17 -07001428 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001429out:
1430 put_task_struct(task);
1431out_no_task:
1432 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001433}
1434
Miklos Szeredi27932742007-05-08 00:26:17 -07001435static int proc_readfd_common(struct file * filp, void * dirent,
1436 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001438 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001439 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001440 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 unsigned int fd, tid, ino;
1442 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001444 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001447 if (!p)
1448 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 retval = 0;
1450 tid = p->pid;
1451
1452 fd = filp->f_pos;
1453 switch (fd) {
1454 case 0:
1455 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1456 goto out;
1457 filp->f_pos++;
1458 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001459 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1461 goto out;
1462 filp->f_pos++;
1463 default:
1464 files = get_files_struct(p);
1465 if (!files)
1466 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001467 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001468 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001470 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001472 char name[PROC_NUMBUF];
1473 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 if (!fcheck_files(files, fd))
1476 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001477 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Miklos Szeredi27932742007-05-08 00:26:17 -07001479 len = snprintf(name, sizeof(name), "%d", fd);
1480 if (proc_fill_cache(filp, dirent, filldir,
1481 name, len, instantiate,
1482 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001483 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 break;
1485 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001486 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001488 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 put_files_struct(files);
1490 }
1491out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001492 put_task_struct(p);
1493out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return retval;
1495}
1496
Miklos Szeredi27932742007-05-08 00:26:17 -07001497static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1498 struct nameidata *nd)
1499{
1500 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1501}
1502
1503static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1504{
1505 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1506}
1507
1508static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1509 size_t len, loff_t *ppos)
1510{
1511 char tmp[PROC_FDINFO_MAX];
1512 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1513 if (!err)
1514 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1515 return err;
1516}
1517
1518static const struct file_operations proc_fdinfo_file_operations = {
1519 .open = nonseekable_open,
1520 .read = proc_fdinfo_read,
1521};
1522
Arjan van de Ven00977a52007-02-12 00:55:34 -08001523static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 .read = generic_read_dir,
1525 .readdir = proc_readfd,
1526};
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001529 * /proc/pid/fd needs a special permission handler so that a process can still
1530 * access /proc/self/fd after it has executed a setuid().
1531 */
1532static int proc_fd_permission(struct inode *inode, int mask,
1533 struct nameidata *nd)
1534{
1535 int rv;
1536
1537 rv = generic_permission(inode, mask, NULL);
1538 if (rv == 0)
1539 return 0;
1540 if (task_pid(current) == proc_pid(inode))
1541 rv = 0;
1542 return rv;
1543}
1544
1545/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 * proc directories can do almost nothing..
1547 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001548static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001550 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001551 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552};
1553
Miklos Szeredi27932742007-05-08 00:26:17 -07001554static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1555 struct dentry *dentry, struct task_struct *task, const void *ptr)
1556{
1557 unsigned fd = *(unsigned *)ptr;
1558 struct inode *inode;
1559 struct proc_inode *ei;
1560 struct dentry *error = ERR_PTR(-ENOENT);
1561
1562 inode = proc_pid_make_inode(dir->i_sb, task);
1563 if (!inode)
1564 goto out;
1565 ei = PROC_I(inode);
1566 ei->fd = fd;
1567 inode->i_mode = S_IFREG | S_IRUSR;
1568 inode->i_fop = &proc_fdinfo_file_operations;
1569 dentry->d_op = &tid_fd_dentry_operations;
1570 d_add(dentry, inode);
1571 /* Close the race of the process dying before we return the dentry */
1572 if (tid_fd_revalidate(dentry, NULL))
1573 error = NULL;
1574
1575 out:
1576 return error;
1577}
1578
1579static struct dentry *proc_lookupfdinfo(struct inode *dir,
1580 struct dentry *dentry,
1581 struct nameidata *nd)
1582{
1583 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1584}
1585
1586static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1587{
1588 return proc_readfd_common(filp, dirent, filldir,
1589 proc_fdinfo_instantiate);
1590}
1591
1592static const struct file_operations proc_fdinfo_operations = {
1593 .read = generic_read_dir,
1594 .readdir = proc_readfdinfo,
1595};
1596
1597/*
1598 * proc directories can do almost nothing..
1599 */
1600static const struct inode_operations proc_fdinfo_inode_operations = {
1601 .lookup = proc_lookupfdinfo,
1602 .setattr = proc_setattr,
1603};
1604
1605
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001606static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001607 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001608{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001609 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001610 struct inode *inode;
1611 struct proc_inode *ei;
1612 struct dentry *error = ERR_PTR(-EINVAL);
1613
Eric W. Biederman61a28782006-10-02 02:18:49 -07001614 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001615 if (!inode)
1616 goto out;
1617
1618 ei = PROC_I(inode);
1619 inode->i_mode = p->mode;
1620 if (S_ISDIR(inode->i_mode))
1621 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1622 if (p->iop)
1623 inode->i_op = p->iop;
1624 if (p->fop)
1625 inode->i_fop = p->fop;
1626 ei->op = p->op;
1627 dentry->d_op = &pid_dentry_operations;
1628 d_add(dentry, inode);
1629 /* Close the race of the process dying before we return the dentry */
1630 if (pid_revalidate(dentry, NULL))
1631 error = NULL;
1632out:
1633 return error;
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636static struct dentry *proc_pident_lookup(struct inode *dir,
1637 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001638 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001639 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001642 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001643 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001644 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001646 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 inode = NULL;
1648
Eric W. Biederman99f89552006-06-26 00:25:55 -07001649 if (!task)
1650 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001652 /*
1653 * Yes, it does not scale. And it should not. Don't add
1654 * new entries into /proc/<tgid>/ without very good reasons.
1655 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001656 last = &ents[nents - 1];
1657 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (p->len != dentry->d_name.len)
1659 continue;
1660 if (!memcmp(dentry->d_name.name, p->name, p->len))
1661 break;
1662 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001663 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto out;
1665
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001666 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001668 put_task_struct(task);
1669out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001670 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Eric Dumazetc5141e62007-05-08 00:26:15 -07001673static int proc_pident_fill_cache(struct file *filp, void *dirent,
1674 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001675{
1676 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1677 proc_pident_instantiate, task, p);
1678}
1679
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001680static int proc_pident_readdir(struct file *filp,
1681 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001682 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001683{
1684 int i;
1685 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001686 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001687 struct inode *inode = dentry->d_inode;
1688 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001689 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001690 ino_t ino;
1691 int ret;
1692
1693 ret = -ENOENT;
1694 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001695 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001696
1697 ret = 0;
1698 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001699 i = filp->f_pos;
1700 switch (i) {
1701 case 0:
1702 ino = inode->i_ino;
1703 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1704 goto out;
1705 i++;
1706 filp->f_pos++;
1707 /* fall through */
1708 case 1:
1709 ino = parent_ino(dentry);
1710 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1711 goto out;
1712 i++;
1713 filp->f_pos++;
1714 /* fall through */
1715 default:
1716 i -= 2;
1717 if (i >= nents) {
1718 ret = 1;
1719 goto out;
1720 }
1721 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001722 last = &ents[nents - 1];
1723 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001724 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001725 goto out;
1726 filp->f_pos++;
1727 p++;
1728 }
1729 }
1730
1731 ret = 1;
1732out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001733 put_task_struct(task);
1734out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001735 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001739static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1740 size_t count, loff_t *ppos)
1741{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001742 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001743 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001744 ssize_t length;
1745 struct task_struct *task = get_proc_task(inode);
1746
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001747 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001748 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001749
1750 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001751 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001752 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001753 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001754 if (length > 0)
1755 length = simple_read_from_buffer(buf, count, ppos, p, length);
1756 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001757 return length;
1758}
1759
1760static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1761 size_t count, loff_t *ppos)
1762{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001763 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001764 char *page;
1765 ssize_t length;
1766 struct task_struct *task = get_proc_task(inode);
1767
1768 length = -ESRCH;
1769 if (!task)
1770 goto out_no_task;
1771 if (count > PAGE_SIZE)
1772 count = PAGE_SIZE;
1773
1774 /* No partial writes. */
1775 length = -EINVAL;
1776 if (*ppos != 0)
1777 goto out;
1778
1779 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001780 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781 if (!page)
1782 goto out;
1783
1784 length = -EFAULT;
1785 if (copy_from_user(page, buf, count))
1786 goto out_free;
1787
1788 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001789 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001790 (void*)page, count);
1791out_free:
1792 free_page((unsigned long) page);
1793out:
1794 put_task_struct(task);
1795out_no_task:
1796 return length;
1797}
1798
Arjan van de Ven00977a52007-02-12 00:55:34 -08001799static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001800 .read = proc_pid_attr_read,
1801 .write = proc_pid_attr_write,
1802};
1803
Eric Dumazetc5141e62007-05-08 00:26:15 -07001804static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001805 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1806 REG("prev", S_IRUGO, pid_attr),
1807 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1808 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1809 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1810 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001811};
1812
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001813static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 void * dirent, filldir_t filldir)
1815{
1816 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001817 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Arjan van de Ven00977a52007-02-12 00:55:34 -08001820static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001822 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823};
1824
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001825static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 struct dentry *dentry, struct nameidata *nd)
1827{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001828 return proc_pident_lookup(dir, dentry,
1829 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001832static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001833 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001834 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001835 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836};
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838#endif
1839
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07001840#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1841static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
1842 size_t count, loff_t *ppos)
1843{
1844 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1845 struct mm_struct *mm;
1846 char buffer[PROC_NUMBUF];
1847 size_t len;
1848 int ret;
1849
1850 if (!task)
1851 return -ESRCH;
1852
1853 ret = 0;
1854 mm = get_task_mm(task);
1855 if (mm) {
1856 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
1857 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
1858 MMF_DUMP_FILTER_SHIFT));
1859 mmput(mm);
1860 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
1861 }
1862
1863 put_task_struct(task);
1864
1865 return ret;
1866}
1867
1868static ssize_t proc_coredump_filter_write(struct file *file,
1869 const char __user *buf,
1870 size_t count,
1871 loff_t *ppos)
1872{
1873 struct task_struct *task;
1874 struct mm_struct *mm;
1875 char buffer[PROC_NUMBUF], *end;
1876 unsigned int val;
1877 int ret;
1878 int i;
1879 unsigned long mask;
1880
1881 ret = -EFAULT;
1882 memset(buffer, 0, sizeof(buffer));
1883 if (count > sizeof(buffer) - 1)
1884 count = sizeof(buffer) - 1;
1885 if (copy_from_user(buffer, buf, count))
1886 goto out_no_task;
1887
1888 ret = -EINVAL;
1889 val = (unsigned int)simple_strtoul(buffer, &end, 0);
1890 if (*end == '\n')
1891 end++;
1892 if (end - buffer == 0)
1893 goto out_no_task;
1894
1895 ret = -ESRCH;
1896 task = get_proc_task(file->f_dentry->d_inode);
1897 if (!task)
1898 goto out_no_task;
1899
1900 ret = end - buffer;
1901 mm = get_task_mm(task);
1902 if (!mm)
1903 goto out_no_mm;
1904
1905 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
1906 if (val & mask)
1907 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1908 else
1909 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1910 }
1911
1912 mmput(mm);
1913 out_no_mm:
1914 put_task_struct(task);
1915 out_no_task:
1916 return ret;
1917}
1918
1919static const struct file_operations proc_coredump_filter_operations = {
1920 .read = proc_coredump_filter_read,
1921 .write = proc_coredump_filter_write,
1922};
1923#endif
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925/*
1926 * /proc/self:
1927 */
1928static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1929 int buflen)
1930{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001931 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 sprintf(tmp, "%d", current->tgid);
1933 return vfs_readlink(dentry,buffer,buflen,tmp);
1934}
1935
Al Viro008b1502005-08-20 00:17:39 +01001936static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001938 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001940 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001943static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 .readlink = proc_self_readlink,
1945 .follow_link = proc_self_follow_link,
1946};
1947
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001948/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001949 * proc base
1950 *
1951 * These are the directory entries in the root directory of /proc
1952 * that properly belong to the /proc filesystem, as they describe
1953 * describe something that is process related.
1954 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07001955static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001956 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001957 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001958};
1959
1960/*
1961 * Exceptional case: normally we are not allowed to unhash a busy
1962 * directory. In this case, however, we can do it - no aliasing problems
1963 * due to the way we treat inodes.
1964 */
1965static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1966{
1967 struct inode *inode = dentry->d_inode;
1968 struct task_struct *task = get_proc_task(inode);
1969 if (task) {
1970 put_task_struct(task);
1971 return 1;
1972 }
1973 d_drop(dentry);
1974 return 0;
1975}
1976
1977static struct dentry_operations proc_base_dentry_operations =
1978{
1979 .d_revalidate = proc_base_revalidate,
1980 .d_delete = pid_delete_dentry,
1981};
1982
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001983static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001984 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001985{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001986 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001987 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001988 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001989 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001990
1991 /* Allocate the inode */
1992 error = ERR_PTR(-ENOMEM);
1993 inode = new_inode(dir->i_sb);
1994 if (!inode)
1995 goto out;
1996
1997 /* Initialize the inode */
1998 ei = PROC_I(inode);
1999 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002000
2001 /*
2002 * grab the reference to the task.
2003 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002004 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002005 if (!ei->pid)
2006 goto out_iput;
2007
2008 inode->i_uid = 0;
2009 inode->i_gid = 0;
2010 inode->i_mode = p->mode;
2011 if (S_ISDIR(inode->i_mode))
2012 inode->i_nlink = 2;
2013 if (S_ISLNK(inode->i_mode))
2014 inode->i_size = 64;
2015 if (p->iop)
2016 inode->i_op = p->iop;
2017 if (p->fop)
2018 inode->i_fop = p->fop;
2019 ei->op = p->op;
2020 dentry->d_op = &proc_base_dentry_operations;
2021 d_add(dentry, inode);
2022 error = NULL;
2023out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002024 return error;
2025out_iput:
2026 iput(inode);
2027 goto out;
2028}
2029
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002030static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2031{
2032 struct dentry *error;
2033 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002034 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002035
2036 error = ERR_PTR(-ENOENT);
2037
2038 if (!task)
2039 goto out_no_task;
2040
2041 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002042 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2043 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002044 if (p->len != dentry->d_name.len)
2045 continue;
2046 if (!memcmp(dentry->d_name.name, p->name, p->len))
2047 break;
2048 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002049 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002050 goto out;
2051
2052 error = proc_base_instantiate(dir, dentry, task, p);
2053
2054out:
2055 put_task_struct(task);
2056out_no_task:
2057 return error;
2058}
2059
Eric Dumazetc5141e62007-05-08 00:26:15 -07002060static int proc_base_fill_cache(struct file *filp, void *dirent,
2061 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002062{
2063 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2064 proc_base_instantiate, task, p);
2065}
2066
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002067#ifdef CONFIG_TASK_IO_ACCOUNTING
2068static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2069{
2070 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002071#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002072 "rchar: %llu\n"
2073 "wchar: %llu\n"
2074 "syscr: %llu\n"
2075 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002076#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002077 "read_bytes: %llu\n"
2078 "write_bytes: %llu\n"
2079 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002080#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002081 (unsigned long long)task->rchar,
2082 (unsigned long long)task->wchar,
2083 (unsigned long long)task->syscr,
2084 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002085#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002086 (unsigned long long)task->ioac.read_bytes,
2087 (unsigned long long)task->ioac.write_bytes,
2088 (unsigned long long)task->ioac.cancelled_write_bytes);
2089}
2090#endif
2091
Eric W. Biederman801199c2006-10-02 02:18:48 -07002092/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002093 * Thread groups
2094 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002095static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002096static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002097
Eric Dumazetc5141e62007-05-08 00:26:15 -07002098static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002099 DIR("task", S_IRUGO|S_IXUGO, task),
2100 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002101 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002102 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002103 INF("auxv", S_IRUSR, pid_auxv),
2104 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002105#ifdef CONFIG_SCHED_DEBUG
2106 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2107#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002108 INF("cmdline", S_IRUGO, pid_cmdline),
2109 INF("stat", S_IRUGO, tgid_stat),
2110 INF("statm", S_IRUGO, pid_statm),
2111 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002112#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002113 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002114#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002115 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002116 LNK("cwd", cwd),
2117 LNK("root", root),
2118 LNK("exe", exe),
2119 REG("mounts", S_IRUGO, mounts),
2120 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002121#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002122 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002123 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002124#endif
2125#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002126 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002127#endif
2128#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002129 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002130#endif
2131#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002132 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002133#endif
Paul Menage8793d852007-10-18 23:39:39 -07002134#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002135 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002136#endif
Paul Menagea4243162007-10-18 23:39:35 -07002137#ifdef CONFIG_CGROUPS
2138 REG("cgroup", S_IRUGO, cgroup),
2139#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002140 INF("oom_score", S_IRUGO, oom_score),
2141 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002142#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002143 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002144#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002145#ifdef CONFIG_FAULT_INJECTION
2146 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2147#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002148#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2149 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2150#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002151#ifdef CONFIG_TASK_IO_ACCOUNTING
2152 INF("io", S_IRUGO, pid_io_accounting),
2153#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002154};
2155
2156static int proc_tgid_base_readdir(struct file * filp,
2157 void * dirent, filldir_t filldir)
2158{
2159 return proc_pident_readdir(filp,dirent,filldir,
2160 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2161}
2162
Arjan van de Ven00977a52007-02-12 00:55:34 -08002163static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002164 .read = generic_read_dir,
2165 .readdir = proc_tgid_base_readdir,
2166};
2167
2168static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002169 return proc_pident_lookup(dir, dentry,
2170 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002171}
2172
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002173static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002174 .lookup = proc_tgid_base_lookup,
2175 .getattr = pid_getattr,
2176 .setattr = proc_setattr,
2177};
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07002180 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002182 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002184 * Looks in the dcache for
2185 * /proc/@pid
2186 * /proc/@tgid/task/@pid
2187 * if either directory is present flushes it and all of it'ts children
2188 * from the dcache.
2189 *
2190 * It is safe and reasonable to cache /proc entries for a task until
2191 * that task exits. After that they just clog up the dcache with
2192 * useless entries, possibly causing useful dcache entries to be
2193 * flushed instead. This routine is proved to flush those useless
2194 * dcache entries at process exit time.
2195 *
2196 * NOTE: This routine is just an optimization so it does not guarantee
2197 * that no dcache entries will exist at process exit time it
2198 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07002200void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002202 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002203 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002204 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Eric W. Biederman48e64842006-06-26 00:25:48 -07002206 name.name = buf;
2207 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2208 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2209 if (dentry) {
2210 shrink_dcache_parent(dentry);
2211 d_drop(dentry);
2212 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Eric W. Biederman48e64842006-06-26 00:25:48 -07002215 if (thread_group_leader(task))
2216 goto out;
2217
2218 name.name = buf;
2219 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2220 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2221 if (!leader)
2222 goto out;
2223
2224 name.name = "task";
2225 name.len = strlen(name.name);
2226 dir = d_hash_and_lookup(leader, &name);
2227 if (!dir)
2228 goto out_put_leader;
2229
2230 name.name = buf;
2231 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2232 dentry = d_hash_and_lookup(dir, &name);
2233 if (dentry) {
2234 shrink_dcache_parent(dentry);
2235 d_drop(dentry);
2236 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002238
2239 dput(dir);
2240out_put_leader:
2241 dput(leader);
2242out:
2243 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Adrian Bunk9711ef92006-12-06 20:38:31 -08002246static struct dentry *proc_pid_instantiate(struct inode *dir,
2247 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002248 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002249{
2250 struct dentry *error = ERR_PTR(-ENOENT);
2251 struct inode *inode;
2252
Eric W. Biederman61a28782006-10-02 02:18:49 -07002253 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002254 if (!inode)
2255 goto out;
2256
2257 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2258 inode->i_op = &proc_tgid_base_inode_operations;
2259 inode->i_fop = &proc_tgid_base_operations;
2260 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002261 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002262#ifdef CONFIG_SECURITY
2263 inode->i_nlink += 1;
2264#endif
2265
2266 dentry->d_op = &pid_dentry_operations;
2267
2268 d_add(dentry, inode);
2269 /* Close the race of the process dying before we return the dentry */
2270 if (pid_revalidate(dentry, NULL))
2271 error = NULL;
2272out:
2273 return error;
2274}
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2277{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002278 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Eric W. Biederman801199c2006-10-02 02:18:48 -07002282 result = proc_base_lookup(dir, dentry);
2283 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2284 goto out;
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 tgid = name_to_int(dentry);
2287 if (tgid == ~0U)
2288 goto out;
2289
Eric W. Biedermande758732006-06-26 00:25:51 -07002290 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 task = find_task_by_pid(tgid);
2292 if (task)
2293 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002294 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (!task)
2296 goto out;
2297
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002298 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002299 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002301 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002305 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002308static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002310 struct task_struct *task;
2311 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002313 rcu_read_lock();
2314retry:
2315 task = NULL;
2316 pid = find_ge_pid(tgid);
2317 if (pid) {
2318 tgid = pid->nr + 1;
2319 task = pid_task(pid, PIDTYPE_PID);
2320 /* What we to know is if the pid we have find is the
2321 * pid of a thread_group_leader. Testing for task
2322 * being a thread_group_leader is the obvious thing
2323 * todo but there is a window when it fails, due to
2324 * the pid transfer logic in de_thread.
2325 *
2326 * So we perform the straight forward test of seeing
2327 * if the pid we have found is the pid of a thread
2328 * group leader, and don't worry if the task we have
2329 * found doesn't happen to be a thread group leader.
2330 * As we don't care in the case of readdir.
2331 */
2332 if (!task || !has_group_leader_pid(task))
2333 goto retry;
2334 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002336 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002337 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002340#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Eric W. Biederman61a28782006-10-02 02:18:49 -07002342static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2343 struct task_struct *task, int tgid)
2344{
2345 char name[PROC_NUMBUF];
2346 int len = snprintf(name, sizeof(name), "%d", tgid);
2347 return proc_fill_cache(filp, dirent, filldir, name, len,
2348 proc_pid_instantiate, task, NULL);
2349}
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351/* for the /proc/ directory itself, after non-process stuff has been done */
2352int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002355 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002356 struct task_struct *task;
2357 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Eric W. Biederman61a28782006-10-02 02:18:49 -07002359 if (!reaper)
2360 goto out_no_task;
2361
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002362 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002363 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002364 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002368 tgid = filp->f_pos - TGID_OFFSET;
2369 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002370 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002371 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002372 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002373 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002374 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002375 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002379 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2380out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002381 put_task_struct(reaper);
2382out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return 0;
2384}
2385
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002386/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002387 * Tasks
2388 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002389static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002390 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002391 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002392 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002393 INF("auxv", S_IRUSR, pid_auxv),
2394 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002395#ifdef CONFIG_SCHED_DEBUG
2396 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2397#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002398 INF("cmdline", S_IRUGO, pid_cmdline),
2399 INF("stat", S_IRUGO, tid_stat),
2400 INF("statm", S_IRUGO, pid_statm),
2401 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002402#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002403 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002404#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002405 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002406 LNK("cwd", cwd),
2407 LNK("root", root),
2408 LNK("exe", exe),
2409 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002410#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002411 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002412 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002413#endif
2414#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002415 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002416#endif
2417#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002418 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002419#endif
2420#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002421 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002422#endif
Paul Menage8793d852007-10-18 23:39:39 -07002423#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002424 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002425#endif
Paul Menagea4243162007-10-18 23:39:35 -07002426#ifdef CONFIG_CGROUPS
2427 REG("cgroup", S_IRUGO, cgroup),
2428#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002429 INF("oom_score", S_IRUGO, oom_score),
2430 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002431#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002432 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002433#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002434#ifdef CONFIG_FAULT_INJECTION
2435 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2436#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002437};
2438
2439static int proc_tid_base_readdir(struct file * filp,
2440 void * dirent, filldir_t filldir)
2441{
2442 return proc_pident_readdir(filp,dirent,filldir,
2443 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2444}
2445
2446static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002447 return proc_pident_lookup(dir, dentry,
2448 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002449}
2450
Arjan van de Ven00977a52007-02-12 00:55:34 -08002451static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002452 .read = generic_read_dir,
2453 .readdir = proc_tid_base_readdir,
2454};
2455
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002456static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002457 .lookup = proc_tid_base_lookup,
2458 .getattr = pid_getattr,
2459 .setattr = proc_setattr,
2460};
2461
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002462static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002463 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002464{
2465 struct dentry *error = ERR_PTR(-ENOENT);
2466 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002467 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002468
2469 if (!inode)
2470 goto out;
2471 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2472 inode->i_op = &proc_tid_base_inode_operations;
2473 inode->i_fop = &proc_tid_base_operations;
2474 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002475 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002476#ifdef CONFIG_SECURITY
2477 inode->i_nlink += 1;
2478#endif
2479
2480 dentry->d_op = &pid_dentry_operations;
2481
2482 d_add(dentry, inode);
2483 /* Close the race of the process dying before we return the dentry */
2484 if (pid_revalidate(dentry, NULL))
2485 error = NULL;
2486out:
2487 return error;
2488}
2489
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2491{
2492 struct dentry *result = ERR_PTR(-ENOENT);
2493 struct task_struct *task;
2494 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002495 unsigned tid;
2496
2497 if (!leader)
2498 goto out_no_task;
2499
2500 tid = name_to_int(dentry);
2501 if (tid == ~0U)
2502 goto out;
2503
2504 rcu_read_lock();
2505 task = find_task_by_pid(tid);
2506 if (task)
2507 get_task_struct(task);
2508 rcu_read_unlock();
2509 if (!task)
2510 goto out;
2511 if (leader->tgid != task->tgid)
2512 goto out_drop_task;
2513
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002514 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002515out_drop_task:
2516 put_task_struct(task);
2517out:
2518 put_task_struct(leader);
2519out_no_task:
2520 return result;
2521}
2522
2523/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002524 * Find the first tid of a thread group to return to user space.
2525 *
2526 * Usually this is just the thread group leader, but if the users
2527 * buffer was too small or there was a seek into the middle of the
2528 * directory we have more work todo.
2529 *
2530 * In the case of a short read we start with find_task_by_pid.
2531 *
2532 * In the case of a seek we start with the leader and walk nr
2533 * threads past it.
2534 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002535static struct task_struct *first_tid(struct task_struct *leader,
2536 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002537{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002538 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002539
Eric W. Biedermancc288732006-06-26 00:26:01 -07002540 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002541 /* Attempt to start with the pid of a thread */
2542 if (tid && (nr > 0)) {
2543 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002544 if (pos && (pos->group_leader == leader))
2545 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002546 }
2547
2548 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002549 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002550 if (nr && nr >= get_nr_threads(leader))
2551 goto out;
2552
2553 /* If we haven't found our starting place yet start
2554 * with the leader and walk nr threads forward.
2555 */
2556 for (pos = leader; nr > 0; --nr) {
2557 pos = next_thread(pos);
2558 if (pos == leader) {
2559 pos = NULL;
2560 goto out;
2561 }
2562 }
2563found:
2564 get_task_struct(pos);
2565out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002566 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002567 return pos;
2568}
2569
2570/*
2571 * Find the next thread in the thread list.
2572 * Return NULL if there is an error or no next thread.
2573 *
2574 * The reference to the input task_struct is released.
2575 */
2576static struct task_struct *next_tid(struct task_struct *start)
2577{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002578 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002579 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002580 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002581 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002582 if (thread_group_leader(pos))
2583 pos = NULL;
2584 else
2585 get_task_struct(pos);
2586 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002587 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002588 put_task_struct(start);
2589 return pos;
2590}
2591
Eric W. Biederman61a28782006-10-02 02:18:49 -07002592static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2593 struct task_struct *task, int tid)
2594{
2595 char name[PROC_NUMBUF];
2596 int len = snprintf(name, sizeof(name), "%d", tid);
2597 return proc_fill_cache(filp, dirent, filldir, name, len,
2598 proc_task_instantiate, task, NULL);
2599}
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601/* for the /proc/TGID/task/ directories */
2602static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2603{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002604 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002606 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002607 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 int retval = -ENOENT;
2609 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002610 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2612
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002613 task = get_proc_task(inode);
2614 if (!task)
2615 goto out_no_task;
2616 rcu_read_lock();
2617 if (pid_alive(task)) {
2618 leader = task->group_leader;
2619 get_task_struct(leader);
2620 }
2621 rcu_read_unlock();
2622 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002623 if (!leader)
2624 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 retval = 0;
2626
2627 switch (pos) {
2628 case 0:
2629 ino = inode->i_ino;
2630 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2631 goto out;
2632 pos++;
2633 /* fall through */
2634 case 1:
2635 ino = parent_ino(dentry);
2636 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2637 goto out;
2638 pos++;
2639 /* fall through */
2640 }
2641
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002642 /* f_version caches the tgid value that the last readdir call couldn't
2643 * return. lseek aka telldir automagically resets f_version to 0.
2644 */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002645 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002646 filp->f_version = 0;
2647 for (task = first_tid(leader, tid, pos - 2);
2648 task;
2649 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002650 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002651 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002652 /* returning this tgid failed, save it as the first
2653 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002654 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002655 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
2659out:
2660 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002661 put_task_struct(leader);
2662out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return retval;
2664}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002665
2666static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2667{
2668 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002669 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002670 generic_fillattr(inode, stat);
2671
Eric W. Biederman99f89552006-06-26 00:25:55 -07002672 if (p) {
2673 rcu_read_lock();
2674 stat->nlink += get_nr_threads(p);
2675 rcu_read_unlock();
2676 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002677 }
2678
2679 return 0;
2680}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002681
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002682static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002683 .lookup = proc_task_lookup,
2684 .getattr = proc_task_getattr,
2685 .setattr = proc_setattr,
2686};
2687
Arjan van de Ven00977a52007-02-12 00:55:34 -08002688static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002689 .read = generic_read_dir,
2690 .readdir = proc_task_readdir,
2691};