blob: 7832efbd43a66bb684c340f73092b7e9f111a4ad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
62#include <linux/namespace.h>
63#include <linux/mm.h>
64#include <linux/smp_lock.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070065#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kallsyms.h>
67#include <linux/mount.h>
68#include <linux/security.h>
69#include <linux/ptrace.h>
70#include <linux/seccomp.h>
71#include <linux/cpuset.h>
72#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050073#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070074#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include "internal.h"
76
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070077/* NOTE:
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
82 *
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
85 */
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * For hysterical raisins we keep the same inumbers as in the old procfs.
89 * Feel free to change the macro below - just keep the range distinct from
90 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
91 * As soon as we'll get a separate superblock we will be able to forget
92 * about magical ranges too.
93 */
94
95#define fake_ino(pid,ino) (((pid)<<16)|(ino))
96
97enum pid_directory_inos {
98 PROC_TGID_INO = 2,
99 PROC_TGID_TASK,
100 PROC_TGID_STATUS,
101 PROC_TGID_MEM,
102#ifdef CONFIG_SECCOMP
103 PROC_TGID_SECCOMP,
104#endif
105 PROC_TGID_CWD,
106 PROC_TGID_ROOT,
107 PROC_TGID_EXE,
108 PROC_TGID_FD,
109 PROC_TGID_ENVIRON,
110 PROC_TGID_AUXV,
111 PROC_TGID_CMDLINE,
112 PROC_TGID_STAT,
113 PROC_TGID_STATM,
114 PROC_TGID_MAPS,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700115 PROC_TGID_NUMA_MAPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 PROC_TGID_MOUNTS,
Chuck Leverb4629fe2006-03-20 13:44:12 -0500117 PROC_TGID_MOUNTSTATS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 PROC_TGID_WCHAN,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700119#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -0700120 PROC_TGID_SMAPS,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#ifdef CONFIG_SCHEDSTATS
123 PROC_TGID_SCHEDSTAT,
124#endif
125#ifdef CONFIG_CPUSETS
126 PROC_TGID_CPUSET,
127#endif
128#ifdef CONFIG_SECURITY
129 PROC_TGID_ATTR,
130 PROC_TGID_ATTR_CURRENT,
131 PROC_TGID_ATTR_PREV,
132 PROC_TGID_ATTR_EXEC,
133 PROC_TGID_ATTR_FSCREATE,
Michael LeMay4eb582c2006-06-26 00:24:57 -0700134 PROC_TGID_ATTR_KEYCREATE,
Eric Paris42c3e032006-06-26 00:26:03 -0700135 PROC_TGID_ATTR_SOCKCREATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#endif
137#ifdef CONFIG_AUDITSYSCALL
138 PROC_TGID_LOGINUID,
139#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 PROC_TGID_OOM_SCORE,
141 PROC_TGID_OOM_ADJUST,
142 PROC_TID_INO,
143 PROC_TID_STATUS,
144 PROC_TID_MEM,
145#ifdef CONFIG_SECCOMP
146 PROC_TID_SECCOMP,
147#endif
148 PROC_TID_CWD,
149 PROC_TID_ROOT,
150 PROC_TID_EXE,
151 PROC_TID_FD,
152 PROC_TID_ENVIRON,
153 PROC_TID_AUXV,
154 PROC_TID_CMDLINE,
155 PROC_TID_STAT,
156 PROC_TID_STATM,
157 PROC_TID_MAPS,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700158 PROC_TID_NUMA_MAPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 PROC_TID_MOUNTS,
Chuck Leverb4629fe2006-03-20 13:44:12 -0500160 PROC_TID_MOUNTSTATS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 PROC_TID_WCHAN,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700162#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -0700163 PROC_TID_SMAPS,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifdef CONFIG_SCHEDSTATS
166 PROC_TID_SCHEDSTAT,
167#endif
168#ifdef CONFIG_CPUSETS
169 PROC_TID_CPUSET,
170#endif
171#ifdef CONFIG_SECURITY
172 PROC_TID_ATTR,
173 PROC_TID_ATTR_CURRENT,
174 PROC_TID_ATTR_PREV,
175 PROC_TID_ATTR_EXEC,
176 PROC_TID_ATTR_FSCREATE,
Michael LeMay4eb582c2006-06-26 00:24:57 -0700177 PROC_TID_ATTR_KEYCREATE,
Eric Paris42c3e032006-06-26 00:26:03 -0700178 PROC_TID_ATTR_SOCKCREATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#endif
180#ifdef CONFIG_AUDITSYSCALL
181 PROC_TID_LOGINUID,
182#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 PROC_TID_OOM_SCORE,
184 PROC_TID_OOM_ADJUST,
Miklos Szeredi5e21ccb2005-09-06 15:18:23 -0700185
186 /* Add new entries before this */
187 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700190/* Worst case buffer size needed for holding an integer. */
191#define PROC_NUMBUF 10
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193struct pid_entry {
194 int type;
195 int len;
196 char *name;
197 mode_t mode;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700198 struct inode_operations *iop;
199 struct file_operations *fop;
200 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700203#define NOD(TYPE, NAME, MODE, IOP, FOP, OP) { \
204 .type = (TYPE), \
205 .len = sizeof(NAME) - 1, \
206 .name = (NAME), \
207 .mode = MODE, \
208 .iop = IOP, \
209 .fop = FOP, \
210 .op = OP, \
211}
212
213#define DIR(TYPE, NAME, MODE, OTYPE) \
214 NOD(TYPE, NAME, (S_IFDIR|(MODE)), \
215 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
216 {} )
217#define LNK(TYPE, NAME, OTYPE) \
218 NOD(TYPE, NAME, (S_IFLNK|S_IRWXUGO), \
219 &proc_pid_link_inode_operations, NULL, \
220 { .proc_get_link = &proc_##OTYPE##_link } )
221#define REG(TYPE, NAME, MODE, OTYPE) \
222 NOD(TYPE, NAME, (S_IFREG|(MODE)), NULL, \
223 &proc_##OTYPE##_operations, {})
224#define INF(TYPE, NAME, MODE, OTYPE) \
225 NOD(TYPE, NAME, (S_IFREG|(MODE)), \
226 NULL, &proc_info_file_operations, \
227 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700229static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700232 task_lock(task);
233 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if(fs)
235 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700236 task_unlock(task);
237 return fs;
238}
239
Eric W. Biederman99f89552006-06-26 00:25:55 -0700240static int get_nr_threads(struct task_struct *tsk)
241{
242 /* Must be called with the rcu_read_lock held */
243 unsigned long flags;
244 int count = 0;
245
246 if (lock_task_sighand(tsk, &flags)) {
247 count = atomic_read(&tsk->signal->count);
248 unlock_task_sighand(tsk, &flags);
249 }
250 return count;
251}
252
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700253static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
254{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700255 struct task_struct *task = get_proc_task(inode);
256 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700257 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700258
259 if (task) {
260 fs = get_fs_struct(task);
261 put_task_struct(task);
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (fs) {
264 read_lock(&fs->lock);
265 *mnt = mntget(fs->pwdmnt);
266 *dentry = dget(fs->pwd);
267 read_unlock(&fs->lock);
268 result = 0;
269 put_fs_struct(fs);
270 }
271 return result;
272}
273
274static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
275{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700276 struct task_struct *task = get_proc_task(inode);
277 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700279
280 if (task) {
281 fs = get_fs_struct(task);
282 put_task_struct(task);
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (fs) {
285 read_lock(&fs->lock);
286 *mnt = mntget(fs->rootmnt);
287 *dentry = dget(fs->root);
288 read_unlock(&fs->lock);
289 result = 0;
290 put_fs_struct(fs);
291 }
292 return result;
293}
294
295#define MAY_PTRACE(task) \
296 (task == current || \
297 (task->parent == current && \
298 (task->ptrace & PT_PTRACED) && \
299 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
300 security_ptrace(current,task) == 0))
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static int proc_pid_environ(struct task_struct *task, char * buffer)
303{
304 int res = 0;
305 struct mm_struct *mm = get_task_mm(task);
306 if (mm) {
307 unsigned int len = mm->env_end - mm->env_start;
308 if (len > PAGE_SIZE)
309 len = PAGE_SIZE;
310 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700311 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 res = -ESRCH;
313 mmput(mm);
314 }
315 return res;
316}
317
318static int proc_pid_cmdline(struct task_struct *task, char * buffer)
319{
320 int res = 0;
321 unsigned int len;
322 struct mm_struct *mm = get_task_mm(task);
323 if (!mm)
324 goto out;
325 if (!mm->arg_end)
326 goto out_mm; /* Shh! No looking before we're done */
327
328 len = mm->arg_end - mm->arg_start;
329
330 if (len > PAGE_SIZE)
331 len = PAGE_SIZE;
332
333 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
334
335 // If the nul at the end of args has been overwritten, then
336 // assume application is using setproctitle(3).
337 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
338 len = strnlen(buffer, res);
339 if (len < res) {
340 res = len;
341 } else {
342 len = mm->env_end - mm->env_start;
343 if (len > PAGE_SIZE - res)
344 len = PAGE_SIZE - res;
345 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
346 res = strnlen(buffer, res);
347 }
348 }
349out_mm:
350 mmput(mm);
351out:
352 return res;
353}
354
355static int proc_pid_auxv(struct task_struct *task, char *buffer)
356{
357 int res = 0;
358 struct mm_struct *mm = get_task_mm(task);
359 if (mm) {
360 unsigned int nwords = 0;
361 do
362 nwords += 2;
363 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
364 res = nwords * sizeof(mm->saved_auxv[0]);
365 if (res > PAGE_SIZE)
366 res = PAGE_SIZE;
367 memcpy(buffer, mm->saved_auxv, res);
368 mmput(mm);
369 }
370 return res;
371}
372
373
374#ifdef CONFIG_KALLSYMS
375/*
376 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
377 * Returns the resolved symbol. If that fails, simply return the address.
378 */
379static int proc_pid_wchan(struct task_struct *task, char *buffer)
380{
381 char *modname;
382 const char *sym_name;
383 unsigned long wchan, size, offset;
384 char namebuf[KSYM_NAME_LEN+1];
385
386 wchan = get_wchan(task);
387
388 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
389 if (sym_name)
390 return sprintf(buffer, "%s", sym_name);
391 return sprintf(buffer, "%lu", wchan);
392}
393#endif /* CONFIG_KALLSYMS */
394
395#ifdef CONFIG_SCHEDSTATS
396/*
397 * Provides /proc/PID/schedstat
398 */
399static int proc_pid_schedstat(struct task_struct *task, char *buffer)
400{
401 return sprintf(buffer, "%lu %lu %lu\n",
402 task->sched_info.cpu_time,
403 task->sched_info.run_delay,
404 task->sched_info.pcnt);
405}
406#endif
407
408/* The badness from the OOM killer */
409unsigned long badness(struct task_struct *p, unsigned long uptime);
410static int proc_oom_score(struct task_struct *task, char *buffer)
411{
412 unsigned long points;
413 struct timespec uptime;
414
415 do_posix_clock_monotonic_gettime(&uptime);
416 points = badness(task, uptime.tv_sec);
417 return sprintf(buffer, "%lu\n", points);
418}
419
420/************************************************************************/
421/* Here the fs part begins */
422/************************************************************************/
423
424/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700425static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700427 struct task_struct *task;
428 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700429 /* Allow access to a task's file descriptors if it is us or we
430 * may use ptrace attach to the process and find out that
431 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700432 */
433 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700434 if (task) {
435 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700436 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700437 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700438 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700441static int proc_setattr(struct dentry *dentry, struct iattr *attr)
442{
443 int error;
444 struct inode *inode = dentry->d_inode;
445
446 if (attr->ia_valid & ATTR_MODE)
447 return -EPERM;
448
449 error = inode_change_ok(inode, attr);
450 if (!error) {
451 error = security_inode_setattr(dentry, attr);
452 if (!error)
453 error = inode_setattr(inode, attr);
454 }
455 return error;
456}
457
458static struct inode_operations proc_def_inode_operations = {
459 .setattr = proc_setattr,
460};
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500463struct proc_mounts {
464 struct seq_file m;
465 int event;
466};
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static int mounts_open(struct inode *inode, struct file *file)
469{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700470 struct task_struct *task = get_proc_task(inode);
471 struct namespace *namespace = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500472 struct proc_mounts *p;
473 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Eric W. Biederman99f89552006-06-26 00:25:55 -0700475 if (task) {
476 task_lock(task);
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700477 namespace = task->nsproxy->namespace;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700478 if (namespace)
479 get_namespace(namespace);
480 task_unlock(task);
481 put_task_struct(task);
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Al Viro5addc5d2005-11-07 17:15:49 -0500484 if (namespace) {
485 ret = -ENOMEM;
486 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
487 if (p) {
488 file->private_data = &p->m;
489 ret = seq_open(file, &mounts_op);
490 if (!ret) {
491 p->m.private = namespace;
492 p->event = namespace->event;
493 return 0;
494 }
495 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Al Viro5addc5d2005-11-07 17:15:49 -0500497 put_namespace(namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 return ret;
500}
501
502static int mounts_release(struct inode *inode, struct file *file)
503{
504 struct seq_file *m = file->private_data;
505 struct namespace *namespace = m->private;
506 put_namespace(namespace);
507 return seq_release(inode, file);
508}
509
Al Viro5addc5d2005-11-07 17:15:49 -0500510static unsigned mounts_poll(struct file *file, poll_table *wait)
511{
512 struct proc_mounts *p = file->private_data;
513 struct namespace *ns = p->m.private;
514 unsigned res = 0;
515
516 poll_wait(file, &ns->poll, wait);
517
518 spin_lock(&vfsmount_lock);
519 if (p->event != ns->event) {
520 p->event = ns->event;
521 res = POLLERR;
522 }
523 spin_unlock(&vfsmount_lock);
524
525 return res;
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528static struct file_operations proc_mounts_operations = {
529 .open = mounts_open,
530 .read = seq_read,
531 .llseek = seq_lseek,
532 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500533 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534};
535
Chuck Leverb4629fe2006-03-20 13:44:12 -0500536extern struct seq_operations mountstats_op;
537static int mountstats_open(struct inode *inode, struct file *file)
538{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500539 int ret = seq_open(file, &mountstats_op);
540
541 if (!ret) {
542 struct seq_file *m = file->private_data;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700543 struct namespace *namespace = NULL;
544 struct task_struct *task = get_proc_task(inode);
545
546 if (task) {
547 task_lock(task);
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700548 namespace = task->nsproxy->namespace;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700549 if (namespace)
550 get_namespace(namespace);
551 task_unlock(task);
552 put_task_struct(task);
553 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500554
555 if (namespace)
556 m->private = namespace;
557 else {
558 seq_release(inode, file);
559 ret = -EINVAL;
560 }
561 }
562 return ret;
563}
564
565static struct file_operations proc_mountstats_operations = {
566 .open = mountstats_open,
567 .read = seq_read,
568 .llseek = seq_lseek,
569 .release = mounts_release,
570};
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
573
574static ssize_t proc_info_read(struct file * file, char __user * buf,
575 size_t count, loff_t *ppos)
576{
577 struct inode * inode = file->f_dentry->d_inode;
578 unsigned long page;
579 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700580 struct task_struct *task = get_proc_task(inode);
581
582 length = -ESRCH;
583 if (!task)
584 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (count > PROC_BLOCK_SIZE)
587 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700588
589 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 length = PROC_I(inode)->op.proc_read(task, (char*)page);
594
595 if (length >= 0)
596 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
597 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700598out:
599 put_task_struct(task);
600out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return length;
602}
603
604static struct file_operations proc_info_file_operations = {
605 .read = proc_info_read,
606};
607
608static int mem_open(struct inode* inode, struct file* file)
609{
610 file->private_data = (void*)((long)current->self_exec_id);
611 return 0;
612}
613
614static ssize_t mem_read(struct file * file, char __user * buf,
615 size_t count, loff_t *ppos)
616{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700617 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 char *page;
619 unsigned long src = *ppos;
620 int ret = -ESRCH;
621 struct mm_struct *mm;
622
Eric W. Biederman99f89552006-06-26 00:25:55 -0700623 if (!task)
624 goto out_no_task;
625
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700626 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 goto out;
628
629 ret = -ENOMEM;
630 page = (char *)__get_free_page(GFP_USER);
631 if (!page)
632 goto out;
633
634 ret = 0;
635
636 mm = get_task_mm(task);
637 if (!mm)
638 goto out_free;
639
640 ret = -EIO;
641
642 if (file->private_data != (void*)((long)current->self_exec_id))
643 goto out_put;
644
645 ret = 0;
646
647 while (count > 0) {
648 int this_len, retval;
649
650 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
651 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700652 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (!ret)
654 ret = -EIO;
655 break;
656 }
657
658 if (copy_to_user(buf, page, retval)) {
659 ret = -EFAULT;
660 break;
661 }
662
663 ret += retval;
664 src += retval;
665 buf += retval;
666 count -= retval;
667 }
668 *ppos = src;
669
670out_put:
671 mmput(mm);
672out_free:
673 free_page((unsigned long) page);
674out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700675 put_task_struct(task);
676out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return ret;
678}
679
680#define mem_write NULL
681
682#ifndef mem_write
683/* This is a security hazard */
684static ssize_t mem_write(struct file * file, const char * buf,
685 size_t count, loff_t *ppos)
686{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700687 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 char *page;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700689 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 unsigned long dst = *ppos;
691
Eric W. Biederman99f89552006-06-26 00:25:55 -0700692 copied = -ESRCH;
693 if (!task)
694 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Eric W. Biederman99f89552006-06-26 00:25:55 -0700696 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
697 goto out;
698
699 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 page = (char *)__get_free_page(GFP_USER);
701 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700702 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700704 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 while (count > 0) {
706 int this_len, retval;
707
708 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
709 if (copy_from_user(page, buf, this_len)) {
710 copied = -EFAULT;
711 break;
712 }
713 retval = access_process_vm(task, dst, page, this_len, 1);
714 if (!retval) {
715 if (!copied)
716 copied = -EIO;
717 break;
718 }
719 copied += retval;
720 buf += retval;
721 dst += retval;
722 count -= retval;
723 }
724 *ppos = dst;
725 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700726out:
727 put_task_struct(task);
728out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return copied;
730}
731#endif
732
733static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
734{
735 switch (orig) {
736 case 0:
737 file->f_pos = offset;
738 break;
739 case 1:
740 file->f_pos += offset;
741 break;
742 default:
743 return -EINVAL;
744 }
745 force_successful_syscall_return();
746 return file->f_pos;
747}
748
749static struct file_operations proc_mem_operations = {
750 .llseek = mem_lseek,
751 .read = mem_read,
752 .write = mem_write,
753 .open = mem_open,
754};
755
756static ssize_t oom_adjust_read(struct file *file, char __user *buf,
757 size_t count, loff_t *ppos)
758{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700759 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700760 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700762 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 loff_t __ppos = *ppos;
764
Eric W. Biederman99f89552006-06-26 00:25:55 -0700765 if (!task)
766 return -ESRCH;
767 oom_adjust = task->oomkilladj;
768 put_task_struct(task);
769
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700770 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (__ppos >= len)
772 return 0;
773 if (count > len-__ppos)
774 count = len-__ppos;
775 if (copy_to_user(buf, buffer + __ppos, count))
776 return -EFAULT;
777 *ppos = __ppos + count;
778 return count;
779}
780
781static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
782 size_t count, loff_t *ppos)
783{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700784 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700785 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int oom_adjust;
787
788 if (!capable(CAP_SYS_RESOURCE))
789 return -EPERM;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700790 memset(buffer, 0, sizeof(buffer));
791 if (count > sizeof(buffer) - 1)
792 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (copy_from_user(buffer, buf, count))
794 return -EFAULT;
795 oom_adjust = simple_strtol(buffer, &end, 0);
Andrea Arcangeli79befd02005-04-16 15:24:05 -0700796 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -EINVAL;
798 if (*end == '\n')
799 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700800 task = get_proc_task(file->f_dentry->d_inode);
801 if (!task)
802 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700804 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (end - buffer == 0)
806 return -EIO;
807 return end - buffer;
808}
809
810static struct file_operations proc_oom_adjust_operations = {
811 .read = oom_adjust_read,
812 .write = oom_adjust_write,
813};
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815#ifdef CONFIG_AUDITSYSCALL
816#define TMPBUFLEN 21
817static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
818 size_t count, loff_t *ppos)
819{
820 struct inode * inode = file->f_dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700821 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ssize_t length;
823 char tmpbuf[TMPBUFLEN];
824
Eric W. Biederman99f89552006-06-26 00:25:55 -0700825 if (!task)
826 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
828 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700829 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
831}
832
833static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
834 size_t count, loff_t *ppos)
835{
836 struct inode * inode = file->f_dentry->d_inode;
837 char *page, *tmp;
838 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 uid_t loginuid;
840
841 if (!capable(CAP_AUDIT_CONTROL))
842 return -EPERM;
843
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700844 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -EPERM;
846
Al Viroe0182902006-05-18 08:28:02 -0400847 if (count >= PAGE_SIZE)
848 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 if (*ppos != 0) {
851 /* No partial writes. */
852 return -EINVAL;
853 }
854 page = (char*)__get_free_page(GFP_USER);
855 if (!page)
856 return -ENOMEM;
857 length = -EFAULT;
858 if (copy_from_user(page, buf, count))
859 goto out_free_page;
860
Al Viroe0182902006-05-18 08:28:02 -0400861 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 loginuid = simple_strtoul(page, &tmp, 10);
863 if (tmp == page) {
864 length = -EINVAL;
865 goto out_free_page;
866
867 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700868 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (likely(length == 0))
870 length = count;
871
872out_free_page:
873 free_page((unsigned long) page);
874 return length;
875}
876
877static struct file_operations proc_loginuid_operations = {
878 .read = proc_loginuid_read,
879 .write = proc_loginuid_write,
880};
881#endif
882
883#ifdef CONFIG_SECCOMP
884static ssize_t seccomp_read(struct file *file, char __user *buf,
885 size_t count, loff_t *ppos)
886{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700887 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 char __buf[20];
889 loff_t __ppos = *ppos;
890 size_t len;
891
Eric W. Biederman99f89552006-06-26 00:25:55 -0700892 if (!tsk)
893 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* no need to print the trailing zero, so use only len */
895 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700896 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (__ppos >= len)
898 return 0;
899 if (count > len - __ppos)
900 count = len - __ppos;
901 if (copy_to_user(buf, __buf + __ppos, count))
902 return -EFAULT;
903 *ppos = __ppos + count;
904 return count;
905}
906
907static ssize_t seccomp_write(struct file *file, const char __user *buf,
908 size_t count, loff_t *ppos)
909{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700910 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 char __buf[20], *end;
912 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700913 ssize_t result;
914
915 result = -ESRCH;
916 if (!tsk)
917 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700920 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700922 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Eric W. Biederman99f89552006-06-26 00:25:55 -0700924 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 memset(__buf, 0, sizeof(__buf));
926 count = min(count, sizeof(__buf) - 1);
927 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700928 goto out;
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 seccomp_mode = simple_strtoul(__buf, &end, 0);
931 if (*end == '\n')
932 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700933 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
935 tsk->seccomp.mode = seccomp_mode;
936 set_tsk_thread_flag(tsk, TIF_SECCOMP);
937 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700938 goto out;
939 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700941 goto out;
942 result = end - __buf;
943out:
944 put_task_struct(tsk);
945out_no_task:
946 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949static struct file_operations proc_seccomp_operations = {
950 .read = seccomp_read,
951 .write = seccomp_write,
952};
953#endif /* CONFIG_SECCOMP */
954
Al Viro008b1502005-08-20 00:17:39 +0100955static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 struct inode *inode = dentry->d_inode;
958 int error = -EACCES;
959
960 /* We don't need a base pointer in the /proc filesystem */
961 path_release(nd);
962
Eric W. Biederman778c1142006-06-26 00:25:58 -0700963 /* Are we allowed to snoop on the tasks file descriptors? */
964 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
968 nd->last_type = LAST_BIND;
969out:
Al Viro008b1502005-08-20 00:17:39 +0100970 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
974 char __user *buffer, int buflen)
975{
976 struct inode * inode;
977 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
978 int len;
979
980 if (!tmp)
981 return -ENOMEM;
982
983 inode = dentry->d_inode;
984 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
985 len = PTR_ERR(path);
986 if (IS_ERR(path))
987 goto out;
988 len = tmp + PAGE_SIZE - 1 - path;
989
990 if (len > buflen)
991 len = buflen;
992 if (copy_to_user(buffer, path, len))
993 len = -EFAULT;
994 out:
995 free_page((unsigned long)tmp);
996 return len;
997}
998
999static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1000{
1001 int error = -EACCES;
1002 struct inode *inode = dentry->d_inode;
1003 struct dentry *de;
1004 struct vfsmount *mnt = NULL;
1005
Eric W. Biederman778c1142006-06-26 00:25:58 -07001006 /* Are we allowed to snoop on the tasks file descriptors? */
1007 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1011 if (error)
1012 goto out;
1013
1014 error = do_proc_readlink(de, mnt, buffer, buflen);
1015 dput(de);
1016 mntput(mnt);
1017out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return error;
1019}
1020
1021static struct inode_operations proc_pid_link_inode_operations = {
1022 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001023 .follow_link = proc_pid_follow_link,
1024 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025};
1026
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001027
1028/* building an inode */
1029
1030static int task_dumpable(struct task_struct *task)
1031{
1032 int dumpable = 0;
1033 struct mm_struct *mm;
1034
1035 task_lock(task);
1036 mm = task->mm;
1037 if (mm)
1038 dumpable = mm->dumpable;
1039 task_unlock(task);
1040 if(dumpable == 1)
1041 return 1;
1042 return 0;
1043}
1044
1045
1046static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1047{
1048 struct inode * inode;
1049 struct proc_inode *ei;
1050
1051 /* We need a new inode */
1052
1053 inode = new_inode(sb);
1054 if (!inode)
1055 goto out;
1056
1057 /* Common stuff */
1058 ei = PROC_I(inode);
1059 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1060 inode->i_ino = fake_ino(task->pid, ino);
1061 inode->i_op = &proc_def_inode_operations;
1062
1063 /*
1064 * grab the reference to task.
1065 */
1066 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1067 if (!ei->pid)
1068 goto out_unlock;
1069
1070 inode->i_uid = 0;
1071 inode->i_gid = 0;
1072 if (task_dumpable(task)) {
1073 inode->i_uid = task->euid;
1074 inode->i_gid = task->egid;
1075 }
1076 security_task_to_inode(task, inode);
1077
1078out:
1079 return inode;
1080
1081out_unlock:
1082 iput(inode);
1083 return NULL;
1084}
1085
1086static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1087{
1088 struct inode *inode = dentry->d_inode;
1089 struct task_struct *task;
1090 generic_fillattr(inode, stat);
1091
1092 rcu_read_lock();
1093 stat->uid = 0;
1094 stat->gid = 0;
1095 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1096 if (task) {
1097 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1098 task_dumpable(task)) {
1099 stat->uid = task->euid;
1100 stat->gid = task->egid;
1101 }
1102 }
1103 rcu_read_unlock();
1104 return 0;
1105}
1106
1107/* dentry stuff */
1108
1109/*
1110 * Exceptional case: normally we are not allowed to unhash a busy
1111 * directory. In this case, however, we can do it - no aliasing problems
1112 * due to the way we treat inodes.
1113 *
1114 * Rewrite the inode's ownerships here because the owning task may have
1115 * performed a setuid(), etc.
1116 *
1117 * Before the /proc/pid/status file was created the only way to read
1118 * the effective uid of a /process was to stat /proc/pid. Reading
1119 * /proc/pid/status is slow enough that procps and other packages
1120 * kept stating /proc/pid. To keep the rules in /proc simple I have
1121 * made this apply to all per process world readable and executable
1122 * directories.
1123 */
1124static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1125{
1126 struct inode *inode = dentry->d_inode;
1127 struct task_struct *task = get_proc_task(inode);
1128 if (task) {
1129 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1130 task_dumpable(task)) {
1131 inode->i_uid = task->euid;
1132 inode->i_gid = task->egid;
1133 } else {
1134 inode->i_uid = 0;
1135 inode->i_gid = 0;
1136 }
1137 inode->i_mode &= ~(S_ISUID | S_ISGID);
1138 security_task_to_inode(task, inode);
1139 put_task_struct(task);
1140 return 1;
1141 }
1142 d_drop(dentry);
1143 return 0;
1144}
1145
1146static int pid_delete_dentry(struct dentry * dentry)
1147{
1148 /* Is the task we represent dead?
1149 * If so, then don't put the dentry on the lru list,
1150 * kill it immediately.
1151 */
1152 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1153}
1154
1155static struct dentry_operations pid_dentry_operations =
1156{
1157 .d_revalidate = pid_revalidate,
1158 .d_delete = pid_delete_dentry,
1159};
1160
1161/* Lookups */
1162
1163static unsigned name_to_int(struct dentry *dentry)
1164{
1165 const char *name = dentry->d_name.name;
1166 int len = dentry->d_name.len;
1167 unsigned n = 0;
1168
1169 if (len > 1 && *name == '0')
1170 goto out;
1171 while (len-- > 0) {
1172 unsigned c = *name++ - '0';
1173 if (c > 9)
1174 goto out;
1175 if (n >= (~0U-9)/10)
1176 goto out;
1177 n *= 10;
1178 n += c;
1179 }
1180 return n;
1181out:
1182 return ~0U;
1183}
1184
1185static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1186{
1187 struct task_struct *task = get_proc_task(inode);
1188 struct files_struct *files = NULL;
1189 struct file *file;
1190 int fd = proc_fd(inode);
1191
1192 if (task) {
1193 files = get_files_struct(task);
1194 put_task_struct(task);
1195 }
1196 if (files) {
1197 /*
1198 * We are not taking a ref to the file structure, so we must
1199 * hold ->file_lock.
1200 */
1201 spin_lock(&files->file_lock);
1202 file = fcheck_files(files, fd);
1203 if (file) {
1204 *mnt = mntget(file->f_vfsmnt);
1205 *dentry = dget(file->f_dentry);
1206 spin_unlock(&files->file_lock);
1207 put_files_struct(files);
1208 return 0;
1209 }
1210 spin_unlock(&files->file_lock);
1211 put_files_struct(files);
1212 }
1213 return -ENOENT;
1214}
1215
1216static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1217{
1218 struct inode *inode = dentry->d_inode;
1219 struct task_struct *task = get_proc_task(inode);
1220 int fd = proc_fd(inode);
1221 struct files_struct *files;
1222
1223 if (task) {
1224 files = get_files_struct(task);
1225 if (files) {
1226 rcu_read_lock();
1227 if (fcheck_files(files, fd)) {
1228 rcu_read_unlock();
1229 put_files_struct(files);
1230 if (task_dumpable(task)) {
1231 inode->i_uid = task->euid;
1232 inode->i_gid = task->egid;
1233 } else {
1234 inode->i_uid = 0;
1235 inode->i_gid = 0;
1236 }
1237 inode->i_mode &= ~(S_ISUID | S_ISGID);
1238 security_task_to_inode(task, inode);
1239 put_task_struct(task);
1240 return 1;
1241 }
1242 rcu_read_unlock();
1243 put_files_struct(files);
1244 }
1245 put_task_struct(task);
1246 }
1247 d_drop(dentry);
1248 return 0;
1249}
1250
1251static struct dentry_operations tid_fd_dentry_operations =
1252{
1253 .d_revalidate = tid_fd_revalidate,
1254 .d_delete = pid_delete_dentry,
1255};
1256
1257/* SMP-safe */
1258static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1259{
1260 struct task_struct *task = get_proc_task(dir);
1261 unsigned fd = name_to_int(dentry);
1262 struct dentry *result = ERR_PTR(-ENOENT);
1263 struct file * file;
1264 struct files_struct * files;
1265 struct inode *inode;
1266 struct proc_inode *ei;
1267
1268 if (!task)
1269 goto out_no_task;
1270 if (fd == ~0U)
1271 goto out;
1272
1273 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1274 if (!inode)
1275 goto out;
1276 ei = PROC_I(inode);
1277 ei->fd = fd;
1278 files = get_files_struct(task);
1279 if (!files)
1280 goto out_unlock;
1281 inode->i_mode = S_IFLNK;
1282
1283 /*
1284 * We are not taking a ref to the file structure, so we must
1285 * hold ->file_lock.
1286 */
1287 spin_lock(&files->file_lock);
1288 file = fcheck_files(files, fd);
1289 if (!file)
1290 goto out_unlock2;
1291 if (file->f_mode & 1)
1292 inode->i_mode |= S_IRUSR | S_IXUSR;
1293 if (file->f_mode & 2)
1294 inode->i_mode |= S_IWUSR | S_IXUSR;
1295 spin_unlock(&files->file_lock);
1296 put_files_struct(files);
1297 inode->i_op = &proc_pid_link_inode_operations;
1298 inode->i_size = 64;
1299 ei->op.proc_get_link = proc_fd_link;
1300 dentry->d_op = &tid_fd_dentry_operations;
1301 d_add(dentry, inode);
1302 /* Close the race of the process dying before we return the dentry */
1303 if (tid_fd_revalidate(dentry, NULL))
1304 result = NULL;
1305out:
1306 put_task_struct(task);
1307out_no_task:
1308 return result;
1309
1310out_unlock2:
1311 spin_unlock(&files->file_lock);
1312 put_files_struct(files);
1313out_unlock:
1314 iput(inode);
1315 goto out;
1316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1319{
Eric W. Biederman56347082006-06-26 00:25:40 -07001320 struct dentry *dentry = filp->f_dentry;
1321 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001322 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 unsigned int fd, tid, ino;
1324 int retval;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001325 char buf[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001327 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001330 if (!p)
1331 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 retval = 0;
1333 tid = p->pid;
1334
1335 fd = filp->f_pos;
1336 switch (fd) {
1337 case 0:
1338 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1339 goto out;
1340 filp->f_pos++;
1341 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001342 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1344 goto out;
1345 filp->f_pos++;
1346 default:
1347 files = get_files_struct(p);
1348 if (!files)
1349 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001350 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001351 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001353 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 fd++, filp->f_pos++) {
1355 unsigned int i,j;
1356
1357 if (!fcheck_files(files, fd))
1358 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001359 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001361 j = PROC_NUMBUF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 i = fd;
1363 do {
1364 j--;
1365 buf[j] = '0' + (i % 10);
1366 i /= 10;
1367 } while (i);
1368
1369 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001370 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001371 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 break;
1373 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001374 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001376 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 put_files_struct(files);
1378 }
1379out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001380 put_task_struct(p);
1381out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return retval;
1383}
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385static struct file_operations proc_fd_operations = {
1386 .read = generic_read_dir,
1387 .readdir = proc_readfd,
1388};
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/*
1391 * proc directories can do almost nothing..
1392 */
1393static struct inode_operations proc_fd_inode_operations = {
1394 .lookup = proc_lookupfd,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001395 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396};
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398/* SMP-safe */
1399static struct dentry *proc_pident_lookup(struct inode *dir,
1400 struct dentry *dentry,
1401 struct pid_entry *ents)
1402{
1403 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001404 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001405 struct task_struct *task = get_proc_task(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 struct pid_entry *p;
1407 struct proc_inode *ei;
1408
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001409 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 inode = NULL;
1411
Eric W. Biederman99f89552006-06-26 00:25:55 -07001412 if (!task)
1413 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001415 /*
1416 * Yes, it does not scale. And it should not. Don't add
1417 * new entries into /proc/<tgid>/ without very good reasons.
1418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 for (p = ents; p->name; p++) {
1420 if (p->len != dentry->d_name.len)
1421 continue;
1422 if (!memcmp(dentry->d_name.name, p->name, p->len))
1423 break;
1424 }
1425 if (!p->name)
1426 goto out;
1427
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001428 error = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1430 if (!inode)
1431 goto out;
1432
1433 ei = PROC_I(inode);
1434 inode->i_mode = p->mode;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001435 if (S_ISDIR(inode->i_mode))
1436 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1437 if (p->iop)
1438 inode->i_op = p->iop;
1439 if (p->fop)
1440 inode->i_fop = p->fop;
1441 ei->op = p->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 dentry->d_op = &pid_dentry_operations;
1443 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001444 /* Close the race of the process dying before we return the dentry */
1445 if (pid_revalidate(dentry, NULL))
1446 error = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001448 put_task_struct(task);
1449out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001450 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001453static int proc_pident_readdir(struct file *filp,
1454 void *dirent, filldir_t filldir,
1455 struct pid_entry *ents, unsigned int nents)
1456{
1457 int i;
1458 int pid;
1459 struct dentry *dentry = filp->f_dentry;
1460 struct inode *inode = dentry->d_inode;
1461 struct task_struct *task = get_proc_task(inode);
1462 struct pid_entry *p;
1463 ino_t ino;
1464 int ret;
1465
1466 ret = -ENOENT;
1467 if (!task)
1468 goto out;
1469
1470 ret = 0;
1471 pid = task->pid;
1472 put_task_struct(task);
1473 i = filp->f_pos;
1474 switch (i) {
1475 case 0:
1476 ino = inode->i_ino;
1477 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1478 goto out;
1479 i++;
1480 filp->f_pos++;
1481 /* fall through */
1482 case 1:
1483 ino = parent_ino(dentry);
1484 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1485 goto out;
1486 i++;
1487 filp->f_pos++;
1488 /* fall through */
1489 default:
1490 i -= 2;
1491 if (i >= nents) {
1492 ret = 1;
1493 goto out;
1494 }
1495 p = ents + i;
1496 while (p->name) {
1497 if (filldir(dirent, p->name, p->len, filp->f_pos,
1498 fake_ino(pid, p->type), p->mode >> 12) < 0)
1499 goto out;
1500 filp->f_pos++;
1501 p++;
1502 }
1503 }
1504
1505 ret = 1;
1506out:
1507 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001511static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1512 size_t count, loff_t *ppos)
1513{
1514 struct inode * inode = file->f_dentry->d_inode;
1515 unsigned long page;
1516 ssize_t length;
1517 struct task_struct *task = get_proc_task(inode);
1518
1519 length = -ESRCH;
1520 if (!task)
1521 goto out_no_task;
1522
1523 if (count > PAGE_SIZE)
1524 count = PAGE_SIZE;
1525 length = -ENOMEM;
1526 if (!(page = __get_free_page(GFP_KERNEL)))
1527 goto out;
1528
1529 length = security_getprocattr(task,
1530 (char*)file->f_dentry->d_name.name,
1531 (void*)page, count);
1532 if (length >= 0)
1533 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1534 free_page(page);
1535out:
1536 put_task_struct(task);
1537out_no_task:
1538 return length;
1539}
1540
1541static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1542 size_t count, loff_t *ppos)
1543{
1544 struct inode * inode = file->f_dentry->d_inode;
1545 char *page;
1546 ssize_t length;
1547 struct task_struct *task = get_proc_task(inode);
1548
1549 length = -ESRCH;
1550 if (!task)
1551 goto out_no_task;
1552 if (count > PAGE_SIZE)
1553 count = PAGE_SIZE;
1554
1555 /* No partial writes. */
1556 length = -EINVAL;
1557 if (*ppos != 0)
1558 goto out;
1559
1560 length = -ENOMEM;
1561 page = (char*)__get_free_page(GFP_USER);
1562 if (!page)
1563 goto out;
1564
1565 length = -EFAULT;
1566 if (copy_from_user(page, buf, count))
1567 goto out_free;
1568
1569 length = security_setprocattr(task,
1570 (char*)file->f_dentry->d_name.name,
1571 (void*)page, count);
1572out_free:
1573 free_page((unsigned long) page);
1574out:
1575 put_task_struct(task);
1576out_no_task:
1577 return length;
1578}
1579
1580static struct file_operations proc_pid_attr_operations = {
1581 .read = proc_pid_attr_read,
1582 .write = proc_pid_attr_write,
1583};
1584
1585static struct pid_entry tgid_attr_stuff[] = {
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001586 REG(PROC_TGID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1587 REG(PROC_TGID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1588 REG(PROC_TGID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1589 REG(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1590 REG(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1591 REG(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1592 {}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001593};
1594static struct pid_entry tid_attr_stuff[] = {
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001595 REG(PROC_TID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1596 REG(PROC_TID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1597 REG(PROC_TID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1598 REG(PROC_TID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1599 REG(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1600 REG(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1601 {}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001602};
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604static int proc_tgid_attr_readdir(struct file * filp,
1605 void * dirent, filldir_t filldir)
1606{
1607 return proc_pident_readdir(filp,dirent,filldir,
1608 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1609}
1610
1611static int proc_tid_attr_readdir(struct file * filp,
1612 void * dirent, filldir_t filldir)
1613{
1614 return proc_pident_readdir(filp,dirent,filldir,
1615 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1616}
1617
1618static struct file_operations proc_tgid_attr_operations = {
1619 .read = generic_read_dir,
1620 .readdir = proc_tgid_attr_readdir,
1621};
1622
1623static struct file_operations proc_tid_attr_operations = {
1624 .read = generic_read_dir,
1625 .readdir = proc_tid_attr_readdir,
1626};
1627
1628static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1629 struct dentry *dentry, struct nameidata *nd)
1630{
1631 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1632}
1633
1634static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1635 struct dentry *dentry, struct nameidata *nd)
1636{
1637 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1638}
1639
1640static struct inode_operations proc_tgid_attr_inode_operations = {
1641 .lookup = proc_tgid_attr_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001642 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001643 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644};
1645
1646static struct inode_operations proc_tid_attr_inode_operations = {
1647 .lookup = proc_tid_attr_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001648 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001649 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650};
1651#endif
1652
1653/*
1654 * /proc/self:
1655 */
1656static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1657 int buflen)
1658{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001659 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 sprintf(tmp, "%d", current->tgid);
1661 return vfs_readlink(dentry,buffer,buflen,tmp);
1662}
1663
Al Viro008b1502005-08-20 00:17:39 +01001664static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001666 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001668 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671static struct inode_operations proc_self_inode_operations = {
1672 .readlink = proc_self_readlink,
1673 .follow_link = proc_self_follow_link,
1674};
1675
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001676/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001677 * proc base
1678 *
1679 * These are the directory entries in the root directory of /proc
1680 * that properly belong to the /proc filesystem, as they describe
1681 * describe something that is process related.
1682 */
1683static struct pid_entry proc_base_stuff[] = {
1684 NOD(PROC_TGID_INO, "self", S_IFLNK|S_IRWXUGO,
1685 &proc_self_inode_operations, NULL, {}),
1686 {}
1687};
1688
1689/*
1690 * Exceptional case: normally we are not allowed to unhash a busy
1691 * directory. In this case, however, we can do it - no aliasing problems
1692 * due to the way we treat inodes.
1693 */
1694static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1695{
1696 struct inode *inode = dentry->d_inode;
1697 struct task_struct *task = get_proc_task(inode);
1698 if (task) {
1699 put_task_struct(task);
1700 return 1;
1701 }
1702 d_drop(dentry);
1703 return 0;
1704}
1705
1706static struct dentry_operations proc_base_dentry_operations =
1707{
1708 .d_revalidate = proc_base_revalidate,
1709 .d_delete = pid_delete_dentry,
1710};
1711
1712static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1713{
1714 struct inode *inode;
1715 struct dentry *error;
1716 struct task_struct *task = get_proc_task(dir);
1717 struct pid_entry *p;
1718 struct proc_inode *ei;
1719
1720 error = ERR_PTR(-ENOENT);
1721 inode = NULL;
1722
1723 if (!task)
1724 goto out_no_task;
1725
1726 /* Lookup the directory entry */
1727 for (p = proc_base_stuff; p->name; p++) {
1728 if (p->len != dentry->d_name.len)
1729 continue;
1730 if (!memcmp(dentry->d_name.name, p->name, p->len))
1731 break;
1732 }
1733 if (!p->name)
1734 goto out;
1735
1736 /* Allocate the inode */
1737 error = ERR_PTR(-ENOMEM);
1738 inode = new_inode(dir->i_sb);
1739 if (!inode)
1740 goto out;
1741
1742 /* Initialize the inode */
1743 ei = PROC_I(inode);
1744 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1745 inode->i_ino = fake_ino(0, p->type);
1746
1747 /*
1748 * grab the reference to the task.
1749 */
1750 ei->pid = get_pid(task_pid(task));
1751 if (!ei->pid)
1752 goto out_iput;
1753
1754 inode->i_uid = 0;
1755 inode->i_gid = 0;
1756 inode->i_mode = p->mode;
1757 if (S_ISDIR(inode->i_mode))
1758 inode->i_nlink = 2;
1759 if (S_ISLNK(inode->i_mode))
1760 inode->i_size = 64;
1761 if (p->iop)
1762 inode->i_op = p->iop;
1763 if (p->fop)
1764 inode->i_fop = p->fop;
1765 ei->op = p->op;
1766 dentry->d_op = &proc_base_dentry_operations;
1767 d_add(dentry, inode);
1768 error = NULL;
1769out:
1770 put_task_struct(task);
1771out_no_task:
1772 return error;
1773out_iput:
1774 iput(inode);
1775 goto out;
1776}
1777
1778/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001779 * Thread groups
1780 */
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001781static struct file_operations proc_task_operations;
1782static struct inode_operations proc_task_inode_operations;
1783
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001784static struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001785 DIR(PROC_TGID_TASK, "task", S_IRUGO|S_IXUGO, task),
1786 DIR(PROC_TGID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1787 INF(PROC_TGID_ENVIRON, "environ", S_IRUSR, pid_environ),
1788 INF(PROC_TGID_AUXV, "auxv", S_IRUSR, pid_auxv),
1789 INF(PROC_TGID_STATUS, "status", S_IRUGO, pid_status),
1790 INF(PROC_TGID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1791 INF(PROC_TGID_STAT, "stat", S_IRUGO, tgid_stat),
1792 INF(PROC_TGID_STATM, "statm", S_IRUGO, pid_statm),
1793 REG(PROC_TGID_MAPS, "maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001794#ifdef CONFIG_NUMA
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001795 REG(PROC_TGID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001796#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001797 REG(PROC_TGID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001798#ifdef CONFIG_SECCOMP
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001799 REG(PROC_TGID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001800#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001801 LNK(PROC_TGID_CWD, "cwd", cwd),
1802 LNK(PROC_TGID_ROOT, "root", root),
1803 LNK(PROC_TGID_EXE, "exe", exe),
1804 REG(PROC_TGID_MOUNTS, "mounts", S_IRUGO, mounts),
1805 REG(PROC_TGID_MOUNTSTATS, "mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001806#ifdef CONFIG_MMU
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001807 REG(PROC_TGID_SMAPS, "smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001808#endif
1809#ifdef CONFIG_SECURITY
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001810 DIR(PROC_TGID_ATTR, "attr", S_IRUGO|S_IXUGO, tgid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001811#endif
1812#ifdef CONFIG_KALLSYMS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001813 INF(PROC_TGID_WCHAN, "wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001814#endif
1815#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001816 INF(PROC_TGID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001817#endif
1818#ifdef CONFIG_CPUSETS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001819 REG(PROC_TGID_CPUSET, "cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001820#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001821 INF(PROC_TGID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1822 REG(PROC_TGID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001823#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001824 REG(PROC_TGID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001825#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001826 {}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001827};
1828
1829static int proc_tgid_base_readdir(struct file * filp,
1830 void * dirent, filldir_t filldir)
1831{
1832 return proc_pident_readdir(filp,dirent,filldir,
1833 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1834}
1835
1836static struct file_operations proc_tgid_base_operations = {
1837 .read = generic_read_dir,
1838 .readdir = proc_tgid_base_readdir,
1839};
1840
1841static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1842 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1843}
1844
1845static struct inode_operations proc_tgid_base_inode_operations = {
1846 .lookup = proc_tgid_base_lookup,
1847 .getattr = pid_getattr,
1848 .setattr = proc_setattr,
1849};
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001852 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001854 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001856 * Looks in the dcache for
1857 * /proc/@pid
1858 * /proc/@tgid/task/@pid
1859 * if either directory is present flushes it and all of it'ts children
1860 * from the dcache.
1861 *
1862 * It is safe and reasonable to cache /proc entries for a task until
1863 * that task exits. After that they just clog up the dcache with
1864 * useless entries, possibly causing useful dcache entries to be
1865 * flushed instead. This routine is proved to flush those useless
1866 * dcache entries at process exit time.
1867 *
1868 * NOTE: This routine is just an optimization so it does not guarantee
1869 * that no dcache entries will exist at process exit time it
1870 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001872void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001874 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001875 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001876 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Eric W. Biederman48e64842006-06-26 00:25:48 -07001878 name.name = buf;
1879 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1880 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1881 if (dentry) {
1882 shrink_dcache_parent(dentry);
1883 d_drop(dentry);
1884 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Eric W. Biederman48e64842006-06-26 00:25:48 -07001887 if (thread_group_leader(task))
1888 goto out;
1889
1890 name.name = buf;
1891 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1892 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1893 if (!leader)
1894 goto out;
1895
1896 name.name = "task";
1897 name.len = strlen(name.name);
1898 dir = d_hash_and_lookup(leader, &name);
1899 if (!dir)
1900 goto out_put_leader;
1901
1902 name.name = buf;
1903 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1904 dentry = d_hash_and_lookup(dir, &name);
1905 if (dentry) {
1906 shrink_dcache_parent(dentry);
1907 d_drop(dentry);
1908 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001910
1911 dput(dir);
1912out_put_leader:
1913 dput(leader);
1914out:
1915 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
1918/* SMP-safe */
1919struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1920{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001921 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 struct task_struct *task;
1923 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Eric W. Biederman801199c2006-10-02 02:18:48 -07001926 result = proc_base_lookup(dir, dentry);
1927 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1928 goto out;
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 tgid = name_to_int(dentry);
1931 if (tgid == ~0U)
1932 goto out;
1933
Eric W. Biedermande758732006-06-26 00:25:51 -07001934 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 task = find_task_by_pid(tgid);
1936 if (task)
1937 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07001938 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (!task)
1940 goto out;
1941
1942 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001943 if (!inode)
1944 goto out_put_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1947 inode->i_op = &proc_tgid_base_inode_operations;
1948 inode->i_fop = &proc_tgid_base_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 inode->i_flags|=S_IMMUTABLE;
Daniel Drakebcf88e12005-05-01 08:59:03 -07001950#ifdef CONFIG_SECURITY
1951 inode->i_nlink = 5;
1952#else
1953 inode->i_nlink = 4;
1954#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Eric W. Biederman48e64842006-06-26 00:25:48 -07001956 dentry->d_op = &pid_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001959 /* Close the race of the process dying before we return the dentry */
1960 if (pid_revalidate(dentry, NULL))
1961 result = NULL;
Eric W. Biederman48e64842006-06-26 00:25:48 -07001962
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001963out_put_task:
Eric W. Biederman48e64842006-06-26 00:25:48 -07001964 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001966 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001970 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001971 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001973static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001975 struct task_struct *task;
1976 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001978 rcu_read_lock();
1979retry:
1980 task = NULL;
1981 pid = find_ge_pid(tgid);
1982 if (pid) {
1983 tgid = pid->nr + 1;
1984 task = pid_task(pid, PIDTYPE_PID);
1985 /* What we to know is if the pid we have find is the
1986 * pid of a thread_group_leader. Testing for task
1987 * being a thread_group_leader is the obvious thing
1988 * todo but there is a window when it fails, due to
1989 * the pid transfer logic in de_thread.
1990 *
1991 * So we perform the straight forward test of seeing
1992 * if the pid we have found is the pid of a thread
1993 * group leader, and don't worry if the task we have
1994 * found doesn't happen to be a thread group leader.
1995 * As we don't care in the case of readdir.
1996 */
1997 if (!task || !has_group_leader_pid(task))
1998 goto retry;
1999 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002001 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002002 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002005#define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007/* for the /proc/ directory itself, after non-process stuff has been done */
2008int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 char buf[PROC_NUMBUF];
2011 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002012 struct task_struct *task;
2013 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Eric W. Biederman801199c2006-10-02 02:18:48 -07002015 for (; nr < (ARRAY_SIZE(proc_base_stuff) - 1); filp->f_pos++, nr++) {
2016 struct pid_entry *p = &proc_base_stuff[nr];
2017 if (filldir(dirent, p->name, p->len, filp->f_pos,
2018 fake_ino(0, p->type), p->mode >> 12) < 0)
2019 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002022 tgid = filp->f_pos - TGID_OFFSET;
2023 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002024 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002025 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002026 int len;
2027 ino_t ino;
2028 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002029 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002030 len = snprintf(buf, sizeof(buf), "%d", tgid);
2031 ino = fake_ino(tgid, PROC_TGID_INO);
2032 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002033 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002037 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2038out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return 0;
2040}
2041
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002042/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002043 * Tasks
2044 */
2045static struct pid_entry tid_base_stuff[] = {
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002046 DIR(PROC_TID_FD, "fd", S_IRUSR|S_IXUSR, fd),
2047 INF(PROC_TID_ENVIRON, "environ", S_IRUSR, pid_environ),
2048 INF(PROC_TID_AUXV, "auxv", S_IRUSR, pid_auxv),
2049 INF(PROC_TID_STATUS, "status", S_IRUGO, pid_status),
2050 INF(PROC_TID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
2051 INF(PROC_TID_STAT, "stat", S_IRUGO, tid_stat),
2052 INF(PROC_TID_STATM, "statm", S_IRUGO, pid_statm),
2053 REG(PROC_TID_MAPS, "maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002054#ifdef CONFIG_NUMA
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002055 REG(PROC_TID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002056#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002057 REG(PROC_TID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002058#ifdef CONFIG_SECCOMP
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002059 REG(PROC_TID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002060#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002061 LNK(PROC_TID_CWD, "cwd", cwd),
2062 LNK(PROC_TID_ROOT, "root", root),
2063 LNK(PROC_TID_EXE, "exe", exe),
2064 REG(PROC_TID_MOUNTS, "mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002065#ifdef CONFIG_MMU
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002066 REG(PROC_TID_SMAPS, "smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002067#endif
2068#ifdef CONFIG_SECURITY
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002069 DIR(PROC_TID_ATTR, "attr", S_IRUGO|S_IXUGO, tid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002070#endif
2071#ifdef CONFIG_KALLSYMS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002072 INF(PROC_TID_WCHAN, "wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002073#endif
2074#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002075 INF(PROC_TID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002076#endif
2077#ifdef CONFIG_CPUSETS
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002078 REG(PROC_TID_CPUSET, "cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002079#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002080 INF(PROC_TID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
2081 REG(PROC_TID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002082#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002083 REG(PROC_TID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002084#endif
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002085 {}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002086};
2087
2088static int proc_tid_base_readdir(struct file * filp,
2089 void * dirent, filldir_t filldir)
2090{
2091 return proc_pident_readdir(filp,dirent,filldir,
2092 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2093}
2094
2095static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2096 return proc_pident_lookup(dir, dentry, tid_base_stuff);
2097}
2098
2099static struct file_operations proc_tid_base_operations = {
2100 .read = generic_read_dir,
2101 .readdir = proc_tid_base_readdir,
2102};
2103
2104static struct inode_operations proc_tid_base_inode_operations = {
2105 .lookup = proc_tid_base_lookup,
2106 .getattr = pid_getattr,
2107 .setattr = proc_setattr,
2108};
2109
2110/* SMP-safe */
2111static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2112{
2113 struct dentry *result = ERR_PTR(-ENOENT);
2114 struct task_struct *task;
2115 struct task_struct *leader = get_proc_task(dir);
2116 struct inode *inode;
2117 unsigned tid;
2118
2119 if (!leader)
2120 goto out_no_task;
2121
2122 tid = name_to_int(dentry);
2123 if (tid == ~0U)
2124 goto out;
2125
2126 rcu_read_lock();
2127 task = find_task_by_pid(tid);
2128 if (task)
2129 get_task_struct(task);
2130 rcu_read_unlock();
2131 if (!task)
2132 goto out;
2133 if (leader->tgid != task->tgid)
2134 goto out_drop_task;
2135
2136 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2137
2138
2139 if (!inode)
2140 goto out_drop_task;
2141 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2142 inode->i_op = &proc_tid_base_inode_operations;
2143 inode->i_fop = &proc_tid_base_operations;
2144 inode->i_flags|=S_IMMUTABLE;
2145#ifdef CONFIG_SECURITY
2146 inode->i_nlink = 4;
2147#else
2148 inode->i_nlink = 3;
2149#endif
2150
2151 dentry->d_op = &pid_dentry_operations;
2152
2153 d_add(dentry, inode);
2154 /* Close the race of the process dying before we return the dentry */
2155 if (pid_revalidate(dentry, NULL))
2156 result = NULL;
2157
2158out_drop_task:
2159 put_task_struct(task);
2160out:
2161 put_task_struct(leader);
2162out_no_task:
2163 return result;
2164}
2165
2166/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002167 * Find the first tid of a thread group to return to user space.
2168 *
2169 * Usually this is just the thread group leader, but if the users
2170 * buffer was too small or there was a seek into the middle of the
2171 * directory we have more work todo.
2172 *
2173 * In the case of a short read we start with find_task_by_pid.
2174 *
2175 * In the case of a seek we start with the leader and walk nr
2176 * threads past it.
2177 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002178static struct task_struct *first_tid(struct task_struct *leader,
2179 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002180{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002181 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002182
Eric W. Biedermancc288732006-06-26 00:26:01 -07002183 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002184 /* Attempt to start with the pid of a thread */
2185 if (tid && (nr > 0)) {
2186 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002187 if (pos && (pos->group_leader == leader))
2188 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002189 }
2190
2191 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002192 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002193 if (nr && nr >= get_nr_threads(leader))
2194 goto out;
2195
2196 /* If we haven't found our starting place yet start
2197 * with the leader and walk nr threads forward.
2198 */
2199 for (pos = leader; nr > 0; --nr) {
2200 pos = next_thread(pos);
2201 if (pos == leader) {
2202 pos = NULL;
2203 goto out;
2204 }
2205 }
2206found:
2207 get_task_struct(pos);
2208out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002209 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002210 return pos;
2211}
2212
2213/*
2214 * Find the next thread in the thread list.
2215 * Return NULL if there is an error or no next thread.
2216 *
2217 * The reference to the input task_struct is released.
2218 */
2219static struct task_struct *next_tid(struct task_struct *start)
2220{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002221 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002222 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002223 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002224 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002225 if (thread_group_leader(pos))
2226 pos = NULL;
2227 else
2228 get_task_struct(pos);
2229 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002230 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002231 put_task_struct(start);
2232 return pos;
2233}
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235/* for the /proc/TGID/task/ directories */
2236static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 char buf[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 struct dentry *dentry = filp->f_dentry;
2240 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002241 struct task_struct *leader = get_proc_task(inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002242 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 int retval = -ENOENT;
2244 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002245 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2247
Eric W. Biederman99f89552006-06-26 00:25:55 -07002248 if (!leader)
2249 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 retval = 0;
2251
2252 switch (pos) {
2253 case 0:
2254 ino = inode->i_ino;
2255 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2256 goto out;
2257 pos++;
2258 /* fall through */
2259 case 1:
2260 ino = parent_ino(dentry);
2261 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2262 goto out;
2263 pos++;
2264 /* fall through */
2265 }
2266
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002267 /* f_version caches the tgid value that the last readdir call couldn't
2268 * return. lseek aka telldir automagically resets f_version to 0.
2269 */
2270 tid = filp->f_version;
2271 filp->f_version = 0;
2272 for (task = first_tid(leader, tid, pos - 2);
2273 task;
2274 task = next_tid(task), pos++) {
2275 int len;
2276 tid = task->pid;
2277 len = snprintf(buf, sizeof(buf), "%d", tid);
2278 ino = fake_ino(tid, PROC_TID_INO);
2279 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2280 /* returning this tgid failed, save it as the first
2281 * pid for the next readir call */
2282 filp->f_version = tid;
2283 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
2287out:
2288 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002289 put_task_struct(leader);
2290out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return retval;
2292}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002293
2294static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2295{
2296 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002297 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002298 generic_fillattr(inode, stat);
2299
Eric W. Biederman99f89552006-06-26 00:25:55 -07002300 if (p) {
2301 rcu_read_lock();
2302 stat->nlink += get_nr_threads(p);
2303 rcu_read_unlock();
2304 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002305 }
2306
2307 return 0;
2308}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002309
2310static struct inode_operations proc_task_inode_operations = {
2311 .lookup = proc_task_lookup,
2312 .getattr = proc_task_getattr,
2313 .setattr = proc_setattr,
2314};
2315
2316static struct file_operations proc_task_operations = {
2317 .read = generic_read_dir,
2318 .readdir = proc_task_readdir,
2319};