blob: 1a979ea3b379931973f79c4b70703df3f237c714 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
64#include <linux/smp_lock.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070065#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kallsyms.h>
67#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 int len;
94 char *name;
95 mode_t mode;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070096 struct inode_operations *iop;
97 struct file_operations *fop;
98 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 .len = sizeof(NAME) - 1, \
103 .name = (NAME), \
104 .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
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700126static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700129 task_lock(task);
130 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if(fs)
132 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700133 task_unlock(task);
134 return fs;
135}
136
Eric W. Biederman99f89552006-06-26 00:25:55 -0700137static int get_nr_threads(struct task_struct *tsk)
138{
139 /* Must be called with the rcu_read_lock held */
140 unsigned long flags;
141 int count = 0;
142
143 if (lock_task_sighand(tsk, &flags)) {
144 count = atomic_read(&tsk->signal->count);
145 unlock_task_sighand(tsk, &flags);
146 }
147 return count;
148}
149
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700150static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
151{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700152 struct task_struct *task = get_proc_task(inode);
153 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700154 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700155
156 if (task) {
157 fs = get_fs_struct(task);
158 put_task_struct(task);
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (fs) {
161 read_lock(&fs->lock);
162 *mnt = mntget(fs->pwdmnt);
163 *dentry = dget(fs->pwd);
164 read_unlock(&fs->lock);
165 result = 0;
166 put_fs_struct(fs);
167 }
168 return result;
169}
170
171static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
172{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700173 struct task_struct *task = get_proc_task(inode);
174 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176
177 if (task) {
178 fs = get_fs_struct(task);
179 put_task_struct(task);
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (fs) {
182 read_lock(&fs->lock);
183 *mnt = mntget(fs->rootmnt);
184 *dentry = dget(fs->root);
185 read_unlock(&fs->lock);
186 result = 0;
187 put_fs_struct(fs);
188 }
189 return result;
190}
191
192#define MAY_PTRACE(task) \
193 (task == current || \
194 (task->parent == current && \
195 (task->ptrace & PT_PTRACED) && \
196 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
197 security_ptrace(current,task) == 0))
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static int proc_pid_environ(struct task_struct *task, char * buffer)
200{
201 int res = 0;
202 struct mm_struct *mm = get_task_mm(task);
203 if (mm) {
204 unsigned int len = mm->env_end - mm->env_start;
205 if (len > PAGE_SIZE)
206 len = PAGE_SIZE;
207 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700208 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 res = -ESRCH;
210 mmput(mm);
211 }
212 return res;
213}
214
215static int proc_pid_cmdline(struct task_struct *task, char * buffer)
216{
217 int res = 0;
218 unsigned int len;
219 struct mm_struct *mm = get_task_mm(task);
220 if (!mm)
221 goto out;
222 if (!mm->arg_end)
223 goto out_mm; /* Shh! No looking before we're done */
224
225 len = mm->arg_end - mm->arg_start;
226
227 if (len > PAGE_SIZE)
228 len = PAGE_SIZE;
229
230 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
231
232 // If the nul at the end of args has been overwritten, then
233 // assume application is using setproctitle(3).
234 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
235 len = strnlen(buffer, res);
236 if (len < res) {
237 res = len;
238 } else {
239 len = mm->env_end - mm->env_start;
240 if (len > PAGE_SIZE - res)
241 len = PAGE_SIZE - res;
242 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
243 res = strnlen(buffer, res);
244 }
245 }
246out_mm:
247 mmput(mm);
248out:
249 return res;
250}
251
252static int proc_pid_auxv(struct task_struct *task, char *buffer)
253{
254 int res = 0;
255 struct mm_struct *mm = get_task_mm(task);
256 if (mm) {
257 unsigned int nwords = 0;
258 do
259 nwords += 2;
260 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
261 res = nwords * sizeof(mm->saved_auxv[0]);
262 if (res > PAGE_SIZE)
263 res = PAGE_SIZE;
264 memcpy(buffer, mm->saved_auxv, res);
265 mmput(mm);
266 }
267 return res;
268}
269
270
271#ifdef CONFIG_KALLSYMS
272/*
273 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
274 * Returns the resolved symbol. If that fails, simply return the address.
275 */
276static int proc_pid_wchan(struct task_struct *task, char *buffer)
277{
278 char *modname;
279 const char *sym_name;
280 unsigned long wchan, size, offset;
281 char namebuf[KSYM_NAME_LEN+1];
282
283 wchan = get_wchan(task);
284
285 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
286 if (sym_name)
287 return sprintf(buffer, "%s", sym_name);
288 return sprintf(buffer, "%lu", wchan);
289}
290#endif /* CONFIG_KALLSYMS */
291
292#ifdef CONFIG_SCHEDSTATS
293/*
294 * Provides /proc/PID/schedstat
295 */
296static int proc_pid_schedstat(struct task_struct *task, char *buffer)
297{
298 return sprintf(buffer, "%lu %lu %lu\n",
299 task->sched_info.cpu_time,
300 task->sched_info.run_delay,
301 task->sched_info.pcnt);
302}
303#endif
304
305/* The badness from the OOM killer */
306unsigned long badness(struct task_struct *p, unsigned long uptime);
307static int proc_oom_score(struct task_struct *task, char *buffer)
308{
309 unsigned long points;
310 struct timespec uptime;
311
312 do_posix_clock_monotonic_gettime(&uptime);
313 points = badness(task, uptime.tv_sec);
314 return sprintf(buffer, "%lu\n", points);
315}
316
317/************************************************************************/
318/* Here the fs part begins */
319/************************************************************************/
320
321/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700322static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700324 struct task_struct *task;
325 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700326 /* Allow access to a task's file descriptors if it is us or we
327 * may use ptrace attach to the process and find out that
328 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700329 */
330 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700331 if (task) {
332 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700333 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700334 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700335 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700338static int proc_setattr(struct dentry *dentry, struct iattr *attr)
339{
340 int error;
341 struct inode *inode = dentry->d_inode;
342
343 if (attr->ia_valid & ATTR_MODE)
344 return -EPERM;
345
346 error = inode_change_ok(inode, attr);
347 if (!error) {
348 error = security_inode_setattr(dentry, attr);
349 if (!error)
350 error = inode_setattr(inode, attr);
351 }
352 return error;
353}
354
355static struct inode_operations proc_def_inode_operations = {
356 .setattr = proc_setattr,
357};
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500360struct proc_mounts {
361 struct seq_file m;
362 int event;
363};
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static int mounts_open(struct inode *inode, struct file *file)
366{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700367 struct task_struct *task = get_proc_task(inode);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800368 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500369 struct proc_mounts *p;
370 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Eric W. Biederman99f89552006-06-26 00:25:55 -0700372 if (task) {
373 task_lock(task);
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800374 if (task->nsproxy) {
375 ns = task->nsproxy->mnt_ns;
376 if (ns)
377 get_mnt_ns(ns);
378 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700379 task_unlock(task);
380 put_task_struct(task);
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800383 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500384 ret = -ENOMEM;
385 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
386 if (p) {
387 file->private_data = &p->m;
388 ret = seq_open(file, &mounts_op);
389 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800390 p->m.private = ns;
391 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500392 return 0;
393 }
394 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800396 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 return ret;
399}
400
401static int mounts_release(struct inode *inode, struct file *file)
402{
403 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800404 struct mnt_namespace *ns = m->private;
405 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return seq_release(inode, file);
407}
408
Al Viro5addc5d2005-11-07 17:15:49 -0500409static unsigned mounts_poll(struct file *file, poll_table *wait)
410{
411 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800412 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500413 unsigned res = 0;
414
415 poll_wait(file, &ns->poll, wait);
416
417 spin_lock(&vfsmount_lock);
418 if (p->event != ns->event) {
419 p->event = ns->event;
420 res = POLLERR;
421 }
422 spin_unlock(&vfsmount_lock);
423
424 return res;
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static struct file_operations proc_mounts_operations = {
428 .open = mounts_open,
429 .read = seq_read,
430 .llseek = seq_lseek,
431 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500432 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
434
Chuck Leverb4629fe2006-03-20 13:44:12 -0500435extern struct seq_operations mountstats_op;
436static int mountstats_open(struct inode *inode, struct file *file)
437{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500438 int ret = seq_open(file, &mountstats_op);
439
440 if (!ret) {
441 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800442 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700443 struct task_struct *task = get_proc_task(inode);
444
445 if (task) {
446 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800447 if (task->nsproxy)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800448 mnt_ns = task->nsproxy->mnt_ns;
449 if (mnt_ns)
450 get_mnt_ns(mnt_ns);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700451 task_unlock(task);
452 put_task_struct(task);
453 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500454
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800455 if (mnt_ns)
456 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500457 else {
458 seq_release(inode, file);
459 ret = -EINVAL;
460 }
461 }
462 return ret;
463}
464
465static struct file_operations proc_mountstats_operations = {
466 .open = mountstats_open,
467 .read = seq_read,
468 .llseek = seq_lseek,
469 .release = mounts_release,
470};
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
473
474static ssize_t proc_info_read(struct file * file, char __user * buf,
475 size_t count, loff_t *ppos)
476{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800477 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unsigned long page;
479 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700480 struct task_struct *task = get_proc_task(inode);
481
482 length = -ESRCH;
483 if (!task)
484 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (count > PROC_BLOCK_SIZE)
487 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700488
489 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 length = PROC_I(inode)->op.proc_read(task, (char*)page);
494
495 if (length >= 0)
496 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
497 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700498out:
499 put_task_struct(task);
500out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return length;
502}
503
504static struct file_operations proc_info_file_operations = {
505 .read = proc_info_read,
506};
507
508static int mem_open(struct inode* inode, struct file* file)
509{
510 file->private_data = (void*)((long)current->self_exec_id);
511 return 0;
512}
513
514static ssize_t mem_read(struct file * file, char __user * buf,
515 size_t count, loff_t *ppos)
516{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800517 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 char *page;
519 unsigned long src = *ppos;
520 int ret = -ESRCH;
521 struct mm_struct *mm;
522
Eric W. Biederman99f89552006-06-26 00:25:55 -0700523 if (!task)
524 goto out_no_task;
525
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700526 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto out;
528
529 ret = -ENOMEM;
530 page = (char *)__get_free_page(GFP_USER);
531 if (!page)
532 goto out;
533
534 ret = 0;
535
536 mm = get_task_mm(task);
537 if (!mm)
538 goto out_free;
539
540 ret = -EIO;
541
542 if (file->private_data != (void*)((long)current->self_exec_id))
543 goto out_put;
544
545 ret = 0;
546
547 while (count > 0) {
548 int this_len, retval;
549
550 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
551 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700552 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!ret)
554 ret = -EIO;
555 break;
556 }
557
558 if (copy_to_user(buf, page, retval)) {
559 ret = -EFAULT;
560 break;
561 }
562
563 ret += retval;
564 src += retval;
565 buf += retval;
566 count -= retval;
567 }
568 *ppos = src;
569
570out_put:
571 mmput(mm);
572out_free:
573 free_page((unsigned long) page);
574out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700575 put_task_struct(task);
576out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return ret;
578}
579
580#define mem_write NULL
581
582#ifndef mem_write
583/* This is a security hazard */
584static ssize_t mem_write(struct file * file, const char * buf,
585 size_t count, loff_t *ppos)
586{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700587 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800589 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 unsigned long dst = *ppos;
591
Eric W. Biederman99f89552006-06-26 00:25:55 -0700592 copied = -ESRCH;
593 if (!task)
594 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Eric W. Biederman99f89552006-06-26 00:25:55 -0700596 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
597 goto out;
598
599 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 page = (char *)__get_free_page(GFP_USER);
601 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700604 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 while (count > 0) {
606 int this_len, retval;
607
608 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
609 if (copy_from_user(page, buf, this_len)) {
610 copied = -EFAULT;
611 break;
612 }
613 retval = access_process_vm(task, dst, page, this_len, 1);
614 if (!retval) {
615 if (!copied)
616 copied = -EIO;
617 break;
618 }
619 copied += retval;
620 buf += retval;
621 dst += retval;
622 count -= retval;
623 }
624 *ppos = dst;
625 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700626out:
627 put_task_struct(task);
628out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return copied;
630}
631#endif
632
633static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
634{
635 switch (orig) {
636 case 0:
637 file->f_pos = offset;
638 break;
639 case 1:
640 file->f_pos += offset;
641 break;
642 default:
643 return -EINVAL;
644 }
645 force_successful_syscall_return();
646 return file->f_pos;
647}
648
649static struct file_operations proc_mem_operations = {
650 .llseek = mem_lseek,
651 .read = mem_read,
652 .write = mem_write,
653 .open = mem_open,
654};
655
656static ssize_t oom_adjust_read(struct file *file, char __user *buf,
657 size_t count, loff_t *ppos)
658{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800659 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700660 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700662 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 loff_t __ppos = *ppos;
664
Eric W. Biederman99f89552006-06-26 00:25:55 -0700665 if (!task)
666 return -ESRCH;
667 oom_adjust = task->oomkilladj;
668 put_task_struct(task);
669
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700670 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (__ppos >= len)
672 return 0;
673 if (count > len-__ppos)
674 count = len-__ppos;
675 if (copy_to_user(buf, buffer + __ppos, count))
676 return -EFAULT;
677 *ppos = __ppos + count;
678 return count;
679}
680
681static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
682 size_t count, loff_t *ppos)
683{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700684 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700685 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int oom_adjust;
687
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700688 memset(buffer, 0, sizeof(buffer));
689 if (count > sizeof(buffer) - 1)
690 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (copy_from_user(buffer, buf, count))
692 return -EFAULT;
693 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700694 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
695 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EINVAL;
697 if (*end == '\n')
698 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800699 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700700 if (!task)
701 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800702 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
703 put_task_struct(task);
704 return -EACCES;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700707 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (end - buffer == 0)
709 return -EIO;
710 return end - buffer;
711}
712
713static struct file_operations proc_oom_adjust_operations = {
714 .read = oom_adjust_read,
715 .write = oom_adjust_write,
716};
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718#ifdef CONFIG_AUDITSYSCALL
719#define TMPBUFLEN 21
720static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
721 size_t count, loff_t *ppos)
722{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800723 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700724 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 ssize_t length;
726 char tmpbuf[TMPBUFLEN];
727
Eric W. Biederman99f89552006-06-26 00:25:55 -0700728 if (!task)
729 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
731 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700732 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
734}
735
736static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
737 size_t count, loff_t *ppos)
738{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800739 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 char *page, *tmp;
741 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 uid_t loginuid;
743
744 if (!capable(CAP_AUDIT_CONTROL))
745 return -EPERM;
746
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700747 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -EPERM;
749
Al Viroe0182902006-05-18 08:28:02 -0400750 if (count >= PAGE_SIZE)
751 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (*ppos != 0) {
754 /* No partial writes. */
755 return -EINVAL;
756 }
757 page = (char*)__get_free_page(GFP_USER);
758 if (!page)
759 return -ENOMEM;
760 length = -EFAULT;
761 if (copy_from_user(page, buf, count))
762 goto out_free_page;
763
Al Viroe0182902006-05-18 08:28:02 -0400764 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 loginuid = simple_strtoul(page, &tmp, 10);
766 if (tmp == page) {
767 length = -EINVAL;
768 goto out_free_page;
769
770 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700771 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (likely(length == 0))
773 length = count;
774
775out_free_page:
776 free_page((unsigned long) page);
777 return length;
778}
779
780static struct file_operations proc_loginuid_operations = {
781 .read = proc_loginuid_read,
782 .write = proc_loginuid_write,
783};
784#endif
785
786#ifdef CONFIG_SECCOMP
787static ssize_t seccomp_read(struct file *file, char __user *buf,
788 size_t count, loff_t *ppos)
789{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700790 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 char __buf[20];
792 loff_t __ppos = *ppos;
793 size_t len;
794
Eric W. Biederman99f89552006-06-26 00:25:55 -0700795 if (!tsk)
796 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* no need to print the trailing zero, so use only len */
798 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700799 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (__ppos >= len)
801 return 0;
802 if (count > len - __ppos)
803 count = len - __ppos;
804 if (copy_to_user(buf, __buf + __ppos, count))
805 return -EFAULT;
806 *ppos = __ppos + count;
807 return count;
808}
809
810static ssize_t seccomp_write(struct file *file, const char __user *buf,
811 size_t count, loff_t *ppos)
812{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700813 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 char __buf[20], *end;
815 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700816 ssize_t result;
817
818 result = -ESRCH;
819 if (!tsk)
820 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700823 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Eric W. Biederman99f89552006-06-26 00:25:55 -0700827 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 memset(__buf, 0, sizeof(__buf));
829 count = min(count, sizeof(__buf) - 1);
830 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700831 goto out;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 seccomp_mode = simple_strtoul(__buf, &end, 0);
834 if (*end == '\n')
835 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700836 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
838 tsk->seccomp.mode = seccomp_mode;
839 set_tsk_thread_flag(tsk, TIF_SECCOMP);
840 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700841 goto out;
842 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700844 goto out;
845 result = end - __buf;
846out:
847 put_task_struct(tsk);
848out_no_task:
849 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852static struct file_operations proc_seccomp_operations = {
853 .read = seccomp_read,
854 .write = seccomp_write,
855};
856#endif /* CONFIG_SECCOMP */
857
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800858#ifdef CONFIG_FAULT_INJECTION
859static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
860 size_t count, loff_t *ppos)
861{
862 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
863 char buffer[PROC_NUMBUF];
864 size_t len;
865 int make_it_fail;
866 loff_t __ppos = *ppos;
867
868 if (!task)
869 return -ESRCH;
870 make_it_fail = task->make_it_fail;
871 put_task_struct(task);
872
873 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
874 if (__ppos >= len)
875 return 0;
876 if (count > len-__ppos)
877 count = len-__ppos;
878 if (copy_to_user(buf, buffer + __ppos, count))
879 return -EFAULT;
880 *ppos = __ppos + count;
881 return count;
882}
883
884static ssize_t proc_fault_inject_write(struct file * file,
885 const char __user * buf, size_t count, loff_t *ppos)
886{
887 struct task_struct *task;
888 char buffer[PROC_NUMBUF], *end;
889 int make_it_fail;
890
891 if (!capable(CAP_SYS_RESOURCE))
892 return -EPERM;
893 memset(buffer, 0, sizeof(buffer));
894 if (count > sizeof(buffer) - 1)
895 count = sizeof(buffer) - 1;
896 if (copy_from_user(buffer, buf, count))
897 return -EFAULT;
898 make_it_fail = simple_strtol(buffer, &end, 0);
899 if (*end == '\n')
900 end++;
901 task = get_proc_task(file->f_dentry->d_inode);
902 if (!task)
903 return -ESRCH;
904 task->make_it_fail = make_it_fail;
905 put_task_struct(task);
906 if (end - buffer == 0)
907 return -EIO;
908 return end - buffer;
909}
910
911static struct file_operations proc_fault_inject_operations = {
912 .read = proc_fault_inject_read,
913 .write = proc_fault_inject_write,
914};
915#endif
916
Al Viro008b1502005-08-20 00:17:39 +0100917static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 struct inode *inode = dentry->d_inode;
920 int error = -EACCES;
921
922 /* We don't need a base pointer in the /proc filesystem */
923 path_release(nd);
924
Eric W. Biederman778c1142006-06-26 00:25:58 -0700925 /* Are we allowed to snoop on the tasks file descriptors? */
926 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
930 nd->last_type = LAST_BIND;
931out:
Al Viro008b1502005-08-20 00:17:39 +0100932 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
936 char __user *buffer, int buflen)
937{
938 struct inode * inode;
939 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
940 int len;
941
942 if (!tmp)
943 return -ENOMEM;
944
945 inode = dentry->d_inode;
946 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
947 len = PTR_ERR(path);
948 if (IS_ERR(path))
949 goto out;
950 len = tmp + PAGE_SIZE - 1 - path;
951
952 if (len > buflen)
953 len = buflen;
954 if (copy_to_user(buffer, path, len))
955 len = -EFAULT;
956 out:
957 free_page((unsigned long)tmp);
958 return len;
959}
960
961static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
962{
963 int error = -EACCES;
964 struct inode *inode = dentry->d_inode;
965 struct dentry *de;
966 struct vfsmount *mnt = NULL;
967
Eric W. Biederman778c1142006-06-26 00:25:58 -0700968 /* Are we allowed to snoop on the tasks file descriptors? */
969 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
973 if (error)
974 goto out;
975
976 error = do_proc_readlink(de, mnt, buffer, buflen);
977 dput(de);
978 mntput(mnt);
979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return error;
981}
982
983static struct inode_operations proc_pid_link_inode_operations = {
984 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700985 .follow_link = proc_pid_follow_link,
986 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987};
988
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700989
990/* building an inode */
991
992static int task_dumpable(struct task_struct *task)
993{
994 int dumpable = 0;
995 struct mm_struct *mm;
996
997 task_lock(task);
998 mm = task->mm;
999 if (mm)
1000 dumpable = mm->dumpable;
1001 task_unlock(task);
1002 if(dumpable == 1)
1003 return 1;
1004 return 0;
1005}
1006
1007
Eric W. Biederman61a28782006-10-02 02:18:49 -07001008static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001009{
1010 struct inode * inode;
1011 struct proc_inode *ei;
1012
1013 /* We need a new inode */
1014
1015 inode = new_inode(sb);
1016 if (!inode)
1017 goto out;
1018
1019 /* Common stuff */
1020 ei = PROC_I(inode);
1021 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001022 inode->i_op = &proc_def_inode_operations;
1023
1024 /*
1025 * grab the reference to task.
1026 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001027 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001028 if (!ei->pid)
1029 goto out_unlock;
1030
1031 inode->i_uid = 0;
1032 inode->i_gid = 0;
1033 if (task_dumpable(task)) {
1034 inode->i_uid = task->euid;
1035 inode->i_gid = task->egid;
1036 }
1037 security_task_to_inode(task, inode);
1038
1039out:
1040 return inode;
1041
1042out_unlock:
1043 iput(inode);
1044 return NULL;
1045}
1046
1047static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1048{
1049 struct inode *inode = dentry->d_inode;
1050 struct task_struct *task;
1051 generic_fillattr(inode, stat);
1052
1053 rcu_read_lock();
1054 stat->uid = 0;
1055 stat->gid = 0;
1056 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1057 if (task) {
1058 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1059 task_dumpable(task)) {
1060 stat->uid = task->euid;
1061 stat->gid = task->egid;
1062 }
1063 }
1064 rcu_read_unlock();
1065 return 0;
1066}
1067
1068/* dentry stuff */
1069
1070/*
1071 * Exceptional case: normally we are not allowed to unhash a busy
1072 * directory. In this case, however, we can do it - no aliasing problems
1073 * due to the way we treat inodes.
1074 *
1075 * Rewrite the inode's ownerships here because the owning task may have
1076 * performed a setuid(), etc.
1077 *
1078 * Before the /proc/pid/status file was created the only way to read
1079 * the effective uid of a /process was to stat /proc/pid. Reading
1080 * /proc/pid/status is slow enough that procps and other packages
1081 * kept stating /proc/pid. To keep the rules in /proc simple I have
1082 * made this apply to all per process world readable and executable
1083 * directories.
1084 */
1085static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1086{
1087 struct inode *inode = dentry->d_inode;
1088 struct task_struct *task = get_proc_task(inode);
1089 if (task) {
1090 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1091 task_dumpable(task)) {
1092 inode->i_uid = task->euid;
1093 inode->i_gid = task->egid;
1094 } else {
1095 inode->i_uid = 0;
1096 inode->i_gid = 0;
1097 }
1098 inode->i_mode &= ~(S_ISUID | S_ISGID);
1099 security_task_to_inode(task, inode);
1100 put_task_struct(task);
1101 return 1;
1102 }
1103 d_drop(dentry);
1104 return 0;
1105}
1106
1107static int pid_delete_dentry(struct dentry * dentry)
1108{
1109 /* Is the task we represent dead?
1110 * If so, then don't put the dentry on the lru list,
1111 * kill it immediately.
1112 */
1113 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1114}
1115
1116static struct dentry_operations pid_dentry_operations =
1117{
1118 .d_revalidate = pid_revalidate,
1119 .d_delete = pid_delete_dentry,
1120};
1121
1122/* Lookups */
1123
Eric W. Biederman61a28782006-10-02 02:18:49 -07001124typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1125
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001126/*
1127 * Fill a directory entry.
1128 *
1129 * If possible create the dcache entry and derive our inode number and
1130 * file type from dcache entry.
1131 *
1132 * Since all of the proc inode numbers are dynamically generated, the inode
1133 * numbers do not exist until the inode is cache. This means creating the
1134 * the dcache entry in readdir is necessary to keep the inode numbers
1135 * reported by readdir in sync with the inode numbers reported
1136 * by stat.
1137 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001138static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1139 char *name, int len,
1140 instantiate_t instantiate, struct task_struct *task, void *ptr)
1141{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001142 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001143 struct inode *inode;
1144 struct qstr qname;
1145 ino_t ino = 0;
1146 unsigned type = DT_UNKNOWN;
1147
1148 qname.name = name;
1149 qname.len = len;
1150 qname.hash = full_name_hash(name, len);
1151
1152 child = d_lookup(dir, &qname);
1153 if (!child) {
1154 struct dentry *new;
1155 new = d_alloc(dir, &qname);
1156 if (new) {
1157 child = instantiate(dir->d_inode, new, task, ptr);
1158 if (child)
1159 dput(new);
1160 else
1161 child = new;
1162 }
1163 }
1164 if (!child || IS_ERR(child) || !child->d_inode)
1165 goto end_instantiate;
1166 inode = child->d_inode;
1167 if (inode) {
1168 ino = inode->i_ino;
1169 type = inode->i_mode >> 12;
1170 }
1171 dput(child);
1172end_instantiate:
1173 if (!ino)
1174 ino = find_inode_number(dir, &qname);
1175 if (!ino)
1176 ino = 1;
1177 return filldir(dirent, name, len, filp->f_pos, ino, type);
1178}
1179
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001180static unsigned name_to_int(struct dentry *dentry)
1181{
1182 const char *name = dentry->d_name.name;
1183 int len = dentry->d_name.len;
1184 unsigned n = 0;
1185
1186 if (len > 1 && *name == '0')
1187 goto out;
1188 while (len-- > 0) {
1189 unsigned c = *name++ - '0';
1190 if (c > 9)
1191 goto out;
1192 if (n >= (~0U-9)/10)
1193 goto out;
1194 n *= 10;
1195 n += c;
1196 }
1197 return n;
1198out:
1199 return ~0U;
1200}
1201
1202static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1203{
1204 struct task_struct *task = get_proc_task(inode);
1205 struct files_struct *files = NULL;
1206 struct file *file;
1207 int fd = proc_fd(inode);
1208
1209 if (task) {
1210 files = get_files_struct(task);
1211 put_task_struct(task);
1212 }
1213 if (files) {
1214 /*
1215 * We are not taking a ref to the file structure, so we must
1216 * hold ->file_lock.
1217 */
1218 spin_lock(&files->file_lock);
1219 file = fcheck_files(files, fd);
1220 if (file) {
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001221 *mnt = mntget(file->f_path.mnt);
1222 *dentry = dget(file->f_path.dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001223 spin_unlock(&files->file_lock);
1224 put_files_struct(files);
1225 return 0;
1226 }
1227 spin_unlock(&files->file_lock);
1228 put_files_struct(files);
1229 }
1230 return -ENOENT;
1231}
1232
1233static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1234{
1235 struct inode *inode = dentry->d_inode;
1236 struct task_struct *task = get_proc_task(inode);
1237 int fd = proc_fd(inode);
1238 struct files_struct *files;
1239
1240 if (task) {
1241 files = get_files_struct(task);
1242 if (files) {
1243 rcu_read_lock();
1244 if (fcheck_files(files, fd)) {
1245 rcu_read_unlock();
1246 put_files_struct(files);
1247 if (task_dumpable(task)) {
1248 inode->i_uid = task->euid;
1249 inode->i_gid = task->egid;
1250 } else {
1251 inode->i_uid = 0;
1252 inode->i_gid = 0;
1253 }
1254 inode->i_mode &= ~(S_ISUID | S_ISGID);
1255 security_task_to_inode(task, inode);
1256 put_task_struct(task);
1257 return 1;
1258 }
1259 rcu_read_unlock();
1260 put_files_struct(files);
1261 }
1262 put_task_struct(task);
1263 }
1264 d_drop(dentry);
1265 return 0;
1266}
1267
1268static struct dentry_operations tid_fd_dentry_operations =
1269{
1270 .d_revalidate = tid_fd_revalidate,
1271 .d_delete = pid_delete_dentry,
1272};
1273
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001274static struct dentry *proc_fd_instantiate(struct inode *dir,
1275 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001276{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001277 unsigned fd = *(unsigned *)ptr;
1278 struct file *file;
1279 struct files_struct *files;
1280 struct inode *inode;
1281 struct proc_inode *ei;
1282 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001283
Eric W. Biederman61a28782006-10-02 02:18:49 -07001284 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001285 if (!inode)
1286 goto out;
1287 ei = PROC_I(inode);
1288 ei->fd = fd;
1289 files = get_files_struct(task);
1290 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001291 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001292 inode->i_mode = S_IFLNK;
1293
1294 /*
1295 * We are not taking a ref to the file structure, so we must
1296 * hold ->file_lock.
1297 */
1298 spin_lock(&files->file_lock);
1299 file = fcheck_files(files, fd);
1300 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001301 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001302 if (file->f_mode & 1)
1303 inode->i_mode |= S_IRUSR | S_IXUSR;
1304 if (file->f_mode & 2)
1305 inode->i_mode |= S_IWUSR | S_IXUSR;
1306 spin_unlock(&files->file_lock);
1307 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001308
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001309 inode->i_op = &proc_pid_link_inode_operations;
1310 inode->i_size = 64;
1311 ei->op.proc_get_link = proc_fd_link;
1312 dentry->d_op = &tid_fd_dentry_operations;
1313 d_add(dentry, inode);
1314 /* Close the race of the process dying before we return the dentry */
1315 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001316 error = NULL;
1317
1318 out:
1319 return error;
1320out_unlock:
1321 spin_unlock(&files->file_lock);
1322 put_files_struct(files);
1323out_iput:
1324 iput(inode);
1325 goto out;
1326}
1327
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001328static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1329{
1330 struct task_struct *task = get_proc_task(dir);
1331 unsigned fd = name_to_int(dentry);
1332 struct dentry *result = ERR_PTR(-ENOENT);
1333
1334 if (!task)
1335 goto out_no_task;
1336 if (fd == ~0U)
1337 goto out;
1338
1339 result = proc_fd_instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001340out:
1341 put_task_struct(task);
1342out_no_task:
1343 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001344}
1345
Eric W. Biederman61a28782006-10-02 02:18:49 -07001346static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1347 struct task_struct *task, int fd)
1348{
1349 char name[PROC_NUMBUF];
1350 int len = snprintf(name, sizeof(name), "%d", fd);
1351 return proc_fill_cache(filp, dirent, filldir, name, len,
1352 proc_fd_instantiate, task, &fd);
1353}
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1356{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001357 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001358 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001359 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 unsigned int fd, tid, ino;
1361 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001363 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001366 if (!p)
1367 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 retval = 0;
1369 tid = p->pid;
1370
1371 fd = filp->f_pos;
1372 switch (fd) {
1373 case 0:
1374 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1375 goto out;
1376 filp->f_pos++;
1377 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001378 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1380 goto out;
1381 filp->f_pos++;
1382 default:
1383 files = get_files_struct(p);
1384 if (!files)
1385 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001386 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001387 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001389 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 fd++, filp->f_pos++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (!fcheck_files(files, fd))
1393 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001394 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Eric W. Biederman61a28782006-10-02 02:18:49 -07001396 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001397 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001400 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001402 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 put_files_struct(files);
1404 }
1405out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001406 put_task_struct(p);
1407out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return retval;
1409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411static struct file_operations proc_fd_operations = {
1412 .read = generic_read_dir,
1413 .readdir = proc_readfd,
1414};
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416/*
1417 * proc directories can do almost nothing..
1418 */
1419static struct inode_operations proc_fd_inode_operations = {
1420 .lookup = proc_lookupfd,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001421 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422};
1423
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001424static struct dentry *proc_pident_instantiate(struct inode *dir,
1425 struct dentry *dentry, struct task_struct *task, void *ptr)
1426{
1427 struct pid_entry *p = ptr;
1428 struct inode *inode;
1429 struct proc_inode *ei;
1430 struct dentry *error = ERR_PTR(-EINVAL);
1431
Eric W. Biederman61a28782006-10-02 02:18:49 -07001432 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001433 if (!inode)
1434 goto out;
1435
1436 ei = PROC_I(inode);
1437 inode->i_mode = p->mode;
1438 if (S_ISDIR(inode->i_mode))
1439 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1440 if (p->iop)
1441 inode->i_op = p->iop;
1442 if (p->fop)
1443 inode->i_fop = p->fop;
1444 ei->op = p->op;
1445 dentry->d_op = &pid_dentry_operations;
1446 d_add(dentry, inode);
1447 /* Close the race of the process dying before we return the dentry */
1448 if (pid_revalidate(dentry, NULL))
1449 error = NULL;
1450out:
1451 return error;
1452}
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454static struct dentry *proc_pident_lookup(struct inode *dir,
1455 struct dentry *dentry,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001456 struct pid_entry *ents,
1457 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001460 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001461 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001462 struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001464 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 inode = NULL;
1466
Eric W. Biederman99f89552006-06-26 00:25:55 -07001467 if (!task)
1468 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001470 /*
1471 * Yes, it does not scale. And it should not. Don't add
1472 * new entries into /proc/<tgid>/ without very good reasons.
1473 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001474 last = &ents[nents - 1];
1475 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (p->len != dentry->d_name.len)
1477 continue;
1478 if (!memcmp(dentry->d_name.name, p->name, p->len))
1479 break;
1480 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001481 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 goto out;
1483
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001484 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001486 put_task_struct(task);
1487out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001488 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Eric W. Biederman61a28782006-10-02 02:18:49 -07001491static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1492 struct task_struct *task, struct pid_entry *p)
1493{
1494 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1495 proc_pident_instantiate, task, p);
1496}
1497
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001498static int proc_pident_readdir(struct file *filp,
1499 void *dirent, filldir_t filldir,
1500 struct pid_entry *ents, unsigned int nents)
1501{
1502 int i;
1503 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001504 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001505 struct inode *inode = dentry->d_inode;
1506 struct task_struct *task = get_proc_task(inode);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001507 struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001508 ino_t ino;
1509 int ret;
1510
1511 ret = -ENOENT;
1512 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001513 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001514
1515 ret = 0;
1516 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001517 i = filp->f_pos;
1518 switch (i) {
1519 case 0:
1520 ino = inode->i_ino;
1521 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1522 goto out;
1523 i++;
1524 filp->f_pos++;
1525 /* fall through */
1526 case 1:
1527 ino = parent_ino(dentry);
1528 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1529 goto out;
1530 i++;
1531 filp->f_pos++;
1532 /* fall through */
1533 default:
1534 i -= 2;
1535 if (i >= nents) {
1536 ret = 1;
1537 goto out;
1538 }
1539 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001540 last = &ents[nents - 1];
1541 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001542 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001543 goto out;
1544 filp->f_pos++;
1545 p++;
1546 }
1547 }
1548
1549 ret = 1;
1550out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001551 put_task_struct(task);
1552out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001553 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001557static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1558 size_t count, loff_t *ppos)
1559{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001560 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001561 unsigned long page;
1562 ssize_t length;
1563 struct task_struct *task = get_proc_task(inode);
1564
1565 length = -ESRCH;
1566 if (!task)
1567 goto out_no_task;
1568
1569 if (count > PAGE_SIZE)
1570 count = PAGE_SIZE;
1571 length = -ENOMEM;
1572 if (!(page = __get_free_page(GFP_KERNEL)))
1573 goto out;
1574
1575 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001576 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001577 (void*)page, count);
1578 if (length >= 0)
1579 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1580 free_page(page);
1581out:
1582 put_task_struct(task);
1583out_no_task:
1584 return length;
1585}
1586
1587static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1588 size_t count, loff_t *ppos)
1589{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001590 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001591 char *page;
1592 ssize_t length;
1593 struct task_struct *task = get_proc_task(inode);
1594
1595 length = -ESRCH;
1596 if (!task)
1597 goto out_no_task;
1598 if (count > PAGE_SIZE)
1599 count = PAGE_SIZE;
1600
1601 /* No partial writes. */
1602 length = -EINVAL;
1603 if (*ppos != 0)
1604 goto out;
1605
1606 length = -ENOMEM;
1607 page = (char*)__get_free_page(GFP_USER);
1608 if (!page)
1609 goto out;
1610
1611 length = -EFAULT;
1612 if (copy_from_user(page, buf, count))
1613 goto out_free;
1614
1615 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001616 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001617 (void*)page, count);
1618out_free:
1619 free_page((unsigned long) page);
1620out:
1621 put_task_struct(task);
1622out_no_task:
1623 return length;
1624}
1625
1626static struct file_operations proc_pid_attr_operations = {
1627 .read = proc_pid_attr_read,
1628 .write = proc_pid_attr_write,
1629};
1630
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001631static struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001632 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1633 REG("prev", S_IRUGO, pid_attr),
1634 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1635 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1636 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1637 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001638};
1639
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001640static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 void * dirent, filldir_t filldir)
1642{
1643 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001644 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001647static struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001649 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650};
1651
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001652static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 struct dentry *dentry, struct nameidata *nd)
1654{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001655 return proc_pident_lookup(dir, dentry,
1656 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001659static struct inode_operations proc_attr_dir_inode_operations = {
1660 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001661 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001662 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663};
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665#endif
1666
1667/*
1668 * /proc/self:
1669 */
1670static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1671 int buflen)
1672{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001673 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 sprintf(tmp, "%d", current->tgid);
1675 return vfs_readlink(dentry,buffer,buflen,tmp);
1676}
1677
Al Viro008b1502005-08-20 00:17:39 +01001678static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001680 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001682 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001683}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685static struct inode_operations proc_self_inode_operations = {
1686 .readlink = proc_self_readlink,
1687 .follow_link = proc_self_follow_link,
1688};
1689
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001690/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001691 * proc base
1692 *
1693 * These are the directory entries in the root directory of /proc
1694 * that properly belong to the /proc filesystem, as they describe
1695 * describe something that is process related.
1696 */
1697static struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001698 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001699 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001700};
1701
1702/*
1703 * Exceptional case: normally we are not allowed to unhash a busy
1704 * directory. In this case, however, we can do it - no aliasing problems
1705 * due to the way we treat inodes.
1706 */
1707static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1708{
1709 struct inode *inode = dentry->d_inode;
1710 struct task_struct *task = get_proc_task(inode);
1711 if (task) {
1712 put_task_struct(task);
1713 return 1;
1714 }
1715 d_drop(dentry);
1716 return 0;
1717}
1718
1719static struct dentry_operations proc_base_dentry_operations =
1720{
1721 .d_revalidate = proc_base_revalidate,
1722 .d_delete = pid_delete_dentry,
1723};
1724
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001725static struct dentry *proc_base_instantiate(struct inode *dir,
1726 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001727{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001728 struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001729 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001730 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001731 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001732
1733 /* Allocate the inode */
1734 error = ERR_PTR(-ENOMEM);
1735 inode = new_inode(dir->i_sb);
1736 if (!inode)
1737 goto out;
1738
1739 /* Initialize the inode */
1740 ei = PROC_I(inode);
1741 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001742
1743 /*
1744 * grab the reference to the task.
1745 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001746 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001747 if (!ei->pid)
1748 goto out_iput;
1749
1750 inode->i_uid = 0;
1751 inode->i_gid = 0;
1752 inode->i_mode = p->mode;
1753 if (S_ISDIR(inode->i_mode))
1754 inode->i_nlink = 2;
1755 if (S_ISLNK(inode->i_mode))
1756 inode->i_size = 64;
1757 if (p->iop)
1758 inode->i_op = p->iop;
1759 if (p->fop)
1760 inode->i_fop = p->fop;
1761 ei->op = p->op;
1762 dentry->d_op = &proc_base_dentry_operations;
1763 d_add(dentry, inode);
1764 error = NULL;
1765out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001766 return error;
1767out_iput:
1768 iput(inode);
1769 goto out;
1770}
1771
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001772static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1773{
1774 struct dentry *error;
1775 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001776 struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001777
1778 error = ERR_PTR(-ENOENT);
1779
1780 if (!task)
1781 goto out_no_task;
1782
1783 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001784 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1785 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001786 if (p->len != dentry->d_name.len)
1787 continue;
1788 if (!memcmp(dentry->d_name.name, p->name, p->len))
1789 break;
1790 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001791 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001792 goto out;
1793
1794 error = proc_base_instantiate(dir, dentry, task, p);
1795
1796out:
1797 put_task_struct(task);
1798out_no_task:
1799 return error;
1800}
1801
Eric W. Biederman61a28782006-10-02 02:18:49 -07001802static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1803 struct task_struct *task, struct pid_entry *p)
1804{
1805 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1806 proc_base_instantiate, task, p);
1807}
1808
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001809#ifdef CONFIG_TASK_IO_ACCOUNTING
1810static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1811{
1812 return sprintf(buffer,
1813 "rchar: %llu\n"
1814 "wchar: %llu\n"
1815 "syscr: %llu\n"
1816 "syscw: %llu\n"
1817 "read_bytes: %llu\n"
1818 "write_bytes: %llu\n"
1819 "cancelled_write_bytes: %llu\n",
1820 (unsigned long long)task->rchar,
1821 (unsigned long long)task->wchar,
1822 (unsigned long long)task->syscr,
1823 (unsigned long long)task->syscw,
1824 (unsigned long long)task->ioac.read_bytes,
1825 (unsigned long long)task->ioac.write_bytes,
1826 (unsigned long long)task->ioac.cancelled_write_bytes);
1827}
1828#endif
1829
Eric W. Biederman801199c2006-10-02 02:18:48 -07001830/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001831 * Thread groups
1832 */
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001833static struct file_operations proc_task_operations;
1834static struct inode_operations proc_task_inode_operations;
1835
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001836static struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001837 DIR("task", S_IRUGO|S_IXUGO, task),
1838 DIR("fd", S_IRUSR|S_IXUSR, fd),
1839 INF("environ", S_IRUSR, pid_environ),
1840 INF("auxv", S_IRUSR, pid_auxv),
1841 INF("status", S_IRUGO, pid_status),
1842 INF("cmdline", S_IRUGO, pid_cmdline),
1843 INF("stat", S_IRUGO, tgid_stat),
1844 INF("statm", S_IRUGO, pid_statm),
1845 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001846#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001847 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001848#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001849 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001850#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07001851 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001852#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001853 LNK("cwd", cwd),
1854 LNK("root", root),
1855 LNK("exe", exe),
1856 REG("mounts", S_IRUGO, mounts),
1857 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001858#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07001859 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001860#endif
1861#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001862 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001863#endif
1864#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001865 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001866#endif
1867#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001868 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001869#endif
1870#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001871 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001872#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001873 INF("oom_score", S_IRUGO, oom_score),
1874 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001875#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07001876 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001877#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001878#ifdef CONFIG_FAULT_INJECTION
1879 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1880#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001881#ifdef CONFIG_TASK_IO_ACCOUNTING
1882 INF("io", S_IRUGO, pid_io_accounting),
1883#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001884};
1885
1886static int proc_tgid_base_readdir(struct file * filp,
1887 void * dirent, filldir_t filldir)
1888{
1889 return proc_pident_readdir(filp,dirent,filldir,
1890 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1891}
1892
1893static struct file_operations proc_tgid_base_operations = {
1894 .read = generic_read_dir,
1895 .readdir = proc_tgid_base_readdir,
1896};
1897
1898static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001899 return proc_pident_lookup(dir, dentry,
1900 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001901}
1902
1903static struct inode_operations proc_tgid_base_inode_operations = {
1904 .lookup = proc_tgid_base_lookup,
1905 .getattr = pid_getattr,
1906 .setattr = proc_setattr,
1907};
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001910 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001912 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001914 * Looks in the dcache for
1915 * /proc/@pid
1916 * /proc/@tgid/task/@pid
1917 * if either directory is present flushes it and all of it'ts children
1918 * from the dcache.
1919 *
1920 * It is safe and reasonable to cache /proc entries for a task until
1921 * that task exits. After that they just clog up the dcache with
1922 * useless entries, possibly causing useful dcache entries to be
1923 * flushed instead. This routine is proved to flush those useless
1924 * dcache entries at process exit time.
1925 *
1926 * NOTE: This routine is just an optimization so it does not guarantee
1927 * that no dcache entries will exist at process exit time it
1928 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001930void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001932 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001933 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001934 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Eric W. Biederman48e64842006-06-26 00:25:48 -07001936 name.name = buf;
1937 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1938 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1939 if (dentry) {
1940 shrink_dcache_parent(dentry);
1941 d_drop(dentry);
1942 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Eric W. Biederman48e64842006-06-26 00:25:48 -07001945 if (thread_group_leader(task))
1946 goto out;
1947
1948 name.name = buf;
1949 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1950 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1951 if (!leader)
1952 goto out;
1953
1954 name.name = "task";
1955 name.len = strlen(name.name);
1956 dir = d_hash_and_lookup(leader, &name);
1957 if (!dir)
1958 goto out_put_leader;
1959
1960 name.name = buf;
1961 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1962 dentry = d_hash_and_lookup(dir, &name);
1963 if (dentry) {
1964 shrink_dcache_parent(dentry);
1965 d_drop(dentry);
1966 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001968
1969 dput(dir);
1970out_put_leader:
1971 dput(leader);
1972out:
1973 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Adrian Bunk9711ef92006-12-06 20:38:31 -08001976static struct dentry *proc_pid_instantiate(struct inode *dir,
1977 struct dentry * dentry,
1978 struct task_struct *task, void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001979{
1980 struct dentry *error = ERR_PTR(-ENOENT);
1981 struct inode *inode;
1982
Eric W. Biederman61a28782006-10-02 02:18:49 -07001983 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001984 if (!inode)
1985 goto out;
1986
1987 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1988 inode->i_op = &proc_tgid_base_inode_operations;
1989 inode->i_fop = &proc_tgid_base_operations;
1990 inode->i_flags|=S_IMMUTABLE;
1991 inode->i_nlink = 4;
1992#ifdef CONFIG_SECURITY
1993 inode->i_nlink += 1;
1994#endif
1995
1996 dentry->d_op = &pid_dentry_operations;
1997
1998 d_add(dentry, inode);
1999 /* Close the race of the process dying before we return the dentry */
2000 if (pid_revalidate(dentry, NULL))
2001 error = NULL;
2002out:
2003 return error;
2004}
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2007{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002008 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Eric W. Biederman801199c2006-10-02 02:18:48 -07002012 result = proc_base_lookup(dir, dentry);
2013 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2014 goto out;
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 tgid = name_to_int(dentry);
2017 if (tgid == ~0U)
2018 goto out;
2019
Eric W. Biedermande758732006-06-26 00:25:51 -07002020 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 task = find_task_by_pid(tgid);
2022 if (task)
2023 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002024 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (!task)
2026 goto out;
2027
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002028 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002029 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002031 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002035 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002036 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002038static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002040 struct task_struct *task;
2041 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002043 rcu_read_lock();
2044retry:
2045 task = NULL;
2046 pid = find_ge_pid(tgid);
2047 if (pid) {
2048 tgid = pid->nr + 1;
2049 task = pid_task(pid, PIDTYPE_PID);
2050 /* What we to know is if the pid we have find is the
2051 * pid of a thread_group_leader. Testing for task
2052 * being a thread_group_leader is the obvious thing
2053 * todo but there is a window when it fails, due to
2054 * the pid transfer logic in de_thread.
2055 *
2056 * So we perform the straight forward test of seeing
2057 * if the pid we have found is the pid of a thread
2058 * group leader, and don't worry if the task we have
2059 * found doesn't happen to be a thread group leader.
2060 * As we don't care in the case of readdir.
2061 */
2062 if (!task || !has_group_leader_pid(task))
2063 goto retry;
2064 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002066 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002067 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002070#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Eric W. Biederman61a28782006-10-02 02:18:49 -07002072static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2073 struct task_struct *task, int tgid)
2074{
2075 char name[PROC_NUMBUF];
2076 int len = snprintf(name, sizeof(name), "%d", tgid);
2077 return proc_fill_cache(filp, dirent, filldir, name, len,
2078 proc_pid_instantiate, task, NULL);
2079}
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081/* for the /proc/ directory itself, after non-process stuff has been done */
2082int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002085 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002086 struct task_struct *task;
2087 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Eric W. Biederman61a28782006-10-02 02:18:49 -07002089 if (!reaper)
2090 goto out_no_task;
2091
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002092 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric W. Biederman801199c2006-10-02 02:18:48 -07002093 struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002094 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002095 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002098 tgid = filp->f_pos - TGID_OFFSET;
2099 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002100 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002101 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002102 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002103 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002104 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002105 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002109 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2110out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002111 put_task_struct(reaper);
2112out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return 0;
2114}
2115
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002116/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002117 * Tasks
2118 */
2119static struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002120 DIR("fd", S_IRUSR|S_IXUSR, fd),
2121 INF("environ", S_IRUSR, pid_environ),
2122 INF("auxv", S_IRUSR, pid_auxv),
2123 INF("status", S_IRUGO, pid_status),
2124 INF("cmdline", S_IRUGO, pid_cmdline),
2125 INF("stat", S_IRUGO, tid_stat),
2126 INF("statm", S_IRUGO, pid_statm),
2127 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002128#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002129 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002130#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002131 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002132#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002133 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002134#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002135 LNK("cwd", cwd),
2136 LNK("root", root),
2137 LNK("exe", exe),
2138 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002139#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07002140 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002141#endif
2142#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002143 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002144#endif
2145#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002146 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002147#endif
2148#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002149 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002150#endif
2151#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002152 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002153#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002154 INF("oom_score", S_IRUGO, oom_score),
2155 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002156#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002157 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002158#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002159#ifdef CONFIG_FAULT_INJECTION
2160 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2161#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002162};
2163
2164static int proc_tid_base_readdir(struct file * filp,
2165 void * dirent, filldir_t filldir)
2166{
2167 return proc_pident_readdir(filp,dirent,filldir,
2168 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2169}
2170
2171static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002172 return proc_pident_lookup(dir, dentry,
2173 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002174}
2175
2176static struct file_operations proc_tid_base_operations = {
2177 .read = generic_read_dir,
2178 .readdir = proc_tid_base_readdir,
2179};
2180
2181static struct inode_operations proc_tid_base_inode_operations = {
2182 .lookup = proc_tid_base_lookup,
2183 .getattr = pid_getattr,
2184 .setattr = proc_setattr,
2185};
2186
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002187static struct dentry *proc_task_instantiate(struct inode *dir,
2188 struct dentry *dentry, struct task_struct *task, void *ptr)
2189{
2190 struct dentry *error = ERR_PTR(-ENOENT);
2191 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002192 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002193
2194 if (!inode)
2195 goto out;
2196 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2197 inode->i_op = &proc_tid_base_inode_operations;
2198 inode->i_fop = &proc_tid_base_operations;
2199 inode->i_flags|=S_IMMUTABLE;
2200 inode->i_nlink = 3;
2201#ifdef CONFIG_SECURITY
2202 inode->i_nlink += 1;
2203#endif
2204
2205 dentry->d_op = &pid_dentry_operations;
2206
2207 d_add(dentry, inode);
2208 /* Close the race of the process dying before we return the dentry */
2209 if (pid_revalidate(dentry, NULL))
2210 error = NULL;
2211out:
2212 return error;
2213}
2214
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002215static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2216{
2217 struct dentry *result = ERR_PTR(-ENOENT);
2218 struct task_struct *task;
2219 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002220 unsigned tid;
2221
2222 if (!leader)
2223 goto out_no_task;
2224
2225 tid = name_to_int(dentry);
2226 if (tid == ~0U)
2227 goto out;
2228
2229 rcu_read_lock();
2230 task = find_task_by_pid(tid);
2231 if (task)
2232 get_task_struct(task);
2233 rcu_read_unlock();
2234 if (!task)
2235 goto out;
2236 if (leader->tgid != task->tgid)
2237 goto out_drop_task;
2238
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002239 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002240out_drop_task:
2241 put_task_struct(task);
2242out:
2243 put_task_struct(leader);
2244out_no_task:
2245 return result;
2246}
2247
2248/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002249 * Find the first tid of a thread group to return to user space.
2250 *
2251 * Usually this is just the thread group leader, but if the users
2252 * buffer was too small or there was a seek into the middle of the
2253 * directory we have more work todo.
2254 *
2255 * In the case of a short read we start with find_task_by_pid.
2256 *
2257 * In the case of a seek we start with the leader and walk nr
2258 * threads past it.
2259 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002260static struct task_struct *first_tid(struct task_struct *leader,
2261 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002262{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002263 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002264
Eric W. Biedermancc288732006-06-26 00:26:01 -07002265 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002266 /* Attempt to start with the pid of a thread */
2267 if (tid && (nr > 0)) {
2268 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002269 if (pos && (pos->group_leader == leader))
2270 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002271 }
2272
2273 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002274 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002275 if (nr && nr >= get_nr_threads(leader))
2276 goto out;
2277
2278 /* If we haven't found our starting place yet start
2279 * with the leader and walk nr threads forward.
2280 */
2281 for (pos = leader; nr > 0; --nr) {
2282 pos = next_thread(pos);
2283 if (pos == leader) {
2284 pos = NULL;
2285 goto out;
2286 }
2287 }
2288found:
2289 get_task_struct(pos);
2290out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002291 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002292 return pos;
2293}
2294
2295/*
2296 * Find the next thread in the thread list.
2297 * Return NULL if there is an error or no next thread.
2298 *
2299 * The reference to the input task_struct is released.
2300 */
2301static struct task_struct *next_tid(struct task_struct *start)
2302{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002303 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002304 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002305 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002306 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002307 if (thread_group_leader(pos))
2308 pos = NULL;
2309 else
2310 get_task_struct(pos);
2311 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002312 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002313 put_task_struct(start);
2314 return pos;
2315}
2316
Eric W. Biederman61a28782006-10-02 02:18:49 -07002317static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2318 struct task_struct *task, int tid)
2319{
2320 char name[PROC_NUMBUF];
2321 int len = snprintf(name, sizeof(name), "%d", tid);
2322 return proc_fill_cache(filp, dirent, filldir, name, len,
2323 proc_task_instantiate, task, NULL);
2324}
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326/* for the /proc/TGID/task/ directories */
2327static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2328{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002329 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002331 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002332 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 int retval = -ENOENT;
2334 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002335 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2337
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002338 task = get_proc_task(inode);
2339 if (!task)
2340 goto out_no_task;
2341 rcu_read_lock();
2342 if (pid_alive(task)) {
2343 leader = task->group_leader;
2344 get_task_struct(leader);
2345 }
2346 rcu_read_unlock();
2347 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002348 if (!leader)
2349 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 retval = 0;
2351
2352 switch (pos) {
2353 case 0:
2354 ino = inode->i_ino;
2355 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2356 goto out;
2357 pos++;
2358 /* fall through */
2359 case 1:
2360 ino = parent_ino(dentry);
2361 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2362 goto out;
2363 pos++;
2364 /* fall through */
2365 }
2366
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002367 /* f_version caches the tgid value that the last readdir call couldn't
2368 * return. lseek aka telldir automagically resets f_version to 0.
2369 */
2370 tid = filp->f_version;
2371 filp->f_version = 0;
2372 for (task = first_tid(leader, tid, pos - 2);
2373 task;
2374 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002375 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002376 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002377 /* returning this tgid failed, save it as the first
2378 * pid for the next readir call */
2379 filp->f_version = tid;
2380 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384out:
2385 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002386 put_task_struct(leader);
2387out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return retval;
2389}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002390
2391static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2392{
2393 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002394 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002395 generic_fillattr(inode, stat);
2396
Eric W. Biederman99f89552006-06-26 00:25:55 -07002397 if (p) {
2398 rcu_read_lock();
2399 stat->nlink += get_nr_threads(p);
2400 rcu_read_unlock();
2401 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002402 }
2403
2404 return 0;
2405}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002406
2407static struct inode_operations proc_task_inode_operations = {
2408 .lookup = proc_task_lookup,
2409 .getattr = proc_task_getattr,
2410 .setattr = proc_setattr,
2411};
2412
2413static struct file_operations proc_task_operations = {
2414 .read = generic_read_dir,
2415 .readdir = proc_task_readdir,
2416};