blob: 78fdfea1a7f87a9afe95a24e0566279c1fbd69ab [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/cpuset.h>
71#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050072#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070073#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070074#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070075#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include "internal.h"
77
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070078/* NOTE:
79 * Implementing inode permission operations in /proc is almost
80 * certainly an error. Permission checks need to happen during
81 * each system call not at open time. The reason is that most of
82 * what we wish to check for permissions in /proc varies at runtime.
83 *
84 * The classic example of a problem is opening file descriptors
85 * in /proc for a task before it execs a suid executable.
86 */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Eric W. Biederman8578cea2006-06-26 00:25:54 -070089/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070090#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070094 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080096 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080097 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070098 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Eric W. Biederman61a28782006-10-02 02:18:49 -0700101#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700102 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700103 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 .mode = MODE, \
105 .iop = IOP, \
106 .fop = FOP, \
107 .op = OP, \
108}
109
Eric W. Biederman61a28782006-10-02 02:18:49 -0700110#define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700114#define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700118#define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700121#define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Kees Cook5096add2007-05-08 00:26:04 -0700126int maps_protect;
127EXPORT_SYMBOL(maps_protect);
128
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700129static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700132 task_lock(task);
133 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if(fs)
135 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700136 task_unlock(task);
137 return fs;
138}
139
Eric W. Biederman99f89552006-06-26 00:25:55 -0700140static int get_nr_threads(struct task_struct *tsk)
141{
142 /* Must be called with the rcu_read_lock held */
143 unsigned long flags;
144 int count = 0;
145
146 if (lock_task_sighand(tsk, &flags)) {
147 count = atomic_read(&tsk->signal->count);
148 unlock_task_sighand(tsk, &flags);
149 }
150 return count;
151}
152
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700153static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
154{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700155 struct task_struct *task = get_proc_task(inode);
156 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700158
159 if (task) {
160 fs = get_fs_struct(task);
161 put_task_struct(task);
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (fs) {
164 read_lock(&fs->lock);
165 *mnt = mntget(fs->pwdmnt);
166 *dentry = dget(fs->pwd);
167 read_unlock(&fs->lock);
168 result = 0;
169 put_fs_struct(fs);
170 }
171 return result;
172}
173
174static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
175{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176 struct task_struct *task = get_proc_task(inode);
177 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179
180 if (task) {
181 fs = get_fs_struct(task);
182 put_task_struct(task);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (fs) {
185 read_lock(&fs->lock);
186 *mnt = mntget(fs->rootmnt);
187 *dentry = dget(fs->root);
188 read_unlock(&fs->lock);
189 result = 0;
190 put_fs_struct(fs);
191 }
192 return result;
193}
194
195#define MAY_PTRACE(task) \
196 (task == current || \
197 (task->parent == current && \
198 (task->ptrace & PT_PTRACED) && \
199 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
200 security_ptrace(current,task) == 0))
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int proc_pid_environ(struct task_struct *task, char * buffer)
203{
204 int res = 0;
205 struct mm_struct *mm = get_task_mm(task);
206 if (mm) {
Alexey Dobriyanda58a162007-07-15 23:40:21 -0700207 unsigned int len;
208
209 res = -ESRCH;
210 if (!ptrace_may_attach(task))
211 goto out;
212
213 len = mm->env_end - mm->env_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (len > PAGE_SIZE)
215 len = PAGE_SIZE;
216 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Alexey Dobriyanda58a162007-07-15 23:40:21 -0700217out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 mmput(mm);
219 }
220 return res;
221}
222
223static int proc_pid_cmdline(struct task_struct *task, char * buffer)
224{
225 int res = 0;
226 unsigned int len;
227 struct mm_struct *mm = get_task_mm(task);
228 if (!mm)
229 goto out;
230 if (!mm->arg_end)
231 goto out_mm; /* Shh! No looking before we're done */
232
233 len = mm->arg_end - mm->arg_start;
234
235 if (len > PAGE_SIZE)
236 len = PAGE_SIZE;
237
238 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
239
240 // If the nul at the end of args has been overwritten, then
241 // assume application is using setproctitle(3).
242 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
243 len = strnlen(buffer, res);
244 if (len < res) {
245 res = len;
246 } else {
247 len = mm->env_end - mm->env_start;
248 if (len > PAGE_SIZE - res)
249 len = PAGE_SIZE - res;
250 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
251 res = strnlen(buffer, res);
252 }
253 }
254out_mm:
255 mmput(mm);
256out:
257 return res;
258}
259
260static int proc_pid_auxv(struct task_struct *task, char *buffer)
261{
262 int res = 0;
263 struct mm_struct *mm = get_task_mm(task);
264 if (mm) {
265 unsigned int nwords = 0;
266 do
267 nwords += 2;
268 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
269 res = nwords * sizeof(mm->saved_auxv[0]);
270 if (res > PAGE_SIZE)
271 res = PAGE_SIZE;
272 memcpy(buffer, mm->saved_auxv, res);
273 mmput(mm);
274 }
275 return res;
276}
277
278
279#ifdef CONFIG_KALLSYMS
280/*
281 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
282 * Returns the resolved symbol. If that fails, simply return the address.
283 */
284static int proc_pid_wchan(struct task_struct *task, char *buffer)
285{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700286 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700287 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 wchan = get_wchan(task);
290
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700291 if (lookup_symbol_name(wchan, symname) < 0)
292 return sprintf(buffer, "%lu", wchan);
293 else
294 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296#endif /* CONFIG_KALLSYMS */
297
298#ifdef CONFIG_SCHEDSTATS
299/*
300 * Provides /proc/PID/schedstat
301 */
302static int proc_pid_schedstat(struct task_struct *task, char *buffer)
303{
Balbir Singh172ba842007-07-09 18:52:00 +0200304 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 task->sched_info.cpu_time,
306 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200307 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309#endif
310
311/* The badness from the OOM killer */
312unsigned long badness(struct task_struct *p, unsigned long uptime);
313static int proc_oom_score(struct task_struct *task, char *buffer)
314{
315 unsigned long points;
316 struct timespec uptime;
317
318 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700319 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700321 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return sprintf(buffer, "%lu\n", points);
323}
324
325/************************************************************************/
326/* Here the fs part begins */
327/************************************************************************/
328
329/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700330static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700332 struct task_struct *task;
333 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700334 /* Allow access to a task's file descriptors if it is us or we
335 * may use ptrace attach to the process and find out that
336 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700337 */
338 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700339 if (task) {
340 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700341 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700342 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700343 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700346static int proc_setattr(struct dentry *dentry, struct iattr *attr)
347{
348 int error;
349 struct inode *inode = dentry->d_inode;
350
351 if (attr->ia_valid & ATTR_MODE)
352 return -EPERM;
353
354 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700355 if (!error)
356 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700357 return error;
358}
359
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800360static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700361 .setattr = proc_setattr,
362};
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500365struct proc_mounts {
366 struct seq_file m;
367 int event;
368};
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static int mounts_open(struct inode *inode, struct file *file)
371{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700372 struct task_struct *task = get_proc_task(inode);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800373 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500374 struct proc_mounts *p;
375 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Eric W. Biederman99f89552006-06-26 00:25:55 -0700377 if (task) {
378 task_lock(task);
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800379 if (task->nsproxy) {
380 ns = task->nsproxy->mnt_ns;
381 if (ns)
382 get_mnt_ns(ns);
383 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700384 task_unlock(task);
385 put_task_struct(task);
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800388 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500389 ret = -ENOMEM;
390 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
391 if (p) {
392 file->private_data = &p->m;
393 ret = seq_open(file, &mounts_op);
394 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800395 p->m.private = ns;
396 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500397 return 0;
398 }
399 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800401 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 return ret;
404}
405
406static int mounts_release(struct inode *inode, struct file *file)
407{
408 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800409 struct mnt_namespace *ns = m->private;
410 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return seq_release(inode, file);
412}
413
Al Viro5addc5d2005-11-07 17:15:49 -0500414static unsigned mounts_poll(struct file *file, poll_table *wait)
415{
416 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800417 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500418 unsigned res = 0;
419
420 poll_wait(file, &ns->poll, wait);
421
422 spin_lock(&vfsmount_lock);
423 if (p->event != ns->event) {
424 p->event = ns->event;
425 res = POLLERR;
426 }
427 spin_unlock(&vfsmount_lock);
428
429 return res;
430}
431
Arjan van de Ven00977a52007-02-12 00:55:34 -0800432static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 .open = mounts_open,
434 .read = seq_read,
435 .llseek = seq_lseek,
436 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500437 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438};
439
Chuck Leverb4629fe2006-03-20 13:44:12 -0500440extern struct seq_operations mountstats_op;
441static int mountstats_open(struct inode *inode, struct file *file)
442{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500443 int ret = seq_open(file, &mountstats_op);
444
445 if (!ret) {
446 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800447 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700448 struct task_struct *task = get_proc_task(inode);
449
450 if (task) {
451 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800452 if (task->nsproxy)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800453 mnt_ns = task->nsproxy->mnt_ns;
454 if (mnt_ns)
455 get_mnt_ns(mnt_ns);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700456 task_unlock(task);
457 put_task_struct(task);
458 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500459
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800460 if (mnt_ns)
461 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500462 else {
463 seq_release(inode, file);
464 ret = -EINVAL;
465 }
466 }
467 return ret;
468}
469
Arjan van de Ven00977a52007-02-12 00:55:34 -0800470static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500471 .open = mountstats_open,
472 .read = seq_read,
473 .llseek = seq_lseek,
474 .release = mounts_release,
475};
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
478
479static ssize_t proc_info_read(struct file * file, char __user * buf,
480 size_t count, loff_t *ppos)
481{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800482 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 unsigned long page;
484 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700485 struct task_struct *task = get_proc_task(inode);
486
487 length = -ESRCH;
488 if (!task)
489 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (count > PROC_BLOCK_SIZE)
492 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700493
494 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700495 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 length = PROC_I(inode)->op.proc_read(task, (char*)page);
499
500 if (length >= 0)
501 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
502 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700503out:
504 put_task_struct(task);
505out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return length;
507}
508
Arjan van de Ven00977a52007-02-12 00:55:34 -0800509static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .read = proc_info_read,
511};
512
513static int mem_open(struct inode* inode, struct file* file)
514{
515 file->private_data = (void*)((long)current->self_exec_id);
516 return 0;
517}
518
519static ssize_t mem_read(struct file * file, char __user * buf,
520 size_t count, loff_t *ppos)
521{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800522 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 char *page;
524 unsigned long src = *ppos;
525 int ret = -ESRCH;
526 struct mm_struct *mm;
527
Eric W. Biederman99f89552006-06-26 00:25:55 -0700528 if (!task)
529 goto out_no_task;
530
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700531 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto out;
533
534 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700535 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (!page)
537 goto out;
538
539 ret = 0;
540
541 mm = get_task_mm(task);
542 if (!mm)
543 goto out_free;
544
545 ret = -EIO;
546
547 if (file->private_data != (void*)((long)current->self_exec_id))
548 goto out_put;
549
550 ret = 0;
551
552 while (count > 0) {
553 int this_len, retval;
554
555 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
556 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700557 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (!ret)
559 ret = -EIO;
560 break;
561 }
562
563 if (copy_to_user(buf, page, retval)) {
564 ret = -EFAULT;
565 break;
566 }
567
568 ret += retval;
569 src += retval;
570 buf += retval;
571 count -= retval;
572 }
573 *ppos = src;
574
575out_put:
576 mmput(mm);
577out_free:
578 free_page((unsigned long) page);
579out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700580 put_task_struct(task);
581out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return ret;
583}
584
585#define mem_write NULL
586
587#ifndef mem_write
588/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800589static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 size_t count, loff_t *ppos)
591{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700592 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800594 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 unsigned long dst = *ppos;
596
Eric W. Biederman99f89552006-06-26 00:25:55 -0700597 copied = -ESRCH;
598 if (!task)
599 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Eric W. Biederman99f89552006-06-26 00:25:55 -0700601 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
602 goto out;
603
604 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700605 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700607 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700609 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 while (count > 0) {
611 int this_len, retval;
612
613 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
614 if (copy_from_user(page, buf, this_len)) {
615 copied = -EFAULT;
616 break;
617 }
618 retval = access_process_vm(task, dst, page, this_len, 1);
619 if (!retval) {
620 if (!copied)
621 copied = -EIO;
622 break;
623 }
624 copied += retval;
625 buf += retval;
626 dst += retval;
627 count -= retval;
628 }
629 *ppos = dst;
630 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700631out:
632 put_task_struct(task);
633out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return copied;
635}
636#endif
637
638static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
639{
640 switch (orig) {
641 case 0:
642 file->f_pos = offset;
643 break;
644 case 1:
645 file->f_pos += offset;
646 break;
647 default:
648 return -EINVAL;
649 }
650 force_successful_syscall_return();
651 return file->f_pos;
652}
653
Arjan van de Ven00977a52007-02-12 00:55:34 -0800654static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 .llseek = mem_lseek,
656 .read = mem_read,
657 .write = mem_write,
658 .open = mem_open,
659};
660
661static ssize_t oom_adjust_read(struct file *file, char __user *buf,
662 size_t count, loff_t *ppos)
663{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800664 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700665 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700667 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Eric W. Biederman99f89552006-06-26 00:25:55 -0700669 if (!task)
670 return -ESRCH;
671 oom_adjust = task->oomkilladj;
672 put_task_struct(task);
673
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700674 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700675
676 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
680 size_t count, loff_t *ppos)
681{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700682 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700683 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int oom_adjust;
685
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700686 memset(buffer, 0, sizeof(buffer));
687 if (count > sizeof(buffer) - 1)
688 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (copy_from_user(buffer, buf, count))
690 return -EFAULT;
691 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700692 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
693 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EINVAL;
695 if (*end == '\n')
696 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800697 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700698 if (!task)
699 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800700 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
701 put_task_struct(task);
702 return -EACCES;
703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700705 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (end - buffer == 0)
707 return -EIO;
708 return end - buffer;
709}
710
Arjan van de Ven00977a52007-02-12 00:55:34 -0800711static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 .read = oom_adjust_read,
713 .write = oom_adjust_write,
714};
715
David Rientjes4b8df892007-05-08 20:27:01 -0700716#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -0700717static ssize_t clear_refs_write(struct file *file, const char __user *buf,
718 size_t count, loff_t *ppos)
719{
720 struct task_struct *task;
721 char buffer[PROC_NUMBUF], *end;
722 struct mm_struct *mm;
723
724 memset(buffer, 0, sizeof(buffer));
725 if (count > sizeof(buffer) - 1)
726 count = sizeof(buffer) - 1;
727 if (copy_from_user(buffer, buf, count))
728 return -EFAULT;
729 if (!simple_strtol(buffer, &end, 0))
730 return -EINVAL;
731 if (*end == '\n')
732 end++;
733 task = get_proc_task(file->f_path.dentry->d_inode);
734 if (!task)
735 return -ESRCH;
736 mm = get_task_mm(task);
737 if (mm) {
738 clear_refs_smap(mm);
739 mmput(mm);
740 }
741 put_task_struct(task);
742 if (end - buffer == 0)
743 return -EIO;
744 return end - buffer;
745}
746
747static struct file_operations proc_clear_refs_operations = {
748 .write = clear_refs_write,
749};
David Rientjes4b8df892007-05-08 20:27:01 -0700750#endif
David Rientjesb813e932007-05-06 14:49:24 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752#ifdef CONFIG_AUDITSYSCALL
753#define TMPBUFLEN 21
754static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
755 size_t count, loff_t *ppos)
756{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800757 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700758 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ssize_t length;
760 char tmpbuf[TMPBUFLEN];
761
Eric W. Biederman99f89552006-06-26 00:25:55 -0700762 if (!task)
763 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
765 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700766 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
768}
769
770static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
771 size_t count, loff_t *ppos)
772{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800773 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 char *page, *tmp;
775 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 uid_t loginuid;
777
778 if (!capable(CAP_AUDIT_CONTROL))
779 return -EPERM;
780
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700781 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EPERM;
783
Al Viroe0182902006-05-18 08:28:02 -0400784 if (count >= PAGE_SIZE)
785 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (*ppos != 0) {
788 /* No partial writes. */
789 return -EINVAL;
790 }
Mel Gormane12ba742007-10-16 01:25:52 -0700791 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!page)
793 return -ENOMEM;
794 length = -EFAULT;
795 if (copy_from_user(page, buf, count))
796 goto out_free_page;
797
Al Viroe0182902006-05-18 08:28:02 -0400798 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 loginuid = simple_strtoul(page, &tmp, 10);
800 if (tmp == page) {
801 length = -EINVAL;
802 goto out_free_page;
803
804 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700805 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (likely(length == 0))
807 length = count;
808
809out_free_page:
810 free_page((unsigned long) page);
811 return length;
812}
813
Arjan van de Ven00977a52007-02-12 00:55:34 -0800814static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 .read = proc_loginuid_read,
816 .write = proc_loginuid_write,
817};
818#endif
819
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800820#ifdef CONFIG_FAULT_INJECTION
821static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
822 size_t count, loff_t *ppos)
823{
824 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
825 char buffer[PROC_NUMBUF];
826 size_t len;
827 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800828
829 if (!task)
830 return -ESRCH;
831 make_it_fail = task->make_it_fail;
832 put_task_struct(task);
833
834 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700835
836 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800837}
838
839static ssize_t proc_fault_inject_write(struct file * file,
840 const char __user * buf, size_t count, loff_t *ppos)
841{
842 struct task_struct *task;
843 char buffer[PROC_NUMBUF], *end;
844 int make_it_fail;
845
846 if (!capable(CAP_SYS_RESOURCE))
847 return -EPERM;
848 memset(buffer, 0, sizeof(buffer));
849 if (count > sizeof(buffer) - 1)
850 count = sizeof(buffer) - 1;
851 if (copy_from_user(buffer, buf, count))
852 return -EFAULT;
853 make_it_fail = simple_strtol(buffer, &end, 0);
854 if (*end == '\n')
855 end++;
856 task = get_proc_task(file->f_dentry->d_inode);
857 if (!task)
858 return -ESRCH;
859 task->make_it_fail = make_it_fail;
860 put_task_struct(task);
861 if (end - buffer == 0)
862 return -EIO;
863 return end - buffer;
864}
865
Arjan van de Ven00977a52007-02-12 00:55:34 -0800866static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800867 .read = proc_fault_inject_read,
868 .write = proc_fault_inject_write,
869};
870#endif
871
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200872#ifdef CONFIG_SCHED_DEBUG
873/*
874 * Print out various scheduling related per-task fields:
875 */
876static int sched_show(struct seq_file *m, void *v)
877{
878 struct inode *inode = m->private;
879 struct task_struct *p;
880
881 WARN_ON(!inode);
882
883 p = get_proc_task(inode);
884 if (!p)
885 return -ESRCH;
886 proc_sched_show_task(p, m);
887
888 put_task_struct(p);
889
890 return 0;
891}
892
893static ssize_t
894sched_write(struct file *file, const char __user *buf,
895 size_t count, loff_t *offset)
896{
897 struct inode *inode = file->f_path.dentry->d_inode;
898 struct task_struct *p;
899
900 WARN_ON(!inode);
901
902 p = get_proc_task(inode);
903 if (!p)
904 return -ESRCH;
905 proc_sched_set_task(p);
906
907 put_task_struct(p);
908
909 return count;
910}
911
912static int sched_open(struct inode *inode, struct file *filp)
913{
914 int ret;
915
916 ret = single_open(filp, sched_show, NULL);
917 if (!ret) {
918 struct seq_file *m = filp->private_data;
919
920 m->private = inode;
921 }
922 return ret;
923}
924
925static const struct file_operations proc_pid_sched_operations = {
926 .open = sched_open,
927 .read = seq_read,
928 .write = sched_write,
929 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -0700930 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200931};
932
933#endif
934
Al Viro008b1502005-08-20 00:17:39 +0100935static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct inode *inode = dentry->d_inode;
938 int error = -EACCES;
939
940 /* We don't need a base pointer in the /proc filesystem */
941 path_release(nd);
942
Eric W. Biederman778c1142006-06-26 00:25:58 -0700943 /* Are we allowed to snoop on the tasks file descriptors? */
944 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
948 nd->last_type = LAST_BIND;
949out:
Al Viro008b1502005-08-20 00:17:39 +0100950 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
954 char __user *buffer, int buflen)
955{
956 struct inode * inode;
Mel Gormane12ba742007-10-16 01:25:52 -0700957 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
958 char *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int len;
960
961 if (!tmp)
962 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 inode = dentry->d_inode;
965 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
966 len = PTR_ERR(path);
967 if (IS_ERR(path))
968 goto out;
969 len = tmp + PAGE_SIZE - 1 - path;
970
971 if (len > buflen)
972 len = buflen;
973 if (copy_to_user(buffer, path, len))
974 len = -EFAULT;
975 out:
976 free_page((unsigned long)tmp);
977 return len;
978}
979
980static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
981{
982 int error = -EACCES;
983 struct inode *inode = dentry->d_inode;
984 struct dentry *de;
985 struct vfsmount *mnt = NULL;
986
Eric W. Biederman778c1142006-06-26 00:25:58 -0700987 /* Are we allowed to snoop on the tasks file descriptors? */
988 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
992 if (error)
993 goto out;
994
995 error = do_proc_readlink(de, mnt, buffer, buflen);
996 dput(de);
997 mntput(mnt);
998out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return error;
1000}
1001
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001002static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001004 .follow_link = proc_pid_follow_link,
1005 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006};
1007
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001008
1009/* building an inode */
1010
1011static int task_dumpable(struct task_struct *task)
1012{
1013 int dumpable = 0;
1014 struct mm_struct *mm;
1015
1016 task_lock(task);
1017 mm = task->mm;
1018 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001019 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001020 task_unlock(task);
1021 if(dumpable == 1)
1022 return 1;
1023 return 0;
1024}
1025
1026
Eric W. Biederman61a28782006-10-02 02:18:49 -07001027static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001028{
1029 struct inode * inode;
1030 struct proc_inode *ei;
1031
1032 /* We need a new inode */
1033
1034 inode = new_inode(sb);
1035 if (!inode)
1036 goto out;
1037
1038 /* Common stuff */
1039 ei = PROC_I(inode);
1040 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001041 inode->i_op = &proc_def_inode_operations;
1042
1043 /*
1044 * grab the reference to task.
1045 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001046 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001047 if (!ei->pid)
1048 goto out_unlock;
1049
1050 inode->i_uid = 0;
1051 inode->i_gid = 0;
1052 if (task_dumpable(task)) {
1053 inode->i_uid = task->euid;
1054 inode->i_gid = task->egid;
1055 }
1056 security_task_to_inode(task, inode);
1057
1058out:
1059 return inode;
1060
1061out_unlock:
1062 iput(inode);
1063 return NULL;
1064}
1065
1066static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1067{
1068 struct inode *inode = dentry->d_inode;
1069 struct task_struct *task;
1070 generic_fillattr(inode, stat);
1071
1072 rcu_read_lock();
1073 stat->uid = 0;
1074 stat->gid = 0;
1075 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1076 if (task) {
1077 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1078 task_dumpable(task)) {
1079 stat->uid = task->euid;
1080 stat->gid = task->egid;
1081 }
1082 }
1083 rcu_read_unlock();
1084 return 0;
1085}
1086
1087/* dentry stuff */
1088
1089/*
1090 * Exceptional case: normally we are not allowed to unhash a busy
1091 * directory. In this case, however, we can do it - no aliasing problems
1092 * due to the way we treat inodes.
1093 *
1094 * Rewrite the inode's ownerships here because the owning task may have
1095 * performed a setuid(), etc.
1096 *
1097 * Before the /proc/pid/status file was created the only way to read
1098 * the effective uid of a /process was to stat /proc/pid. Reading
1099 * /proc/pid/status is slow enough that procps and other packages
1100 * kept stating /proc/pid. To keep the rules in /proc simple I have
1101 * made this apply to all per process world readable and executable
1102 * directories.
1103 */
1104static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1105{
1106 struct inode *inode = dentry->d_inode;
1107 struct task_struct *task = get_proc_task(inode);
1108 if (task) {
1109 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1110 task_dumpable(task)) {
1111 inode->i_uid = task->euid;
1112 inode->i_gid = task->egid;
1113 } else {
1114 inode->i_uid = 0;
1115 inode->i_gid = 0;
1116 }
1117 inode->i_mode &= ~(S_ISUID | S_ISGID);
1118 security_task_to_inode(task, inode);
1119 put_task_struct(task);
1120 return 1;
1121 }
1122 d_drop(dentry);
1123 return 0;
1124}
1125
1126static int pid_delete_dentry(struct dentry * dentry)
1127{
1128 /* Is the task we represent dead?
1129 * If so, then don't put the dentry on the lru list,
1130 * kill it immediately.
1131 */
1132 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1133}
1134
1135static struct dentry_operations pid_dentry_operations =
1136{
1137 .d_revalidate = pid_revalidate,
1138 .d_delete = pid_delete_dentry,
1139};
1140
1141/* Lookups */
1142
Eric Dumazetc5141e62007-05-08 00:26:15 -07001143typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1144 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001145
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001146/*
1147 * Fill a directory entry.
1148 *
1149 * If possible create the dcache entry and derive our inode number and
1150 * file type from dcache entry.
1151 *
1152 * Since all of the proc inode numbers are dynamically generated, the inode
1153 * numbers do not exist until the inode is cache. This means creating the
1154 * the dcache entry in readdir is necessary to keep the inode numbers
1155 * reported by readdir in sync with the inode numbers reported
1156 * by stat.
1157 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001158static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1159 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001160 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001161{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001162 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001163 struct inode *inode;
1164 struct qstr qname;
1165 ino_t ino = 0;
1166 unsigned type = DT_UNKNOWN;
1167
1168 qname.name = name;
1169 qname.len = len;
1170 qname.hash = full_name_hash(name, len);
1171
1172 child = d_lookup(dir, &qname);
1173 if (!child) {
1174 struct dentry *new;
1175 new = d_alloc(dir, &qname);
1176 if (new) {
1177 child = instantiate(dir->d_inode, new, task, ptr);
1178 if (child)
1179 dput(new);
1180 else
1181 child = new;
1182 }
1183 }
1184 if (!child || IS_ERR(child) || !child->d_inode)
1185 goto end_instantiate;
1186 inode = child->d_inode;
1187 if (inode) {
1188 ino = inode->i_ino;
1189 type = inode->i_mode >> 12;
1190 }
1191 dput(child);
1192end_instantiate:
1193 if (!ino)
1194 ino = find_inode_number(dir, &qname);
1195 if (!ino)
1196 ino = 1;
1197 return filldir(dirent, name, len, filp->f_pos, ino, type);
1198}
1199
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001200static unsigned name_to_int(struct dentry *dentry)
1201{
1202 const char *name = dentry->d_name.name;
1203 int len = dentry->d_name.len;
1204 unsigned n = 0;
1205
1206 if (len > 1 && *name == '0')
1207 goto out;
1208 while (len-- > 0) {
1209 unsigned c = *name++ - '0';
1210 if (c > 9)
1211 goto out;
1212 if (n >= (~0U-9)/10)
1213 goto out;
1214 n *= 10;
1215 n += c;
1216 }
1217 return n;
1218out:
1219 return ~0U;
1220}
1221
Miklos Szeredi27932742007-05-08 00:26:17 -07001222#define PROC_FDINFO_MAX 64
1223
1224static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1225 struct vfsmount **mnt, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001226{
1227 struct task_struct *task = get_proc_task(inode);
1228 struct files_struct *files = NULL;
1229 struct file *file;
1230 int fd = proc_fd(inode);
1231
1232 if (task) {
1233 files = get_files_struct(task);
1234 put_task_struct(task);
1235 }
1236 if (files) {
1237 /*
1238 * We are not taking a ref to the file structure, so we must
1239 * hold ->file_lock.
1240 */
1241 spin_lock(&files->file_lock);
1242 file = fcheck_files(files, fd);
1243 if (file) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001244 if (mnt)
1245 *mnt = mntget(file->f_path.mnt);
1246 if (dentry)
1247 *dentry = dget(file->f_path.dentry);
1248 if (info)
1249 snprintf(info, PROC_FDINFO_MAX,
1250 "pos:\t%lli\n"
1251 "flags:\t0%o\n",
1252 (long long) file->f_pos,
1253 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001254 spin_unlock(&files->file_lock);
1255 put_files_struct(files);
1256 return 0;
1257 }
1258 spin_unlock(&files->file_lock);
1259 put_files_struct(files);
1260 }
1261 return -ENOENT;
1262}
1263
Miklos Szeredi27932742007-05-08 00:26:17 -07001264static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1265 struct vfsmount **mnt)
1266{
1267 return proc_fd_info(inode, dentry, mnt, NULL);
1268}
1269
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001270static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1271{
1272 struct inode *inode = dentry->d_inode;
1273 struct task_struct *task = get_proc_task(inode);
1274 int fd = proc_fd(inode);
1275 struct files_struct *files;
1276
1277 if (task) {
1278 files = get_files_struct(task);
1279 if (files) {
1280 rcu_read_lock();
1281 if (fcheck_files(files, fd)) {
1282 rcu_read_unlock();
1283 put_files_struct(files);
1284 if (task_dumpable(task)) {
1285 inode->i_uid = task->euid;
1286 inode->i_gid = task->egid;
1287 } else {
1288 inode->i_uid = 0;
1289 inode->i_gid = 0;
1290 }
1291 inode->i_mode &= ~(S_ISUID | S_ISGID);
1292 security_task_to_inode(task, inode);
1293 put_task_struct(task);
1294 return 1;
1295 }
1296 rcu_read_unlock();
1297 put_files_struct(files);
1298 }
1299 put_task_struct(task);
1300 }
1301 d_drop(dentry);
1302 return 0;
1303}
1304
1305static struct dentry_operations tid_fd_dentry_operations =
1306{
1307 .d_revalidate = tid_fd_revalidate,
1308 .d_delete = pid_delete_dentry,
1309};
1310
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001311static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001312 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001313{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001314 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001315 struct file *file;
1316 struct files_struct *files;
1317 struct inode *inode;
1318 struct proc_inode *ei;
1319 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001320
Eric W. Biederman61a28782006-10-02 02:18:49 -07001321 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001322 if (!inode)
1323 goto out;
1324 ei = PROC_I(inode);
1325 ei->fd = fd;
1326 files = get_files_struct(task);
1327 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001328 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001329 inode->i_mode = S_IFLNK;
1330
1331 /*
1332 * We are not taking a ref to the file structure, so we must
1333 * hold ->file_lock.
1334 */
1335 spin_lock(&files->file_lock);
1336 file = fcheck_files(files, fd);
1337 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001338 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001339 if (file->f_mode & 1)
1340 inode->i_mode |= S_IRUSR | S_IXUSR;
1341 if (file->f_mode & 2)
1342 inode->i_mode |= S_IWUSR | S_IXUSR;
1343 spin_unlock(&files->file_lock);
1344 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001345
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001346 inode->i_op = &proc_pid_link_inode_operations;
1347 inode->i_size = 64;
1348 ei->op.proc_get_link = proc_fd_link;
1349 dentry->d_op = &tid_fd_dentry_operations;
1350 d_add(dentry, inode);
1351 /* Close the race of the process dying before we return the dentry */
1352 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001353 error = NULL;
1354
1355 out:
1356 return error;
1357out_unlock:
1358 spin_unlock(&files->file_lock);
1359 put_files_struct(files);
1360out_iput:
1361 iput(inode);
1362 goto out;
1363}
1364
Miklos Szeredi27932742007-05-08 00:26:17 -07001365static struct dentry *proc_lookupfd_common(struct inode *dir,
1366 struct dentry *dentry,
1367 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001368{
1369 struct task_struct *task = get_proc_task(dir);
1370 unsigned fd = name_to_int(dentry);
1371 struct dentry *result = ERR_PTR(-ENOENT);
1372
1373 if (!task)
1374 goto out_no_task;
1375 if (fd == ~0U)
1376 goto out;
1377
Miklos Szeredi27932742007-05-08 00:26:17 -07001378 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001379out:
1380 put_task_struct(task);
1381out_no_task:
1382 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001383}
1384
Miklos Szeredi27932742007-05-08 00:26:17 -07001385static int proc_readfd_common(struct file * filp, void * dirent,
1386 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001388 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001389 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001390 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 unsigned int fd, tid, ino;
1392 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001394 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001397 if (!p)
1398 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 retval = 0;
1400 tid = p->pid;
1401
1402 fd = filp->f_pos;
1403 switch (fd) {
1404 case 0:
1405 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1406 goto out;
1407 filp->f_pos++;
1408 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001409 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1411 goto out;
1412 filp->f_pos++;
1413 default:
1414 files = get_files_struct(p);
1415 if (!files)
1416 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001417 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001418 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001420 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001422 char name[PROC_NUMBUF];
1423 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (!fcheck_files(files, fd))
1426 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001427 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Miklos Szeredi27932742007-05-08 00:26:17 -07001429 len = snprintf(name, sizeof(name), "%d", fd);
1430 if (proc_fill_cache(filp, dirent, filldir,
1431 name, len, instantiate,
1432 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001433 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 break;
1435 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001436 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001438 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 put_files_struct(files);
1440 }
1441out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001442 put_task_struct(p);
1443out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return retval;
1445}
1446
Miklos Szeredi27932742007-05-08 00:26:17 -07001447static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1448 struct nameidata *nd)
1449{
1450 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1451}
1452
1453static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1454{
1455 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1456}
1457
1458static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1459 size_t len, loff_t *ppos)
1460{
1461 char tmp[PROC_FDINFO_MAX];
1462 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1463 if (!err)
1464 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1465 return err;
1466}
1467
1468static const struct file_operations proc_fdinfo_file_operations = {
1469 .open = nonseekable_open,
1470 .read = proc_fdinfo_read,
1471};
1472
Arjan van de Ven00977a52007-02-12 00:55:34 -08001473static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 .read = generic_read_dir,
1475 .readdir = proc_readfd,
1476};
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001479 * /proc/pid/fd needs a special permission handler so that a process can still
1480 * access /proc/self/fd after it has executed a setuid().
1481 */
1482static int proc_fd_permission(struct inode *inode, int mask,
1483 struct nameidata *nd)
1484{
1485 int rv;
1486
1487 rv = generic_permission(inode, mask, NULL);
1488 if (rv == 0)
1489 return 0;
1490 if (task_pid(current) == proc_pid(inode))
1491 rv = 0;
1492 return rv;
1493}
1494
1495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * proc directories can do almost nothing..
1497 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001498static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001500 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001501 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502};
1503
Miklos Szeredi27932742007-05-08 00:26:17 -07001504static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1505 struct dentry *dentry, struct task_struct *task, const void *ptr)
1506{
1507 unsigned fd = *(unsigned *)ptr;
1508 struct inode *inode;
1509 struct proc_inode *ei;
1510 struct dentry *error = ERR_PTR(-ENOENT);
1511
1512 inode = proc_pid_make_inode(dir->i_sb, task);
1513 if (!inode)
1514 goto out;
1515 ei = PROC_I(inode);
1516 ei->fd = fd;
1517 inode->i_mode = S_IFREG | S_IRUSR;
1518 inode->i_fop = &proc_fdinfo_file_operations;
1519 dentry->d_op = &tid_fd_dentry_operations;
1520 d_add(dentry, inode);
1521 /* Close the race of the process dying before we return the dentry */
1522 if (tid_fd_revalidate(dentry, NULL))
1523 error = NULL;
1524
1525 out:
1526 return error;
1527}
1528
1529static struct dentry *proc_lookupfdinfo(struct inode *dir,
1530 struct dentry *dentry,
1531 struct nameidata *nd)
1532{
1533 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1534}
1535
1536static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1537{
1538 return proc_readfd_common(filp, dirent, filldir,
1539 proc_fdinfo_instantiate);
1540}
1541
1542static const struct file_operations proc_fdinfo_operations = {
1543 .read = generic_read_dir,
1544 .readdir = proc_readfdinfo,
1545};
1546
1547/*
1548 * proc directories can do almost nothing..
1549 */
1550static const struct inode_operations proc_fdinfo_inode_operations = {
1551 .lookup = proc_lookupfdinfo,
1552 .setattr = proc_setattr,
1553};
1554
1555
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001556static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001557 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001558{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001559 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001560 struct inode *inode;
1561 struct proc_inode *ei;
1562 struct dentry *error = ERR_PTR(-EINVAL);
1563
Eric W. Biederman61a28782006-10-02 02:18:49 -07001564 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001565 if (!inode)
1566 goto out;
1567
1568 ei = PROC_I(inode);
1569 inode->i_mode = p->mode;
1570 if (S_ISDIR(inode->i_mode))
1571 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1572 if (p->iop)
1573 inode->i_op = p->iop;
1574 if (p->fop)
1575 inode->i_fop = p->fop;
1576 ei->op = p->op;
1577 dentry->d_op = &pid_dentry_operations;
1578 d_add(dentry, inode);
1579 /* Close the race of the process dying before we return the dentry */
1580 if (pid_revalidate(dentry, NULL))
1581 error = NULL;
1582out:
1583 return error;
1584}
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586static struct dentry *proc_pident_lookup(struct inode *dir,
1587 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001588 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001589 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001592 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001593 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001594 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001596 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 inode = NULL;
1598
Eric W. Biederman99f89552006-06-26 00:25:55 -07001599 if (!task)
1600 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001602 /*
1603 * Yes, it does not scale. And it should not. Don't add
1604 * new entries into /proc/<tgid>/ without very good reasons.
1605 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001606 last = &ents[nents - 1];
1607 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (p->len != dentry->d_name.len)
1609 continue;
1610 if (!memcmp(dentry->d_name.name, p->name, p->len))
1611 break;
1612 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001613 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto out;
1615
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001616 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001618 put_task_struct(task);
1619out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001620 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Eric Dumazetc5141e62007-05-08 00:26:15 -07001623static int proc_pident_fill_cache(struct file *filp, void *dirent,
1624 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001625{
1626 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1627 proc_pident_instantiate, task, p);
1628}
1629
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001630static int proc_pident_readdir(struct file *filp,
1631 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001632 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001633{
1634 int i;
1635 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001636 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001637 struct inode *inode = dentry->d_inode;
1638 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001639 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001640 ino_t ino;
1641 int ret;
1642
1643 ret = -ENOENT;
1644 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001645 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001646
1647 ret = 0;
1648 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001649 i = filp->f_pos;
1650 switch (i) {
1651 case 0:
1652 ino = inode->i_ino;
1653 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1654 goto out;
1655 i++;
1656 filp->f_pos++;
1657 /* fall through */
1658 case 1:
1659 ino = parent_ino(dentry);
1660 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1661 goto out;
1662 i++;
1663 filp->f_pos++;
1664 /* fall through */
1665 default:
1666 i -= 2;
1667 if (i >= nents) {
1668 ret = 1;
1669 goto out;
1670 }
1671 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001672 last = &ents[nents - 1];
1673 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001674 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001675 goto out;
1676 filp->f_pos++;
1677 p++;
1678 }
1679 }
1680
1681 ret = 1;
1682out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001683 put_task_struct(task);
1684out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001685 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001689static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1690 size_t count, loff_t *ppos)
1691{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001692 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001693 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001694 ssize_t length;
1695 struct task_struct *task = get_proc_task(inode);
1696
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001697 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001698 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001699
1700 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001701 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001702 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001703 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001704 if (length > 0)
1705 length = simple_read_from_buffer(buf, count, ppos, p, length);
1706 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001707 return length;
1708}
1709
1710static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1711 size_t count, loff_t *ppos)
1712{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001713 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001714 char *page;
1715 ssize_t length;
1716 struct task_struct *task = get_proc_task(inode);
1717
1718 length = -ESRCH;
1719 if (!task)
1720 goto out_no_task;
1721 if (count > PAGE_SIZE)
1722 count = PAGE_SIZE;
1723
1724 /* No partial writes. */
1725 length = -EINVAL;
1726 if (*ppos != 0)
1727 goto out;
1728
1729 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001730 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001731 if (!page)
1732 goto out;
1733
1734 length = -EFAULT;
1735 if (copy_from_user(page, buf, count))
1736 goto out_free;
1737
1738 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001739 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001740 (void*)page, count);
1741out_free:
1742 free_page((unsigned long) page);
1743out:
1744 put_task_struct(task);
1745out_no_task:
1746 return length;
1747}
1748
Arjan van de Ven00977a52007-02-12 00:55:34 -08001749static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001750 .read = proc_pid_attr_read,
1751 .write = proc_pid_attr_write,
1752};
1753
Eric Dumazetc5141e62007-05-08 00:26:15 -07001754static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001755 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1756 REG("prev", S_IRUGO, pid_attr),
1757 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1758 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1759 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1760 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761};
1762
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001763static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 void * dirent, filldir_t filldir)
1765{
1766 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001767 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Arjan van de Ven00977a52007-02-12 00:55:34 -08001770static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001772 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773};
1774
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001775static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 struct dentry *dentry, struct nameidata *nd)
1777{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001778 return proc_pident_lookup(dir, dentry,
1779 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001782static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001783 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001784 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001785 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786};
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788#endif
1789
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07001790#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1791static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
1792 size_t count, loff_t *ppos)
1793{
1794 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1795 struct mm_struct *mm;
1796 char buffer[PROC_NUMBUF];
1797 size_t len;
1798 int ret;
1799
1800 if (!task)
1801 return -ESRCH;
1802
1803 ret = 0;
1804 mm = get_task_mm(task);
1805 if (mm) {
1806 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
1807 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
1808 MMF_DUMP_FILTER_SHIFT));
1809 mmput(mm);
1810 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
1811 }
1812
1813 put_task_struct(task);
1814
1815 return ret;
1816}
1817
1818static ssize_t proc_coredump_filter_write(struct file *file,
1819 const char __user *buf,
1820 size_t count,
1821 loff_t *ppos)
1822{
1823 struct task_struct *task;
1824 struct mm_struct *mm;
1825 char buffer[PROC_NUMBUF], *end;
1826 unsigned int val;
1827 int ret;
1828 int i;
1829 unsigned long mask;
1830
1831 ret = -EFAULT;
1832 memset(buffer, 0, sizeof(buffer));
1833 if (count > sizeof(buffer) - 1)
1834 count = sizeof(buffer) - 1;
1835 if (copy_from_user(buffer, buf, count))
1836 goto out_no_task;
1837
1838 ret = -EINVAL;
1839 val = (unsigned int)simple_strtoul(buffer, &end, 0);
1840 if (*end == '\n')
1841 end++;
1842 if (end - buffer == 0)
1843 goto out_no_task;
1844
1845 ret = -ESRCH;
1846 task = get_proc_task(file->f_dentry->d_inode);
1847 if (!task)
1848 goto out_no_task;
1849
1850 ret = end - buffer;
1851 mm = get_task_mm(task);
1852 if (!mm)
1853 goto out_no_mm;
1854
1855 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
1856 if (val & mask)
1857 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1858 else
1859 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1860 }
1861
1862 mmput(mm);
1863 out_no_mm:
1864 put_task_struct(task);
1865 out_no_task:
1866 return ret;
1867}
1868
1869static const struct file_operations proc_coredump_filter_operations = {
1870 .read = proc_coredump_filter_read,
1871 .write = proc_coredump_filter_write,
1872};
1873#endif
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875/*
1876 * /proc/self:
1877 */
1878static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1879 int buflen)
1880{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001881 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 sprintf(tmp, "%d", current->tgid);
1883 return vfs_readlink(dentry,buffer,buflen,tmp);
1884}
1885
Al Viro008b1502005-08-20 00:17:39 +01001886static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001888 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001890 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001891}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001893static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 .readlink = proc_self_readlink,
1895 .follow_link = proc_self_follow_link,
1896};
1897
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001898/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001899 * proc base
1900 *
1901 * These are the directory entries in the root directory of /proc
1902 * that properly belong to the /proc filesystem, as they describe
1903 * describe something that is process related.
1904 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07001905static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001906 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001907 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001908};
1909
1910/*
1911 * Exceptional case: normally we are not allowed to unhash a busy
1912 * directory. In this case, however, we can do it - no aliasing problems
1913 * due to the way we treat inodes.
1914 */
1915static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1916{
1917 struct inode *inode = dentry->d_inode;
1918 struct task_struct *task = get_proc_task(inode);
1919 if (task) {
1920 put_task_struct(task);
1921 return 1;
1922 }
1923 d_drop(dentry);
1924 return 0;
1925}
1926
1927static struct dentry_operations proc_base_dentry_operations =
1928{
1929 .d_revalidate = proc_base_revalidate,
1930 .d_delete = pid_delete_dentry,
1931};
1932
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001933static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001934 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001935{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001936 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001937 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001938 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001939 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001940
1941 /* Allocate the inode */
1942 error = ERR_PTR(-ENOMEM);
1943 inode = new_inode(dir->i_sb);
1944 if (!inode)
1945 goto out;
1946
1947 /* Initialize the inode */
1948 ei = PROC_I(inode);
1949 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001950
1951 /*
1952 * grab the reference to the task.
1953 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001954 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001955 if (!ei->pid)
1956 goto out_iput;
1957
1958 inode->i_uid = 0;
1959 inode->i_gid = 0;
1960 inode->i_mode = p->mode;
1961 if (S_ISDIR(inode->i_mode))
1962 inode->i_nlink = 2;
1963 if (S_ISLNK(inode->i_mode))
1964 inode->i_size = 64;
1965 if (p->iop)
1966 inode->i_op = p->iop;
1967 if (p->fop)
1968 inode->i_fop = p->fop;
1969 ei->op = p->op;
1970 dentry->d_op = &proc_base_dentry_operations;
1971 d_add(dentry, inode);
1972 error = NULL;
1973out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001974 return error;
1975out_iput:
1976 iput(inode);
1977 goto out;
1978}
1979
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001980static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1981{
1982 struct dentry *error;
1983 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001984 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001985
1986 error = ERR_PTR(-ENOENT);
1987
1988 if (!task)
1989 goto out_no_task;
1990
1991 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001992 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1993 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001994 if (p->len != dentry->d_name.len)
1995 continue;
1996 if (!memcmp(dentry->d_name.name, p->name, p->len))
1997 break;
1998 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001999 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002000 goto out;
2001
2002 error = proc_base_instantiate(dir, dentry, task, p);
2003
2004out:
2005 put_task_struct(task);
2006out_no_task:
2007 return error;
2008}
2009
Eric Dumazetc5141e62007-05-08 00:26:15 -07002010static int proc_base_fill_cache(struct file *filp, void *dirent,
2011 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002012{
2013 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2014 proc_base_instantiate, task, p);
2015}
2016
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002017#ifdef CONFIG_TASK_IO_ACCOUNTING
2018static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2019{
2020 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002021#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002022 "rchar: %llu\n"
2023 "wchar: %llu\n"
2024 "syscr: %llu\n"
2025 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002026#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002027 "read_bytes: %llu\n"
2028 "write_bytes: %llu\n"
2029 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002030#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002031 (unsigned long long)task->rchar,
2032 (unsigned long long)task->wchar,
2033 (unsigned long long)task->syscr,
2034 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002035#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002036 (unsigned long long)task->ioac.read_bytes,
2037 (unsigned long long)task->ioac.write_bytes,
2038 (unsigned long long)task->ioac.cancelled_write_bytes);
2039}
2040#endif
2041
Eric W. Biederman801199c2006-10-02 02:18:48 -07002042/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002043 * Thread groups
2044 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002045static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002046static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002047
Eric Dumazetc5141e62007-05-08 00:26:15 -07002048static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002049 DIR("task", S_IRUGO|S_IXUGO, task),
2050 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002051 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002052 INF("environ", S_IRUSR, pid_environ),
2053 INF("auxv", S_IRUSR, pid_auxv),
2054 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002055#ifdef CONFIG_SCHED_DEBUG
2056 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2057#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002058 INF("cmdline", S_IRUGO, pid_cmdline),
2059 INF("stat", S_IRUGO, tgid_stat),
2060 INF("statm", S_IRUGO, pid_statm),
2061 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002062#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002063 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002064#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002065 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002066 LNK("cwd", cwd),
2067 LNK("root", root),
2068 LNK("exe", exe),
2069 REG("mounts", S_IRUGO, mounts),
2070 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002071#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002072 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002073 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002074#endif
2075#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002076 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002077#endif
2078#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002079 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002080#endif
2081#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002082 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002083#endif
2084#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002085 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002086#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002087 INF("oom_score", S_IRUGO, oom_score),
2088 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002089#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002090 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002091#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002092#ifdef CONFIG_FAULT_INJECTION
2093 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2094#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002095#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2096 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2097#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002098#ifdef CONFIG_TASK_IO_ACCOUNTING
2099 INF("io", S_IRUGO, pid_io_accounting),
2100#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002101};
2102
2103static int proc_tgid_base_readdir(struct file * filp,
2104 void * dirent, filldir_t filldir)
2105{
2106 return proc_pident_readdir(filp,dirent,filldir,
2107 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2108}
2109
Arjan van de Ven00977a52007-02-12 00:55:34 -08002110static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002111 .read = generic_read_dir,
2112 .readdir = proc_tgid_base_readdir,
2113};
2114
2115static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002116 return proc_pident_lookup(dir, dentry,
2117 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002118}
2119
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002120static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002121 .lookup = proc_tgid_base_lookup,
2122 .getattr = pid_getattr,
2123 .setattr = proc_setattr,
2124};
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07002127 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002129 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002131 * Looks in the dcache for
2132 * /proc/@pid
2133 * /proc/@tgid/task/@pid
2134 * if either directory is present flushes it and all of it'ts children
2135 * from the dcache.
2136 *
2137 * It is safe and reasonable to cache /proc entries for a task until
2138 * that task exits. After that they just clog up the dcache with
2139 * useless entries, possibly causing useful dcache entries to be
2140 * flushed instead. This routine is proved to flush those useless
2141 * dcache entries at process exit time.
2142 *
2143 * NOTE: This routine is just an optimization so it does not guarantee
2144 * that no dcache entries will exist at process exit time it
2145 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07002147void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002149 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002150 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002151 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Eric W. Biederman48e64842006-06-26 00:25:48 -07002153 name.name = buf;
2154 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2155 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2156 if (dentry) {
2157 shrink_dcache_parent(dentry);
2158 d_drop(dentry);
2159 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Eric W. Biederman48e64842006-06-26 00:25:48 -07002162 if (thread_group_leader(task))
2163 goto out;
2164
2165 name.name = buf;
2166 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2167 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2168 if (!leader)
2169 goto out;
2170
2171 name.name = "task";
2172 name.len = strlen(name.name);
2173 dir = d_hash_and_lookup(leader, &name);
2174 if (!dir)
2175 goto out_put_leader;
2176
2177 name.name = buf;
2178 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2179 dentry = d_hash_and_lookup(dir, &name);
2180 if (dentry) {
2181 shrink_dcache_parent(dentry);
2182 d_drop(dentry);
2183 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002185
2186 dput(dir);
2187out_put_leader:
2188 dput(leader);
2189out:
2190 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
Adrian Bunk9711ef92006-12-06 20:38:31 -08002193static struct dentry *proc_pid_instantiate(struct inode *dir,
2194 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002195 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002196{
2197 struct dentry *error = ERR_PTR(-ENOENT);
2198 struct inode *inode;
2199
Eric W. Biederman61a28782006-10-02 02:18:49 -07002200 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002201 if (!inode)
2202 goto out;
2203
2204 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2205 inode->i_op = &proc_tgid_base_inode_operations;
2206 inode->i_fop = &proc_tgid_base_operations;
2207 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002208 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002209#ifdef CONFIG_SECURITY
2210 inode->i_nlink += 1;
2211#endif
2212
2213 dentry->d_op = &pid_dentry_operations;
2214
2215 d_add(dentry, inode);
2216 /* Close the race of the process dying before we return the dentry */
2217 if (pid_revalidate(dentry, NULL))
2218 error = NULL;
2219out:
2220 return error;
2221}
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2224{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002225 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Eric W. Biederman801199c2006-10-02 02:18:48 -07002229 result = proc_base_lookup(dir, dentry);
2230 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2231 goto out;
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 tgid = name_to_int(dentry);
2234 if (tgid == ~0U)
2235 goto out;
2236
Eric W. Biedermande758732006-06-26 00:25:51 -07002237 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 task = find_task_by_pid(tgid);
2239 if (task)
2240 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002241 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (!task)
2243 goto out;
2244
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002245 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002246 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002248 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002252 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002253 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002255static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002257 struct task_struct *task;
2258 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002260 rcu_read_lock();
2261retry:
2262 task = NULL;
2263 pid = find_ge_pid(tgid);
2264 if (pid) {
2265 tgid = pid->nr + 1;
2266 task = pid_task(pid, PIDTYPE_PID);
2267 /* What we to know is if the pid we have find is the
2268 * pid of a thread_group_leader. Testing for task
2269 * being a thread_group_leader is the obvious thing
2270 * todo but there is a window when it fails, due to
2271 * the pid transfer logic in de_thread.
2272 *
2273 * So we perform the straight forward test of seeing
2274 * if the pid we have found is the pid of a thread
2275 * group leader, and don't worry if the task we have
2276 * found doesn't happen to be a thread group leader.
2277 * As we don't care in the case of readdir.
2278 */
2279 if (!task || !has_group_leader_pid(task))
2280 goto retry;
2281 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002283 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002284 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002287#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Eric W. Biederman61a28782006-10-02 02:18:49 -07002289static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2290 struct task_struct *task, int tgid)
2291{
2292 char name[PROC_NUMBUF];
2293 int len = snprintf(name, sizeof(name), "%d", tgid);
2294 return proc_fill_cache(filp, dirent, filldir, name, len,
2295 proc_pid_instantiate, task, NULL);
2296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298/* for the /proc/ directory itself, after non-process stuff has been done */
2299int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2300{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002302 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002303 struct task_struct *task;
2304 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Eric W. Biederman61a28782006-10-02 02:18:49 -07002306 if (!reaper)
2307 goto out_no_task;
2308
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002309 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002310 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002311 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
2314
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002315 tgid = filp->f_pos - TGID_OFFSET;
2316 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002317 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002318 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002319 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002320 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002321 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002322 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002323 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002326 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2327out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002328 put_task_struct(reaper);
2329out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return 0;
2331}
2332
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002333/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002334 * Tasks
2335 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002336static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002337 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002338 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002339 INF("environ", S_IRUSR, pid_environ),
2340 INF("auxv", S_IRUSR, pid_auxv),
2341 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002342#ifdef CONFIG_SCHED_DEBUG
2343 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2344#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002345 INF("cmdline", S_IRUGO, pid_cmdline),
2346 INF("stat", S_IRUGO, tid_stat),
2347 INF("statm", S_IRUGO, pid_statm),
2348 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002349#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002350 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002351#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002352 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002353 LNK("cwd", cwd),
2354 LNK("root", root),
2355 LNK("exe", exe),
2356 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002357#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002358 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002359 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002360#endif
2361#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002362 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002363#endif
2364#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002365 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002366#endif
2367#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002368 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002369#endif
2370#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002371 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002372#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002373 INF("oom_score", S_IRUGO, oom_score),
2374 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002375#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002376 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002377#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002378#ifdef CONFIG_FAULT_INJECTION
2379 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2380#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002381};
2382
2383static int proc_tid_base_readdir(struct file * filp,
2384 void * dirent, filldir_t filldir)
2385{
2386 return proc_pident_readdir(filp,dirent,filldir,
2387 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2388}
2389
2390static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002391 return proc_pident_lookup(dir, dentry,
2392 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002393}
2394
Arjan van de Ven00977a52007-02-12 00:55:34 -08002395static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002396 .read = generic_read_dir,
2397 .readdir = proc_tid_base_readdir,
2398};
2399
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002400static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002401 .lookup = proc_tid_base_lookup,
2402 .getattr = pid_getattr,
2403 .setattr = proc_setattr,
2404};
2405
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002406static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002407 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002408{
2409 struct dentry *error = ERR_PTR(-ENOENT);
2410 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002411 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002412
2413 if (!inode)
2414 goto out;
2415 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2416 inode->i_op = &proc_tid_base_inode_operations;
2417 inode->i_fop = &proc_tid_base_operations;
2418 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002419 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002420#ifdef CONFIG_SECURITY
2421 inode->i_nlink += 1;
2422#endif
2423
2424 dentry->d_op = &pid_dentry_operations;
2425
2426 d_add(dentry, inode);
2427 /* Close the race of the process dying before we return the dentry */
2428 if (pid_revalidate(dentry, NULL))
2429 error = NULL;
2430out:
2431 return error;
2432}
2433
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002434static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2435{
2436 struct dentry *result = ERR_PTR(-ENOENT);
2437 struct task_struct *task;
2438 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002439 unsigned tid;
2440
2441 if (!leader)
2442 goto out_no_task;
2443
2444 tid = name_to_int(dentry);
2445 if (tid == ~0U)
2446 goto out;
2447
2448 rcu_read_lock();
2449 task = find_task_by_pid(tid);
2450 if (task)
2451 get_task_struct(task);
2452 rcu_read_unlock();
2453 if (!task)
2454 goto out;
2455 if (leader->tgid != task->tgid)
2456 goto out_drop_task;
2457
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002458 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002459out_drop_task:
2460 put_task_struct(task);
2461out:
2462 put_task_struct(leader);
2463out_no_task:
2464 return result;
2465}
2466
2467/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002468 * Find the first tid of a thread group to return to user space.
2469 *
2470 * Usually this is just the thread group leader, but if the users
2471 * buffer was too small or there was a seek into the middle of the
2472 * directory we have more work todo.
2473 *
2474 * In the case of a short read we start with find_task_by_pid.
2475 *
2476 * In the case of a seek we start with the leader and walk nr
2477 * threads past it.
2478 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002479static struct task_struct *first_tid(struct task_struct *leader,
2480 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002481{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002482 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002483
Eric W. Biedermancc288732006-06-26 00:26:01 -07002484 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002485 /* Attempt to start with the pid of a thread */
2486 if (tid && (nr > 0)) {
2487 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002488 if (pos && (pos->group_leader == leader))
2489 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002490 }
2491
2492 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002493 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002494 if (nr && nr >= get_nr_threads(leader))
2495 goto out;
2496
2497 /* If we haven't found our starting place yet start
2498 * with the leader and walk nr threads forward.
2499 */
2500 for (pos = leader; nr > 0; --nr) {
2501 pos = next_thread(pos);
2502 if (pos == leader) {
2503 pos = NULL;
2504 goto out;
2505 }
2506 }
2507found:
2508 get_task_struct(pos);
2509out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002510 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002511 return pos;
2512}
2513
2514/*
2515 * Find the next thread in the thread list.
2516 * Return NULL if there is an error or no next thread.
2517 *
2518 * The reference to the input task_struct is released.
2519 */
2520static struct task_struct *next_tid(struct task_struct *start)
2521{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002522 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002523 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002524 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002525 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002526 if (thread_group_leader(pos))
2527 pos = NULL;
2528 else
2529 get_task_struct(pos);
2530 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002531 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002532 put_task_struct(start);
2533 return pos;
2534}
2535
Eric W. Biederman61a28782006-10-02 02:18:49 -07002536static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2537 struct task_struct *task, int tid)
2538{
2539 char name[PROC_NUMBUF];
2540 int len = snprintf(name, sizeof(name), "%d", tid);
2541 return proc_fill_cache(filp, dirent, filldir, name, len,
2542 proc_task_instantiate, task, NULL);
2543}
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545/* for the /proc/TGID/task/ directories */
2546static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2547{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002548 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002550 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002551 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 int retval = -ENOENT;
2553 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002554 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2556
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002557 task = get_proc_task(inode);
2558 if (!task)
2559 goto out_no_task;
2560 rcu_read_lock();
2561 if (pid_alive(task)) {
2562 leader = task->group_leader;
2563 get_task_struct(leader);
2564 }
2565 rcu_read_unlock();
2566 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002567 if (!leader)
2568 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 retval = 0;
2570
2571 switch (pos) {
2572 case 0:
2573 ino = inode->i_ino;
2574 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2575 goto out;
2576 pos++;
2577 /* fall through */
2578 case 1:
2579 ino = parent_ino(dentry);
2580 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2581 goto out;
2582 pos++;
2583 /* fall through */
2584 }
2585
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002586 /* f_version caches the tgid value that the last readdir call couldn't
2587 * return. lseek aka telldir automagically resets f_version to 0.
2588 */
2589 tid = filp->f_version;
2590 filp->f_version = 0;
2591 for (task = first_tid(leader, tid, pos - 2);
2592 task;
2593 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002594 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002595 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002596 /* returning this tgid failed, save it as the first
2597 * pid for the next readir call */
2598 filp->f_version = tid;
2599 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603out:
2604 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002605 put_task_struct(leader);
2606out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return retval;
2608}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002609
2610static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2611{
2612 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002613 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002614 generic_fillattr(inode, stat);
2615
Eric W. Biederman99f89552006-06-26 00:25:55 -07002616 if (p) {
2617 rcu_read_lock();
2618 stat->nlink += get_nr_threads(p);
2619 rcu_read_unlock();
2620 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002621 }
2622
2623 return 0;
2624}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002625
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002626static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002627 .lookup = proc_task_lookup,
2628 .getattr = proc_task_getattr,
2629 .setattr = proc_setattr,
2630};
2631
Arjan van de Ven00977a52007-02-12 00:55:34 -08002632static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002633 .read = generic_read_dir,
2634 .readdir = proc_task_readdir,
2635};