[PATCH] proc: Remove tasklist_lock from proc_pid_readdir
[deliverable/linux.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
1548/* SMP-safe */
1549static struct dentry *proc_pident_lookup(struct inode *dir,
1550 struct dentry *dentry,
1551 struct pid_entry *ents)
1552{
1553 struct inode *inode;
cd6a3ce9 1554 struct dentry *error;
1da177e4
LT
1555 struct task_struct *task = proc_task(dir);
1556 struct pid_entry *p;
1557 struct proc_inode *ei;
1558
cd6a3ce9 1559 error = ERR_PTR(-ENOENT);
1da177e4
LT
1560 inode = NULL;
1561
1562 if (!pid_alive(task))
1563 goto out;
1564
1565 for (p = ents; p->name; p++) {
1566 if (p->len != dentry->d_name.len)
1567 continue;
1568 if (!memcmp(dentry->d_name.name, p->name, p->len))
1569 break;
1570 }
1571 if (!p->name)
1572 goto out;
1573
cd6a3ce9 1574 error = ERR_PTR(-EINVAL);
1da177e4
LT
1575 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1576 if (!inode)
1577 goto out;
1578
1579 ei = PROC_I(inode);
1580 inode->i_mode = p->mode;
1581 /*
1582 * Yes, it does not scale. And it should not. Don't add
1583 * new entries into /proc/<tgid>/ without very good reasons.
1584 */
1585 switch(p->type) {
1586 case PROC_TGID_TASK:
6e66b52b 1587 inode->i_nlink = 2;
1da177e4
LT
1588 inode->i_op = &proc_task_inode_operations;
1589 inode->i_fop = &proc_task_operations;
1590 break;
1591 case PROC_TID_FD:
1592 case PROC_TGID_FD:
1593 inode->i_nlink = 2;
1594 inode->i_op = &proc_fd_inode_operations;
1595 inode->i_fop = &proc_fd_operations;
1596 break;
1597 case PROC_TID_EXE:
1598 case PROC_TGID_EXE:
1599 inode->i_op = &proc_pid_link_inode_operations;
1600 ei->op.proc_get_link = proc_exe_link;
1601 break;
1602 case PROC_TID_CWD:
1603 case PROC_TGID_CWD:
1604 inode->i_op = &proc_pid_link_inode_operations;
1605 ei->op.proc_get_link = proc_cwd_link;
1606 break;
1607 case PROC_TID_ROOT:
1608 case PROC_TGID_ROOT:
1609 inode->i_op = &proc_pid_link_inode_operations;
1610 ei->op.proc_get_link = proc_root_link;
1611 break;
1612 case PROC_TID_ENVIRON:
1613 case PROC_TGID_ENVIRON:
1614 inode->i_fop = &proc_info_file_operations;
1615 ei->op.proc_read = proc_pid_environ;
1616 break;
1617 case PROC_TID_AUXV:
1618 case PROC_TGID_AUXV:
1619 inode->i_fop = &proc_info_file_operations;
1620 ei->op.proc_read = proc_pid_auxv;
1621 break;
1622 case PROC_TID_STATUS:
1623 case PROC_TGID_STATUS:
1624 inode->i_fop = &proc_info_file_operations;
1625 ei->op.proc_read = proc_pid_status;
1626 break;
1627 case PROC_TID_STAT:
1628 inode->i_fop = &proc_info_file_operations;
1629 ei->op.proc_read = proc_tid_stat;
1630 break;
1631 case PROC_TGID_STAT:
1632 inode->i_fop = &proc_info_file_operations;
1633 ei->op.proc_read = proc_tgid_stat;
1634 break;
1635 case PROC_TID_CMDLINE:
1636 case PROC_TGID_CMDLINE:
1637 inode->i_fop = &proc_info_file_operations;
1638 ei->op.proc_read = proc_pid_cmdline;
1639 break;
1640 case PROC_TID_STATM:
1641 case PROC_TGID_STATM:
1642 inode->i_fop = &proc_info_file_operations;
1643 ei->op.proc_read = proc_pid_statm;
1644 break;
1645 case PROC_TID_MAPS:
1646 case PROC_TGID_MAPS:
1647 inode->i_fop = &proc_maps_operations;
1648 break;
6e21c8f1
CL
1649#ifdef CONFIG_NUMA
1650 case PROC_TID_NUMA_MAPS:
1651 case PROC_TGID_NUMA_MAPS:
1652 inode->i_fop = &proc_numa_maps_operations;
1653 break;
1654#endif
1da177e4
LT
1655 case PROC_TID_MEM:
1656 case PROC_TGID_MEM:
1da177e4
LT
1657 inode->i_fop = &proc_mem_operations;
1658 break;
1659#ifdef CONFIG_SECCOMP
1660 case PROC_TID_SECCOMP:
1661 case PROC_TGID_SECCOMP:
1662 inode->i_fop = &proc_seccomp_operations;
1663 break;
1664#endif /* CONFIG_SECCOMP */
1665 case PROC_TID_MOUNTS:
1666 case PROC_TGID_MOUNTS:
1667 inode->i_fop = &proc_mounts_operations;
1668 break;
63c6764c 1669#ifdef CONFIG_MMU
e070ad49
ML
1670 case PROC_TID_SMAPS:
1671 case PROC_TGID_SMAPS:
1672 inode->i_fop = &proc_smaps_operations;
1673 break;
63c6764c 1674#endif
b4629fe2
CL
1675 case PROC_TID_MOUNTSTATS:
1676 case PROC_TGID_MOUNTSTATS:
1677 inode->i_fop = &proc_mountstats_operations;
1678 break;
1da177e4
LT
1679#ifdef CONFIG_SECURITY
1680 case PROC_TID_ATTR:
1681 inode->i_nlink = 2;
1682 inode->i_op = &proc_tid_attr_inode_operations;
1683 inode->i_fop = &proc_tid_attr_operations;
1684 break;
1685 case PROC_TGID_ATTR:
1686 inode->i_nlink = 2;
1687 inode->i_op = &proc_tgid_attr_inode_operations;
1688 inode->i_fop = &proc_tgid_attr_operations;
1689 break;
1690 case PROC_TID_ATTR_CURRENT:
1691 case PROC_TGID_ATTR_CURRENT:
1692 case PROC_TID_ATTR_PREV:
1693 case PROC_TGID_ATTR_PREV:
1694 case PROC_TID_ATTR_EXEC:
1695 case PROC_TGID_ATTR_EXEC:
1696 case PROC_TID_ATTR_FSCREATE:
1697 case PROC_TGID_ATTR_FSCREATE:
4eb582cf
ML
1698 case PROC_TID_ATTR_KEYCREATE:
1699 case PROC_TGID_ATTR_KEYCREATE:
1da177e4
LT
1700 inode->i_fop = &proc_pid_attr_operations;
1701 break;
1702#endif
1703#ifdef CONFIG_KALLSYMS
1704 case PROC_TID_WCHAN:
1705 case PROC_TGID_WCHAN:
1706 inode->i_fop = &proc_info_file_operations;
1707 ei->op.proc_read = proc_pid_wchan;
1708 break;
1709#endif
1710#ifdef CONFIG_SCHEDSTATS
1711 case PROC_TID_SCHEDSTAT:
1712 case PROC_TGID_SCHEDSTAT:
1713 inode->i_fop = &proc_info_file_operations;
1714 ei->op.proc_read = proc_pid_schedstat;
1715 break;
1716#endif
1717#ifdef CONFIG_CPUSETS
1718 case PROC_TID_CPUSET:
1719 case PROC_TGID_CPUSET:
1720 inode->i_fop = &proc_cpuset_operations;
1721 break;
1722#endif
1723 case PROC_TID_OOM_SCORE:
1724 case PROC_TGID_OOM_SCORE:
1725 inode->i_fop = &proc_info_file_operations;
1726 ei->op.proc_read = proc_oom_score;
1727 break;
1728 case PROC_TID_OOM_ADJUST:
1729 case PROC_TGID_OOM_ADJUST:
1730 inode->i_fop = &proc_oom_adjust_operations;
1731 break;
1732#ifdef CONFIG_AUDITSYSCALL
1733 case PROC_TID_LOGINUID:
1734 case PROC_TGID_LOGINUID:
1735 inode->i_fop = &proc_loginuid_operations;
1736 break;
1737#endif
1738 default:
1739 printk("procfs: impossible type (%d)",p->type);
1740 iput(inode);
cd6a3ce9
EB
1741 error = ERR_PTR(-EINVAL);
1742 goto out;
1da177e4
LT
1743 }
1744 dentry->d_op = &pid_dentry_operations;
1745 d_add(dentry, inode);
cd6a3ce9
EB
1746 /* Close the race of the process dying before we return the dentry */
1747 if (pid_revalidate(dentry, NULL))
1748 error = NULL;
1da177e4 1749out:
cd6a3ce9 1750 return error;
1da177e4
LT
1751}
1752
1753static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1754 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1755}
1756
1757static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1758 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1759}
1760
1761static struct file_operations proc_tgid_base_operations = {
1762 .read = generic_read_dir,
1763 .readdir = proc_tgid_base_readdir,
1764};
1765
1766static struct file_operations proc_tid_base_operations = {
1767 .read = generic_read_dir,
1768 .readdir = proc_tid_base_readdir,
1769};
1770
1771static struct inode_operations proc_tgid_base_inode_operations = {
1772 .lookup = proc_tgid_base_lookup,
1773};
1774
1775static struct inode_operations proc_tid_base_inode_operations = {
1776 .lookup = proc_tid_base_lookup,
1777};
1778
1779#ifdef CONFIG_SECURITY
1780static int proc_tgid_attr_readdir(struct file * filp,
1781 void * dirent, filldir_t filldir)
1782{
1783 return proc_pident_readdir(filp,dirent,filldir,
1784 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1785}
1786
1787static int proc_tid_attr_readdir(struct file * filp,
1788 void * dirent, filldir_t filldir)
1789{
1790 return proc_pident_readdir(filp,dirent,filldir,
1791 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1792}
1793
1794static struct file_operations proc_tgid_attr_operations = {
1795 .read = generic_read_dir,
1796 .readdir = proc_tgid_attr_readdir,
1797};
1798
1799static struct file_operations proc_tid_attr_operations = {
1800 .read = generic_read_dir,
1801 .readdir = proc_tid_attr_readdir,
1802};
1803
1804static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1805 struct dentry *dentry, struct nameidata *nd)
1806{
1807 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1808}
1809
1810static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1811 struct dentry *dentry, struct nameidata *nd)
1812{
1813 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1814}
1815
1816static struct inode_operations proc_tgid_attr_inode_operations = {
1817 .lookup = proc_tgid_attr_lookup,
1818};
1819
1820static struct inode_operations proc_tid_attr_inode_operations = {
1821 .lookup = proc_tid_attr_lookup,
1822};
1823#endif
1824
1825/*
1826 * /proc/self:
1827 */
1828static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1829 int buflen)
1830{
1831 char tmp[30];
1832 sprintf(tmp, "%d", current->tgid);
1833 return vfs_readlink(dentry,buffer,buflen,tmp);
1834}
1835
008b150a 1836static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1837{
1838 char tmp[30];
1839 sprintf(tmp, "%d", current->tgid);
008b150a 1840 return ERR_PTR(vfs_follow_link(nd,tmp));
1da177e4
LT
1841}
1842
1843static struct inode_operations proc_self_inode_operations = {
1844 .readlink = proc_self_readlink,
1845 .follow_link = proc_self_follow_link,
1846};
1847
1848/**
48e6484d
EB
1849 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1850 *
1851 * @task: task that should be flushed.
1852 *
1853 * Looks in the dcache for
1854 * /proc/@pid
1855 * /proc/@tgid/task/@pid
1856 * if either directory is present flushes it and all of it'ts children
1857 * from the dcache.
1da177e4 1858 *
48e6484d
EB
1859 * It is safe and reasonable to cache /proc entries for a task until
1860 * that task exits. After that they just clog up the dcache with
1861 * useless entries, possibly causing useful dcache entries to be
1862 * flushed instead. This routine is proved to flush those useless
1863 * dcache entries at process exit time.
1da177e4 1864 *
48e6484d
EB
1865 * NOTE: This routine is just an optimization so it does not guarantee
1866 * that no dcache entries will exist at process exit time it
1867 * just makes it very unlikely that any will persist.
1da177e4 1868 */
48e6484d 1869void proc_flush_task(struct task_struct *task)
1da177e4 1870{
48e6484d
EB
1871 struct dentry *dentry, *leader, *dir;
1872 char buf[30];
1873 struct qstr name;
1874
1875 name.name = buf;
1876 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1877 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1878 if (dentry) {
1879 shrink_dcache_parent(dentry);
1880 d_drop(dentry);
1881 dput(dentry);
1882 }
1da177e4 1883
48e6484d
EB
1884 if (thread_group_leader(task))
1885 goto out;
1da177e4 1886
48e6484d
EB
1887 name.name = buf;
1888 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1889 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1890 if (!leader)
1891 goto out;
1da177e4 1892
48e6484d
EB
1893 name.name = "task";
1894 name.len = strlen(name.name);
1895 dir = d_hash_and_lookup(leader, &name);
1896 if (!dir)
1897 goto out_put_leader;
1898
1899 name.name = buf;
1900 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1901 dentry = d_hash_and_lookup(dir, &name);
1902 if (dentry) {
1903 shrink_dcache_parent(dentry);
1904 d_drop(dentry);
1905 dput(dentry);
1da177e4 1906 }
48e6484d
EB
1907
1908 dput(dir);
1909out_put_leader:
1910 dput(leader);
1911out:
1912 return;
1da177e4
LT
1913}
1914
1915/* SMP-safe */
1916struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1917{
cd6a3ce9 1918 struct dentry *result = ERR_PTR(-ENOENT);
1da177e4
LT
1919 struct task_struct *task;
1920 struct inode *inode;
1921 struct proc_inode *ei;
1922 unsigned tgid;
1da177e4
LT
1923
1924 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1925 inode = new_inode(dir->i_sb);
1926 if (!inode)
1927 return ERR_PTR(-ENOMEM);
1928 ei = PROC_I(inode);
1929 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1930 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1931 ei->pde = NULL;
1932 inode->i_mode = S_IFLNK|S_IRWXUGO;
1933 inode->i_uid = inode->i_gid = 0;
1934 inode->i_size = 64;
1935 inode->i_op = &proc_self_inode_operations;
1936 d_add(dentry, inode);
1937 return NULL;
1938 }
1939 tgid = name_to_int(dentry);
1940 if (tgid == ~0U)
1941 goto out;
1942
1943 read_lock(&tasklist_lock);
1944 task = find_task_by_pid(tgid);
1945 if (task)
1946 get_task_struct(task);
1947 read_unlock(&tasklist_lock);
1948 if (!task)
1949 goto out;
1950
1951 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
cd6a3ce9
EB
1952 if (!inode)
1953 goto out_put_task;
1da177e4 1954
1da177e4
LT
1955 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1956 inode->i_op = &proc_tgid_base_inode_operations;
1957 inode->i_fop = &proc_tgid_base_operations;
1da177e4 1958 inode->i_flags|=S_IMMUTABLE;
bcf88e11
DD
1959#ifdef CONFIG_SECURITY
1960 inode->i_nlink = 5;
1961#else
1962 inode->i_nlink = 4;
1963#endif
1da177e4 1964
48e6484d 1965 dentry->d_op = &pid_dentry_operations;
1da177e4 1966
1da177e4 1967 d_add(dentry, inode);
cd6a3ce9
EB
1968 /* Close the race of the process dying before we return the dentry */
1969 if (pid_revalidate(dentry, NULL))
1970 result = NULL;
1da177e4 1971
cd6a3ce9 1972out_put_task:
1da177e4 1973 put_task_struct(task);
1da177e4 1974out:
cd6a3ce9 1975 return result;
1da177e4
LT
1976}
1977
1978/* SMP-safe */
1979static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1980{
cd6a3ce9 1981 struct dentry *result = ERR_PTR(-ENOENT);
1da177e4
LT
1982 struct task_struct *task;
1983 struct task_struct *leader = proc_task(dir);
1984 struct inode *inode;
1985 unsigned tid;
1986
1987 tid = name_to_int(dentry);
1988 if (tid == ~0U)
1989 goto out;
1990
1991 read_lock(&tasklist_lock);
1992 task = find_task_by_pid(tid);
1993 if (task)
1994 get_task_struct(task);
1995 read_unlock(&tasklist_lock);
1996 if (!task)
1997 goto out;
1998 if (leader->tgid != task->tgid)
1999 goto out_drop_task;
2000
2001 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2002
2003
2004 if (!inode)
2005 goto out_drop_task;
2006 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2007 inode->i_op = &proc_tid_base_inode_operations;
2008 inode->i_fop = &proc_tid_base_operations;
1da177e4 2009 inode->i_flags|=S_IMMUTABLE;
bcf88e11
DD
2010#ifdef CONFIG_SECURITY
2011 inode->i_nlink = 4;
2012#else
2013 inode->i_nlink = 3;
2014#endif
1da177e4 2015
48e6484d 2016 dentry->d_op = &pid_dentry_operations;
1da177e4
LT
2017
2018 d_add(dentry, inode);
cd6a3ce9
EB
2019 /* Close the race of the process dying before we return the dentry */
2020 if (pid_revalidate(dentry, NULL))
2021 result = NULL;
1da177e4 2022
1da177e4
LT
2023out_drop_task:
2024 put_task_struct(task);
2025out:
cd6a3ce9 2026 return result;
1da177e4
LT
2027}
2028
2029#define PROC_NUMBUF 10
1da177e4
LT
2030
2031/*
0bc58a91
EB
2032 * Find the first tgid to return to user space.
2033 *
2034 * Usually this is just whatever follows &init_task, but if the users
2035 * buffer was too small to hold the full list or there was a seek into
2036 * the middle of the directory we have more work to do.
2037 *
2038 * In the case of a short read we start with find_task_by_pid.
2039 *
2040 * In the case of a seek we start with &init_task and walk nr
2041 * threads past it.
1da177e4 2042 */
0bc58a91 2043static struct task_struct *first_tgid(int tgid, int nr)
1da177e4 2044{
0bc58a91 2045 struct task_struct *pos = NULL;
454cc105 2046 rcu_read_lock();
0bc58a91
EB
2047 if (tgid && nr) {
2048 pos = find_task_by_pid(tgid);
2049 if (pos && !thread_group_leader(pos))
2050 pos = NULL;
2051 if (pos)
2052 nr = 0;
1da177e4 2053 }
0bc58a91
EB
2054 /* If nr exceeds the number of processes get out quickly */
2055 if (nr && nr >= nr_processes())
2056 goto done;
1da177e4 2057
0bc58a91
EB
2058 /* If we haven't found our starting place yet start with
2059 * the init_task and walk nr tasks forward.
2060 */
2061 if (!pos && (nr >= 0))
2062 pos = next_task(&init_task);
1da177e4 2063
0bc58a91
EB
2064 for (; pos && pid_alive(pos); pos = next_task(pos)) {
2065 if (--nr > 0)
1da177e4 2066 continue;
0bc58a91
EB
2067 get_task_struct(pos);
2068 goto done;
1da177e4 2069 }
0bc58a91
EB
2070 pos = NULL;
2071done:
454cc105 2072 rcu_read_unlock();
0bc58a91 2073 return pos;
1da177e4
LT
2074}
2075
2076/*
0bc58a91
EB
2077 * Find the next task in the task list.
2078 * Return NULL if we loop or there is any error.
2079 *
2080 * The reference to the input task_struct is released.
1da177e4 2081 */
0bc58a91 2082static struct task_struct *next_tgid(struct task_struct *start)
1da177e4 2083{
0bc58a91 2084 struct task_struct *pos;
454cc105 2085 rcu_read_lock();
0bc58a91
EB
2086 pos = start;
2087 if (pid_alive(start))
2088 pos = next_task(start);
2089 if (pid_alive(pos) && (pos != &init_task)) {
2090 get_task_struct(pos);
2091 goto done;
2092 }
2093 pos = NULL;
2094done:
454cc105 2095 rcu_read_unlock();
0bc58a91
EB
2096 put_task_struct(start);
2097 return pos;
1da177e4
LT
2098}
2099
2100/* for the /proc/ directory itself, after non-process stuff has been done */
2101int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2102{
1da177e4
LT
2103 char buf[PROC_NUMBUF];
2104 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
0bc58a91
EB
2105 struct task_struct *task;
2106 int tgid;
1da177e4
LT
2107
2108 if (!nr) {
2109 ino_t ino = fake_ino(0,PROC_TGID_INO);
2110 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2111 return 0;
2112 filp->f_pos++;
2113 nr++;
2114 }
0bc58a91 2115 nr -= 1;
1da177e4
LT
2116
2117 /* f_version caches the tgid value that the last readdir call couldn't
2118 * return. lseek aka telldir automagically resets f_version to 0.
2119 */
0bc58a91 2120 tgid = filp->f_version;
1da177e4 2121 filp->f_version = 0;
0bc58a91
EB
2122 for (task = first_tgid(tgid, nr);
2123 task;
2124 task = next_tgid(task), filp->f_pos++) {
2125 int len;
2126 ino_t ino;
2127 tgid = task->pid;
2128 len = snprintf(buf, sizeof(buf), "%d", tgid);
2129 ino = fake_ino(tgid, PROC_TGID_INO);
2130 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2131 /* returning this tgid failed, save it as the first
2132 * pid for the next readir call */
2133 filp->f_version = tgid;
2134 put_task_struct(task);
1da177e4
LT
2135 break;
2136 }
0bc58a91
EB
2137 }
2138 return 0;
2139}
1da177e4 2140
0bc58a91
EB
2141/*
2142 * Find the first tid of a thread group to return to user space.
2143 *
2144 * Usually this is just the thread group leader, but if the users
2145 * buffer was too small or there was a seek into the middle of the
2146 * directory we have more work todo.
2147 *
2148 * In the case of a short read we start with find_task_by_pid.
2149 *
2150 * In the case of a seek we start with the leader and walk nr
2151 * threads past it.
2152 */
2153static struct task_struct *first_tid(struct task_struct *leader, int tid, int nr)
2154{
2155 struct task_struct *pos = NULL;
2156 read_lock(&tasklist_lock);
1da177e4 2157
0bc58a91
EB
2158 /* Attempt to start with the pid of a thread */
2159 if (tid && (nr > 0)) {
2160 pos = find_task_by_pid(tid);
2161 if (pos && (pos->group_leader != leader))
2162 pos = NULL;
2163 if (pos)
2164 nr = 0;
2165 }
1da177e4 2166
0bc58a91
EB
2167 /* If nr exceeds the number of threads there is nothing todo */
2168 if (nr) {
2169 int threads = 0;
2170 task_lock(leader);
2171 if (leader->signal)
2172 threads = atomic_read(&leader->signal->count);
2173 task_unlock(leader);
2174 if (nr >= threads)
2175 goto done;
2176 }
1da177e4 2177
0bc58a91
EB
2178 /* If we haven't found our starting place yet start with the
2179 * leader and walk nr threads forward.
2180 */
2181 if (!pos && (nr >= 0))
2182 pos = leader;
2183
2184 for (; pos && pid_alive(pos); pos = next_thread(pos)) {
2185 if (--nr > 0)
2186 continue;
2187 get_task_struct(pos);
2188 goto done;
1da177e4 2189 }
0bc58a91
EB
2190 pos = NULL;
2191done:
2192 read_unlock(&tasklist_lock);
2193 return pos;
2194}
2195
2196/*
2197 * Find the next thread in the thread list.
2198 * Return NULL if there is an error or no next thread.
2199 *
2200 * The reference to the input task_struct is released.
2201 */
2202static struct task_struct *next_tid(struct task_struct *start)
2203{
2204 struct task_struct *pos;
2205 read_lock(&tasklist_lock);
2206 pos = start;
2207 if (pid_alive(start))
2208 pos = next_thread(start);
2209 if (pid_alive(pos) && (pos != start->group_leader))
2210 get_task_struct(pos);
2211 else
2212 pos = NULL;
2213 read_unlock(&tasklist_lock);
2214 put_task_struct(start);
2215 return pos;
1da177e4
LT
2216}
2217
2218/* for the /proc/TGID/task/ directories */
2219static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2220{
1da177e4 2221 char buf[PROC_NUMBUF];
1da177e4
LT
2222 struct dentry *dentry = filp->f_dentry;
2223 struct inode *inode = dentry->d_inode;
0bc58a91
EB
2224 struct task_struct *leader = proc_task(inode);
2225 struct task_struct *task;
1da177e4
LT
2226 int retval = -ENOENT;
2227 ino_t ino;
0bc58a91 2228 int tid;
1da177e4
LT
2229 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2230
0bc58a91 2231 if (!pid_alive(leader))
1da177e4
LT
2232 goto out;
2233 retval = 0;
2234
2235 switch (pos) {
2236 case 0:
2237 ino = inode->i_ino;
2238 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2239 goto out;
2240 pos++;
2241 /* fall through */
2242 case 1:
2243 ino = parent_ino(dentry);
2244 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2245 goto out;
2246 pos++;
2247 /* fall through */
2248 }
2249
0bc58a91
EB
2250 /* f_version caches the tgid value that the last readdir call couldn't
2251 * return. lseek aka telldir automagically resets f_version to 0.
2252 */
2253 tid = filp->f_version;
2254 filp->f_version = 0;
2255 for (task = first_tid(leader, tid, pos - 2);
2256 task;
2257 task = next_tid(task), pos++) {
2258 int len;
2259 tid = task->pid;
2260 len = snprintf(buf, sizeof(buf), "%d", tid);
2261 ino = fake_ino(tid, PROC_TID_INO);
2262 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2263 /* returning this tgid failed, save it as the first
2264 * pid for the next readir call */
2265 filp->f_version = tid;
2266 put_task_struct(task);
1da177e4 2267 break;
0bc58a91 2268 }
1da177e4
LT
2269 }
2270out:
2271 filp->f_pos = pos;
2272 return retval;
2273}
6e66b52b
EB
2274
2275static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2276{
2277 struct inode *inode = dentry->d_inode;
2278 struct task_struct *p = proc_task(inode);
2279 generic_fillattr(inode, stat);
2280
2281 if (pid_alive(p)) {
2282 task_lock(p);
2283 if (p->signal)
2284 stat->nlink += atomic_read(&p->signal->count);
2285 task_unlock(p);
2286 }
2287
2288 return 0;
2289}
This page took 0.251447 seconds and 5 git commands to generate.