]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/proc/base.c
[PATCH] proc: Close the race of a process dying durning lookup
[net-next-2.6.git] / fs / proc / base.c
CommitLineData
1da177e4
LT
1/*
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.
e070ad49
ML
14 *
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.
1da177e4
LT
48 */
49
50#include <asm/uaccess.h>
51
52#include <linux/config.h>
53#include <linux/errno.h>
54#include <linux/time.h>
55#include <linux/proc_fs.h>
56#include <linux/stat.h>
57#include <linux/init.h>
16f7e0fe 58#include <linux/capability.h>
1da177e4
LT
59#include <linux/file.h>
60#include <linux/string.h>
61#include <linux/seq_file.h>
62#include <linux/namei.h>
63#include <linux/namespace.h>
64#include <linux/mm.h>
65#include <linux/smp_lock.h>
b835996f 66#include <linux/rcupdate.h>
1da177e4
LT
67#include <linux/kallsyms.h>
68#include <linux/mount.h>
69#include <linux/security.h>
70#include <linux/ptrace.h>
71#include <linux/seccomp.h>
72#include <linux/cpuset.h>
73#include <linux/audit.h>
5addc5dd 74#include <linux/poll.h>
1da177e4
LT
75#include "internal.h"
76
0f2fe20f
EB
77/* 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
1da177e4
LT
87/*
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,
6e21c8f1 115 PROC_TGID_NUMA_MAPS,
1da177e4 116 PROC_TGID_MOUNTS,
b4629fe2 117 PROC_TGID_MOUNTSTATS,
1da177e4 118 PROC_TGID_WCHAN,
63c6764c 119#ifdef CONFIG_MMU
e070ad49 120 PROC_TGID_SMAPS,
63c6764c 121#endif
1da177e4
LT
122#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,
4eb582cf 134 PROC_TGID_ATTR_KEYCREATE,
1da177e4
LT
135#endif
136#ifdef CONFIG_AUDITSYSCALL
137 PROC_TGID_LOGINUID,
138#endif
1da177e4
LT
139 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,
6e21c8f1 157 PROC_TID_NUMA_MAPS,
1da177e4 158 PROC_TID_MOUNTS,
b4629fe2 159 PROC_TID_MOUNTSTATS,
1da177e4 160 PROC_TID_WCHAN,
63c6764c 161#ifdef CONFIG_MMU
e070ad49 162 PROC_TID_SMAPS,
63c6764c 163#endif
1da177e4
LT
164#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,
4eb582cf 176 PROC_TID_ATTR_KEYCREATE,
1da177e4
LT
177#endif
178#ifdef CONFIG_AUDITSYSCALL
179 PROC_TID_LOGINUID,
180#endif
1da177e4
LT
181 PROC_TID_OOM_SCORE,
182 PROC_TID_OOM_ADJUST,
5e21ccb1
MS
183
184 /* Add new entries before this */
185 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
1da177e4
LT
186};
187
188struct pid_entry {
189 int type;
190 int len;
191 char *name;
192 mode_t mode;
193};
194
195#define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
196
197static struct pid_entry tgid_base_stuff[] = {
198 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
199 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
200 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
201 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
202 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
203 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
204 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
205 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
206 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
6e21c8f1
CL
207#ifdef CONFIG_NUMA
208 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
209#endif
1da177e4
LT
210 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
211#ifdef CONFIG_SECCOMP
212 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
213#endif
214 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
215 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
216 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
217 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
b4629fe2 218 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
63c6764c 219#ifdef CONFIG_MMU
e070ad49 220 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
63c6764c 221#endif
1da177e4
LT
222#ifdef CONFIG_SECURITY
223 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
224#endif
225#ifdef CONFIG_KALLSYMS
226 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
227#endif
228#ifdef CONFIG_SCHEDSTATS
229 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
230#endif
231#ifdef CONFIG_CPUSETS
232 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
233#endif
234 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
235 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
236#ifdef CONFIG_AUDITSYSCALL
237 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
238#endif
239 {0,0,NULL,0}
240};
241static struct pid_entry tid_base_stuff[] = {
242 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
243 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
244 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
245 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
246 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
247 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
248 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
249 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
6e21c8f1
CL
250#ifdef CONFIG_NUMA
251 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
252#endif
1da177e4
LT
253 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
254#ifdef CONFIG_SECCOMP
255 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
256#endif
257 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
258 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
259 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
260 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
63c6764c 261#ifdef CONFIG_MMU
e070ad49 262 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
63c6764c 263#endif
1da177e4
LT
264#ifdef CONFIG_SECURITY
265 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
266#endif
267#ifdef CONFIG_KALLSYMS
268 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
269#endif
270#ifdef CONFIG_SCHEDSTATS
271 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
272#endif
273#ifdef CONFIG_CPUSETS
274 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
275#endif
276 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
277 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
278#ifdef CONFIG_AUDITSYSCALL
279 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
280#endif
281 {0,0,NULL,0}
282};
283
284#ifdef CONFIG_SECURITY
285static struct pid_entry tgid_attr_stuff[] = {
286 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
287 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
288 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
289 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
4eb582cf 290 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
1da177e4
LT
291 {0,0,NULL,0}
292};
293static struct pid_entry tid_attr_stuff[] = {
294 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
295 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
296 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
297 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
4eb582cf 298 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
1da177e4
LT
299 {0,0,NULL,0}
300};
301#endif
302
303#undef E
304
305static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
306{
307 struct task_struct *task = proc_task(inode);
308 struct files_struct *files;
309 struct file *file;
aed7a6c4 310 int fd = proc_fd(inode);
1da177e4
LT
311
312 files = get_files_struct(task);
313 if (files) {
ca99c1da
DS
314 /*
315 * We are not taking a ref to the file structure, so we must
316 * hold ->file_lock.
317 */
318 spin_lock(&files->file_lock);
1da177e4
LT
319 file = fcheck_files(files, fd);
320 if (file) {
321 *mnt = mntget(file->f_vfsmnt);
322 *dentry = dget(file->f_dentry);
ca99c1da 323 spin_unlock(&files->file_lock);
1da177e4
LT
324 put_files_struct(files);
325 return 0;
326 }
ca99c1da 327 spin_unlock(&files->file_lock);
1da177e4
LT
328 put_files_struct(files);
329 }
330 return -ENOENT;
331}
332
0494f6ec 333static struct fs_struct *get_fs_struct(struct task_struct *task)
1da177e4
LT
334{
335 struct fs_struct *fs;
0494f6ec
MS
336 task_lock(task);
337 fs = task->fs;
1da177e4
LT
338 if(fs)
339 atomic_inc(&fs->count);
0494f6ec
MS
340 task_unlock(task);
341 return fs;
342}
343
344static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
345{
346 struct fs_struct *fs = get_fs_struct(proc_task(inode));
347 int result = -ENOENT;
1da177e4
LT
348 if (fs) {
349 read_lock(&fs->lock);
350 *mnt = mntget(fs->pwdmnt);
351 *dentry = dget(fs->pwd);
352 read_unlock(&fs->lock);
353 result = 0;
354 put_fs_struct(fs);
355 }
356 return result;
357}
358
359static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
360{
0494f6ec 361 struct fs_struct *fs = get_fs_struct(proc_task(inode));
1da177e4 362 int result = -ENOENT;
1da177e4
LT
363 if (fs) {
364 read_lock(&fs->lock);
365 *mnt = mntget(fs->rootmnt);
366 *dentry = dget(fs->root);
367 read_unlock(&fs->lock);
368 result = 0;
369 put_fs_struct(fs);
370 }
371 return result;
372}
373
374#define MAY_PTRACE(task) \
375 (task == current || \
376 (task->parent == current && \
377 (task->ptrace & PT_PTRACED) && \
378 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
379 security_ptrace(current,task) == 0))
380
1da177e4
LT
381static int proc_pid_environ(struct task_struct *task, char * buffer)
382{
383 int res = 0;
384 struct mm_struct *mm = get_task_mm(task);
385 if (mm) {
386 unsigned int len = mm->env_end - mm->env_start;
387 if (len > PAGE_SIZE)
388 len = PAGE_SIZE;
389 res = access_process_vm(task, mm->env_start, buffer, len, 0);
ab8d11be 390 if (!ptrace_may_attach(task))
1da177e4
LT
391 res = -ESRCH;
392 mmput(mm);
393 }
394 return res;
395}
396
397static int proc_pid_cmdline(struct task_struct *task, char * buffer)
398{
399 int res = 0;
400 unsigned int len;
401 struct mm_struct *mm = get_task_mm(task);
402 if (!mm)
403 goto out;
404 if (!mm->arg_end)
405 goto out_mm; /* Shh! No looking before we're done */
406
407 len = mm->arg_end - mm->arg_start;
408
409 if (len > PAGE_SIZE)
410 len = PAGE_SIZE;
411
412 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
413
414 // If the nul at the end of args has been overwritten, then
415 // assume application is using setproctitle(3).
416 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
417 len = strnlen(buffer, res);
418 if (len < res) {
419 res = len;
420 } else {
421 len = mm->env_end - mm->env_start;
422 if (len > PAGE_SIZE - res)
423 len = PAGE_SIZE - res;
424 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
425 res = strnlen(buffer, res);
426 }
427 }
428out_mm:
429 mmput(mm);
430out:
431 return res;
432}
433
434static int proc_pid_auxv(struct task_struct *task, char *buffer)
435{
436 int res = 0;
437 struct mm_struct *mm = get_task_mm(task);
438 if (mm) {
439 unsigned int nwords = 0;
440 do
441 nwords += 2;
442 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
443 res = nwords * sizeof(mm->saved_auxv[0]);
444 if (res > PAGE_SIZE)
445 res = PAGE_SIZE;
446 memcpy(buffer, mm->saved_auxv, res);
447 mmput(mm);
448 }
449 return res;
450}
451
452
453#ifdef CONFIG_KALLSYMS
454/*
455 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
456 * Returns the resolved symbol. If that fails, simply return the address.
457 */
458static int proc_pid_wchan(struct task_struct *task, char *buffer)
459{
460 char *modname;
461 const char *sym_name;
462 unsigned long wchan, size, offset;
463 char namebuf[KSYM_NAME_LEN+1];
464
465 wchan = get_wchan(task);
466
467 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
468 if (sym_name)
469 return sprintf(buffer, "%s", sym_name);
470 return sprintf(buffer, "%lu", wchan);
471}
472#endif /* CONFIG_KALLSYMS */
473
474#ifdef CONFIG_SCHEDSTATS
475/*
476 * Provides /proc/PID/schedstat
477 */
478static int proc_pid_schedstat(struct task_struct *task, char *buffer)
479{
480 return sprintf(buffer, "%lu %lu %lu\n",
481 task->sched_info.cpu_time,
482 task->sched_info.run_delay,
483 task->sched_info.pcnt);
484}
485#endif
486
487/* The badness from the OOM killer */
488unsigned long badness(struct task_struct *p, unsigned long uptime);
489static int proc_oom_score(struct task_struct *task, char *buffer)
490{
491 unsigned long points;
492 struct timespec uptime;
493
494 do_posix_clock_monotonic_gettime(&uptime);
495 points = badness(task, uptime.tv_sec);
496 return sprintf(buffer, "%lu\n", points);
497}
498
499/************************************************************************/
500/* Here the fs part begins */
501/************************************************************************/
502
503/* permission checks */
504
66dcca06
SK
505/* If the process being read is separated by chroot from the reading process,
506 * don't let the reader access the threads.
507 */
0f2fe20f 508static int proc_check_chroot(struct dentry *de, struct vfsmount *mnt)
1da177e4 509{
0f2fe20f
EB
510 struct dentry *base;
511 struct vfsmount *our_vfsmnt;
1da177e4 512 int res = 0;
e4e5d3fc 513
1da177e4
LT
514 read_lock(&current->fs->lock);
515 our_vfsmnt = mntget(current->fs->rootmnt);
516 base = dget(current->fs->root);
517 read_unlock(&current->fs->lock);
518
519 spin_lock(&vfsmount_lock);
1da177e4 520
e4e5d3fc
HP
521 while (mnt != our_vfsmnt) {
522 if (mnt == mnt->mnt_parent)
1da177e4 523 goto out;
e4e5d3fc
HP
524 de = mnt->mnt_mountpoint;
525 mnt = mnt->mnt_parent;
1da177e4
LT
526 }
527
528 if (!is_subdir(de, base))
529 goto out;
530 spin_unlock(&vfsmount_lock);
531
532exit:
533 dput(base);
534 mntput(our_vfsmnt);
1da177e4
LT
535 return res;
536out:
537 spin_unlock(&vfsmount_lock);
538 res = -EACCES;
539 goto exit;
540}
541
1da177e4 542extern struct seq_operations mounts_op;
5addc5dd
AV
543struct proc_mounts {
544 struct seq_file m;
545 int event;
546};
547
1da177e4
LT
548static int mounts_open(struct inode *inode, struct file *file)
549{
550 struct task_struct *task = proc_task(inode);
5addc5dd
AV
551 struct namespace *namespace;
552 struct proc_mounts *p;
553 int ret = -EINVAL;
1da177e4 554
5addc5dd
AV
555 task_lock(task);
556 namespace = task->namespace;
557 if (namespace)
558 get_namespace(namespace);
559 task_unlock(task);
560
561 if (namespace) {
562 ret = -ENOMEM;
563 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
564 if (p) {
565 file->private_data = &p->m;
566 ret = seq_open(file, &mounts_op);
567 if (!ret) {
568 p->m.private = namespace;
569 p->event = namespace->event;
570 return 0;
571 }
572 kfree(p);
1da177e4 573 }
5addc5dd 574 put_namespace(namespace);
1da177e4
LT
575 }
576 return ret;
577}
578
579static int mounts_release(struct inode *inode, struct file *file)
580{
581 struct seq_file *m = file->private_data;
582 struct namespace *namespace = m->private;
583 put_namespace(namespace);
584 return seq_release(inode, file);
585}
586
5addc5dd
AV
587static unsigned mounts_poll(struct file *file, poll_table *wait)
588{
589 struct proc_mounts *p = file->private_data;
590 struct namespace *ns = p->m.private;
591 unsigned res = 0;
592
593 poll_wait(file, &ns->poll, wait);
594
595 spin_lock(&vfsmount_lock);
596 if (p->event != ns->event) {
597 p->event = ns->event;
598 res = POLLERR;
599 }
600 spin_unlock(&vfsmount_lock);
601
602 return res;
603}
604
1da177e4
LT
605static struct file_operations proc_mounts_operations = {
606 .open = mounts_open,
607 .read = seq_read,
608 .llseek = seq_lseek,
609 .release = mounts_release,
5addc5dd 610 .poll = mounts_poll,
1da177e4
LT
611};
612
b4629fe2
CL
613extern struct seq_operations mountstats_op;
614static int mountstats_open(struct inode *inode, struct file *file)
615{
616 struct task_struct *task = proc_task(inode);
617 int ret = seq_open(file, &mountstats_op);
618
619 if (!ret) {
620 struct seq_file *m = file->private_data;
621 struct namespace *namespace;
622 task_lock(task);
623 namespace = task->namespace;
624 if (namespace)
625 get_namespace(namespace);
626 task_unlock(task);
627
628 if (namespace)
629 m->private = namespace;
630 else {
631 seq_release(inode, file);
632 ret = -EINVAL;
633 }
634 }
635 return ret;
636}
637
638static struct file_operations proc_mountstats_operations = {
639 .open = mountstats_open,
640 .read = seq_read,
641 .llseek = seq_lseek,
642 .release = mounts_release,
643};
644
1da177e4
LT
645#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
646
647static ssize_t proc_info_read(struct file * file, char __user * buf,
648 size_t count, loff_t *ppos)
649{
650 struct inode * inode = file->f_dentry->d_inode;
651 unsigned long page;
652 ssize_t length;
653 struct task_struct *task = proc_task(inode);
654
655 if (count > PROC_BLOCK_SIZE)
656 count = PROC_BLOCK_SIZE;
657 if (!(page = __get_free_page(GFP_KERNEL)))
658 return -ENOMEM;
659
660 length = PROC_I(inode)->op.proc_read(task, (char*)page);
661
662 if (length >= 0)
663 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
664 free_page(page);
665 return length;
666}
667
668static struct file_operations proc_info_file_operations = {
669 .read = proc_info_read,
670};
671
672static int mem_open(struct inode* inode, struct file* file)
673{
674 file->private_data = (void*)((long)current->self_exec_id);
675 return 0;
676}
677
678static ssize_t mem_read(struct file * file, char __user * buf,
679 size_t count, loff_t *ppos)
680{
681 struct task_struct *task = proc_task(file->f_dentry->d_inode);
682 char *page;
683 unsigned long src = *ppos;
684 int ret = -ESRCH;
685 struct mm_struct *mm;
686
ab8d11be 687 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
1da177e4
LT
688 goto out;
689
690 ret = -ENOMEM;
691 page = (char *)__get_free_page(GFP_USER);
692 if (!page)
693 goto out;
694
695 ret = 0;
696
697 mm = get_task_mm(task);
698 if (!mm)
699 goto out_free;
700
701 ret = -EIO;
702
703 if (file->private_data != (void*)((long)current->self_exec_id))
704 goto out_put;
705
706 ret = 0;
707
708 while (count > 0) {
709 int this_len, retval;
710
711 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
712 retval = access_process_vm(task, src, page, this_len, 0);
ab8d11be 713 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
1da177e4
LT
714 if (!ret)
715 ret = -EIO;
716 break;
717 }
718
719 if (copy_to_user(buf, page, retval)) {
720 ret = -EFAULT;
721 break;
722 }
723
724 ret += retval;
725 src += retval;
726 buf += retval;
727 count -= retval;
728 }
729 *ppos = src;
730
731out_put:
732 mmput(mm);
733out_free:
734 free_page((unsigned long) page);
735out:
736 return ret;
737}
738
739#define mem_write NULL
740
741#ifndef mem_write
742/* This is a security hazard */
743static ssize_t mem_write(struct file * file, const char * buf,
744 size_t count, loff_t *ppos)
745{
746 int copied = 0;
747 char *page;
748 struct task_struct *task = proc_task(file->f_dentry->d_inode);
749 unsigned long dst = *ppos;
750
ab8d11be 751 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
1da177e4
LT
752 return -ESRCH;
753
754 page = (char *)__get_free_page(GFP_USER);
755 if (!page)
756 return -ENOMEM;
757
758 while (count > 0) {
759 int this_len, retval;
760
761 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
762 if (copy_from_user(page, buf, this_len)) {
763 copied = -EFAULT;
764 break;
765 }
766 retval = access_process_vm(task, dst, page, this_len, 1);
767 if (!retval) {
768 if (!copied)
769 copied = -EIO;
770 break;
771 }
772 copied += retval;
773 buf += retval;
774 dst += retval;
775 count -= retval;
776 }
777 *ppos = dst;
778 free_page((unsigned long) page);
779 return copied;
780}
781#endif
782
783static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
784{
785 switch (orig) {
786 case 0:
787 file->f_pos = offset;
788 break;
789 case 1:
790 file->f_pos += offset;
791 break;
792 default:
793 return -EINVAL;
794 }
795 force_successful_syscall_return();
796 return file->f_pos;
797}
798
799static struct file_operations proc_mem_operations = {
800 .llseek = mem_lseek,
801 .read = mem_read,
802 .write = mem_write,
803 .open = mem_open,
804};
805
806static ssize_t oom_adjust_read(struct file *file, char __user *buf,
807 size_t count, loff_t *ppos)
808{
809 struct task_struct *task = proc_task(file->f_dentry->d_inode);
810 char buffer[8];
811 size_t len;
812 int oom_adjust = task->oomkilladj;
813 loff_t __ppos = *ppos;
814
815 len = sprintf(buffer, "%i\n", oom_adjust);
816 if (__ppos >= len)
817 return 0;
818 if (count > len-__ppos)
819 count = len-__ppos;
820 if (copy_to_user(buf, buffer + __ppos, count))
821 return -EFAULT;
822 *ppos = __ppos + count;
823 return count;
824}
825
826static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
827 size_t count, loff_t *ppos)
828{
829 struct task_struct *task = proc_task(file->f_dentry->d_inode);
830 char buffer[8], *end;
831 int oom_adjust;
832
833 if (!capable(CAP_SYS_RESOURCE))
834 return -EPERM;
835 memset(buffer, 0, 8);
836 if (count > 6)
837 count = 6;
838 if (copy_from_user(buffer, buf, count))
839 return -EFAULT;
840 oom_adjust = simple_strtol(buffer, &end, 0);
79befd0c 841 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
1da177e4
LT
842 return -EINVAL;
843 if (*end == '\n')
844 end++;
845 task->oomkilladj = oom_adjust;
846 if (end - buffer == 0)
847 return -EIO;
848 return end - buffer;
849}
850
851static struct file_operations proc_oom_adjust_operations = {
852 .read = oom_adjust_read,
853 .write = oom_adjust_write,
854};
855
1da177e4
LT
856#ifdef CONFIG_AUDITSYSCALL
857#define TMPBUFLEN 21
858static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
859 size_t count, loff_t *ppos)
860{
861 struct inode * inode = file->f_dentry->d_inode;
862 struct task_struct *task = proc_task(inode);
863 ssize_t length;
864 char tmpbuf[TMPBUFLEN];
865
866 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
867 audit_get_loginuid(task->audit_context));
868 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
869}
870
871static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
872 size_t count, loff_t *ppos)
873{
874 struct inode * inode = file->f_dentry->d_inode;
875 char *page, *tmp;
876 ssize_t length;
877 struct task_struct *task = proc_task(inode);
878 uid_t loginuid;
879
880 if (!capable(CAP_AUDIT_CONTROL))
881 return -EPERM;
882
883 if (current != task)
884 return -EPERM;
885
e0182909
AV
886 if (count >= PAGE_SIZE)
887 count = PAGE_SIZE - 1;
1da177e4
LT
888
889 if (*ppos != 0) {
890 /* No partial writes. */
891 return -EINVAL;
892 }
893 page = (char*)__get_free_page(GFP_USER);
894 if (!page)
895 return -ENOMEM;
896 length = -EFAULT;
897 if (copy_from_user(page, buf, count))
898 goto out_free_page;
899
e0182909 900 page[count] = '\0';
1da177e4
LT
901 loginuid = simple_strtoul(page, &tmp, 10);
902 if (tmp == page) {
903 length = -EINVAL;
904 goto out_free_page;
905
906 }
456be6cd 907 length = audit_set_loginuid(task, loginuid);
1da177e4
LT
908 if (likely(length == 0))
909 length = count;
910
911out_free_page:
912 free_page((unsigned long) page);
913 return length;
914}
915
916static struct file_operations proc_loginuid_operations = {
917 .read = proc_loginuid_read,
918 .write = proc_loginuid_write,
919};
920#endif
921
922#ifdef CONFIG_SECCOMP
923static ssize_t seccomp_read(struct file *file, char __user *buf,
924 size_t count, loff_t *ppos)
925{
926 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
927 char __buf[20];
928 loff_t __ppos = *ppos;
929 size_t len;
930
931 /* no need to print the trailing zero, so use only len */
932 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
933 if (__ppos >= len)
934 return 0;
935 if (count > len - __ppos)
936 count = len - __ppos;
937 if (copy_to_user(buf, __buf + __ppos, count))
938 return -EFAULT;
939 *ppos = __ppos + count;
940 return count;
941}
942
943static ssize_t seccomp_write(struct file *file, const char __user *buf,
944 size_t count, loff_t *ppos)
945{
946 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
947 char __buf[20], *end;
948 unsigned int seccomp_mode;
949
950 /* can set it only once to be even more secure */
951 if (unlikely(tsk->seccomp.mode))
952 return -EPERM;
953
954 memset(__buf, 0, sizeof(__buf));
955 count = min(count, sizeof(__buf) - 1);
956 if (copy_from_user(__buf, buf, count))
957 return -EFAULT;
958 seccomp_mode = simple_strtoul(__buf, &end, 0);
959 if (*end == '\n')
960 end++;
961 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
962 tsk->seccomp.mode = seccomp_mode;
963 set_tsk_thread_flag(tsk, TIF_SECCOMP);
964 } else
965 return -EINVAL;
966 if (unlikely(!(end - __buf)))
967 return -EIO;
968 return end - __buf;
969}
970
971static struct file_operations proc_seccomp_operations = {
972 .read = seccomp_read,
973 .write = seccomp_write,
974};
975#endif /* CONFIG_SECCOMP */
976
0f2fe20f
EB
977static int proc_check_dentry_visible(struct inode *inode,
978 struct dentry *dentry, struct vfsmount *mnt)
979{
980 /* Verify that the current process can already see the
981 * file pointed at by the file descriptor.
982 * This prevents /proc from being an accidental information leak.
983 *
984 * This prevents access to files that are not visible do to
985 * being on the otherside of a chroot, in a different
986 * namespace, or are simply process local (like pipes).
987 */
988 struct task_struct *task;
989 struct files_struct *task_files, *files;
990 int error = -EACCES;
991
992 /* See if the the two tasks share a commone set of
993 * file descriptors. If so everything is visible.
994 */
995 task = proc_task(inode);
996 if (!task)
997 goto out;
998 files = get_files_struct(current);
999 task_files = get_files_struct(task);
1000 if (files && task_files && (files == task_files))
1001 error = 0;
1002 if (task_files)
1003 put_files_struct(task_files);
1004 if (files)
1005 put_files_struct(files);
1006 if (!error)
1007 goto out;
1008
1009 /* If the two tasks don't share a common set of file
1010 * descriptors see if the destination dentry is already
1011 * visible in the current tasks filesystem namespace.
1012 */
1013 error = proc_check_chroot(dentry, mnt);
1014out:
1015 return error;
1016
1017}
1018
008b150a 1019static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1020{
1021 struct inode *inode = dentry->d_inode;
1022 int error = -EACCES;
1023
1024 /* We don't need a base pointer in the /proc filesystem */
1025 path_release(nd);
1026
1027 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1028 goto out;
1da177e4
LT
1029
1030 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1031 nd->last_type = LAST_BIND;
0f2fe20f
EB
1032 if (error)
1033 goto out;
1034
1035 /* Only return files this task can already see */
1036 error = proc_check_dentry_visible(inode, nd->dentry, nd->mnt);
1037 if (error)
1038 path_release(nd);
1da177e4 1039out:
008b150a 1040 return ERR_PTR(error);
1da177e4
LT
1041}
1042
1043static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1044 char __user *buffer, int buflen)
1045{
1046 struct inode * inode;
1047 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1048 int len;
1049
1050 if (!tmp)
1051 return -ENOMEM;
1052
1053 inode = dentry->d_inode;
1054 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1055 len = PTR_ERR(path);
1056 if (IS_ERR(path))
1057 goto out;
1058 len = tmp + PAGE_SIZE - 1 - path;
1059
1060 if (len > buflen)
1061 len = buflen;
1062 if (copy_to_user(buffer, path, len))
1063 len = -EFAULT;
1064 out:
1065 free_page((unsigned long)tmp);
1066 return len;
1067}
1068
1069static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1070{
1071 int error = -EACCES;
1072 struct inode *inode = dentry->d_inode;
1073 struct dentry *de;
1074 struct vfsmount *mnt = NULL;
1075
1da177e4
LT
1076
1077 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1078 goto out;
1da177e4
LT
1079
1080 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1081 if (error)
1082 goto out;
1083
0f2fe20f
EB
1084 /* Only return files this task can already see */
1085 error = proc_check_dentry_visible(inode, de, mnt);
1086 if (error)
1087 goto out_put;
1088
1da177e4 1089 error = do_proc_readlink(de, mnt, buffer, buflen);
0f2fe20f 1090out_put:
1da177e4
LT
1091 dput(de);
1092 mntput(mnt);
1093out:
1da177e4
LT
1094 return error;
1095}
1096
1097static struct inode_operations proc_pid_link_inode_operations = {
1098 .readlink = proc_pid_readlink,
1099 .follow_link = proc_pid_follow_link
1100};
1101
1102#define NUMBUF 10
1103
1104static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1105{
5634708b
EB
1106 struct dentry *dentry = filp->f_dentry;
1107 struct inode *inode = dentry->d_inode;
1da177e4
LT
1108 struct task_struct *p = proc_task(inode);
1109 unsigned int fd, tid, ino;
1110 int retval;
1111 char buf[NUMBUF];
1112 struct files_struct * files;
badf1662 1113 struct fdtable *fdt;
1da177e4
LT
1114
1115 retval = -ENOENT;
1116 if (!pid_alive(p))
1117 goto out;
1118 retval = 0;
1119 tid = p->pid;
1120
1121 fd = filp->f_pos;
1122 switch (fd) {
1123 case 0:
1124 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1125 goto out;
1126 filp->f_pos++;
1127 case 1:
5634708b 1128 ino = parent_ino(dentry);
1da177e4
LT
1129 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1130 goto out;
1131 filp->f_pos++;
1132 default:
1133 files = get_files_struct(p);
1134 if (!files)
1135 goto out;
b835996f 1136 rcu_read_lock();
badf1662 1137 fdt = files_fdtable(files);
1da177e4 1138 for (fd = filp->f_pos-2;
badf1662 1139 fd < fdt->max_fds;
1da177e4
LT
1140 fd++, filp->f_pos++) {
1141 unsigned int i,j;
1142
1143 if (!fcheck_files(files, fd))
1144 continue;
b835996f 1145 rcu_read_unlock();
1da177e4
LT
1146
1147 j = NUMBUF;
1148 i = fd;
1149 do {
1150 j--;
1151 buf[j] = '0' + (i % 10);
1152 i /= 10;
1153 } while (i);
1154
1155 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1156 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
b835996f 1157 rcu_read_lock();
1da177e4
LT
1158 break;
1159 }
b835996f 1160 rcu_read_lock();
1da177e4 1161 }
b835996f 1162 rcu_read_unlock();
1da177e4
LT
1163 put_files_struct(files);
1164 }
1165out:
1166 return retval;
1167}
1168
1169static int proc_pident_readdir(struct file *filp,
1170 void *dirent, filldir_t filldir,
1171 struct pid_entry *ents, unsigned int nents)
1172{
1173 int i;
1174 int pid;
1175 struct dentry *dentry = filp->f_dentry;
1176 struct inode *inode = dentry->d_inode;
1177 struct pid_entry *p;
1178 ino_t ino;
1179 int ret;
1180
1181 ret = -ENOENT;
1182 if (!pid_alive(proc_task(inode)))
1183 goto out;
1184
1185 ret = 0;
1186 pid = proc_task(inode)->pid;
1187 i = filp->f_pos;
1188 switch (i) {
1189 case 0:
1190 ino = inode->i_ino;
1191 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1192 goto out;
1193 i++;
1194 filp->f_pos++;
1195 /* fall through */
1196 case 1:
1197 ino = parent_ino(dentry);
1198 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1199 goto out;
1200 i++;
1201 filp->f_pos++;
1202 /* fall through */
1203 default:
1204 i -= 2;
1205 if (i >= nents) {
1206 ret = 1;
1207 goto out;
1208 }
1209 p = ents + i;
1210 while (p->name) {
1211 if (filldir(dirent, p->name, p->len, filp->f_pos,
1212 fake_ino(pid, p->type), p->mode >> 12) < 0)
1213 goto out;
1214 filp->f_pos++;
1215 p++;
1216 }
1217 }
1218
1219 ret = 1;
1220out:
1221 return ret;
1222}
1223
1224static int proc_tgid_base_readdir(struct file * filp,
1225 void * dirent, filldir_t filldir)
1226{
1227 return proc_pident_readdir(filp,dirent,filldir,
1228 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1229}
1230
1231static int proc_tid_base_readdir(struct file * filp,
1232 void * dirent, filldir_t filldir)
1233{
1234 return proc_pident_readdir(filp,dirent,filldir,
1235 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1236}
1237
1238/* building an inode */
1239
1240static int task_dumpable(struct task_struct *task)
1241{
1242 int dumpable = 0;
1243 struct mm_struct *mm;
1244
1245 task_lock(task);
1246 mm = task->mm;
1247 if (mm)
1248 dumpable = mm->dumpable;
1249 task_unlock(task);
d6e71144
AC
1250 if(dumpable == 1)
1251 return 1;
1252 return 0;
1da177e4
LT
1253}
1254
1255
1256static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1257{
1258 struct inode * inode;
1259 struct proc_inode *ei;
1260
1261 /* We need a new inode */
1262
1263 inode = new_inode(sb);
1264 if (!inode)
1265 goto out;
1266
1267 /* Common stuff */
1268 ei = PROC_I(inode);
1da177e4
LT
1269 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1270 inode->i_ino = fake_ino(task->pid, ino);
1271
1272 if (!pid_alive(task))
1273 goto out_unlock;
1274
1275 /*
1276 * grab the reference to task.
1277 */
1278 get_task_struct(task);
1279 ei->task = task;
1da177e4
LT
1280 inode->i_uid = 0;
1281 inode->i_gid = 0;
87bfbf67 1282 if (task_dumpable(task)) {
1da177e4
LT
1283 inode->i_uid = task->euid;
1284 inode->i_gid = task->egid;
1285 }
1286 security_task_to_inode(task, inode);
1287
1288out:
1289 return inode;
1290
1291out_unlock:
1da177e4
LT
1292 iput(inode);
1293 return NULL;
1294}
1295
1296/* dentry stuff */
1297
1298/*
1299 * Exceptional case: normally we are not allowed to unhash a busy
1300 * directory. In this case, however, we can do it - no aliasing problems
1301 * due to the way we treat inodes.
1302 *
1303 * Rewrite the inode's ownerships here because the owning task may have
1304 * performed a setuid(), etc.
1305 */
1306static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1307{
1308 struct inode *inode = dentry->d_inode;
1309 struct task_struct *task = proc_task(inode);
1310 if (pid_alive(task)) {
87bfbf67 1311 if (task_dumpable(task)) {
1da177e4
LT
1312 inode->i_uid = task->euid;
1313 inode->i_gid = task->egid;
1314 } else {
1315 inode->i_uid = 0;
1316 inode->i_gid = 0;
1317 }
1318 security_task_to_inode(task, inode);
1319 return 1;
1320 }
1321 d_drop(dentry);
1322 return 0;
1323}
1324
1325static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1326{
1327 struct inode *inode = dentry->d_inode;
1328 struct task_struct *task = proc_task(inode);
aed7a6c4 1329 int fd = proc_fd(inode);
1da177e4
LT
1330 struct files_struct *files;
1331
1332 files = get_files_struct(task);
1333 if (files) {
b835996f 1334 rcu_read_lock();
1da177e4 1335 if (fcheck_files(files, fd)) {
b835996f 1336 rcu_read_unlock();
1da177e4
LT
1337 put_files_struct(files);
1338 if (task_dumpable(task)) {
1339 inode->i_uid = task->euid;
1340 inode->i_gid = task->egid;
1341 } else {
1342 inode->i_uid = 0;
1343 inode->i_gid = 0;
1344 }
1345 security_task_to_inode(task, inode);
1346 return 1;
1347 }
b835996f 1348 rcu_read_unlock();
1da177e4
LT
1349 put_files_struct(files);
1350 }
1351 d_drop(dentry);
1352 return 0;
1353}
1354
1da177e4
LT
1355static int pid_delete_dentry(struct dentry * dentry)
1356{
1357 /* Is the task we represent dead?
1358 * If so, then don't put the dentry on the lru list,
1359 * kill it immediately.
1360 */
1361 return !pid_alive(proc_task(dentry->d_inode));
1362}
1363
1364static struct dentry_operations tid_fd_dentry_operations =
1365{
1366 .d_revalidate = tid_fd_revalidate,
1367 .d_delete = pid_delete_dentry,
1368};
1369
1370static struct dentry_operations pid_dentry_operations =
1371{
1372 .d_revalidate = pid_revalidate,
1373 .d_delete = pid_delete_dentry,
1374};
1375
1da177e4
LT
1376/* Lookups */
1377
1378static unsigned name_to_int(struct dentry *dentry)
1379{
1380 const char *name = dentry->d_name.name;
1381 int len = dentry->d_name.len;
1382 unsigned n = 0;
1383
1384 if (len > 1 && *name == '0')
1385 goto out;
1386 while (len-- > 0) {
1387 unsigned c = *name++ - '0';
1388 if (c > 9)
1389 goto out;
1390 if (n >= (~0U-9)/10)
1391 goto out;
1392 n *= 10;
1393 n += c;
1394 }
1395 return n;
1396out:
1397 return ~0U;
1398}
1399
1400/* SMP-safe */
1401static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1402{
1403 struct task_struct *task = proc_task(dir);
1404 unsigned fd = name_to_int(dentry);
cd6a3ce9 1405 struct dentry *result = ERR_PTR(-ENOENT);
1da177e4
LT
1406 struct file * file;
1407 struct files_struct * files;
1408 struct inode *inode;
1409 struct proc_inode *ei;
1410
1411 if (fd == ~0U)
1412 goto out;
1413 if (!pid_alive(task))
1414 goto out;
1415
1416 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1417 if (!inode)
1418 goto out;
1419 ei = PROC_I(inode);
aed7a6c4 1420 ei->fd = fd;
1da177e4
LT
1421 files = get_files_struct(task);
1422 if (!files)
1423 goto out_unlock;
1424 inode->i_mode = S_IFLNK;
ca99c1da
DS
1425
1426 /*
1427 * We are not taking a ref to the file structure, so we must
1428 * hold ->file_lock.
1429 */
1430 spin_lock(&files->file_lock);
1da177e4
LT
1431 file = fcheck_files(files, fd);
1432 if (!file)
1433 goto out_unlock2;
1434 if (file->f_mode & 1)
1435 inode->i_mode |= S_IRUSR | S_IXUSR;
1436 if (file->f_mode & 2)
1437 inode->i_mode |= S_IWUSR | S_IXUSR;
ca99c1da 1438 spin_unlock(&files->file_lock);
1da177e4
LT
1439 put_files_struct(files);
1440 inode->i_op = &proc_pid_link_inode_operations;
1441 inode->i_size = 64;
1442 ei->op.proc_get_link = proc_fd_link;
1443 dentry->d_op = &tid_fd_dentry_operations;
1444 d_add(dentry, inode);
cd6a3ce9
EB
1445 /* Close the race of the process dying before we return the dentry */
1446 if (tid_fd_revalidate(dentry, NULL))
1447 result = NULL;
1448out:
1449 return result;
1da177e4
LT
1450
1451out_unlock2:
ca99c1da 1452 spin_unlock(&files->file_lock);
1da177e4
LT
1453 put_files_struct(files);
1454out_unlock:
1455 iput(inode);
cd6a3ce9 1456 goto out;
1da177e4
LT
1457}
1458
1459static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1460static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
6e66b52b 1461static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
1da177e4
LT
1462
1463static struct file_operations proc_fd_operations = {
1464 .read = generic_read_dir,
1465 .readdir = proc_readfd,
1466};
1467
1468static struct file_operations proc_task_operations = {
1469 .read = generic_read_dir,
1470 .readdir = proc_task_readdir,
1471};
1472
1473/*
1474 * proc directories can do almost nothing..
1475 */
1476static struct inode_operations proc_fd_inode_operations = {
1477 .lookup = proc_lookupfd,
1da177e4
LT
1478};
1479
1480static struct inode_operations proc_task_inode_operations = {
1481 .lookup = proc_task_lookup,
6e66b52b 1482 .getattr = proc_task_getattr,
1da177e4
LT
1483};
1484
1485#ifdef CONFIG_SECURITY
1486static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1487 size_t count, loff_t *ppos)
1488{
1489 struct inode * inode = file->f_dentry->d_inode;
1490 unsigned long page;
1491 ssize_t length;
1492 struct task_struct *task = proc_task(inode);
1493
1494 if (count > PAGE_SIZE)
1495 count = PAGE_SIZE;
1496 if (!(page = __get_free_page(GFP_KERNEL)))
1497 return -ENOMEM;
1498
1499 length = security_getprocattr(task,
1500 (char*)file->f_dentry->d_name.name,
1501 (void*)page, count);
1502 if (length >= 0)
1503 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1504 free_page(page);
1505 return length;
1506}
1507
1508static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1509 size_t count, loff_t *ppos)
1510{
1511 struct inode * inode = file->f_dentry->d_inode;
1512 char *page;
1513 ssize_t length;
1514 struct task_struct *task = proc_task(inode);
1515
1516 if (count > PAGE_SIZE)
1517 count = PAGE_SIZE;
1518 if (*ppos != 0) {
1519 /* No partial writes. */
1520 return -EINVAL;
1521 }
1522 page = (char*)__get_free_page(GFP_USER);
1523 if (!page)
1524 return -ENOMEM;
1525 length = -EFAULT;
1526 if (copy_from_user(page, buf, count))
1527 goto out;
1528
1529 length = security_setprocattr(task,
1530 (char*)file->f_dentry->d_name.name,
1531 (void*)page, count);
1532out:
1533 free_page((unsigned long) page);
1534 return length;
1535}
1536
1537static struct file_operations proc_pid_attr_operations = {
1538 .read = proc_pid_attr_read,
1539 .write = proc_pid_attr_write,
1540};
1541
1542static struct file_operations proc_tid_attr_operations;
1543static struct inode_operations proc_tid_attr_inode_operations;
1544static struct file_operations proc_tgid_attr_operations;
1545static struct inode_operations proc_tgid_attr_inode_operations;
1546#endif
1547
f246315e
DD
1548static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1549
1da177e4
LT
1550/* SMP-safe */
1551static struct dentry *proc_pident_lookup(struct inode *dir,
1552 struct dentry *dentry,
1553 struct pid_entry *ents)
1554{
1555 struct inode *inode;
cd6a3ce9 1556 struct dentry *error;
1da177e4
LT
1557 struct task_struct *task = proc_task(dir);
1558 struct pid_entry *p;
1559 struct proc_inode *ei;
1560
cd6a3ce9 1561 error = ERR_PTR(-ENOENT);
1da177e4
LT
1562 inode = NULL;
1563
1564 if (!pid_alive(task))
1565 goto out;
1566
1567 for (p = ents; p->name; p++) {
1568 if (p->len != dentry->d_name.len)
1569 continue;
1570 if (!memcmp(dentry->d_name.name, p->name, p->len))
1571 break;
1572 }
1573 if (!p->name)
1574 goto out;
1575
cd6a3ce9 1576 error = ERR_PTR(-EINVAL);
1da177e4
LT
1577 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1578 if (!inode)
1579 goto out;
1580
1581 ei = PROC_I(inode);
1582 inode->i_mode = p->mode;
1583 /*
1584 * Yes, it does not scale. And it should not. Don't add
1585 * new entries into /proc/<tgid>/ without very good reasons.
1586 */
1587 switch(p->type) {
1588 case PROC_TGID_TASK:
6e66b52b 1589 inode->i_nlink = 2;
1da177e4
LT
1590 inode->i_op = &proc_task_inode_operations;
1591 inode->i_fop = &proc_task_operations;
1592 break;
1593 case PROC_TID_FD:
1594 case PROC_TGID_FD:
1595 inode->i_nlink = 2;
1596 inode->i_op = &proc_fd_inode_operations;
1597 inode->i_fop = &proc_fd_operations;
1598 break;
1599 case PROC_TID_EXE:
1600 case PROC_TGID_EXE:
1601 inode->i_op = &proc_pid_link_inode_operations;
1602 ei->op.proc_get_link = proc_exe_link;
1603 break;
1604 case PROC_TID_CWD:
1605 case PROC_TGID_CWD:
1606 inode->i_op = &proc_pid_link_inode_operations;
1607 ei->op.proc_get_link = proc_cwd_link;
1608 break;
1609 case PROC_TID_ROOT:
1610 case PROC_TGID_ROOT:
1611 inode->i_op = &proc_pid_link_inode_operations;
1612 ei->op.proc_get_link = proc_root_link;
1613 break;
1614 case PROC_TID_ENVIRON:
1615 case PROC_TGID_ENVIRON:
1616 inode->i_fop = &proc_info_file_operations;
1617 ei->op.proc_read = proc_pid_environ;
1618 break;
1619 case PROC_TID_AUXV:
1620 case PROC_TGID_AUXV:
1621 inode->i_fop = &proc_info_file_operations;
1622 ei->op.proc_read = proc_pid_auxv;
1623 break;
1624 case PROC_TID_STATUS:
1625 case PROC_TGID_STATUS:
1626 inode->i_fop = &proc_info_file_operations;
1627 ei->op.proc_read = proc_pid_status;
1628 break;
1629 case PROC_TID_STAT:
1630 inode->i_fop = &proc_info_file_operations;
1631 ei->op.proc_read = proc_tid_stat;
1632 break;
1633 case PROC_TGID_STAT:
1634 inode->i_fop = &proc_info_file_operations;
1635 ei->op.proc_read = proc_tgid_stat;
1636 break;
1637 case PROC_TID_CMDLINE:
1638 case PROC_TGID_CMDLINE:
1639 inode->i_fop = &proc_info_file_operations;
1640 ei->op.proc_read = proc_pid_cmdline;
1641 break;
1642 case PROC_TID_STATM:
1643 case PROC_TGID_STATM:
1644 inode->i_fop = &proc_info_file_operations;
1645 ei->op.proc_read = proc_pid_statm;
1646 break;
1647 case PROC_TID_MAPS:
1648 case PROC_TGID_MAPS:
1649 inode->i_fop = &proc_maps_operations;
1650 break;
6e21c8f1
CL
1651#ifdef CONFIG_NUMA
1652 case PROC_TID_NUMA_MAPS:
1653 case PROC_TGID_NUMA_MAPS:
1654 inode->i_fop = &proc_numa_maps_operations;
1655 break;
1656#endif
1da177e4
LT
1657 case PROC_TID_MEM:
1658 case PROC_TGID_MEM:
1da177e4
LT
1659 inode->i_fop = &proc_mem_operations;
1660 break;
1661#ifdef CONFIG_SECCOMP
1662 case PROC_TID_SECCOMP:
1663 case PROC_TGID_SECCOMP:
1664 inode->i_fop = &proc_seccomp_operations;
1665 break;
1666#endif /* CONFIG_SECCOMP */
1667 case PROC_TID_MOUNTS:
1668 case PROC_TGID_MOUNTS:
1669 inode->i_fop = &proc_mounts_operations;
1670 break;
63c6764c 1671#ifdef CONFIG_MMU
e070ad49
ML
1672 case PROC_TID_SMAPS:
1673 case PROC_TGID_SMAPS:
1674 inode->i_fop = &proc_smaps_operations;
1675 break;
63c6764c 1676#endif
b4629fe2
CL
1677 case PROC_TID_MOUNTSTATS:
1678 case PROC_TGID_MOUNTSTATS:
1679 inode->i_fop = &proc_mountstats_operations;
1680 break;
1da177e4
LT
1681#ifdef CONFIG_SECURITY
1682 case PROC_TID_ATTR:
1683 inode->i_nlink = 2;
1684 inode->i_op = &proc_tid_attr_inode_operations;
1685 inode->i_fop = &proc_tid_attr_operations;
1686 break;
1687 case PROC_TGID_ATTR:
1688 inode->i_nlink = 2;
1689 inode->i_op = &proc_tgid_attr_inode_operations;
1690 inode->i_fop = &proc_tgid_attr_operations;
1691 break;
1692 case PROC_TID_ATTR_CURRENT:
1693 case PROC_TGID_ATTR_CURRENT:
1694 case PROC_TID_ATTR_PREV:
1695 case PROC_TGID_ATTR_PREV:
1696 case PROC_TID_ATTR_EXEC:
1697 case PROC_TGID_ATTR_EXEC:
1698 case PROC_TID_ATTR_FSCREATE:
1699 case PROC_TGID_ATTR_FSCREATE:
4eb582cf
ML
1700 case PROC_TID_ATTR_KEYCREATE:
1701 case PROC_TGID_ATTR_KEYCREATE:
1da177e4
LT
1702 inode->i_fop = &proc_pid_attr_operations;
1703 break;
1704#endif
1705#ifdef CONFIG_KALLSYMS
1706 case PROC_TID_WCHAN:
1707 case PROC_TGID_WCHAN:
1708 inode->i_fop = &proc_info_file_operations;
1709 ei->op.proc_read = proc_pid_wchan;
1710 break;
1711#endif
1712#ifdef CONFIG_SCHEDSTATS
1713 case PROC_TID_SCHEDSTAT:
1714 case PROC_TGID_SCHEDSTAT:
1715 inode->i_fop = &proc_info_file_operations;
1716 ei->op.proc_read = proc_pid_schedstat;
1717 break;
1718#endif
1719#ifdef CONFIG_CPUSETS
1720 case PROC_TID_CPUSET:
1721 case PROC_TGID_CPUSET:
1722 inode->i_fop = &proc_cpuset_operations;
1723 break;
1724#endif
1725 case PROC_TID_OOM_SCORE:
1726 case PROC_TGID_OOM_SCORE:
1727 inode->i_fop = &proc_info_file_operations;
1728 ei->op.proc_read = proc_oom_score;
1729 break;
1730 case PROC_TID_OOM_ADJUST:
1731 case PROC_TGID_OOM_ADJUST:
1732 inode->i_fop = &proc_oom_adjust_operations;
1733 break;
1734#ifdef CONFIG_AUDITSYSCALL
1735 case PROC_TID_LOGINUID:
1736 case PROC_TGID_LOGINUID:
1737 inode->i_fop = &proc_loginuid_operations;
1738 break;
1739#endif
1740 default:
1741 printk("procfs: impossible type (%d)",p->type);
1742 iput(inode);
cd6a3ce9
EB
1743 error = ERR_PTR(-EINVAL);
1744 goto out;
1da177e4
LT
1745 }
1746 dentry->d_op = &pid_dentry_operations;
1747 d_add(dentry, inode);
cd6a3ce9
EB
1748 /* Close the race of the process dying before we return the dentry */
1749 if (pid_revalidate(dentry, NULL))
1750 error = NULL;
1da177e4 1751out:
cd6a3ce9 1752 return error;
1da177e4
LT
1753}
1754
1755static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1756 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1757}
1758
1759static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1760 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1761}
1762
1763static struct file_operations proc_tgid_base_operations = {
1764 .read = generic_read_dir,
1765 .readdir = proc_tgid_base_readdir,
1766};
1767
1768static struct file_operations proc_tid_base_operations = {
1769 .read = generic_read_dir,
1770 .readdir = proc_tid_base_readdir,
1771};
1772
1773static struct inode_operations proc_tgid_base_inode_operations = {
1774 .lookup = proc_tgid_base_lookup,
1775};
1776
1777static struct inode_operations proc_tid_base_inode_operations = {
1778 .lookup = proc_tid_base_lookup,
1779};
1780
1781#ifdef CONFIG_SECURITY
1782static int proc_tgid_attr_readdir(struct file * filp,
1783 void * dirent, filldir_t filldir)
1784{
1785 return proc_pident_readdir(filp,dirent,filldir,
1786 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1787}
1788
1789static int proc_tid_attr_readdir(struct file * filp,
1790 void * dirent, filldir_t filldir)
1791{
1792 return proc_pident_readdir(filp,dirent,filldir,
1793 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1794}
1795
1796static struct file_operations proc_tgid_attr_operations = {
1797 .read = generic_read_dir,
1798 .readdir = proc_tgid_attr_readdir,
1799};
1800
1801static struct file_operations proc_tid_attr_operations = {
1802 .read = generic_read_dir,
1803 .readdir = proc_tid_attr_readdir,
1804};
1805
1806static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1807 struct dentry *dentry, struct nameidata *nd)
1808{
1809 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1810}
1811
1812static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1813 struct dentry *dentry, struct nameidata *nd)
1814{
1815 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1816}
1817
1818static struct inode_operations proc_tgid_attr_inode_operations = {
1819 .lookup = proc_tgid_attr_lookup,
1820};
1821
1822static struct inode_operations proc_tid_attr_inode_operations = {
1823 .lookup = proc_tid_attr_lookup,
1824};
1825#endif
1826
1827/*
1828 * /proc/self:
1829 */
1830static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1831 int buflen)
1832{
1833 char tmp[30];
1834 sprintf(tmp, "%d", current->tgid);
1835 return vfs_readlink(dentry,buffer,buflen,tmp);
1836}
1837
008b150a 1838static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1839{
1840 char tmp[30];
1841 sprintf(tmp, "%d", current->tgid);
008b150a 1842 return ERR_PTR(vfs_follow_link(nd,tmp));
1da177e4
LT
1843}
1844
1845static struct inode_operations proc_self_inode_operations = {
1846 .readlink = proc_self_readlink,
1847 .follow_link = proc_self_follow_link,
1848};
1849
1850/**
48e6484d
EB
1851 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1852 *
1853 * @task: task that should be flushed.
1854 *
1855 * Looks in the dcache for
1856 * /proc/@pid
1857 * /proc/@tgid/task/@pid
1858 * if either directory is present flushes it and all of it'ts children
1859 * from the dcache.
1da177e4 1860 *
48e6484d
EB
1861 * It is safe and reasonable to cache /proc entries for a task until
1862 * that task exits. After that they just clog up the dcache with
1863 * useless entries, possibly causing useful dcache entries to be
1864 * flushed instead. This routine is proved to flush those useless
1865 * dcache entries at process exit time.
1da177e4 1866 *
48e6484d
EB
1867 * NOTE: This routine is just an optimization so it does not guarantee
1868 * that no dcache entries will exist at process exit time it
1869 * just makes it very unlikely that any will persist.
1da177e4 1870 */
48e6484d 1871void proc_flush_task(struct task_struct *task)
1da177e4 1872{
48e6484d
EB
1873 struct dentry *dentry, *leader, *dir;
1874 char buf[30];
1875 struct qstr name;
1876
1877 name.name = buf;
1878 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1879 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1880 if (dentry) {
1881 shrink_dcache_parent(dentry);
1882 d_drop(dentry);
1883 dput(dentry);
1884 }
1da177e4 1885
48e6484d
EB
1886 if (thread_group_leader(task))
1887 goto out;
1da177e4 1888
48e6484d
EB
1889 name.name = buf;
1890 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1891 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1892 if (!leader)
1893 goto out;
1da177e4 1894
48e6484d
EB
1895 name.name = "task";
1896 name.len = strlen(name.name);
1897 dir = d_hash_and_lookup(leader, &name);
1898 if (!dir)
1899 goto out_put_leader;
1900
1901 name.name = buf;
1902 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1903 dentry = d_hash_and_lookup(dir, &name);
1904 if (dentry) {
1905 shrink_dcache_parent(dentry);
1906 d_drop(dentry);
1907 dput(dentry);
1da177e4 1908 }
48e6484d
EB
1909
1910 dput(dir);
1911out_put_leader:
1912 dput(leader);
1913out:
1914 return;
1da177e4
LT
1915}
1916
1917/* SMP-safe */
1918struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1919{
cd6a3ce9 1920 struct dentry *result = ERR_PTR(-ENOENT);
1da177e4
LT
1921 struct task_struct *task;
1922 struct inode *inode;
1923 struct proc_inode *ei;
1924 unsigned tgid;
1da177e4
LT
1925
1926 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1927 inode = new_inode(dir->i_sb);
1928 if (!inode)
1929 return ERR_PTR(-ENOMEM);
1930 ei = PROC_I(inode);
1931 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1932 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1933 ei->pde = NULL;
1934 inode->i_mode = S_IFLNK|S_IRWXUGO;
1935 inode->i_uid = inode->i_gid = 0;
1936 inode->i_size = 64;
1937 inode->i_op = &proc_self_inode_operations;
1938 d_add(dentry, inode);
1939 return NULL;
1940 }
1941 tgid = name_to_int(dentry);
1942 if (tgid == ~0U)
1943 goto out;
1944
1945 read_lock(&tasklist_lock);
1946 task = find_task_by_pid(tgid);
1947 if (task)
1948 get_task_struct(task);
1949 read_unlock(&tasklist_lock);
1950 if (!task)
1951 goto out;
1952
1953 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
cd6a3ce9
EB
1954 if (!inode)
1955 goto out_put_task;
1da177e4 1956
1da177e4
LT
1957 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1958 inode->i_op = &proc_tgid_base_inode_operations;
1959 inode->i_fop = &proc_tgid_base_operations;
1da177e4 1960 inode->i_flags|=S_IMMUTABLE;
bcf88e11
DD
1961#ifdef CONFIG_SECURITY
1962 inode->i_nlink = 5;
1963#else
1964 inode->i_nlink = 4;
1965#endif
1da177e4 1966
48e6484d 1967 dentry->d_op = &pid_dentry_operations;
1da177e4 1968
1da177e4 1969 d_add(dentry, inode);
cd6a3ce9
EB
1970 /* Close the race of the process dying before we return the dentry */
1971 if (pid_revalidate(dentry, NULL))
1972 result = NULL;
1da177e4 1973
cd6a3ce9 1974out_put_task:
1da177e4 1975 put_task_struct(task);
1da177e4 1976out:
cd6a3ce9 1977 return result;
1da177e4
LT
1978}
1979
1980/* SMP-safe */
1981static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1982{
cd6a3ce9 1983 struct dentry *result = ERR_PTR(-ENOENT);
1da177e4
LT
1984 struct task_struct *task;
1985 struct task_struct *leader = proc_task(dir);
1986 struct inode *inode;
1987 unsigned tid;
1988
1989 tid = name_to_int(dentry);
1990 if (tid == ~0U)
1991 goto out;
1992
1993 read_lock(&tasklist_lock);
1994 task = find_task_by_pid(tid);
1995 if (task)
1996 get_task_struct(task);
1997 read_unlock(&tasklist_lock);
1998 if (!task)
1999 goto out;
2000 if (leader->tgid != task->tgid)
2001 goto out_drop_task;
2002
2003 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2004
2005
2006 if (!inode)
2007 goto out_drop_task;
2008 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2009 inode->i_op = &proc_tid_base_inode_operations;
2010 inode->i_fop = &proc_tid_base_operations;
1da177e4 2011 inode->i_flags|=S_IMMUTABLE;
bcf88e11
DD
2012#ifdef CONFIG_SECURITY
2013 inode->i_nlink = 4;
2014#else
2015 inode->i_nlink = 3;
2016#endif
1da177e4 2017
48e6484d 2018 dentry->d_op = &pid_dentry_operations;
1da177e4
LT
2019
2020 d_add(dentry, inode);
cd6a3ce9
EB
2021 /* Close the race of the process dying before we return the dentry */
2022 if (pid_revalidate(dentry, NULL))
2023 result = NULL;
1da177e4 2024
1da177e4
LT
2025out_drop_task:
2026 put_task_struct(task);
2027out:
cd6a3ce9 2028 return result;
1da177e4
LT
2029}
2030
2031#define PROC_NUMBUF 10
2032#define PROC_MAXPIDS 20
2033
2034/*
2035 * Get a few tgid's to return for filldir - we need to hold the
2036 * tasklist lock while doing this, and we must release it before
2037 * we actually do the filldir itself, so we use a temp buffer..
2038 */
2039static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2040{
2041 struct task_struct *p;
2042 int nr_tgids = 0;
2043
2044 index--;
2045 read_lock(&tasklist_lock);
2046 p = NULL;
2047 if (version) {
2048 p = find_task_by_pid(version);
2049 if (p && !thread_group_leader(p))
2050 p = NULL;
2051 }
2052
2053 if (p)
2054 index = 0;
2055 else
2056 p = next_task(&init_task);
2057
2058 for ( ; p != &init_task; p = next_task(p)) {
2059 int tgid = p->pid;
2060 if (!pid_alive(p))
2061 continue;
2062 if (--index >= 0)
2063 continue;
2064 tgids[nr_tgids] = tgid;
2065 nr_tgids++;
2066 if (nr_tgids >= PROC_MAXPIDS)
2067 break;
2068 }
2069 read_unlock(&tasklist_lock);
2070 return nr_tgids;
2071}
2072
2073/*
2074 * Get a few tid's to return for filldir - we need to hold the
2075 * tasklist lock while doing this, and we must release it before
2076 * we actually do the filldir itself, so we use a temp buffer..
2077 */
2078static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2079{
2080 struct task_struct *leader_task = proc_task(dir);
2081 struct task_struct *task = leader_task;
2082 int nr_tids = 0;
2083
2084 index -= 2;
2085 read_lock(&tasklist_lock);
2086 /*
2087 * The starting point task (leader_task) might be an already
2088 * unlinked task, which cannot be used to access the task-list
2089 * via next_thread().
2090 */
2091 if (pid_alive(task)) do {
2092 int tid = task->pid;
2093
2094 if (--index >= 0)
2095 continue;
f246315e
DD
2096 if (tids != NULL)
2097 tids[nr_tids] = tid;
1da177e4
LT
2098 nr_tids++;
2099 if (nr_tids >= PROC_MAXPIDS)
2100 break;
2101 } while ((task = next_thread(task)) != leader_task);
2102 read_unlock(&tasklist_lock);
2103 return nr_tids;
2104}
2105
2106/* for the /proc/ directory itself, after non-process stuff has been done */
2107int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2108{
2109 unsigned int tgid_array[PROC_MAXPIDS];
2110 char buf[PROC_NUMBUF];
2111 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2112 unsigned int nr_tgids, i;
2113 int next_tgid;
2114
2115 if (!nr) {
2116 ino_t ino = fake_ino(0,PROC_TGID_INO);
2117 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2118 return 0;
2119 filp->f_pos++;
2120 nr++;
2121 }
2122
2123 /* f_version caches the tgid value that the last readdir call couldn't
2124 * return. lseek aka telldir automagically resets f_version to 0.
2125 */
2126 next_tgid = filp->f_version;
2127 filp->f_version = 0;
2128 for (;;) {
2129 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2130 if (!nr_tgids) {
2131 /* no more entries ! */
2132 break;
2133 }
2134 next_tgid = 0;
2135
2136 /* do not use the last found pid, reserve it for next_tgid */
2137 if (nr_tgids == PROC_MAXPIDS) {
2138 nr_tgids--;
2139 next_tgid = tgid_array[nr_tgids];
2140 }
2141
2142 for (i=0;i<nr_tgids;i++) {
2143 int tgid = tgid_array[i];
2144 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2145 unsigned long j = PROC_NUMBUF;
2146
2147 do
2148 buf[--j] = '0' + (tgid % 10);
2149 while ((tgid /= 10) != 0);
2150
2151 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2152 /* returning this tgid failed, save it as the first
2153 * pid for the next readir call */
2154 filp->f_version = tgid_array[i];
2155 goto out;
2156 }
2157 filp->f_pos++;
2158 nr++;
2159 }
2160 }
2161out:
2162 return 0;
2163}
2164
2165/* for the /proc/TGID/task/ directories */
2166static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2167{
2168 unsigned int tid_array[PROC_MAXPIDS];
2169 char buf[PROC_NUMBUF];
2170 unsigned int nr_tids, i;
2171 struct dentry *dentry = filp->f_dentry;
2172 struct inode *inode = dentry->d_inode;
2173 int retval = -ENOENT;
2174 ino_t ino;
2175 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2176
2177 if (!pid_alive(proc_task(inode)))
2178 goto out;
2179 retval = 0;
2180
2181 switch (pos) {
2182 case 0:
2183 ino = inode->i_ino;
2184 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2185 goto out;
2186 pos++;
2187 /* fall through */
2188 case 1:
2189 ino = parent_ino(dentry);
2190 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2191 goto out;
2192 pos++;
2193 /* fall through */
2194 }
2195
2196 nr_tids = get_tid_list(pos, tid_array, inode);
2197
2198 for (i = 0; i < nr_tids; i++) {
2199 unsigned long j = PROC_NUMBUF;
2200 int tid = tid_array[i];
2201
2202 ino = fake_ino(tid,PROC_TID_INO);
2203
2204 do
2205 buf[--j] = '0' + (tid % 10);
2206 while ((tid /= 10) != 0);
2207
2208 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
2209 break;
2210 pos++;
2211 }
2212out:
2213 filp->f_pos = pos;
2214 return retval;
2215}
6e66b52b
EB
2216
2217static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2218{
2219 struct inode *inode = dentry->d_inode;
2220 struct task_struct *p = proc_task(inode);
2221 generic_fillattr(inode, stat);
2222
2223 if (pid_alive(p)) {
2224 task_lock(p);
2225 if (p->signal)
2226 stat->nlink += atomic_read(&p->signal->count);
2227 task_unlock(p);
2228 }
2229
2230 return 0;
2231}