blob: 8df27401d29207683e3dd1ad1a0d0ac5b5508adb [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>
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);
368 struct namespace *namespace = 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);
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700374 namespace = task->nsproxy->namespace;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700375 if (namespace)
376 get_namespace(namespace);
377 task_unlock(task);
378 put_task_struct(task);
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Al Viro5addc5d2005-11-07 17:15:49 -0500381 if (namespace) {
382 ret = -ENOMEM;
383 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
384 if (p) {
385 file->private_data = &p->m;
386 ret = seq_open(file, &mounts_op);
387 if (!ret) {
388 p->m.private = namespace;
389 p->event = namespace->event;
390 return 0;
391 }
392 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Al Viro5addc5d2005-11-07 17:15:49 -0500394 put_namespace(namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 return ret;
397}
398
399static int mounts_release(struct inode *inode, struct file *file)
400{
401 struct seq_file *m = file->private_data;
402 struct namespace *namespace = m->private;
403 put_namespace(namespace);
404 return seq_release(inode, file);
405}
406
Al Viro5addc5d2005-11-07 17:15:49 -0500407static unsigned mounts_poll(struct file *file, poll_table *wait)
408{
409 struct proc_mounts *p = file->private_data;
410 struct namespace *ns = p->m.private;
411 unsigned res = 0;
412
413 poll_wait(file, &ns->poll, wait);
414
415 spin_lock(&vfsmount_lock);
416 if (p->event != ns->event) {
417 p->event = ns->event;
418 res = POLLERR;
419 }
420 spin_unlock(&vfsmount_lock);
421
422 return res;
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static struct file_operations proc_mounts_operations = {
426 .open = mounts_open,
427 .read = seq_read,
428 .llseek = seq_lseek,
429 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500430 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431};
432
Chuck Leverb4629fe2006-03-20 13:44:12 -0500433extern struct seq_operations mountstats_op;
434static int mountstats_open(struct inode *inode, struct file *file)
435{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500436 int ret = seq_open(file, &mountstats_op);
437
438 if (!ret) {
439 struct seq_file *m = file->private_data;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700440 struct namespace *namespace = NULL;
441 struct task_struct *task = get_proc_task(inode);
442
443 if (task) {
444 task_lock(task);
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700445 namespace = task->nsproxy->namespace;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700446 if (namespace)
447 get_namespace(namespace);
448 task_unlock(task);
449 put_task_struct(task);
450 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500451
452 if (namespace)
453 m->private = namespace;
454 else {
455 seq_release(inode, file);
456 ret = -EINVAL;
457 }
458 }
459 return ret;
460}
461
462static struct file_operations proc_mountstats_operations = {
463 .open = mountstats_open,
464 .read = seq_read,
465 .llseek = seq_lseek,
466 .release = mounts_release,
467};
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
470
471static ssize_t proc_info_read(struct file * file, char __user * buf,
472 size_t count, loff_t *ppos)
473{
474 struct inode * inode = file->f_dentry->d_inode;
475 unsigned long page;
476 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700477 struct task_struct *task = get_proc_task(inode);
478
479 length = -ESRCH;
480 if (!task)
481 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (count > PROC_BLOCK_SIZE)
484 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700485
486 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 length = PROC_I(inode)->op.proc_read(task, (char*)page);
491
492 if (length >= 0)
493 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
494 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700495out:
496 put_task_struct(task);
497out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return length;
499}
500
501static struct file_operations proc_info_file_operations = {
502 .read = proc_info_read,
503};
504
505static int mem_open(struct inode* inode, struct file* file)
506{
507 file->private_data = (void*)((long)current->self_exec_id);
508 return 0;
509}
510
511static ssize_t mem_read(struct file * file, char __user * buf,
512 size_t count, loff_t *ppos)
513{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700514 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 char *page;
516 unsigned long src = *ppos;
517 int ret = -ESRCH;
518 struct mm_struct *mm;
519
Eric W. Biederman99f89552006-06-26 00:25:55 -0700520 if (!task)
521 goto out_no_task;
522
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700523 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto out;
525
526 ret = -ENOMEM;
527 page = (char *)__get_free_page(GFP_USER);
528 if (!page)
529 goto out;
530
531 ret = 0;
532
533 mm = get_task_mm(task);
534 if (!mm)
535 goto out_free;
536
537 ret = -EIO;
538
539 if (file->private_data != (void*)((long)current->self_exec_id))
540 goto out_put;
541
542 ret = 0;
543
544 while (count > 0) {
545 int this_len, retval;
546
547 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
548 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700549 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!ret)
551 ret = -EIO;
552 break;
553 }
554
555 if (copy_to_user(buf, page, retval)) {
556 ret = -EFAULT;
557 break;
558 }
559
560 ret += retval;
561 src += retval;
562 buf += retval;
563 count -= retval;
564 }
565 *ppos = src;
566
567out_put:
568 mmput(mm);
569out_free:
570 free_page((unsigned long) page);
571out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700572 put_task_struct(task);
573out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return ret;
575}
576
577#define mem_write NULL
578
579#ifndef mem_write
580/* This is a security hazard */
581static ssize_t mem_write(struct file * file, const char * buf,
582 size_t count, loff_t *ppos)
583{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700584 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 char *page;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700586 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 unsigned long dst = *ppos;
588
Eric W. Biederman99f89552006-06-26 00:25:55 -0700589 copied = -ESRCH;
590 if (!task)
591 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Eric W. Biederman99f89552006-06-26 00:25:55 -0700593 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
594 goto out;
595
596 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 page = (char *)__get_free_page(GFP_USER);
598 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700599 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700601 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 while (count > 0) {
603 int this_len, retval;
604
605 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
606 if (copy_from_user(page, buf, this_len)) {
607 copied = -EFAULT;
608 break;
609 }
610 retval = access_process_vm(task, dst, page, this_len, 1);
611 if (!retval) {
612 if (!copied)
613 copied = -EIO;
614 break;
615 }
616 copied += retval;
617 buf += retval;
618 dst += retval;
619 count -= retval;
620 }
621 *ppos = dst;
622 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700623out:
624 put_task_struct(task);
625out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return copied;
627}
628#endif
629
630static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
631{
632 switch (orig) {
633 case 0:
634 file->f_pos = offset;
635 break;
636 case 1:
637 file->f_pos += offset;
638 break;
639 default:
640 return -EINVAL;
641 }
642 force_successful_syscall_return();
643 return file->f_pos;
644}
645
646static struct file_operations proc_mem_operations = {
647 .llseek = mem_lseek,
648 .read = mem_read,
649 .write = mem_write,
650 .open = mem_open,
651};
652
653static ssize_t oom_adjust_read(struct file *file, char __user *buf,
654 size_t count, loff_t *ppos)
655{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700656 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700657 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700659 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 loff_t __ppos = *ppos;
661
Eric W. Biederman99f89552006-06-26 00:25:55 -0700662 if (!task)
663 return -ESRCH;
664 oom_adjust = task->oomkilladj;
665 put_task_struct(task);
666
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700667 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (__ppos >= len)
669 return 0;
670 if (count > len-__ppos)
671 count = len-__ppos;
672 if (copy_to_user(buf, buffer + __ppos, count))
673 return -EFAULT;
674 *ppos = __ppos + count;
675 return count;
676}
677
678static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
679 size_t count, loff_t *ppos)
680{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700681 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700682 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int oom_adjust;
684
685 if (!capable(CAP_SYS_RESOURCE))
686 return -EPERM;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700687 memset(buffer, 0, sizeof(buffer));
688 if (count > sizeof(buffer) - 1)
689 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (copy_from_user(buffer, buf, count))
691 return -EFAULT;
692 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700693 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
694 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -EINVAL;
696 if (*end == '\n')
697 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700698 task = get_proc_task(file->f_dentry->d_inode);
699 if (!task)
700 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700702 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (end - buffer == 0)
704 return -EIO;
705 return end - buffer;
706}
707
708static struct file_operations proc_oom_adjust_operations = {
709 .read = oom_adjust_read,
710 .write = oom_adjust_write,
711};
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713#ifdef CONFIG_AUDITSYSCALL
714#define TMPBUFLEN 21
715static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
716 size_t count, loff_t *ppos)
717{
718 struct inode * inode = file->f_dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700719 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ssize_t length;
721 char tmpbuf[TMPBUFLEN];
722
Eric W. Biederman99f89552006-06-26 00:25:55 -0700723 if (!task)
724 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
726 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700727 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
729}
730
731static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
732 size_t count, loff_t *ppos)
733{
734 struct inode * inode = file->f_dentry->d_inode;
735 char *page, *tmp;
736 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 uid_t loginuid;
738
739 if (!capable(CAP_AUDIT_CONTROL))
740 return -EPERM;
741
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700742 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -EPERM;
744
Al Viroe0182902006-05-18 08:28:02 -0400745 if (count >= PAGE_SIZE)
746 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (*ppos != 0) {
749 /* No partial writes. */
750 return -EINVAL;
751 }
752 page = (char*)__get_free_page(GFP_USER);
753 if (!page)
754 return -ENOMEM;
755 length = -EFAULT;
756 if (copy_from_user(page, buf, count))
757 goto out_free_page;
758
Al Viroe0182902006-05-18 08:28:02 -0400759 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 loginuid = simple_strtoul(page, &tmp, 10);
761 if (tmp == page) {
762 length = -EINVAL;
763 goto out_free_page;
764
765 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700766 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (likely(length == 0))
768 length = count;
769
770out_free_page:
771 free_page((unsigned long) page);
772 return length;
773}
774
775static struct file_operations proc_loginuid_operations = {
776 .read = proc_loginuid_read,
777 .write = proc_loginuid_write,
778};
779#endif
780
781#ifdef CONFIG_SECCOMP
782static ssize_t seccomp_read(struct file *file, char __user *buf,
783 size_t count, loff_t *ppos)
784{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700785 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 char __buf[20];
787 loff_t __ppos = *ppos;
788 size_t len;
789
Eric W. Biederman99f89552006-06-26 00:25:55 -0700790 if (!tsk)
791 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /* no need to print the trailing zero, so use only len */
793 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700794 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (__ppos >= len)
796 return 0;
797 if (count > len - __ppos)
798 count = len - __ppos;
799 if (copy_to_user(buf, __buf + __ppos, count))
800 return -EFAULT;
801 *ppos = __ppos + count;
802 return count;
803}
804
805static ssize_t seccomp_write(struct file *file, const char __user *buf,
806 size_t count, loff_t *ppos)
807{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700808 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 char __buf[20], *end;
810 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700811 ssize_t result;
812
813 result = -ESRCH;
814 if (!tsk)
815 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700818 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700820 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Eric W. Biederman99f89552006-06-26 00:25:55 -0700822 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 memset(__buf, 0, sizeof(__buf));
824 count = min(count, sizeof(__buf) - 1);
825 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700826 goto out;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 seccomp_mode = simple_strtoul(__buf, &end, 0);
829 if (*end == '\n')
830 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700831 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
833 tsk->seccomp.mode = seccomp_mode;
834 set_tsk_thread_flag(tsk, TIF_SECCOMP);
835 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700836 goto out;
837 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700839 goto out;
840 result = end - __buf;
841out:
842 put_task_struct(tsk);
843out_no_task:
844 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847static struct file_operations proc_seccomp_operations = {
848 .read = seccomp_read,
849 .write = seccomp_write,
850};
851#endif /* CONFIG_SECCOMP */
852
Al Viro008b1502005-08-20 00:17:39 +0100853static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct inode *inode = dentry->d_inode;
856 int error = -EACCES;
857
858 /* We don't need a base pointer in the /proc filesystem */
859 path_release(nd);
860
Eric W. Biederman778c1142006-06-26 00:25:58 -0700861 /* Are we allowed to snoop on the tasks file descriptors? */
862 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
866 nd->last_type = LAST_BIND;
867out:
Al Viro008b1502005-08-20 00:17:39 +0100868 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
872 char __user *buffer, int buflen)
873{
874 struct inode * inode;
875 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
876 int len;
877
878 if (!tmp)
879 return -ENOMEM;
880
881 inode = dentry->d_inode;
882 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
883 len = PTR_ERR(path);
884 if (IS_ERR(path))
885 goto out;
886 len = tmp + PAGE_SIZE - 1 - path;
887
888 if (len > buflen)
889 len = buflen;
890 if (copy_to_user(buffer, path, len))
891 len = -EFAULT;
892 out:
893 free_page((unsigned long)tmp);
894 return len;
895}
896
897static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
898{
899 int error = -EACCES;
900 struct inode *inode = dentry->d_inode;
901 struct dentry *de;
902 struct vfsmount *mnt = NULL;
903
Eric W. Biederman778c1142006-06-26 00:25:58 -0700904 /* Are we allowed to snoop on the tasks file descriptors? */
905 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
909 if (error)
910 goto out;
911
912 error = do_proc_readlink(de, mnt, buffer, buflen);
913 dput(de);
914 mntput(mnt);
915out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return error;
917}
918
919static struct inode_operations proc_pid_link_inode_operations = {
920 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700921 .follow_link = proc_pid_follow_link,
922 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923};
924
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700925
926/* building an inode */
927
928static int task_dumpable(struct task_struct *task)
929{
930 int dumpable = 0;
931 struct mm_struct *mm;
932
933 task_lock(task);
934 mm = task->mm;
935 if (mm)
936 dumpable = mm->dumpable;
937 task_unlock(task);
938 if(dumpable == 1)
939 return 1;
940 return 0;
941}
942
943
Eric W. Biederman61a28782006-10-02 02:18:49 -0700944static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700945{
946 struct inode * inode;
947 struct proc_inode *ei;
948
949 /* We need a new inode */
950
951 inode = new_inode(sb);
952 if (!inode)
953 goto out;
954
955 /* Common stuff */
956 ei = PROC_I(inode);
957 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700958 inode->i_op = &proc_def_inode_operations;
959
960 /*
961 * grab the reference to task.
962 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -0700963 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700964 if (!ei->pid)
965 goto out_unlock;
966
967 inode->i_uid = 0;
968 inode->i_gid = 0;
969 if (task_dumpable(task)) {
970 inode->i_uid = task->euid;
971 inode->i_gid = task->egid;
972 }
973 security_task_to_inode(task, inode);
974
975out:
976 return inode;
977
978out_unlock:
979 iput(inode);
980 return NULL;
981}
982
983static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
984{
985 struct inode *inode = dentry->d_inode;
986 struct task_struct *task;
987 generic_fillattr(inode, stat);
988
989 rcu_read_lock();
990 stat->uid = 0;
991 stat->gid = 0;
992 task = pid_task(proc_pid(inode), PIDTYPE_PID);
993 if (task) {
994 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
995 task_dumpable(task)) {
996 stat->uid = task->euid;
997 stat->gid = task->egid;
998 }
999 }
1000 rcu_read_unlock();
1001 return 0;
1002}
1003
1004/* dentry stuff */
1005
1006/*
1007 * Exceptional case: normally we are not allowed to unhash a busy
1008 * directory. In this case, however, we can do it - no aliasing problems
1009 * due to the way we treat inodes.
1010 *
1011 * Rewrite the inode's ownerships here because the owning task may have
1012 * performed a setuid(), etc.
1013 *
1014 * Before the /proc/pid/status file was created the only way to read
1015 * the effective uid of a /process was to stat /proc/pid. Reading
1016 * /proc/pid/status is slow enough that procps and other packages
1017 * kept stating /proc/pid. To keep the rules in /proc simple I have
1018 * made this apply to all per process world readable and executable
1019 * directories.
1020 */
1021static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1022{
1023 struct inode *inode = dentry->d_inode;
1024 struct task_struct *task = get_proc_task(inode);
1025 if (task) {
1026 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1027 task_dumpable(task)) {
1028 inode->i_uid = task->euid;
1029 inode->i_gid = task->egid;
1030 } else {
1031 inode->i_uid = 0;
1032 inode->i_gid = 0;
1033 }
1034 inode->i_mode &= ~(S_ISUID | S_ISGID);
1035 security_task_to_inode(task, inode);
1036 put_task_struct(task);
1037 return 1;
1038 }
1039 d_drop(dentry);
1040 return 0;
1041}
1042
1043static int pid_delete_dentry(struct dentry * dentry)
1044{
1045 /* Is the task we represent dead?
1046 * If so, then don't put the dentry on the lru list,
1047 * kill it immediately.
1048 */
1049 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1050}
1051
1052static struct dentry_operations pid_dentry_operations =
1053{
1054 .d_revalidate = pid_revalidate,
1055 .d_delete = pid_delete_dentry,
1056};
1057
1058/* Lookups */
1059
Eric W. Biederman61a28782006-10-02 02:18:49 -07001060typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1061
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001062/*
1063 * Fill a directory entry.
1064 *
1065 * If possible create the dcache entry and derive our inode number and
1066 * file type from dcache entry.
1067 *
1068 * Since all of the proc inode numbers are dynamically generated, the inode
1069 * numbers do not exist until the inode is cache. This means creating the
1070 * the dcache entry in readdir is necessary to keep the inode numbers
1071 * reported by readdir in sync with the inode numbers reported
1072 * by stat.
1073 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001074static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1075 char *name, int len,
1076 instantiate_t instantiate, struct task_struct *task, void *ptr)
1077{
1078 struct dentry *child, *dir = filp->f_dentry;
1079 struct inode *inode;
1080 struct qstr qname;
1081 ino_t ino = 0;
1082 unsigned type = DT_UNKNOWN;
1083
1084 qname.name = name;
1085 qname.len = len;
1086 qname.hash = full_name_hash(name, len);
1087
1088 child = d_lookup(dir, &qname);
1089 if (!child) {
1090 struct dentry *new;
1091 new = d_alloc(dir, &qname);
1092 if (new) {
1093 child = instantiate(dir->d_inode, new, task, ptr);
1094 if (child)
1095 dput(new);
1096 else
1097 child = new;
1098 }
1099 }
1100 if (!child || IS_ERR(child) || !child->d_inode)
1101 goto end_instantiate;
1102 inode = child->d_inode;
1103 if (inode) {
1104 ino = inode->i_ino;
1105 type = inode->i_mode >> 12;
1106 }
1107 dput(child);
1108end_instantiate:
1109 if (!ino)
1110 ino = find_inode_number(dir, &qname);
1111 if (!ino)
1112 ino = 1;
1113 return filldir(dirent, name, len, filp->f_pos, ino, type);
1114}
1115
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001116static unsigned name_to_int(struct dentry *dentry)
1117{
1118 const char *name = dentry->d_name.name;
1119 int len = dentry->d_name.len;
1120 unsigned n = 0;
1121
1122 if (len > 1 && *name == '0')
1123 goto out;
1124 while (len-- > 0) {
1125 unsigned c = *name++ - '0';
1126 if (c > 9)
1127 goto out;
1128 if (n >= (~0U-9)/10)
1129 goto out;
1130 n *= 10;
1131 n += c;
1132 }
1133 return n;
1134out:
1135 return ~0U;
1136}
1137
1138static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1139{
1140 struct task_struct *task = get_proc_task(inode);
1141 struct files_struct *files = NULL;
1142 struct file *file;
1143 int fd = proc_fd(inode);
1144
1145 if (task) {
1146 files = get_files_struct(task);
1147 put_task_struct(task);
1148 }
1149 if (files) {
1150 /*
1151 * We are not taking a ref to the file structure, so we must
1152 * hold ->file_lock.
1153 */
1154 spin_lock(&files->file_lock);
1155 file = fcheck_files(files, fd);
1156 if (file) {
1157 *mnt = mntget(file->f_vfsmnt);
1158 *dentry = dget(file->f_dentry);
1159 spin_unlock(&files->file_lock);
1160 put_files_struct(files);
1161 return 0;
1162 }
1163 spin_unlock(&files->file_lock);
1164 put_files_struct(files);
1165 }
1166 return -ENOENT;
1167}
1168
1169static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1170{
1171 struct inode *inode = dentry->d_inode;
1172 struct task_struct *task = get_proc_task(inode);
1173 int fd = proc_fd(inode);
1174 struct files_struct *files;
1175
1176 if (task) {
1177 files = get_files_struct(task);
1178 if (files) {
1179 rcu_read_lock();
1180 if (fcheck_files(files, fd)) {
1181 rcu_read_unlock();
1182 put_files_struct(files);
1183 if (task_dumpable(task)) {
1184 inode->i_uid = task->euid;
1185 inode->i_gid = task->egid;
1186 } else {
1187 inode->i_uid = 0;
1188 inode->i_gid = 0;
1189 }
1190 inode->i_mode &= ~(S_ISUID | S_ISGID);
1191 security_task_to_inode(task, inode);
1192 put_task_struct(task);
1193 return 1;
1194 }
1195 rcu_read_unlock();
1196 put_files_struct(files);
1197 }
1198 put_task_struct(task);
1199 }
1200 d_drop(dentry);
1201 return 0;
1202}
1203
1204static struct dentry_operations tid_fd_dentry_operations =
1205{
1206 .d_revalidate = tid_fd_revalidate,
1207 .d_delete = pid_delete_dentry,
1208};
1209
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001210static struct dentry *proc_fd_instantiate(struct inode *dir,
1211 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001212{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001213 unsigned fd = *(unsigned *)ptr;
1214 struct file *file;
1215 struct files_struct *files;
1216 struct inode *inode;
1217 struct proc_inode *ei;
1218 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001219
Eric W. Biederman61a28782006-10-02 02:18:49 -07001220 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001221 if (!inode)
1222 goto out;
1223 ei = PROC_I(inode);
1224 ei->fd = fd;
1225 files = get_files_struct(task);
1226 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001227 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001228 inode->i_mode = S_IFLNK;
1229
1230 /*
1231 * We are not taking a ref to the file structure, so we must
1232 * hold ->file_lock.
1233 */
1234 spin_lock(&files->file_lock);
1235 file = fcheck_files(files, fd);
1236 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001237 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001238 if (file->f_mode & 1)
1239 inode->i_mode |= S_IRUSR | S_IXUSR;
1240 if (file->f_mode & 2)
1241 inode->i_mode |= S_IWUSR | S_IXUSR;
1242 spin_unlock(&files->file_lock);
1243 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001244
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001245 inode->i_op = &proc_pid_link_inode_operations;
1246 inode->i_size = 64;
1247 ei->op.proc_get_link = proc_fd_link;
1248 dentry->d_op = &tid_fd_dentry_operations;
1249 d_add(dentry, inode);
1250 /* Close the race of the process dying before we return the dentry */
1251 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001252 error = NULL;
1253
1254 out:
1255 return error;
1256out_unlock:
1257 spin_unlock(&files->file_lock);
1258 put_files_struct(files);
1259out_iput:
1260 iput(inode);
1261 goto out;
1262}
1263
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001264static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1265{
1266 struct task_struct *task = get_proc_task(dir);
1267 unsigned fd = name_to_int(dentry);
1268 struct dentry *result = ERR_PTR(-ENOENT);
1269
1270 if (!task)
1271 goto out_no_task;
1272 if (fd == ~0U)
1273 goto out;
1274
1275 result = proc_fd_instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001276out:
1277 put_task_struct(task);
1278out_no_task:
1279 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001280}
1281
Eric W. Biederman61a28782006-10-02 02:18:49 -07001282static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1283 struct task_struct *task, int fd)
1284{
1285 char name[PROC_NUMBUF];
1286 int len = snprintf(name, sizeof(name), "%d", fd);
1287 return proc_fill_cache(filp, dirent, filldir, name, len,
1288 proc_fd_instantiate, task, &fd);
1289}
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1292{
Eric W. Biederman56347082006-06-26 00:25:40 -07001293 struct dentry *dentry = filp->f_dentry;
1294 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001295 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 unsigned int fd, tid, ino;
1297 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001299 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001302 if (!p)
1303 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 retval = 0;
1305 tid = p->pid;
1306
1307 fd = filp->f_pos;
1308 switch (fd) {
1309 case 0:
1310 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1311 goto out;
1312 filp->f_pos++;
1313 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001314 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1316 goto out;
1317 filp->f_pos++;
1318 default:
1319 files = get_files_struct(p);
1320 if (!files)
1321 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001322 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001323 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001325 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 fd++, filp->f_pos++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (!fcheck_files(files, fd))
1329 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001330 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Eric W. Biederman61a28782006-10-02 02:18:49 -07001332 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001333 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
1335 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001336 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001338 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 put_files_struct(files);
1340 }
1341out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001342 put_task_struct(p);
1343out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return retval;
1345}
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347static struct file_operations proc_fd_operations = {
1348 .read = generic_read_dir,
1349 .readdir = proc_readfd,
1350};
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352/*
1353 * proc directories can do almost nothing..
1354 */
1355static struct inode_operations proc_fd_inode_operations = {
1356 .lookup = proc_lookupfd,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001357 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358};
1359
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001360static struct dentry *proc_pident_instantiate(struct inode *dir,
1361 struct dentry *dentry, struct task_struct *task, void *ptr)
1362{
1363 struct pid_entry *p = ptr;
1364 struct inode *inode;
1365 struct proc_inode *ei;
1366 struct dentry *error = ERR_PTR(-EINVAL);
1367
Eric W. Biederman61a28782006-10-02 02:18:49 -07001368 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001369 if (!inode)
1370 goto out;
1371
1372 ei = PROC_I(inode);
1373 inode->i_mode = p->mode;
1374 if (S_ISDIR(inode->i_mode))
1375 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1376 if (p->iop)
1377 inode->i_op = p->iop;
1378 if (p->fop)
1379 inode->i_fop = p->fop;
1380 ei->op = p->op;
1381 dentry->d_op = &pid_dentry_operations;
1382 d_add(dentry, inode);
1383 /* Close the race of the process dying before we return the dentry */
1384 if (pid_revalidate(dentry, NULL))
1385 error = NULL;
1386out:
1387 return error;
1388}
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390static struct dentry *proc_pident_lookup(struct inode *dir,
1391 struct dentry *dentry,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001392 struct pid_entry *ents,
1393 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001396 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001397 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001398 struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001400 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 inode = NULL;
1402
Eric W. Biederman99f89552006-06-26 00:25:55 -07001403 if (!task)
1404 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001406 /*
1407 * Yes, it does not scale. And it should not. Don't add
1408 * new entries into /proc/<tgid>/ without very good reasons.
1409 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001410 last = &ents[nents - 1];
1411 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (p->len != dentry->d_name.len)
1413 continue;
1414 if (!memcmp(dentry->d_name.name, p->name, p->len))
1415 break;
1416 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001417 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto out;
1419
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001420 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001422 put_task_struct(task);
1423out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001424 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Eric W. Biederman61a28782006-10-02 02:18:49 -07001427static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1428 struct task_struct *task, struct pid_entry *p)
1429{
1430 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1431 proc_pident_instantiate, task, p);
1432}
1433
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001434static int proc_pident_readdir(struct file *filp,
1435 void *dirent, filldir_t filldir,
1436 struct pid_entry *ents, unsigned int nents)
1437{
1438 int i;
1439 int pid;
1440 struct dentry *dentry = filp->f_dentry;
1441 struct inode *inode = dentry->d_inode;
1442 struct task_struct *task = get_proc_task(inode);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001443 struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001444 ino_t ino;
1445 int ret;
1446
1447 ret = -ENOENT;
1448 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001449 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001450
1451 ret = 0;
1452 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001453 i = filp->f_pos;
1454 switch (i) {
1455 case 0:
1456 ino = inode->i_ino;
1457 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1458 goto out;
1459 i++;
1460 filp->f_pos++;
1461 /* fall through */
1462 case 1:
1463 ino = parent_ino(dentry);
1464 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1465 goto out;
1466 i++;
1467 filp->f_pos++;
1468 /* fall through */
1469 default:
1470 i -= 2;
1471 if (i >= nents) {
1472 ret = 1;
1473 goto out;
1474 }
1475 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001476 last = &ents[nents - 1];
1477 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001478 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001479 goto out;
1480 filp->f_pos++;
1481 p++;
1482 }
1483 }
1484
1485 ret = 1;
1486out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001487 put_task_struct(task);
1488out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001489 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001493static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1494 size_t count, loff_t *ppos)
1495{
1496 struct inode * inode = file->f_dentry->d_inode;
1497 unsigned long page;
1498 ssize_t length;
1499 struct task_struct *task = get_proc_task(inode);
1500
1501 length = -ESRCH;
1502 if (!task)
1503 goto out_no_task;
1504
1505 if (count > PAGE_SIZE)
1506 count = PAGE_SIZE;
1507 length = -ENOMEM;
1508 if (!(page = __get_free_page(GFP_KERNEL)))
1509 goto out;
1510
1511 length = security_getprocattr(task,
1512 (char*)file->f_dentry->d_name.name,
1513 (void*)page, count);
1514 if (length >= 0)
1515 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1516 free_page(page);
1517out:
1518 put_task_struct(task);
1519out_no_task:
1520 return length;
1521}
1522
1523static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1524 size_t count, loff_t *ppos)
1525{
1526 struct inode * inode = file->f_dentry->d_inode;
1527 char *page;
1528 ssize_t length;
1529 struct task_struct *task = get_proc_task(inode);
1530
1531 length = -ESRCH;
1532 if (!task)
1533 goto out_no_task;
1534 if (count > PAGE_SIZE)
1535 count = PAGE_SIZE;
1536
1537 /* No partial writes. */
1538 length = -EINVAL;
1539 if (*ppos != 0)
1540 goto out;
1541
1542 length = -ENOMEM;
1543 page = (char*)__get_free_page(GFP_USER);
1544 if (!page)
1545 goto out;
1546
1547 length = -EFAULT;
1548 if (copy_from_user(page, buf, count))
1549 goto out_free;
1550
1551 length = security_setprocattr(task,
1552 (char*)file->f_dentry->d_name.name,
1553 (void*)page, count);
1554out_free:
1555 free_page((unsigned long) page);
1556out:
1557 put_task_struct(task);
1558out_no_task:
1559 return length;
1560}
1561
1562static struct file_operations proc_pid_attr_operations = {
1563 .read = proc_pid_attr_read,
1564 .write = proc_pid_attr_write,
1565};
1566
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001567static struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001568 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1569 REG("prev", S_IRUGO, pid_attr),
1570 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1571 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1572 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1573 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001574};
1575
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001576static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 void * dirent, filldir_t filldir)
1578{
1579 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001580 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 file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001585 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586};
1587
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001588static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 struct dentry *dentry, struct nameidata *nd)
1590{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001591 return proc_pident_lookup(dir, dentry,
1592 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001595static struct inode_operations proc_attr_dir_inode_operations = {
1596 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001597 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001598 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599};
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601#endif
1602
1603/*
1604 * /proc/self:
1605 */
1606static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1607 int buflen)
1608{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001609 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 sprintf(tmp, "%d", current->tgid);
1611 return vfs_readlink(dentry,buffer,buflen,tmp);
1612}
1613
Al Viro008b1502005-08-20 00:17:39 +01001614static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001616 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001618 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001619}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621static struct inode_operations proc_self_inode_operations = {
1622 .readlink = proc_self_readlink,
1623 .follow_link = proc_self_follow_link,
1624};
1625
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001626/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001627 * proc base
1628 *
1629 * These are the directory entries in the root directory of /proc
1630 * that properly belong to the /proc filesystem, as they describe
1631 * describe something that is process related.
1632 */
1633static struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001634 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001635 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001636};
1637
1638/*
1639 * Exceptional case: normally we are not allowed to unhash a busy
1640 * directory. In this case, however, we can do it - no aliasing problems
1641 * due to the way we treat inodes.
1642 */
1643static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1644{
1645 struct inode *inode = dentry->d_inode;
1646 struct task_struct *task = get_proc_task(inode);
1647 if (task) {
1648 put_task_struct(task);
1649 return 1;
1650 }
1651 d_drop(dentry);
1652 return 0;
1653}
1654
1655static struct dentry_operations proc_base_dentry_operations =
1656{
1657 .d_revalidate = proc_base_revalidate,
1658 .d_delete = pid_delete_dentry,
1659};
1660
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001661static struct dentry *proc_base_instantiate(struct inode *dir,
1662 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001663{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001664 struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001665 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001666 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001667 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001668
1669 /* Allocate the inode */
1670 error = ERR_PTR(-ENOMEM);
1671 inode = new_inode(dir->i_sb);
1672 if (!inode)
1673 goto out;
1674
1675 /* Initialize the inode */
1676 ei = PROC_I(inode);
1677 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001678
1679 /*
1680 * grab the reference to the task.
1681 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001682 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001683 if (!ei->pid)
1684 goto out_iput;
1685
1686 inode->i_uid = 0;
1687 inode->i_gid = 0;
1688 inode->i_mode = p->mode;
1689 if (S_ISDIR(inode->i_mode))
1690 inode->i_nlink = 2;
1691 if (S_ISLNK(inode->i_mode))
1692 inode->i_size = 64;
1693 if (p->iop)
1694 inode->i_op = p->iop;
1695 if (p->fop)
1696 inode->i_fop = p->fop;
1697 ei->op = p->op;
1698 dentry->d_op = &proc_base_dentry_operations;
1699 d_add(dentry, inode);
1700 error = NULL;
1701out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001702 return error;
1703out_iput:
1704 iput(inode);
1705 goto out;
1706}
1707
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001708static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1709{
1710 struct dentry *error;
1711 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001712 struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001713
1714 error = ERR_PTR(-ENOENT);
1715
1716 if (!task)
1717 goto out_no_task;
1718
1719 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001720 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1721 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001722 if (p->len != dentry->d_name.len)
1723 continue;
1724 if (!memcmp(dentry->d_name.name, p->name, p->len))
1725 break;
1726 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001727 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001728 goto out;
1729
1730 error = proc_base_instantiate(dir, dentry, task, p);
1731
1732out:
1733 put_task_struct(task);
1734out_no_task:
1735 return error;
1736}
1737
Eric W. Biederman61a28782006-10-02 02:18:49 -07001738static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1739 struct task_struct *task, struct pid_entry *p)
1740{
1741 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1742 proc_base_instantiate, task, p);
1743}
1744
Eric W. Biederman801199c2006-10-02 02:18:48 -07001745/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001746 * Thread groups
1747 */
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001748static struct file_operations proc_task_operations;
1749static struct inode_operations proc_task_inode_operations;
1750
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001751static struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001752 DIR("task", S_IRUGO|S_IXUGO, task),
1753 DIR("fd", S_IRUSR|S_IXUSR, fd),
1754 INF("environ", S_IRUSR, pid_environ),
1755 INF("auxv", S_IRUSR, pid_auxv),
1756 INF("status", S_IRUGO, pid_status),
1757 INF("cmdline", S_IRUGO, pid_cmdline),
1758 INF("stat", S_IRUGO, tgid_stat),
1759 INF("statm", S_IRUGO, pid_statm),
1760 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001762 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001763#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001764 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001765#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07001766 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001767#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001768 LNK("cwd", cwd),
1769 LNK("root", root),
1770 LNK("exe", exe),
1771 REG("mounts", S_IRUGO, mounts),
1772 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001773#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07001774 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001775#endif
1776#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001777 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778#endif
1779#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001780 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781#endif
1782#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001783 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001784#endif
1785#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001786 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001787#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001788 INF("oom_score", S_IRUGO, oom_score),
1789 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001790#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07001791 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001792#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001793};
1794
1795static int proc_tgid_base_readdir(struct file * filp,
1796 void * dirent, filldir_t filldir)
1797{
1798 return proc_pident_readdir(filp,dirent,filldir,
1799 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1800}
1801
1802static struct file_operations proc_tgid_base_operations = {
1803 .read = generic_read_dir,
1804 .readdir = proc_tgid_base_readdir,
1805};
1806
1807static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001808 return proc_pident_lookup(dir, dentry,
1809 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001810}
1811
1812static struct inode_operations proc_tgid_base_inode_operations = {
1813 .lookup = proc_tgid_base_lookup,
1814 .getattr = pid_getattr,
1815 .setattr = proc_setattr,
1816};
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001819 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001821 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001823 * Looks in the dcache for
1824 * /proc/@pid
1825 * /proc/@tgid/task/@pid
1826 * if either directory is present flushes it and all of it'ts children
1827 * from the dcache.
1828 *
1829 * It is safe and reasonable to cache /proc entries for a task until
1830 * that task exits. After that they just clog up the dcache with
1831 * useless entries, possibly causing useful dcache entries to be
1832 * flushed instead. This routine is proved to flush those useless
1833 * dcache entries at process exit time.
1834 *
1835 * NOTE: This routine is just an optimization so it does not guarantee
1836 * that no dcache entries will exist at process exit time it
1837 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001839void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001841 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001842 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001843 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Eric W. Biederman48e64842006-06-26 00:25:48 -07001845 name.name = buf;
1846 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1847 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1848 if (dentry) {
1849 shrink_dcache_parent(dentry);
1850 d_drop(dentry);
1851 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Eric W. Biederman48e64842006-06-26 00:25:48 -07001854 if (thread_group_leader(task))
1855 goto out;
1856
1857 name.name = buf;
1858 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1859 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1860 if (!leader)
1861 goto out;
1862
1863 name.name = "task";
1864 name.len = strlen(name.name);
1865 dir = d_hash_and_lookup(leader, &name);
1866 if (!dir)
1867 goto out_put_leader;
1868
1869 name.name = buf;
1870 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1871 dentry = d_hash_and_lookup(dir, &name);
1872 if (dentry) {
1873 shrink_dcache_parent(dentry);
1874 d_drop(dentry);
1875 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001877
1878 dput(dir);
1879out_put_leader:
1880 dput(leader);
1881out:
1882 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001885struct dentry *proc_pid_instantiate(struct inode *dir,
1886 struct dentry * dentry, struct task_struct *task, void *ptr)
1887{
1888 struct dentry *error = ERR_PTR(-ENOENT);
1889 struct inode *inode;
1890
Eric W. Biederman61a28782006-10-02 02:18:49 -07001891 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001892 if (!inode)
1893 goto out;
1894
1895 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1896 inode->i_op = &proc_tgid_base_inode_operations;
1897 inode->i_fop = &proc_tgid_base_operations;
1898 inode->i_flags|=S_IMMUTABLE;
1899 inode->i_nlink = 4;
1900#ifdef CONFIG_SECURITY
1901 inode->i_nlink += 1;
1902#endif
1903
1904 dentry->d_op = &pid_dentry_operations;
1905
1906 d_add(dentry, inode);
1907 /* Close the race of the process dying before we return the dentry */
1908 if (pid_revalidate(dentry, NULL))
1909 error = NULL;
1910out:
1911 return error;
1912}
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1915{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001916 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Eric W. Biederman801199c2006-10-02 02:18:48 -07001920 result = proc_base_lookup(dir, dentry);
1921 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1922 goto out;
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 tgid = name_to_int(dentry);
1925 if (tgid == ~0U)
1926 goto out;
1927
Eric W. Biedermande758732006-06-26 00:25:51 -07001928 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 task = find_task_by_pid(tgid);
1930 if (task)
1931 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07001932 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (!task)
1934 goto out;
1935
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001936 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07001937 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001939 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001943 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001946static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001948 struct task_struct *task;
1949 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001951 rcu_read_lock();
1952retry:
1953 task = NULL;
1954 pid = find_ge_pid(tgid);
1955 if (pid) {
1956 tgid = pid->nr + 1;
1957 task = pid_task(pid, PIDTYPE_PID);
1958 /* What we to know is if the pid we have find is the
1959 * pid of a thread_group_leader. Testing for task
1960 * being a thread_group_leader is the obvious thing
1961 * todo but there is a window when it fails, due to
1962 * the pid transfer logic in de_thread.
1963 *
1964 * So we perform the straight forward test of seeing
1965 * if the pid we have found is the pid of a thread
1966 * group leader, and don't worry if the task we have
1967 * found doesn't happen to be a thread group leader.
1968 * As we don't care in the case of readdir.
1969 */
1970 if (!task || !has_group_leader_pid(task))
1971 goto retry;
1972 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07001974 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001975 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001978#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Eric W. Biederman61a28782006-10-02 02:18:49 -07001980static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1981 struct task_struct *task, int tgid)
1982{
1983 char name[PROC_NUMBUF];
1984 int len = snprintf(name, sizeof(name), "%d", tgid);
1985 return proc_fill_cache(filp, dirent, filldir, name, len,
1986 proc_pid_instantiate, task, NULL);
1987}
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989/* for the /proc/ directory itself, after non-process stuff has been done */
1990int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1991{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001993 struct task_struct *reaper = get_proc_task(filp->f_dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001994 struct task_struct *task;
1995 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Eric W. Biederman61a28782006-10-02 02:18:49 -07001997 if (!reaper)
1998 goto out_no_task;
1999
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002000 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric W. Biederman801199c2006-10-02 02:18:48 -07002001 struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002002 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002003 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002006 tgid = filp->f_pos - TGID_OFFSET;
2007 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002008 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002009 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002010 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002011 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002012 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002013 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002014 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002017 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2018out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002019 put_task_struct(reaper);
2020out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return 0;
2022}
2023
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002024/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002025 * Tasks
2026 */
2027static struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002028 DIR("fd", S_IRUSR|S_IXUSR, fd),
2029 INF("environ", S_IRUSR, pid_environ),
2030 INF("auxv", S_IRUSR, pid_auxv),
2031 INF("status", S_IRUGO, pid_status),
2032 INF("cmdline", S_IRUGO, pid_cmdline),
2033 INF("stat", S_IRUGO, tid_stat),
2034 INF("statm", S_IRUGO, pid_statm),
2035 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002036#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002037 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002038#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002039 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002040#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002041 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002042#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002043 LNK("cwd", cwd),
2044 LNK("root", root),
2045 LNK("exe", exe),
2046 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002047#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07002048 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002049#endif
2050#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002051 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002052#endif
2053#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002054 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002055#endif
2056#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002057 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002058#endif
2059#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002060 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002061#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002062 INF("oom_score", S_IRUGO, oom_score),
2063 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002064#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002065 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002066#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002067};
2068
2069static int proc_tid_base_readdir(struct file * filp,
2070 void * dirent, filldir_t filldir)
2071{
2072 return proc_pident_readdir(filp,dirent,filldir,
2073 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2074}
2075
2076static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002077 return proc_pident_lookup(dir, dentry,
2078 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002079}
2080
2081static struct file_operations proc_tid_base_operations = {
2082 .read = generic_read_dir,
2083 .readdir = proc_tid_base_readdir,
2084};
2085
2086static struct inode_operations proc_tid_base_inode_operations = {
2087 .lookup = proc_tid_base_lookup,
2088 .getattr = pid_getattr,
2089 .setattr = proc_setattr,
2090};
2091
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002092static struct dentry *proc_task_instantiate(struct inode *dir,
2093 struct dentry *dentry, struct task_struct *task, void *ptr)
2094{
2095 struct dentry *error = ERR_PTR(-ENOENT);
2096 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002097 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002098
2099 if (!inode)
2100 goto out;
2101 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2102 inode->i_op = &proc_tid_base_inode_operations;
2103 inode->i_fop = &proc_tid_base_operations;
2104 inode->i_flags|=S_IMMUTABLE;
2105 inode->i_nlink = 3;
2106#ifdef CONFIG_SECURITY
2107 inode->i_nlink += 1;
2108#endif
2109
2110 dentry->d_op = &pid_dentry_operations;
2111
2112 d_add(dentry, inode);
2113 /* Close the race of the process dying before we return the dentry */
2114 if (pid_revalidate(dentry, NULL))
2115 error = NULL;
2116out:
2117 return error;
2118}
2119
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002120static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2121{
2122 struct dentry *result = ERR_PTR(-ENOENT);
2123 struct task_struct *task;
2124 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002125 unsigned tid;
2126
2127 if (!leader)
2128 goto out_no_task;
2129
2130 tid = name_to_int(dentry);
2131 if (tid == ~0U)
2132 goto out;
2133
2134 rcu_read_lock();
2135 task = find_task_by_pid(tid);
2136 if (task)
2137 get_task_struct(task);
2138 rcu_read_unlock();
2139 if (!task)
2140 goto out;
2141 if (leader->tgid != task->tgid)
2142 goto out_drop_task;
2143
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002144 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002145out_drop_task:
2146 put_task_struct(task);
2147out:
2148 put_task_struct(leader);
2149out_no_task:
2150 return result;
2151}
2152
2153/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002154 * Find the first tid of a thread group to return to user space.
2155 *
2156 * Usually this is just the thread group leader, but if the users
2157 * buffer was too small or there was a seek into the middle of the
2158 * directory we have more work todo.
2159 *
2160 * In the case of a short read we start with find_task_by_pid.
2161 *
2162 * In the case of a seek we start with the leader and walk nr
2163 * threads past it.
2164 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002165static struct task_struct *first_tid(struct task_struct *leader,
2166 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002167{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002168 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002169
Eric W. Biedermancc288732006-06-26 00:26:01 -07002170 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002171 /* Attempt to start with the pid of a thread */
2172 if (tid && (nr > 0)) {
2173 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002174 if (pos && (pos->group_leader == leader))
2175 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002176 }
2177
2178 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002179 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002180 if (nr && nr >= get_nr_threads(leader))
2181 goto out;
2182
2183 /* If we haven't found our starting place yet start
2184 * with the leader and walk nr threads forward.
2185 */
2186 for (pos = leader; nr > 0; --nr) {
2187 pos = next_thread(pos);
2188 if (pos == leader) {
2189 pos = NULL;
2190 goto out;
2191 }
2192 }
2193found:
2194 get_task_struct(pos);
2195out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002196 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002197 return pos;
2198}
2199
2200/*
2201 * Find the next thread in the thread list.
2202 * Return NULL if there is an error or no next thread.
2203 *
2204 * The reference to the input task_struct is released.
2205 */
2206static struct task_struct *next_tid(struct task_struct *start)
2207{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002208 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002209 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002210 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002211 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002212 if (thread_group_leader(pos))
2213 pos = NULL;
2214 else
2215 get_task_struct(pos);
2216 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002217 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002218 put_task_struct(start);
2219 return pos;
2220}
2221
Eric W. Biederman61a28782006-10-02 02:18:49 -07002222static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2223 struct task_struct *task, int tid)
2224{
2225 char name[PROC_NUMBUF];
2226 int len = snprintf(name, sizeof(name), "%d", tid);
2227 return proc_fill_cache(filp, dirent, filldir, name, len,
2228 proc_task_instantiate, task, NULL);
2229}
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231/* for the /proc/TGID/task/ directories */
2232static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 struct dentry *dentry = filp->f_dentry;
2235 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002236 struct task_struct *leader = get_proc_task(inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002237 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int retval = -ENOENT;
2239 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002240 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2242
Eric W. Biederman99f89552006-06-26 00:25:55 -07002243 if (!leader)
2244 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 retval = 0;
2246
2247 switch (pos) {
2248 case 0:
2249 ino = inode->i_ino;
2250 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2251 goto out;
2252 pos++;
2253 /* fall through */
2254 case 1:
2255 ino = parent_ino(dentry);
2256 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2257 goto out;
2258 pos++;
2259 /* fall through */
2260 }
2261
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002262 /* f_version caches the tgid value that the last readdir call couldn't
2263 * return. lseek aka telldir automagically resets f_version to 0.
2264 */
2265 tid = filp->f_version;
2266 filp->f_version = 0;
2267 for (task = first_tid(leader, tid, pos - 2);
2268 task;
2269 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002270 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002271 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002272 /* returning this tgid failed, save it as the first
2273 * pid for the next readir call */
2274 filp->f_version = tid;
2275 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
2279out:
2280 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002281 put_task_struct(leader);
2282out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return retval;
2284}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002285
2286static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2287{
2288 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002289 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002290 generic_fillattr(inode, stat);
2291
Eric W. Biederman99f89552006-06-26 00:25:55 -07002292 if (p) {
2293 rcu_read_lock();
2294 stat->nlink += get_nr_threads(p);
2295 rcu_read_unlock();
2296 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002297 }
2298
2299 return 0;
2300}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002301
2302static struct inode_operations proc_task_inode_operations = {
2303 .lookup = proc_task_lookup,
2304 .getattr = proc_task_getattr,
2305 .setattr = proc_setattr,
2306};
2307
2308static struct file_operations proc_task_operations = {
2309 .read = generic_read_dir,
2310 .readdir = proc_task_readdir,
2311};