blob: f38da6bda269c3b7627dbd755d4fa8714ca6fb71 [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{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700310 struct task_struct *task = get_proc_task(inode);
311 struct files_struct *files = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 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
Eric W. Biederman99f89552006-06-26 00:25:55 -0700315 if (task) {
316 files = get_files_struct(task);
317 put_task_struct(task);
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (files) {
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -0700320 /*
321 * We are not taking a ref to the file structure, so we must
322 * hold ->file_lock.
323 */
324 spin_lock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 file = fcheck_files(files, fd);
326 if (file) {
327 *mnt = mntget(file->f_vfsmnt);
328 *dentry = dget(file->f_dentry);
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -0700329 spin_unlock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 put_files_struct(files);
331 return 0;
332 }
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -0700333 spin_unlock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 put_files_struct(files);
335 }
336 return -ENOENT;
337}
338
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700339static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700342 task_lock(task);
343 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if(fs)
345 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700346 task_unlock(task);
347 return fs;
348}
349
Eric W. Biederman99f89552006-06-26 00:25:55 -0700350static int get_nr_threads(struct task_struct *tsk)
351{
352 /* Must be called with the rcu_read_lock held */
353 unsigned long flags;
354 int count = 0;
355
356 if (lock_task_sighand(tsk, &flags)) {
357 count = atomic_read(&tsk->signal->count);
358 unlock_task_sighand(tsk, &flags);
359 }
360 return count;
361}
362
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700363static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
364{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700365 struct task_struct *task = get_proc_task(inode);
366 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700367 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700368
369 if (task) {
370 fs = get_fs_struct(task);
371 put_task_struct(task);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (fs) {
374 read_lock(&fs->lock);
375 *mnt = mntget(fs->pwdmnt);
376 *dentry = dget(fs->pwd);
377 read_unlock(&fs->lock);
378 result = 0;
379 put_fs_struct(fs);
380 }
381 return result;
382}
383
384static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
385{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700386 struct task_struct *task = get_proc_task(inode);
387 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700389
390 if (task) {
391 fs = get_fs_struct(task);
392 put_task_struct(task);
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (fs) {
395 read_lock(&fs->lock);
396 *mnt = mntget(fs->rootmnt);
397 *dentry = dget(fs->root);
398 read_unlock(&fs->lock);
399 result = 0;
400 put_fs_struct(fs);
401 }
402 return result;
403}
404
405#define MAY_PTRACE(task) \
406 (task == current || \
407 (task->parent == current && \
408 (task->ptrace & PT_PTRACED) && \
409 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
410 security_ptrace(current,task) == 0))
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static int proc_pid_environ(struct task_struct *task, char * buffer)
413{
414 int res = 0;
415 struct mm_struct *mm = get_task_mm(task);
416 if (mm) {
417 unsigned int len = mm->env_end - mm->env_start;
418 if (len > PAGE_SIZE)
419 len = PAGE_SIZE;
420 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700421 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 res = -ESRCH;
423 mmput(mm);
424 }
425 return res;
426}
427
428static int proc_pid_cmdline(struct task_struct *task, char * buffer)
429{
430 int res = 0;
431 unsigned int len;
432 struct mm_struct *mm = get_task_mm(task);
433 if (!mm)
434 goto out;
435 if (!mm->arg_end)
436 goto out_mm; /* Shh! No looking before we're done */
437
438 len = mm->arg_end - mm->arg_start;
439
440 if (len > PAGE_SIZE)
441 len = PAGE_SIZE;
442
443 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
444
445 // If the nul at the end of args has been overwritten, then
446 // assume application is using setproctitle(3).
447 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
448 len = strnlen(buffer, res);
449 if (len < res) {
450 res = len;
451 } else {
452 len = mm->env_end - mm->env_start;
453 if (len > PAGE_SIZE - res)
454 len = PAGE_SIZE - res;
455 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
456 res = strnlen(buffer, res);
457 }
458 }
459out_mm:
460 mmput(mm);
461out:
462 return res;
463}
464
465static int proc_pid_auxv(struct task_struct *task, char *buffer)
466{
467 int res = 0;
468 struct mm_struct *mm = get_task_mm(task);
469 if (mm) {
470 unsigned int nwords = 0;
471 do
472 nwords += 2;
473 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
474 res = nwords * sizeof(mm->saved_auxv[0]);
475 if (res > PAGE_SIZE)
476 res = PAGE_SIZE;
477 memcpy(buffer, mm->saved_auxv, res);
478 mmput(mm);
479 }
480 return res;
481}
482
483
484#ifdef CONFIG_KALLSYMS
485/*
486 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
487 * Returns the resolved symbol. If that fails, simply return the address.
488 */
489static int proc_pid_wchan(struct task_struct *task, char *buffer)
490{
491 char *modname;
492 const char *sym_name;
493 unsigned long wchan, size, offset;
494 char namebuf[KSYM_NAME_LEN+1];
495
496 wchan = get_wchan(task);
497
498 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
499 if (sym_name)
500 return sprintf(buffer, "%s", sym_name);
501 return sprintf(buffer, "%lu", wchan);
502}
503#endif /* CONFIG_KALLSYMS */
504
505#ifdef CONFIG_SCHEDSTATS
506/*
507 * Provides /proc/PID/schedstat
508 */
509static int proc_pid_schedstat(struct task_struct *task, char *buffer)
510{
511 return sprintf(buffer, "%lu %lu %lu\n",
512 task->sched_info.cpu_time,
513 task->sched_info.run_delay,
514 task->sched_info.pcnt);
515}
516#endif
517
518/* The badness from the OOM killer */
519unsigned long badness(struct task_struct *p, unsigned long uptime);
520static int proc_oom_score(struct task_struct *task, char *buffer)
521{
522 unsigned long points;
523 struct timespec uptime;
524
525 do_posix_clock_monotonic_gettime(&uptime);
526 points = badness(task, uptime.tv_sec);
527 return sprintf(buffer, "%lu\n", points);
528}
529
530/************************************************************************/
531/* Here the fs part begins */
532/************************************************************************/
533
534/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700535static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700537 struct task_struct *task;
538 int allowed = 0;
539 /* Allow access to a task's file descriptors if either we may
540 * use ptrace attach to the process and find out that
541 * information, or if the task cannot possibly be ptraced
542 * allow access if we have the proper capability.
543 */
544 task = get_proc_task(inode);
545 if (task == current)
546 allowed = 1;
547 if (task && !allowed) {
548 int alive;
Herbert Poetzle4e5d3f2006-03-31 02:31:35 -0800549
Eric W. Biederman778c1142006-06-26 00:25:58 -0700550 task_lock(task);
551 alive = !!task->mm;
552 task_unlock(task);
553 if (alive)
554 /* For a living task obey ptrace_may_attach */
555 allowed = ptrace_may_attach(task);
556 else
557 /* For a special task simply check the capability */
558 allowed = capable(CAP_SYS_PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700560 if (task)
561 put_task_struct(task);
562 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500566struct proc_mounts {
567 struct seq_file m;
568 int event;
569};
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571static int mounts_open(struct inode *inode, struct file *file)
572{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700573 struct task_struct *task = get_proc_task(inode);
574 struct namespace *namespace = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500575 struct proc_mounts *p;
576 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Eric W. Biederman99f89552006-06-26 00:25:55 -0700578 if (task) {
579 task_lock(task);
580 namespace = task->namespace;
581 if (namespace)
582 get_namespace(namespace);
583 task_unlock(task);
584 put_task_struct(task);
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Al Viro5addc5d2005-11-07 17:15:49 -0500587 if (namespace) {
588 ret = -ENOMEM;
589 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
590 if (p) {
591 file->private_data = &p->m;
592 ret = seq_open(file, &mounts_op);
593 if (!ret) {
594 p->m.private = namespace;
595 p->event = namespace->event;
596 return 0;
597 }
598 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Al Viro5addc5d2005-11-07 17:15:49 -0500600 put_namespace(namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 return ret;
603}
604
605static int mounts_release(struct inode *inode, struct file *file)
606{
607 struct seq_file *m = file->private_data;
608 struct namespace *namespace = m->private;
609 put_namespace(namespace);
610 return seq_release(inode, file);
611}
612
Al Viro5addc5d2005-11-07 17:15:49 -0500613static unsigned mounts_poll(struct file *file, poll_table *wait)
614{
615 struct proc_mounts *p = file->private_data;
616 struct namespace *ns = p->m.private;
617 unsigned res = 0;
618
619 poll_wait(file, &ns->poll, wait);
620
621 spin_lock(&vfsmount_lock);
622 if (p->event != ns->event) {
623 p->event = ns->event;
624 res = POLLERR;
625 }
626 spin_unlock(&vfsmount_lock);
627
628 return res;
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631static struct file_operations proc_mounts_operations = {
632 .open = mounts_open,
633 .read = seq_read,
634 .llseek = seq_lseek,
635 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500636 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637};
638
Chuck Leverb4629fe2006-03-20 13:44:12 -0500639extern struct seq_operations mountstats_op;
640static int mountstats_open(struct inode *inode, struct file *file)
641{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500642 int ret = seq_open(file, &mountstats_op);
643
644 if (!ret) {
645 struct seq_file *m = file->private_data;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700646 struct namespace *namespace = NULL;
647 struct task_struct *task = get_proc_task(inode);
648
649 if (task) {
650 task_lock(task);
651 namespace = task->namespace;
652 if (namespace)
653 get_namespace(namespace);
654 task_unlock(task);
655 put_task_struct(task);
656 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500657
658 if (namespace)
659 m->private = namespace;
660 else {
661 seq_release(inode, file);
662 ret = -EINVAL;
663 }
664 }
665 return ret;
666}
667
668static struct file_operations proc_mountstats_operations = {
669 .open = mountstats_open,
670 .read = seq_read,
671 .llseek = seq_lseek,
672 .release = mounts_release,
673};
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
676
677static ssize_t proc_info_read(struct file * file, char __user * buf,
678 size_t count, loff_t *ppos)
679{
680 struct inode * inode = file->f_dentry->d_inode;
681 unsigned long page;
682 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700683 struct task_struct *task = get_proc_task(inode);
684
685 length = -ESRCH;
686 if (!task)
687 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (count > PROC_BLOCK_SIZE)
690 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700691
692 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700694 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 length = PROC_I(inode)->op.proc_read(task, (char*)page);
697
698 if (length >= 0)
699 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
700 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700701out:
702 put_task_struct(task);
703out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return length;
705}
706
707static struct file_operations proc_info_file_operations = {
708 .read = proc_info_read,
709};
710
711static int mem_open(struct inode* inode, struct file* file)
712{
713 file->private_data = (void*)((long)current->self_exec_id);
714 return 0;
715}
716
717static ssize_t mem_read(struct file * file, char __user * buf,
718 size_t count, loff_t *ppos)
719{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700720 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 char *page;
722 unsigned long src = *ppos;
723 int ret = -ESRCH;
724 struct mm_struct *mm;
725
Eric W. Biederman99f89552006-06-26 00:25:55 -0700726 if (!task)
727 goto out_no_task;
728
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700729 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 goto out;
731
732 ret = -ENOMEM;
733 page = (char *)__get_free_page(GFP_USER);
734 if (!page)
735 goto out;
736
737 ret = 0;
738
739 mm = get_task_mm(task);
740 if (!mm)
741 goto out_free;
742
743 ret = -EIO;
744
745 if (file->private_data != (void*)((long)current->self_exec_id))
746 goto out_put;
747
748 ret = 0;
749
750 while (count > 0) {
751 int this_len, retval;
752
753 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
754 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700755 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (!ret)
757 ret = -EIO;
758 break;
759 }
760
761 if (copy_to_user(buf, page, retval)) {
762 ret = -EFAULT;
763 break;
764 }
765
766 ret += retval;
767 src += retval;
768 buf += retval;
769 count -= retval;
770 }
771 *ppos = src;
772
773out_put:
774 mmput(mm);
775out_free:
776 free_page((unsigned long) page);
777out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700778 put_task_struct(task);
779out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return ret;
781}
782
783#define mem_write NULL
784
785#ifndef mem_write
786/* This is a security hazard */
787static ssize_t mem_write(struct file * file, const char * buf,
788 size_t count, loff_t *ppos)
789{
790 int copied = 0;
791 char *page;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700792 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 unsigned long dst = *ppos;
794
Eric W. Biederman99f89552006-06-26 00:25:55 -0700795 copied = -ESRCH;
796 if (!task)
797 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Eric W. Biederman99f89552006-06-26 00:25:55 -0700799 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
800 goto out;
801
802 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 page = (char *)__get_free_page(GFP_USER);
804 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700805 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 while (count > 0) {
808 int this_len, retval;
809
810 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
811 if (copy_from_user(page, buf, this_len)) {
812 copied = -EFAULT;
813 break;
814 }
815 retval = access_process_vm(task, dst, page, this_len, 1);
816 if (!retval) {
817 if (!copied)
818 copied = -EIO;
819 break;
820 }
821 copied += retval;
822 buf += retval;
823 dst += retval;
824 count -= retval;
825 }
826 *ppos = dst;
827 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700828out:
829 put_task_struct(task);
830out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return copied;
832}
833#endif
834
835static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
836{
837 switch (orig) {
838 case 0:
839 file->f_pos = offset;
840 break;
841 case 1:
842 file->f_pos += offset;
843 break;
844 default:
845 return -EINVAL;
846 }
847 force_successful_syscall_return();
848 return file->f_pos;
849}
850
851static struct file_operations proc_mem_operations = {
852 .llseek = mem_lseek,
853 .read = mem_read,
854 .write = mem_write,
855 .open = mem_open,
856};
857
858static ssize_t oom_adjust_read(struct file *file, char __user *buf,
859 size_t count, loff_t *ppos)
860{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700861 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700862 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700864 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 loff_t __ppos = *ppos;
866
Eric W. Biederman99f89552006-06-26 00:25:55 -0700867 if (!task)
868 return -ESRCH;
869 oom_adjust = task->oomkilladj;
870 put_task_struct(task);
871
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700872 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (__ppos >= len)
874 return 0;
875 if (count > len-__ppos)
876 count = len-__ppos;
877 if (copy_to_user(buf, buffer + __ppos, count))
878 return -EFAULT;
879 *ppos = __ppos + count;
880 return count;
881}
882
883static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
884 size_t count, loff_t *ppos)
885{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700886 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700887 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int oom_adjust;
889
890 if (!capable(CAP_SYS_RESOURCE))
891 return -EPERM;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700892 memset(buffer, 0, sizeof(buffer));
893 if (count > sizeof(buffer) - 1)
894 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (copy_from_user(buffer, buf, count))
896 return -EFAULT;
897 oom_adjust = simple_strtol(buffer, &end, 0);
Andrea Arcangeli79befd02005-04-16 15:24:05 -0700898 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -EINVAL;
900 if (*end == '\n')
901 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700902 task = get_proc_task(file->f_dentry->d_inode);
903 if (!task)
904 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700906 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (end - buffer == 0)
908 return -EIO;
909 return end - buffer;
910}
911
912static struct file_operations proc_oom_adjust_operations = {
913 .read = oom_adjust_read,
914 .write = oom_adjust_write,
915};
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917#ifdef CONFIG_AUDITSYSCALL
918#define TMPBUFLEN 21
919static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
920 size_t count, loff_t *ppos)
921{
922 struct inode * inode = file->f_dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700923 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 ssize_t length;
925 char tmpbuf[TMPBUFLEN];
926
Eric W. Biederman99f89552006-06-26 00:25:55 -0700927 if (!task)
928 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
930 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700931 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
933}
934
935static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
936 size_t count, loff_t *ppos)
937{
938 struct inode * inode = file->f_dentry->d_inode;
939 char *page, *tmp;
940 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 uid_t loginuid;
942
943 if (!capable(CAP_AUDIT_CONTROL))
944 return -EPERM;
945
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700946 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return -EPERM;
948
Al Viroe0182902006-05-18 08:28:02 -0400949 if (count >= PAGE_SIZE)
950 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (*ppos != 0) {
953 /* No partial writes. */
954 return -EINVAL;
955 }
956 page = (char*)__get_free_page(GFP_USER);
957 if (!page)
958 return -ENOMEM;
959 length = -EFAULT;
960 if (copy_from_user(page, buf, count))
961 goto out_free_page;
962
Al Viroe0182902006-05-18 08:28:02 -0400963 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 loginuid = simple_strtoul(page, &tmp, 10);
965 if (tmp == page) {
966 length = -EINVAL;
967 goto out_free_page;
968
969 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700970 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (likely(length == 0))
972 length = count;
973
974out_free_page:
975 free_page((unsigned long) page);
976 return length;
977}
978
979static struct file_operations proc_loginuid_operations = {
980 .read = proc_loginuid_read,
981 .write = proc_loginuid_write,
982};
983#endif
984
985#ifdef CONFIG_SECCOMP
986static ssize_t seccomp_read(struct file *file, char __user *buf,
987 size_t count, loff_t *ppos)
988{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700989 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 char __buf[20];
991 loff_t __ppos = *ppos;
992 size_t len;
993
Eric W. Biederman99f89552006-06-26 00:25:55 -0700994 if (!tsk)
995 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /* no need to print the trailing zero, so use only len */
997 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700998 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (__ppos >= len)
1000 return 0;
1001 if (count > len - __ppos)
1002 count = len - __ppos;
1003 if (copy_to_user(buf, __buf + __ppos, count))
1004 return -EFAULT;
1005 *ppos = __ppos + count;
1006 return count;
1007}
1008
1009static ssize_t seccomp_write(struct file *file, const char __user *buf,
1010 size_t count, loff_t *ppos)
1011{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001012 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 char __buf[20], *end;
1014 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001015 ssize_t result;
1016
1017 result = -ESRCH;
1018 if (!tsk)
1019 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -07001022 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -07001024 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Eric W. Biederman99f89552006-06-26 00:25:55 -07001026 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 memset(__buf, 0, sizeof(__buf));
1028 count = min(count, sizeof(__buf) - 1);
1029 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -07001030 goto out;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 seccomp_mode = simple_strtoul(__buf, &end, 0);
1033 if (*end == '\n')
1034 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001035 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1037 tsk->seccomp.mode = seccomp_mode;
1038 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1039 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -07001040 goto out;
1041 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -07001043 goto out;
1044 result = end - __buf;
1045out:
1046 put_task_struct(tsk);
1047out_no_task:
1048 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051static struct file_operations proc_seccomp_operations = {
1052 .read = seccomp_read,
1053 .write = seccomp_write,
1054};
1055#endif /* CONFIG_SECCOMP */
1056
Al Viro008b1502005-08-20 00:17:39 +01001057static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 struct inode *inode = dentry->d_inode;
1060 int error = -EACCES;
1061
1062 /* We don't need a base pointer in the /proc filesystem */
1063 path_release(nd);
1064
Eric W. Biederman778c1142006-06-26 00:25:58 -07001065 /* Are we allowed to snoop on the tasks file descriptors? */
1066 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1070 nd->last_type = LAST_BIND;
1071out:
Al Viro008b1502005-08-20 00:17:39 +01001072 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1076 char __user *buffer, int buflen)
1077{
1078 struct inode * inode;
1079 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1080 int len;
1081
1082 if (!tmp)
1083 return -ENOMEM;
1084
1085 inode = dentry->d_inode;
1086 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1087 len = PTR_ERR(path);
1088 if (IS_ERR(path))
1089 goto out;
1090 len = tmp + PAGE_SIZE - 1 - path;
1091
1092 if (len > buflen)
1093 len = buflen;
1094 if (copy_to_user(buffer, path, len))
1095 len = -EFAULT;
1096 out:
1097 free_page((unsigned long)tmp);
1098 return len;
1099}
1100
1101static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1102{
1103 int error = -EACCES;
1104 struct inode *inode = dentry->d_inode;
1105 struct dentry *de;
1106 struct vfsmount *mnt = NULL;
1107
Eric W. Biederman778c1142006-06-26 00:25:58 -07001108 /* Are we allowed to snoop on the tasks file descriptors? */
1109 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1113 if (error)
1114 goto out;
1115
1116 error = do_proc_readlink(de, mnt, buffer, buflen);
1117 dput(de);
1118 mntput(mnt);
1119out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return error;
1121}
1122
1123static struct inode_operations proc_pid_link_inode_operations = {
1124 .readlink = proc_pid_readlink,
1125 .follow_link = proc_pid_follow_link
1126};
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1129{
Eric W. Biederman56347082006-06-26 00:25:40 -07001130 struct dentry *dentry = filp->f_dentry;
1131 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001132 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 unsigned int fd, tid, ino;
1134 int retval;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001135 char buf[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001137 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001140 if (!p)
1141 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 retval = 0;
1143 tid = p->pid;
1144
1145 fd = filp->f_pos;
1146 switch (fd) {
1147 case 0:
1148 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1149 goto out;
1150 filp->f_pos++;
1151 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001152 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1154 goto out;
1155 filp->f_pos++;
1156 default:
1157 files = get_files_struct(p);
1158 if (!files)
1159 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001160 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001161 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001163 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 fd++, filp->f_pos++) {
1165 unsigned int i,j;
1166
1167 if (!fcheck_files(files, fd))
1168 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001169 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001171 j = PROC_NUMBUF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 i = fd;
1173 do {
1174 j--;
1175 buf[j] = '0' + (i % 10);
1176 i /= 10;
1177 } while (i);
1178
1179 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001180 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001181 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
1183 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001184 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001186 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 put_files_struct(files);
1188 }
1189out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001190 put_task_struct(p);
1191out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return retval;
1193}
1194
1195static int proc_pident_readdir(struct file *filp,
1196 void *dirent, filldir_t filldir,
1197 struct pid_entry *ents, unsigned int nents)
1198{
1199 int i;
1200 int pid;
1201 struct dentry *dentry = filp->f_dentry;
1202 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001203 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct pid_entry *p;
1205 ino_t ino;
1206 int ret;
1207
1208 ret = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001209 if (!task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto out;
1211
1212 ret = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001213 pid = task->pid;
1214 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 i = filp->f_pos;
1216 switch (i) {
1217 case 0:
1218 ino = inode->i_ino;
1219 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1220 goto out;
1221 i++;
1222 filp->f_pos++;
1223 /* fall through */
1224 case 1:
1225 ino = parent_ino(dentry);
1226 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1227 goto out;
1228 i++;
1229 filp->f_pos++;
1230 /* fall through */
1231 default:
1232 i -= 2;
1233 if (i >= nents) {
1234 ret = 1;
1235 goto out;
1236 }
1237 p = ents + i;
1238 while (p->name) {
1239 if (filldir(dirent, p->name, p->len, filp->f_pos,
1240 fake_ino(pid, p->type), p->mode >> 12) < 0)
1241 goto out;
1242 filp->f_pos++;
1243 p++;
1244 }
1245 }
1246
1247 ret = 1;
1248out:
1249 return ret;
1250}
1251
1252static int proc_tgid_base_readdir(struct file * filp,
1253 void * dirent, filldir_t filldir)
1254{
1255 return proc_pident_readdir(filp,dirent,filldir,
1256 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1257}
1258
1259static int proc_tid_base_readdir(struct file * filp,
1260 void * dirent, filldir_t filldir)
1261{
1262 return proc_pident_readdir(filp,dirent,filldir,
1263 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1264}
1265
1266/* building an inode */
1267
1268static int task_dumpable(struct task_struct *task)
1269{
1270 int dumpable = 0;
1271 struct mm_struct *mm;
1272
1273 task_lock(task);
1274 mm = task->mm;
1275 if (mm)
1276 dumpable = mm->dumpable;
1277 task_unlock(task);
Alan Coxd6e71142005-06-23 00:09:43 -07001278 if(dumpable == 1)
1279 return 1;
1280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283
1284static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1285{
1286 struct inode * inode;
1287 struct proc_inode *ei;
1288
1289 /* We need a new inode */
1290
1291 inode = new_inode(sb);
1292 if (!inode)
1293 goto out;
1294
1295 /* Common stuff */
1296 ei = PROC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1298 inode->i_ino = fake_ino(task->pid, ino);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 /*
1301 * grab the reference to task.
1302 */
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001303 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1304 if (!ei->pid)
Eric W. Biederman99f89552006-06-26 00:25:55 -07001305 goto out_unlock;
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 inode->i_uid = 0;
1308 inode->i_gid = 0;
Eric W. Biederman87bfbf62006-06-26 00:25:43 -07001309 if (task_dumpable(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 inode->i_uid = task->euid;
1311 inode->i_gid = task->egid;
1312 }
1313 security_task_to_inode(task, inode);
1314
1315out:
1316 return inode;
1317
1318out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 iput(inode);
1320 return NULL;
1321}
1322
1323/* dentry stuff */
1324
1325/*
1326 * Exceptional case: normally we are not allowed to unhash a busy
1327 * directory. In this case, however, we can do it - no aliasing problems
1328 * due to the way we treat inodes.
1329 *
1330 * Rewrite the inode's ownerships here because the owning task may have
1331 * performed a setuid(), etc.
Eric W. Biederman99f89552006-06-26 00:25:55 -07001332 *
1333 * Before the /proc/pid/status file was created the only way to read
1334 * the effective uid of a /process was to stat /proc/pid. Reading
1335 * /proc/pid/status is slow enough that procps and other packages
1336 * kept stating /proc/pid. To keep the rules in /proc simple I have
1337 * made this apply to all per process world readable and executable
1338 * directories.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
1340static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1341{
1342 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001343 struct task_struct *task = get_proc_task(inode);
1344 if (task) {
1345 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1346 task_dumpable(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 inode->i_uid = task->euid;
1348 inode->i_gid = task->egid;
1349 } else {
1350 inode->i_uid = 0;
1351 inode->i_gid = 0;
1352 }
1353 security_task_to_inode(task, inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001354 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return 1;
1356 }
1357 d_drop(dentry);
1358 return 0;
1359}
1360
Eric W. Biederman99f89552006-06-26 00:25:55 -07001361static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1362{
1363 struct inode *inode = dentry->d_inode;
1364 struct task_struct *task;
1365 generic_fillattr(inode, stat);
1366
1367 rcu_read_lock();
1368 stat->uid = 0;
1369 stat->gid = 0;
1370 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1371 if (task) {
1372 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1373 task_dumpable(task)) {
1374 stat->uid = task->euid;
1375 stat->gid = task->egid;
1376 }
1377 }
1378 rcu_read_unlock();
1379 return 0;
1380}
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1383{
1384 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001385 struct task_struct *task = get_proc_task(inode);
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -07001386 int fd = proc_fd(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct files_struct *files;
1388
Eric W. Biederman99f89552006-06-26 00:25:55 -07001389 if (task) {
1390 files = get_files_struct(task);
1391 if (files) {
1392 rcu_read_lock();
1393 if (fcheck_files(files, fd)) {
1394 rcu_read_unlock();
1395 put_files_struct(files);
1396 if (task_dumpable(task)) {
1397 inode->i_uid = task->euid;
1398 inode->i_gid = task->egid;
1399 } else {
1400 inode->i_uid = 0;
1401 inode->i_gid = 0;
1402 }
1403 security_task_to_inode(task, inode);
1404 put_task_struct(task);
1405 return 1;
1406 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 put_files_struct(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001410 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 d_drop(dentry);
1413 return 0;
1414}
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416static int pid_delete_dentry(struct dentry * dentry)
1417{
1418 /* Is the task we represent dead?
1419 * If so, then don't put the dentry on the lru list,
1420 * kill it immediately.
1421 */
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001422 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425static struct dentry_operations tid_fd_dentry_operations =
1426{
1427 .d_revalidate = tid_fd_revalidate,
1428 .d_delete = pid_delete_dentry,
1429};
1430
1431static struct dentry_operations pid_dentry_operations =
1432{
1433 .d_revalidate = pid_revalidate,
1434 .d_delete = pid_delete_dentry,
1435};
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/* Lookups */
1438
1439static unsigned name_to_int(struct dentry *dentry)
1440{
1441 const char *name = dentry->d_name.name;
1442 int len = dentry->d_name.len;
1443 unsigned n = 0;
1444
1445 if (len > 1 && *name == '0')
1446 goto out;
1447 while (len-- > 0) {
1448 unsigned c = *name++ - '0';
1449 if (c > 9)
1450 goto out;
1451 if (n >= (~0U-9)/10)
1452 goto out;
1453 n *= 10;
1454 n += c;
1455 }
1456 return n;
1457out:
1458 return ~0U;
1459}
1460
1461/* SMP-safe */
1462static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1463{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001464 struct task_struct *task = get_proc_task(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 unsigned fd = name_to_int(dentry);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001466 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 struct file * file;
1468 struct files_struct * files;
1469 struct inode *inode;
1470 struct proc_inode *ei;
1471
Eric W. Biederman99f89552006-06-26 00:25:55 -07001472 if (!task)
1473 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (fd == ~0U)
1475 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1478 if (!inode)
1479 goto out;
1480 ei = PROC_I(inode);
Eric W. Biedermanaed7a6c2006-06-26 00:25:44 -07001481 ei->fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 files = get_files_struct(task);
1483 if (!files)
1484 goto out_unlock;
1485 inode->i_mode = S_IFLNK;
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07001486
1487 /*
1488 * We are not taking a ref to the file structure, so we must
1489 * hold ->file_lock.
1490 */
1491 spin_lock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 file = fcheck_files(files, fd);
1493 if (!file)
1494 goto out_unlock2;
1495 if (file->f_mode & 1)
1496 inode->i_mode |= S_IRUSR | S_IXUSR;
1497 if (file->f_mode & 2)
1498 inode->i_mode |= S_IWUSR | S_IXUSR;
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07001499 spin_unlock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 put_files_struct(files);
1501 inode->i_op = &proc_pid_link_inode_operations;
1502 inode->i_size = 64;
1503 ei->op.proc_get_link = proc_fd_link;
1504 dentry->d_op = &tid_fd_dentry_operations;
1505 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001506 /* Close the race of the process dying before we return the dentry */
1507 if (tid_fd_revalidate(dentry, NULL))
1508 result = NULL;
1509out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001510 put_task_struct(task);
1511out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001512 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514out_unlock2:
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07001515 spin_unlock(&files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 put_files_struct(files);
1517out_unlock:
1518 iput(inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1523static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07001524static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526static struct file_operations proc_fd_operations = {
1527 .read = generic_read_dir,
1528 .readdir = proc_readfd,
1529};
1530
1531static struct file_operations proc_task_operations = {
1532 .read = generic_read_dir,
1533 .readdir = proc_task_readdir,
1534};
1535
1536/*
1537 * proc directories can do almost nothing..
1538 */
1539static struct inode_operations proc_fd_inode_operations = {
1540 .lookup = proc_lookupfd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541};
1542
1543static struct inode_operations proc_task_inode_operations = {
1544 .lookup = proc_task_lookup,
Eric W. Biederman6e66b522006-06-26 00:25:47 -07001545 .getattr = proc_task_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546};
1547
1548#ifdef CONFIG_SECURITY
1549static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1550 size_t count, loff_t *ppos)
1551{
1552 struct inode * inode = file->f_dentry->d_inode;
1553 unsigned long page;
1554 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001555 struct task_struct *task = get_proc_task(inode);
1556
1557 length = -ESRCH;
1558 if (!task)
1559 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (count > PAGE_SIZE)
1562 count = PAGE_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001563 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -07001565 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 length = security_getprocattr(task,
1568 (char*)file->f_dentry->d_name.name,
1569 (void*)page, count);
1570 if (length >= 0)
1571 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1572 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001573out:
1574 put_task_struct(task);
1575out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return length;
1577}
1578
1579static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1580 size_t count, loff_t *ppos)
1581{
1582 struct inode * inode = file->f_dentry->d_inode;
1583 char *page;
1584 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001585 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Eric W. Biederman99f89552006-06-26 00:25:55 -07001587 length = -ESRCH;
1588 if (!task)
1589 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (count > PAGE_SIZE)
1591 count = PAGE_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001592
1593 /* No partial writes. */
1594 length = -EINVAL;
1595 if (*ppos != 0)
1596 goto out;
1597
1598 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 page = (char*)__get_free_page(GFP_USER);
1600 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -07001601 goto out;
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 length = -EFAULT;
1604 if (copy_from_user(page, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -07001605 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 length = security_setprocattr(task,
1608 (char*)file->f_dentry->d_name.name,
1609 (void*)page, count);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001610out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001612out:
1613 put_task_struct(task);
1614out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return length;
1616}
1617
1618static struct file_operations proc_pid_attr_operations = {
1619 .read = proc_pid_attr_read,
1620 .write = proc_pid_attr_write,
1621};
1622
1623static struct file_operations proc_tid_attr_operations;
1624static struct inode_operations proc_tid_attr_inode_operations;
1625static struct file_operations proc_tgid_attr_operations;
1626static struct inode_operations proc_tgid_attr_inode_operations;
1627#endif
1628
1629/* SMP-safe */
1630static struct dentry *proc_pident_lookup(struct inode *dir,
1631 struct dentry *dentry,
1632 struct pid_entry *ents)
1633{
1634 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001635 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001636 struct task_struct *task = get_proc_task(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 struct pid_entry *p;
1638 struct proc_inode *ei;
1639
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001640 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 inode = NULL;
1642
Eric W. Biederman99f89552006-06-26 00:25:55 -07001643 if (!task)
1644 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 for (p = ents; p->name; p++) {
1647 if (p->len != dentry->d_name.len)
1648 continue;
1649 if (!memcmp(dentry->d_name.name, p->name, p->len))
1650 break;
1651 }
1652 if (!p->name)
1653 goto out;
1654
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001655 error = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1657 if (!inode)
1658 goto out;
1659
1660 ei = PROC_I(inode);
1661 inode->i_mode = p->mode;
1662 /*
1663 * Yes, it does not scale. And it should not. Don't add
1664 * new entries into /proc/<tgid>/ without very good reasons.
1665 */
1666 switch(p->type) {
1667 case PROC_TGID_TASK:
Eric W. Biederman6e66b522006-06-26 00:25:47 -07001668 inode->i_nlink = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 inode->i_op = &proc_task_inode_operations;
1670 inode->i_fop = &proc_task_operations;
1671 break;
1672 case PROC_TID_FD:
1673 case PROC_TGID_FD:
1674 inode->i_nlink = 2;
1675 inode->i_op = &proc_fd_inode_operations;
1676 inode->i_fop = &proc_fd_operations;
1677 break;
1678 case PROC_TID_EXE:
1679 case PROC_TGID_EXE:
1680 inode->i_op = &proc_pid_link_inode_operations;
1681 ei->op.proc_get_link = proc_exe_link;
1682 break;
1683 case PROC_TID_CWD:
1684 case PROC_TGID_CWD:
1685 inode->i_op = &proc_pid_link_inode_operations;
1686 ei->op.proc_get_link = proc_cwd_link;
1687 break;
1688 case PROC_TID_ROOT:
1689 case PROC_TGID_ROOT:
1690 inode->i_op = &proc_pid_link_inode_operations;
1691 ei->op.proc_get_link = proc_root_link;
1692 break;
1693 case PROC_TID_ENVIRON:
1694 case PROC_TGID_ENVIRON:
1695 inode->i_fop = &proc_info_file_operations;
1696 ei->op.proc_read = proc_pid_environ;
1697 break;
1698 case PROC_TID_AUXV:
1699 case PROC_TGID_AUXV:
1700 inode->i_fop = &proc_info_file_operations;
1701 ei->op.proc_read = proc_pid_auxv;
1702 break;
1703 case PROC_TID_STATUS:
1704 case PROC_TGID_STATUS:
1705 inode->i_fop = &proc_info_file_operations;
1706 ei->op.proc_read = proc_pid_status;
1707 break;
1708 case PROC_TID_STAT:
1709 inode->i_fop = &proc_info_file_operations;
1710 ei->op.proc_read = proc_tid_stat;
1711 break;
1712 case PROC_TGID_STAT:
1713 inode->i_fop = &proc_info_file_operations;
1714 ei->op.proc_read = proc_tgid_stat;
1715 break;
1716 case PROC_TID_CMDLINE:
1717 case PROC_TGID_CMDLINE:
1718 inode->i_fop = &proc_info_file_operations;
1719 ei->op.proc_read = proc_pid_cmdline;
1720 break;
1721 case PROC_TID_STATM:
1722 case PROC_TGID_STATM:
1723 inode->i_fop = &proc_info_file_operations;
1724 ei->op.proc_read = proc_pid_statm;
1725 break;
1726 case PROC_TID_MAPS:
1727 case PROC_TGID_MAPS:
1728 inode->i_fop = &proc_maps_operations;
1729 break;
Christoph Lameter6e21c8f2005-09-03 15:54:45 -07001730#ifdef CONFIG_NUMA
1731 case PROC_TID_NUMA_MAPS:
1732 case PROC_TGID_NUMA_MAPS:
1733 inode->i_fop = &proc_numa_maps_operations;
1734 break;
1735#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 case PROC_TID_MEM:
1737 case PROC_TGID_MEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 inode->i_fop = &proc_mem_operations;
1739 break;
1740#ifdef CONFIG_SECCOMP
1741 case PROC_TID_SECCOMP:
1742 case PROC_TGID_SECCOMP:
1743 inode->i_fop = &proc_seccomp_operations;
1744 break;
1745#endif /* CONFIG_SECCOMP */
1746 case PROC_TID_MOUNTS:
1747 case PROC_TGID_MOUNTS:
1748 inode->i_fop = &proc_mounts_operations;
1749 break;
Yoshinori Sato63c67642005-10-14 15:59:11 -07001750#ifdef CONFIG_MMU
Mauricio Line070ad42005-09-03 15:55:10 -07001751 case PROC_TID_SMAPS:
1752 case PROC_TGID_SMAPS:
1753 inode->i_fop = &proc_smaps_operations;
1754 break;
Yoshinori Sato63c67642005-10-14 15:59:11 -07001755#endif
Chuck Leverb4629fe2006-03-20 13:44:12 -05001756 case PROC_TID_MOUNTSTATS:
1757 case PROC_TGID_MOUNTSTATS:
1758 inode->i_fop = &proc_mountstats_operations;
1759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760#ifdef CONFIG_SECURITY
1761 case PROC_TID_ATTR:
1762 inode->i_nlink = 2;
1763 inode->i_op = &proc_tid_attr_inode_operations;
1764 inode->i_fop = &proc_tid_attr_operations;
1765 break;
1766 case PROC_TGID_ATTR:
1767 inode->i_nlink = 2;
1768 inode->i_op = &proc_tgid_attr_inode_operations;
1769 inode->i_fop = &proc_tgid_attr_operations;
1770 break;
1771 case PROC_TID_ATTR_CURRENT:
1772 case PROC_TGID_ATTR_CURRENT:
1773 case PROC_TID_ATTR_PREV:
1774 case PROC_TGID_ATTR_PREV:
1775 case PROC_TID_ATTR_EXEC:
1776 case PROC_TGID_ATTR_EXEC:
1777 case PROC_TID_ATTR_FSCREATE:
1778 case PROC_TGID_ATTR_FSCREATE:
Michael LeMay4eb582c2006-06-26 00:24:57 -07001779 case PROC_TID_ATTR_KEYCREATE:
1780 case PROC_TGID_ATTR_KEYCREATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 inode->i_fop = &proc_pid_attr_operations;
1782 break;
1783#endif
1784#ifdef CONFIG_KALLSYMS
1785 case PROC_TID_WCHAN:
1786 case PROC_TGID_WCHAN:
1787 inode->i_fop = &proc_info_file_operations;
1788 ei->op.proc_read = proc_pid_wchan;
1789 break;
1790#endif
1791#ifdef CONFIG_SCHEDSTATS
1792 case PROC_TID_SCHEDSTAT:
1793 case PROC_TGID_SCHEDSTAT:
1794 inode->i_fop = &proc_info_file_operations;
1795 ei->op.proc_read = proc_pid_schedstat;
1796 break;
1797#endif
1798#ifdef CONFIG_CPUSETS
1799 case PROC_TID_CPUSET:
1800 case PROC_TGID_CPUSET:
1801 inode->i_fop = &proc_cpuset_operations;
1802 break;
1803#endif
1804 case PROC_TID_OOM_SCORE:
1805 case PROC_TGID_OOM_SCORE:
1806 inode->i_fop = &proc_info_file_operations;
1807 ei->op.proc_read = proc_oom_score;
1808 break;
1809 case PROC_TID_OOM_ADJUST:
1810 case PROC_TGID_OOM_ADJUST:
1811 inode->i_fop = &proc_oom_adjust_operations;
1812 break;
1813#ifdef CONFIG_AUDITSYSCALL
1814 case PROC_TID_LOGINUID:
1815 case PROC_TGID_LOGINUID:
1816 inode->i_fop = &proc_loginuid_operations;
1817 break;
1818#endif
1819 default:
1820 printk("procfs: impossible type (%d)",p->type);
1821 iput(inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001822 error = ERR_PTR(-EINVAL);
1823 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825 dentry->d_op = &pid_dentry_operations;
1826 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001827 /* Close the race of the process dying before we return the dentry */
1828 if (pid_revalidate(dentry, NULL))
1829 error = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001831 put_task_struct(task);
1832out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001833 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
1836static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1837 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1838}
1839
1840static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1841 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1842}
1843
1844static struct file_operations proc_tgid_base_operations = {
1845 .read = generic_read_dir,
1846 .readdir = proc_tgid_base_readdir,
1847};
1848
1849static struct file_operations proc_tid_base_operations = {
1850 .read = generic_read_dir,
1851 .readdir = proc_tid_base_readdir,
1852};
1853
1854static struct inode_operations proc_tgid_base_inode_operations = {
1855 .lookup = proc_tgid_base_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001856 .getattr = pid_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857};
1858
1859static struct inode_operations proc_tid_base_inode_operations = {
1860 .lookup = proc_tid_base_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001861 .getattr = pid_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862};
1863
1864#ifdef CONFIG_SECURITY
1865static int proc_tgid_attr_readdir(struct file * filp,
1866 void * dirent, filldir_t filldir)
1867{
1868 return proc_pident_readdir(filp,dirent,filldir,
1869 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1870}
1871
1872static int proc_tid_attr_readdir(struct file * filp,
1873 void * dirent, filldir_t filldir)
1874{
1875 return proc_pident_readdir(filp,dirent,filldir,
1876 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1877}
1878
1879static struct file_operations proc_tgid_attr_operations = {
1880 .read = generic_read_dir,
1881 .readdir = proc_tgid_attr_readdir,
1882};
1883
1884static struct file_operations proc_tid_attr_operations = {
1885 .read = generic_read_dir,
1886 .readdir = proc_tid_attr_readdir,
1887};
1888
1889static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1890 struct dentry *dentry, struct nameidata *nd)
1891{
1892 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1893}
1894
1895static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1896 struct dentry *dentry, struct nameidata *nd)
1897{
1898 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1899}
1900
1901static struct inode_operations proc_tgid_attr_inode_operations = {
1902 .lookup = proc_tgid_attr_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001903 .getattr = pid_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904};
1905
1906static struct inode_operations proc_tid_attr_inode_operations = {
1907 .lookup = proc_tid_attr_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001908 .getattr = pid_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909};
1910#endif
1911
1912/*
1913 * /proc/self:
1914 */
1915static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1916 int buflen)
1917{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001918 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 sprintf(tmp, "%d", current->tgid);
1920 return vfs_readlink(dentry,buffer,buflen,tmp);
1921}
1922
Al Viro008b1502005-08-20 00:17:39 +01001923static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001925 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001927 return ERR_PTR(vfs_follow_link(nd,tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
1930static struct inode_operations proc_self_inode_operations = {
1931 .readlink = proc_self_readlink,
1932 .follow_link = proc_self_follow_link,
1933};
1934
1935/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001936 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001938 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001940 * Looks in the dcache for
1941 * /proc/@pid
1942 * /proc/@tgid/task/@pid
1943 * if either directory is present flushes it and all of it'ts children
1944 * from the dcache.
1945 *
1946 * It is safe and reasonable to cache /proc entries for a task until
1947 * that task exits. After that they just clog up the dcache with
1948 * useless entries, possibly causing useful dcache entries to be
1949 * flushed instead. This routine is proved to flush those useless
1950 * dcache entries at process exit time.
1951 *
1952 * NOTE: This routine is just an optimization so it does not guarantee
1953 * that no dcache entries will exist at process exit time it
1954 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001956void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001958 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001959 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001960 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Eric W. Biederman48e64842006-06-26 00:25:48 -07001962 name.name = buf;
1963 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1964 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1965 if (dentry) {
1966 shrink_dcache_parent(dentry);
1967 d_drop(dentry);
1968 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Eric W. Biederman48e64842006-06-26 00:25:48 -07001971 if (thread_group_leader(task))
1972 goto out;
1973
1974 name.name = buf;
1975 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1976 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1977 if (!leader)
1978 goto out;
1979
1980 name.name = "task";
1981 name.len = strlen(name.name);
1982 dir = d_hash_and_lookup(leader, &name);
1983 if (!dir)
1984 goto out_put_leader;
1985
1986 name.name = buf;
1987 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1988 dentry = d_hash_and_lookup(dir, &name);
1989 if (dentry) {
1990 shrink_dcache_parent(dentry);
1991 d_drop(dentry);
1992 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001994
1995 dput(dir);
1996out_put_leader:
1997 dput(leader);
1998out:
1999 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
2002/* SMP-safe */
2003struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2004{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002005 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct task_struct *task;
2007 struct inode *inode;
2008 struct proc_inode *ei;
2009 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2012 inode = new_inode(dir->i_sb);
2013 if (!inode)
2014 return ERR_PTR(-ENOMEM);
2015 ei = PROC_I(inode);
2016 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2017 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2018 ei->pde = NULL;
2019 inode->i_mode = S_IFLNK|S_IRWXUGO;
2020 inode->i_uid = inode->i_gid = 0;
2021 inode->i_size = 64;
2022 inode->i_op = &proc_self_inode_operations;
2023 d_add(dentry, inode);
2024 return NULL;
2025 }
2026 tgid = name_to_int(dentry);
2027 if (tgid == ~0U)
2028 goto out;
2029
Eric W. Biedermande758732006-06-26 00:25:51 -07002030 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 task = find_task_by_pid(tgid);
2032 if (task)
2033 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002034 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (!task)
2036 goto out;
2037
2038 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002039 if (!inode)
2040 goto out_put_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2043 inode->i_op = &proc_tgid_base_inode_operations;
2044 inode->i_fop = &proc_tgid_base_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 inode->i_flags|=S_IMMUTABLE;
Daniel Drakebcf88e12005-05-01 08:59:03 -07002046#ifdef CONFIG_SECURITY
2047 inode->i_nlink = 5;
2048#else
2049 inode->i_nlink = 4;
2050#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Eric W. Biederman48e64842006-06-26 00:25:48 -07002052 dentry->d_op = &pid_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002055 /* Close the race of the process dying before we return the dentry */
2056 if (pid_revalidate(dentry, NULL))
2057 result = NULL;
Eric W. Biederman48e64842006-06-26 00:25:48 -07002058
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002059out_put_task:
Eric W. Biederman48e64842006-06-26 00:25:48 -07002060 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002062 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
2065/* SMP-safe */
2066static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2067{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002068 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct task_struct *task;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002070 struct task_struct *leader = get_proc_task(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 struct inode *inode;
2072 unsigned tid;
2073
Eric W. Biederman99f89552006-06-26 00:25:55 -07002074 if (!leader)
2075 goto out_no_task;
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 tid = name_to_int(dentry);
2078 if (tid == ~0U)
2079 goto out;
2080
Eric W. Biedermande758732006-06-26 00:25:51 -07002081 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 task = find_task_by_pid(tid);
2083 if (task)
2084 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002085 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (!task)
2087 goto out;
2088 if (leader->tgid != task->tgid)
2089 goto out_drop_task;
2090
2091 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2092
2093
2094 if (!inode)
2095 goto out_drop_task;
2096 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2097 inode->i_op = &proc_tid_base_inode_operations;
2098 inode->i_fop = &proc_tid_base_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 inode->i_flags|=S_IMMUTABLE;
Daniel Drakebcf88e12005-05-01 08:59:03 -07002100#ifdef CONFIG_SECURITY
2101 inode->i_nlink = 4;
2102#else
2103 inode->i_nlink = 3;
2104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Eric W. Biederman48e64842006-06-26 00:25:48 -07002106 dentry->d_op = &pid_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 d_add(dentry, inode);
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002109 /* Close the race of the process dying before we return the dentry */
2110 if (pid_revalidate(dentry, NULL))
2111 result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113out_drop_task:
2114 put_task_struct(task);
2115out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002116 put_task_struct(leader);
2117out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002118 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002122 * Find the first tgid to return to user space.
2123 *
2124 * Usually this is just whatever follows &init_task, but if the users
2125 * buffer was too small to hold the full list or there was a seek into
2126 * the middle of the directory we have more work to do.
2127 *
2128 * In the case of a short read we start with find_task_by_pid.
2129 *
2130 * In the case of a seek we start with &init_task and walk nr
2131 * threads past it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 */
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002133static struct task_struct *first_tgid(int tgid, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002135 struct task_struct *pos;
Eric W. Biederman454cc102006-06-26 00:25:51 -07002136 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002137 if (tgid && nr) {
2138 pos = find_task_by_pid(tgid);
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002139 if (pos && thread_group_leader(pos))
2140 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002142 /* If nr exceeds the number of processes get out quickly */
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002143 pos = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002144 if (nr && nr >= nr_processes())
2145 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002147 /* If we haven't found our starting place yet start with
2148 * the init_task and walk nr tasks forward.
2149 */
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002150 for (pos = next_task(&init_task); nr > 0; --nr) {
2151 pos = next_task(pos);
2152 if (pos == &init_task) {
2153 pos = NULL;
2154 goto done;
2155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
Eric W. Biederman9cc8cbc2006-06-26 00:25:52 -07002157found:
2158 get_task_struct(pos);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002159done:
Eric W. Biederman454cc102006-06-26 00:25:51 -07002160 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002161 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
2164/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002165 * Find the next task in the task list.
2166 * Return NULL if we loop or there is any error.
2167 *
2168 * The reference to the input task_struct is released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002170static struct task_struct *next_tgid(struct task_struct *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002172 struct task_struct *pos;
Eric W. Biederman454cc102006-06-26 00:25:51 -07002173 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002174 pos = start;
2175 if (pid_alive(start))
2176 pos = next_task(start);
2177 if (pid_alive(pos) && (pos != &init_task)) {
2178 get_task_struct(pos);
2179 goto done;
2180 }
2181 pos = NULL;
2182done:
Eric W. Biederman454cc102006-06-26 00:25:51 -07002183 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002184 put_task_struct(start);
2185 return pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
2188/* for the /proc/ directory itself, after non-process stuff has been done */
2189int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 char buf[PROC_NUMBUF];
2192 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002193 struct task_struct *task;
2194 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 if (!nr) {
2197 ino_t ino = fake_ino(0,PROC_TGID_INO);
2198 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2199 return 0;
2200 filp->f_pos++;
2201 nr++;
2202 }
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002203 nr -= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 /* f_version caches the tgid value that the last readdir call couldn't
2206 * return. lseek aka telldir automagically resets f_version to 0.
2207 */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002208 tgid = filp->f_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 filp->f_version = 0;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002210 for (task = first_tgid(tgid, nr);
2211 task;
2212 task = next_tgid(task), filp->f_pos++) {
2213 int len;
2214 ino_t ino;
2215 tgid = task->pid;
2216 len = snprintf(buf, sizeof(buf), "%d", tgid);
2217 ino = fake_ino(tgid, PROC_TGID_INO);
2218 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2219 /* returning this tgid failed, save it as the first
2220 * pid for the next readir call */
2221 filp->f_version = tgid;
2222 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 break;
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return 0;
2227}
2228
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002229/*
2230 * Find the first tid of a thread group to return to user space.
2231 *
2232 * Usually this is just the thread group leader, but if the users
2233 * buffer was too small or there was a seek into the middle of the
2234 * directory we have more work todo.
2235 *
2236 * In the case of a short read we start with find_task_by_pid.
2237 *
2238 * In the case of a seek we start with the leader and walk nr
2239 * threads past it.
2240 */
2241static struct task_struct *first_tid(struct task_struct *leader, int tid, int nr)
2242{
2243 struct task_struct *pos = NULL;
2244 read_lock(&tasklist_lock);
2245
2246 /* Attempt to start with the pid of a thread */
2247 if (tid && (nr > 0)) {
2248 pos = find_task_by_pid(tid);
2249 if (pos && (pos->group_leader != leader))
2250 pos = NULL;
2251 if (pos)
2252 nr = 0;
2253 }
2254
2255 /* If nr exceeds the number of threads there is nothing todo */
2256 if (nr) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07002257 if (nr >= get_nr_threads(leader))
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002258 goto done;
2259 }
2260
2261 /* If we haven't found our starting place yet start with the
2262 * leader and walk nr threads forward.
2263 */
2264 if (!pos && (nr >= 0))
2265 pos = leader;
2266
2267 for (; pos && pid_alive(pos); pos = next_thread(pos)) {
2268 if (--nr > 0)
2269 continue;
2270 get_task_struct(pos);
2271 goto done;
2272 }
2273 pos = NULL;
2274done:
2275 read_unlock(&tasklist_lock);
2276 return pos;
2277}
2278
2279/*
2280 * Find the next thread in the thread list.
2281 * Return NULL if there is an error or no next thread.
2282 *
2283 * The reference to the input task_struct is released.
2284 */
2285static struct task_struct *next_tid(struct task_struct *start)
2286{
2287 struct task_struct *pos;
2288 read_lock(&tasklist_lock);
2289 pos = start;
2290 if (pid_alive(start))
2291 pos = next_thread(start);
2292 if (pid_alive(pos) && (pos != start->group_leader))
2293 get_task_struct(pos);
2294 else
2295 pos = NULL;
2296 read_unlock(&tasklist_lock);
2297 put_task_struct(start);
2298 return pos;
2299}
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301/* for the /proc/TGID/task/ directories */
2302static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 char buf[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct dentry *dentry = filp->f_dentry;
2306 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002307 struct task_struct *leader = get_proc_task(inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002308 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 int retval = -ENOENT;
2310 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002311 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2313
Eric W. Biederman99f89552006-06-26 00:25:55 -07002314 if (!leader)
2315 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 retval = 0;
2317
2318 switch (pos) {
2319 case 0:
2320 ino = inode->i_ino;
2321 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2322 goto out;
2323 pos++;
2324 /* fall through */
2325 case 1:
2326 ino = parent_ino(dentry);
2327 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2328 goto out;
2329 pos++;
2330 /* fall through */
2331 }
2332
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002333 /* f_version caches the tgid value that the last readdir call couldn't
2334 * return. lseek aka telldir automagically resets f_version to 0.
2335 */
2336 tid = filp->f_version;
2337 filp->f_version = 0;
2338 for (task = first_tid(leader, tid, pos - 2);
2339 task;
2340 task = next_tid(task), pos++) {
2341 int len;
2342 tid = task->pid;
2343 len = snprintf(buf, sizeof(buf), "%d", tid);
2344 ino = fake_ino(tid, PROC_TID_INO);
2345 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2346 /* returning this tgid failed, save it as the first
2347 * pid for the next readir call */
2348 filp->f_version = tid;
2349 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
2353out:
2354 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002355 put_task_struct(leader);
2356out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return retval;
2358}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002359
2360static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2361{
2362 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002363 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002364 generic_fillattr(inode, stat);
2365
Eric W. Biederman99f89552006-06-26 00:25:55 -07002366 if (p) {
2367 rcu_read_lock();
2368 stat->nlink += get_nr_threads(p);
2369 rcu_read_unlock();
2370 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002371 }
2372
2373 return 0;
2374}