blob: 20746e124409fe8c040bc68dadb12c0909956644 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
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>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#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>
Dipankar Sarmab8359962005-09-09 13:04:14 -070066#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#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>
Al Viro5addc5d2005-11-07 17:15:49 -050074#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include "internal.h"
76
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070077/* NOTE:
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
82 *
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
85 */
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * For hysterical raisins we keep the same inumbers as in the old procfs.
89 * Feel free to change the macro below - just keep the range distinct from
90 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
91 * As soon as we'll get a separate superblock we will be able to forget
92 * about magical ranges too.
93 */
94
95#define fake_ino(pid,ino) (((pid)<<16)|(ino))
96
97enum pid_directory_inos {
98 PROC_TGID_INO = 2,
99 PROC_TGID_TASK,
100 PROC_TGID_STATUS,
101 PROC_TGID_MEM,
102#ifdef CONFIG_SECCOMP
103 PROC_TGID_SECCOMP,
104#endif
105 PROC_TGID_CWD,
106 PROC_TGID_ROOT,
107 PROC_TGID_EXE,
108 PROC_TGID_FD,
109 PROC_TGID_ENVIRON,
110 PROC_TGID_AUXV,
111 PROC_TGID_CMDLINE,
112 PROC_TGID_STAT,
113 PROC_TGID_STATM,
114 PROC_TGID_MAPS,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700115 PROC_TGID_NUMA_MAPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 PROC_TGID_MOUNTS,
Chuck Leverb4629fe2006-03-20 13:44:12 -0500117 PROC_TGID_MOUNTSTATS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 PROC_TGID_WCHAN,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700119#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -0700120 PROC_TGID_SMAPS,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#ifdef CONFIG_SCHEDSTATS
123 PROC_TGID_SCHEDSTAT,
124#endif
125#ifdef CONFIG_CPUSETS
126 PROC_TGID_CPUSET,
127#endif
128#ifdef CONFIG_SECURITY
129 PROC_TGID_ATTR,
130 PROC_TGID_ATTR_CURRENT,
131 PROC_TGID_ATTR_PREV,
132 PROC_TGID_ATTR_EXEC,
133 PROC_TGID_ATTR_FSCREATE,
Michael LeMay4eb582c2006-06-26 00:24:57 -0700134 PROC_TGID_ATTR_KEYCREATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#endif
136#ifdef CONFIG_AUDITSYSCALL
137 PROC_TGID_LOGINUID,
138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 PROC_TGID_OOM_SCORE,
140 PROC_TGID_OOM_ADJUST,
141 PROC_TID_INO,
142 PROC_TID_STATUS,
143 PROC_TID_MEM,
144#ifdef CONFIG_SECCOMP
145 PROC_TID_SECCOMP,
146#endif
147 PROC_TID_CWD,
148 PROC_TID_ROOT,
149 PROC_TID_EXE,
150 PROC_TID_FD,
151 PROC_TID_ENVIRON,
152 PROC_TID_AUXV,
153 PROC_TID_CMDLINE,
154 PROC_TID_STAT,
155 PROC_TID_STATM,
156 PROC_TID_MAPS,
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700157 PROC_TID_NUMA_MAPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 PROC_TID_MOUNTS,
Chuck Leverb4629fe2006-03-20 13:44:12 -0500159 PROC_TID_MOUNTSTATS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 PROC_TID_WCHAN,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700161#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -0700162 PROC_TID_SMAPS,
Yoshinori Sato63c67642005-10-14 15:59:11 -0700163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#ifdef CONFIG_SCHEDSTATS
165 PROC_TID_SCHEDSTAT,
166#endif
167#ifdef CONFIG_CPUSETS
168 PROC_TID_CPUSET,
169#endif
170#ifdef CONFIG_SECURITY
171 PROC_TID_ATTR,
172 PROC_TID_ATTR_CURRENT,
173 PROC_TID_ATTR_PREV,
174 PROC_TID_ATTR_EXEC,
175 PROC_TID_ATTR_FSCREATE,
Michael LeMay4eb582c2006-06-26 00:24:57 -0700176 PROC_TID_ATTR_KEYCREATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#endif
178#ifdef CONFIG_AUDITSYSCALL
179 PROC_TID_LOGINUID,
180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 PROC_TID_OOM_SCORE,
182 PROC_TID_OOM_ADJUST,
Miklos Szeredi5e21ccb2005-09-06 15:18:23 -0700183
184 /* Add new entries before this */
185 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700188/* Worst case buffer size needed for holding an integer. */
189#define PROC_NUMBUF 10
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191struct pid_entry {
192 int type;
193 int len;
194 char *name;
195 mode_t mode;
196};
197
198#define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
199
200static struct pid_entry tgid_base_stuff[] = {
201 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
202 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
203 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
204 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
205 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
206 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
207 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
208 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
209 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700210#ifdef CONFIG_NUMA
211 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
214#ifdef CONFIG_SECCOMP
215 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
216#endif
217 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
218 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
219 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
220 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
Chuck Leverb4629fe2006-03-20 13:44:12 -0500221 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
Yoshinori Sato63c67642005-10-14 15:59:11 -0700222#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -0700223 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
Yoshinori Sato63c67642005-10-14 15:59:11 -0700224#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#ifdef CONFIG_SECURITY
226 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
227#endif
228#ifdef CONFIG_KALLSYMS
229 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
230#endif
231#ifdef CONFIG_SCHEDSTATS
232 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
233#endif
234#ifdef CONFIG_CPUSETS
235 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
236#endif
237 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
238 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
239#ifdef CONFIG_AUDITSYSCALL
240 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
241#endif
242 {0,0,NULL,0}
243};
244static struct pid_entry tid_base_stuff[] = {
245 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
246 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
247 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
248 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
249 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
250 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
251 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
252 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
Christoph Lameter6e21c8f2005-09-03 15:54:45 -0700253#ifdef CONFIG_NUMA
254 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
255#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
257#ifdef CONFIG_SECCOMP
258 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
259#endif
260 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
261 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
262 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
263 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
Yoshinori Sato63c67642005-10-14 15:59:11 -0700264#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -0700265 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
Yoshinori Sato63c67642005-10-14 15:59:11 -0700266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#ifdef CONFIG_SECURITY
268 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
269#endif
270#ifdef CONFIG_KALLSYMS
271 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
272#endif
273#ifdef CONFIG_SCHEDSTATS
274 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
275#endif
276#ifdef CONFIG_CPUSETS
277 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
278#endif
279 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
280 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
281#ifdef CONFIG_AUDITSYSCALL
282 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
283#endif
284 {0,0,NULL,0}
285};
286
287#ifdef CONFIG_SECURITY
288static struct pid_entry tgid_attr_stuff[] = {
289 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
290 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
291 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
292 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
Michael LeMay4eb582c2006-06-26 00:24:57 -0700293 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 {0,0,NULL,0}
295};
296static struct pid_entry tid_attr_stuff[] = {
297 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
298 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
299 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
300 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
Michael LeMay4eb582c2006-06-26 00:24:57 -0700301 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 {0,0,NULL,0}
303};
304#endif
305
306#undef E
307
308static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
309{
310 struct task_struct *task = proc_task(inode);
311 struct files_struct *files;
312 struct file *file;
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -0700313 int fd = proc_fd(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 files = get_files_struct(task);
316 if (files) {
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -0700317 /*
318 * We are not taking a ref to the file structure, so we must
319 * hold ->file_lock.
320 */
321 spin_lock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 file = fcheck_files(files, fd);
323 if (file) {
324 *mnt = mntget(file->f_vfsmnt);
325 *dentry = dget(file->f_dentry);
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -0700326 spin_unlock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 put_files_struct(files);
328 return 0;
329 }
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -0700330 spin_unlock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 put_files_struct(files);
332 }
333 return -ENOENT;
334}
335
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700336static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700339 task_lock(task);
340 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if(fs)
342 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700343 task_unlock(task);
344 return fs;
345}
346
347static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
348{
349 struct fs_struct *fs = get_fs_struct(proc_task(inode));
350 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (fs) {
352 read_lock(&fs->lock);
353 *mnt = mntget(fs->pwdmnt);
354 *dentry = dget(fs->pwd);
355 read_unlock(&fs->lock);
356 result = 0;
357 put_fs_struct(fs);
358 }
359 return result;
360}
361
362static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
363{
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700364 struct fs_struct *fs = get_fs_struct(proc_task(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (fs) {
367 read_lock(&fs->lock);
368 *mnt = mntget(fs->rootmnt);
369 *dentry = dget(fs->root);
370 read_unlock(&fs->lock);
371 result = 0;
372 put_fs_struct(fs);
373 }
374 return result;
375}
376
377#define MAY_PTRACE(task) \
378 (task == current || \
379 (task->parent == current && \
380 (task->ptrace & PT_PTRACED) && \
381 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
382 security_ptrace(current,task) == 0))
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static int proc_pid_environ(struct task_struct *task, char * buffer)
385{
386 int res = 0;
387 struct mm_struct *mm = get_task_mm(task);
388 if (mm) {
389 unsigned int len = mm->env_end - mm->env_start;
390 if (len > PAGE_SIZE)
391 len = PAGE_SIZE;
392 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700393 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 res = -ESRCH;
395 mmput(mm);
396 }
397 return res;
398}
399
400static int proc_pid_cmdline(struct task_struct *task, char * buffer)
401{
402 int res = 0;
403 unsigned int len;
404 struct mm_struct *mm = get_task_mm(task);
405 if (!mm)
406 goto out;
407 if (!mm->arg_end)
408 goto out_mm; /* Shh! No looking before we're done */
409
410 len = mm->arg_end - mm->arg_start;
411
412 if (len > PAGE_SIZE)
413 len = PAGE_SIZE;
414
415 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
416
417 // If the nul at the end of args has been overwritten, then
418 // assume application is using setproctitle(3).
419 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
420 len = strnlen(buffer, res);
421 if (len < res) {
422 res = len;
423 } else {
424 len = mm->env_end - mm->env_start;
425 if (len > PAGE_SIZE - res)
426 len = PAGE_SIZE - res;
427 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
428 res = strnlen(buffer, res);
429 }
430 }
431out_mm:
432 mmput(mm);
433out:
434 return res;
435}
436
437static int proc_pid_auxv(struct task_struct *task, char *buffer)
438{
439 int res = 0;
440 struct mm_struct *mm = get_task_mm(task);
441 if (mm) {
442 unsigned int nwords = 0;
443 do
444 nwords += 2;
445 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
446 res = nwords * sizeof(mm->saved_auxv[0]);
447 if (res > PAGE_SIZE)
448 res = PAGE_SIZE;
449 memcpy(buffer, mm->saved_auxv, res);
450 mmput(mm);
451 }
452 return res;
453}
454
455
456#ifdef CONFIG_KALLSYMS
457/*
458 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
459 * Returns the resolved symbol. If that fails, simply return the address.
460 */
461static int proc_pid_wchan(struct task_struct *task, char *buffer)
462{
463 char *modname;
464 const char *sym_name;
465 unsigned long wchan, size, offset;
466 char namebuf[KSYM_NAME_LEN+1];
467
468 wchan = get_wchan(task);
469
470 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
471 if (sym_name)
472 return sprintf(buffer, "%s", sym_name);
473 return sprintf(buffer, "%lu", wchan);
474}
475#endif /* CONFIG_KALLSYMS */
476
477#ifdef CONFIG_SCHEDSTATS
478/*
479 * Provides /proc/PID/schedstat
480 */
481static int proc_pid_schedstat(struct task_struct *task, char *buffer)
482{
483 return sprintf(buffer, "%lu %lu %lu\n",
484 task->sched_info.cpu_time,
485 task->sched_info.run_delay,
486 task->sched_info.pcnt);
487}
488#endif
489
490/* The badness from the OOM killer */
491unsigned long badness(struct task_struct *p, unsigned long uptime);
492static int proc_oom_score(struct task_struct *task, char *buffer)
493{
494 unsigned long points;
495 struct timespec uptime;
496
497 do_posix_clock_monotonic_gettime(&uptime);
498 points = badness(task, uptime.tv_sec);
499 return sprintf(buffer, "%lu\n", points);
500}
501
502/************************************************************************/
503/* Here the fs part begins */
504/************************************************************************/
505
506/* permission checks */
507
Sripathi Kodi66dcca02005-09-19 18:26:12 -0500508/* If the process being read is separated by chroot from the reading process,
509 * don't let the reader access the threads.
510 */
Eric W. Biederman0f2fe202006-06-26 00:25:46 -0700511static int proc_check_chroot(struct dentry *de, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Eric W. Biederman0f2fe202006-06-26 00:25:46 -0700513 struct dentry *base;
514 struct vfsmount *our_vfsmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int res = 0;
Herbert Poetzle4e5d3f2006-03-31 02:31:35 -0800516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 read_lock(&current->fs->lock);
518 our_vfsmnt = mntget(current->fs->rootmnt);
519 base = dget(current->fs->root);
520 read_unlock(&current->fs->lock);
521
522 spin_lock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Herbert Poetzle4e5d3f2006-03-31 02:31:35 -0800524 while (mnt != our_vfsmnt) {
525 if (mnt == mnt->mnt_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto out;
Herbert Poetzle4e5d3f2006-03-31 02:31:35 -0800527 de = mnt->mnt_mountpoint;
528 mnt = mnt->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
531 if (!is_subdir(de, base))
532 goto out;
533 spin_unlock(&vfsmount_lock);
534
535exit:
536 dput(base);
537 mntput(our_vfsmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return res;
539out:
540 spin_unlock(&vfsmount_lock);
541 res = -EACCES;
542 goto exit;
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500546struct proc_mounts {
547 struct seq_file m;
548 int event;
549};
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551static int mounts_open(struct inode *inode, struct file *file)
552{
553 struct task_struct *task = proc_task(inode);
Al Viro5addc5d2005-11-07 17:15:49 -0500554 struct namespace *namespace;
555 struct proc_mounts *p;
556 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Al Viro5addc5d2005-11-07 17:15:49 -0500558 task_lock(task);
559 namespace = task->namespace;
560 if (namespace)
561 get_namespace(namespace);
562 task_unlock(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Al Viro5addc5d2005-11-07 17:15:49 -0500564 if (namespace) {
565 ret = -ENOMEM;
566 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
567 if (p) {
568 file->private_data = &p->m;
569 ret = seq_open(file, &mounts_op);
570 if (!ret) {
571 p->m.private = namespace;
572 p->event = namespace->event;
573 return 0;
574 }
575 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Al Viro5addc5d2005-11-07 17:15:49 -0500577 put_namespace(namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 return ret;
580}
581
582static int mounts_release(struct inode *inode, struct file *file)
583{
584 struct seq_file *m = file->private_data;
585 struct namespace *namespace = m->private;
586 put_namespace(namespace);
587 return seq_release(inode, file);
588}
589
Al Viro5addc5d2005-11-07 17:15:49 -0500590static unsigned mounts_poll(struct file *file, poll_table *wait)
591{
592 struct proc_mounts *p = file->private_data;
593 struct namespace *ns = p->m.private;
594 unsigned res = 0;
595
596 poll_wait(file, &ns->poll, wait);
597
598 spin_lock(&vfsmount_lock);
599 if (p->event != ns->event) {
600 p->event = ns->event;
601 res = POLLERR;
602 }
603 spin_unlock(&vfsmount_lock);
604
605 return res;
606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static struct file_operations proc_mounts_operations = {
609 .open = mounts_open,
610 .read = seq_read,
611 .llseek = seq_lseek,
612 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500613 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614};
615
Chuck Leverb4629fe2006-03-20 13:44:12 -0500616extern struct seq_operations mountstats_op;
617static int mountstats_open(struct inode *inode, struct file *file)
618{
619 struct task_struct *task = proc_task(inode);
620 int ret = seq_open(file, &mountstats_op);
621
622 if (!ret) {
623 struct seq_file *m = file->private_data;
624 struct namespace *namespace;
625 task_lock(task);
626 namespace = task->namespace;
627 if (namespace)
628 get_namespace(namespace);
629 task_unlock(task);
630
631 if (namespace)
632 m->private = namespace;
633 else {
634 seq_release(inode, file);
635 ret = -EINVAL;
636 }
637 }
638 return ret;
639}
640
641static struct file_operations proc_mountstats_operations = {
642 .open = mountstats_open,
643 .read = seq_read,
644 .llseek = seq_lseek,
645 .release = mounts_release,
646};
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
649
650static ssize_t proc_info_read(struct file * file, char __user * buf,
651 size_t count, loff_t *ppos)
652{
653 struct inode * inode = file->f_dentry->d_inode;
654 unsigned long page;
655 ssize_t length;
656 struct task_struct *task = proc_task(inode);
657
658 if (count > PROC_BLOCK_SIZE)
659 count = PROC_BLOCK_SIZE;
660 if (!(page = __get_free_page(GFP_KERNEL)))
661 return -ENOMEM;
662
663 length = PROC_I(inode)->op.proc_read(task, (char*)page);
664
665 if (length >= 0)
666 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
667 free_page(page);
668 return length;
669}
670
671static struct file_operations proc_info_file_operations = {
672 .read = proc_info_read,
673};
674
675static int mem_open(struct inode* inode, struct file* file)
676{
677 file->private_data = (void*)((long)current->self_exec_id);
678 return 0;
679}
680
681static ssize_t mem_read(struct file * file, char __user * buf,
682 size_t count, loff_t *ppos)
683{
684 struct task_struct *task = proc_task(file->f_dentry->d_inode);
685 char *page;
686 unsigned long src = *ppos;
687 int ret = -ESRCH;
688 struct mm_struct *mm;
689
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700690 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 goto out;
692
693 ret = -ENOMEM;
694 page = (char *)__get_free_page(GFP_USER);
695 if (!page)
696 goto out;
697
698 ret = 0;
699
700 mm = get_task_mm(task);
701 if (!mm)
702 goto out_free;
703
704 ret = -EIO;
705
706 if (file->private_data != (void*)((long)current->self_exec_id))
707 goto out_put;
708
709 ret = 0;
710
711 while (count > 0) {
712 int this_len, retval;
713
714 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
715 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700716 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (!ret)
718 ret = -EIO;
719 break;
720 }
721
722 if (copy_to_user(buf, page, retval)) {
723 ret = -EFAULT;
724 break;
725 }
726
727 ret += retval;
728 src += retval;
729 buf += retval;
730 count -= retval;
731 }
732 *ppos = src;
733
734out_put:
735 mmput(mm);
736out_free:
737 free_page((unsigned long) page);
738out:
739 return ret;
740}
741
742#define mem_write NULL
743
744#ifndef mem_write
745/* This is a security hazard */
746static ssize_t mem_write(struct file * file, const char * buf,
747 size_t count, loff_t *ppos)
748{
749 int copied = 0;
750 char *page;
751 struct task_struct *task = proc_task(file->f_dentry->d_inode);
752 unsigned long dst = *ppos;
753
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700754 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -ESRCH;
756
757 page = (char *)__get_free_page(GFP_USER);
758 if (!page)
759 return -ENOMEM;
760
761 while (count > 0) {
762 int this_len, retval;
763
764 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
765 if (copy_from_user(page, buf, this_len)) {
766 copied = -EFAULT;
767 break;
768 }
769 retval = access_process_vm(task, dst, page, this_len, 1);
770 if (!retval) {
771 if (!copied)
772 copied = -EIO;
773 break;
774 }
775 copied += retval;
776 buf += retval;
777 dst += retval;
778 count -= retval;
779 }
780 *ppos = dst;
781 free_page((unsigned long) page);
782 return copied;
783}
784#endif
785
786static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
787{
788 switch (orig) {
789 case 0:
790 file->f_pos = offset;
791 break;
792 case 1:
793 file->f_pos += offset;
794 break;
795 default:
796 return -EINVAL;
797 }
798 force_successful_syscall_return();
799 return file->f_pos;
800}
801
802static struct file_operations proc_mem_operations = {
803 .llseek = mem_lseek,
804 .read = mem_read,
805 .write = mem_write,
806 .open = mem_open,
807};
808
809static ssize_t oom_adjust_read(struct file *file, char __user *buf,
810 size_t count, loff_t *ppos)
811{
812 struct task_struct *task = proc_task(file->f_dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700813 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 size_t len;
815 int oom_adjust = task->oomkilladj;
816 loff_t __ppos = *ppos;
817
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700818 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (__ppos >= len)
820 return 0;
821 if (count > len-__ppos)
822 count = len-__ppos;
823 if (copy_to_user(buf, buffer + __ppos, count))
824 return -EFAULT;
825 *ppos = __ppos + count;
826 return count;
827}
828
829static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
830 size_t count, loff_t *ppos)
831{
832 struct task_struct *task = proc_task(file->f_dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700833 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 int oom_adjust;
835
836 if (!capable(CAP_SYS_RESOURCE))
837 return -EPERM;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700838 memset(buffer, 0, sizeof(buffer));
839 if (count > sizeof(buffer) - 1)
840 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (copy_from_user(buffer, buf, count))
842 return -EFAULT;
843 oom_adjust = simple_strtol(buffer, &end, 0);
Andrea Arcangeli79befd02005-04-16 15:24:05 -0700844 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -EINVAL;
846 if (*end == '\n')
847 end++;
848 task->oomkilladj = oom_adjust;
849 if (end - buffer == 0)
850 return -EIO;
851 return end - buffer;
852}
853
854static struct file_operations proc_oom_adjust_operations = {
855 .read = oom_adjust_read,
856 .write = oom_adjust_write,
857};
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859#ifdef CONFIG_AUDITSYSCALL
860#define TMPBUFLEN 21
861static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
862 size_t count, loff_t *ppos)
863{
864 struct inode * inode = file->f_dentry->d_inode;
865 struct task_struct *task = proc_task(inode);
866 ssize_t length;
867 char tmpbuf[TMPBUFLEN];
868
869 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
870 audit_get_loginuid(task->audit_context));
871 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
872}
873
874static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
875 size_t count, loff_t *ppos)
876{
877 struct inode * inode = file->f_dentry->d_inode;
878 char *page, *tmp;
879 ssize_t length;
880 struct task_struct *task = proc_task(inode);
881 uid_t loginuid;
882
883 if (!capable(CAP_AUDIT_CONTROL))
884 return -EPERM;
885
886 if (current != task)
887 return -EPERM;
888
Al Viroe0182902006-05-18 08:28:02 -0400889 if (count >= PAGE_SIZE)
890 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (*ppos != 0) {
893 /* No partial writes. */
894 return -EINVAL;
895 }
896 page = (char*)__get_free_page(GFP_USER);
897 if (!page)
898 return -ENOMEM;
899 length = -EFAULT;
900 if (copy_from_user(page, buf, count))
901 goto out_free_page;
902
Al Viroe0182902006-05-18 08:28:02 -0400903 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 loginuid = simple_strtoul(page, &tmp, 10);
905 if (tmp == page) {
906 length = -EINVAL;
907 goto out_free_page;
908
909 }
Steve Grubb456be6c2005-04-29 17:30:07 +0100910 length = audit_set_loginuid(task, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (likely(length == 0))
912 length = count;
913
914out_free_page:
915 free_page((unsigned long) page);
916 return length;
917}
918
919static struct file_operations proc_loginuid_operations = {
920 .read = proc_loginuid_read,
921 .write = proc_loginuid_write,
922};
923#endif
924
925#ifdef CONFIG_SECCOMP
926static ssize_t seccomp_read(struct file *file, char __user *buf,
927 size_t count, loff_t *ppos)
928{
929 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
930 char __buf[20];
931 loff_t __ppos = *ppos;
932 size_t len;
933
934 /* no need to print the trailing zero, so use only len */
935 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
936 if (__ppos >= len)
937 return 0;
938 if (count > len - __ppos)
939 count = len - __ppos;
940 if (copy_to_user(buf, __buf + __ppos, count))
941 return -EFAULT;
942 *ppos = __ppos + count;
943 return count;
944}
945
946static ssize_t seccomp_write(struct file *file, const char __user *buf,
947 size_t count, loff_t *ppos)
948{
949 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
950 char __buf[20], *end;
951 unsigned int seccomp_mode;
952
953 /* can set it only once to be even more secure */
954 if (unlikely(tsk->seccomp.mode))
955 return -EPERM;
956
957 memset(__buf, 0, sizeof(__buf));
958 count = min(count, sizeof(__buf) - 1);
959 if (copy_from_user(__buf, buf, count))
960 return -EFAULT;
961 seccomp_mode = simple_strtoul(__buf, &end, 0);
962 if (*end == '\n')
963 end++;
964 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
965 tsk->seccomp.mode = seccomp_mode;
966 set_tsk_thread_flag(tsk, TIF_SECCOMP);
967 } else
968 return -EINVAL;
969 if (unlikely(!(end - __buf)))
970 return -EIO;
971 return end - __buf;
972}
973
974static struct file_operations proc_seccomp_operations = {
975 .read = seccomp_read,
976 .write = seccomp_write,
977};
978#endif /* CONFIG_SECCOMP */
979
Eric W. Biederman0f2fe202006-06-26 00:25:46 -0700980static int proc_check_dentry_visible(struct inode *inode,
981 struct dentry *dentry, struct vfsmount *mnt)
982{
983 /* Verify that the current process can already see the
984 * file pointed at by the file descriptor.
985 * This prevents /proc from being an accidental information leak.
986 *
987 * This prevents access to files that are not visible do to
988 * being on the otherside of a chroot, in a different
989 * namespace, or are simply process local (like pipes).
990 */
991 struct task_struct *task;
992 struct files_struct *task_files, *files;
993 int error = -EACCES;
994
995 /* See if the the two tasks share a commone set of
996 * file descriptors. If so everything is visible.
997 */
998 task = proc_task(inode);
999 if (!task)
1000 goto out;
1001 files = get_files_struct(current);
1002 task_files = get_files_struct(task);
1003 if (files && task_files && (files == task_files))
1004 error = 0;
1005 if (task_files)
1006 put_files_struct(task_files);
1007 if (files)
1008 put_files_struct(files);
1009 if (!error)
1010 goto out;
1011
1012 /* If the two tasks don't share a common set of file
1013 * descriptors see if the destination dentry is already
1014 * visible in the current tasks filesystem namespace.
1015 */
1016 error = proc_check_chroot(dentry, mnt);
1017out:
1018 return error;
1019
1020}
1021
Al Viro008b1502005-08-20 00:17:39 +01001022static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 struct inode *inode = dentry->d_inode;
1025 int error = -EACCES;
1026
1027 /* We don't need a base pointer in the /proc filesystem */
1028 path_release(nd);
1029
1030 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1031 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1034 nd->last_type = LAST_BIND;
Eric W. Biederman0f2fe202006-06-26 00:25:46 -07001035 if (error)
1036 goto out;
1037
1038 /* Only return files this task can already see */
1039 error = proc_check_dentry_visible(inode, nd->dentry, nd->mnt);
1040 if (error)
1041 path_release(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042out:
Al Viro008b1502005-08-20 00:17:39 +01001043 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1047 char __user *buffer, int buflen)
1048{
1049 struct inode * inode;
1050 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1051 int len;
1052
1053 if (!tmp)
1054 return -ENOMEM;
1055
1056 inode = dentry->d_inode;
1057 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1058 len = PTR_ERR(path);
1059 if (IS_ERR(path))
1060 goto out;
1061 len = tmp + PAGE_SIZE - 1 - path;
1062
1063 if (len > buflen)
1064 len = buflen;
1065 if (copy_to_user(buffer, path, len))
1066 len = -EFAULT;
1067 out:
1068 free_page((unsigned long)tmp);
1069 return len;
1070}
1071
1072static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1073{
1074 int error = -EACCES;
1075 struct inode *inode = dentry->d_inode;
1076 struct dentry *de;
1077 struct vfsmount *mnt = NULL;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1081 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1084 if (error)
1085 goto out;
1086
Eric W. Biederman0f2fe202006-06-26 00:25:46 -07001087 /* Only return files this task can already see */
1088 error = proc_check_dentry_visible(inode, de, mnt);
1089 if (error)
1090 goto out_put;
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 error = do_proc_readlink(de, mnt, buffer, buflen);
Eric W. Biederman0f2fe202006-06-26 00:25:46 -07001093out_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 dput(de);
1095 mntput(mnt);
1096out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return error;
1098}
1099
1100static struct inode_operations proc_pid_link_inode_operations = {
1101 .readlink = proc_pid_readlink,
1102 .follow_link = proc_pid_follow_link
1103};
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1106{
Eric W. Biederman56347082006-06-26 00:25:40 -07001107 struct dentry *dentry = filp->f_dentry;
1108 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct task_struct *p = proc_task(inode);
1110 unsigned int fd, tid, ino;
1111 int retval;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001112 char buf[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001114 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 retval = -ENOENT;
1117 if (!pid_alive(p))
1118 goto out;
1119 retval = 0;
1120 tid = p->pid;
1121
1122 fd = filp->f_pos;
1123 switch (fd) {
1124 case 0:
1125 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1126 goto out;
1127 filp->f_pos++;
1128 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001129 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1131 goto out;
1132 filp->f_pos++;
1133 default:
1134 files = get_files_struct(p);
1135 if (!files)
1136 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001137 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001138 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001140 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 fd++, filp->f_pos++) {
1142 unsigned int i,j;
1143
1144 if (!fcheck_files(files, fd))
1145 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001146 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001148 j = PROC_NUMBUF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 i = fd;
1150 do {
1151 j--;
1152 buf[j] = '0' + (i % 10);
1153 i /= 10;
1154 } while (i);
1155
1156 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001157 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001158 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 break;
1160 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001161 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001163 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 put_files_struct(files);
1165 }
1166out:
1167 return retval;
1168}
1169
1170static int proc_pident_readdir(struct file *filp,
1171 void *dirent, filldir_t filldir,
1172 struct pid_entry *ents, unsigned int nents)
1173{
1174 int i;
1175 int pid;
1176 struct dentry *dentry = filp->f_dentry;
1177 struct inode *inode = dentry->d_inode;
1178 struct pid_entry *p;
1179 ino_t ino;
1180 int ret;
1181
1182 ret = -ENOENT;
1183 if (!pid_alive(proc_task(inode)))
1184 goto out;
1185
1186 ret = 0;
1187 pid = proc_task(inode)->pid;
1188 i = filp->f_pos;
1189 switch (i) {
1190 case 0:
1191 ino = inode->i_ino;
1192 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1193 goto out;
1194 i++;
1195 filp->f_pos++;
1196 /* fall through */
1197 case 1:
1198 ino = parent_ino(dentry);
1199 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1200 goto out;
1201 i++;
1202 filp->f_pos++;
1203 /* fall through */
1204 default:
1205 i -= 2;
1206 if (i >= nents) {
1207 ret = 1;
1208 goto out;
1209 }
1210 p = ents + i;
1211 while (p->name) {
1212 if (filldir(dirent, p->name, p->len, filp->f_pos,
1213 fake_ino(pid, p->type), p->mode >> 12) < 0)
1214 goto out;
1215 filp->f_pos++;
1216 p++;
1217 }
1218 }
1219
1220 ret = 1;
1221out:
1222 return ret;
1223}
1224
1225static int proc_tgid_base_readdir(struct file * filp,
1226 void * dirent, filldir_t filldir)
1227{
1228 return proc_pident_readdir(filp,dirent,filldir,
1229 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1230}
1231
1232static int proc_tid_base_readdir(struct file * filp,
1233 void * dirent, filldir_t filldir)
1234{
1235 return proc_pident_readdir(filp,dirent,filldir,
1236 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1237}
1238
1239/* building an inode */
1240
1241static int task_dumpable(struct task_struct *task)
1242{
1243 int dumpable = 0;
1244 struct mm_struct *mm;
1245
1246 task_lock(task);
1247 mm = task->mm;
1248 if (mm)
1249 dumpable = mm->dumpable;
1250 task_unlock(task);
Alan Coxd6e71142005-06-23 00:09:43 -07001251 if(dumpable == 1)
1252 return 1;
1253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256
1257static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1258{
1259 struct inode * inode;
1260 struct proc_inode *ei;
1261
1262 /* We need a new inode */
1263
1264 inode = new_inode(sb);
1265 if (!inode)
1266 goto out;
1267
1268 /* Common stuff */
1269 ei = PROC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1271 inode->i_ino = fake_ino(task->pid, ino);
1272
1273 if (!pid_alive(task))
1274 goto out_unlock;
1275
1276 /*
1277 * grab the reference to task.
1278 */
1279 get_task_struct(task);
1280 ei->task = task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 inode->i_uid = 0;
1282 inode->i_gid = 0;
Eric W. Biederman87bfbf62006-06-26 00:25:43 -07001283 if (task_dumpable(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 inode->i_uid = task->euid;
1285 inode->i_gid = task->egid;
1286 }
1287 security_task_to_inode(task, inode);
1288
1289out:
1290 return inode;
1291
1292out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 iput(inode);
1294 return NULL;
1295}
1296
1297/* dentry stuff */
1298
1299/*
1300 * Exceptional case: normally we are not allowed to unhash a busy
1301 * directory. In this case, however, we can do it - no aliasing problems
1302 * due to the way we treat inodes.
1303 *
1304 * Rewrite the inode's ownerships here because the owning task may have
1305 * performed a setuid(), etc.
1306 */
1307static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1308{
1309 struct inode *inode = dentry->d_inode;
1310 struct task_struct *task = proc_task(inode);
1311 if (pid_alive(task)) {
Eric W. Biederman87bfbf62006-06-26 00:25:43 -07001312 if (task_dumpable(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 inode->i_uid = task->euid;
1314 inode->i_gid = task->egid;
1315 } else {
1316 inode->i_uid = 0;
1317 inode->i_gid = 0;
1318 }
1319 security_task_to_inode(task, inode);
1320 return 1;
1321 }
1322 d_drop(dentry);
1323 return 0;
1324}
1325
1326static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1327{
1328 struct inode *inode = dentry->d_inode;
1329 struct task_struct *task = proc_task(inode);
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -07001330 int fd = proc_fd(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 struct files_struct *files;
1332
1333 files = get_files_struct(task);
1334 if (files) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001335 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (fcheck_files(files, fd)) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001337 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 put_files_struct(files);
1339 if (task_dumpable(task)) {
1340 inode->i_uid = task->euid;
1341 inode->i_gid = task->egid;
1342 } else {
1343 inode->i_uid = 0;
1344 inode->i_gid = 0;
1345 }
1346 security_task_to_inode(task, inode);
1347 return 1;
1348 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001349 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 put_files_struct(files);
1351 }
1352 d_drop(dentry);
1353 return 0;
1354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356static int pid_delete_dentry(struct dentry * dentry)
1357{
1358 /* Is the task we represent dead?
1359 * If so, then don't put the dentry on the lru list,
1360 * kill it immediately.
1361 */
1362 return !pid_alive(proc_task(dentry->d_inode));
1363}
1364
1365static struct dentry_operations tid_fd_dentry_operations =
1366{
1367 .d_revalidate = tid_fd_revalidate,
1368 .d_delete = pid_delete_dentry,
1369};
1370
1371static struct dentry_operations pid_dentry_operations =
1372{
1373 .d_revalidate = pid_revalidate,
1374 .d_delete = pid_delete_dentry,
1375};
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377/* Lookups */
1378
1379static unsigned name_to_int(struct dentry *dentry)
1380{
1381 const char *name = dentry->d_name.name;
1382 int len = dentry->d_name.len;
1383 unsigned n = 0;
1384
1385 if (len > 1 && *name == '0')
1386 goto out;
1387 while (len-- > 0) {
1388 unsigned c = *name++ - '0';
1389 if (c > 9)
1390 goto out;
1391 if (n >= (~0U-9)/10)
1392 goto out;
1393 n *= 10;
1394 n += c;
1395 }
1396 return n;
1397out:
1398 return ~0U;
1399}
1400
1401/* SMP-safe */
1402static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1403{
1404 struct task_struct *task = proc_task(dir);
1405 unsigned fd = name_to_int(dentry);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001406 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct file * file;
1408 struct files_struct * files;
1409 struct inode *inode;
1410 struct proc_inode *ei;
1411
1412 if (fd == ~0U)
1413 goto out;
1414 if (!pid_alive(task))
1415 goto out;
1416
1417 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1418 if (!inode)
1419 goto out;
1420 ei = PROC_I(inode);
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -07001421 ei->fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 files = get_files_struct(task);
1423 if (!files)
1424 goto out_unlock;
1425 inode->i_mode = S_IFLNK;
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07001426
1427 /*
1428 * We are not taking a ref to the file structure, so we must
1429 * hold ->file_lock.
1430 */
1431 spin_lock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 file = fcheck_files(files, fd);
1433 if (!file)
1434 goto out_unlock2;
1435 if (file->f_mode & 1)
1436 inode->i_mode |= S_IRUSR | S_IXUSR;
1437 if (file->f_mode & 2)
1438 inode->i_mode |= S_IWUSR | S_IXUSR;
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07001439 spin_unlock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 put_files_struct(files);
1441 inode->i_op = &proc_pid_link_inode_operations;
1442 inode->i_size = 64;
1443 ei->op.proc_get_link = proc_fd_link;
1444 dentry->d_op = &tid_fd_dentry_operations;
1445 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001446 /* Close the race of the process dying before we return the dentry */
1447 if (tid_fd_revalidate(dentry, NULL))
1448 result = NULL;
1449out:
1450 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452out_unlock2:
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07001453 spin_unlock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 put_files_struct(files);
1455out_unlock:
1456 iput(inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1461static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07001462static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464static struct file_operations proc_fd_operations = {
1465 .read = generic_read_dir,
1466 .readdir = proc_readfd,
1467};
1468
1469static struct file_operations proc_task_operations = {
1470 .read = generic_read_dir,
1471 .readdir = proc_task_readdir,
1472};
1473
1474/*
1475 * proc directories can do almost nothing..
1476 */
1477static struct inode_operations proc_fd_inode_operations = {
1478 .lookup = proc_lookupfd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479};
1480
1481static struct inode_operations proc_task_inode_operations = {
1482 .lookup = proc_task_lookup,
Eric W. Biederman6e66b522006-06-26 00:25:47 -07001483 .getattr = proc_task_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484};
1485
1486#ifdef CONFIG_SECURITY
1487static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1488 size_t count, loff_t *ppos)
1489{
1490 struct inode * inode = file->f_dentry->d_inode;
1491 unsigned long page;
1492 ssize_t length;
1493 struct task_struct *task = proc_task(inode);
1494
1495 if (count > PAGE_SIZE)
1496 count = PAGE_SIZE;
1497 if (!(page = __get_free_page(GFP_KERNEL)))
1498 return -ENOMEM;
1499
1500 length = security_getprocattr(task,
1501 (char*)file->f_dentry->d_name.name,
1502 (void*)page, count);
1503 if (length >= 0)
1504 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1505 free_page(page);
1506 return length;
1507}
1508
1509static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1510 size_t count, loff_t *ppos)
1511{
1512 struct inode * inode = file->f_dentry->d_inode;
1513 char *page;
1514 ssize_t length;
1515 struct task_struct *task = proc_task(inode);
1516
1517 if (count > PAGE_SIZE)
1518 count = PAGE_SIZE;
1519 if (*ppos != 0) {
1520 /* No partial writes. */
1521 return -EINVAL;
1522 }
1523 page = (char*)__get_free_page(GFP_USER);
1524 if (!page)
1525 return -ENOMEM;
1526 length = -EFAULT;
1527 if (copy_from_user(page, buf, count))
1528 goto out;
1529
1530 length = security_setprocattr(task,
1531 (char*)file->f_dentry->d_name.name,
1532 (void*)page, count);
1533out:
1534 free_page((unsigned long) page);
1535 return length;
1536}
1537
1538static struct file_operations proc_pid_attr_operations = {
1539 .read = proc_pid_attr_read,
1540 .write = proc_pid_attr_write,
1541};
1542
1543static struct file_operations proc_tid_attr_operations;
1544static struct inode_operations proc_tid_attr_inode_operations;
1545static struct file_operations proc_tgid_attr_operations;
1546static struct inode_operations proc_tgid_attr_inode_operations;
1547#endif
1548
1549/* SMP-safe */
1550static struct dentry *proc_pident_lookup(struct inode *dir,
1551 struct dentry *dentry,
1552 struct pid_entry *ents)
1553{
1554 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001555 struct dentry *error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct task_struct *task = proc_task(dir);
1557 struct pid_entry *p;
1558 struct proc_inode *ei;
1559
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001560 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 inode = NULL;
1562
1563 if (!pid_alive(task))
1564 goto out;
1565
1566 for (p = ents; p->name; p++) {
1567 if (p->len != dentry->d_name.len)
1568 continue;
1569 if (!memcmp(dentry->d_name.name, p->name, p->len))
1570 break;
1571 }
1572 if (!p->name)
1573 goto out;
1574
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001575 error = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1577 if (!inode)
1578 goto out;
1579
1580 ei = PROC_I(inode);
1581 inode->i_mode = p->mode;
1582 /*
1583 * Yes, it does not scale. And it should not. Don't add
1584 * new entries into /proc/<tgid>/ without very good reasons.
1585 */
1586 switch(p->type) {
1587 case PROC_TGID_TASK:
Eric W. Biederman6e66b522006-06-26 00:25:47 -07001588 inode->i_nlink = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 inode->i_op = &proc_task_inode_operations;
1590 inode->i_fop = &proc_task_operations;
1591 break;
1592 case PROC_TID_FD:
1593 case PROC_TGID_FD:
1594 inode->i_nlink = 2;
1595 inode->i_op = &proc_fd_inode_operations;
1596 inode->i_fop = &proc_fd_operations;
1597 break;
1598 case PROC_TID_EXE:
1599 case PROC_TGID_EXE:
1600 inode->i_op = &proc_pid_link_inode_operations;
1601 ei->op.proc_get_link = proc_exe_link;
1602 break;
1603 case PROC_TID_CWD:
1604 case PROC_TGID_CWD:
1605 inode->i_op = &proc_pid_link_inode_operations;
1606 ei->op.proc_get_link = proc_cwd_link;
1607 break;
1608 case PROC_TID_ROOT:
1609 case PROC_TGID_ROOT:
1610 inode->i_op = &proc_pid_link_inode_operations;
1611 ei->op.proc_get_link = proc_root_link;
1612 break;
1613 case PROC_TID_ENVIRON:
1614 case PROC_TGID_ENVIRON:
1615 inode->i_fop = &proc_info_file_operations;
1616 ei->op.proc_read = proc_pid_environ;
1617 break;
1618 case PROC_TID_AUXV:
1619 case PROC_TGID_AUXV:
1620 inode->i_fop = &proc_info_file_operations;
1621 ei->op.proc_read = proc_pid_auxv;
1622 break;
1623 case PROC_TID_STATUS:
1624 case PROC_TGID_STATUS:
1625 inode->i_fop = &proc_info_file_operations;
1626 ei->op.proc_read = proc_pid_status;
1627 break;
1628 case PROC_TID_STAT:
1629 inode->i_fop = &proc_info_file_operations;
1630 ei->op.proc_read = proc_tid_stat;
1631 break;
1632 case PROC_TGID_STAT:
1633 inode->i_fop = &proc_info_file_operations;
1634 ei->op.proc_read = proc_tgid_stat;
1635 break;
1636 case PROC_TID_CMDLINE:
1637 case PROC_TGID_CMDLINE:
1638 inode->i_fop = &proc_info_file_operations;
1639 ei->op.proc_read = proc_pid_cmdline;
1640 break;
1641 case PROC_TID_STATM:
1642 case PROC_TGID_STATM:
1643 inode->i_fop = &proc_info_file_operations;
1644 ei->op.proc_read = proc_pid_statm;
1645 break;
1646 case PROC_TID_MAPS:
1647 case PROC_TGID_MAPS:
1648 inode->i_fop = &proc_maps_operations;
1649 break;
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001650#ifdef CONFIG_NUMA
1651 case PROC_TID_NUMA_MAPS:
1652 case PROC_TGID_NUMA_MAPS:
1653 inode->i_fop = &proc_numa_maps_operations;
1654 break;
1655#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 case PROC_TID_MEM:
1657 case PROC_TGID_MEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 inode->i_fop = &proc_mem_operations;
1659 break;
1660#ifdef CONFIG_SECCOMP
1661 case PROC_TID_SECCOMP:
1662 case PROC_TGID_SECCOMP:
1663 inode->i_fop = &proc_seccomp_operations;
1664 break;
1665#endif /* CONFIG_SECCOMP */
1666 case PROC_TID_MOUNTS:
1667 case PROC_TGID_MOUNTS:
1668 inode->i_fop = &proc_mounts_operations;
1669 break;
Yoshinori Sato63c67642005-10-14 15:59:11 -07001670#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -07001671 case PROC_TID_SMAPS:
1672 case PROC_TGID_SMAPS:
1673 inode->i_fop = &proc_smaps_operations;
1674 break;
Yoshinori Sato63c67642005-10-14 15:59:11 -07001675#endif
Chuck Leverb4629fe2006-03-20 13:44:12 -05001676 case PROC_TID_MOUNTSTATS:
1677 case PROC_TGID_MOUNTSTATS:
1678 inode->i_fop = &proc_mountstats_operations;
1679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680#ifdef CONFIG_SECURITY
1681 case PROC_TID_ATTR:
1682 inode->i_nlink = 2;
1683 inode->i_op = &proc_tid_attr_inode_operations;
1684 inode->i_fop = &proc_tid_attr_operations;
1685 break;
1686 case PROC_TGID_ATTR:
1687 inode->i_nlink = 2;
1688 inode->i_op = &proc_tgid_attr_inode_operations;
1689 inode->i_fop = &proc_tgid_attr_operations;
1690 break;
1691 case PROC_TID_ATTR_CURRENT:
1692 case PROC_TGID_ATTR_CURRENT:
1693 case PROC_TID_ATTR_PREV:
1694 case PROC_TGID_ATTR_PREV:
1695 case PROC_TID_ATTR_EXEC:
1696 case PROC_TGID_ATTR_EXEC:
1697 case PROC_TID_ATTR_FSCREATE:
1698 case PROC_TGID_ATTR_FSCREATE:
Michael LeMay4eb582c2006-06-26 00:24:57 -07001699 case PROC_TID_ATTR_KEYCREATE:
1700 case PROC_TGID_ATTR_KEYCREATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 inode->i_fop = &proc_pid_attr_operations;
1702 break;
1703#endif
1704#ifdef CONFIG_KALLSYMS
1705 case PROC_TID_WCHAN:
1706 case PROC_TGID_WCHAN:
1707 inode->i_fop = &proc_info_file_operations;
1708 ei->op.proc_read = proc_pid_wchan;
1709 break;
1710#endif
1711#ifdef CONFIG_SCHEDSTATS
1712 case PROC_TID_SCHEDSTAT:
1713 case PROC_TGID_SCHEDSTAT:
1714 inode->i_fop = &proc_info_file_operations;
1715 ei->op.proc_read = proc_pid_schedstat;
1716 break;
1717#endif
1718#ifdef CONFIG_CPUSETS
1719 case PROC_TID_CPUSET:
1720 case PROC_TGID_CPUSET:
1721 inode->i_fop = &proc_cpuset_operations;
1722 break;
1723#endif
1724 case PROC_TID_OOM_SCORE:
1725 case PROC_TGID_OOM_SCORE:
1726 inode->i_fop = &proc_info_file_operations;
1727 ei->op.proc_read = proc_oom_score;
1728 break;
1729 case PROC_TID_OOM_ADJUST:
1730 case PROC_TGID_OOM_ADJUST:
1731 inode->i_fop = &proc_oom_adjust_operations;
1732 break;
1733#ifdef CONFIG_AUDITSYSCALL
1734 case PROC_TID_LOGINUID:
1735 case PROC_TGID_LOGINUID:
1736 inode->i_fop = &proc_loginuid_operations;
1737 break;
1738#endif
1739 default:
1740 printk("procfs: impossible type (%d)",p->type);
1741 iput(inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001742 error = ERR_PTR(-EINVAL);
1743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745 dentry->d_op = &pid_dentry_operations;
1746 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001747 /* Close the race of the process dying before we return the dentry */
1748 if (pid_revalidate(dentry, NULL))
1749 error = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001751 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
1754static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1755 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1756}
1757
1758static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1759 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1760}
1761
1762static struct file_operations proc_tgid_base_operations = {
1763 .read = generic_read_dir,
1764 .readdir = proc_tgid_base_readdir,
1765};
1766
1767static struct file_operations proc_tid_base_operations = {
1768 .read = generic_read_dir,
1769 .readdir = proc_tid_base_readdir,
1770};
1771
1772static struct inode_operations proc_tgid_base_inode_operations = {
1773 .lookup = proc_tgid_base_lookup,
1774};
1775
1776static struct inode_operations proc_tid_base_inode_operations = {
1777 .lookup = proc_tid_base_lookup,
1778};
1779
1780#ifdef CONFIG_SECURITY
1781static int proc_tgid_attr_readdir(struct file * filp,
1782 void * dirent, filldir_t filldir)
1783{
1784 return proc_pident_readdir(filp,dirent,filldir,
1785 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1786}
1787
1788static int proc_tid_attr_readdir(struct file * filp,
1789 void * dirent, filldir_t filldir)
1790{
1791 return proc_pident_readdir(filp,dirent,filldir,
1792 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1793}
1794
1795static struct file_operations proc_tgid_attr_operations = {
1796 .read = generic_read_dir,
1797 .readdir = proc_tgid_attr_readdir,
1798};
1799
1800static struct file_operations proc_tid_attr_operations = {
1801 .read = generic_read_dir,
1802 .readdir = proc_tid_attr_readdir,
1803};
1804
1805static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1806 struct dentry *dentry, struct nameidata *nd)
1807{
1808 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1809}
1810
1811static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1812 struct dentry *dentry, struct nameidata *nd)
1813{
1814 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1815}
1816
1817static struct inode_operations proc_tgid_attr_inode_operations = {
1818 .lookup = proc_tgid_attr_lookup,
1819};
1820
1821static struct inode_operations proc_tid_attr_inode_operations = {
1822 .lookup = proc_tid_attr_lookup,
1823};
1824#endif
1825
1826/*
1827 * /proc/self:
1828 */
1829static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1830 int buflen)
1831{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001832 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 sprintf(tmp, "%d", current->tgid);
1834 return vfs_readlink(dentry,buffer,buflen,tmp);
1835}
1836
Al Viro008b1502005-08-20 00:17:39 +01001837static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001839 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001841 return ERR_PTR(vfs_follow_link(nd,tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844static struct inode_operations proc_self_inode_operations = {
1845 .readlink = proc_self_readlink,
1846 .follow_link = proc_self_follow_link,
1847};
1848
1849/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001850 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001852 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001854 * Looks in the dcache for
1855 * /proc/@pid
1856 * /proc/@tgid/task/@pid
1857 * if either directory is present flushes it and all of it'ts children
1858 * from the dcache.
1859 *
1860 * It is safe and reasonable to cache /proc entries for a task until
1861 * that task exits. After that they just clog up the dcache with
1862 * useless entries, possibly causing useful dcache entries to be
1863 * flushed instead. This routine is proved to flush those useless
1864 * dcache entries at process exit time.
1865 *
1866 * NOTE: This routine is just an optimization so it does not guarantee
1867 * that no dcache entries will exist at process exit time it
1868 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001870void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001872 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001873 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001874 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Eric W. Biederman48e64842006-06-26 00:25:48 -07001876 name.name = buf;
1877 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1878 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1879 if (dentry) {
1880 shrink_dcache_parent(dentry);
1881 d_drop(dentry);
1882 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Eric W. Biederman48e64842006-06-26 00:25:48 -07001885 if (thread_group_leader(task))
1886 goto out;
1887
1888 name.name = buf;
1889 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1890 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1891 if (!leader)
1892 goto out;
1893
1894 name.name = "task";
1895 name.len = strlen(name.name);
1896 dir = d_hash_and_lookup(leader, &name);
1897 if (!dir)
1898 goto out_put_leader;
1899
1900 name.name = buf;
1901 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1902 dentry = d_hash_and_lookup(dir, &name);
1903 if (dentry) {
1904 shrink_dcache_parent(dentry);
1905 d_drop(dentry);
1906 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001908
1909 dput(dir);
1910out_put_leader:
1911 dput(leader);
1912out:
1913 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
1916/* SMP-safe */
1917struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1918{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001919 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct task_struct *task;
1921 struct inode *inode;
1922 struct proc_inode *ei;
1923 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1926 inode = new_inode(dir->i_sb);
1927 if (!inode)
1928 return ERR_PTR(-ENOMEM);
1929 ei = PROC_I(inode);
1930 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1931 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1932 ei->pde = NULL;
1933 inode->i_mode = S_IFLNK|S_IRWXUGO;
1934 inode->i_uid = inode->i_gid = 0;
1935 inode->i_size = 64;
1936 inode->i_op = &proc_self_inode_operations;
1937 d_add(dentry, inode);
1938 return NULL;
1939 }
1940 tgid = name_to_int(dentry);
1941 if (tgid == ~0U)
1942 goto out;
1943
Eric W. Biedermande758732006-06-26 00:25:51 -07001944 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 task = find_task_by_pid(tgid);
1946 if (task)
1947 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07001948 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (!task)
1950 goto out;
1951
1952 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001953 if (!inode)
1954 goto out_put_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1957 inode->i_op = &proc_tgid_base_inode_operations;
1958 inode->i_fop = &proc_tgid_base_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 inode->i_flags|=S_IMMUTABLE;
Daniel Drakebcf88e12005-05-01 08:59:03 -07001960#ifdef CONFIG_SECURITY
1961 inode->i_nlink = 5;
1962#else
1963 inode->i_nlink = 4;
1964#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Eric W. Biederman48e64842006-06-26 00:25:48 -07001966 dentry->d_op = &pid_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001969 /* Close the race of the process dying before we return the dentry */
1970 if (pid_revalidate(dentry, NULL))
1971 result = NULL;
Eric W. Biederman48e64842006-06-26 00:25:48 -07001972
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001973out_put_task:
Eric W. Biederman48e64842006-06-26 00:25:48 -07001974 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001976 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
1979/* SMP-safe */
1980static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1981{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001982 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct task_struct *task;
1984 struct task_struct *leader = proc_task(dir);
1985 struct inode *inode;
1986 unsigned tid;
1987
1988 tid = name_to_int(dentry);
1989 if (tid == ~0U)
1990 goto out;
1991
Eric W. Biedermande758732006-06-26 00:25:51 -07001992 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 task = find_task_by_pid(tid);
1994 if (task)
1995 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07001996 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (!task)
1998 goto out;
1999 if (leader->tgid != task->tgid)
2000 goto out_drop_task;
2001
2002 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2003
2004
2005 if (!inode)
2006 goto out_drop_task;
2007 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2008 inode->i_op = &proc_tid_base_inode_operations;
2009 inode->i_fop = &proc_tid_base_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 inode->i_flags|=S_IMMUTABLE;
Daniel Drakebcf88e12005-05-01 08:59:03 -07002011#ifdef CONFIG_SECURITY
2012 inode->i_nlink = 4;
2013#else
2014 inode->i_nlink = 3;
2015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Eric W. Biederman48e64842006-06-26 00:25:48 -07002017 dentry->d_op = &pid_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002020 /* Close the race of the process dying before we return the dentry */
2021 if (pid_revalidate(dentry, NULL))
2022 result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024out_drop_task:
2025 put_task_struct(task);
2026out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002027 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002031 * Find the first tgid to return to user space.
2032 *
2033 * Usually this is just whatever follows &init_task, but if the users
2034 * buffer was too small to hold the full list or there was a seek into
2035 * the middle of the directory we have more work to do.
2036 *
2037 * In the case of a short read we start with find_task_by_pid.
2038 *
2039 * In the case of a seek we start with &init_task and walk nr
2040 * threads past it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 */
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002042static struct task_struct *first_tgid(int tgid, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002044 struct task_struct *pos;
Eric W. Biederman454cc102006-06-26 00:25:51 -07002045 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002046 if (tgid && nr) {
2047 pos = find_task_by_pid(tgid);
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002048 if (pos && thread_group_leader(pos))
2049 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002051 /* If nr exceeds the number of processes get out quickly */
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002052 pos = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002053 if (nr && nr >= nr_processes())
2054 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002056 /* If we haven't found our starting place yet start with
2057 * the init_task and walk nr tasks forward.
2058 */
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002059 for (pos = next_task(&init_task); nr > 0; --nr) {
2060 pos = next_task(pos);
2061 if (pos == &init_task) {
2062 pos = NULL;
2063 goto done;
2064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002066found:
2067 get_task_struct(pos);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002068done:
Eric W. Biederman454cc102006-06-26 00:25:51 -07002069 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002070 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
2073/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002074 * Find the next task in the task list.
2075 * Return NULL if we loop or there is any error.
2076 *
2077 * The reference to the input task_struct is released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002079static struct task_struct *next_tgid(struct task_struct *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002081 struct task_struct *pos;
Eric W. Biederman454cc102006-06-26 00:25:51 -07002082 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002083 pos = start;
2084 if (pid_alive(start))
2085 pos = next_task(start);
2086 if (pid_alive(pos) && (pos != &init_task)) {
2087 get_task_struct(pos);
2088 goto done;
2089 }
2090 pos = NULL;
2091done:
Eric W. Biederman454cc102006-06-26 00:25:51 -07002092 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002093 put_task_struct(start);
2094 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
2097/* for the /proc/ directory itself, after non-process stuff has been done */
2098int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 char buf[PROC_NUMBUF];
2101 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002102 struct task_struct *task;
2103 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 if (!nr) {
2106 ino_t ino = fake_ino(0,PROC_TGID_INO);
2107 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2108 return 0;
2109 filp->f_pos++;
2110 nr++;
2111 }
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002112 nr -= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 /* f_version caches the tgid value that the last readdir call couldn't
2115 * return. lseek aka telldir automagically resets f_version to 0.
2116 */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002117 tgid = filp->f_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 filp->f_version = 0;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002119 for (task = first_tgid(tgid, nr);
2120 task;
2121 task = next_tgid(task), filp->f_pos++) {
2122 int len;
2123 ino_t ino;
2124 tgid = task->pid;
2125 len = snprintf(buf, sizeof(buf), "%d", tgid);
2126 ino = fake_ino(tgid, PROC_TGID_INO);
2127 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2128 /* returning this tgid failed, save it as the first
2129 * pid for the next readir call */
2130 filp->f_version = tgid;
2131 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 break;
2133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return 0;
2136}
2137
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002138/*
2139 * Find the first tid of a thread group to return to user space.
2140 *
2141 * Usually this is just the thread group leader, but if the users
2142 * buffer was too small or there was a seek into the middle of the
2143 * directory we have more work todo.
2144 *
2145 * In the case of a short read we start with find_task_by_pid.
2146 *
2147 * In the case of a seek we start with the leader and walk nr
2148 * threads past it.
2149 */
2150static struct task_struct *first_tid(struct task_struct *leader, int tid, int nr)
2151{
2152 struct task_struct *pos = NULL;
2153 read_lock(&tasklist_lock);
2154
2155 /* Attempt to start with the pid of a thread */
2156 if (tid && (nr > 0)) {
2157 pos = find_task_by_pid(tid);
2158 if (pos && (pos->group_leader != leader))
2159 pos = NULL;
2160 if (pos)
2161 nr = 0;
2162 }
2163
2164 /* If nr exceeds the number of threads there is nothing todo */
2165 if (nr) {
2166 int threads = 0;
2167 task_lock(leader);
2168 if (leader->signal)
2169 threads = atomic_read(&leader->signal->count);
2170 task_unlock(leader);
2171 if (nr >= threads)
2172 goto done;
2173 }
2174
2175 /* If we haven't found our starting place yet start with the
2176 * leader and walk nr threads forward.
2177 */
2178 if (!pos && (nr >= 0))
2179 pos = leader;
2180
2181 for (; pos && pid_alive(pos); pos = next_thread(pos)) {
2182 if (--nr > 0)
2183 continue;
2184 get_task_struct(pos);
2185 goto done;
2186 }
2187 pos = NULL;
2188done:
2189 read_unlock(&tasklist_lock);
2190 return pos;
2191}
2192
2193/*
2194 * Find the next thread in the thread list.
2195 * Return NULL if there is an error or no next thread.
2196 *
2197 * The reference to the input task_struct is released.
2198 */
2199static struct task_struct *next_tid(struct task_struct *start)
2200{
2201 struct task_struct *pos;
2202 read_lock(&tasklist_lock);
2203 pos = start;
2204 if (pid_alive(start))
2205 pos = next_thread(start);
2206 if (pid_alive(pos) && (pos != start->group_leader))
2207 get_task_struct(pos);
2208 else
2209 pos = NULL;
2210 read_unlock(&tasklist_lock);
2211 put_task_struct(start);
2212 return pos;
2213}
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215/* for the /proc/TGID/task/ directories */
2216static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 char buf[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 struct dentry *dentry = filp->f_dentry;
2220 struct inode *inode = dentry->d_inode;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002221 struct task_struct *leader = proc_task(inode);
2222 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 int retval = -ENOENT;
2224 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002225 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2227
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002228 if (!pid_alive(leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 goto out;
2230 retval = 0;
2231
2232 switch (pos) {
2233 case 0:
2234 ino = inode->i_ino;
2235 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2236 goto out;
2237 pos++;
2238 /* fall through */
2239 case 1:
2240 ino = parent_ino(dentry);
2241 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2242 goto out;
2243 pos++;
2244 /* fall through */
2245 }
2246
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002247 /* f_version caches the tgid value that the last readdir call couldn't
2248 * return. lseek aka telldir automagically resets f_version to 0.
2249 */
2250 tid = filp->f_version;
2251 filp->f_version = 0;
2252 for (task = first_tid(leader, tid, pos - 2);
2253 task;
2254 task = next_tid(task), pos++) {
2255 int len;
2256 tid = task->pid;
2257 len = snprintf(buf, sizeof(buf), "%d", tid);
2258 ino = fake_ino(tid, PROC_TID_INO);
2259 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2260 /* returning this tgid failed, save it as the first
2261 * pid for the next readir call */
2262 filp->f_version = tid;
2263 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267out:
2268 filp->f_pos = pos;
2269 return retval;
2270}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002271
2272static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2273{
2274 struct inode *inode = dentry->d_inode;
2275 struct task_struct *p = proc_task(inode);
2276 generic_fillattr(inode, stat);
2277
2278 if (pid_alive(p)) {
2279 task_lock(p);
2280 if (p->signal)
2281 stat->nlink += atomic_read(&p->signal->count);
2282 task_unlock(p);
2283 }
2284
2285 return 0;
2286}