blob: 42cb4f5613b6f43c5eeb386718b02352f3539d4a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include "internal.h"
76
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070077/* NOTE:
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
82 *
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
85 */
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Eric W. Biederman8578cea2006-06-26 00:25:54 -070088/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070089#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070093 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080095 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080096 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070097 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Eric W. Biederman61a28782006-10-02 02:18:49 -0700100#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700101 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700102 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .mode = MODE, \
104 .iop = IOP, \
105 .fop = FOP, \
106 .op = OP, \
107}
108
Eric W. Biederman61a28782006-10-02 02:18:49 -0700109#define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
112 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700113#define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700117#define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Kees Cook5096add2007-05-08 00:26:04 -0700125int maps_protect;
126EXPORT_SYMBOL(maps_protect);
127
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700128static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700131 task_lock(task);
132 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if(fs)
134 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700135 task_unlock(task);
136 return fs;
137}
138
Eric W. Biederman99f89552006-06-26 00:25:55 -0700139static int get_nr_threads(struct task_struct *tsk)
140{
141 /* Must be called with the rcu_read_lock held */
142 unsigned long flags;
143 int count = 0;
144
145 if (lock_task_sighand(tsk, &flags)) {
146 count = atomic_read(&tsk->signal->count);
147 unlock_task_sighand(tsk, &flags);
148 }
149 return count;
150}
151
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700152static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
153{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700154 struct task_struct *task = get_proc_task(inode);
155 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700156 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700157
158 if (task) {
159 fs = get_fs_struct(task);
160 put_task_struct(task);
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (fs) {
163 read_lock(&fs->lock);
164 *mnt = mntget(fs->pwdmnt);
165 *dentry = dget(fs->pwd);
166 read_unlock(&fs->lock);
167 result = 0;
168 put_fs_struct(fs);
169 }
170 return result;
171}
172
173static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
174{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700175 struct task_struct *task = get_proc_task(inode);
176 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700178
179 if (task) {
180 fs = get_fs_struct(task);
181 put_task_struct(task);
182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (fs) {
184 read_lock(&fs->lock);
185 *mnt = mntget(fs->rootmnt);
186 *dentry = dget(fs->root);
187 read_unlock(&fs->lock);
188 result = 0;
189 put_fs_struct(fs);
190 }
191 return result;
192}
193
194#define MAY_PTRACE(task) \
195 (task == current || \
196 (task->parent == current && \
197 (task->ptrace & PT_PTRACED) && \
198 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
199 security_ptrace(current,task) == 0))
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int proc_pid_environ(struct task_struct *task, char * buffer)
202{
203 int res = 0;
204 struct mm_struct *mm = get_task_mm(task);
205 if (mm) {
Alexey Dobriyanda58a162007-07-15 23:40:21 -0700206 unsigned int len;
207
208 res = -ESRCH;
209 if (!ptrace_may_attach(task))
210 goto out;
211
212 len = mm->env_end - mm->env_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (len > PAGE_SIZE)
214 len = PAGE_SIZE;
215 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Alexey Dobriyanda58a162007-07-15 23:40:21 -0700216out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 mmput(mm);
218 }
219 return res;
220}
221
222static int proc_pid_cmdline(struct task_struct *task, char * buffer)
223{
224 int res = 0;
225 unsigned int len;
226 struct mm_struct *mm = get_task_mm(task);
227 if (!mm)
228 goto out;
229 if (!mm->arg_end)
230 goto out_mm; /* Shh! No looking before we're done */
231
232 len = mm->arg_end - mm->arg_start;
233
234 if (len > PAGE_SIZE)
235 len = PAGE_SIZE;
236
237 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
238
239 // If the nul at the end of args has been overwritten, then
240 // assume application is using setproctitle(3).
241 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
242 len = strnlen(buffer, res);
243 if (len < res) {
244 res = len;
245 } else {
246 len = mm->env_end - mm->env_start;
247 if (len > PAGE_SIZE - res)
248 len = PAGE_SIZE - res;
249 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
250 res = strnlen(buffer, res);
251 }
252 }
253out_mm:
254 mmput(mm);
255out:
256 return res;
257}
258
259static int proc_pid_auxv(struct task_struct *task, char *buffer)
260{
261 int res = 0;
262 struct mm_struct *mm = get_task_mm(task);
263 if (mm) {
264 unsigned int nwords = 0;
265 do
266 nwords += 2;
267 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
268 res = nwords * sizeof(mm->saved_auxv[0]);
269 if (res > PAGE_SIZE)
270 res = PAGE_SIZE;
271 memcpy(buffer, mm->saved_auxv, res);
272 mmput(mm);
273 }
274 return res;
275}
276
277
278#ifdef CONFIG_KALLSYMS
279/*
280 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
281 * Returns the resolved symbol. If that fails, simply return the address.
282 */
283static int proc_pid_wchan(struct task_struct *task, char *buffer)
284{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700285 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700286 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 wchan = get_wchan(task);
289
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700290 if (lookup_symbol_name(wchan, symname) < 0)
291 return sprintf(buffer, "%lu", wchan);
292 else
293 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295#endif /* CONFIG_KALLSYMS */
296
297#ifdef CONFIG_SCHEDSTATS
298/*
299 * Provides /proc/PID/schedstat
300 */
301static int proc_pid_schedstat(struct task_struct *task, char *buffer)
302{
Balbir Singh172ba842007-07-09 18:52:00 +0200303 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 task->sched_info.cpu_time,
305 task->sched_info.run_delay,
306 task->sched_info.pcnt);
307}
308#endif
309
310/* The badness from the OOM killer */
311unsigned long badness(struct task_struct *p, unsigned long uptime);
312static int proc_oom_score(struct task_struct *task, char *buffer)
313{
314 unsigned long points;
315 struct timespec uptime;
316
317 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700318 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700320 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return sprintf(buffer, "%lu\n", points);
322}
323
324/************************************************************************/
325/* Here the fs part begins */
326/************************************************************************/
327
328/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700329static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700331 struct task_struct *task;
332 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700333 /* Allow access to a task's file descriptors if it is us or we
334 * may use ptrace attach to the process and find out that
335 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700336 */
337 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700338 if (task) {
339 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700340 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700341 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700342 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700345static int proc_setattr(struct dentry *dentry, struct iattr *attr)
346{
347 int error;
348 struct inode *inode = dentry->d_inode;
349
350 if (attr->ia_valid & ATTR_MODE)
351 return -EPERM;
352
353 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700354 if (!error)
355 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700356 return error;
357}
358
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800359static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700360 .setattr = proc_setattr,
361};
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500364struct proc_mounts {
365 struct seq_file m;
366 int event;
367};
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static int mounts_open(struct inode *inode, struct file *file)
370{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700371 struct task_struct *task = get_proc_task(inode);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800372 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500373 struct proc_mounts *p;
374 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Eric W. Biederman99f89552006-06-26 00:25:55 -0700376 if (task) {
377 task_lock(task);
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800378 if (task->nsproxy) {
379 ns = task->nsproxy->mnt_ns;
380 if (ns)
381 get_mnt_ns(ns);
382 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700383 task_unlock(task);
384 put_task_struct(task);
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800387 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500388 ret = -ENOMEM;
389 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
390 if (p) {
391 file->private_data = &p->m;
392 ret = seq_open(file, &mounts_op);
393 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800394 p->m.private = ns;
395 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500396 return 0;
397 }
398 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800400 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 return ret;
403}
404
405static int mounts_release(struct inode *inode, struct file *file)
406{
407 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800408 struct mnt_namespace *ns = m->private;
409 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return seq_release(inode, file);
411}
412
Al Viro5addc5d2005-11-07 17:15:49 -0500413static unsigned mounts_poll(struct file *file, poll_table *wait)
414{
415 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800416 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500417 unsigned res = 0;
418
419 poll_wait(file, &ns->poll, wait);
420
421 spin_lock(&vfsmount_lock);
422 if (p->event != ns->event) {
423 p->event = ns->event;
424 res = POLLERR;
425 }
426 spin_unlock(&vfsmount_lock);
427
428 return res;
429}
430
Arjan van de Ven00977a52007-02-12 00:55:34 -0800431static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 .open = mounts_open,
433 .read = seq_read,
434 .llseek = seq_lseek,
435 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500436 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437};
438
Chuck Leverb4629fe2006-03-20 13:44:12 -0500439extern struct seq_operations mountstats_op;
440static int mountstats_open(struct inode *inode, struct file *file)
441{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500442 int ret = seq_open(file, &mountstats_op);
443
444 if (!ret) {
445 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800446 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700447 struct task_struct *task = get_proc_task(inode);
448
449 if (task) {
450 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800451 if (task->nsproxy)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800452 mnt_ns = task->nsproxy->mnt_ns;
453 if (mnt_ns)
454 get_mnt_ns(mnt_ns);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700455 task_unlock(task);
456 put_task_struct(task);
457 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500458
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800459 if (mnt_ns)
460 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500461 else {
462 seq_release(inode, file);
463 ret = -EINVAL;
464 }
465 }
466 return ret;
467}
468
Arjan van de Ven00977a52007-02-12 00:55:34 -0800469static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500470 .open = mountstats_open,
471 .read = seq_read,
472 .llseek = seq_lseek,
473 .release = mounts_release,
474};
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
477
478static ssize_t proc_info_read(struct file * file, char __user * buf,
479 size_t count, loff_t *ppos)
480{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800481 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 unsigned long page;
483 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700484 struct task_struct *task = get_proc_task(inode);
485
486 length = -ESRCH;
487 if (!task)
488 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (count > PROC_BLOCK_SIZE)
491 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700492
493 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700495 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 length = PROC_I(inode)->op.proc_read(task, (char*)page);
498
499 if (length >= 0)
500 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
501 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700502out:
503 put_task_struct(task);
504out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return length;
506}
507
Arjan van de Ven00977a52007-02-12 00:55:34 -0800508static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 .read = proc_info_read,
510};
511
512static int mem_open(struct inode* inode, struct file* file)
513{
514 file->private_data = (void*)((long)current->self_exec_id);
515 return 0;
516}
517
518static ssize_t mem_read(struct file * file, char __user * buf,
519 size_t count, loff_t *ppos)
520{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800521 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 char *page;
523 unsigned long src = *ppos;
524 int ret = -ESRCH;
525 struct mm_struct *mm;
526
Eric W. Biederman99f89552006-06-26 00:25:55 -0700527 if (!task)
528 goto out_no_task;
529
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700530 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto out;
532
533 ret = -ENOMEM;
534 page = (char *)__get_free_page(GFP_USER);
535 if (!page)
536 goto out;
537
538 ret = 0;
539
540 mm = get_task_mm(task);
541 if (!mm)
542 goto out_free;
543
544 ret = -EIO;
545
546 if (file->private_data != (void*)((long)current->self_exec_id))
547 goto out_put;
548
549 ret = 0;
550
551 while (count > 0) {
552 int this_len, retval;
553
554 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
555 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700556 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (!ret)
558 ret = -EIO;
559 break;
560 }
561
562 if (copy_to_user(buf, page, retval)) {
563 ret = -EFAULT;
564 break;
565 }
566
567 ret += retval;
568 src += retval;
569 buf += retval;
570 count -= retval;
571 }
572 *ppos = src;
573
574out_put:
575 mmput(mm);
576out_free:
577 free_page((unsigned long) page);
578out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700579 put_task_struct(task);
580out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return ret;
582}
583
584#define mem_write NULL
585
586#ifndef mem_write
587/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800588static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 size_t count, loff_t *ppos)
590{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700591 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800593 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 unsigned long dst = *ppos;
595
Eric W. Biederman99f89552006-06-26 00:25:55 -0700596 copied = -ESRCH;
597 if (!task)
598 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Eric W. Biederman99f89552006-06-26 00:25:55 -0700600 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
601 goto out;
602
603 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 page = (char *)__get_free_page(GFP_USER);
605 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700606 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700608 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 while (count > 0) {
610 int this_len, retval;
611
612 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
613 if (copy_from_user(page, buf, this_len)) {
614 copied = -EFAULT;
615 break;
616 }
617 retval = access_process_vm(task, dst, page, this_len, 1);
618 if (!retval) {
619 if (!copied)
620 copied = -EIO;
621 break;
622 }
623 copied += retval;
624 buf += retval;
625 dst += retval;
626 count -= retval;
627 }
628 *ppos = dst;
629 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700630out:
631 put_task_struct(task);
632out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return copied;
634}
635#endif
636
637static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
638{
639 switch (orig) {
640 case 0:
641 file->f_pos = offset;
642 break;
643 case 1:
644 file->f_pos += offset;
645 break;
646 default:
647 return -EINVAL;
648 }
649 force_successful_syscall_return();
650 return file->f_pos;
651}
652
Arjan van de Ven00977a52007-02-12 00:55:34 -0800653static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 .llseek = mem_lseek,
655 .read = mem_read,
656 .write = mem_write,
657 .open = mem_open,
658};
659
660static ssize_t oom_adjust_read(struct file *file, char __user *buf,
661 size_t count, loff_t *ppos)
662{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800663 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700664 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700666 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Eric W. Biederman99f89552006-06-26 00:25:55 -0700668 if (!task)
669 return -ESRCH;
670 oom_adjust = task->oomkilladj;
671 put_task_struct(task);
672
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700673 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700674
675 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
679 size_t count, loff_t *ppos)
680{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700681 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700682 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int oom_adjust;
684
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700685 memset(buffer, 0, sizeof(buffer));
686 if (count > sizeof(buffer) - 1)
687 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (copy_from_user(buffer, buf, count))
689 return -EFAULT;
690 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700691 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
692 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EINVAL;
694 if (*end == '\n')
695 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800696 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700697 if (!task)
698 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800699 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
700 put_task_struct(task);
701 return -EACCES;
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700704 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (end - buffer == 0)
706 return -EIO;
707 return end - buffer;
708}
709
Arjan van de Ven00977a52007-02-12 00:55:34 -0800710static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 .read = oom_adjust_read,
712 .write = oom_adjust_write,
713};
714
David Rientjes4b8df892007-05-08 20:27:01 -0700715#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -0700716static ssize_t clear_refs_write(struct file *file, const char __user *buf,
717 size_t count, loff_t *ppos)
718{
719 struct task_struct *task;
720 char buffer[PROC_NUMBUF], *end;
721 struct mm_struct *mm;
722
723 memset(buffer, 0, sizeof(buffer));
724 if (count > sizeof(buffer) - 1)
725 count = sizeof(buffer) - 1;
726 if (copy_from_user(buffer, buf, count))
727 return -EFAULT;
728 if (!simple_strtol(buffer, &end, 0))
729 return -EINVAL;
730 if (*end == '\n')
731 end++;
732 task = get_proc_task(file->f_path.dentry->d_inode);
733 if (!task)
734 return -ESRCH;
735 mm = get_task_mm(task);
736 if (mm) {
737 clear_refs_smap(mm);
738 mmput(mm);
739 }
740 put_task_struct(task);
741 if (end - buffer == 0)
742 return -EIO;
743 return end - buffer;
744}
745
746static struct file_operations proc_clear_refs_operations = {
747 .write = clear_refs_write,
748};
David Rientjes4b8df892007-05-08 20:27:01 -0700749#endif
David Rientjesb813e932007-05-06 14:49:24 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#ifdef CONFIG_AUDITSYSCALL
752#define TMPBUFLEN 21
753static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
754 size_t count, loff_t *ppos)
755{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800756 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700757 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 ssize_t length;
759 char tmpbuf[TMPBUFLEN];
760
Eric W. Biederman99f89552006-06-26 00:25:55 -0700761 if (!task)
762 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
764 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700765 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
767}
768
769static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
770 size_t count, loff_t *ppos)
771{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800772 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 char *page, *tmp;
774 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 uid_t loginuid;
776
777 if (!capable(CAP_AUDIT_CONTROL))
778 return -EPERM;
779
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700780 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -EPERM;
782
Al Viroe0182902006-05-18 08:28:02 -0400783 if (count >= PAGE_SIZE)
784 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 if (*ppos != 0) {
787 /* No partial writes. */
788 return -EINVAL;
789 }
790 page = (char*)__get_free_page(GFP_USER);
791 if (!page)
792 return -ENOMEM;
793 length = -EFAULT;
794 if (copy_from_user(page, buf, count))
795 goto out_free_page;
796
Al Viroe0182902006-05-18 08:28:02 -0400797 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 loginuid = simple_strtoul(page, &tmp, 10);
799 if (tmp == page) {
800 length = -EINVAL;
801 goto out_free_page;
802
803 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700804 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (likely(length == 0))
806 length = count;
807
808out_free_page:
809 free_page((unsigned long) page);
810 return length;
811}
812
Arjan van de Ven00977a52007-02-12 00:55:34 -0800813static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 .read = proc_loginuid_read,
815 .write = proc_loginuid_write,
816};
817#endif
818
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800819#ifdef CONFIG_FAULT_INJECTION
820static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
821 size_t count, loff_t *ppos)
822{
823 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
824 char buffer[PROC_NUMBUF];
825 size_t len;
826 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800827
828 if (!task)
829 return -ESRCH;
830 make_it_fail = task->make_it_fail;
831 put_task_struct(task);
832
833 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700834
835 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800836}
837
838static ssize_t proc_fault_inject_write(struct file * file,
839 const char __user * buf, size_t count, loff_t *ppos)
840{
841 struct task_struct *task;
842 char buffer[PROC_NUMBUF], *end;
843 int make_it_fail;
844
845 if (!capable(CAP_SYS_RESOURCE))
846 return -EPERM;
847 memset(buffer, 0, sizeof(buffer));
848 if (count > sizeof(buffer) - 1)
849 count = sizeof(buffer) - 1;
850 if (copy_from_user(buffer, buf, count))
851 return -EFAULT;
852 make_it_fail = simple_strtol(buffer, &end, 0);
853 if (*end == '\n')
854 end++;
855 task = get_proc_task(file->f_dentry->d_inode);
856 if (!task)
857 return -ESRCH;
858 task->make_it_fail = make_it_fail;
859 put_task_struct(task);
860 if (end - buffer == 0)
861 return -EIO;
862 return end - buffer;
863}
864
Arjan van de Ven00977a52007-02-12 00:55:34 -0800865static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800866 .read = proc_fault_inject_read,
867 .write = proc_fault_inject_write,
868};
869#endif
870
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200871#ifdef CONFIG_SCHED_DEBUG
872/*
873 * Print out various scheduling related per-task fields:
874 */
875static int sched_show(struct seq_file *m, void *v)
876{
877 struct inode *inode = m->private;
878 struct task_struct *p;
879
880 WARN_ON(!inode);
881
882 p = get_proc_task(inode);
883 if (!p)
884 return -ESRCH;
885 proc_sched_show_task(p, m);
886
887 put_task_struct(p);
888
889 return 0;
890}
891
892static ssize_t
893sched_write(struct file *file, const char __user *buf,
894 size_t count, loff_t *offset)
895{
896 struct inode *inode = file->f_path.dentry->d_inode;
897 struct task_struct *p;
898
899 WARN_ON(!inode);
900
901 p = get_proc_task(inode);
902 if (!p)
903 return -ESRCH;
904 proc_sched_set_task(p);
905
906 put_task_struct(p);
907
908 return count;
909}
910
911static int sched_open(struct inode *inode, struct file *filp)
912{
913 int ret;
914
915 ret = single_open(filp, sched_show, NULL);
916 if (!ret) {
917 struct seq_file *m = filp->private_data;
918
919 m->private = inode;
920 }
921 return ret;
922}
923
924static const struct file_operations proc_pid_sched_operations = {
925 .open = sched_open,
926 .read = seq_read,
927 .write = sched_write,
928 .llseek = seq_lseek,
929 .release = seq_release,
930};
931
932#endif
933
Al Viro008b1502005-08-20 00:17:39 +0100934static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct inode *inode = dentry->d_inode;
937 int error = -EACCES;
938
939 /* We don't need a base pointer in the /proc filesystem */
940 path_release(nd);
941
Eric W. Biederman778c1142006-06-26 00:25:58 -0700942 /* Are we allowed to snoop on the tasks file descriptors? */
943 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
947 nd->last_type = LAST_BIND;
948out:
Al Viro008b1502005-08-20 00:17:39 +0100949 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
953 char __user *buffer, int buflen)
954{
955 struct inode * inode;
956 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
957 int len;
958
959 if (!tmp)
960 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -0700961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 inode = dentry->d_inode;
963 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
964 len = PTR_ERR(path);
965 if (IS_ERR(path))
966 goto out;
967 len = tmp + PAGE_SIZE - 1 - path;
968
969 if (len > buflen)
970 len = buflen;
971 if (copy_to_user(buffer, path, len))
972 len = -EFAULT;
973 out:
974 free_page((unsigned long)tmp);
975 return len;
976}
977
978static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
979{
980 int error = -EACCES;
981 struct inode *inode = dentry->d_inode;
982 struct dentry *de;
983 struct vfsmount *mnt = NULL;
984
Eric W. Biederman778c1142006-06-26 00:25:58 -0700985 /* Are we allowed to snoop on the tasks file descriptors? */
986 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
990 if (error)
991 goto out;
992
993 error = do_proc_readlink(de, mnt, buffer, buflen);
994 dput(de);
995 mntput(mnt);
996out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return error;
998}
999
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001000static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001002 .follow_link = proc_pid_follow_link,
1003 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004};
1005
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001006
1007/* building an inode */
1008
1009static int task_dumpable(struct task_struct *task)
1010{
1011 int dumpable = 0;
1012 struct mm_struct *mm;
1013
1014 task_lock(task);
1015 mm = task->mm;
1016 if (mm)
1017 dumpable = mm->dumpable;
1018 task_unlock(task);
1019 if(dumpable == 1)
1020 return 1;
1021 return 0;
1022}
1023
1024
Eric W. Biederman61a28782006-10-02 02:18:49 -07001025static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001026{
1027 struct inode * inode;
1028 struct proc_inode *ei;
1029
1030 /* We need a new inode */
1031
1032 inode = new_inode(sb);
1033 if (!inode)
1034 goto out;
1035
1036 /* Common stuff */
1037 ei = PROC_I(inode);
1038 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001039 inode->i_op = &proc_def_inode_operations;
1040
1041 /*
1042 * grab the reference to task.
1043 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001044 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001045 if (!ei->pid)
1046 goto out_unlock;
1047
1048 inode->i_uid = 0;
1049 inode->i_gid = 0;
1050 if (task_dumpable(task)) {
1051 inode->i_uid = task->euid;
1052 inode->i_gid = task->egid;
1053 }
1054 security_task_to_inode(task, inode);
1055
1056out:
1057 return inode;
1058
1059out_unlock:
1060 iput(inode);
1061 return NULL;
1062}
1063
1064static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1065{
1066 struct inode *inode = dentry->d_inode;
1067 struct task_struct *task;
1068 generic_fillattr(inode, stat);
1069
1070 rcu_read_lock();
1071 stat->uid = 0;
1072 stat->gid = 0;
1073 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1074 if (task) {
1075 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1076 task_dumpable(task)) {
1077 stat->uid = task->euid;
1078 stat->gid = task->egid;
1079 }
1080 }
1081 rcu_read_unlock();
1082 return 0;
1083}
1084
1085/* dentry stuff */
1086
1087/*
1088 * Exceptional case: normally we are not allowed to unhash a busy
1089 * directory. In this case, however, we can do it - no aliasing problems
1090 * due to the way we treat inodes.
1091 *
1092 * Rewrite the inode's ownerships here because the owning task may have
1093 * performed a setuid(), etc.
1094 *
1095 * Before the /proc/pid/status file was created the only way to read
1096 * the effective uid of a /process was to stat /proc/pid. Reading
1097 * /proc/pid/status is slow enough that procps and other packages
1098 * kept stating /proc/pid. To keep the rules in /proc simple I have
1099 * made this apply to all per process world readable and executable
1100 * directories.
1101 */
1102static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1103{
1104 struct inode *inode = dentry->d_inode;
1105 struct task_struct *task = get_proc_task(inode);
1106 if (task) {
1107 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1108 task_dumpable(task)) {
1109 inode->i_uid = task->euid;
1110 inode->i_gid = task->egid;
1111 } else {
1112 inode->i_uid = 0;
1113 inode->i_gid = 0;
1114 }
1115 inode->i_mode &= ~(S_ISUID | S_ISGID);
1116 security_task_to_inode(task, inode);
1117 put_task_struct(task);
1118 return 1;
1119 }
1120 d_drop(dentry);
1121 return 0;
1122}
1123
1124static int pid_delete_dentry(struct dentry * dentry)
1125{
1126 /* Is the task we represent dead?
1127 * If so, then don't put the dentry on the lru list,
1128 * kill it immediately.
1129 */
1130 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1131}
1132
1133static struct dentry_operations pid_dentry_operations =
1134{
1135 .d_revalidate = pid_revalidate,
1136 .d_delete = pid_delete_dentry,
1137};
1138
1139/* Lookups */
1140
Eric Dumazetc5141e62007-05-08 00:26:15 -07001141typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1142 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001143
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001144/*
1145 * Fill a directory entry.
1146 *
1147 * If possible create the dcache entry and derive our inode number and
1148 * file type from dcache entry.
1149 *
1150 * Since all of the proc inode numbers are dynamically generated, the inode
1151 * numbers do not exist until the inode is cache. This means creating the
1152 * the dcache entry in readdir is necessary to keep the inode numbers
1153 * reported by readdir in sync with the inode numbers reported
1154 * by stat.
1155 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001156static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1157 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001158 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001159{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001160 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001161 struct inode *inode;
1162 struct qstr qname;
1163 ino_t ino = 0;
1164 unsigned type = DT_UNKNOWN;
1165
1166 qname.name = name;
1167 qname.len = len;
1168 qname.hash = full_name_hash(name, len);
1169
1170 child = d_lookup(dir, &qname);
1171 if (!child) {
1172 struct dentry *new;
1173 new = d_alloc(dir, &qname);
1174 if (new) {
1175 child = instantiate(dir->d_inode, new, task, ptr);
1176 if (child)
1177 dput(new);
1178 else
1179 child = new;
1180 }
1181 }
1182 if (!child || IS_ERR(child) || !child->d_inode)
1183 goto end_instantiate;
1184 inode = child->d_inode;
1185 if (inode) {
1186 ino = inode->i_ino;
1187 type = inode->i_mode >> 12;
1188 }
1189 dput(child);
1190end_instantiate:
1191 if (!ino)
1192 ino = find_inode_number(dir, &qname);
1193 if (!ino)
1194 ino = 1;
1195 return filldir(dirent, name, len, filp->f_pos, ino, type);
1196}
1197
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001198static unsigned name_to_int(struct dentry *dentry)
1199{
1200 const char *name = dentry->d_name.name;
1201 int len = dentry->d_name.len;
1202 unsigned n = 0;
1203
1204 if (len > 1 && *name == '0')
1205 goto out;
1206 while (len-- > 0) {
1207 unsigned c = *name++ - '0';
1208 if (c > 9)
1209 goto out;
1210 if (n >= (~0U-9)/10)
1211 goto out;
1212 n *= 10;
1213 n += c;
1214 }
1215 return n;
1216out:
1217 return ~0U;
1218}
1219
Miklos Szeredi27932742007-05-08 00:26:17 -07001220#define PROC_FDINFO_MAX 64
1221
1222static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1223 struct vfsmount **mnt, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001224{
1225 struct task_struct *task = get_proc_task(inode);
1226 struct files_struct *files = NULL;
1227 struct file *file;
1228 int fd = proc_fd(inode);
1229
1230 if (task) {
1231 files = get_files_struct(task);
1232 put_task_struct(task);
1233 }
1234 if (files) {
1235 /*
1236 * We are not taking a ref to the file structure, so we must
1237 * hold ->file_lock.
1238 */
1239 spin_lock(&files->file_lock);
1240 file = fcheck_files(files, fd);
1241 if (file) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001242 if (mnt)
1243 *mnt = mntget(file->f_path.mnt);
1244 if (dentry)
1245 *dentry = dget(file->f_path.dentry);
1246 if (info)
1247 snprintf(info, PROC_FDINFO_MAX,
1248 "pos:\t%lli\n"
1249 "flags:\t0%o\n",
1250 (long long) file->f_pos,
1251 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001252 spin_unlock(&files->file_lock);
1253 put_files_struct(files);
1254 return 0;
1255 }
1256 spin_unlock(&files->file_lock);
1257 put_files_struct(files);
1258 }
1259 return -ENOENT;
1260}
1261
Miklos Szeredi27932742007-05-08 00:26:17 -07001262static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1263 struct vfsmount **mnt)
1264{
1265 return proc_fd_info(inode, dentry, mnt, NULL);
1266}
1267
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001268static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1269{
1270 struct inode *inode = dentry->d_inode;
1271 struct task_struct *task = get_proc_task(inode);
1272 int fd = proc_fd(inode);
1273 struct files_struct *files;
1274
1275 if (task) {
1276 files = get_files_struct(task);
1277 if (files) {
1278 rcu_read_lock();
1279 if (fcheck_files(files, fd)) {
1280 rcu_read_unlock();
1281 put_files_struct(files);
1282 if (task_dumpable(task)) {
1283 inode->i_uid = task->euid;
1284 inode->i_gid = task->egid;
1285 } else {
1286 inode->i_uid = 0;
1287 inode->i_gid = 0;
1288 }
1289 inode->i_mode &= ~(S_ISUID | S_ISGID);
1290 security_task_to_inode(task, inode);
1291 put_task_struct(task);
1292 return 1;
1293 }
1294 rcu_read_unlock();
1295 put_files_struct(files);
1296 }
1297 put_task_struct(task);
1298 }
1299 d_drop(dentry);
1300 return 0;
1301}
1302
1303static struct dentry_operations tid_fd_dentry_operations =
1304{
1305 .d_revalidate = tid_fd_revalidate,
1306 .d_delete = pid_delete_dentry,
1307};
1308
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001309static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001310 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001311{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001312 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001313 struct file *file;
1314 struct files_struct *files;
1315 struct inode *inode;
1316 struct proc_inode *ei;
1317 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001318
Eric W. Biederman61a28782006-10-02 02:18:49 -07001319 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001320 if (!inode)
1321 goto out;
1322 ei = PROC_I(inode);
1323 ei->fd = fd;
1324 files = get_files_struct(task);
1325 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001326 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001327 inode->i_mode = S_IFLNK;
1328
1329 /*
1330 * We are not taking a ref to the file structure, so we must
1331 * hold ->file_lock.
1332 */
1333 spin_lock(&files->file_lock);
1334 file = fcheck_files(files, fd);
1335 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001336 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001337 if (file->f_mode & 1)
1338 inode->i_mode |= S_IRUSR | S_IXUSR;
1339 if (file->f_mode & 2)
1340 inode->i_mode |= S_IWUSR | S_IXUSR;
1341 spin_unlock(&files->file_lock);
1342 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001343
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001344 inode->i_op = &proc_pid_link_inode_operations;
1345 inode->i_size = 64;
1346 ei->op.proc_get_link = proc_fd_link;
1347 dentry->d_op = &tid_fd_dentry_operations;
1348 d_add(dentry, inode);
1349 /* Close the race of the process dying before we return the dentry */
1350 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001351 error = NULL;
1352
1353 out:
1354 return error;
1355out_unlock:
1356 spin_unlock(&files->file_lock);
1357 put_files_struct(files);
1358out_iput:
1359 iput(inode);
1360 goto out;
1361}
1362
Miklos Szeredi27932742007-05-08 00:26:17 -07001363static struct dentry *proc_lookupfd_common(struct inode *dir,
1364 struct dentry *dentry,
1365 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001366{
1367 struct task_struct *task = get_proc_task(dir);
1368 unsigned fd = name_to_int(dentry);
1369 struct dentry *result = ERR_PTR(-ENOENT);
1370
1371 if (!task)
1372 goto out_no_task;
1373 if (fd == ~0U)
1374 goto out;
1375
Miklos Szeredi27932742007-05-08 00:26:17 -07001376 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001377out:
1378 put_task_struct(task);
1379out_no_task:
1380 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001381}
1382
Miklos Szeredi27932742007-05-08 00:26:17 -07001383static int proc_readfd_common(struct file * filp, void * dirent,
1384 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001386 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001387 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001388 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 unsigned int fd, tid, ino;
1390 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001392 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001395 if (!p)
1396 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 retval = 0;
1398 tid = p->pid;
1399
1400 fd = filp->f_pos;
1401 switch (fd) {
1402 case 0:
1403 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1404 goto out;
1405 filp->f_pos++;
1406 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001407 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1409 goto out;
1410 filp->f_pos++;
1411 default:
1412 files = get_files_struct(p);
1413 if (!files)
1414 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001415 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001416 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001418 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001420 char name[PROC_NUMBUF];
1421 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 if (!fcheck_files(files, fd))
1424 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001425 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Miklos Szeredi27932742007-05-08 00:26:17 -07001427 len = snprintf(name, sizeof(name), "%d", fd);
1428 if (proc_fill_cache(filp, dirent, filldir,
1429 name, len, instantiate,
1430 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001431 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
1433 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001434 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001436 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 put_files_struct(files);
1438 }
1439out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001440 put_task_struct(p);
1441out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return retval;
1443}
1444
Miklos Szeredi27932742007-05-08 00:26:17 -07001445static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1446 struct nameidata *nd)
1447{
1448 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1449}
1450
1451static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1452{
1453 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1454}
1455
1456static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1457 size_t len, loff_t *ppos)
1458{
1459 char tmp[PROC_FDINFO_MAX];
1460 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1461 if (!err)
1462 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1463 return err;
1464}
1465
1466static const struct file_operations proc_fdinfo_file_operations = {
1467 .open = nonseekable_open,
1468 .read = proc_fdinfo_read,
1469};
1470
Arjan van de Ven00977a52007-02-12 00:55:34 -08001471static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 .read = generic_read_dir,
1473 .readdir = proc_readfd,
1474};
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001477 * /proc/pid/fd needs a special permission handler so that a process can still
1478 * access /proc/self/fd after it has executed a setuid().
1479 */
1480static int proc_fd_permission(struct inode *inode, int mask,
1481 struct nameidata *nd)
1482{
1483 int rv;
1484
1485 rv = generic_permission(inode, mask, NULL);
1486 if (rv == 0)
1487 return 0;
1488 if (task_pid(current) == proc_pid(inode))
1489 rv = 0;
1490 return rv;
1491}
1492
1493/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 * proc directories can do almost nothing..
1495 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001496static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001498 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001499 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500};
1501
Miklos Szeredi27932742007-05-08 00:26:17 -07001502static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1503 struct dentry *dentry, struct task_struct *task, const void *ptr)
1504{
1505 unsigned fd = *(unsigned *)ptr;
1506 struct inode *inode;
1507 struct proc_inode *ei;
1508 struct dentry *error = ERR_PTR(-ENOENT);
1509
1510 inode = proc_pid_make_inode(dir->i_sb, task);
1511 if (!inode)
1512 goto out;
1513 ei = PROC_I(inode);
1514 ei->fd = fd;
1515 inode->i_mode = S_IFREG | S_IRUSR;
1516 inode->i_fop = &proc_fdinfo_file_operations;
1517 dentry->d_op = &tid_fd_dentry_operations;
1518 d_add(dentry, inode);
1519 /* Close the race of the process dying before we return the dentry */
1520 if (tid_fd_revalidate(dentry, NULL))
1521 error = NULL;
1522
1523 out:
1524 return error;
1525}
1526
1527static struct dentry *proc_lookupfdinfo(struct inode *dir,
1528 struct dentry *dentry,
1529 struct nameidata *nd)
1530{
1531 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1532}
1533
1534static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1535{
1536 return proc_readfd_common(filp, dirent, filldir,
1537 proc_fdinfo_instantiate);
1538}
1539
1540static const struct file_operations proc_fdinfo_operations = {
1541 .read = generic_read_dir,
1542 .readdir = proc_readfdinfo,
1543};
1544
1545/*
1546 * proc directories can do almost nothing..
1547 */
1548static const struct inode_operations proc_fdinfo_inode_operations = {
1549 .lookup = proc_lookupfdinfo,
1550 .setattr = proc_setattr,
1551};
1552
1553
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001554static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001555 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001556{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001557 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001558 struct inode *inode;
1559 struct proc_inode *ei;
1560 struct dentry *error = ERR_PTR(-EINVAL);
1561
Eric W. Biederman61a28782006-10-02 02:18:49 -07001562 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001563 if (!inode)
1564 goto out;
1565
1566 ei = PROC_I(inode);
1567 inode->i_mode = p->mode;
1568 if (S_ISDIR(inode->i_mode))
1569 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1570 if (p->iop)
1571 inode->i_op = p->iop;
1572 if (p->fop)
1573 inode->i_fop = p->fop;
1574 ei->op = p->op;
1575 dentry->d_op = &pid_dentry_operations;
1576 d_add(dentry, inode);
1577 /* Close the race of the process dying before we return the dentry */
1578 if (pid_revalidate(dentry, NULL))
1579 error = NULL;
1580out:
1581 return error;
1582}
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584static struct dentry *proc_pident_lookup(struct inode *dir,
1585 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001586 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001587 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001590 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001591 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001592 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001594 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 inode = NULL;
1596
Eric W. Biederman99f89552006-06-26 00:25:55 -07001597 if (!task)
1598 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001600 /*
1601 * Yes, it does not scale. And it should not. Don't add
1602 * new entries into /proc/<tgid>/ without very good reasons.
1603 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001604 last = &ents[nents - 1];
1605 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (p->len != dentry->d_name.len)
1607 continue;
1608 if (!memcmp(dentry->d_name.name, p->name, p->len))
1609 break;
1610 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001611 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 goto out;
1613
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001614 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001616 put_task_struct(task);
1617out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001618 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Eric Dumazetc5141e62007-05-08 00:26:15 -07001621static int proc_pident_fill_cache(struct file *filp, void *dirent,
1622 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001623{
1624 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1625 proc_pident_instantiate, task, p);
1626}
1627
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001628static int proc_pident_readdir(struct file *filp,
1629 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001630 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001631{
1632 int i;
1633 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001634 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001635 struct inode *inode = dentry->d_inode;
1636 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001637 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001638 ino_t ino;
1639 int ret;
1640
1641 ret = -ENOENT;
1642 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001643 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001644
1645 ret = 0;
1646 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001647 i = filp->f_pos;
1648 switch (i) {
1649 case 0:
1650 ino = inode->i_ino;
1651 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1652 goto out;
1653 i++;
1654 filp->f_pos++;
1655 /* fall through */
1656 case 1:
1657 ino = parent_ino(dentry);
1658 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1659 goto out;
1660 i++;
1661 filp->f_pos++;
1662 /* fall through */
1663 default:
1664 i -= 2;
1665 if (i >= nents) {
1666 ret = 1;
1667 goto out;
1668 }
1669 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001670 last = &ents[nents - 1];
1671 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001672 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001673 goto out;
1674 filp->f_pos++;
1675 p++;
1676 }
1677 }
1678
1679 ret = 1;
1680out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001681 put_task_struct(task);
1682out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001683 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001687static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1688 size_t count, loff_t *ppos)
1689{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001690 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001691 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001692 ssize_t length;
1693 struct task_struct *task = get_proc_task(inode);
1694
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001695 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001696 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001697
1698 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001699 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001700 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001701 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001702 if (length > 0)
1703 length = simple_read_from_buffer(buf, count, ppos, p, length);
1704 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001705 return length;
1706}
1707
1708static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1709 size_t count, loff_t *ppos)
1710{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001711 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001712 char *page;
1713 ssize_t length;
1714 struct task_struct *task = get_proc_task(inode);
1715
1716 length = -ESRCH;
1717 if (!task)
1718 goto out_no_task;
1719 if (count > PAGE_SIZE)
1720 count = PAGE_SIZE;
1721
1722 /* No partial writes. */
1723 length = -EINVAL;
1724 if (*ppos != 0)
1725 goto out;
1726
1727 length = -ENOMEM;
1728 page = (char*)__get_free_page(GFP_USER);
1729 if (!page)
1730 goto out;
1731
1732 length = -EFAULT;
1733 if (copy_from_user(page, buf, count))
1734 goto out_free;
1735
1736 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001737 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001738 (void*)page, count);
1739out_free:
1740 free_page((unsigned long) page);
1741out:
1742 put_task_struct(task);
1743out_no_task:
1744 return length;
1745}
1746
Arjan van de Ven00977a52007-02-12 00:55:34 -08001747static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001748 .read = proc_pid_attr_read,
1749 .write = proc_pid_attr_write,
1750};
1751
Eric Dumazetc5141e62007-05-08 00:26:15 -07001752static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001753 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1754 REG("prev", S_IRUGO, pid_attr),
1755 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1756 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1757 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1758 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001759};
1760
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001761static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 void * dirent, filldir_t filldir)
1763{
1764 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001765 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
Arjan van de Ven00977a52007-02-12 00:55:34 -08001768static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001770 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771};
1772
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001773static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct dentry *dentry, struct nameidata *nd)
1775{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001776 return proc_pident_lookup(dir, dentry,
1777 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001780static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001781 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001782 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001783 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784};
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786#endif
1787
1788/*
1789 * /proc/self:
1790 */
1791static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1792 int buflen)
1793{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001794 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 sprintf(tmp, "%d", current->tgid);
1796 return vfs_readlink(dentry,buffer,buflen,tmp);
1797}
1798
Al Viro008b1502005-08-20 00:17:39 +01001799static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001801 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001803 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001804}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001806static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 .readlink = proc_self_readlink,
1808 .follow_link = proc_self_follow_link,
1809};
1810
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001811/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001812 * proc base
1813 *
1814 * These are the directory entries in the root directory of /proc
1815 * that properly belong to the /proc filesystem, as they describe
1816 * describe something that is process related.
1817 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07001818static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001819 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001820 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001821};
1822
1823/*
1824 * Exceptional case: normally we are not allowed to unhash a busy
1825 * directory. In this case, however, we can do it - no aliasing problems
1826 * due to the way we treat inodes.
1827 */
1828static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1829{
1830 struct inode *inode = dentry->d_inode;
1831 struct task_struct *task = get_proc_task(inode);
1832 if (task) {
1833 put_task_struct(task);
1834 return 1;
1835 }
1836 d_drop(dentry);
1837 return 0;
1838}
1839
1840static struct dentry_operations proc_base_dentry_operations =
1841{
1842 .d_revalidate = proc_base_revalidate,
1843 .d_delete = pid_delete_dentry,
1844};
1845
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001846static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001847 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001848{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001849 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001850 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001851 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001852 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001853
1854 /* Allocate the inode */
1855 error = ERR_PTR(-ENOMEM);
1856 inode = new_inode(dir->i_sb);
1857 if (!inode)
1858 goto out;
1859
1860 /* Initialize the inode */
1861 ei = PROC_I(inode);
1862 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001863
1864 /*
1865 * grab the reference to the task.
1866 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001867 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001868 if (!ei->pid)
1869 goto out_iput;
1870
1871 inode->i_uid = 0;
1872 inode->i_gid = 0;
1873 inode->i_mode = p->mode;
1874 if (S_ISDIR(inode->i_mode))
1875 inode->i_nlink = 2;
1876 if (S_ISLNK(inode->i_mode))
1877 inode->i_size = 64;
1878 if (p->iop)
1879 inode->i_op = p->iop;
1880 if (p->fop)
1881 inode->i_fop = p->fop;
1882 ei->op = p->op;
1883 dentry->d_op = &proc_base_dentry_operations;
1884 d_add(dentry, inode);
1885 error = NULL;
1886out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001887 return error;
1888out_iput:
1889 iput(inode);
1890 goto out;
1891}
1892
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001893static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1894{
1895 struct dentry *error;
1896 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001897 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001898
1899 error = ERR_PTR(-ENOENT);
1900
1901 if (!task)
1902 goto out_no_task;
1903
1904 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001905 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1906 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001907 if (p->len != dentry->d_name.len)
1908 continue;
1909 if (!memcmp(dentry->d_name.name, p->name, p->len))
1910 break;
1911 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001912 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001913 goto out;
1914
1915 error = proc_base_instantiate(dir, dentry, task, p);
1916
1917out:
1918 put_task_struct(task);
1919out_no_task:
1920 return error;
1921}
1922
Eric Dumazetc5141e62007-05-08 00:26:15 -07001923static int proc_base_fill_cache(struct file *filp, void *dirent,
1924 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001925{
1926 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1927 proc_base_instantiate, task, p);
1928}
1929
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001930#ifdef CONFIG_TASK_IO_ACCOUNTING
1931static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1932{
1933 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001934#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001935 "rchar: %llu\n"
1936 "wchar: %llu\n"
1937 "syscr: %llu\n"
1938 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001939#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001940 "read_bytes: %llu\n"
1941 "write_bytes: %llu\n"
1942 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001943#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001944 (unsigned long long)task->rchar,
1945 (unsigned long long)task->wchar,
1946 (unsigned long long)task->syscr,
1947 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001948#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001949 (unsigned long long)task->ioac.read_bytes,
1950 (unsigned long long)task->ioac.write_bytes,
1951 (unsigned long long)task->ioac.cancelled_write_bytes);
1952}
1953#endif
1954
Eric W. Biederman801199c2006-10-02 02:18:48 -07001955/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001956 * Thread groups
1957 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08001958static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001959static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001960
Eric Dumazetc5141e62007-05-08 00:26:15 -07001961static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001962 DIR("task", S_IRUGO|S_IXUGO, task),
1963 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07001964 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07001965 INF("environ", S_IRUSR, pid_environ),
1966 INF("auxv", S_IRUSR, pid_auxv),
1967 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001968#ifdef CONFIG_SCHED_DEBUG
1969 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
1970#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001971 INF("cmdline", S_IRUGO, pid_cmdline),
1972 INF("stat", S_IRUGO, tgid_stat),
1973 INF("statm", S_IRUGO, pid_statm),
1974 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001975#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001976 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001977#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001978 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07001979 LNK("cwd", cwd),
1980 LNK("root", root),
1981 LNK("exe", exe),
1982 REG("mounts", S_IRUGO, mounts),
1983 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001984#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07001985 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07001986 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001987#endif
1988#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001989 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001990#endif
1991#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001992 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001993#endif
1994#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001995 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001996#endif
1997#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001998 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001999#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002000 INF("oom_score", S_IRUGO, oom_score),
2001 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002002#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002003 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002004#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002005#ifdef CONFIG_FAULT_INJECTION
2006 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2007#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002008#ifdef CONFIG_TASK_IO_ACCOUNTING
2009 INF("io", S_IRUGO, pid_io_accounting),
2010#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002011};
2012
2013static int proc_tgid_base_readdir(struct file * filp,
2014 void * dirent, filldir_t filldir)
2015{
2016 return proc_pident_readdir(filp,dirent,filldir,
2017 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2018}
2019
Arjan van de Ven00977a52007-02-12 00:55:34 -08002020static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002021 .read = generic_read_dir,
2022 .readdir = proc_tgid_base_readdir,
2023};
2024
2025static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002026 return proc_pident_lookup(dir, dentry,
2027 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002028}
2029
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002030static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002031 .lookup = proc_tgid_base_lookup,
2032 .getattr = pid_getattr,
2033 .setattr = proc_setattr,
2034};
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07002037 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002039 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002041 * Looks in the dcache for
2042 * /proc/@pid
2043 * /proc/@tgid/task/@pid
2044 * if either directory is present flushes it and all of it'ts children
2045 * from the dcache.
2046 *
2047 * It is safe and reasonable to cache /proc entries for a task until
2048 * that task exits. After that they just clog up the dcache with
2049 * useless entries, possibly causing useful dcache entries to be
2050 * flushed instead. This routine is proved to flush those useless
2051 * dcache entries at process exit time.
2052 *
2053 * NOTE: This routine is just an optimization so it does not guarantee
2054 * that no dcache entries will exist at process exit time it
2055 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07002057void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002059 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002060 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002061 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Eric W. Biederman48e64842006-06-26 00:25:48 -07002063 name.name = buf;
2064 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2065 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2066 if (dentry) {
2067 shrink_dcache_parent(dentry);
2068 d_drop(dentry);
2069 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Eric W. Biederman48e64842006-06-26 00:25:48 -07002072 if (thread_group_leader(task))
2073 goto out;
2074
2075 name.name = buf;
2076 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2077 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2078 if (!leader)
2079 goto out;
2080
2081 name.name = "task";
2082 name.len = strlen(name.name);
2083 dir = d_hash_and_lookup(leader, &name);
2084 if (!dir)
2085 goto out_put_leader;
2086
2087 name.name = buf;
2088 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2089 dentry = d_hash_and_lookup(dir, &name);
2090 if (dentry) {
2091 shrink_dcache_parent(dentry);
2092 d_drop(dentry);
2093 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002095
2096 dput(dir);
2097out_put_leader:
2098 dput(leader);
2099out:
2100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Adrian Bunk9711ef92006-12-06 20:38:31 -08002103static struct dentry *proc_pid_instantiate(struct inode *dir,
2104 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002105 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002106{
2107 struct dentry *error = ERR_PTR(-ENOENT);
2108 struct inode *inode;
2109
Eric W. Biederman61a28782006-10-02 02:18:49 -07002110 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002111 if (!inode)
2112 goto out;
2113
2114 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2115 inode->i_op = &proc_tgid_base_inode_operations;
2116 inode->i_fop = &proc_tgid_base_operations;
2117 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002118 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002119#ifdef CONFIG_SECURITY
2120 inode->i_nlink += 1;
2121#endif
2122
2123 dentry->d_op = &pid_dentry_operations;
2124
2125 d_add(dentry, inode);
2126 /* Close the race of the process dying before we return the dentry */
2127 if (pid_revalidate(dentry, NULL))
2128 error = NULL;
2129out:
2130 return error;
2131}
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2134{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002135 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Eric W. Biederman801199c2006-10-02 02:18:48 -07002139 result = proc_base_lookup(dir, dentry);
2140 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2141 goto out;
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 tgid = name_to_int(dentry);
2144 if (tgid == ~0U)
2145 goto out;
2146
Eric W. Biedermande758732006-06-26 00:25:51 -07002147 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 task = find_task_by_pid(tgid);
2149 if (task)
2150 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002151 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (!task)
2153 goto out;
2154
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002155 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002156 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002158 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002162 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002163 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002165static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002167 struct task_struct *task;
2168 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002170 rcu_read_lock();
2171retry:
2172 task = NULL;
2173 pid = find_ge_pid(tgid);
2174 if (pid) {
2175 tgid = pid->nr + 1;
2176 task = pid_task(pid, PIDTYPE_PID);
2177 /* What we to know is if the pid we have find is the
2178 * pid of a thread_group_leader. Testing for task
2179 * being a thread_group_leader is the obvious thing
2180 * todo but there is a window when it fails, due to
2181 * the pid transfer logic in de_thread.
2182 *
2183 * So we perform the straight forward test of seeing
2184 * if the pid we have found is the pid of a thread
2185 * group leader, and don't worry if the task we have
2186 * found doesn't happen to be a thread group leader.
2187 * As we don't care in the case of readdir.
2188 */
2189 if (!task || !has_group_leader_pid(task))
2190 goto retry;
2191 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002193 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002194 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002197#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Eric W. Biederman61a28782006-10-02 02:18:49 -07002199static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2200 struct task_struct *task, int tgid)
2201{
2202 char name[PROC_NUMBUF];
2203 int len = snprintf(name, sizeof(name), "%d", tgid);
2204 return proc_fill_cache(filp, dirent, filldir, name, len,
2205 proc_pid_instantiate, task, NULL);
2206}
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208/* for the /proc/ directory itself, after non-process stuff has been done */
2209int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2210{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002212 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002213 struct task_struct *task;
2214 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Eric W. Biederman61a28782006-10-02 02:18:49 -07002216 if (!reaper)
2217 goto out_no_task;
2218
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002219 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002220 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002221 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002225 tgid = filp->f_pos - TGID_OFFSET;
2226 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002227 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002228 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002229 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002230 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002231 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002232 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002233 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002236 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2237out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002238 put_task_struct(reaper);
2239out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return 0;
2241}
2242
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002243/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002244 * Tasks
2245 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002246static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002247 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002248 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002249 INF("environ", S_IRUSR, pid_environ),
2250 INF("auxv", S_IRUSR, pid_auxv),
2251 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002252#ifdef CONFIG_SCHED_DEBUG
2253 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2254#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002255 INF("cmdline", S_IRUGO, pid_cmdline),
2256 INF("stat", S_IRUGO, tid_stat),
2257 INF("statm", S_IRUGO, pid_statm),
2258 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002259#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002260 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002261#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002262 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002263 LNK("cwd", cwd),
2264 LNK("root", root),
2265 LNK("exe", exe),
2266 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002267#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002268 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002269 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002270#endif
2271#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002272 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002273#endif
2274#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002275 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002276#endif
2277#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002278 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002279#endif
2280#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002281 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002282#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002283 INF("oom_score", S_IRUGO, oom_score),
2284 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002285#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002286 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002287#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002288#ifdef CONFIG_FAULT_INJECTION
2289 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2290#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002291};
2292
2293static int proc_tid_base_readdir(struct file * filp,
2294 void * dirent, filldir_t filldir)
2295{
2296 return proc_pident_readdir(filp,dirent,filldir,
2297 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2298}
2299
2300static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002301 return proc_pident_lookup(dir, dentry,
2302 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002303}
2304
Arjan van de Ven00977a52007-02-12 00:55:34 -08002305static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002306 .read = generic_read_dir,
2307 .readdir = proc_tid_base_readdir,
2308};
2309
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002310static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002311 .lookup = proc_tid_base_lookup,
2312 .getattr = pid_getattr,
2313 .setattr = proc_setattr,
2314};
2315
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002316static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002317 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002318{
2319 struct dentry *error = ERR_PTR(-ENOENT);
2320 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002321 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002322
2323 if (!inode)
2324 goto out;
2325 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2326 inode->i_op = &proc_tid_base_inode_operations;
2327 inode->i_fop = &proc_tid_base_operations;
2328 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002329 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002330#ifdef CONFIG_SECURITY
2331 inode->i_nlink += 1;
2332#endif
2333
2334 dentry->d_op = &pid_dentry_operations;
2335
2336 d_add(dentry, inode);
2337 /* Close the race of the process dying before we return the dentry */
2338 if (pid_revalidate(dentry, NULL))
2339 error = NULL;
2340out:
2341 return error;
2342}
2343
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002344static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2345{
2346 struct dentry *result = ERR_PTR(-ENOENT);
2347 struct task_struct *task;
2348 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002349 unsigned tid;
2350
2351 if (!leader)
2352 goto out_no_task;
2353
2354 tid = name_to_int(dentry);
2355 if (tid == ~0U)
2356 goto out;
2357
2358 rcu_read_lock();
2359 task = find_task_by_pid(tid);
2360 if (task)
2361 get_task_struct(task);
2362 rcu_read_unlock();
2363 if (!task)
2364 goto out;
2365 if (leader->tgid != task->tgid)
2366 goto out_drop_task;
2367
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002368 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002369out_drop_task:
2370 put_task_struct(task);
2371out:
2372 put_task_struct(leader);
2373out_no_task:
2374 return result;
2375}
2376
2377/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002378 * Find the first tid of a thread group to return to user space.
2379 *
2380 * Usually this is just the thread group leader, but if the users
2381 * buffer was too small or there was a seek into the middle of the
2382 * directory we have more work todo.
2383 *
2384 * In the case of a short read we start with find_task_by_pid.
2385 *
2386 * In the case of a seek we start with the leader and walk nr
2387 * threads past it.
2388 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002389static struct task_struct *first_tid(struct task_struct *leader,
2390 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002391{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002392 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002393
Eric W. Biedermancc288732006-06-26 00:26:01 -07002394 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002395 /* Attempt to start with the pid of a thread */
2396 if (tid && (nr > 0)) {
2397 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002398 if (pos && (pos->group_leader == leader))
2399 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002400 }
2401
2402 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002403 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002404 if (nr && nr >= get_nr_threads(leader))
2405 goto out;
2406
2407 /* If we haven't found our starting place yet start
2408 * with the leader and walk nr threads forward.
2409 */
2410 for (pos = leader; nr > 0; --nr) {
2411 pos = next_thread(pos);
2412 if (pos == leader) {
2413 pos = NULL;
2414 goto out;
2415 }
2416 }
2417found:
2418 get_task_struct(pos);
2419out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002420 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002421 return pos;
2422}
2423
2424/*
2425 * Find the next thread in the thread list.
2426 * Return NULL if there is an error or no next thread.
2427 *
2428 * The reference to the input task_struct is released.
2429 */
2430static struct task_struct *next_tid(struct task_struct *start)
2431{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002432 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002433 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002434 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002435 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002436 if (thread_group_leader(pos))
2437 pos = NULL;
2438 else
2439 get_task_struct(pos);
2440 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002441 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002442 put_task_struct(start);
2443 return pos;
2444}
2445
Eric W. Biederman61a28782006-10-02 02:18:49 -07002446static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2447 struct task_struct *task, int tid)
2448{
2449 char name[PROC_NUMBUF];
2450 int len = snprintf(name, sizeof(name), "%d", tid);
2451 return proc_fill_cache(filp, dirent, filldir, name, len,
2452 proc_task_instantiate, task, NULL);
2453}
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455/* for the /proc/TGID/task/ directories */
2456static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2457{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002458 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002460 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002461 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 int retval = -ENOENT;
2463 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002464 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2466
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002467 task = get_proc_task(inode);
2468 if (!task)
2469 goto out_no_task;
2470 rcu_read_lock();
2471 if (pid_alive(task)) {
2472 leader = task->group_leader;
2473 get_task_struct(leader);
2474 }
2475 rcu_read_unlock();
2476 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002477 if (!leader)
2478 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 retval = 0;
2480
2481 switch (pos) {
2482 case 0:
2483 ino = inode->i_ino;
2484 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2485 goto out;
2486 pos++;
2487 /* fall through */
2488 case 1:
2489 ino = parent_ino(dentry);
2490 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2491 goto out;
2492 pos++;
2493 /* fall through */
2494 }
2495
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002496 /* f_version caches the tgid value that the last readdir call couldn't
2497 * return. lseek aka telldir automagically resets f_version to 0.
2498 */
2499 tid = filp->f_version;
2500 filp->f_version = 0;
2501 for (task = first_tid(leader, tid, pos - 2);
2502 task;
2503 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002504 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002505 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002506 /* returning this tgid failed, save it as the first
2507 * pid for the next readir call */
2508 filp->f_version = tid;
2509 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
2513out:
2514 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002515 put_task_struct(leader);
2516out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return retval;
2518}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002519
2520static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2521{
2522 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002523 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002524 generic_fillattr(inode, stat);
2525
Eric W. Biederman99f89552006-06-26 00:25:55 -07002526 if (p) {
2527 rcu_read_lock();
2528 stat->nlink += get_nr_threads(p);
2529 rcu_read_unlock();
2530 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002531 }
2532
2533 return 0;
2534}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002535
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002536static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002537 .lookup = proc_task_lookup,
2538 .getattr = proc_task_getattr,
2539 .setattr = proc_setattr,
2540};
2541
Arjan van de Ven00977a52007-02-12 00:55:34 -08002542static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002543 .read = generic_read_dir,
2544 .readdir = proc_task_readdir,
2545};