blob: 9c6a809f92b69228c8ce2ea99d29438a5095b50f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include "internal.h"
75
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070076/* NOTE:
77 * Implementing inode permission operations in /proc is almost
78 * certainly an error. Permission checks need to happen during
79 * each system call not at open time. The reason is that most of
80 * what we wish to check for permissions in /proc varies at runtime.
81 *
82 * The classic example of a problem is opening file descriptors
83 * in /proc for a task before it execs a suid executable.
84 */
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * For hysterical raisins we keep the same inumbers as in the old procfs.
88 * Feel free to change the macro below - just keep the range distinct from
89 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
90 * As soon as we'll get a separate superblock we will be able to forget
91 * about magical ranges too.
92 */
93
94#define fake_ino(pid,ino) (((pid)<<16)|(ino))
95
96enum pid_directory_inos {
97 PROC_TGID_INO = 2,
98 PROC_TGID_TASK,
99 PROC_TGID_STATUS,
100 PROC_TGID_MEM,
101#ifdef CONFIG_SECCOMP
102 PROC_TGID_SECCOMP,
103#endif
104 PROC_TGID_CWD,
105 PROC_TGID_ROOT,
106 PROC_TGID_EXE,
107 PROC_TGID_FD,
108 PROC_TGID_ENVIRON,
109 PROC_TGID_AUXV,
110 PROC_TGID_CMDLINE,
111 PROC_TGID_STAT,
112 PROC_TGID_STATM,
113 PROC_TGID_MAPS,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700114 PROC_TGID_NUMA_MAPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 PROC_TGID_MOUNTS,
Chuck Leverb4629fe2006-03-20 13:44:12 -0500116 PROC_TGID_MOUNTSTATS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 PROC_TGID_WCHAN,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700118#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -0700119 PROC_TGID_SMAPS,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#ifdef CONFIG_SCHEDSTATS
122 PROC_TGID_SCHEDSTAT,
123#endif
124#ifdef CONFIG_CPUSETS
125 PROC_TGID_CPUSET,
126#endif
127#ifdef CONFIG_SECURITY
128 PROC_TGID_ATTR,
129 PROC_TGID_ATTR_CURRENT,
130 PROC_TGID_ATTR_PREV,
131 PROC_TGID_ATTR_EXEC,
132 PROC_TGID_ATTR_FSCREATE,
Michael LeMay4eb582c2006-06-26 00:24:57 -0700133 PROC_TGID_ATTR_KEYCREATE,
Eric Paris42c3e032006-06-26 00:26:03 -0700134 PROC_TGID_ATTR_SOCKCREATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#endif
136#ifdef CONFIG_AUDITSYSCALL
137 PROC_TGID_LOGINUID,
138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 PROC_TGID_OOM_SCORE,
140 PROC_TGID_OOM_ADJUST,
141 PROC_TID_INO,
142 PROC_TID_STATUS,
143 PROC_TID_MEM,
144#ifdef CONFIG_SECCOMP
145 PROC_TID_SECCOMP,
146#endif
147 PROC_TID_CWD,
148 PROC_TID_ROOT,
149 PROC_TID_EXE,
150 PROC_TID_FD,
151 PROC_TID_ENVIRON,
152 PROC_TID_AUXV,
153 PROC_TID_CMDLINE,
154 PROC_TID_STAT,
155 PROC_TID_STATM,
156 PROC_TID_MAPS,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700157 PROC_TID_NUMA_MAPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 PROC_TID_MOUNTS,
Chuck Leverb4629fe2006-03-20 13:44:12 -0500159 PROC_TID_MOUNTSTATS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 PROC_TID_WCHAN,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700161#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -0700162 PROC_TID_SMAPS,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#ifdef CONFIG_SCHEDSTATS
165 PROC_TID_SCHEDSTAT,
166#endif
167#ifdef CONFIG_CPUSETS
168 PROC_TID_CPUSET,
169#endif
170#ifdef CONFIG_SECURITY
171 PROC_TID_ATTR,
172 PROC_TID_ATTR_CURRENT,
173 PROC_TID_ATTR_PREV,
174 PROC_TID_ATTR_EXEC,
175 PROC_TID_ATTR_FSCREATE,
Michael LeMay4eb582c2006-06-26 00:24:57 -0700176 PROC_TID_ATTR_KEYCREATE,
Eric Paris42c3e032006-06-26 00:26:03 -0700177 PROC_TID_ATTR_SOCKCREATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif
179#ifdef CONFIG_AUDITSYSCALL
180 PROC_TID_LOGINUID,
181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 PROC_TID_OOM_SCORE,
183 PROC_TID_OOM_ADJUST,
Miklos Szeredi5e21ccb2005-09-06 15:18:23 -0700184
185 /* Add new entries before this */
186 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700189/* Worst case buffer size needed for holding an integer. */
190#define PROC_NUMBUF 10
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192struct pid_entry {
193 int type;
194 int len;
195 char *name;
196 mode_t mode;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700197 struct inode_operations *iop;
198 struct file_operations *fop;
199 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700202#define NOD(TYPE, NAME, MODE, IOP, FOP, OP) { \
203 .type = (TYPE), \
204 .len = sizeof(NAME) - 1, \
205 .name = (NAME), \
206 .mode = MODE, \
207 .iop = IOP, \
208 .fop = FOP, \
209 .op = OP, \
210}
211
212#define DIR(TYPE, NAME, MODE, OTYPE) \
213 NOD(TYPE, NAME, (S_IFDIR|(MODE)), \
214 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
215 {} )
216#define LNK(TYPE, NAME, OTYPE) \
217 NOD(TYPE, NAME, (S_IFLNK|S_IRWXUGO), \
218 &proc_pid_link_inode_operations, NULL, \
219 { .proc_get_link = &proc_##OTYPE##_link } )
220#define REG(TYPE, NAME, MODE, OTYPE) \
221 NOD(TYPE, NAME, (S_IFREG|(MODE)), NULL, \
222 &proc_##OTYPE##_operations, {})
223#define INF(TYPE, NAME, MODE, OTYPE) \
224 NOD(TYPE, NAME, (S_IFREG|(MODE)), \
225 NULL, &proc_info_file_operations, \
226 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700228static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700231 task_lock(task);
232 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if(fs)
234 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700235 task_unlock(task);
236 return fs;
237}
238
Eric W. Biederman99f89552006-06-26 00:25:55 -0700239static int get_nr_threads(struct task_struct *tsk)
240{
241 /* Must be called with the rcu_read_lock held */
242 unsigned long flags;
243 int count = 0;
244
245 if (lock_task_sighand(tsk, &flags)) {
246 count = atomic_read(&tsk->signal->count);
247 unlock_task_sighand(tsk, &flags);
248 }
249 return count;
250}
251
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700252static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
253{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700254 struct task_struct *task = get_proc_task(inode);
255 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700256 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700257
258 if (task) {
259 fs = get_fs_struct(task);
260 put_task_struct(task);
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (fs) {
263 read_lock(&fs->lock);
264 *mnt = mntget(fs->pwdmnt);
265 *dentry = dget(fs->pwd);
266 read_unlock(&fs->lock);
267 result = 0;
268 put_fs_struct(fs);
269 }
270 return result;
271}
272
273static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
274{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700275 struct task_struct *task = get_proc_task(inode);
276 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700278
279 if (task) {
280 fs = get_fs_struct(task);
281 put_task_struct(task);
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (fs) {
284 read_lock(&fs->lock);
285 *mnt = mntget(fs->rootmnt);
286 *dentry = dget(fs->root);
287 read_unlock(&fs->lock);
288 result = 0;
289 put_fs_struct(fs);
290 }
291 return result;
292}
293
294#define MAY_PTRACE(task) \
295 (task == current || \
296 (task->parent == current && \
297 (task->ptrace & PT_PTRACED) && \
298 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
299 security_ptrace(current,task) == 0))
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static int proc_pid_environ(struct task_struct *task, char * buffer)
302{
303 int res = 0;
304 struct mm_struct *mm = get_task_mm(task);
305 if (mm) {
306 unsigned int len = mm->env_end - mm->env_start;
307 if (len > PAGE_SIZE)
308 len = PAGE_SIZE;
309 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700310 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 res = -ESRCH;
312 mmput(mm);
313 }
314 return res;
315}
316
317static int proc_pid_cmdline(struct task_struct *task, char * buffer)
318{
319 int res = 0;
320 unsigned int len;
321 struct mm_struct *mm = get_task_mm(task);
322 if (!mm)
323 goto out;
324 if (!mm->arg_end)
325 goto out_mm; /* Shh! No looking before we're done */
326
327 len = mm->arg_end - mm->arg_start;
328
329 if (len > PAGE_SIZE)
330 len = PAGE_SIZE;
331
332 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
333
334 // If the nul at the end of args has been overwritten, then
335 // assume application is using setproctitle(3).
336 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
337 len = strnlen(buffer, res);
338 if (len < res) {
339 res = len;
340 } else {
341 len = mm->env_end - mm->env_start;
342 if (len > PAGE_SIZE - res)
343 len = PAGE_SIZE - res;
344 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
345 res = strnlen(buffer, res);
346 }
347 }
348out_mm:
349 mmput(mm);
350out:
351 return res;
352}
353
354static int proc_pid_auxv(struct task_struct *task, char *buffer)
355{
356 int res = 0;
357 struct mm_struct *mm = get_task_mm(task);
358 if (mm) {
359 unsigned int nwords = 0;
360 do
361 nwords += 2;
362 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
363 res = nwords * sizeof(mm->saved_auxv[0]);
364 if (res > PAGE_SIZE)
365 res = PAGE_SIZE;
366 memcpy(buffer, mm->saved_auxv, res);
367 mmput(mm);
368 }
369 return res;
370}
371
372
373#ifdef CONFIG_KALLSYMS
374/*
375 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
376 * Returns the resolved symbol. If that fails, simply return the address.
377 */
378static int proc_pid_wchan(struct task_struct *task, char *buffer)
379{
380 char *modname;
381 const char *sym_name;
382 unsigned long wchan, size, offset;
383 char namebuf[KSYM_NAME_LEN+1];
384
385 wchan = get_wchan(task);
386
387 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
388 if (sym_name)
389 return sprintf(buffer, "%s", sym_name);
390 return sprintf(buffer, "%lu", wchan);
391}
392#endif /* CONFIG_KALLSYMS */
393
394#ifdef CONFIG_SCHEDSTATS
395/*
396 * Provides /proc/PID/schedstat
397 */
398static int proc_pid_schedstat(struct task_struct *task, char *buffer)
399{
400 return sprintf(buffer, "%lu %lu %lu\n",
401 task->sched_info.cpu_time,
402 task->sched_info.run_delay,
403 task->sched_info.pcnt);
404}
405#endif
406
407/* The badness from the OOM killer */
408unsigned long badness(struct task_struct *p, unsigned long uptime);
409static int proc_oom_score(struct task_struct *task, char *buffer)
410{
411 unsigned long points;
412 struct timespec uptime;
413
414 do_posix_clock_monotonic_gettime(&uptime);
415 points = badness(task, uptime.tv_sec);
416 return sprintf(buffer, "%lu\n", points);
417}
418
419/************************************************************************/
420/* Here the fs part begins */
421/************************************************************************/
422
423/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700424static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700426 struct task_struct *task;
427 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700428 /* Allow access to a task's file descriptors if it is us or we
429 * may use ptrace attach to the process and find out that
430 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700431 */
432 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700433 if (task) {
434 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700435 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700436 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700437 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700440static int proc_setattr(struct dentry *dentry, struct iattr *attr)
441{
442 int error;
443 struct inode *inode = dentry->d_inode;
444
445 if (attr->ia_valid & ATTR_MODE)
446 return -EPERM;
447
448 error = inode_change_ok(inode, attr);
449 if (!error) {
450 error = security_inode_setattr(dentry, attr);
451 if (!error)
452 error = inode_setattr(inode, attr);
453 }
454 return error;
455}
456
457static struct inode_operations proc_def_inode_operations = {
458 .setattr = proc_setattr,
459};
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500462struct proc_mounts {
463 struct seq_file m;
464 int event;
465};
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static int mounts_open(struct inode *inode, struct file *file)
468{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700469 struct task_struct *task = get_proc_task(inode);
470 struct namespace *namespace = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500471 struct proc_mounts *p;
472 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Eric W. Biederman99f89552006-06-26 00:25:55 -0700474 if (task) {
475 task_lock(task);
476 namespace = task->namespace;
477 if (namespace)
478 get_namespace(namespace);
479 task_unlock(task);
480 put_task_struct(task);
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Al Viro5addc5d2005-11-07 17:15:49 -0500483 if (namespace) {
484 ret = -ENOMEM;
485 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
486 if (p) {
487 file->private_data = &p->m;
488 ret = seq_open(file, &mounts_op);
489 if (!ret) {
490 p->m.private = namespace;
491 p->event = namespace->event;
492 return 0;
493 }
494 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Al Viro5addc5d2005-11-07 17:15:49 -0500496 put_namespace(namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 return ret;
499}
500
501static int mounts_release(struct inode *inode, struct file *file)
502{
503 struct seq_file *m = file->private_data;
504 struct namespace *namespace = m->private;
505 put_namespace(namespace);
506 return seq_release(inode, file);
507}
508
Al Viro5addc5d2005-11-07 17:15:49 -0500509static unsigned mounts_poll(struct file *file, poll_table *wait)
510{
511 struct proc_mounts *p = file->private_data;
512 struct namespace *ns = p->m.private;
513 unsigned res = 0;
514
515 poll_wait(file, &ns->poll, wait);
516
517 spin_lock(&vfsmount_lock);
518 if (p->event != ns->event) {
519 p->event = ns->event;
520 res = POLLERR;
521 }
522 spin_unlock(&vfsmount_lock);
523
524 return res;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static struct file_operations proc_mounts_operations = {
528 .open = mounts_open,
529 .read = seq_read,
530 .llseek = seq_lseek,
531 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500532 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533};
534
Chuck Leverb4629fe2006-03-20 13:44:12 -0500535extern struct seq_operations mountstats_op;
536static int mountstats_open(struct inode *inode, struct file *file)
537{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500538 int ret = seq_open(file, &mountstats_op);
539
540 if (!ret) {
541 struct seq_file *m = file->private_data;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700542 struct namespace *namespace = NULL;
543 struct task_struct *task = get_proc_task(inode);
544
545 if (task) {
546 task_lock(task);
547 namespace = task->namespace;
548 if (namespace)
549 get_namespace(namespace);
550 task_unlock(task);
551 put_task_struct(task);
552 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500553
554 if (namespace)
555 m->private = namespace;
556 else {
557 seq_release(inode, file);
558 ret = -EINVAL;
559 }
560 }
561 return ret;
562}
563
564static struct file_operations proc_mountstats_operations = {
565 .open = mountstats_open,
566 .read = seq_read,
567 .llseek = seq_lseek,
568 .release = mounts_release,
569};
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
572
573static ssize_t proc_info_read(struct file * file, char __user * buf,
574 size_t count, loff_t *ppos)
575{
576 struct inode * inode = file->f_dentry->d_inode;
577 unsigned long page;
578 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700579 struct task_struct *task = get_proc_task(inode);
580
581 length = -ESRCH;
582 if (!task)
583 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (count > PROC_BLOCK_SIZE)
586 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700587
588 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 length = PROC_I(inode)->op.proc_read(task, (char*)page);
593
594 if (length >= 0)
595 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
596 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700597out:
598 put_task_struct(task);
599out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return length;
601}
602
603static struct file_operations proc_info_file_operations = {
604 .read = proc_info_read,
605};
606
607static int mem_open(struct inode* inode, struct file* file)
608{
609 file->private_data = (void*)((long)current->self_exec_id);
610 return 0;
611}
612
613static ssize_t mem_read(struct file * file, char __user * buf,
614 size_t count, loff_t *ppos)
615{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700616 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 char *page;
618 unsigned long src = *ppos;
619 int ret = -ESRCH;
620 struct mm_struct *mm;
621
Eric W. Biederman99f89552006-06-26 00:25:55 -0700622 if (!task)
623 goto out_no_task;
624
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700625 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 goto out;
627
628 ret = -ENOMEM;
629 page = (char *)__get_free_page(GFP_USER);
630 if (!page)
631 goto out;
632
633 ret = 0;
634
635 mm = get_task_mm(task);
636 if (!mm)
637 goto out_free;
638
639 ret = -EIO;
640
641 if (file->private_data != (void*)((long)current->self_exec_id))
642 goto out_put;
643
644 ret = 0;
645
646 while (count > 0) {
647 int this_len, retval;
648
649 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
650 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700651 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!ret)
653 ret = -EIO;
654 break;
655 }
656
657 if (copy_to_user(buf, page, retval)) {
658 ret = -EFAULT;
659 break;
660 }
661
662 ret += retval;
663 src += retval;
664 buf += retval;
665 count -= retval;
666 }
667 *ppos = src;
668
669out_put:
670 mmput(mm);
671out_free:
672 free_page((unsigned long) page);
673out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700674 put_task_struct(task);
675out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return ret;
677}
678
679#define mem_write NULL
680
681#ifndef mem_write
682/* This is a security hazard */
683static ssize_t mem_write(struct file * file, const char * buf,
684 size_t count, loff_t *ppos)
685{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700686 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 char *page;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700688 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned long dst = *ppos;
690
Eric W. Biederman99f89552006-06-26 00:25:55 -0700691 copied = -ESRCH;
692 if (!task)
693 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Eric W. Biederman99f89552006-06-26 00:25:55 -0700695 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
696 goto out;
697
698 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 page = (char *)__get_free_page(GFP_USER);
700 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700701 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700703 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 while (count > 0) {
705 int this_len, retval;
706
707 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
708 if (copy_from_user(page, buf, this_len)) {
709 copied = -EFAULT;
710 break;
711 }
712 retval = access_process_vm(task, dst, page, this_len, 1);
713 if (!retval) {
714 if (!copied)
715 copied = -EIO;
716 break;
717 }
718 copied += retval;
719 buf += retval;
720 dst += retval;
721 count -= retval;
722 }
723 *ppos = dst;
724 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700725out:
726 put_task_struct(task);
727out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return copied;
729}
730#endif
731
732static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
733{
734 switch (orig) {
735 case 0:
736 file->f_pos = offset;
737 break;
738 case 1:
739 file->f_pos += offset;
740 break;
741 default:
742 return -EINVAL;
743 }
744 force_successful_syscall_return();
745 return file->f_pos;
746}
747
748static struct file_operations proc_mem_operations = {
749 .llseek = mem_lseek,
750 .read = mem_read,
751 .write = mem_write,
752 .open = mem_open,
753};
754
755static ssize_t oom_adjust_read(struct file *file, char __user *buf,
756 size_t count, loff_t *ppos)
757{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700758 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700759 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700761 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 loff_t __ppos = *ppos;
763
Eric W. Biederman99f89552006-06-26 00:25:55 -0700764 if (!task)
765 return -ESRCH;
766 oom_adjust = task->oomkilladj;
767 put_task_struct(task);
768
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700769 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (__ppos >= len)
771 return 0;
772 if (count > len-__ppos)
773 count = len-__ppos;
774 if (copy_to_user(buf, buffer + __ppos, count))
775 return -EFAULT;
776 *ppos = __ppos + count;
777 return count;
778}
779
780static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
781 size_t count, loff_t *ppos)
782{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700783 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700784 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int oom_adjust;
786
787 if (!capable(CAP_SYS_RESOURCE))
788 return -EPERM;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700789 memset(buffer, 0, sizeof(buffer));
790 if (count > sizeof(buffer) - 1)
791 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (copy_from_user(buffer, buf, count))
793 return -EFAULT;
794 oom_adjust = simple_strtol(buffer, &end, 0);
Andrea Arcangeli79befd02005-04-16 15:24:05 -0700795 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -EINVAL;
797 if (*end == '\n')
798 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700799 task = get_proc_task(file->f_dentry->d_inode);
800 if (!task)
801 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700803 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (end - buffer == 0)
805 return -EIO;
806 return end - buffer;
807}
808
809static struct file_operations proc_oom_adjust_operations = {
810 .read = oom_adjust_read,
811 .write = oom_adjust_write,
812};
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814#ifdef CONFIG_AUDITSYSCALL
815#define TMPBUFLEN 21
816static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
817 size_t count, loff_t *ppos)
818{
819 struct inode * inode = file->f_dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700820 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ssize_t length;
822 char tmpbuf[TMPBUFLEN];
823
Eric W. Biederman99f89552006-06-26 00:25:55 -0700824 if (!task)
825 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
827 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700828 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
830}
831
832static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
833 size_t count, loff_t *ppos)
834{
835 struct inode * inode = file->f_dentry->d_inode;
836 char *page, *tmp;
837 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 uid_t loginuid;
839
840 if (!capable(CAP_AUDIT_CONTROL))
841 return -EPERM;
842
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700843 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EPERM;
845
Al Viroe0182902006-05-18 08:28:02 -0400846 if (count >= PAGE_SIZE)
847 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (*ppos != 0) {
850 /* No partial writes. */
851 return -EINVAL;
852 }
853 page = (char*)__get_free_page(GFP_USER);
854 if (!page)
855 return -ENOMEM;
856 length = -EFAULT;
857 if (copy_from_user(page, buf, count))
858 goto out_free_page;
859
Al Viroe0182902006-05-18 08:28:02 -0400860 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 loginuid = simple_strtoul(page, &tmp, 10);
862 if (tmp == page) {
863 length = -EINVAL;
864 goto out_free_page;
865
866 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700867 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (likely(length == 0))
869 length = count;
870
871out_free_page:
872 free_page((unsigned long) page);
873 return length;
874}
875
876static struct file_operations proc_loginuid_operations = {
877 .read = proc_loginuid_read,
878 .write = proc_loginuid_write,
879};
880#endif
881
882#ifdef CONFIG_SECCOMP
883static ssize_t seccomp_read(struct file *file, char __user *buf,
884 size_t count, loff_t *ppos)
885{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700886 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 char __buf[20];
888 loff_t __ppos = *ppos;
889 size_t len;
890
Eric W. Biederman99f89552006-06-26 00:25:55 -0700891 if (!tsk)
892 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* no need to print the trailing zero, so use only len */
894 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700895 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (__ppos >= len)
897 return 0;
898 if (count > len - __ppos)
899 count = len - __ppos;
900 if (copy_to_user(buf, __buf + __ppos, count))
901 return -EFAULT;
902 *ppos = __ppos + count;
903 return count;
904}
905
906static ssize_t seccomp_write(struct file *file, const char __user *buf,
907 size_t count, loff_t *ppos)
908{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700909 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 char __buf[20], *end;
911 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700912 ssize_t result;
913
914 result = -ESRCH;
915 if (!tsk)
916 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700919 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700921 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Eric W. Biederman99f89552006-06-26 00:25:55 -0700923 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 memset(__buf, 0, sizeof(__buf));
925 count = min(count, sizeof(__buf) - 1);
926 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700927 goto out;
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 seccomp_mode = simple_strtoul(__buf, &end, 0);
930 if (*end == '\n')
931 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700932 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
934 tsk->seccomp.mode = seccomp_mode;
935 set_tsk_thread_flag(tsk, TIF_SECCOMP);
936 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700937 goto out;
938 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700940 goto out;
941 result = end - __buf;
942out:
943 put_task_struct(tsk);
944out_no_task:
945 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948static struct file_operations proc_seccomp_operations = {
949 .read = seccomp_read,
950 .write = seccomp_write,
951};
952#endif /* CONFIG_SECCOMP */
953
Al Viro008b1502005-08-20 00:17:39 +0100954static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 struct inode *inode = dentry->d_inode;
957 int error = -EACCES;
958
959 /* We don't need a base pointer in the /proc filesystem */
960 path_release(nd);
961
Eric W. Biederman778c1142006-06-26 00:25:58 -0700962 /* Are we allowed to snoop on the tasks file descriptors? */
963 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
967 nd->last_type = LAST_BIND;
968out:
Al Viro008b1502005-08-20 00:17:39 +0100969 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
973 char __user *buffer, int buflen)
974{
975 struct inode * inode;
976 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
977 int len;
978
979 if (!tmp)
980 return -ENOMEM;
981
982 inode = dentry->d_inode;
983 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
984 len = PTR_ERR(path);
985 if (IS_ERR(path))
986 goto out;
987 len = tmp + PAGE_SIZE - 1 - path;
988
989 if (len > buflen)
990 len = buflen;
991 if (copy_to_user(buffer, path, len))
992 len = -EFAULT;
993 out:
994 free_page((unsigned long)tmp);
995 return len;
996}
997
998static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
999{
1000 int error = -EACCES;
1001 struct inode *inode = dentry->d_inode;
1002 struct dentry *de;
1003 struct vfsmount *mnt = NULL;
1004
Eric W. Biederman778c1142006-06-26 00:25:58 -07001005 /* Are we allowed to snoop on the tasks file descriptors? */
1006 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1010 if (error)
1011 goto out;
1012
1013 error = do_proc_readlink(de, mnt, buffer, buflen);
1014 dput(de);
1015 mntput(mnt);
1016out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return error;
1018}
1019
1020static struct inode_operations proc_pid_link_inode_operations = {
1021 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001022 .follow_link = proc_pid_follow_link,
1023 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024};
1025
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001026
1027/* building an inode */
1028
1029static int task_dumpable(struct task_struct *task)
1030{
1031 int dumpable = 0;
1032 struct mm_struct *mm;
1033
1034 task_lock(task);
1035 mm = task->mm;
1036 if (mm)
1037 dumpable = mm->dumpable;
1038 task_unlock(task);
1039 if(dumpable == 1)
1040 return 1;
1041 return 0;
1042}
1043
1044
1045static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1046{
1047 struct inode * inode;
1048 struct proc_inode *ei;
1049
1050 /* We need a new inode */
1051
1052 inode = new_inode(sb);
1053 if (!inode)
1054 goto out;
1055
1056 /* Common stuff */
1057 ei = PROC_I(inode);
1058 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1059 inode->i_ino = fake_ino(task->pid, ino);
1060 inode->i_op = &proc_def_inode_operations;
1061
1062 /*
1063 * grab the reference to task.
1064 */
1065 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1066 if (!ei->pid)
1067 goto out_unlock;
1068
1069 inode->i_uid = 0;
1070 inode->i_gid = 0;
1071 if (task_dumpable(task)) {
1072 inode->i_uid = task->euid;
1073 inode->i_gid = task->egid;
1074 }
1075 security_task_to_inode(task, inode);
1076
1077out:
1078 return inode;
1079
1080out_unlock:
1081 iput(inode);
1082 return NULL;
1083}
1084
1085static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1086{
1087 struct inode *inode = dentry->d_inode;
1088 struct task_struct *task;
1089 generic_fillattr(inode, stat);
1090
1091 rcu_read_lock();
1092 stat->uid = 0;
1093 stat->gid = 0;
1094 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1095 if (task) {
1096 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1097 task_dumpable(task)) {
1098 stat->uid = task->euid;
1099 stat->gid = task->egid;
1100 }
1101 }
1102 rcu_read_unlock();
1103 return 0;
1104}
1105
1106/* dentry stuff */
1107
1108/*
1109 * Exceptional case: normally we are not allowed to unhash a busy
1110 * directory. In this case, however, we can do it - no aliasing problems
1111 * due to the way we treat inodes.
1112 *
1113 * Rewrite the inode's ownerships here because the owning task may have
1114 * performed a setuid(), etc.
1115 *
1116 * Before the /proc/pid/status file was created the only way to read
1117 * the effective uid of a /process was to stat /proc/pid. Reading
1118 * /proc/pid/status is slow enough that procps and other packages
1119 * kept stating /proc/pid. To keep the rules in /proc simple I have
1120 * made this apply to all per process world readable and executable
1121 * directories.
1122 */
1123static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1124{
1125 struct inode *inode = dentry->d_inode;
1126 struct task_struct *task = get_proc_task(inode);
1127 if (task) {
1128 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1129 task_dumpable(task)) {
1130 inode->i_uid = task->euid;
1131 inode->i_gid = task->egid;
1132 } else {
1133 inode->i_uid = 0;
1134 inode->i_gid = 0;
1135 }
1136 inode->i_mode &= ~(S_ISUID | S_ISGID);
1137 security_task_to_inode(task, inode);
1138 put_task_struct(task);
1139 return 1;
1140 }
1141 d_drop(dentry);
1142 return 0;
1143}
1144
1145static int pid_delete_dentry(struct dentry * dentry)
1146{
1147 /* Is the task we represent dead?
1148 * If so, then don't put the dentry on the lru list,
1149 * kill it immediately.
1150 */
1151 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1152}
1153
1154static struct dentry_operations pid_dentry_operations =
1155{
1156 .d_revalidate = pid_revalidate,
1157 .d_delete = pid_delete_dentry,
1158};
1159
1160/* Lookups */
1161
1162static unsigned name_to_int(struct dentry *dentry)
1163{
1164 const char *name = dentry->d_name.name;
1165 int len = dentry->d_name.len;
1166 unsigned n = 0;
1167
1168 if (len > 1 && *name == '0')
1169 goto out;
1170 while (len-- > 0) {
1171 unsigned c = *name++ - '0';
1172 if (c > 9)
1173 goto out;
1174 if (n >= (~0U-9)/10)
1175 goto out;
1176 n *= 10;
1177 n += c;
1178 }
1179 return n;
1180out:
1181 return ~0U;
1182}
1183
1184static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1185{
1186 struct task_struct *task = get_proc_task(inode);
1187 struct files_struct *files = NULL;
1188 struct file *file;
1189 int fd = proc_fd(inode);
1190
1191 if (task) {
1192 files = get_files_struct(task);
1193 put_task_struct(task);
1194 }
1195 if (files) {
1196 /*
1197 * We are not taking a ref to the file structure, so we must
1198 * hold ->file_lock.
1199 */
1200 spin_lock(&files->file_lock);
1201 file = fcheck_files(files, fd);
1202 if (file) {
1203 *mnt = mntget(file->f_vfsmnt);
1204 *dentry = dget(file->f_dentry);
1205 spin_unlock(&files->file_lock);
1206 put_files_struct(files);
1207 return 0;
1208 }
1209 spin_unlock(&files->file_lock);
1210 put_files_struct(files);
1211 }
1212 return -ENOENT;
1213}
1214
1215static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1216{
1217 struct inode *inode = dentry->d_inode;
1218 struct task_struct *task = get_proc_task(inode);
1219 int fd = proc_fd(inode);
1220 struct files_struct *files;
1221
1222 if (task) {
1223 files = get_files_struct(task);
1224 if (files) {
1225 rcu_read_lock();
1226 if (fcheck_files(files, fd)) {
1227 rcu_read_unlock();
1228 put_files_struct(files);
1229 if (task_dumpable(task)) {
1230 inode->i_uid = task->euid;
1231 inode->i_gid = task->egid;
1232 } else {
1233 inode->i_uid = 0;
1234 inode->i_gid = 0;
1235 }
1236 inode->i_mode &= ~(S_ISUID | S_ISGID);
1237 security_task_to_inode(task, inode);
1238 put_task_struct(task);
1239 return 1;
1240 }
1241 rcu_read_unlock();
1242 put_files_struct(files);
1243 }
1244 put_task_struct(task);
1245 }
1246 d_drop(dentry);
1247 return 0;
1248}
1249
1250static struct dentry_operations tid_fd_dentry_operations =
1251{
1252 .d_revalidate = tid_fd_revalidate,
1253 .d_delete = pid_delete_dentry,
1254};
1255
1256/* SMP-safe */
1257static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1258{
1259 struct task_struct *task = get_proc_task(dir);
1260 unsigned fd = name_to_int(dentry);
1261 struct dentry *result = ERR_PTR(-ENOENT);
1262 struct file * file;
1263 struct files_struct * files;
1264 struct inode *inode;
1265 struct proc_inode *ei;
1266
1267 if (!task)
1268 goto out_no_task;
1269 if (fd == ~0U)
1270 goto out;
1271
1272 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1273 if (!inode)
1274 goto out;
1275 ei = PROC_I(inode);
1276 ei->fd = fd;
1277 files = get_files_struct(task);
1278 if (!files)
1279 goto out_unlock;
1280 inode->i_mode = S_IFLNK;
1281
1282 /*
1283 * We are not taking a ref to the file structure, so we must
1284 * hold ->file_lock.
1285 */
1286 spin_lock(&files->file_lock);
1287 file = fcheck_files(files, fd);
1288 if (!file)
1289 goto out_unlock2;
1290 if (file->f_mode & 1)
1291 inode->i_mode |= S_IRUSR | S_IXUSR;
1292 if (file->f_mode & 2)
1293 inode->i_mode |= S_IWUSR | S_IXUSR;
1294 spin_unlock(&files->file_lock);
1295 put_files_struct(files);
1296 inode->i_op = &proc_pid_link_inode_operations;
1297 inode->i_size = 64;
1298 ei->op.proc_get_link = proc_fd_link;
1299 dentry->d_op = &tid_fd_dentry_operations;
1300 d_add(dentry, inode);
1301 /* Close the race of the process dying before we return the dentry */
1302 if (tid_fd_revalidate(dentry, NULL))
1303 result = NULL;
1304out:
1305 put_task_struct(task);
1306out_no_task:
1307 return result;
1308
1309out_unlock2:
1310 spin_unlock(&files->file_lock);
1311 put_files_struct(files);
1312out_unlock:
1313 iput(inode);
1314 goto out;
1315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1318{
Eric W. Biederman56347082006-06-26 00:25:40 -07001319 struct dentry *dentry = filp->f_dentry;
1320 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001321 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 unsigned int fd, tid, ino;
1323 int retval;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001324 char buf[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001326 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001329 if (!p)
1330 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 retval = 0;
1332 tid = p->pid;
1333
1334 fd = filp->f_pos;
1335 switch (fd) {
1336 case 0:
1337 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1338 goto out;
1339 filp->f_pos++;
1340 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001341 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1343 goto out;
1344 filp->f_pos++;
1345 default:
1346 files = get_files_struct(p);
1347 if (!files)
1348 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001349 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001350 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001352 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 fd++, filp->f_pos++) {
1354 unsigned int i,j;
1355
1356 if (!fcheck_files(files, fd))
1357 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001358 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001360 j = PROC_NUMBUF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 i = fd;
1362 do {
1363 j--;
1364 buf[j] = '0' + (i % 10);
1365 i /= 10;
1366 } while (i);
1367
1368 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001369 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001370 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 break;
1372 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001373 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001375 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 put_files_struct(files);
1377 }
1378out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001379 put_task_struct(p);
1380out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return retval;
1382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384static struct file_operations proc_fd_operations = {
1385 .read = generic_read_dir,
1386 .readdir = proc_readfd,
1387};
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/*
1390 * proc directories can do almost nothing..
1391 */
1392static struct inode_operations proc_fd_inode_operations = {
1393 .lookup = proc_lookupfd,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001394 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395};
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397/* SMP-safe */
1398static struct dentry *proc_pident_lookup(struct inode *dir,
1399 struct dentry *dentry,
1400 struct pid_entry *ents)
1401{
1402 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001403 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001404 struct task_struct *task = get_proc_task(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 struct pid_entry *p;
1406 struct proc_inode *ei;
1407
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001408 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 inode = NULL;
1410
Eric W. Biederman99f89552006-06-26 00:25:55 -07001411 if (!task)
1412 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001414 /*
1415 * Yes, it does not scale. And it should not. Don't add
1416 * new entries into /proc/<tgid>/ without very good reasons.
1417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 for (p = ents; p->name; p++) {
1419 if (p->len != dentry->d_name.len)
1420 continue;
1421 if (!memcmp(dentry->d_name.name, p->name, p->len))
1422 break;
1423 }
1424 if (!p->name)
1425 goto out;
1426
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001427 error = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1429 if (!inode)
1430 goto out;
1431
1432 ei = PROC_I(inode);
1433 inode->i_mode = p->mode;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001434 if (S_ISDIR(inode->i_mode))
1435 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1436 if (p->iop)
1437 inode->i_op = p->iop;
1438 if (p->fop)
1439 inode->i_fop = p->fop;
1440 ei->op = p->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 dentry->d_op = &pid_dentry_operations;
1442 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001443 /* Close the race of the process dying before we return the dentry */
1444 if (pid_revalidate(dentry, NULL))
1445 error = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001447 put_task_struct(task);
1448out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001449 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001452static int proc_pident_readdir(struct file *filp,
1453 void *dirent, filldir_t filldir,
1454 struct pid_entry *ents, unsigned int nents)
1455{
1456 int i;
1457 int pid;
1458 struct dentry *dentry = filp->f_dentry;
1459 struct inode *inode = dentry->d_inode;
1460 struct task_struct *task = get_proc_task(inode);
1461 struct pid_entry *p;
1462 ino_t ino;
1463 int ret;
1464
1465 ret = -ENOENT;
1466 if (!task)
1467 goto out;
1468
1469 ret = 0;
1470 pid = task->pid;
1471 put_task_struct(task);
1472 i = filp->f_pos;
1473 switch (i) {
1474 case 0:
1475 ino = inode->i_ino;
1476 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1477 goto out;
1478 i++;
1479 filp->f_pos++;
1480 /* fall through */
1481 case 1:
1482 ino = parent_ino(dentry);
1483 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1484 goto out;
1485 i++;
1486 filp->f_pos++;
1487 /* fall through */
1488 default:
1489 i -= 2;
1490 if (i >= nents) {
1491 ret = 1;
1492 goto out;
1493 }
1494 p = ents + i;
1495 while (p->name) {
1496 if (filldir(dirent, p->name, p->len, filp->f_pos,
1497 fake_ino(pid, p->type), p->mode >> 12) < 0)
1498 goto out;
1499 filp->f_pos++;
1500 p++;
1501 }
1502 }
1503
1504 ret = 1;
1505out:
1506 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001510static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1511 size_t count, loff_t *ppos)
1512{
1513 struct inode * inode = file->f_dentry->d_inode;
1514 unsigned long page;
1515 ssize_t length;
1516 struct task_struct *task = get_proc_task(inode);
1517
1518 length = -ESRCH;
1519 if (!task)
1520 goto out_no_task;
1521
1522 if (count > PAGE_SIZE)
1523 count = PAGE_SIZE;
1524 length = -ENOMEM;
1525 if (!(page = __get_free_page(GFP_KERNEL)))
1526 goto out;
1527
1528 length = security_getprocattr(task,
1529 (char*)file->f_dentry->d_name.name,
1530 (void*)page, count);
1531 if (length >= 0)
1532 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1533 free_page(page);
1534out:
1535 put_task_struct(task);
1536out_no_task:
1537 return length;
1538}
1539
1540static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1541 size_t count, loff_t *ppos)
1542{
1543 struct inode * inode = file->f_dentry->d_inode;
1544 char *page;
1545 ssize_t length;
1546 struct task_struct *task = get_proc_task(inode);
1547
1548 length = -ESRCH;
1549 if (!task)
1550 goto out_no_task;
1551 if (count > PAGE_SIZE)
1552 count = PAGE_SIZE;
1553
1554 /* No partial writes. */
1555 length = -EINVAL;
1556 if (*ppos != 0)
1557 goto out;
1558
1559 length = -ENOMEM;
1560 page = (char*)__get_free_page(GFP_USER);
1561 if (!page)
1562 goto out;
1563
1564 length = -EFAULT;
1565 if (copy_from_user(page, buf, count))
1566 goto out_free;
1567
1568 length = security_setprocattr(task,
1569 (char*)file->f_dentry->d_name.name,
1570 (void*)page, count);
1571out_free:
1572 free_page((unsigned long) page);
1573out:
1574 put_task_struct(task);
1575out_no_task:
1576 return length;
1577}
1578
1579static struct file_operations proc_pid_attr_operations = {
1580 .read = proc_pid_attr_read,
1581 .write = proc_pid_attr_write,
1582};
1583
1584static struct pid_entry tgid_attr_stuff[] = {
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001585 REG(PROC_TGID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1586 REG(PROC_TGID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1587 REG(PROC_TGID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1588 REG(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1589 REG(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1590 REG(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1591 {}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001592};
1593static struct pid_entry tid_attr_stuff[] = {
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001594 REG(PROC_TID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1595 REG(PROC_TID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1596 REG(PROC_TID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1597 REG(PROC_TID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1598 REG(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1599 REG(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1600 {}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001601};
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603static int proc_tgid_attr_readdir(struct file * filp,
1604 void * dirent, filldir_t filldir)
1605{
1606 return proc_pident_readdir(filp,dirent,filldir,
1607 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1608}
1609
1610static int proc_tid_attr_readdir(struct file * filp,
1611 void * dirent, filldir_t filldir)
1612{
1613 return proc_pident_readdir(filp,dirent,filldir,
1614 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1615}
1616
1617static struct file_operations proc_tgid_attr_operations = {
1618 .read = generic_read_dir,
1619 .readdir = proc_tgid_attr_readdir,
1620};
1621
1622static struct file_operations proc_tid_attr_operations = {
1623 .read = generic_read_dir,
1624 .readdir = proc_tid_attr_readdir,
1625};
1626
1627static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1628 struct dentry *dentry, struct nameidata *nd)
1629{
1630 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1631}
1632
1633static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1634 struct dentry *dentry, struct nameidata *nd)
1635{
1636 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1637}
1638
1639static struct inode_operations proc_tgid_attr_inode_operations = {
1640 .lookup = proc_tgid_attr_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001641 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001642 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643};
1644
1645static struct inode_operations proc_tid_attr_inode_operations = {
1646 .lookup = proc_tid_attr_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001647 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001648 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649};
1650#endif
1651
1652/*
1653 * /proc/self:
1654 */
1655static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1656 int buflen)
1657{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001658 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 sprintf(tmp, "%d", current->tgid);
1660 return vfs_readlink(dentry,buffer,buflen,tmp);
1661}
1662
Al Viro008b1502005-08-20 00:17:39 +01001663static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001665 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001667 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001668}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670static struct inode_operations proc_self_inode_operations = {
1671 .readlink = proc_self_readlink,
1672 .follow_link = proc_self_follow_link,
1673};
1674
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001675/*
1676 * Thread groups
1677 */
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001678static struct file_operations proc_task_operations;
1679static struct inode_operations proc_task_inode_operations;
1680
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001681static struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001682 DIR(PROC_TGID_TASK, "task", S_IRUGO|S_IXUGO, task),
1683 DIR(PROC_TGID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1684 INF(PROC_TGID_ENVIRON, "environ", S_IRUSR, pid_environ),
1685 INF(PROC_TGID_AUXV, "auxv", S_IRUSR, pid_auxv),
1686 INF(PROC_TGID_STATUS, "status", S_IRUGO, pid_status),
1687 INF(PROC_TGID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1688 INF(PROC_TGID_STAT, "stat", S_IRUGO, tgid_stat),
1689 INF(PROC_TGID_STATM, "statm", S_IRUGO, pid_statm),
1690 REG(PROC_TGID_MAPS, "maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001691#ifdef CONFIG_NUMA
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001692 REG(PROC_TGID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001693#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001694 REG(PROC_TGID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001695#ifdef CONFIG_SECCOMP
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001696 REG(PROC_TGID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001697#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001698 LNK(PROC_TGID_CWD, "cwd", cwd),
1699 LNK(PROC_TGID_ROOT, "root", root),
1700 LNK(PROC_TGID_EXE, "exe", exe),
1701 REG(PROC_TGID_MOUNTS, "mounts", S_IRUGO, mounts),
1702 REG(PROC_TGID_MOUNTSTATS, "mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001703#ifdef CONFIG_MMU
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001704 REG(PROC_TGID_SMAPS, "smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001705#endif
1706#ifdef CONFIG_SECURITY
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001707 DIR(PROC_TGID_ATTR, "attr", S_IRUGO|S_IXUGO, tgid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001708#endif
1709#ifdef CONFIG_KALLSYMS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001710 INF(PROC_TGID_WCHAN, "wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001711#endif
1712#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001713 INF(PROC_TGID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001714#endif
1715#ifdef CONFIG_CPUSETS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001716 REG(PROC_TGID_CPUSET, "cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001717#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001718 INF(PROC_TGID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1719 REG(PROC_TGID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001720#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001721 REG(PROC_TGID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001722#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001723 {}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001724};
1725
1726static int proc_tgid_base_readdir(struct file * filp,
1727 void * dirent, filldir_t filldir)
1728{
1729 return proc_pident_readdir(filp,dirent,filldir,
1730 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1731}
1732
1733static struct file_operations proc_tgid_base_operations = {
1734 .read = generic_read_dir,
1735 .readdir = proc_tgid_base_readdir,
1736};
1737
1738static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1739 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1740}
1741
1742static struct inode_operations proc_tgid_base_inode_operations = {
1743 .lookup = proc_tgid_base_lookup,
1744 .getattr = pid_getattr,
1745 .setattr = proc_setattr,
1746};
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001749 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001751 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001753 * Looks in the dcache for
1754 * /proc/@pid
1755 * /proc/@tgid/task/@pid
1756 * if either directory is present flushes it and all of it'ts children
1757 * from the dcache.
1758 *
1759 * It is safe and reasonable to cache /proc entries for a task until
1760 * that task exits. After that they just clog up the dcache with
1761 * useless entries, possibly causing useful dcache entries to be
1762 * flushed instead. This routine is proved to flush those useless
1763 * dcache entries at process exit time.
1764 *
1765 * NOTE: This routine is just an optimization so it does not guarantee
1766 * that no dcache entries will exist at process exit time it
1767 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001769void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001771 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001772 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001773 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Eric W. Biederman48e64842006-06-26 00:25:48 -07001775 name.name = buf;
1776 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1777 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1778 if (dentry) {
1779 shrink_dcache_parent(dentry);
1780 d_drop(dentry);
1781 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Eric W. Biederman48e64842006-06-26 00:25:48 -07001784 if (thread_group_leader(task))
1785 goto out;
1786
1787 name.name = buf;
1788 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1789 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1790 if (!leader)
1791 goto out;
1792
1793 name.name = "task";
1794 name.len = strlen(name.name);
1795 dir = d_hash_and_lookup(leader, &name);
1796 if (!dir)
1797 goto out_put_leader;
1798
1799 name.name = buf;
1800 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1801 dentry = d_hash_and_lookup(dir, &name);
1802 if (dentry) {
1803 shrink_dcache_parent(dentry);
1804 d_drop(dentry);
1805 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001807
1808 dput(dir);
1809out_put_leader:
1810 dput(leader);
1811out:
1812 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815/* SMP-safe */
1816struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1817{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001818 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct task_struct *task;
1820 struct inode *inode;
1821 struct proc_inode *ei;
1822 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1825 inode = new_inode(dir->i_sb);
1826 if (!inode)
1827 return ERR_PTR(-ENOMEM);
1828 ei = PROC_I(inode);
1829 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1830 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1831 ei->pde = NULL;
1832 inode->i_mode = S_IFLNK|S_IRWXUGO;
1833 inode->i_uid = inode->i_gid = 0;
1834 inode->i_size = 64;
1835 inode->i_op = &proc_self_inode_operations;
1836 d_add(dentry, inode);
1837 return NULL;
1838 }
1839 tgid = name_to_int(dentry);
1840 if (tgid == ~0U)
1841 goto out;
1842
Eric W. Biedermande758732006-06-26 00:25:51 -07001843 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 task = find_task_by_pid(tgid);
1845 if (task)
1846 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07001847 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (!task)
1849 goto out;
1850
1851 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001852 if (!inode)
1853 goto out_put_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1856 inode->i_op = &proc_tgid_base_inode_operations;
1857 inode->i_fop = &proc_tgid_base_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 inode->i_flags|=S_IMMUTABLE;
Daniel Drakebcf88e12005-05-01 08:59:03 -07001859#ifdef CONFIG_SECURITY
1860 inode->i_nlink = 5;
1861#else
1862 inode->i_nlink = 4;
1863#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Eric W. Biederman48e64842006-06-26 00:25:48 -07001865 dentry->d_op = &pid_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001868 /* Close the race of the process dying before we return the dentry */
1869 if (pid_revalidate(dentry, NULL))
1870 result = NULL;
Eric W. Biederman48e64842006-06-26 00:25:48 -07001871
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001872out_put_task:
Eric W. Biederman48e64842006-06-26 00:25:48 -07001873 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001875 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001879 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001880 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001882static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001884 struct task_struct *task;
1885 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001887 rcu_read_lock();
1888retry:
1889 task = NULL;
1890 pid = find_ge_pid(tgid);
1891 if (pid) {
1892 tgid = pid->nr + 1;
1893 task = pid_task(pid, PIDTYPE_PID);
1894 /* What we to know is if the pid we have find is the
1895 * pid of a thread_group_leader. Testing for task
1896 * being a thread_group_leader is the obvious thing
1897 * todo but there is a window when it fails, due to
1898 * the pid transfer logic in de_thread.
1899 *
1900 * So we perform the straight forward test of seeing
1901 * if the pid we have found is the pid of a thread
1902 * group leader, and don't worry if the task we have
1903 * found doesn't happen to be a thread group leader.
1904 * As we don't care in the case of readdir.
1905 */
1906 if (!task || !has_group_leader_pid(task))
1907 goto retry;
1908 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07001910 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001911 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001914#define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916/* for the /proc/ directory itself, after non-process stuff has been done */
1917int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1918{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 char buf[PROC_NUMBUF];
1920 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001921 struct task_struct *task;
1922 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 if (!nr) {
1925 ino_t ino = fake_ino(0,PROC_TGID_INO);
1926 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1927 return 0;
1928 filp->f_pos++;
1929 nr++;
1930 }
1931
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001932 tgid = filp->f_pos - TGID_OFFSET;
1933 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001934 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001935 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001936 int len;
1937 ino_t ino;
1938 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001939 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001940 len = snprintf(buf, sizeof(buf), "%d", tgid);
1941 ino = fake_ino(tgid, PROC_TGID_INO);
1942 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001943 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001944 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001947 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
1948out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return 0;
1950}
1951
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001952/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001953 * Tasks
1954 */
1955static struct pid_entry tid_base_stuff[] = {
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001956 DIR(PROC_TID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1957 INF(PROC_TID_ENVIRON, "environ", S_IRUSR, pid_environ),
1958 INF(PROC_TID_AUXV, "auxv", S_IRUSR, pid_auxv),
1959 INF(PROC_TID_STATUS, "status", S_IRUGO, pid_status),
1960 INF(PROC_TID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1961 INF(PROC_TID_STAT, "stat", S_IRUGO, tid_stat),
1962 INF(PROC_TID_STATM, "statm", S_IRUGO, pid_statm),
1963 REG(PROC_TID_MAPS, "maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001964#ifdef CONFIG_NUMA
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001965 REG(PROC_TID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001966#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001967 REG(PROC_TID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001968#ifdef CONFIG_SECCOMP
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001969 REG(PROC_TID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001970#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001971 LNK(PROC_TID_CWD, "cwd", cwd),
1972 LNK(PROC_TID_ROOT, "root", root),
1973 LNK(PROC_TID_EXE, "exe", exe),
1974 REG(PROC_TID_MOUNTS, "mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001975#ifdef CONFIG_MMU
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001976 REG(PROC_TID_SMAPS, "smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001977#endif
1978#ifdef CONFIG_SECURITY
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001979 DIR(PROC_TID_ATTR, "attr", S_IRUGO|S_IXUGO, tid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001980#endif
1981#ifdef CONFIG_KALLSYMS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001982 INF(PROC_TID_WCHAN, "wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001983#endif
1984#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001985 INF(PROC_TID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001986#endif
1987#ifdef CONFIG_CPUSETS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001988 REG(PROC_TID_CPUSET, "cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001989#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001990 INF(PROC_TID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1991 REG(PROC_TID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001992#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001993 REG(PROC_TID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001994#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001995 {}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001996};
1997
1998static int proc_tid_base_readdir(struct file * filp,
1999 void * dirent, filldir_t filldir)
2000{
2001 return proc_pident_readdir(filp,dirent,filldir,
2002 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2003}
2004
2005static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2006 return proc_pident_lookup(dir, dentry, tid_base_stuff);
2007}
2008
2009static struct file_operations proc_tid_base_operations = {
2010 .read = generic_read_dir,
2011 .readdir = proc_tid_base_readdir,
2012};
2013
2014static struct inode_operations proc_tid_base_inode_operations = {
2015 .lookup = proc_tid_base_lookup,
2016 .getattr = pid_getattr,
2017 .setattr = proc_setattr,
2018};
2019
2020/* SMP-safe */
2021static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2022{
2023 struct dentry *result = ERR_PTR(-ENOENT);
2024 struct task_struct *task;
2025 struct task_struct *leader = get_proc_task(dir);
2026 struct inode *inode;
2027 unsigned tid;
2028
2029 if (!leader)
2030 goto out_no_task;
2031
2032 tid = name_to_int(dentry);
2033 if (tid == ~0U)
2034 goto out;
2035
2036 rcu_read_lock();
2037 task = find_task_by_pid(tid);
2038 if (task)
2039 get_task_struct(task);
2040 rcu_read_unlock();
2041 if (!task)
2042 goto out;
2043 if (leader->tgid != task->tgid)
2044 goto out_drop_task;
2045
2046 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2047
2048
2049 if (!inode)
2050 goto out_drop_task;
2051 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2052 inode->i_op = &proc_tid_base_inode_operations;
2053 inode->i_fop = &proc_tid_base_operations;
2054 inode->i_flags|=S_IMMUTABLE;
2055#ifdef CONFIG_SECURITY
2056 inode->i_nlink = 4;
2057#else
2058 inode->i_nlink = 3;
2059#endif
2060
2061 dentry->d_op = &pid_dentry_operations;
2062
2063 d_add(dentry, inode);
2064 /* Close the race of the process dying before we return the dentry */
2065 if (pid_revalidate(dentry, NULL))
2066 result = NULL;
2067
2068out_drop_task:
2069 put_task_struct(task);
2070out:
2071 put_task_struct(leader);
2072out_no_task:
2073 return result;
2074}
2075
2076/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002077 * Find the first tid of a thread group to return to user space.
2078 *
2079 * Usually this is just the thread group leader, but if the users
2080 * buffer was too small or there was a seek into the middle of the
2081 * directory we have more work todo.
2082 *
2083 * In the case of a short read we start with find_task_by_pid.
2084 *
2085 * In the case of a seek we start with the leader and walk nr
2086 * threads past it.
2087 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002088static struct task_struct *first_tid(struct task_struct *leader,
2089 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002090{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002091 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002092
Eric W. Biedermancc288732006-06-26 00:26:01 -07002093 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002094 /* Attempt to start with the pid of a thread */
2095 if (tid && (nr > 0)) {
2096 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002097 if (pos && (pos->group_leader == leader))
2098 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002099 }
2100
2101 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002102 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002103 if (nr && nr >= get_nr_threads(leader))
2104 goto out;
2105
2106 /* If we haven't found our starting place yet start
2107 * with the leader and walk nr threads forward.
2108 */
2109 for (pos = leader; nr > 0; --nr) {
2110 pos = next_thread(pos);
2111 if (pos == leader) {
2112 pos = NULL;
2113 goto out;
2114 }
2115 }
2116found:
2117 get_task_struct(pos);
2118out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002119 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002120 return pos;
2121}
2122
2123/*
2124 * Find the next thread in the thread list.
2125 * Return NULL if there is an error or no next thread.
2126 *
2127 * The reference to the input task_struct is released.
2128 */
2129static struct task_struct *next_tid(struct task_struct *start)
2130{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002131 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002132 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002133 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002134 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002135 if (thread_group_leader(pos))
2136 pos = NULL;
2137 else
2138 get_task_struct(pos);
2139 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002140 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002141 put_task_struct(start);
2142 return pos;
2143}
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145/* for the /proc/TGID/task/ directories */
2146static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 char buf[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 struct dentry *dentry = filp->f_dentry;
2150 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002151 struct task_struct *leader = get_proc_task(inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002152 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 int retval = -ENOENT;
2154 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002155 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2157
Eric W. Biederman99f89552006-06-26 00:25:55 -07002158 if (!leader)
2159 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 retval = 0;
2161
2162 switch (pos) {
2163 case 0:
2164 ino = inode->i_ino;
2165 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2166 goto out;
2167 pos++;
2168 /* fall through */
2169 case 1:
2170 ino = parent_ino(dentry);
2171 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2172 goto out;
2173 pos++;
2174 /* fall through */
2175 }
2176
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002177 /* f_version caches the tgid value that the last readdir call couldn't
2178 * return. lseek aka telldir automagically resets f_version to 0.
2179 */
2180 tid = filp->f_version;
2181 filp->f_version = 0;
2182 for (task = first_tid(leader, tid, pos - 2);
2183 task;
2184 task = next_tid(task), pos++) {
2185 int len;
2186 tid = task->pid;
2187 len = snprintf(buf, sizeof(buf), "%d", tid);
2188 ino = fake_ino(tid, PROC_TID_INO);
2189 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2190 /* returning this tgid failed, save it as the first
2191 * pid for the next readir call */
2192 filp->f_version = tid;
2193 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197out:
2198 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002199 put_task_struct(leader);
2200out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return retval;
2202}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002203
2204static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2205{
2206 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002207 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002208 generic_fillattr(inode, stat);
2209
Eric W. Biederman99f89552006-06-26 00:25:55 -07002210 if (p) {
2211 rcu_read_lock();
2212 stat->nlink += get_nr_threads(p);
2213 rcu_read_unlock();
2214 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002215 }
2216
2217 return 0;
2218}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002219
2220static struct inode_operations proc_task_inode_operations = {
2221 .lookup = proc_task_lookup,
2222 .getattr = proc_task_getattr,
2223 .setattr = proc_setattr,
2224};
2225
2226static struct file_operations proc_task_operations = {
2227 .read = generic_read_dir,
2228 .readdir = proc_task_readdir,
2229};