blob: 795319c54f7283430f2ec28ed346d39af6291e34 [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);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800445 if (task->nsproxy)
446 namespace = task->nsproxy->namespace;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700447 if (namespace)
448 get_namespace(namespace);
449 task_unlock(task);
450 put_task_struct(task);
451 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500452
453 if (namespace)
454 m->private = namespace;
455 else {
456 seq_release(inode, file);
457 ret = -EINVAL;
458 }
459 }
460 return ret;
461}
462
463static struct file_operations proc_mountstats_operations = {
464 .open = mountstats_open,
465 .read = seq_read,
466 .llseek = seq_lseek,
467 .release = mounts_release,
468};
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
471
472static ssize_t proc_info_read(struct file * file, char __user * buf,
473 size_t count, loff_t *ppos)
474{
475 struct inode * inode = file->f_dentry->d_inode;
476 unsigned long page;
477 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700478 struct task_struct *task = get_proc_task(inode);
479
480 length = -ESRCH;
481 if (!task)
482 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (count > PROC_BLOCK_SIZE)
485 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700486
487 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 length = PROC_I(inode)->op.proc_read(task, (char*)page);
492
493 if (length >= 0)
494 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
495 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700496out:
497 put_task_struct(task);
498out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return length;
500}
501
502static struct file_operations proc_info_file_operations = {
503 .read = proc_info_read,
504};
505
506static int mem_open(struct inode* inode, struct file* file)
507{
508 file->private_data = (void*)((long)current->self_exec_id);
509 return 0;
510}
511
512static ssize_t mem_read(struct file * file, char __user * buf,
513 size_t count, loff_t *ppos)
514{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700515 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 char *page;
517 unsigned long src = *ppos;
518 int ret = -ESRCH;
519 struct mm_struct *mm;
520
Eric W. Biederman99f89552006-06-26 00:25:55 -0700521 if (!task)
522 goto out_no_task;
523
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700524 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto out;
526
527 ret = -ENOMEM;
528 page = (char *)__get_free_page(GFP_USER);
529 if (!page)
530 goto out;
531
532 ret = 0;
533
534 mm = get_task_mm(task);
535 if (!mm)
536 goto out_free;
537
538 ret = -EIO;
539
540 if (file->private_data != (void*)((long)current->self_exec_id))
541 goto out_put;
542
543 ret = 0;
544
545 while (count > 0) {
546 int this_len, retval;
547
548 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
549 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700550 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!ret)
552 ret = -EIO;
553 break;
554 }
555
556 if (copy_to_user(buf, page, retval)) {
557 ret = -EFAULT;
558 break;
559 }
560
561 ret += retval;
562 src += retval;
563 buf += retval;
564 count -= retval;
565 }
566 *ppos = src;
567
568out_put:
569 mmput(mm);
570out_free:
571 free_page((unsigned long) page);
572out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700573 put_task_struct(task);
574out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return ret;
576}
577
578#define mem_write NULL
579
580#ifndef mem_write
581/* This is a security hazard */
582static ssize_t mem_write(struct file * file, const char * buf,
583 size_t count, loff_t *ppos)
584{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700585 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 char *page;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700587 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 unsigned long dst = *ppos;
589
Eric W. Biederman99f89552006-06-26 00:25:55 -0700590 copied = -ESRCH;
591 if (!task)
592 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Eric W. Biederman99f89552006-06-26 00:25:55 -0700594 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
595 goto out;
596
597 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 page = (char *)__get_free_page(GFP_USER);
599 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700602 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 while (count > 0) {
604 int this_len, retval;
605
606 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
607 if (copy_from_user(page, buf, this_len)) {
608 copied = -EFAULT;
609 break;
610 }
611 retval = access_process_vm(task, dst, page, this_len, 1);
612 if (!retval) {
613 if (!copied)
614 copied = -EIO;
615 break;
616 }
617 copied += retval;
618 buf += retval;
619 dst += retval;
620 count -= retval;
621 }
622 *ppos = dst;
623 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700624out:
625 put_task_struct(task);
626out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return copied;
628}
629#endif
630
631static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
632{
633 switch (orig) {
634 case 0:
635 file->f_pos = offset;
636 break;
637 case 1:
638 file->f_pos += offset;
639 break;
640 default:
641 return -EINVAL;
642 }
643 force_successful_syscall_return();
644 return file->f_pos;
645}
646
647static struct file_operations proc_mem_operations = {
648 .llseek = mem_lseek,
649 .read = mem_read,
650 .write = mem_write,
651 .open = mem_open,
652};
653
654static ssize_t oom_adjust_read(struct file *file, char __user *buf,
655 size_t count, loff_t *ppos)
656{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700657 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700658 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700660 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 loff_t __ppos = *ppos;
662
Eric W. Biederman99f89552006-06-26 00:25:55 -0700663 if (!task)
664 return -ESRCH;
665 oom_adjust = task->oomkilladj;
666 put_task_struct(task);
667
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700668 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (__ppos >= len)
670 return 0;
671 if (count > len-__ppos)
672 count = len-__ppos;
673 if (copy_to_user(buf, buffer + __ppos, count))
674 return -EFAULT;
675 *ppos = __ppos + count;
676 return count;
677}
678
679static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
680 size_t count, loff_t *ppos)
681{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700682 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700683 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int oom_adjust;
685
686 if (!capable(CAP_SYS_RESOURCE))
687 return -EPERM;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700688 memset(buffer, 0, sizeof(buffer));
689 if (count > sizeof(buffer) - 1)
690 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (copy_from_user(buffer, buf, count))
692 return -EFAULT;
693 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700694 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
695 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EINVAL;
697 if (*end == '\n')
698 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700699 task = get_proc_task(file->f_dentry->d_inode);
700 if (!task)
701 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700703 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (end - buffer == 0)
705 return -EIO;
706 return end - buffer;
707}
708
709static struct file_operations proc_oom_adjust_operations = {
710 .read = oom_adjust_read,
711 .write = oom_adjust_write,
712};
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714#ifdef CONFIG_AUDITSYSCALL
715#define TMPBUFLEN 21
716static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
717 size_t count, loff_t *ppos)
718{
719 struct inode * inode = file->f_dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700720 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ssize_t length;
722 char tmpbuf[TMPBUFLEN];
723
Eric W. Biederman99f89552006-06-26 00:25:55 -0700724 if (!task)
725 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
727 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700728 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
730}
731
732static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
733 size_t count, loff_t *ppos)
734{
735 struct inode * inode = file->f_dentry->d_inode;
736 char *page, *tmp;
737 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 uid_t loginuid;
739
740 if (!capable(CAP_AUDIT_CONTROL))
741 return -EPERM;
742
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700743 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -EPERM;
745
Al Viroe0182902006-05-18 08:28:02 -0400746 if (count >= PAGE_SIZE)
747 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (*ppos != 0) {
750 /* No partial writes. */
751 return -EINVAL;
752 }
753 page = (char*)__get_free_page(GFP_USER);
754 if (!page)
755 return -ENOMEM;
756 length = -EFAULT;
757 if (copy_from_user(page, buf, count))
758 goto out_free_page;
759
Al Viroe0182902006-05-18 08:28:02 -0400760 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 loginuid = simple_strtoul(page, &tmp, 10);
762 if (tmp == page) {
763 length = -EINVAL;
764 goto out_free_page;
765
766 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700767 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (likely(length == 0))
769 length = count;
770
771out_free_page:
772 free_page((unsigned long) page);
773 return length;
774}
775
776static struct file_operations proc_loginuid_operations = {
777 .read = proc_loginuid_read,
778 .write = proc_loginuid_write,
779};
780#endif
781
782#ifdef CONFIG_SECCOMP
783static ssize_t seccomp_read(struct file *file, char __user *buf,
784 size_t count, loff_t *ppos)
785{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700786 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 char __buf[20];
788 loff_t __ppos = *ppos;
789 size_t len;
790
Eric W. Biederman99f89552006-06-26 00:25:55 -0700791 if (!tsk)
792 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* no need to print the trailing zero, so use only len */
794 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700795 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (__ppos >= len)
797 return 0;
798 if (count > len - __ppos)
799 count = len - __ppos;
800 if (copy_to_user(buf, __buf + __ppos, count))
801 return -EFAULT;
802 *ppos = __ppos + count;
803 return count;
804}
805
806static ssize_t seccomp_write(struct file *file, const char __user *buf,
807 size_t count, loff_t *ppos)
808{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700809 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 char __buf[20], *end;
811 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700812 ssize_t result;
813
814 result = -ESRCH;
815 if (!tsk)
816 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700819 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Eric W. Biederman99f89552006-06-26 00:25:55 -0700823 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 memset(__buf, 0, sizeof(__buf));
825 count = min(count, sizeof(__buf) - 1);
826 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700827 goto out;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 seccomp_mode = simple_strtoul(__buf, &end, 0);
830 if (*end == '\n')
831 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700832 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
834 tsk->seccomp.mode = seccomp_mode;
835 set_tsk_thread_flag(tsk, TIF_SECCOMP);
836 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700837 goto out;
838 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700840 goto out;
841 result = end - __buf;
842out:
843 put_task_struct(tsk);
844out_no_task:
845 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848static struct file_operations proc_seccomp_operations = {
849 .read = seccomp_read,
850 .write = seccomp_write,
851};
852#endif /* CONFIG_SECCOMP */
853
Al Viro008b1502005-08-20 00:17:39 +0100854static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct inode *inode = dentry->d_inode;
857 int error = -EACCES;
858
859 /* We don't need a base pointer in the /proc filesystem */
860 path_release(nd);
861
Eric W. Biederman778c1142006-06-26 00:25:58 -0700862 /* Are we allowed to snoop on the tasks file descriptors? */
863 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
867 nd->last_type = LAST_BIND;
868out:
Al Viro008b1502005-08-20 00:17:39 +0100869 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
873 char __user *buffer, int buflen)
874{
875 struct inode * inode;
876 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
877 int len;
878
879 if (!tmp)
880 return -ENOMEM;
881
882 inode = dentry->d_inode;
883 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
884 len = PTR_ERR(path);
885 if (IS_ERR(path))
886 goto out;
887 len = tmp + PAGE_SIZE - 1 - path;
888
889 if (len > buflen)
890 len = buflen;
891 if (copy_to_user(buffer, path, len))
892 len = -EFAULT;
893 out:
894 free_page((unsigned long)tmp);
895 return len;
896}
897
898static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
899{
900 int error = -EACCES;
901 struct inode *inode = dentry->d_inode;
902 struct dentry *de;
903 struct vfsmount *mnt = NULL;
904
Eric W. Biederman778c1142006-06-26 00:25:58 -0700905 /* Are we allowed to snoop on the tasks file descriptors? */
906 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
910 if (error)
911 goto out;
912
913 error = do_proc_readlink(de, mnt, buffer, buflen);
914 dput(de);
915 mntput(mnt);
916out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return error;
918}
919
920static struct inode_operations proc_pid_link_inode_operations = {
921 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700922 .follow_link = proc_pid_follow_link,
923 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924};
925
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700926
927/* building an inode */
928
929static int task_dumpable(struct task_struct *task)
930{
931 int dumpable = 0;
932 struct mm_struct *mm;
933
934 task_lock(task);
935 mm = task->mm;
936 if (mm)
937 dumpable = mm->dumpable;
938 task_unlock(task);
939 if(dumpable == 1)
940 return 1;
941 return 0;
942}
943
944
Eric W. Biederman61a28782006-10-02 02:18:49 -0700945static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700946{
947 struct inode * inode;
948 struct proc_inode *ei;
949
950 /* We need a new inode */
951
952 inode = new_inode(sb);
953 if (!inode)
954 goto out;
955
956 /* Common stuff */
957 ei = PROC_I(inode);
958 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700959 inode->i_op = &proc_def_inode_operations;
960
961 /*
962 * grab the reference to task.
963 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -0700964 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700965 if (!ei->pid)
966 goto out_unlock;
967
968 inode->i_uid = 0;
969 inode->i_gid = 0;
970 if (task_dumpable(task)) {
971 inode->i_uid = task->euid;
972 inode->i_gid = task->egid;
973 }
974 security_task_to_inode(task, inode);
975
976out:
977 return inode;
978
979out_unlock:
980 iput(inode);
981 return NULL;
982}
983
984static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
985{
986 struct inode *inode = dentry->d_inode;
987 struct task_struct *task;
988 generic_fillattr(inode, stat);
989
990 rcu_read_lock();
991 stat->uid = 0;
992 stat->gid = 0;
993 task = pid_task(proc_pid(inode), PIDTYPE_PID);
994 if (task) {
995 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
996 task_dumpable(task)) {
997 stat->uid = task->euid;
998 stat->gid = task->egid;
999 }
1000 }
1001 rcu_read_unlock();
1002 return 0;
1003}
1004
1005/* dentry stuff */
1006
1007/*
1008 * Exceptional case: normally we are not allowed to unhash a busy
1009 * directory. In this case, however, we can do it - no aliasing problems
1010 * due to the way we treat inodes.
1011 *
1012 * Rewrite the inode's ownerships here because the owning task may have
1013 * performed a setuid(), etc.
1014 *
1015 * Before the /proc/pid/status file was created the only way to read
1016 * the effective uid of a /process was to stat /proc/pid. Reading
1017 * /proc/pid/status is slow enough that procps and other packages
1018 * kept stating /proc/pid. To keep the rules in /proc simple I have
1019 * made this apply to all per process world readable and executable
1020 * directories.
1021 */
1022static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1023{
1024 struct inode *inode = dentry->d_inode;
1025 struct task_struct *task = get_proc_task(inode);
1026 if (task) {
1027 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1028 task_dumpable(task)) {
1029 inode->i_uid = task->euid;
1030 inode->i_gid = task->egid;
1031 } else {
1032 inode->i_uid = 0;
1033 inode->i_gid = 0;
1034 }
1035 inode->i_mode &= ~(S_ISUID | S_ISGID);
1036 security_task_to_inode(task, inode);
1037 put_task_struct(task);
1038 return 1;
1039 }
1040 d_drop(dentry);
1041 return 0;
1042}
1043
1044static int pid_delete_dentry(struct dentry * dentry)
1045{
1046 /* Is the task we represent dead?
1047 * If so, then don't put the dentry on the lru list,
1048 * kill it immediately.
1049 */
1050 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1051}
1052
1053static struct dentry_operations pid_dentry_operations =
1054{
1055 .d_revalidate = pid_revalidate,
1056 .d_delete = pid_delete_dentry,
1057};
1058
1059/* Lookups */
1060
Eric W. Biederman61a28782006-10-02 02:18:49 -07001061typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1062
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001063/*
1064 * Fill a directory entry.
1065 *
1066 * If possible create the dcache entry and derive our inode number and
1067 * file type from dcache entry.
1068 *
1069 * Since all of the proc inode numbers are dynamically generated, the inode
1070 * numbers do not exist until the inode is cache. This means creating the
1071 * the dcache entry in readdir is necessary to keep the inode numbers
1072 * reported by readdir in sync with the inode numbers reported
1073 * by stat.
1074 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001075static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1076 char *name, int len,
1077 instantiate_t instantiate, struct task_struct *task, void *ptr)
1078{
1079 struct dentry *child, *dir = filp->f_dentry;
1080 struct inode *inode;
1081 struct qstr qname;
1082 ino_t ino = 0;
1083 unsigned type = DT_UNKNOWN;
1084
1085 qname.name = name;
1086 qname.len = len;
1087 qname.hash = full_name_hash(name, len);
1088
1089 child = d_lookup(dir, &qname);
1090 if (!child) {
1091 struct dentry *new;
1092 new = d_alloc(dir, &qname);
1093 if (new) {
1094 child = instantiate(dir->d_inode, new, task, ptr);
1095 if (child)
1096 dput(new);
1097 else
1098 child = new;
1099 }
1100 }
1101 if (!child || IS_ERR(child) || !child->d_inode)
1102 goto end_instantiate;
1103 inode = child->d_inode;
1104 if (inode) {
1105 ino = inode->i_ino;
1106 type = inode->i_mode >> 12;
1107 }
1108 dput(child);
1109end_instantiate:
1110 if (!ino)
1111 ino = find_inode_number(dir, &qname);
1112 if (!ino)
1113 ino = 1;
1114 return filldir(dirent, name, len, filp->f_pos, ino, type);
1115}
1116
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001117static unsigned name_to_int(struct dentry *dentry)
1118{
1119 const char *name = dentry->d_name.name;
1120 int len = dentry->d_name.len;
1121 unsigned n = 0;
1122
1123 if (len > 1 && *name == '0')
1124 goto out;
1125 while (len-- > 0) {
1126 unsigned c = *name++ - '0';
1127 if (c > 9)
1128 goto out;
1129 if (n >= (~0U-9)/10)
1130 goto out;
1131 n *= 10;
1132 n += c;
1133 }
1134 return n;
1135out:
1136 return ~0U;
1137}
1138
1139static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1140{
1141 struct task_struct *task = get_proc_task(inode);
1142 struct files_struct *files = NULL;
1143 struct file *file;
1144 int fd = proc_fd(inode);
1145
1146 if (task) {
1147 files = get_files_struct(task);
1148 put_task_struct(task);
1149 }
1150 if (files) {
1151 /*
1152 * We are not taking a ref to the file structure, so we must
1153 * hold ->file_lock.
1154 */
1155 spin_lock(&files->file_lock);
1156 file = fcheck_files(files, fd);
1157 if (file) {
1158 *mnt = mntget(file->f_vfsmnt);
1159 *dentry = dget(file->f_dentry);
1160 spin_unlock(&files->file_lock);
1161 put_files_struct(files);
1162 return 0;
1163 }
1164 spin_unlock(&files->file_lock);
1165 put_files_struct(files);
1166 }
1167 return -ENOENT;
1168}
1169
1170static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1171{
1172 struct inode *inode = dentry->d_inode;
1173 struct task_struct *task = get_proc_task(inode);
1174 int fd = proc_fd(inode);
1175 struct files_struct *files;
1176
1177 if (task) {
1178 files = get_files_struct(task);
1179 if (files) {
1180 rcu_read_lock();
1181 if (fcheck_files(files, fd)) {
1182 rcu_read_unlock();
1183 put_files_struct(files);
1184 if (task_dumpable(task)) {
1185 inode->i_uid = task->euid;
1186 inode->i_gid = task->egid;
1187 } else {
1188 inode->i_uid = 0;
1189 inode->i_gid = 0;
1190 }
1191 inode->i_mode &= ~(S_ISUID | S_ISGID);
1192 security_task_to_inode(task, inode);
1193 put_task_struct(task);
1194 return 1;
1195 }
1196 rcu_read_unlock();
1197 put_files_struct(files);
1198 }
1199 put_task_struct(task);
1200 }
1201 d_drop(dentry);
1202 return 0;
1203}
1204
1205static struct dentry_operations tid_fd_dentry_operations =
1206{
1207 .d_revalidate = tid_fd_revalidate,
1208 .d_delete = pid_delete_dentry,
1209};
1210
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001211static struct dentry *proc_fd_instantiate(struct inode *dir,
1212 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001213{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001214 unsigned fd = *(unsigned *)ptr;
1215 struct file *file;
1216 struct files_struct *files;
1217 struct inode *inode;
1218 struct proc_inode *ei;
1219 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001220
Eric W. Biederman61a28782006-10-02 02:18:49 -07001221 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001222 if (!inode)
1223 goto out;
1224 ei = PROC_I(inode);
1225 ei->fd = fd;
1226 files = get_files_struct(task);
1227 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001228 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001229 inode->i_mode = S_IFLNK;
1230
1231 /*
1232 * We are not taking a ref to the file structure, so we must
1233 * hold ->file_lock.
1234 */
1235 spin_lock(&files->file_lock);
1236 file = fcheck_files(files, fd);
1237 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001238 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001239 if (file->f_mode & 1)
1240 inode->i_mode |= S_IRUSR | S_IXUSR;
1241 if (file->f_mode & 2)
1242 inode->i_mode |= S_IWUSR | S_IXUSR;
1243 spin_unlock(&files->file_lock);
1244 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001245
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001246 inode->i_op = &proc_pid_link_inode_operations;
1247 inode->i_size = 64;
1248 ei->op.proc_get_link = proc_fd_link;
1249 dentry->d_op = &tid_fd_dentry_operations;
1250 d_add(dentry, inode);
1251 /* Close the race of the process dying before we return the dentry */
1252 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001253 error = NULL;
1254
1255 out:
1256 return error;
1257out_unlock:
1258 spin_unlock(&files->file_lock);
1259 put_files_struct(files);
1260out_iput:
1261 iput(inode);
1262 goto out;
1263}
1264
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001265static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1266{
1267 struct task_struct *task = get_proc_task(dir);
1268 unsigned fd = name_to_int(dentry);
1269 struct dentry *result = ERR_PTR(-ENOENT);
1270
1271 if (!task)
1272 goto out_no_task;
1273 if (fd == ~0U)
1274 goto out;
1275
1276 result = proc_fd_instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001277out:
1278 put_task_struct(task);
1279out_no_task:
1280 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001281}
1282
Eric W. Biederman61a28782006-10-02 02:18:49 -07001283static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1284 struct task_struct *task, int fd)
1285{
1286 char name[PROC_NUMBUF];
1287 int len = snprintf(name, sizeof(name), "%d", fd);
1288 return proc_fill_cache(filp, dirent, filldir, name, len,
1289 proc_fd_instantiate, task, &fd);
1290}
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1293{
Eric W. Biederman56347082006-06-26 00:25:40 -07001294 struct dentry *dentry = filp->f_dentry;
1295 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001296 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 unsigned int fd, tid, ino;
1298 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001300 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001303 if (!p)
1304 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 retval = 0;
1306 tid = p->pid;
1307
1308 fd = filp->f_pos;
1309 switch (fd) {
1310 case 0:
1311 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1312 goto out;
1313 filp->f_pos++;
1314 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001315 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1317 goto out;
1318 filp->f_pos++;
1319 default:
1320 files = get_files_struct(p);
1321 if (!files)
1322 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001323 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001324 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001326 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 fd++, filp->f_pos++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 if (!fcheck_files(files, fd))
1330 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001331 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Eric W. Biederman61a28782006-10-02 02:18:49 -07001333 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001334 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
1336 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001337 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001339 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 put_files_struct(files);
1341 }
1342out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001343 put_task_struct(p);
1344out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return retval;
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348static struct file_operations proc_fd_operations = {
1349 .read = generic_read_dir,
1350 .readdir = proc_readfd,
1351};
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353/*
1354 * proc directories can do almost nothing..
1355 */
1356static struct inode_operations proc_fd_inode_operations = {
1357 .lookup = proc_lookupfd,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001358 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359};
1360
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001361static struct dentry *proc_pident_instantiate(struct inode *dir,
1362 struct dentry *dentry, struct task_struct *task, void *ptr)
1363{
1364 struct pid_entry *p = ptr;
1365 struct inode *inode;
1366 struct proc_inode *ei;
1367 struct dentry *error = ERR_PTR(-EINVAL);
1368
Eric W. Biederman61a28782006-10-02 02:18:49 -07001369 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001370 if (!inode)
1371 goto out;
1372
1373 ei = PROC_I(inode);
1374 inode->i_mode = p->mode;
1375 if (S_ISDIR(inode->i_mode))
1376 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1377 if (p->iop)
1378 inode->i_op = p->iop;
1379 if (p->fop)
1380 inode->i_fop = p->fop;
1381 ei->op = p->op;
1382 dentry->d_op = &pid_dentry_operations;
1383 d_add(dentry, inode);
1384 /* Close the race of the process dying before we return the dentry */
1385 if (pid_revalidate(dentry, NULL))
1386 error = NULL;
1387out:
1388 return error;
1389}
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391static struct dentry *proc_pident_lookup(struct inode *dir,
1392 struct dentry *dentry,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001393 struct pid_entry *ents,
1394 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001397 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001398 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001399 struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001401 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 inode = NULL;
1403
Eric W. Biederman99f89552006-06-26 00:25:55 -07001404 if (!task)
1405 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001407 /*
1408 * Yes, it does not scale. And it should not. Don't add
1409 * new entries into /proc/<tgid>/ without very good reasons.
1410 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001411 last = &ents[nents - 1];
1412 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (p->len != dentry->d_name.len)
1414 continue;
1415 if (!memcmp(dentry->d_name.name, p->name, p->len))
1416 break;
1417 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001418 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 goto out;
1420
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001421 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001423 put_task_struct(task);
1424out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001425 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Eric W. Biederman61a28782006-10-02 02:18:49 -07001428static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1429 struct task_struct *task, struct pid_entry *p)
1430{
1431 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1432 proc_pident_instantiate, task, p);
1433}
1434
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001435static int proc_pident_readdir(struct file *filp,
1436 void *dirent, filldir_t filldir,
1437 struct pid_entry *ents, unsigned int nents)
1438{
1439 int i;
1440 int pid;
1441 struct dentry *dentry = filp->f_dentry;
1442 struct inode *inode = dentry->d_inode;
1443 struct task_struct *task = get_proc_task(inode);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001444 struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001445 ino_t ino;
1446 int ret;
1447
1448 ret = -ENOENT;
1449 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001450 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001451
1452 ret = 0;
1453 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001454 i = filp->f_pos;
1455 switch (i) {
1456 case 0:
1457 ino = inode->i_ino;
1458 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1459 goto out;
1460 i++;
1461 filp->f_pos++;
1462 /* fall through */
1463 case 1:
1464 ino = parent_ino(dentry);
1465 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1466 goto out;
1467 i++;
1468 filp->f_pos++;
1469 /* fall through */
1470 default:
1471 i -= 2;
1472 if (i >= nents) {
1473 ret = 1;
1474 goto out;
1475 }
1476 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001477 last = &ents[nents - 1];
1478 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001479 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001480 goto out;
1481 filp->f_pos++;
1482 p++;
1483 }
1484 }
1485
1486 ret = 1;
1487out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001488 put_task_struct(task);
1489out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001490 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001494static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1495 size_t count, loff_t *ppos)
1496{
1497 struct inode * inode = file->f_dentry->d_inode;
1498 unsigned long page;
1499 ssize_t length;
1500 struct task_struct *task = get_proc_task(inode);
1501
1502 length = -ESRCH;
1503 if (!task)
1504 goto out_no_task;
1505
1506 if (count > PAGE_SIZE)
1507 count = PAGE_SIZE;
1508 length = -ENOMEM;
1509 if (!(page = __get_free_page(GFP_KERNEL)))
1510 goto out;
1511
1512 length = security_getprocattr(task,
1513 (char*)file->f_dentry->d_name.name,
1514 (void*)page, count);
1515 if (length >= 0)
1516 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1517 free_page(page);
1518out:
1519 put_task_struct(task);
1520out_no_task:
1521 return length;
1522}
1523
1524static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1525 size_t count, loff_t *ppos)
1526{
1527 struct inode * inode = file->f_dentry->d_inode;
1528 char *page;
1529 ssize_t length;
1530 struct task_struct *task = get_proc_task(inode);
1531
1532 length = -ESRCH;
1533 if (!task)
1534 goto out_no_task;
1535 if (count > PAGE_SIZE)
1536 count = PAGE_SIZE;
1537
1538 /* No partial writes. */
1539 length = -EINVAL;
1540 if (*ppos != 0)
1541 goto out;
1542
1543 length = -ENOMEM;
1544 page = (char*)__get_free_page(GFP_USER);
1545 if (!page)
1546 goto out;
1547
1548 length = -EFAULT;
1549 if (copy_from_user(page, buf, count))
1550 goto out_free;
1551
1552 length = security_setprocattr(task,
1553 (char*)file->f_dentry->d_name.name,
1554 (void*)page, count);
1555out_free:
1556 free_page((unsigned long) page);
1557out:
1558 put_task_struct(task);
1559out_no_task:
1560 return length;
1561}
1562
1563static struct file_operations proc_pid_attr_operations = {
1564 .read = proc_pid_attr_read,
1565 .write = proc_pid_attr_write,
1566};
1567
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001568static struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001569 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1570 REG("prev", S_IRUGO, pid_attr),
1571 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1572 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1573 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1574 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001575};
1576
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001577static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 void * dirent, filldir_t filldir)
1579{
1580 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001581 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001584static struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001586 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587};
1588
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001589static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 struct dentry *dentry, struct nameidata *nd)
1591{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001592 return proc_pident_lookup(dir, dentry,
1593 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001596static struct inode_operations proc_attr_dir_inode_operations = {
1597 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001598 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001599 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600};
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602#endif
1603
1604/*
1605 * /proc/self:
1606 */
1607static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1608 int buflen)
1609{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001610 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 sprintf(tmp, "%d", current->tgid);
1612 return vfs_readlink(dentry,buffer,buflen,tmp);
1613}
1614
Al Viro008b1502005-08-20 00:17:39 +01001615static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001617 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001619 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001620}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622static struct inode_operations proc_self_inode_operations = {
1623 .readlink = proc_self_readlink,
1624 .follow_link = proc_self_follow_link,
1625};
1626
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001627/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001628 * proc base
1629 *
1630 * These are the directory entries in the root directory of /proc
1631 * that properly belong to the /proc filesystem, as they describe
1632 * describe something that is process related.
1633 */
1634static struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001635 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001636 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001637};
1638
1639/*
1640 * Exceptional case: normally we are not allowed to unhash a busy
1641 * directory. In this case, however, we can do it - no aliasing problems
1642 * due to the way we treat inodes.
1643 */
1644static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1645{
1646 struct inode *inode = dentry->d_inode;
1647 struct task_struct *task = get_proc_task(inode);
1648 if (task) {
1649 put_task_struct(task);
1650 return 1;
1651 }
1652 d_drop(dentry);
1653 return 0;
1654}
1655
1656static struct dentry_operations proc_base_dentry_operations =
1657{
1658 .d_revalidate = proc_base_revalidate,
1659 .d_delete = pid_delete_dentry,
1660};
1661
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001662static struct dentry *proc_base_instantiate(struct inode *dir,
1663 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001664{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001665 struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001666 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001667 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001668 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001669
1670 /* Allocate the inode */
1671 error = ERR_PTR(-ENOMEM);
1672 inode = new_inode(dir->i_sb);
1673 if (!inode)
1674 goto out;
1675
1676 /* Initialize the inode */
1677 ei = PROC_I(inode);
1678 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001679
1680 /*
1681 * grab the reference to the task.
1682 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001683 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001684 if (!ei->pid)
1685 goto out_iput;
1686
1687 inode->i_uid = 0;
1688 inode->i_gid = 0;
1689 inode->i_mode = p->mode;
1690 if (S_ISDIR(inode->i_mode))
1691 inode->i_nlink = 2;
1692 if (S_ISLNK(inode->i_mode))
1693 inode->i_size = 64;
1694 if (p->iop)
1695 inode->i_op = p->iop;
1696 if (p->fop)
1697 inode->i_fop = p->fop;
1698 ei->op = p->op;
1699 dentry->d_op = &proc_base_dentry_operations;
1700 d_add(dentry, inode);
1701 error = NULL;
1702out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001703 return error;
1704out_iput:
1705 iput(inode);
1706 goto out;
1707}
1708
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001709static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1710{
1711 struct dentry *error;
1712 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001713 struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001714
1715 error = ERR_PTR(-ENOENT);
1716
1717 if (!task)
1718 goto out_no_task;
1719
1720 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001721 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1722 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001723 if (p->len != dentry->d_name.len)
1724 continue;
1725 if (!memcmp(dentry->d_name.name, p->name, p->len))
1726 break;
1727 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001728 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001729 goto out;
1730
1731 error = proc_base_instantiate(dir, dentry, task, p);
1732
1733out:
1734 put_task_struct(task);
1735out_no_task:
1736 return error;
1737}
1738
Eric W. Biederman61a28782006-10-02 02:18:49 -07001739static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1740 struct task_struct *task, struct pid_entry *p)
1741{
1742 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1743 proc_base_instantiate, task, p);
1744}
1745
Eric W. Biederman801199c2006-10-02 02:18:48 -07001746/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001747 * Thread groups
1748 */
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001749static struct file_operations proc_task_operations;
1750static struct inode_operations proc_task_inode_operations;
1751
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001752static struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001753 DIR("task", S_IRUGO|S_IXUGO, task),
1754 DIR("fd", S_IRUSR|S_IXUSR, fd),
1755 INF("environ", S_IRUSR, pid_environ),
1756 INF("auxv", S_IRUSR, pid_auxv),
1757 INF("status", S_IRUGO, pid_status),
1758 INF("cmdline", S_IRUGO, pid_cmdline),
1759 INF("stat", S_IRUGO, tgid_stat),
1760 INF("statm", S_IRUGO, pid_statm),
1761 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001762#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001763 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001764#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001765 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001766#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07001767 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001769 LNK("cwd", cwd),
1770 LNK("root", root),
1771 LNK("exe", exe),
1772 REG("mounts", S_IRUGO, mounts),
1773 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001774#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07001775 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001776#endif
1777#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001778 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001779#endif
1780#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001781 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001782#endif
1783#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001784 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001785#endif
1786#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001787 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001788#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001789 INF("oom_score", S_IRUGO, oom_score),
1790 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001791#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07001792 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001793#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001794};
1795
1796static int proc_tgid_base_readdir(struct file * filp,
1797 void * dirent, filldir_t filldir)
1798{
1799 return proc_pident_readdir(filp,dirent,filldir,
1800 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1801}
1802
1803static struct file_operations proc_tgid_base_operations = {
1804 .read = generic_read_dir,
1805 .readdir = proc_tgid_base_readdir,
1806};
1807
1808static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001809 return proc_pident_lookup(dir, dentry,
1810 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001811}
1812
1813static struct inode_operations proc_tgid_base_inode_operations = {
1814 .lookup = proc_tgid_base_lookup,
1815 .getattr = pid_getattr,
1816 .setattr = proc_setattr,
1817};
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001820 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001822 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001824 * Looks in the dcache for
1825 * /proc/@pid
1826 * /proc/@tgid/task/@pid
1827 * if either directory is present flushes it and all of it'ts children
1828 * from the dcache.
1829 *
1830 * It is safe and reasonable to cache /proc entries for a task until
1831 * that task exits. After that they just clog up the dcache with
1832 * useless entries, possibly causing useful dcache entries to be
1833 * flushed instead. This routine is proved to flush those useless
1834 * dcache entries at process exit time.
1835 *
1836 * NOTE: This routine is just an optimization so it does not guarantee
1837 * that no dcache entries will exist at process exit time it
1838 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001840void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001842 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001843 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001844 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Eric W. Biederman48e64842006-06-26 00:25:48 -07001846 name.name = buf;
1847 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1848 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1849 if (dentry) {
1850 shrink_dcache_parent(dentry);
1851 d_drop(dentry);
1852 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Eric W. Biederman48e64842006-06-26 00:25:48 -07001855 if (thread_group_leader(task))
1856 goto out;
1857
1858 name.name = buf;
1859 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1860 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1861 if (!leader)
1862 goto out;
1863
1864 name.name = "task";
1865 name.len = strlen(name.name);
1866 dir = d_hash_and_lookup(leader, &name);
1867 if (!dir)
1868 goto out_put_leader;
1869
1870 name.name = buf;
1871 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1872 dentry = d_hash_and_lookup(dir, &name);
1873 if (dentry) {
1874 shrink_dcache_parent(dentry);
1875 d_drop(dentry);
1876 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001878
1879 dput(dir);
1880out_put_leader:
1881 dput(leader);
1882out:
1883 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001886struct dentry *proc_pid_instantiate(struct inode *dir,
1887 struct dentry * dentry, struct task_struct *task, void *ptr)
1888{
1889 struct dentry *error = ERR_PTR(-ENOENT);
1890 struct inode *inode;
1891
Eric W. Biederman61a28782006-10-02 02:18:49 -07001892 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001893 if (!inode)
1894 goto out;
1895
1896 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1897 inode->i_op = &proc_tgid_base_inode_operations;
1898 inode->i_fop = &proc_tgid_base_operations;
1899 inode->i_flags|=S_IMMUTABLE;
1900 inode->i_nlink = 4;
1901#ifdef CONFIG_SECURITY
1902 inode->i_nlink += 1;
1903#endif
1904
1905 dentry->d_op = &pid_dentry_operations;
1906
1907 d_add(dentry, inode);
1908 /* Close the race of the process dying before we return the dentry */
1909 if (pid_revalidate(dentry, NULL))
1910 error = NULL;
1911out:
1912 return error;
1913}
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1916{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001917 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Eric W. Biederman801199c2006-10-02 02:18:48 -07001921 result = proc_base_lookup(dir, dentry);
1922 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1923 goto out;
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 tgid = name_to_int(dentry);
1926 if (tgid == ~0U)
1927 goto out;
1928
Eric W. Biedermande758732006-06-26 00:25:51 -07001929 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 task = find_task_by_pid(tgid);
1931 if (task)
1932 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07001933 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (!task)
1935 goto out;
1936
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001937 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07001938 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001940 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001944 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001945 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001947static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001949 struct task_struct *task;
1950 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001952 rcu_read_lock();
1953retry:
1954 task = NULL;
1955 pid = find_ge_pid(tgid);
1956 if (pid) {
1957 tgid = pid->nr + 1;
1958 task = pid_task(pid, PIDTYPE_PID);
1959 /* What we to know is if the pid we have find is the
1960 * pid of a thread_group_leader. Testing for task
1961 * being a thread_group_leader is the obvious thing
1962 * todo but there is a window when it fails, due to
1963 * the pid transfer logic in de_thread.
1964 *
1965 * So we perform the straight forward test of seeing
1966 * if the pid we have found is the pid of a thread
1967 * group leader, and don't worry if the task we have
1968 * found doesn't happen to be a thread group leader.
1969 * As we don't care in the case of readdir.
1970 */
1971 if (!task || !has_group_leader_pid(task))
1972 goto retry;
1973 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07001975 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001976 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001979#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Eric W. Biederman61a28782006-10-02 02:18:49 -07001981static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1982 struct task_struct *task, int tgid)
1983{
1984 char name[PROC_NUMBUF];
1985 int len = snprintf(name, sizeof(name), "%d", tgid);
1986 return proc_fill_cache(filp, dirent, filldir, name, len,
1987 proc_pid_instantiate, task, NULL);
1988}
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990/* for the /proc/ directory itself, after non-process stuff has been done */
1991int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1992{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001994 struct task_struct *reaper = get_proc_task(filp->f_dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001995 struct task_struct *task;
1996 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Eric W. Biederman61a28782006-10-02 02:18:49 -07001998 if (!reaper)
1999 goto out_no_task;
2000
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002001 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric W. Biederman801199c2006-10-02 02:18:48 -07002002 struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002003 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002007 tgid = filp->f_pos - TGID_OFFSET;
2008 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002009 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002010 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002011 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002012 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002013 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002014 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002015 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002018 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2019out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002020 put_task_struct(reaper);
2021out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return 0;
2023}
2024
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002025/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002026 * Tasks
2027 */
2028static struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002029 DIR("fd", S_IRUSR|S_IXUSR, fd),
2030 INF("environ", S_IRUSR, pid_environ),
2031 INF("auxv", S_IRUSR, pid_auxv),
2032 INF("status", S_IRUGO, pid_status),
2033 INF("cmdline", S_IRUGO, pid_cmdline),
2034 INF("stat", S_IRUGO, tid_stat),
2035 INF("statm", S_IRUGO, pid_statm),
2036 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002037#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002038 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002039#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002040 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002041#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002042 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002043#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002044 LNK("cwd", cwd),
2045 LNK("root", root),
2046 LNK("exe", exe),
2047 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002048#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07002049 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002050#endif
2051#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002052 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002053#endif
2054#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002055 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002056#endif
2057#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002058 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002059#endif
2060#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002061 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002062#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002063 INF("oom_score", S_IRUGO, oom_score),
2064 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002065#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002066 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002067#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002068};
2069
2070static int proc_tid_base_readdir(struct file * filp,
2071 void * dirent, filldir_t filldir)
2072{
2073 return proc_pident_readdir(filp,dirent,filldir,
2074 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2075}
2076
2077static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002078 return proc_pident_lookup(dir, dentry,
2079 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002080}
2081
2082static struct file_operations proc_tid_base_operations = {
2083 .read = generic_read_dir,
2084 .readdir = proc_tid_base_readdir,
2085};
2086
2087static struct inode_operations proc_tid_base_inode_operations = {
2088 .lookup = proc_tid_base_lookup,
2089 .getattr = pid_getattr,
2090 .setattr = proc_setattr,
2091};
2092
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002093static struct dentry *proc_task_instantiate(struct inode *dir,
2094 struct dentry *dentry, struct task_struct *task, void *ptr)
2095{
2096 struct dentry *error = ERR_PTR(-ENOENT);
2097 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002098 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002099
2100 if (!inode)
2101 goto out;
2102 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2103 inode->i_op = &proc_tid_base_inode_operations;
2104 inode->i_fop = &proc_tid_base_operations;
2105 inode->i_flags|=S_IMMUTABLE;
2106 inode->i_nlink = 3;
2107#ifdef CONFIG_SECURITY
2108 inode->i_nlink += 1;
2109#endif
2110
2111 dentry->d_op = &pid_dentry_operations;
2112
2113 d_add(dentry, inode);
2114 /* Close the race of the process dying before we return the dentry */
2115 if (pid_revalidate(dentry, NULL))
2116 error = NULL;
2117out:
2118 return error;
2119}
2120
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002121static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2122{
2123 struct dentry *result = ERR_PTR(-ENOENT);
2124 struct task_struct *task;
2125 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002126 unsigned tid;
2127
2128 if (!leader)
2129 goto out_no_task;
2130
2131 tid = name_to_int(dentry);
2132 if (tid == ~0U)
2133 goto out;
2134
2135 rcu_read_lock();
2136 task = find_task_by_pid(tid);
2137 if (task)
2138 get_task_struct(task);
2139 rcu_read_unlock();
2140 if (!task)
2141 goto out;
2142 if (leader->tgid != task->tgid)
2143 goto out_drop_task;
2144
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002145 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002146out_drop_task:
2147 put_task_struct(task);
2148out:
2149 put_task_struct(leader);
2150out_no_task:
2151 return result;
2152}
2153
2154/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002155 * Find the first tid of a thread group to return to user space.
2156 *
2157 * Usually this is just the thread group leader, but if the users
2158 * buffer was too small or there was a seek into the middle of the
2159 * directory we have more work todo.
2160 *
2161 * In the case of a short read we start with find_task_by_pid.
2162 *
2163 * In the case of a seek we start with the leader and walk nr
2164 * threads past it.
2165 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002166static struct task_struct *first_tid(struct task_struct *leader,
2167 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002168{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002169 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002170
Eric W. Biedermancc288732006-06-26 00:26:01 -07002171 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002172 /* Attempt to start with the pid of a thread */
2173 if (tid && (nr > 0)) {
2174 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002175 if (pos && (pos->group_leader == leader))
2176 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002177 }
2178
2179 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002180 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002181 if (nr && nr >= get_nr_threads(leader))
2182 goto out;
2183
2184 /* If we haven't found our starting place yet start
2185 * with the leader and walk nr threads forward.
2186 */
2187 for (pos = leader; nr > 0; --nr) {
2188 pos = next_thread(pos);
2189 if (pos == leader) {
2190 pos = NULL;
2191 goto out;
2192 }
2193 }
2194found:
2195 get_task_struct(pos);
2196out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002197 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002198 return pos;
2199}
2200
2201/*
2202 * Find the next thread in the thread list.
2203 * Return NULL if there is an error or no next thread.
2204 *
2205 * The reference to the input task_struct is released.
2206 */
2207static struct task_struct *next_tid(struct task_struct *start)
2208{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002209 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002210 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002211 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002212 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002213 if (thread_group_leader(pos))
2214 pos = NULL;
2215 else
2216 get_task_struct(pos);
2217 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002218 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002219 put_task_struct(start);
2220 return pos;
2221}
2222
Eric W. Biederman61a28782006-10-02 02:18:49 -07002223static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2224 struct task_struct *task, int tid)
2225{
2226 char name[PROC_NUMBUF];
2227 int len = snprintf(name, sizeof(name), "%d", tid);
2228 return proc_fill_cache(filp, dirent, filldir, name, len,
2229 proc_task_instantiate, task, NULL);
2230}
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232/* for the /proc/TGID/task/ directories */
2233static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 struct dentry *dentry = filp->f_dentry;
2236 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002237 struct task_struct *leader = get_proc_task(inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002238 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 int retval = -ENOENT;
2240 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002241 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2243
Eric W. Biederman99f89552006-06-26 00:25:55 -07002244 if (!leader)
2245 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 retval = 0;
2247
2248 switch (pos) {
2249 case 0:
2250 ino = inode->i_ino;
2251 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2252 goto out;
2253 pos++;
2254 /* fall through */
2255 case 1:
2256 ino = parent_ino(dentry);
2257 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2258 goto out;
2259 pos++;
2260 /* fall through */
2261 }
2262
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002263 /* f_version caches the tgid value that the last readdir call couldn't
2264 * return. lseek aka telldir automagically resets f_version to 0.
2265 */
2266 tid = filp->f_version;
2267 filp->f_version = 0;
2268 for (task = first_tid(leader, tid, pos - 2);
2269 task;
2270 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002271 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002272 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002273 /* returning this tgid failed, save it as the first
2274 * pid for the next readir call */
2275 filp->f_version = tid;
2276 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280out:
2281 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002282 put_task_struct(leader);
2283out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return retval;
2285}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002286
2287static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2288{
2289 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002290 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002291 generic_fillattr(inode, stat);
2292
Eric W. Biederman99f89552006-06-26 00:25:55 -07002293 if (p) {
2294 rcu_read_lock();
2295 stat->nlink += get_nr_threads(p);
2296 rcu_read_unlock();
2297 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002298 }
2299
2300 return 0;
2301}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002302
2303static struct inode_operations proc_task_inode_operations = {
2304 .lookup = proc_task_lookup,
2305 .getattr = proc_task_getattr,
2306 .setattr = proc_setattr,
2307};
2308
2309static struct file_operations proc_task_operations = {
2310 .read = generic_read_dir,
2311 .readdir = proc_task_readdir,
2312};