blob: 725e279c3bdf0dc6aba79cc33ea4c970c593b1cc [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>
62#include <linux/namespace.h>
63#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include "internal.h"
76
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070077/* NOTE:
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
82 *
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
85 */
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Eric W. Biederman8578cea2006-06-26 00:25:54 -070088/* Worst case buffer size needed for holding an integer. */
89#define PROC_NUMBUF 10
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int len;
93 char *name;
94 mode_t mode;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070095 struct inode_operations *iop;
96 struct file_operations *fop;
97 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Eric W. Biederman61a28782006-10-02 02:18:49 -0700100#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700101 .len = sizeof(NAME) - 1, \
102 .name = (NAME), \
103 .mode = MODE, \
104 .iop = IOP, \
105 .fop = FOP, \
106 .op = OP, \
107}
108
Eric W. Biederman61a28782006-10-02 02:18:49 -0700109#define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
112 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700113#define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700117#define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700125static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700128 task_lock(task);
129 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if(fs)
131 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700132 task_unlock(task);
133 return fs;
134}
135
Eric W. Biederman99f89552006-06-26 00:25:55 -0700136static int get_nr_threads(struct task_struct *tsk)
137{
138 /* Must be called with the rcu_read_lock held */
139 unsigned long flags;
140 int count = 0;
141
142 if (lock_task_sighand(tsk, &flags)) {
143 count = atomic_read(&tsk->signal->count);
144 unlock_task_sighand(tsk, &flags);
145 }
146 return count;
147}
148
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700149static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
150{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700151 struct task_struct *task = get_proc_task(inode);
152 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700153 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700154
155 if (task) {
156 fs = get_fs_struct(task);
157 put_task_struct(task);
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (fs) {
160 read_lock(&fs->lock);
161 *mnt = mntget(fs->pwdmnt);
162 *dentry = dget(fs->pwd);
163 read_unlock(&fs->lock);
164 result = 0;
165 put_fs_struct(fs);
166 }
167 return result;
168}
169
170static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
171{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700172 struct task_struct *task = get_proc_task(inode);
173 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700175
176 if (task) {
177 fs = get_fs_struct(task);
178 put_task_struct(task);
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (fs) {
181 read_lock(&fs->lock);
182 *mnt = mntget(fs->rootmnt);
183 *dentry = dget(fs->root);
184 read_unlock(&fs->lock);
185 result = 0;
186 put_fs_struct(fs);
187 }
188 return result;
189}
190
191#define MAY_PTRACE(task) \
192 (task == current || \
193 (task->parent == current && \
194 (task->ptrace & PT_PTRACED) && \
195 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
196 security_ptrace(current,task) == 0))
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static int proc_pid_environ(struct task_struct *task, char * buffer)
199{
200 int res = 0;
201 struct mm_struct *mm = get_task_mm(task);
202 if (mm) {
203 unsigned int len = mm->env_end - mm->env_start;
204 if (len > PAGE_SIZE)
205 len = PAGE_SIZE;
206 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700207 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 res = -ESRCH;
209 mmput(mm);
210 }
211 return res;
212}
213
214static int proc_pid_cmdline(struct task_struct *task, char * buffer)
215{
216 int res = 0;
217 unsigned int len;
218 struct mm_struct *mm = get_task_mm(task);
219 if (!mm)
220 goto out;
221 if (!mm->arg_end)
222 goto out_mm; /* Shh! No looking before we're done */
223
224 len = mm->arg_end - mm->arg_start;
225
226 if (len > PAGE_SIZE)
227 len = PAGE_SIZE;
228
229 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
230
231 // If the nul at the end of args has been overwritten, then
232 // assume application is using setproctitle(3).
233 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
234 len = strnlen(buffer, res);
235 if (len < res) {
236 res = len;
237 } else {
238 len = mm->env_end - mm->env_start;
239 if (len > PAGE_SIZE - res)
240 len = PAGE_SIZE - res;
241 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
242 res = strnlen(buffer, res);
243 }
244 }
245out_mm:
246 mmput(mm);
247out:
248 return res;
249}
250
251static int proc_pid_auxv(struct task_struct *task, char *buffer)
252{
253 int res = 0;
254 struct mm_struct *mm = get_task_mm(task);
255 if (mm) {
256 unsigned int nwords = 0;
257 do
258 nwords += 2;
259 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
260 res = nwords * sizeof(mm->saved_auxv[0]);
261 if (res > PAGE_SIZE)
262 res = PAGE_SIZE;
263 memcpy(buffer, mm->saved_auxv, res);
264 mmput(mm);
265 }
266 return res;
267}
268
269
270#ifdef CONFIG_KALLSYMS
271/*
272 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
273 * Returns the resolved symbol. If that fails, simply return the address.
274 */
275static int proc_pid_wchan(struct task_struct *task, char *buffer)
276{
277 char *modname;
278 const char *sym_name;
279 unsigned long wchan, size, offset;
280 char namebuf[KSYM_NAME_LEN+1];
281
282 wchan = get_wchan(task);
283
284 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
285 if (sym_name)
286 return sprintf(buffer, "%s", sym_name);
287 return sprintf(buffer, "%lu", wchan);
288}
289#endif /* CONFIG_KALLSYMS */
290
291#ifdef CONFIG_SCHEDSTATS
292/*
293 * Provides /proc/PID/schedstat
294 */
295static int proc_pid_schedstat(struct task_struct *task, char *buffer)
296{
297 return sprintf(buffer, "%lu %lu %lu\n",
298 task->sched_info.cpu_time,
299 task->sched_info.run_delay,
300 task->sched_info.pcnt);
301}
302#endif
303
304/* The badness from the OOM killer */
305unsigned long badness(struct task_struct *p, unsigned long uptime);
306static int proc_oom_score(struct task_struct *task, char *buffer)
307{
308 unsigned long points;
309 struct timespec uptime;
310
311 do_posix_clock_monotonic_gettime(&uptime);
312 points = badness(task, uptime.tv_sec);
313 return sprintf(buffer, "%lu\n", points);
314}
315
316/************************************************************************/
317/* Here the fs part begins */
318/************************************************************************/
319
320/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700321static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700323 struct task_struct *task;
324 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700325 /* Allow access to a task's file descriptors if it is us or we
326 * may use ptrace attach to the process and find out that
327 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700328 */
329 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700330 if (task) {
331 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700332 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700333 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700334 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700337static int proc_setattr(struct dentry *dentry, struct iattr *attr)
338{
339 int error;
340 struct inode *inode = dentry->d_inode;
341
342 if (attr->ia_valid & ATTR_MODE)
343 return -EPERM;
344
345 error = inode_change_ok(inode, attr);
346 if (!error) {
347 error = security_inode_setattr(dentry, attr);
348 if (!error)
349 error = inode_setattr(inode, attr);
350 }
351 return error;
352}
353
354static struct inode_operations proc_def_inode_operations = {
355 .setattr = proc_setattr,
356};
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500359struct proc_mounts {
360 struct seq_file m;
361 int event;
362};
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364static int mounts_open(struct inode *inode, struct file *file)
365{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700366 struct task_struct *task = get_proc_task(inode);
367 struct namespace *namespace = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500368 struct proc_mounts *p;
369 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Eric W. Biederman99f89552006-06-26 00:25:55 -0700371 if (task) {
372 task_lock(task);
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700373 namespace = task->nsproxy->namespace;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700374 if (namespace)
375 get_namespace(namespace);
376 task_unlock(task);
377 put_task_struct(task);
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Al Viro5addc5d2005-11-07 17:15:49 -0500380 if (namespace) {
381 ret = -ENOMEM;
382 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
383 if (p) {
384 file->private_data = &p->m;
385 ret = seq_open(file, &mounts_op);
386 if (!ret) {
387 p->m.private = namespace;
388 p->event = namespace->event;
389 return 0;
390 }
391 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Al Viro5addc5d2005-11-07 17:15:49 -0500393 put_namespace(namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 return ret;
396}
397
398static int mounts_release(struct inode *inode, struct file *file)
399{
400 struct seq_file *m = file->private_data;
401 struct namespace *namespace = m->private;
402 put_namespace(namespace);
403 return seq_release(inode, file);
404}
405
Al Viro5addc5d2005-11-07 17:15:49 -0500406static unsigned mounts_poll(struct file *file, poll_table *wait)
407{
408 struct proc_mounts *p = file->private_data;
409 struct namespace *ns = p->m.private;
410 unsigned res = 0;
411
412 poll_wait(file, &ns->poll, wait);
413
414 spin_lock(&vfsmount_lock);
415 if (p->event != ns->event) {
416 p->event = ns->event;
417 res = POLLERR;
418 }
419 spin_unlock(&vfsmount_lock);
420
421 return res;
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static struct file_operations proc_mounts_operations = {
425 .open = mounts_open,
426 .read = seq_read,
427 .llseek = seq_lseek,
428 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500429 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430};
431
Chuck Leverb4629fe2006-03-20 13:44:12 -0500432extern struct seq_operations mountstats_op;
433static int mountstats_open(struct inode *inode, struct file *file)
434{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500435 int ret = seq_open(file, &mountstats_op);
436
437 if (!ret) {
438 struct seq_file *m = file->private_data;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700439 struct namespace *namespace = NULL;
440 struct task_struct *task = get_proc_task(inode);
441
442 if (task) {
443 task_lock(task);
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700444 namespace = task->nsproxy->namespace;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700445 if (namespace)
446 get_namespace(namespace);
447 task_unlock(task);
448 put_task_struct(task);
449 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500450
451 if (namespace)
452 m->private = namespace;
453 else {
454 seq_release(inode, file);
455 ret = -EINVAL;
456 }
457 }
458 return ret;
459}
460
461static struct file_operations proc_mountstats_operations = {
462 .open = mountstats_open,
463 .read = seq_read,
464 .llseek = seq_lseek,
465 .release = mounts_release,
466};
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
469
470static ssize_t proc_info_read(struct file * file, char __user * buf,
471 size_t count, loff_t *ppos)
472{
473 struct inode * inode = file->f_dentry->d_inode;
474 unsigned long page;
475 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700476 struct task_struct *task = get_proc_task(inode);
477
478 length = -ESRCH;
479 if (!task)
480 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (count > PROC_BLOCK_SIZE)
483 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700484
485 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 length = PROC_I(inode)->op.proc_read(task, (char*)page);
490
491 if (length >= 0)
492 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
493 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700494out:
495 put_task_struct(task);
496out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return length;
498}
499
500static struct file_operations proc_info_file_operations = {
501 .read = proc_info_read,
502};
503
504static int mem_open(struct inode* inode, struct file* file)
505{
506 file->private_data = (void*)((long)current->self_exec_id);
507 return 0;
508}
509
510static ssize_t mem_read(struct file * file, char __user * buf,
511 size_t count, loff_t *ppos)
512{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700513 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 char *page;
515 unsigned long src = *ppos;
516 int ret = -ESRCH;
517 struct mm_struct *mm;
518
Eric W. Biederman99f89552006-06-26 00:25:55 -0700519 if (!task)
520 goto out_no_task;
521
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700522 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto out;
524
525 ret = -ENOMEM;
526 page = (char *)__get_free_page(GFP_USER);
527 if (!page)
528 goto out;
529
530 ret = 0;
531
532 mm = get_task_mm(task);
533 if (!mm)
534 goto out_free;
535
536 ret = -EIO;
537
538 if (file->private_data != (void*)((long)current->self_exec_id))
539 goto out_put;
540
541 ret = 0;
542
543 while (count > 0) {
544 int this_len, retval;
545
546 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
547 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700548 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!ret)
550 ret = -EIO;
551 break;
552 }
553
554 if (copy_to_user(buf, page, retval)) {
555 ret = -EFAULT;
556 break;
557 }
558
559 ret += retval;
560 src += retval;
561 buf += retval;
562 count -= retval;
563 }
564 *ppos = src;
565
566out_put:
567 mmput(mm);
568out_free:
569 free_page((unsigned long) page);
570out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700571 put_task_struct(task);
572out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return ret;
574}
575
576#define mem_write NULL
577
578#ifndef mem_write
579/* This is a security hazard */
580static ssize_t mem_write(struct file * file, const char * buf,
581 size_t count, loff_t *ppos)
582{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700583 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 char *page;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700585 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 unsigned long dst = *ppos;
587
Eric W. Biederman99f89552006-06-26 00:25:55 -0700588 copied = -ESRCH;
589 if (!task)
590 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Eric W. Biederman99f89552006-06-26 00:25:55 -0700592 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
593 goto out;
594
595 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 page = (char *)__get_free_page(GFP_USER);
597 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700598 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700600 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 while (count > 0) {
602 int this_len, retval;
603
604 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
605 if (copy_from_user(page, buf, this_len)) {
606 copied = -EFAULT;
607 break;
608 }
609 retval = access_process_vm(task, dst, page, this_len, 1);
610 if (!retval) {
611 if (!copied)
612 copied = -EIO;
613 break;
614 }
615 copied += retval;
616 buf += retval;
617 dst += retval;
618 count -= retval;
619 }
620 *ppos = dst;
621 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700622out:
623 put_task_struct(task);
624out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return copied;
626}
627#endif
628
629static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
630{
631 switch (orig) {
632 case 0:
633 file->f_pos = offset;
634 break;
635 case 1:
636 file->f_pos += offset;
637 break;
638 default:
639 return -EINVAL;
640 }
641 force_successful_syscall_return();
642 return file->f_pos;
643}
644
645static struct file_operations proc_mem_operations = {
646 .llseek = mem_lseek,
647 .read = mem_read,
648 .write = mem_write,
649 .open = mem_open,
650};
651
652static ssize_t oom_adjust_read(struct file *file, char __user *buf,
653 size_t count, loff_t *ppos)
654{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700655 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700656 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700658 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 loff_t __ppos = *ppos;
660
Eric W. Biederman99f89552006-06-26 00:25:55 -0700661 if (!task)
662 return -ESRCH;
663 oom_adjust = task->oomkilladj;
664 put_task_struct(task);
665
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700666 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (__ppos >= len)
668 return 0;
669 if (count > len-__ppos)
670 count = len-__ppos;
671 if (copy_to_user(buf, buffer + __ppos, count))
672 return -EFAULT;
673 *ppos = __ppos + count;
674 return count;
675}
676
677static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
678 size_t count, loff_t *ppos)
679{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700680 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700681 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int oom_adjust;
683
684 if (!capable(CAP_SYS_RESOURCE))
685 return -EPERM;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700686 memset(buffer, 0, sizeof(buffer));
687 if (count > sizeof(buffer) - 1)
688 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (copy_from_user(buffer, buf, count))
690 return -EFAULT;
691 oom_adjust = simple_strtol(buffer, &end, 0);
Andrea Arcangeli79befd02005-04-16 15:24:05 -0700692 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EINVAL;
694 if (*end == '\n')
695 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700696 task = get_proc_task(file->f_dentry->d_inode);
697 if (!task)
698 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700700 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (end - buffer == 0)
702 return -EIO;
703 return end - buffer;
704}
705
706static struct file_operations proc_oom_adjust_operations = {
707 .read = oom_adjust_read,
708 .write = oom_adjust_write,
709};
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711#ifdef CONFIG_AUDITSYSCALL
712#define TMPBUFLEN 21
713static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
714 size_t count, loff_t *ppos)
715{
716 struct inode * inode = file->f_dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700717 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 ssize_t length;
719 char tmpbuf[TMPBUFLEN];
720
Eric W. Biederman99f89552006-06-26 00:25:55 -0700721 if (!task)
722 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
724 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700725 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
727}
728
729static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
730 size_t count, loff_t *ppos)
731{
732 struct inode * inode = file->f_dentry->d_inode;
733 char *page, *tmp;
734 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 uid_t loginuid;
736
737 if (!capable(CAP_AUDIT_CONTROL))
738 return -EPERM;
739
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700740 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -EPERM;
742
Al Viroe0182902006-05-18 08:28:02 -0400743 if (count >= PAGE_SIZE)
744 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (*ppos != 0) {
747 /* No partial writes. */
748 return -EINVAL;
749 }
750 page = (char*)__get_free_page(GFP_USER);
751 if (!page)
752 return -ENOMEM;
753 length = -EFAULT;
754 if (copy_from_user(page, buf, count))
755 goto out_free_page;
756
Al Viroe0182902006-05-18 08:28:02 -0400757 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 loginuid = simple_strtoul(page, &tmp, 10);
759 if (tmp == page) {
760 length = -EINVAL;
761 goto out_free_page;
762
763 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700764 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (likely(length == 0))
766 length = count;
767
768out_free_page:
769 free_page((unsigned long) page);
770 return length;
771}
772
773static struct file_operations proc_loginuid_operations = {
774 .read = proc_loginuid_read,
775 .write = proc_loginuid_write,
776};
777#endif
778
779#ifdef CONFIG_SECCOMP
780static ssize_t seccomp_read(struct file *file, char __user *buf,
781 size_t count, loff_t *ppos)
782{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700783 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 char __buf[20];
785 loff_t __ppos = *ppos;
786 size_t len;
787
Eric W. Biederman99f89552006-06-26 00:25:55 -0700788 if (!tsk)
789 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /* no need to print the trailing zero, so use only len */
791 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700792 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (__ppos >= len)
794 return 0;
795 if (count > len - __ppos)
796 count = len - __ppos;
797 if (copy_to_user(buf, __buf + __ppos, count))
798 return -EFAULT;
799 *ppos = __ppos + count;
800 return count;
801}
802
803static ssize_t seccomp_write(struct file *file, const char __user *buf,
804 size_t count, loff_t *ppos)
805{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700806 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 char __buf[20], *end;
808 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700809 ssize_t result;
810
811 result = -ESRCH;
812 if (!tsk)
813 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700816 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Eric W. Biederman99f89552006-06-26 00:25:55 -0700820 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 memset(__buf, 0, sizeof(__buf));
822 count = min(count, sizeof(__buf) - 1);
823 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700824 goto out;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 seccomp_mode = simple_strtoul(__buf, &end, 0);
827 if (*end == '\n')
828 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700829 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
831 tsk->seccomp.mode = seccomp_mode;
832 set_tsk_thread_flag(tsk, TIF_SECCOMP);
833 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700834 goto out;
835 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700837 goto out;
838 result = end - __buf;
839out:
840 put_task_struct(tsk);
841out_no_task:
842 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845static struct file_operations proc_seccomp_operations = {
846 .read = seccomp_read,
847 .write = seccomp_write,
848};
849#endif /* CONFIG_SECCOMP */
850
Al Viro008b1502005-08-20 00:17:39 +0100851static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct inode *inode = dentry->d_inode;
854 int error = -EACCES;
855
856 /* We don't need a base pointer in the /proc filesystem */
857 path_release(nd);
858
Eric W. Biederman778c1142006-06-26 00:25:58 -0700859 /* Are we allowed to snoop on the tasks file descriptors? */
860 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
864 nd->last_type = LAST_BIND;
865out:
Al Viro008b1502005-08-20 00:17:39 +0100866 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
870 char __user *buffer, int buflen)
871{
872 struct inode * inode;
873 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
874 int len;
875
876 if (!tmp)
877 return -ENOMEM;
878
879 inode = dentry->d_inode;
880 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
881 len = PTR_ERR(path);
882 if (IS_ERR(path))
883 goto out;
884 len = tmp + PAGE_SIZE - 1 - path;
885
886 if (len > buflen)
887 len = buflen;
888 if (copy_to_user(buffer, path, len))
889 len = -EFAULT;
890 out:
891 free_page((unsigned long)tmp);
892 return len;
893}
894
895static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
896{
897 int error = -EACCES;
898 struct inode *inode = dentry->d_inode;
899 struct dentry *de;
900 struct vfsmount *mnt = NULL;
901
Eric W. Biederman778c1142006-06-26 00:25:58 -0700902 /* Are we allowed to snoop on the tasks file descriptors? */
903 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
907 if (error)
908 goto out;
909
910 error = do_proc_readlink(de, mnt, buffer, buflen);
911 dput(de);
912 mntput(mnt);
913out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return error;
915}
916
917static struct inode_operations proc_pid_link_inode_operations = {
918 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700919 .follow_link = proc_pid_follow_link,
920 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921};
922
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700923
924/* building an inode */
925
926static int task_dumpable(struct task_struct *task)
927{
928 int dumpable = 0;
929 struct mm_struct *mm;
930
931 task_lock(task);
932 mm = task->mm;
933 if (mm)
934 dumpable = mm->dumpable;
935 task_unlock(task);
936 if(dumpable == 1)
937 return 1;
938 return 0;
939}
940
941
Eric W. Biederman61a28782006-10-02 02:18:49 -0700942static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700943{
944 struct inode * inode;
945 struct proc_inode *ei;
946
947 /* We need a new inode */
948
949 inode = new_inode(sb);
950 if (!inode)
951 goto out;
952
953 /* Common stuff */
954 ei = PROC_I(inode);
955 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700956 inode->i_op = &proc_def_inode_operations;
957
958 /*
959 * grab the reference to task.
960 */
Eric W. Biederman7fbaac02006-10-02 02:18:51 -0700961 ei->pid = get_pid(task_pid(task));
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700962 if (!ei->pid)
963 goto out_unlock;
964
965 inode->i_uid = 0;
966 inode->i_gid = 0;
967 if (task_dumpable(task)) {
968 inode->i_uid = task->euid;
969 inode->i_gid = task->egid;
970 }
971 security_task_to_inode(task, inode);
972
973out:
974 return inode;
975
976out_unlock:
977 iput(inode);
978 return NULL;
979}
980
981static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
982{
983 struct inode *inode = dentry->d_inode;
984 struct task_struct *task;
985 generic_fillattr(inode, stat);
986
987 rcu_read_lock();
988 stat->uid = 0;
989 stat->gid = 0;
990 task = pid_task(proc_pid(inode), PIDTYPE_PID);
991 if (task) {
992 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
993 task_dumpable(task)) {
994 stat->uid = task->euid;
995 stat->gid = task->egid;
996 }
997 }
998 rcu_read_unlock();
999 return 0;
1000}
1001
1002/* dentry stuff */
1003
1004/*
1005 * Exceptional case: normally we are not allowed to unhash a busy
1006 * directory. In this case, however, we can do it - no aliasing problems
1007 * due to the way we treat inodes.
1008 *
1009 * Rewrite the inode's ownerships here because the owning task may have
1010 * performed a setuid(), etc.
1011 *
1012 * Before the /proc/pid/status file was created the only way to read
1013 * the effective uid of a /process was to stat /proc/pid. Reading
1014 * /proc/pid/status is slow enough that procps and other packages
1015 * kept stating /proc/pid. To keep the rules in /proc simple I have
1016 * made this apply to all per process world readable and executable
1017 * directories.
1018 */
1019static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1020{
1021 struct inode *inode = dentry->d_inode;
1022 struct task_struct *task = get_proc_task(inode);
1023 if (task) {
1024 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1025 task_dumpable(task)) {
1026 inode->i_uid = task->euid;
1027 inode->i_gid = task->egid;
1028 } else {
1029 inode->i_uid = 0;
1030 inode->i_gid = 0;
1031 }
1032 inode->i_mode &= ~(S_ISUID | S_ISGID);
1033 security_task_to_inode(task, inode);
1034 put_task_struct(task);
1035 return 1;
1036 }
1037 d_drop(dentry);
1038 return 0;
1039}
1040
1041static int pid_delete_dentry(struct dentry * dentry)
1042{
1043 /* Is the task we represent dead?
1044 * If so, then don't put the dentry on the lru list,
1045 * kill it immediately.
1046 */
1047 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1048}
1049
1050static struct dentry_operations pid_dentry_operations =
1051{
1052 .d_revalidate = pid_revalidate,
1053 .d_delete = pid_delete_dentry,
1054};
1055
1056/* Lookups */
1057
Eric W. Biederman61a28782006-10-02 02:18:49 -07001058typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1059
1060static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1061 char *name, int len,
1062 instantiate_t instantiate, struct task_struct *task, void *ptr)
1063{
1064 struct dentry *child, *dir = filp->f_dentry;
1065 struct inode *inode;
1066 struct qstr qname;
1067 ino_t ino = 0;
1068 unsigned type = DT_UNKNOWN;
1069
1070 qname.name = name;
1071 qname.len = len;
1072 qname.hash = full_name_hash(name, len);
1073
1074 child = d_lookup(dir, &qname);
1075 if (!child) {
1076 struct dentry *new;
1077 new = d_alloc(dir, &qname);
1078 if (new) {
1079 child = instantiate(dir->d_inode, new, task, ptr);
1080 if (child)
1081 dput(new);
1082 else
1083 child = new;
1084 }
1085 }
1086 if (!child || IS_ERR(child) || !child->d_inode)
1087 goto end_instantiate;
1088 inode = child->d_inode;
1089 if (inode) {
1090 ino = inode->i_ino;
1091 type = inode->i_mode >> 12;
1092 }
1093 dput(child);
1094end_instantiate:
1095 if (!ino)
1096 ino = find_inode_number(dir, &qname);
1097 if (!ino)
1098 ino = 1;
1099 return filldir(dirent, name, len, filp->f_pos, ino, type);
1100}
1101
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001102static unsigned name_to_int(struct dentry *dentry)
1103{
1104 const char *name = dentry->d_name.name;
1105 int len = dentry->d_name.len;
1106 unsigned n = 0;
1107
1108 if (len > 1 && *name == '0')
1109 goto out;
1110 while (len-- > 0) {
1111 unsigned c = *name++ - '0';
1112 if (c > 9)
1113 goto out;
1114 if (n >= (~0U-9)/10)
1115 goto out;
1116 n *= 10;
1117 n += c;
1118 }
1119 return n;
1120out:
1121 return ~0U;
1122}
1123
1124static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1125{
1126 struct task_struct *task = get_proc_task(inode);
1127 struct files_struct *files = NULL;
1128 struct file *file;
1129 int fd = proc_fd(inode);
1130
1131 if (task) {
1132 files = get_files_struct(task);
1133 put_task_struct(task);
1134 }
1135 if (files) {
1136 /*
1137 * We are not taking a ref to the file structure, so we must
1138 * hold ->file_lock.
1139 */
1140 spin_lock(&files->file_lock);
1141 file = fcheck_files(files, fd);
1142 if (file) {
1143 *mnt = mntget(file->f_vfsmnt);
1144 *dentry = dget(file->f_dentry);
1145 spin_unlock(&files->file_lock);
1146 put_files_struct(files);
1147 return 0;
1148 }
1149 spin_unlock(&files->file_lock);
1150 put_files_struct(files);
1151 }
1152 return -ENOENT;
1153}
1154
1155static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1156{
1157 struct inode *inode = dentry->d_inode;
1158 struct task_struct *task = get_proc_task(inode);
1159 int fd = proc_fd(inode);
1160 struct files_struct *files;
1161
1162 if (task) {
1163 files = get_files_struct(task);
1164 if (files) {
1165 rcu_read_lock();
1166 if (fcheck_files(files, fd)) {
1167 rcu_read_unlock();
1168 put_files_struct(files);
1169 if (task_dumpable(task)) {
1170 inode->i_uid = task->euid;
1171 inode->i_gid = task->egid;
1172 } else {
1173 inode->i_uid = 0;
1174 inode->i_gid = 0;
1175 }
1176 inode->i_mode &= ~(S_ISUID | S_ISGID);
1177 security_task_to_inode(task, inode);
1178 put_task_struct(task);
1179 return 1;
1180 }
1181 rcu_read_unlock();
1182 put_files_struct(files);
1183 }
1184 put_task_struct(task);
1185 }
1186 d_drop(dentry);
1187 return 0;
1188}
1189
1190static struct dentry_operations tid_fd_dentry_operations =
1191{
1192 .d_revalidate = tid_fd_revalidate,
1193 .d_delete = pid_delete_dentry,
1194};
1195
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001196static struct dentry *proc_fd_instantiate(struct inode *dir,
1197 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001198{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001199 unsigned fd = *(unsigned *)ptr;
1200 struct file *file;
1201 struct files_struct *files;
1202 struct inode *inode;
1203 struct proc_inode *ei;
1204 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001205
Eric W. Biederman61a28782006-10-02 02:18:49 -07001206 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001207 if (!inode)
1208 goto out;
1209 ei = PROC_I(inode);
1210 ei->fd = fd;
1211 files = get_files_struct(task);
1212 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001213 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001214 inode->i_mode = S_IFLNK;
1215
1216 /*
1217 * We are not taking a ref to the file structure, so we must
1218 * hold ->file_lock.
1219 */
1220 spin_lock(&files->file_lock);
1221 file = fcheck_files(files, fd);
1222 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001223 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001224 if (file->f_mode & 1)
1225 inode->i_mode |= S_IRUSR | S_IXUSR;
1226 if (file->f_mode & 2)
1227 inode->i_mode |= S_IWUSR | S_IXUSR;
1228 spin_unlock(&files->file_lock);
1229 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001230
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001231 inode->i_op = &proc_pid_link_inode_operations;
1232 inode->i_size = 64;
1233 ei->op.proc_get_link = proc_fd_link;
1234 dentry->d_op = &tid_fd_dentry_operations;
1235 d_add(dentry, inode);
1236 /* Close the race of the process dying before we return the dentry */
1237 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001238 error = NULL;
1239
1240 out:
1241 return error;
1242out_unlock:
1243 spin_unlock(&files->file_lock);
1244 put_files_struct(files);
1245out_iput:
1246 iput(inode);
1247 goto out;
1248}
1249
1250/* SMP-safe */
1251static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1252{
1253 struct task_struct *task = get_proc_task(dir);
1254 unsigned fd = name_to_int(dentry);
1255 struct dentry *result = ERR_PTR(-ENOENT);
1256
1257 if (!task)
1258 goto out_no_task;
1259 if (fd == ~0U)
1260 goto out;
1261
1262 result = proc_fd_instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001263out:
1264 put_task_struct(task);
1265out_no_task:
1266 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001267}
1268
Eric W. Biederman61a28782006-10-02 02:18:49 -07001269static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1270 struct task_struct *task, int fd)
1271{
1272 char name[PROC_NUMBUF];
1273 int len = snprintf(name, sizeof(name), "%d", fd);
1274 return proc_fill_cache(filp, dirent, filldir, name, len,
1275 proc_fd_instantiate, task, &fd);
1276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1279{
Eric W. Biederman56347082006-06-26 00:25:40 -07001280 struct dentry *dentry = filp->f_dentry;
1281 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001282 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 unsigned int fd, tid, ino;
1284 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001286 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001289 if (!p)
1290 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 retval = 0;
1292 tid = p->pid;
1293
1294 fd = filp->f_pos;
1295 switch (fd) {
1296 case 0:
1297 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1298 goto out;
1299 filp->f_pos++;
1300 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001301 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1303 goto out;
1304 filp->f_pos++;
1305 default:
1306 files = get_files_struct(p);
1307 if (!files)
1308 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001309 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001310 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001312 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 fd++, filp->f_pos++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (!fcheck_files(files, fd))
1316 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001317 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Eric W. Biederman61a28782006-10-02 02:18:49 -07001319 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001320 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 break;
1322 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001323 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001325 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 put_files_struct(files);
1327 }
1328out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001329 put_task_struct(p);
1330out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return retval;
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334static struct file_operations proc_fd_operations = {
1335 .read = generic_read_dir,
1336 .readdir = proc_readfd,
1337};
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/*
1340 * proc directories can do almost nothing..
1341 */
1342static struct inode_operations proc_fd_inode_operations = {
1343 .lookup = proc_lookupfd,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001344 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345};
1346
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001347static struct dentry *proc_pident_instantiate(struct inode *dir,
1348 struct dentry *dentry, struct task_struct *task, void *ptr)
1349{
1350 struct pid_entry *p = ptr;
1351 struct inode *inode;
1352 struct proc_inode *ei;
1353 struct dentry *error = ERR_PTR(-EINVAL);
1354
Eric W. Biederman61a28782006-10-02 02:18:49 -07001355 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001356 if (!inode)
1357 goto out;
1358
1359 ei = PROC_I(inode);
1360 inode->i_mode = p->mode;
1361 if (S_ISDIR(inode->i_mode))
1362 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1363 if (p->iop)
1364 inode->i_op = p->iop;
1365 if (p->fop)
1366 inode->i_fop = p->fop;
1367 ei->op = p->op;
1368 dentry->d_op = &pid_dentry_operations;
1369 d_add(dentry, inode);
1370 /* Close the race of the process dying before we return the dentry */
1371 if (pid_revalidate(dentry, NULL))
1372 error = NULL;
1373out:
1374 return error;
1375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377/* SMP-safe */
1378static struct dentry *proc_pident_lookup(struct inode *dir,
1379 struct dentry *dentry,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001380 struct pid_entry *ents,
1381 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001384 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001385 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001386 struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001388 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 inode = NULL;
1390
Eric W. Biederman99f89552006-06-26 00:25:55 -07001391 if (!task)
1392 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001394 /*
1395 * Yes, it does not scale. And it should not. Don't add
1396 * new entries into /proc/<tgid>/ without very good reasons.
1397 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001398 last = &ents[nents - 1];
1399 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (p->len != dentry->d_name.len)
1401 continue;
1402 if (!memcmp(dentry->d_name.name, p->name, p->len))
1403 break;
1404 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001405 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 goto out;
1407
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001408 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001410 put_task_struct(task);
1411out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001412 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Eric W. Biederman61a28782006-10-02 02:18:49 -07001415static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1416 struct task_struct *task, struct pid_entry *p)
1417{
1418 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1419 proc_pident_instantiate, task, p);
1420}
1421
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001422static int proc_pident_readdir(struct file *filp,
1423 void *dirent, filldir_t filldir,
1424 struct pid_entry *ents, unsigned int nents)
1425{
1426 int i;
1427 int pid;
1428 struct dentry *dentry = filp->f_dentry;
1429 struct inode *inode = dentry->d_inode;
1430 struct task_struct *task = get_proc_task(inode);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001431 struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001432 ino_t ino;
1433 int ret;
1434
1435 ret = -ENOENT;
1436 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001437 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001438
1439 ret = 0;
1440 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001441 i = filp->f_pos;
1442 switch (i) {
1443 case 0:
1444 ino = inode->i_ino;
1445 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1446 goto out;
1447 i++;
1448 filp->f_pos++;
1449 /* fall through */
1450 case 1:
1451 ino = parent_ino(dentry);
1452 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1453 goto out;
1454 i++;
1455 filp->f_pos++;
1456 /* fall through */
1457 default:
1458 i -= 2;
1459 if (i >= nents) {
1460 ret = 1;
1461 goto out;
1462 }
1463 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001464 last = &ents[nents - 1];
1465 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001466 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001467 goto out;
1468 filp->f_pos++;
1469 p++;
1470 }
1471 }
1472
1473 ret = 1;
1474out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001475 put_task_struct(task);
1476out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001477 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001481static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1482 size_t count, loff_t *ppos)
1483{
1484 struct inode * inode = file->f_dentry->d_inode;
1485 unsigned long page;
1486 ssize_t length;
1487 struct task_struct *task = get_proc_task(inode);
1488
1489 length = -ESRCH;
1490 if (!task)
1491 goto out_no_task;
1492
1493 if (count > PAGE_SIZE)
1494 count = PAGE_SIZE;
1495 length = -ENOMEM;
1496 if (!(page = __get_free_page(GFP_KERNEL)))
1497 goto out;
1498
1499 length = security_getprocattr(task,
1500 (char*)file->f_dentry->d_name.name,
1501 (void*)page, count);
1502 if (length >= 0)
1503 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1504 free_page(page);
1505out:
1506 put_task_struct(task);
1507out_no_task:
1508 return length;
1509}
1510
1511static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1512 size_t count, loff_t *ppos)
1513{
1514 struct inode * inode = file->f_dentry->d_inode;
1515 char *page;
1516 ssize_t length;
1517 struct task_struct *task = get_proc_task(inode);
1518
1519 length = -ESRCH;
1520 if (!task)
1521 goto out_no_task;
1522 if (count > PAGE_SIZE)
1523 count = PAGE_SIZE;
1524
1525 /* No partial writes. */
1526 length = -EINVAL;
1527 if (*ppos != 0)
1528 goto out;
1529
1530 length = -ENOMEM;
1531 page = (char*)__get_free_page(GFP_USER);
1532 if (!page)
1533 goto out;
1534
1535 length = -EFAULT;
1536 if (copy_from_user(page, buf, count))
1537 goto out_free;
1538
1539 length = security_setprocattr(task,
1540 (char*)file->f_dentry->d_name.name,
1541 (void*)page, count);
1542out_free:
1543 free_page((unsigned long) page);
1544out:
1545 put_task_struct(task);
1546out_no_task:
1547 return length;
1548}
1549
1550static struct file_operations proc_pid_attr_operations = {
1551 .read = proc_pid_attr_read,
1552 .write = proc_pid_attr_write,
1553};
1554
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001555static struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001556 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1557 REG("prev", S_IRUGO, pid_attr),
1558 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1559 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1560 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1561 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001562};
1563
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001564static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 void * dirent, filldir_t filldir)
1566{
1567 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001568 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001571static struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001573 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574};
1575
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001576static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 struct dentry *dentry, struct nameidata *nd)
1578{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001579 return proc_pident_lookup(dir, dentry,
1580 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001583static struct inode_operations proc_attr_dir_inode_operations = {
1584 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001585 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001586 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587};
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589#endif
1590
1591/*
1592 * /proc/self:
1593 */
1594static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1595 int buflen)
1596{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001597 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 sprintf(tmp, "%d", current->tgid);
1599 return vfs_readlink(dentry,buffer,buflen,tmp);
1600}
1601
Al Viro008b1502005-08-20 00:17:39 +01001602static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001604 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001606 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001607}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609static struct inode_operations proc_self_inode_operations = {
1610 .readlink = proc_self_readlink,
1611 .follow_link = proc_self_follow_link,
1612};
1613
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001614/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001615 * proc base
1616 *
1617 * These are the directory entries in the root directory of /proc
1618 * that properly belong to the /proc filesystem, as they describe
1619 * describe something that is process related.
1620 */
1621static struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001622 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001623 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001624};
1625
1626/*
1627 * Exceptional case: normally we are not allowed to unhash a busy
1628 * directory. In this case, however, we can do it - no aliasing problems
1629 * due to the way we treat inodes.
1630 */
1631static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1632{
1633 struct inode *inode = dentry->d_inode;
1634 struct task_struct *task = get_proc_task(inode);
1635 if (task) {
1636 put_task_struct(task);
1637 return 1;
1638 }
1639 d_drop(dentry);
1640 return 0;
1641}
1642
1643static struct dentry_operations proc_base_dentry_operations =
1644{
1645 .d_revalidate = proc_base_revalidate,
1646 .d_delete = pid_delete_dentry,
1647};
1648
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001649static struct dentry *proc_base_instantiate(struct inode *dir,
1650 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001651{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001652 struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001653 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001654 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001655 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001656
1657 /* Allocate the inode */
1658 error = ERR_PTR(-ENOMEM);
1659 inode = new_inode(dir->i_sb);
1660 if (!inode)
1661 goto out;
1662
1663 /* Initialize the inode */
1664 ei = PROC_I(inode);
1665 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001666
1667 /*
1668 * grab the reference to the task.
1669 */
1670 ei->pid = get_pid(task_pid(task));
1671 if (!ei->pid)
1672 goto out_iput;
1673
1674 inode->i_uid = 0;
1675 inode->i_gid = 0;
1676 inode->i_mode = p->mode;
1677 if (S_ISDIR(inode->i_mode))
1678 inode->i_nlink = 2;
1679 if (S_ISLNK(inode->i_mode))
1680 inode->i_size = 64;
1681 if (p->iop)
1682 inode->i_op = p->iop;
1683 if (p->fop)
1684 inode->i_fop = p->fop;
1685 ei->op = p->op;
1686 dentry->d_op = &proc_base_dentry_operations;
1687 d_add(dentry, inode);
1688 error = NULL;
1689out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001690 return error;
1691out_iput:
1692 iput(inode);
1693 goto out;
1694}
1695
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001696static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1697{
1698 struct dentry *error;
1699 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001700 struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001701
1702 error = ERR_PTR(-ENOENT);
1703
1704 if (!task)
1705 goto out_no_task;
1706
1707 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001708 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1709 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001710 if (p->len != dentry->d_name.len)
1711 continue;
1712 if (!memcmp(dentry->d_name.name, p->name, p->len))
1713 break;
1714 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001715 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001716 goto out;
1717
1718 error = proc_base_instantiate(dir, dentry, task, p);
1719
1720out:
1721 put_task_struct(task);
1722out_no_task:
1723 return error;
1724}
1725
Eric W. Biederman61a28782006-10-02 02:18:49 -07001726static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1727 struct task_struct *task, struct pid_entry *p)
1728{
1729 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1730 proc_base_instantiate, task, p);
1731}
1732
Eric W. Biederman801199c2006-10-02 02:18:48 -07001733/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001734 * Thread groups
1735 */
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001736static struct file_operations proc_task_operations;
1737static struct inode_operations proc_task_inode_operations;
1738
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001739static struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001740 DIR("task", S_IRUGO|S_IXUGO, task),
1741 DIR("fd", S_IRUSR|S_IXUSR, fd),
1742 INF("environ", S_IRUSR, pid_environ),
1743 INF("auxv", S_IRUSR, pid_auxv),
1744 INF("status", S_IRUGO, pid_status),
1745 INF("cmdline", S_IRUGO, pid_cmdline),
1746 INF("stat", S_IRUGO, tgid_stat),
1747 INF("statm", S_IRUGO, pid_statm),
1748 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001749#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001750 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001751#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001752 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001753#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07001754 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001755#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001756 LNK("cwd", cwd),
1757 LNK("root", root),
1758 LNK("exe", exe),
1759 REG("mounts", S_IRUGO, mounts),
1760 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07001762 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001763#endif
1764#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001765 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001766#endif
1767#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001768 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001769#endif
1770#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001771 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001772#endif
1773#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001774 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001775#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001776 INF("oom_score", S_IRUGO, oom_score),
1777 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07001779 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001780#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781};
1782
1783static int proc_tgid_base_readdir(struct file * filp,
1784 void * dirent, filldir_t filldir)
1785{
1786 return proc_pident_readdir(filp,dirent,filldir,
1787 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1788}
1789
1790static struct file_operations proc_tgid_base_operations = {
1791 .read = generic_read_dir,
1792 .readdir = proc_tgid_base_readdir,
1793};
1794
1795static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001796 return proc_pident_lookup(dir, dentry,
1797 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001798}
1799
1800static struct inode_operations proc_tgid_base_inode_operations = {
1801 .lookup = proc_tgid_base_lookup,
1802 .getattr = pid_getattr,
1803 .setattr = proc_setattr,
1804};
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001807 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001809 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001811 * Looks in the dcache for
1812 * /proc/@pid
1813 * /proc/@tgid/task/@pid
1814 * if either directory is present flushes it and all of it'ts children
1815 * from the dcache.
1816 *
1817 * It is safe and reasonable to cache /proc entries for a task until
1818 * that task exits. After that they just clog up the dcache with
1819 * useless entries, possibly causing useful dcache entries to be
1820 * flushed instead. This routine is proved to flush those useless
1821 * dcache entries at process exit time.
1822 *
1823 * NOTE: This routine is just an optimization so it does not guarantee
1824 * that no dcache entries will exist at process exit time it
1825 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001827void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001829 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001830 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001831 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Eric W. Biederman48e64842006-06-26 00:25:48 -07001833 name.name = buf;
1834 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1835 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1836 if (dentry) {
1837 shrink_dcache_parent(dentry);
1838 d_drop(dentry);
1839 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Eric W. Biederman48e64842006-06-26 00:25:48 -07001842 if (thread_group_leader(task))
1843 goto out;
1844
1845 name.name = buf;
1846 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1847 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1848 if (!leader)
1849 goto out;
1850
1851 name.name = "task";
1852 name.len = strlen(name.name);
1853 dir = d_hash_and_lookup(leader, &name);
1854 if (!dir)
1855 goto out_put_leader;
1856
1857 name.name = buf;
1858 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1859 dentry = d_hash_and_lookup(dir, &name);
1860 if (dentry) {
1861 shrink_dcache_parent(dentry);
1862 d_drop(dentry);
1863 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001865
1866 dput(dir);
1867out_put_leader:
1868 dput(leader);
1869out:
1870 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001873struct dentry *proc_pid_instantiate(struct inode *dir,
1874 struct dentry * dentry, struct task_struct *task, void *ptr)
1875{
1876 struct dentry *error = ERR_PTR(-ENOENT);
1877 struct inode *inode;
1878
Eric W. Biederman61a28782006-10-02 02:18:49 -07001879 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001880 if (!inode)
1881 goto out;
1882
1883 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1884 inode->i_op = &proc_tgid_base_inode_operations;
1885 inode->i_fop = &proc_tgid_base_operations;
1886 inode->i_flags|=S_IMMUTABLE;
1887 inode->i_nlink = 4;
1888#ifdef CONFIG_SECURITY
1889 inode->i_nlink += 1;
1890#endif
1891
1892 dentry->d_op = &pid_dentry_operations;
1893
1894 d_add(dentry, inode);
1895 /* Close the race of the process dying before we return the dentry */
1896 if (pid_revalidate(dentry, NULL))
1897 error = NULL;
1898out:
1899 return error;
1900}
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902/* SMP-safe */
1903struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1904{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001905 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Eric W. Biederman801199c2006-10-02 02:18:48 -07001909 result = proc_base_lookup(dir, dentry);
1910 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1911 goto out;
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 tgid = name_to_int(dentry);
1914 if (tgid == ~0U)
1915 goto out;
1916
Eric W. Biedermande758732006-06-26 00:25:51 -07001917 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 task = find_task_by_pid(tgid);
1919 if (task)
1920 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07001921 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (!task)
1923 goto out;
1924
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001925 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07001926 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001928 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001932 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001933 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001935static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001937 struct task_struct *task;
1938 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001940 rcu_read_lock();
1941retry:
1942 task = NULL;
1943 pid = find_ge_pid(tgid);
1944 if (pid) {
1945 tgid = pid->nr + 1;
1946 task = pid_task(pid, PIDTYPE_PID);
1947 /* What we to know is if the pid we have find is the
1948 * pid of a thread_group_leader. Testing for task
1949 * being a thread_group_leader is the obvious thing
1950 * todo but there is a window when it fails, due to
1951 * the pid transfer logic in de_thread.
1952 *
1953 * So we perform the straight forward test of seeing
1954 * if the pid we have found is the pid of a thread
1955 * group leader, and don't worry if the task we have
1956 * found doesn't happen to be a thread group leader.
1957 * As we don't care in the case of readdir.
1958 */
1959 if (!task || !has_group_leader_pid(task))
1960 goto retry;
1961 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07001963 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001964 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001967#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Eric W. Biederman61a28782006-10-02 02:18:49 -07001969static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1970 struct task_struct *task, int tgid)
1971{
1972 char name[PROC_NUMBUF];
1973 int len = snprintf(name, sizeof(name), "%d", tgid);
1974 return proc_fill_cache(filp, dirent, filldir, name, len,
1975 proc_pid_instantiate, task, NULL);
1976}
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978/* for the /proc/ directory itself, after non-process stuff has been done */
1979int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1980{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001982 struct task_struct *reaper = get_proc_task(filp->f_dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001983 struct task_struct *task;
1984 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Eric W. Biederman61a28782006-10-02 02:18:49 -07001986 if (!reaper)
1987 goto out_no_task;
1988
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001989 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric W. Biederman801199c2006-10-02 02:18:48 -07001990 struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07001991 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001992 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001995 tgid = filp->f_pos - TGID_OFFSET;
1996 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001997 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001998 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001999 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002000 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002001 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002002 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002003 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002006 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2007out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002008 put_task_struct(reaper);
2009out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return 0;
2011}
2012
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002013/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002014 * Tasks
2015 */
2016static struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002017 DIR("fd", S_IRUSR|S_IXUSR, fd),
2018 INF("environ", S_IRUSR, pid_environ),
2019 INF("auxv", S_IRUSR, pid_auxv),
2020 INF("status", S_IRUGO, pid_status),
2021 INF("cmdline", S_IRUGO, pid_cmdline),
2022 INF("stat", S_IRUGO, tid_stat),
2023 INF("statm", S_IRUGO, pid_statm),
2024 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002025#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002026 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002027#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002028 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002029#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002030 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002031#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002032 LNK("cwd", cwd),
2033 LNK("root", root),
2034 LNK("exe", exe),
2035 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002036#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07002037 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002038#endif
2039#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002040 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002041#endif
2042#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002043 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002044#endif
2045#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002046 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002047#endif
2048#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002049 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002050#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002051 INF("oom_score", S_IRUGO, oom_score),
2052 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002053#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002054 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002055#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002056};
2057
2058static int proc_tid_base_readdir(struct file * filp,
2059 void * dirent, filldir_t filldir)
2060{
2061 return proc_pident_readdir(filp,dirent,filldir,
2062 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2063}
2064
2065static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002066 return proc_pident_lookup(dir, dentry,
2067 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002068}
2069
2070static struct file_operations proc_tid_base_operations = {
2071 .read = generic_read_dir,
2072 .readdir = proc_tid_base_readdir,
2073};
2074
2075static struct inode_operations proc_tid_base_inode_operations = {
2076 .lookup = proc_tid_base_lookup,
2077 .getattr = pid_getattr,
2078 .setattr = proc_setattr,
2079};
2080
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002081static struct dentry *proc_task_instantiate(struct inode *dir,
2082 struct dentry *dentry, struct task_struct *task, void *ptr)
2083{
2084 struct dentry *error = ERR_PTR(-ENOENT);
2085 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002086 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002087
2088 if (!inode)
2089 goto out;
2090 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2091 inode->i_op = &proc_tid_base_inode_operations;
2092 inode->i_fop = &proc_tid_base_operations;
2093 inode->i_flags|=S_IMMUTABLE;
2094 inode->i_nlink = 3;
2095#ifdef CONFIG_SECURITY
2096 inode->i_nlink += 1;
2097#endif
2098
2099 dentry->d_op = &pid_dentry_operations;
2100
2101 d_add(dentry, inode);
2102 /* Close the race of the process dying before we return the dentry */
2103 if (pid_revalidate(dentry, NULL))
2104 error = NULL;
2105out:
2106 return error;
2107}
2108
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002109/* SMP-safe */
2110static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2111{
2112 struct dentry *result = ERR_PTR(-ENOENT);
2113 struct task_struct *task;
2114 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002115 unsigned tid;
2116
2117 if (!leader)
2118 goto out_no_task;
2119
2120 tid = name_to_int(dentry);
2121 if (tid == ~0U)
2122 goto out;
2123
2124 rcu_read_lock();
2125 task = find_task_by_pid(tid);
2126 if (task)
2127 get_task_struct(task);
2128 rcu_read_unlock();
2129 if (!task)
2130 goto out;
2131 if (leader->tgid != task->tgid)
2132 goto out_drop_task;
2133
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002134 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002135out_drop_task:
2136 put_task_struct(task);
2137out:
2138 put_task_struct(leader);
2139out_no_task:
2140 return result;
2141}
2142
2143/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002144 * Find the first tid of a thread group to return to user space.
2145 *
2146 * Usually this is just the thread group leader, but if the users
2147 * buffer was too small or there was a seek into the middle of the
2148 * directory we have more work todo.
2149 *
2150 * In the case of a short read we start with find_task_by_pid.
2151 *
2152 * In the case of a seek we start with the leader and walk nr
2153 * threads past it.
2154 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002155static struct task_struct *first_tid(struct task_struct *leader,
2156 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002157{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002158 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002159
Eric W. Biedermancc288732006-06-26 00:26:01 -07002160 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002161 /* Attempt to start with the pid of a thread */
2162 if (tid && (nr > 0)) {
2163 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002164 if (pos && (pos->group_leader == leader))
2165 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002166 }
2167
2168 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002169 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002170 if (nr && nr >= get_nr_threads(leader))
2171 goto out;
2172
2173 /* If we haven't found our starting place yet start
2174 * with the leader and walk nr threads forward.
2175 */
2176 for (pos = leader; nr > 0; --nr) {
2177 pos = next_thread(pos);
2178 if (pos == leader) {
2179 pos = NULL;
2180 goto out;
2181 }
2182 }
2183found:
2184 get_task_struct(pos);
2185out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002186 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002187 return pos;
2188}
2189
2190/*
2191 * Find the next thread in the thread list.
2192 * Return NULL if there is an error or no next thread.
2193 *
2194 * The reference to the input task_struct is released.
2195 */
2196static struct task_struct *next_tid(struct task_struct *start)
2197{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002198 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002199 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002200 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002201 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002202 if (thread_group_leader(pos))
2203 pos = NULL;
2204 else
2205 get_task_struct(pos);
2206 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002207 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002208 put_task_struct(start);
2209 return pos;
2210}
2211
Eric W. Biederman61a28782006-10-02 02:18:49 -07002212static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2213 struct task_struct *task, int tid)
2214{
2215 char name[PROC_NUMBUF];
2216 int len = snprintf(name, sizeof(name), "%d", tid);
2217 return proc_fill_cache(filp, dirent, filldir, name, len,
2218 proc_task_instantiate, task, NULL);
2219}
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221/* for the /proc/TGID/task/ directories */
2222static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2223{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 struct dentry *dentry = filp->f_dentry;
2225 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002226 struct task_struct *leader = get_proc_task(inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002227 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 int retval = -ENOENT;
2229 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002230 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2232
Eric W. Biederman99f89552006-06-26 00:25:55 -07002233 if (!leader)
2234 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 retval = 0;
2236
2237 switch (pos) {
2238 case 0:
2239 ino = inode->i_ino;
2240 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2241 goto out;
2242 pos++;
2243 /* fall through */
2244 case 1:
2245 ino = parent_ino(dentry);
2246 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2247 goto out;
2248 pos++;
2249 /* fall through */
2250 }
2251
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002252 /* f_version caches the tgid value that the last readdir call couldn't
2253 * return. lseek aka telldir automagically resets f_version to 0.
2254 */
2255 tid = filp->f_version;
2256 filp->f_version = 0;
2257 for (task = first_tid(leader, tid, pos - 2);
2258 task;
2259 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002260 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002261 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002262 /* returning this tgid failed, save it as the first
2263 * pid for the next readir call */
2264 filp->f_version = tid;
2265 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269out:
2270 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002271 put_task_struct(leader);
2272out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return retval;
2274}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002275
2276static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2277{
2278 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002279 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002280 generic_fillattr(inode, stat);
2281
Eric W. Biederman99f89552006-06-26 00:25:55 -07002282 if (p) {
2283 rcu_read_lock();
2284 stat->nlink += get_nr_threads(p);
2285 rcu_read_unlock();
2286 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002287 }
2288
2289 return 0;
2290}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002291
2292static struct inode_operations proc_task_inode_operations = {
2293 .lookup = proc_task_lookup,
2294 .getattr = proc_task_getattr,
2295 .setattr = proc_setattr,
2296};
2297
2298static struct file_operations proc_task_operations = {
2299 .read = generic_read_dir,
2300 .readdir = proc_task_readdir,
2301};