blob: 0c2052c79243803dbc6674c590fd3f9ab44e13e4 [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>
70#include <linux/seccomp.h>
71#include <linux/cpuset.h>
72#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050073#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070074#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070075#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include "internal.h"
77
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070078/* NOTE:
79 * Implementing inode permission operations in /proc is almost
80 * certainly an error. Permission checks need to happen during
81 * each system call not at open time. The reason is that most of
82 * what we wish to check for permissions in /proc varies at runtime.
83 *
84 * The classic example of a problem is opening file descriptors
85 * in /proc for a task before it execs a suid executable.
86 */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Eric W. Biederman8578cea2006-06-26 00:25:54 -070089/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070090#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070094 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080096 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080097 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070098 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Eric W. Biederman61a28782006-10-02 02:18:49 -0700101#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700102 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700103 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 .mode = MODE, \
105 .iop = IOP, \
106 .fop = FOP, \
107 .op = OP, \
108}
109
Eric W. Biederman61a28782006-10-02 02:18:49 -0700110#define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700114#define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700118#define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700121#define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Kees Cook5096add2007-05-08 00:26:04 -0700126int maps_protect;
127EXPORT_SYMBOL(maps_protect);
128
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700129static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700132 task_lock(task);
133 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if(fs)
135 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700136 task_unlock(task);
137 return fs;
138}
139
Eric W. Biederman99f89552006-06-26 00:25:55 -0700140static int get_nr_threads(struct task_struct *tsk)
141{
142 /* Must be called with the rcu_read_lock held */
143 unsigned long flags;
144 int count = 0;
145
146 if (lock_task_sighand(tsk, &flags)) {
147 count = atomic_read(&tsk->signal->count);
148 unlock_task_sighand(tsk, &flags);
149 }
150 return count;
151}
152
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700153static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
154{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700155 struct task_struct *task = get_proc_task(inode);
156 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700158
159 if (task) {
160 fs = get_fs_struct(task);
161 put_task_struct(task);
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (fs) {
164 read_lock(&fs->lock);
165 *mnt = mntget(fs->pwdmnt);
166 *dentry = dget(fs->pwd);
167 read_unlock(&fs->lock);
168 result = 0;
169 put_fs_struct(fs);
170 }
171 return result;
172}
173
174static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
175{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176 struct task_struct *task = get_proc_task(inode);
177 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179
180 if (task) {
181 fs = get_fs_struct(task);
182 put_task_struct(task);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (fs) {
185 read_lock(&fs->lock);
186 *mnt = mntget(fs->rootmnt);
187 *dentry = dget(fs->root);
188 read_unlock(&fs->lock);
189 result = 0;
190 put_fs_struct(fs);
191 }
192 return result;
193}
194
195#define MAY_PTRACE(task) \
196 (task == current || \
197 (task->parent == current && \
198 (task->ptrace & PT_PTRACED) && \
199 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
200 security_ptrace(current,task) == 0))
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int proc_pid_environ(struct task_struct *task, char * buffer)
203{
204 int res = 0;
205 struct mm_struct *mm = get_task_mm(task);
206 if (mm) {
207 unsigned int len = mm->env_end - mm->env_start;
208 if (len > PAGE_SIZE)
209 len = PAGE_SIZE;
210 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700211 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 res = -ESRCH;
213 mmput(mm);
214 }
215 return res;
216}
217
218static int proc_pid_cmdline(struct task_struct *task, char * buffer)
219{
220 int res = 0;
221 unsigned int len;
222 struct mm_struct *mm = get_task_mm(task);
223 if (!mm)
224 goto out;
225 if (!mm->arg_end)
226 goto out_mm; /* Shh! No looking before we're done */
227
228 len = mm->arg_end - mm->arg_start;
229
230 if (len > PAGE_SIZE)
231 len = PAGE_SIZE;
232
233 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
234
235 // If the nul at the end of args has been overwritten, then
236 // assume application is using setproctitle(3).
237 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
238 len = strnlen(buffer, res);
239 if (len < res) {
240 res = len;
241 } else {
242 len = mm->env_end - mm->env_start;
243 if (len > PAGE_SIZE - res)
244 len = PAGE_SIZE - res;
245 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
246 res = strnlen(buffer, res);
247 }
248 }
249out_mm:
250 mmput(mm);
251out:
252 return res;
253}
254
255static int proc_pid_auxv(struct task_struct *task, char *buffer)
256{
257 int res = 0;
258 struct mm_struct *mm = get_task_mm(task);
259 if (mm) {
260 unsigned int nwords = 0;
261 do
262 nwords += 2;
263 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
264 res = nwords * sizeof(mm->saved_auxv[0]);
265 if (res > PAGE_SIZE)
266 res = PAGE_SIZE;
267 memcpy(buffer, mm->saved_auxv, res);
268 mmput(mm);
269 }
270 return res;
271}
272
273
274#ifdef CONFIG_KALLSYMS
275/*
276 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
277 * Returns the resolved symbol. If that fails, simply return the address.
278 */
279static int proc_pid_wchan(struct task_struct *task, char *buffer)
280{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700281 unsigned long wchan;
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700282 char symname[KSYM_NAME_LEN+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 wchan = get_wchan(task);
285
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700286 if (lookup_symbol_name(wchan, symname) < 0)
287 return sprintf(buffer, "%lu", wchan);
288 else
289 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291#endif /* CONFIG_KALLSYMS */
292
293#ifdef CONFIG_SCHEDSTATS
294/*
295 * Provides /proc/PID/schedstat
296 */
297static int proc_pid_schedstat(struct task_struct *task, char *buffer)
298{
299 return sprintf(buffer, "%lu %lu %lu\n",
300 task->sched_info.cpu_time,
301 task->sched_info.run_delay,
302 task->sched_info.pcnt);
303}
304#endif
305
306/* The badness from the OOM killer */
307unsigned long badness(struct task_struct *p, unsigned long uptime);
308static int proc_oom_score(struct task_struct *task, char *buffer)
309{
310 unsigned long points;
311 struct timespec uptime;
312
313 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700314 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700316 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return sprintf(buffer, "%lu\n", points);
318}
319
320/************************************************************************/
321/* Here the fs part begins */
322/************************************************************************/
323
324/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700325static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700327 struct task_struct *task;
328 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700329 /* Allow access to a task's file descriptors if it is us or we
330 * may use ptrace attach to the process and find out that
331 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700332 */
333 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700334 if (task) {
335 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700336 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700337 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700338 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700341static int proc_setattr(struct dentry *dentry, struct iattr *attr)
342{
343 int error;
344 struct inode *inode = dentry->d_inode;
345
346 if (attr->ia_valid & ATTR_MODE)
347 return -EPERM;
348
349 error = inode_change_ok(inode, attr);
350 if (!error) {
351 error = security_inode_setattr(dentry, attr);
352 if (!error)
353 error = inode_setattr(inode, attr);
354 }
355 return error;
356}
357
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800358static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700359 .setattr = proc_setattr,
360};
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500363struct proc_mounts {
364 struct seq_file m;
365 int event;
366};
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static int mounts_open(struct inode *inode, struct file *file)
369{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700370 struct task_struct *task = get_proc_task(inode);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800371 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500372 struct proc_mounts *p;
373 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Eric W. Biederman99f89552006-06-26 00:25:55 -0700375 if (task) {
376 task_lock(task);
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800377 if (task->nsproxy) {
378 ns = task->nsproxy->mnt_ns;
379 if (ns)
380 get_mnt_ns(ns);
381 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700382 task_unlock(task);
383 put_task_struct(task);
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800386 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500387 ret = -ENOMEM;
388 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
389 if (p) {
390 file->private_data = &p->m;
391 ret = seq_open(file, &mounts_op);
392 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800393 p->m.private = ns;
394 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500395 return 0;
396 }
397 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800399 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 return ret;
402}
403
404static int mounts_release(struct inode *inode, struct file *file)
405{
406 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800407 struct mnt_namespace *ns = m->private;
408 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return seq_release(inode, file);
410}
411
Al Viro5addc5d2005-11-07 17:15:49 -0500412static unsigned mounts_poll(struct file *file, poll_table *wait)
413{
414 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800415 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500416 unsigned res = 0;
417
418 poll_wait(file, &ns->poll, wait);
419
420 spin_lock(&vfsmount_lock);
421 if (p->event != ns->event) {
422 p->event = ns->event;
423 res = POLLERR;
424 }
425 spin_unlock(&vfsmount_lock);
426
427 return res;
428}
429
Arjan van de Ven00977a52007-02-12 00:55:34 -0800430static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 .open = mounts_open,
432 .read = seq_read,
433 .llseek = seq_lseek,
434 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500435 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436};
437
Chuck Leverb4629fe2006-03-20 13:44:12 -0500438extern struct seq_operations mountstats_op;
439static int mountstats_open(struct inode *inode, struct file *file)
440{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500441 int ret = seq_open(file, &mountstats_op);
442
443 if (!ret) {
444 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800445 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700446 struct task_struct *task = get_proc_task(inode);
447
448 if (task) {
449 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800450 if (task->nsproxy)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800451 mnt_ns = task->nsproxy->mnt_ns;
452 if (mnt_ns)
453 get_mnt_ns(mnt_ns);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700454 task_unlock(task);
455 put_task_struct(task);
456 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500457
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800458 if (mnt_ns)
459 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500460 else {
461 seq_release(inode, file);
462 ret = -EINVAL;
463 }
464 }
465 return ret;
466}
467
Arjan van de Ven00977a52007-02-12 00:55:34 -0800468static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500469 .open = mountstats_open,
470 .read = seq_read,
471 .llseek = seq_lseek,
472 .release = mounts_release,
473};
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
476
477static ssize_t proc_info_read(struct file * file, char __user * buf,
478 size_t count, loff_t *ppos)
479{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800480 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 unsigned long page;
482 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700483 struct task_struct *task = get_proc_task(inode);
484
485 length = -ESRCH;
486 if (!task)
487 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (count > PROC_BLOCK_SIZE)
490 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700491
492 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 length = PROC_I(inode)->op.proc_read(task, (char*)page);
497
498 if (length >= 0)
499 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
500 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700501out:
502 put_task_struct(task);
503out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return length;
505}
506
Arjan van de Ven00977a52007-02-12 00:55:34 -0800507static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .read = proc_info_read,
509};
510
511static int mem_open(struct inode* inode, struct file* file)
512{
513 file->private_data = (void*)((long)current->self_exec_id);
514 return 0;
515}
516
517static ssize_t mem_read(struct file * file, char __user * buf,
518 size_t count, loff_t *ppos)
519{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800520 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 char *page;
522 unsigned long src = *ppos;
523 int ret = -ESRCH;
524 struct mm_struct *mm;
525
Eric W. Biederman99f89552006-06-26 00:25:55 -0700526 if (!task)
527 goto out_no_task;
528
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700529 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto out;
531
532 ret = -ENOMEM;
533 page = (char *)__get_free_page(GFP_USER);
534 if (!page)
535 goto out;
536
537 ret = 0;
538
539 mm = get_task_mm(task);
540 if (!mm)
541 goto out_free;
542
543 ret = -EIO;
544
545 if (file->private_data != (void*)((long)current->self_exec_id))
546 goto out_put;
547
548 ret = 0;
549
550 while (count > 0) {
551 int this_len, retval;
552
553 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
554 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700555 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!ret)
557 ret = -EIO;
558 break;
559 }
560
561 if (copy_to_user(buf, page, retval)) {
562 ret = -EFAULT;
563 break;
564 }
565
566 ret += retval;
567 src += retval;
568 buf += retval;
569 count -= retval;
570 }
571 *ppos = src;
572
573out_put:
574 mmput(mm);
575out_free:
576 free_page((unsigned long) page);
577out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700578 put_task_struct(task);
579out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return ret;
581}
582
583#define mem_write NULL
584
585#ifndef mem_write
586/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800587static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 size_t count, loff_t *ppos)
589{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700590 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800592 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 unsigned long dst = *ppos;
594
Eric W. Biederman99f89552006-06-26 00:25:55 -0700595 copied = -ESRCH;
596 if (!task)
597 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Eric W. Biederman99f89552006-06-26 00:25:55 -0700599 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
600 goto out;
601
602 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 page = (char *)__get_free_page(GFP_USER);
604 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700607 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 while (count > 0) {
609 int this_len, retval;
610
611 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
612 if (copy_from_user(page, buf, this_len)) {
613 copied = -EFAULT;
614 break;
615 }
616 retval = access_process_vm(task, dst, page, this_len, 1);
617 if (!retval) {
618 if (!copied)
619 copied = -EIO;
620 break;
621 }
622 copied += retval;
623 buf += retval;
624 dst += retval;
625 count -= retval;
626 }
627 *ppos = dst;
628 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700629out:
630 put_task_struct(task);
631out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return copied;
633}
634#endif
635
636static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
637{
638 switch (orig) {
639 case 0:
640 file->f_pos = offset;
641 break;
642 case 1:
643 file->f_pos += offset;
644 break;
645 default:
646 return -EINVAL;
647 }
648 force_successful_syscall_return();
649 return file->f_pos;
650}
651
Arjan van de Ven00977a52007-02-12 00:55:34 -0800652static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 .llseek = mem_lseek,
654 .read = mem_read,
655 .write = mem_write,
656 .open = mem_open,
657};
658
659static ssize_t oom_adjust_read(struct file *file, char __user *buf,
660 size_t count, loff_t *ppos)
661{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800662 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700663 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700665 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 loff_t __ppos = *ppos;
667
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (__ppos >= len)
675 return 0;
676 if (count > len-__ppos)
677 count = len-__ppos;
678 if (copy_to_user(buf, buffer + __ppos, count))
679 return -EFAULT;
680 *ppos = __ppos + count;
681 return count;
682}
683
684static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
685 size_t count, loff_t *ppos)
686{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700687 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700688 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int oom_adjust;
690
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700691 memset(buffer, 0, sizeof(buffer));
692 if (count > sizeof(buffer) - 1)
693 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (copy_from_user(buffer, buf, count))
695 return -EFAULT;
696 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700697 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
698 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -EINVAL;
700 if (*end == '\n')
701 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800702 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700703 if (!task)
704 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800705 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
706 put_task_struct(task);
707 return -EACCES;
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700710 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (end - buffer == 0)
712 return -EIO;
713 return end - buffer;
714}
715
Arjan van de Ven00977a52007-02-12 00:55:34 -0800716static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 .read = oom_adjust_read,
718 .write = oom_adjust_write,
719};
720
David Rientjesb813e932007-05-06 14:49:24 -0700721static ssize_t clear_refs_write(struct file *file, const char __user *buf,
722 size_t count, loff_t *ppos)
723{
724 struct task_struct *task;
725 char buffer[PROC_NUMBUF], *end;
726 struct mm_struct *mm;
727
728 memset(buffer, 0, sizeof(buffer));
729 if (count > sizeof(buffer) - 1)
730 count = sizeof(buffer) - 1;
731 if (copy_from_user(buffer, buf, count))
732 return -EFAULT;
733 if (!simple_strtol(buffer, &end, 0))
734 return -EINVAL;
735 if (*end == '\n')
736 end++;
737 task = get_proc_task(file->f_path.dentry->d_inode);
738 if (!task)
739 return -ESRCH;
740 mm = get_task_mm(task);
741 if (mm) {
742 clear_refs_smap(mm);
743 mmput(mm);
744 }
745 put_task_struct(task);
746 if (end - buffer == 0)
747 return -EIO;
748 return end - buffer;
749}
750
751static struct file_operations proc_clear_refs_operations = {
752 .write = clear_refs_write,
753};
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#ifdef CONFIG_AUDITSYSCALL
756#define TMPBUFLEN 21
757static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
758 size_t count, loff_t *ppos)
759{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800760 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700761 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ssize_t length;
763 char tmpbuf[TMPBUFLEN];
764
Eric W. Biederman99f89552006-06-26 00:25:55 -0700765 if (!task)
766 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
768 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700769 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
771}
772
773static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
774 size_t count, loff_t *ppos)
775{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800776 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 char *page, *tmp;
778 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 uid_t loginuid;
780
781 if (!capable(CAP_AUDIT_CONTROL))
782 return -EPERM;
783
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700784 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -EPERM;
786
Al Viroe0182902006-05-18 08:28:02 -0400787 if (count >= PAGE_SIZE)
788 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (*ppos != 0) {
791 /* No partial writes. */
792 return -EINVAL;
793 }
794 page = (char*)__get_free_page(GFP_USER);
795 if (!page)
796 return -ENOMEM;
797 length = -EFAULT;
798 if (copy_from_user(page, buf, count))
799 goto out_free_page;
800
Al Viroe0182902006-05-18 08:28:02 -0400801 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 loginuid = simple_strtoul(page, &tmp, 10);
803 if (tmp == page) {
804 length = -EINVAL;
805 goto out_free_page;
806
807 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700808 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (likely(length == 0))
810 length = count;
811
812out_free_page:
813 free_page((unsigned long) page);
814 return length;
815}
816
Arjan van de Ven00977a52007-02-12 00:55:34 -0800817static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .read = proc_loginuid_read,
819 .write = proc_loginuid_write,
820};
821#endif
822
823#ifdef CONFIG_SECCOMP
824static ssize_t seccomp_read(struct file *file, char __user *buf,
825 size_t count, loff_t *ppos)
826{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700827 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 char __buf[20];
829 loff_t __ppos = *ppos;
830 size_t len;
831
Eric W. Biederman99f89552006-06-26 00:25:55 -0700832 if (!tsk)
833 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* no need to print the trailing zero, so use only len */
835 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700836 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (__ppos >= len)
838 return 0;
839 if (count > len - __ppos)
840 count = len - __ppos;
841 if (copy_to_user(buf, __buf + __ppos, count))
842 return -EFAULT;
843 *ppos = __ppos + count;
844 return count;
845}
846
847static ssize_t seccomp_write(struct file *file, const char __user *buf,
848 size_t count, loff_t *ppos)
849{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700850 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 char __buf[20], *end;
852 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700853 ssize_t result;
854
855 result = -ESRCH;
856 if (!tsk)
857 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700860 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700862 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Eric W. Biederman99f89552006-06-26 00:25:55 -0700864 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 memset(__buf, 0, sizeof(__buf));
866 count = min(count, sizeof(__buf) - 1);
867 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700868 goto out;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 seccomp_mode = simple_strtoul(__buf, &end, 0);
871 if (*end == '\n')
872 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700873 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
875 tsk->seccomp.mode = seccomp_mode;
876 set_tsk_thread_flag(tsk, TIF_SECCOMP);
877 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700878 goto out;
879 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700881 goto out;
882 result = end - __buf;
883out:
884 put_task_struct(tsk);
885out_no_task:
886 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Arjan van de Ven00977a52007-02-12 00:55:34 -0800889static const struct file_operations proc_seccomp_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 .read = seccomp_read,
891 .write = seccomp_write,
892};
893#endif /* CONFIG_SECCOMP */
894
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800895#ifdef CONFIG_FAULT_INJECTION
896static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
897 size_t count, loff_t *ppos)
898{
899 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
900 char buffer[PROC_NUMBUF];
901 size_t len;
902 int make_it_fail;
903 loff_t __ppos = *ppos;
904
905 if (!task)
906 return -ESRCH;
907 make_it_fail = task->make_it_fail;
908 put_task_struct(task);
909
910 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
911 if (__ppos >= len)
912 return 0;
913 if (count > len-__ppos)
914 count = len-__ppos;
915 if (copy_to_user(buf, buffer + __ppos, count))
916 return -EFAULT;
917 *ppos = __ppos + count;
918 return count;
919}
920
921static ssize_t proc_fault_inject_write(struct file * file,
922 const char __user * buf, size_t count, loff_t *ppos)
923{
924 struct task_struct *task;
925 char buffer[PROC_NUMBUF], *end;
926 int make_it_fail;
927
928 if (!capable(CAP_SYS_RESOURCE))
929 return -EPERM;
930 memset(buffer, 0, sizeof(buffer));
931 if (count > sizeof(buffer) - 1)
932 count = sizeof(buffer) - 1;
933 if (copy_from_user(buffer, buf, count))
934 return -EFAULT;
935 make_it_fail = simple_strtol(buffer, &end, 0);
936 if (*end == '\n')
937 end++;
938 task = get_proc_task(file->f_dentry->d_inode);
939 if (!task)
940 return -ESRCH;
941 task->make_it_fail = make_it_fail;
942 put_task_struct(task);
943 if (end - buffer == 0)
944 return -EIO;
945 return end - buffer;
946}
947
Arjan van de Ven00977a52007-02-12 00:55:34 -0800948static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800949 .read = proc_fault_inject_read,
950 .write = proc_fault_inject_write,
951};
952#endif
953
Al Viro008b1502005-08-20 00:17:39 +0100954static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 struct inode *inode = dentry->d_inode;
957 int error = -EACCES;
958
959 /* We don't need a base pointer in the /proc filesystem */
960 path_release(nd);
961
Eric W. Biederman778c1142006-06-26 00:25:58 -0700962 /* Are we allowed to snoop on the tasks file descriptors? */
963 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
967 nd->last_type = LAST_BIND;
968out:
Al Viro008b1502005-08-20 00:17:39 +0100969 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
973 char __user *buffer, int buflen)
974{
975 struct inode * inode;
976 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
977 int len;
978
979 if (!tmp)
980 return -ENOMEM;
981
982 inode = dentry->d_inode;
983 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
984 len = PTR_ERR(path);
985 if (IS_ERR(path))
986 goto out;
987 len = tmp + PAGE_SIZE - 1 - path;
988
989 if (len > buflen)
990 len = buflen;
991 if (copy_to_user(buffer, path, len))
992 len = -EFAULT;
993 out:
994 free_page((unsigned long)tmp);
995 return len;
996}
997
998static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
999{
1000 int error = -EACCES;
1001 struct inode *inode = dentry->d_inode;
1002 struct dentry *de;
1003 struct vfsmount *mnt = NULL;
1004
Eric W. Biederman778c1142006-06-26 00:25:58 -07001005 /* Are we allowed to snoop on the tasks file descriptors? */
1006 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1010 if (error)
1011 goto out;
1012
1013 error = do_proc_readlink(de, mnt, buffer, buflen);
1014 dput(de);
1015 mntput(mnt);
1016out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return error;
1018}
1019
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001020static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001022 .follow_link = proc_pid_follow_link,
1023 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024};
1025
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001026
1027/* building an inode */
1028
1029static int task_dumpable(struct task_struct *task)
1030{
1031 int dumpable = 0;
1032 struct mm_struct *mm;
1033
1034 task_lock(task);
1035 mm = task->mm;
1036 if (mm)
1037 dumpable = mm->dumpable;
1038 task_unlock(task);
1039 if(dumpable == 1)
1040 return 1;
1041 return 0;
1042}
1043
1044
Eric W. Biederman61a28782006-10-02 02:18:49 -07001045static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001046{
1047 struct inode * inode;
1048 struct proc_inode *ei;
1049
1050 /* We need a new inode */
1051
1052 inode = new_inode(sb);
1053 if (!inode)
1054 goto out;
1055
1056 /* Common stuff */
1057 ei = PROC_I(inode);
1058 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001059 inode->i_op = &proc_def_inode_operations;
1060
1061 /*
1062 * grab the reference to task.
1063 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001064 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001065 if (!ei->pid)
1066 goto out_unlock;
1067
1068 inode->i_uid = 0;
1069 inode->i_gid = 0;
1070 if (task_dumpable(task)) {
1071 inode->i_uid = task->euid;
1072 inode->i_gid = task->egid;
1073 }
1074 security_task_to_inode(task, inode);
1075
1076out:
1077 return inode;
1078
1079out_unlock:
1080 iput(inode);
1081 return NULL;
1082}
1083
1084static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1085{
1086 struct inode *inode = dentry->d_inode;
1087 struct task_struct *task;
1088 generic_fillattr(inode, stat);
1089
1090 rcu_read_lock();
1091 stat->uid = 0;
1092 stat->gid = 0;
1093 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1094 if (task) {
1095 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1096 task_dumpable(task)) {
1097 stat->uid = task->euid;
1098 stat->gid = task->egid;
1099 }
1100 }
1101 rcu_read_unlock();
1102 return 0;
1103}
1104
1105/* dentry stuff */
1106
1107/*
1108 * Exceptional case: normally we are not allowed to unhash a busy
1109 * directory. In this case, however, we can do it - no aliasing problems
1110 * due to the way we treat inodes.
1111 *
1112 * Rewrite the inode's ownerships here because the owning task may have
1113 * performed a setuid(), etc.
1114 *
1115 * Before the /proc/pid/status file was created the only way to read
1116 * the effective uid of a /process was to stat /proc/pid. Reading
1117 * /proc/pid/status is slow enough that procps and other packages
1118 * kept stating /proc/pid. To keep the rules in /proc simple I have
1119 * made this apply to all per process world readable and executable
1120 * directories.
1121 */
1122static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1123{
1124 struct inode *inode = dentry->d_inode;
1125 struct task_struct *task = get_proc_task(inode);
1126 if (task) {
1127 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1128 task_dumpable(task)) {
1129 inode->i_uid = task->euid;
1130 inode->i_gid = task->egid;
1131 } else {
1132 inode->i_uid = 0;
1133 inode->i_gid = 0;
1134 }
1135 inode->i_mode &= ~(S_ISUID | S_ISGID);
1136 security_task_to_inode(task, inode);
1137 put_task_struct(task);
1138 return 1;
1139 }
1140 d_drop(dentry);
1141 return 0;
1142}
1143
1144static int pid_delete_dentry(struct dentry * dentry)
1145{
1146 /* Is the task we represent dead?
1147 * If so, then don't put the dentry on the lru list,
1148 * kill it immediately.
1149 */
1150 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1151}
1152
1153static struct dentry_operations pid_dentry_operations =
1154{
1155 .d_revalidate = pid_revalidate,
1156 .d_delete = pid_delete_dentry,
1157};
1158
1159/* Lookups */
1160
Eric Dumazetc5141e62007-05-08 00:26:15 -07001161typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1162 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001163
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001164/*
1165 * Fill a directory entry.
1166 *
1167 * If possible create the dcache entry and derive our inode number and
1168 * file type from dcache entry.
1169 *
1170 * Since all of the proc inode numbers are dynamically generated, the inode
1171 * numbers do not exist until the inode is cache. This means creating the
1172 * the dcache entry in readdir is necessary to keep the inode numbers
1173 * reported by readdir in sync with the inode numbers reported
1174 * by stat.
1175 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001176static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1177 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001178 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001179{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001180 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001181 struct inode *inode;
1182 struct qstr qname;
1183 ino_t ino = 0;
1184 unsigned type = DT_UNKNOWN;
1185
1186 qname.name = name;
1187 qname.len = len;
1188 qname.hash = full_name_hash(name, len);
1189
1190 child = d_lookup(dir, &qname);
1191 if (!child) {
1192 struct dentry *new;
1193 new = d_alloc(dir, &qname);
1194 if (new) {
1195 child = instantiate(dir->d_inode, new, task, ptr);
1196 if (child)
1197 dput(new);
1198 else
1199 child = new;
1200 }
1201 }
1202 if (!child || IS_ERR(child) || !child->d_inode)
1203 goto end_instantiate;
1204 inode = child->d_inode;
1205 if (inode) {
1206 ino = inode->i_ino;
1207 type = inode->i_mode >> 12;
1208 }
1209 dput(child);
1210end_instantiate:
1211 if (!ino)
1212 ino = find_inode_number(dir, &qname);
1213 if (!ino)
1214 ino = 1;
1215 return filldir(dirent, name, len, filp->f_pos, ino, type);
1216}
1217
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001218static unsigned name_to_int(struct dentry *dentry)
1219{
1220 const char *name = dentry->d_name.name;
1221 int len = dentry->d_name.len;
1222 unsigned n = 0;
1223
1224 if (len > 1 && *name == '0')
1225 goto out;
1226 while (len-- > 0) {
1227 unsigned c = *name++ - '0';
1228 if (c > 9)
1229 goto out;
1230 if (n >= (~0U-9)/10)
1231 goto out;
1232 n *= 10;
1233 n += c;
1234 }
1235 return n;
1236out:
1237 return ~0U;
1238}
1239
Miklos Szeredi27932742007-05-08 00:26:17 -07001240#define PROC_FDINFO_MAX 64
1241
1242static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1243 struct vfsmount **mnt, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001244{
1245 struct task_struct *task = get_proc_task(inode);
1246 struct files_struct *files = NULL;
1247 struct file *file;
1248 int fd = proc_fd(inode);
1249
1250 if (task) {
1251 files = get_files_struct(task);
1252 put_task_struct(task);
1253 }
1254 if (files) {
1255 /*
1256 * We are not taking a ref to the file structure, so we must
1257 * hold ->file_lock.
1258 */
1259 spin_lock(&files->file_lock);
1260 file = fcheck_files(files, fd);
1261 if (file) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001262 if (mnt)
1263 *mnt = mntget(file->f_path.mnt);
1264 if (dentry)
1265 *dentry = dget(file->f_path.dentry);
1266 if (info)
1267 snprintf(info, PROC_FDINFO_MAX,
1268 "pos:\t%lli\n"
1269 "flags:\t0%o\n",
1270 (long long) file->f_pos,
1271 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001272 spin_unlock(&files->file_lock);
1273 put_files_struct(files);
1274 return 0;
1275 }
1276 spin_unlock(&files->file_lock);
1277 put_files_struct(files);
1278 }
1279 return -ENOENT;
1280}
1281
Miklos Szeredi27932742007-05-08 00:26:17 -07001282static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1283 struct vfsmount **mnt)
1284{
1285 return proc_fd_info(inode, dentry, mnt, NULL);
1286}
1287
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001288static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1289{
1290 struct inode *inode = dentry->d_inode;
1291 struct task_struct *task = get_proc_task(inode);
1292 int fd = proc_fd(inode);
1293 struct files_struct *files;
1294
1295 if (task) {
1296 files = get_files_struct(task);
1297 if (files) {
1298 rcu_read_lock();
1299 if (fcheck_files(files, fd)) {
1300 rcu_read_unlock();
1301 put_files_struct(files);
1302 if (task_dumpable(task)) {
1303 inode->i_uid = task->euid;
1304 inode->i_gid = task->egid;
1305 } else {
1306 inode->i_uid = 0;
1307 inode->i_gid = 0;
1308 }
1309 inode->i_mode &= ~(S_ISUID | S_ISGID);
1310 security_task_to_inode(task, inode);
1311 put_task_struct(task);
1312 return 1;
1313 }
1314 rcu_read_unlock();
1315 put_files_struct(files);
1316 }
1317 put_task_struct(task);
1318 }
1319 d_drop(dentry);
1320 return 0;
1321}
1322
1323static struct dentry_operations tid_fd_dentry_operations =
1324{
1325 .d_revalidate = tid_fd_revalidate,
1326 .d_delete = pid_delete_dentry,
1327};
1328
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001329static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001330 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001331{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001332 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001333 struct file *file;
1334 struct files_struct *files;
1335 struct inode *inode;
1336 struct proc_inode *ei;
1337 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001338
Eric W. Biederman61a28782006-10-02 02:18:49 -07001339 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001340 if (!inode)
1341 goto out;
1342 ei = PROC_I(inode);
1343 ei->fd = fd;
1344 files = get_files_struct(task);
1345 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001346 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001347 inode->i_mode = S_IFLNK;
1348
1349 /*
1350 * We are not taking a ref to the file structure, so we must
1351 * hold ->file_lock.
1352 */
1353 spin_lock(&files->file_lock);
1354 file = fcheck_files(files, fd);
1355 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001356 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001357 if (file->f_mode & 1)
1358 inode->i_mode |= S_IRUSR | S_IXUSR;
1359 if (file->f_mode & 2)
1360 inode->i_mode |= S_IWUSR | S_IXUSR;
1361 spin_unlock(&files->file_lock);
1362 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001363
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001364 inode->i_op = &proc_pid_link_inode_operations;
1365 inode->i_size = 64;
1366 ei->op.proc_get_link = proc_fd_link;
1367 dentry->d_op = &tid_fd_dentry_operations;
1368 d_add(dentry, inode);
1369 /* Close the race of the process dying before we return the dentry */
1370 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001371 error = NULL;
1372
1373 out:
1374 return error;
1375out_unlock:
1376 spin_unlock(&files->file_lock);
1377 put_files_struct(files);
1378out_iput:
1379 iput(inode);
1380 goto out;
1381}
1382
Miklos Szeredi27932742007-05-08 00:26:17 -07001383static struct dentry *proc_lookupfd_common(struct inode *dir,
1384 struct dentry *dentry,
1385 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001386{
1387 struct task_struct *task = get_proc_task(dir);
1388 unsigned fd = name_to_int(dentry);
1389 struct dentry *result = ERR_PTR(-ENOENT);
1390
1391 if (!task)
1392 goto out_no_task;
1393 if (fd == ~0U)
1394 goto out;
1395
Miklos Szeredi27932742007-05-08 00:26:17 -07001396 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001397out:
1398 put_task_struct(task);
1399out_no_task:
1400 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001401}
1402
Miklos Szeredi27932742007-05-08 00:26:17 -07001403static int proc_readfd_common(struct file * filp, void * dirent,
1404 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001406 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001407 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001408 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 unsigned int fd, tid, ino;
1410 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001412 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001415 if (!p)
1416 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 retval = 0;
1418 tid = p->pid;
1419
1420 fd = filp->f_pos;
1421 switch (fd) {
1422 case 0:
1423 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1424 goto out;
1425 filp->f_pos++;
1426 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001427 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1429 goto out;
1430 filp->f_pos++;
1431 default:
1432 files = get_files_struct(p);
1433 if (!files)
1434 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001435 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001436 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001438 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001440 char name[PROC_NUMBUF];
1441 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (!fcheck_files(files, fd))
1444 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001445 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Miklos Szeredi27932742007-05-08 00:26:17 -07001447 len = snprintf(name, sizeof(name), "%d", fd);
1448 if (proc_fill_cache(filp, dirent, filldir,
1449 name, len, instantiate,
1450 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001451 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 break;
1453 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001454 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001456 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 put_files_struct(files);
1458 }
1459out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001460 put_task_struct(p);
1461out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return retval;
1463}
1464
Miklos Szeredi27932742007-05-08 00:26:17 -07001465static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1466 struct nameidata *nd)
1467{
1468 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1469}
1470
1471static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1472{
1473 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1474}
1475
1476static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1477 size_t len, loff_t *ppos)
1478{
1479 char tmp[PROC_FDINFO_MAX];
1480 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1481 if (!err)
1482 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1483 return err;
1484}
1485
1486static const struct file_operations proc_fdinfo_file_operations = {
1487 .open = nonseekable_open,
1488 .read = proc_fdinfo_read,
1489};
1490
Arjan van de Ven00977a52007-02-12 00:55:34 -08001491static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 .read = generic_read_dir,
1493 .readdir = proc_readfd,
1494};
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001497 * /proc/pid/fd needs a special permission handler so that a process can still
1498 * access /proc/self/fd after it has executed a setuid().
1499 */
1500static int proc_fd_permission(struct inode *inode, int mask,
1501 struct nameidata *nd)
1502{
1503 int rv;
1504
1505 rv = generic_permission(inode, mask, NULL);
1506 if (rv == 0)
1507 return 0;
1508 if (task_pid(current) == proc_pid(inode))
1509 rv = 0;
1510 return rv;
1511}
1512
1513/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 * proc directories can do almost nothing..
1515 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001516static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001518 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001519 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520};
1521
Miklos Szeredi27932742007-05-08 00:26:17 -07001522static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1523 struct dentry *dentry, struct task_struct *task, const void *ptr)
1524{
1525 unsigned fd = *(unsigned *)ptr;
1526 struct inode *inode;
1527 struct proc_inode *ei;
1528 struct dentry *error = ERR_PTR(-ENOENT);
1529
1530 inode = proc_pid_make_inode(dir->i_sb, task);
1531 if (!inode)
1532 goto out;
1533 ei = PROC_I(inode);
1534 ei->fd = fd;
1535 inode->i_mode = S_IFREG | S_IRUSR;
1536 inode->i_fop = &proc_fdinfo_file_operations;
1537 dentry->d_op = &tid_fd_dentry_operations;
1538 d_add(dentry, inode);
1539 /* Close the race of the process dying before we return the dentry */
1540 if (tid_fd_revalidate(dentry, NULL))
1541 error = NULL;
1542
1543 out:
1544 return error;
1545}
1546
1547static struct dentry *proc_lookupfdinfo(struct inode *dir,
1548 struct dentry *dentry,
1549 struct nameidata *nd)
1550{
1551 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1552}
1553
1554static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1555{
1556 return proc_readfd_common(filp, dirent, filldir,
1557 proc_fdinfo_instantiate);
1558}
1559
1560static const struct file_operations proc_fdinfo_operations = {
1561 .read = generic_read_dir,
1562 .readdir = proc_readfdinfo,
1563};
1564
1565/*
1566 * proc directories can do almost nothing..
1567 */
1568static const struct inode_operations proc_fdinfo_inode_operations = {
1569 .lookup = proc_lookupfdinfo,
1570 .setattr = proc_setattr,
1571};
1572
1573
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001574static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001575 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001576{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001577 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001578 struct inode *inode;
1579 struct proc_inode *ei;
1580 struct dentry *error = ERR_PTR(-EINVAL);
1581
Eric W. Biederman61a28782006-10-02 02:18:49 -07001582 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001583 if (!inode)
1584 goto out;
1585
1586 ei = PROC_I(inode);
1587 inode->i_mode = p->mode;
1588 if (S_ISDIR(inode->i_mode))
1589 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1590 if (p->iop)
1591 inode->i_op = p->iop;
1592 if (p->fop)
1593 inode->i_fop = p->fop;
1594 ei->op = p->op;
1595 dentry->d_op = &pid_dentry_operations;
1596 d_add(dentry, inode);
1597 /* Close the race of the process dying before we return the dentry */
1598 if (pid_revalidate(dentry, NULL))
1599 error = NULL;
1600out:
1601 return error;
1602}
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604static struct dentry *proc_pident_lookup(struct inode *dir,
1605 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001606 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001607 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001610 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001611 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001612 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001614 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 inode = NULL;
1616
Eric W. Biederman99f89552006-06-26 00:25:55 -07001617 if (!task)
1618 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001620 /*
1621 * Yes, it does not scale. And it should not. Don't add
1622 * new entries into /proc/<tgid>/ without very good reasons.
1623 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001624 last = &ents[nents - 1];
1625 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (p->len != dentry->d_name.len)
1627 continue;
1628 if (!memcmp(dentry->d_name.name, p->name, p->len))
1629 break;
1630 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001631 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 goto out;
1633
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001634 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001636 put_task_struct(task);
1637out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001638 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Eric Dumazetc5141e62007-05-08 00:26:15 -07001641static int proc_pident_fill_cache(struct file *filp, void *dirent,
1642 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001643{
1644 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1645 proc_pident_instantiate, task, p);
1646}
1647
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001648static int proc_pident_readdir(struct file *filp,
1649 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001650 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001651{
1652 int i;
1653 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001654 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001655 struct inode *inode = dentry->d_inode;
1656 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001657 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001658 ino_t ino;
1659 int ret;
1660
1661 ret = -ENOENT;
1662 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001663 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001664
1665 ret = 0;
1666 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001667 i = filp->f_pos;
1668 switch (i) {
1669 case 0:
1670 ino = inode->i_ino;
1671 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1672 goto out;
1673 i++;
1674 filp->f_pos++;
1675 /* fall through */
1676 case 1:
1677 ino = parent_ino(dentry);
1678 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1679 goto out;
1680 i++;
1681 filp->f_pos++;
1682 /* fall through */
1683 default:
1684 i -= 2;
1685 if (i >= nents) {
1686 ret = 1;
1687 goto out;
1688 }
1689 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001690 last = &ents[nents - 1];
1691 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001692 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001693 goto out;
1694 filp->f_pos++;
1695 p++;
1696 }
1697 }
1698
1699 ret = 1;
1700out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001701 put_task_struct(task);
1702out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001703 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001707static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1708 size_t count, loff_t *ppos)
1709{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001710 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001711 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001712 ssize_t length;
1713 struct task_struct *task = get_proc_task(inode);
1714
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001715 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001716 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001717
1718 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001719 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001720 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001721 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001722 if (length > 0)
1723 length = simple_read_from_buffer(buf, count, ppos, p, length);
1724 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001725 return length;
1726}
1727
1728static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1729 size_t count, loff_t *ppos)
1730{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001731 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001732 char *page;
1733 ssize_t length;
1734 struct task_struct *task = get_proc_task(inode);
1735
1736 length = -ESRCH;
1737 if (!task)
1738 goto out_no_task;
1739 if (count > PAGE_SIZE)
1740 count = PAGE_SIZE;
1741
1742 /* No partial writes. */
1743 length = -EINVAL;
1744 if (*ppos != 0)
1745 goto out;
1746
1747 length = -ENOMEM;
1748 page = (char*)__get_free_page(GFP_USER);
1749 if (!page)
1750 goto out;
1751
1752 length = -EFAULT;
1753 if (copy_from_user(page, buf, count))
1754 goto out_free;
1755
1756 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001757 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001758 (void*)page, count);
1759out_free:
1760 free_page((unsigned long) page);
1761out:
1762 put_task_struct(task);
1763out_no_task:
1764 return length;
1765}
1766
Arjan van de Ven00977a52007-02-12 00:55:34 -08001767static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768 .read = proc_pid_attr_read,
1769 .write = proc_pid_attr_write,
1770};
1771
Eric Dumazetc5141e62007-05-08 00:26:15 -07001772static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001773 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1774 REG("prev", S_IRUGO, pid_attr),
1775 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1776 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1777 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1778 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001779};
1780
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001781static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 void * dirent, filldir_t filldir)
1783{
1784 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001785 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Arjan van de Ven00977a52007-02-12 00:55:34 -08001788static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001790 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791};
1792
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001793static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 struct dentry *dentry, struct nameidata *nd)
1795{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001796 return proc_pident_lookup(dir, dentry,
1797 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001800static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001801 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001802 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001803 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804};
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806#endif
1807
1808/*
1809 * /proc/self:
1810 */
1811static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1812 int buflen)
1813{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001814 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 sprintf(tmp, "%d", current->tgid);
1816 return vfs_readlink(dentry,buffer,buflen,tmp);
1817}
1818
Al Viro008b1502005-08-20 00:17:39 +01001819static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001821 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001823 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001826static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 .readlink = proc_self_readlink,
1828 .follow_link = proc_self_follow_link,
1829};
1830
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001831/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001832 * proc base
1833 *
1834 * These are the directory entries in the root directory of /proc
1835 * that properly belong to the /proc filesystem, as they describe
1836 * describe something that is process related.
1837 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07001838static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001839 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001840 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001841};
1842
1843/*
1844 * Exceptional case: normally we are not allowed to unhash a busy
1845 * directory. In this case, however, we can do it - no aliasing problems
1846 * due to the way we treat inodes.
1847 */
1848static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1849{
1850 struct inode *inode = dentry->d_inode;
1851 struct task_struct *task = get_proc_task(inode);
1852 if (task) {
1853 put_task_struct(task);
1854 return 1;
1855 }
1856 d_drop(dentry);
1857 return 0;
1858}
1859
1860static struct dentry_operations proc_base_dentry_operations =
1861{
1862 .d_revalidate = proc_base_revalidate,
1863 .d_delete = pid_delete_dentry,
1864};
1865
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001866static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001867 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001868{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001869 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001870 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001871 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001872 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001873
1874 /* Allocate the inode */
1875 error = ERR_PTR(-ENOMEM);
1876 inode = new_inode(dir->i_sb);
1877 if (!inode)
1878 goto out;
1879
1880 /* Initialize the inode */
1881 ei = PROC_I(inode);
1882 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001883
1884 /*
1885 * grab the reference to the task.
1886 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001887 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001888 if (!ei->pid)
1889 goto out_iput;
1890
1891 inode->i_uid = 0;
1892 inode->i_gid = 0;
1893 inode->i_mode = p->mode;
1894 if (S_ISDIR(inode->i_mode))
1895 inode->i_nlink = 2;
1896 if (S_ISLNK(inode->i_mode))
1897 inode->i_size = 64;
1898 if (p->iop)
1899 inode->i_op = p->iop;
1900 if (p->fop)
1901 inode->i_fop = p->fop;
1902 ei->op = p->op;
1903 dentry->d_op = &proc_base_dentry_operations;
1904 d_add(dentry, inode);
1905 error = NULL;
1906out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001907 return error;
1908out_iput:
1909 iput(inode);
1910 goto out;
1911}
1912
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001913static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1914{
1915 struct dentry *error;
1916 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001917 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001918
1919 error = ERR_PTR(-ENOENT);
1920
1921 if (!task)
1922 goto out_no_task;
1923
1924 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001925 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1926 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001927 if (p->len != dentry->d_name.len)
1928 continue;
1929 if (!memcmp(dentry->d_name.name, p->name, p->len))
1930 break;
1931 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001932 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001933 goto out;
1934
1935 error = proc_base_instantiate(dir, dentry, task, p);
1936
1937out:
1938 put_task_struct(task);
1939out_no_task:
1940 return error;
1941}
1942
Eric Dumazetc5141e62007-05-08 00:26:15 -07001943static int proc_base_fill_cache(struct file *filp, void *dirent,
1944 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001945{
1946 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1947 proc_base_instantiate, task, p);
1948}
1949
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001950#ifdef CONFIG_TASK_IO_ACCOUNTING
1951static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1952{
1953 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001954#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001955 "rchar: %llu\n"
1956 "wchar: %llu\n"
1957 "syscr: %llu\n"
1958 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001959#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001960 "read_bytes: %llu\n"
1961 "write_bytes: %llu\n"
1962 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001963#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001964 (unsigned long long)task->rchar,
1965 (unsigned long long)task->wchar,
1966 (unsigned long long)task->syscr,
1967 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001968#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001969 (unsigned long long)task->ioac.read_bytes,
1970 (unsigned long long)task->ioac.write_bytes,
1971 (unsigned long long)task->ioac.cancelled_write_bytes);
1972}
1973#endif
1974
Eric W. Biederman801199c2006-10-02 02:18:48 -07001975/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001976 * Thread groups
1977 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08001978static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001979static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001980
Eric Dumazetc5141e62007-05-08 00:26:15 -07001981static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001982 DIR("task", S_IRUGO|S_IXUGO, task),
1983 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07001984 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07001985 INF("environ", S_IRUSR, pid_environ),
1986 INF("auxv", S_IRUSR, pid_auxv),
1987 INF("status", S_IRUGO, pid_status),
1988 INF("cmdline", S_IRUGO, pid_cmdline),
1989 INF("stat", S_IRUGO, tgid_stat),
1990 INF("statm", S_IRUGO, pid_statm),
1991 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001992#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001993 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001994#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001995 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001996#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07001997 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001998#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001999 LNK("cwd", cwd),
2000 LNK("root", root),
2001 LNK("exe", exe),
2002 REG("mounts", S_IRUGO, mounts),
2003 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002004#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002005 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002006 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002007#endif
2008#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002009 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002010#endif
2011#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002012 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002013#endif
2014#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002015 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002016#endif
2017#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002018 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002019#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002020 INF("oom_score", S_IRUGO, oom_score),
2021 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002022#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002023 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002024#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002025#ifdef CONFIG_FAULT_INJECTION
2026 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2027#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002028#ifdef CONFIG_TASK_IO_ACCOUNTING
2029 INF("io", S_IRUGO, pid_io_accounting),
2030#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002031};
2032
2033static int proc_tgid_base_readdir(struct file * filp,
2034 void * dirent, filldir_t filldir)
2035{
2036 return proc_pident_readdir(filp,dirent,filldir,
2037 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2038}
2039
Arjan van de Ven00977a52007-02-12 00:55:34 -08002040static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002041 .read = generic_read_dir,
2042 .readdir = proc_tgid_base_readdir,
2043};
2044
2045static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002046 return proc_pident_lookup(dir, dentry,
2047 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002048}
2049
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002050static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002051 .lookup = proc_tgid_base_lookup,
2052 .getattr = pid_getattr,
2053 .setattr = proc_setattr,
2054};
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07002057 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002059 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002061 * Looks in the dcache for
2062 * /proc/@pid
2063 * /proc/@tgid/task/@pid
2064 * if either directory is present flushes it and all of it'ts children
2065 * from the dcache.
2066 *
2067 * It is safe and reasonable to cache /proc entries for a task until
2068 * that task exits. After that they just clog up the dcache with
2069 * useless entries, possibly causing useful dcache entries to be
2070 * flushed instead. This routine is proved to flush those useless
2071 * dcache entries at process exit time.
2072 *
2073 * NOTE: This routine is just an optimization so it does not guarantee
2074 * that no dcache entries will exist at process exit time it
2075 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07002077void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002079 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002080 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002081 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Eric W. Biederman48e64842006-06-26 00:25:48 -07002083 name.name = buf;
2084 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2085 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2086 if (dentry) {
2087 shrink_dcache_parent(dentry);
2088 d_drop(dentry);
2089 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Eric W. Biederman48e64842006-06-26 00:25:48 -07002092 if (thread_group_leader(task))
2093 goto out;
2094
2095 name.name = buf;
2096 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2097 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2098 if (!leader)
2099 goto out;
2100
2101 name.name = "task";
2102 name.len = strlen(name.name);
2103 dir = d_hash_and_lookup(leader, &name);
2104 if (!dir)
2105 goto out_put_leader;
2106
2107 name.name = buf;
2108 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2109 dentry = d_hash_and_lookup(dir, &name);
2110 if (dentry) {
2111 shrink_dcache_parent(dentry);
2112 d_drop(dentry);
2113 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002115
2116 dput(dir);
2117out_put_leader:
2118 dput(leader);
2119out:
2120 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Adrian Bunk9711ef92006-12-06 20:38:31 -08002123static struct dentry *proc_pid_instantiate(struct inode *dir,
2124 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002125 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002126{
2127 struct dentry *error = ERR_PTR(-ENOENT);
2128 struct inode *inode;
2129
Eric W. Biederman61a28782006-10-02 02:18:49 -07002130 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002131 if (!inode)
2132 goto out;
2133
2134 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2135 inode->i_op = &proc_tgid_base_inode_operations;
2136 inode->i_fop = &proc_tgid_base_operations;
2137 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002138 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002139#ifdef CONFIG_SECURITY
2140 inode->i_nlink += 1;
2141#endif
2142
2143 dentry->d_op = &pid_dentry_operations;
2144
2145 d_add(dentry, inode);
2146 /* Close the race of the process dying before we return the dentry */
2147 if (pid_revalidate(dentry, NULL))
2148 error = NULL;
2149out:
2150 return error;
2151}
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2154{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002155 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Eric W. Biederman801199c2006-10-02 02:18:48 -07002159 result = proc_base_lookup(dir, dentry);
2160 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2161 goto out;
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 tgid = name_to_int(dentry);
2164 if (tgid == ~0U)
2165 goto out;
2166
Eric W. Biedermande758732006-06-26 00:25:51 -07002167 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 task = find_task_by_pid(tgid);
2169 if (task)
2170 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002171 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (!task)
2173 goto out;
2174
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002175 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002176 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002178 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002182 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002183 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002185static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002187 struct task_struct *task;
2188 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002190 rcu_read_lock();
2191retry:
2192 task = NULL;
2193 pid = find_ge_pid(tgid);
2194 if (pid) {
2195 tgid = pid->nr + 1;
2196 task = pid_task(pid, PIDTYPE_PID);
2197 /* What we to know is if the pid we have find is the
2198 * pid of a thread_group_leader. Testing for task
2199 * being a thread_group_leader is the obvious thing
2200 * todo but there is a window when it fails, due to
2201 * the pid transfer logic in de_thread.
2202 *
2203 * So we perform the straight forward test of seeing
2204 * if the pid we have found is the pid of a thread
2205 * group leader, and don't worry if the task we have
2206 * found doesn't happen to be a thread group leader.
2207 * As we don't care in the case of readdir.
2208 */
2209 if (!task || !has_group_leader_pid(task))
2210 goto retry;
2211 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002213 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002214 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002217#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Eric W. Biederman61a28782006-10-02 02:18:49 -07002219static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2220 struct task_struct *task, int tgid)
2221{
2222 char name[PROC_NUMBUF];
2223 int len = snprintf(name, sizeof(name), "%d", tgid);
2224 return proc_fill_cache(filp, dirent, filldir, name, len,
2225 proc_pid_instantiate, task, NULL);
2226}
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228/* for the /proc/ directory itself, after non-process stuff has been done */
2229int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002232 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002233 struct task_struct *task;
2234 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Eric W. Biederman61a28782006-10-02 02:18:49 -07002236 if (!reaper)
2237 goto out_no_task;
2238
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002239 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002240 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002241 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002242 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
2244
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002245 tgid = filp->f_pos - TGID_OFFSET;
2246 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002247 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002248 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002249 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002250 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002251 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002252 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002253 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002256 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2257out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002258 put_task_struct(reaper);
2259out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return 0;
2261}
2262
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002263/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002264 * Tasks
2265 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002266static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002267 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002268 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002269 INF("environ", S_IRUSR, pid_environ),
2270 INF("auxv", S_IRUSR, pid_auxv),
2271 INF("status", S_IRUGO, pid_status),
2272 INF("cmdline", S_IRUGO, pid_cmdline),
2273 INF("stat", S_IRUGO, tid_stat),
2274 INF("statm", S_IRUGO, pid_statm),
2275 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002276#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002277 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002278#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002279 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002280#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002281 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002282#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002283 LNK("cwd", cwd),
2284 LNK("root", root),
2285 LNK("exe", exe),
2286 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002287#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002288 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002289 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002290#endif
2291#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002292 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002293#endif
2294#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002295 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002296#endif
2297#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002298 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002299#endif
2300#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002301 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002302#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002303 INF("oom_score", S_IRUGO, oom_score),
2304 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002305#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002306 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002307#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002308#ifdef CONFIG_FAULT_INJECTION
2309 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2310#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002311};
2312
2313static int proc_tid_base_readdir(struct file * filp,
2314 void * dirent, filldir_t filldir)
2315{
2316 return proc_pident_readdir(filp,dirent,filldir,
2317 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2318}
2319
2320static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002321 return proc_pident_lookup(dir, dentry,
2322 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002323}
2324
Arjan van de Ven00977a52007-02-12 00:55:34 -08002325static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002326 .read = generic_read_dir,
2327 .readdir = proc_tid_base_readdir,
2328};
2329
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002330static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002331 .lookup = proc_tid_base_lookup,
2332 .getattr = pid_getattr,
2333 .setattr = proc_setattr,
2334};
2335
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002336static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002337 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002338{
2339 struct dentry *error = ERR_PTR(-ENOENT);
2340 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002341 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002342
2343 if (!inode)
2344 goto out;
2345 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2346 inode->i_op = &proc_tid_base_inode_operations;
2347 inode->i_fop = &proc_tid_base_operations;
2348 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002349 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002350#ifdef CONFIG_SECURITY
2351 inode->i_nlink += 1;
2352#endif
2353
2354 dentry->d_op = &pid_dentry_operations;
2355
2356 d_add(dentry, inode);
2357 /* Close the race of the process dying before we return the dentry */
2358 if (pid_revalidate(dentry, NULL))
2359 error = NULL;
2360out:
2361 return error;
2362}
2363
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002364static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2365{
2366 struct dentry *result = ERR_PTR(-ENOENT);
2367 struct task_struct *task;
2368 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002369 unsigned tid;
2370
2371 if (!leader)
2372 goto out_no_task;
2373
2374 tid = name_to_int(dentry);
2375 if (tid == ~0U)
2376 goto out;
2377
2378 rcu_read_lock();
2379 task = find_task_by_pid(tid);
2380 if (task)
2381 get_task_struct(task);
2382 rcu_read_unlock();
2383 if (!task)
2384 goto out;
2385 if (leader->tgid != task->tgid)
2386 goto out_drop_task;
2387
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002388 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002389out_drop_task:
2390 put_task_struct(task);
2391out:
2392 put_task_struct(leader);
2393out_no_task:
2394 return result;
2395}
2396
2397/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002398 * Find the first tid of a thread group to return to user space.
2399 *
2400 * Usually this is just the thread group leader, but if the users
2401 * buffer was too small or there was a seek into the middle of the
2402 * directory we have more work todo.
2403 *
2404 * In the case of a short read we start with find_task_by_pid.
2405 *
2406 * In the case of a seek we start with the leader and walk nr
2407 * threads past it.
2408 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002409static struct task_struct *first_tid(struct task_struct *leader,
2410 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002411{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002412 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002413
Eric W. Biedermancc288732006-06-26 00:26:01 -07002414 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002415 /* Attempt to start with the pid of a thread */
2416 if (tid && (nr > 0)) {
2417 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002418 if (pos && (pos->group_leader == leader))
2419 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002420 }
2421
2422 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002423 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002424 if (nr && nr >= get_nr_threads(leader))
2425 goto out;
2426
2427 /* If we haven't found our starting place yet start
2428 * with the leader and walk nr threads forward.
2429 */
2430 for (pos = leader; nr > 0; --nr) {
2431 pos = next_thread(pos);
2432 if (pos == leader) {
2433 pos = NULL;
2434 goto out;
2435 }
2436 }
2437found:
2438 get_task_struct(pos);
2439out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002440 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002441 return pos;
2442}
2443
2444/*
2445 * Find the next thread in the thread list.
2446 * Return NULL if there is an error or no next thread.
2447 *
2448 * The reference to the input task_struct is released.
2449 */
2450static struct task_struct *next_tid(struct task_struct *start)
2451{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002452 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002453 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002454 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002455 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002456 if (thread_group_leader(pos))
2457 pos = NULL;
2458 else
2459 get_task_struct(pos);
2460 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002461 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002462 put_task_struct(start);
2463 return pos;
2464}
2465
Eric W. Biederman61a28782006-10-02 02:18:49 -07002466static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2467 struct task_struct *task, int tid)
2468{
2469 char name[PROC_NUMBUF];
2470 int len = snprintf(name, sizeof(name), "%d", tid);
2471 return proc_fill_cache(filp, dirent, filldir, name, len,
2472 proc_task_instantiate, task, NULL);
2473}
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475/* for the /proc/TGID/task/ directories */
2476static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2477{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002478 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002480 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002481 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 int retval = -ENOENT;
2483 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002484 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2486
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002487 task = get_proc_task(inode);
2488 if (!task)
2489 goto out_no_task;
2490 rcu_read_lock();
2491 if (pid_alive(task)) {
2492 leader = task->group_leader;
2493 get_task_struct(leader);
2494 }
2495 rcu_read_unlock();
2496 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002497 if (!leader)
2498 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 retval = 0;
2500
2501 switch (pos) {
2502 case 0:
2503 ino = inode->i_ino;
2504 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2505 goto out;
2506 pos++;
2507 /* fall through */
2508 case 1:
2509 ino = parent_ino(dentry);
2510 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2511 goto out;
2512 pos++;
2513 /* fall through */
2514 }
2515
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002516 /* f_version caches the tgid value that the last readdir call couldn't
2517 * return. lseek aka telldir automagically resets f_version to 0.
2518 */
2519 tid = filp->f_version;
2520 filp->f_version = 0;
2521 for (task = first_tid(leader, tid, pos - 2);
2522 task;
2523 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002524 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002525 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002526 /* returning this tgid failed, save it as the first
2527 * pid for the next readir call */
2528 filp->f_version = tid;
2529 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533out:
2534 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002535 put_task_struct(leader);
2536out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return retval;
2538}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002539
2540static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2541{
2542 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002543 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002544 generic_fillattr(inode, stat);
2545
Eric W. Biederman99f89552006-06-26 00:25:55 -07002546 if (p) {
2547 rcu_read_lock();
2548 stat->nlink += get_nr_threads(p);
2549 rcu_read_unlock();
2550 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002551 }
2552
2553 return 0;
2554}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002555
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002556static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002557 .lookup = proc_task_lookup,
2558 .getattr = proc_task_getattr,
2559 .setattr = proc_setattr,
2560};
2561
Arjan van de Ven00977a52007-02-12 00:55:34 -08002562static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002563 .read = generic_read_dir,
2564 .readdir = proc_task_readdir,
2565};