blob: db7636041c107bd787ac281416c013abf0f85f53 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070064#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/kallsyms.h>
Kees Cook5096add2007-05-08 00:26:04 -070066#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/mount.h>
68#include <linux/security.h>
69#include <linux/ptrace.h>
Paul Menagea4243162007-10-18 23:39:35 -070070#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/cpuset.h>
72#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050073#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070074#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070075#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070076#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070077#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include "internal.h"
79
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070080/* NOTE:
81 * Implementing inode permission operations in /proc is almost
82 * certainly an error. Permission checks need to happen during
83 * each system call not at open time. The reason is that most of
84 * what we wish to check for permissions in /proc varies at runtime.
85 *
86 * The classic example of a problem is opening file descriptors
87 * in /proc for a task before it execs a suid executable.
88 */
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Eric W. Biederman8578cea2006-06-26 00:25:54 -070091/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070092#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070096 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080098 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080099 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700100 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Eric W. Biederman61a28782006-10-02 02:18:49 -0700103#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700105 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700106 .mode = MODE, \
107 .iop = IOP, \
108 .fop = FOP, \
109 .op = OP, \
110}
111
Eric W. Biederman61a28782006-10-02 02:18:49 -0700112#define DIR(NAME, MODE, OTYPE) \
113 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700114 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
115 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116#define LNK(NAME, OTYPE) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define REG(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700123#define INF(NAME, MODE, OTYPE) \
124 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700125 NULL, &proc_info_file_operations, \
126 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Kees Cook5096add2007-05-08 00:26:04 -0700128int maps_protect;
129EXPORT_SYMBOL(maps_protect);
130
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700131static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700134 task_lock(task);
135 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if(fs)
137 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700138 task_unlock(task);
139 return fs;
140}
141
Eric W. Biederman99f89552006-06-26 00:25:55 -0700142static int get_nr_threads(struct task_struct *tsk)
143{
144 /* Must be called with the rcu_read_lock held */
145 unsigned long flags;
146 int count = 0;
147
148 if (lock_task_sighand(tsk, &flags)) {
149 count = atomic_read(&tsk->signal->count);
150 unlock_task_sighand(tsk, &flags);
151 }
152 return count;
153}
154
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700155static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
156{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700157 struct task_struct *task = get_proc_task(inode);
158 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700159 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700160
161 if (task) {
162 fs = get_fs_struct(task);
163 put_task_struct(task);
164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (fs) {
166 read_lock(&fs->lock);
167 *mnt = mntget(fs->pwdmnt);
168 *dentry = dget(fs->pwd);
169 read_unlock(&fs->lock);
170 result = 0;
171 put_fs_struct(fs);
172 }
173 return result;
174}
175
176static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
177{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700178 struct task_struct *task = get_proc_task(inode);
179 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700181
182 if (task) {
183 fs = get_fs_struct(task);
184 put_task_struct(task);
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (fs) {
187 read_lock(&fs->lock);
188 *mnt = mntget(fs->rootmnt);
189 *dentry = dget(fs->root);
190 read_unlock(&fs->lock);
191 result = 0;
192 put_fs_struct(fs);
193 }
194 return result;
195}
196
197#define MAY_PTRACE(task) \
198 (task == current || \
199 (task->parent == current && \
200 (task->ptrace & PT_PTRACED) && \
201 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
202 security_ptrace(current,task) == 0))
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static int proc_pid_cmdline(struct task_struct *task, char * buffer)
205{
206 int res = 0;
207 unsigned int len;
208 struct mm_struct *mm = get_task_mm(task);
209 if (!mm)
210 goto out;
211 if (!mm->arg_end)
212 goto out_mm; /* Shh! No looking before we're done */
213
214 len = mm->arg_end - mm->arg_start;
215
216 if (len > PAGE_SIZE)
217 len = PAGE_SIZE;
218
219 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
220
221 // If the nul at the end of args has been overwritten, then
222 // assume application is using setproctitle(3).
223 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
224 len = strnlen(buffer, res);
225 if (len < res) {
226 res = len;
227 } else {
228 len = mm->env_end - mm->env_start;
229 if (len > PAGE_SIZE - res)
230 len = PAGE_SIZE - res;
231 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
232 res = strnlen(buffer, res);
233 }
234 }
235out_mm:
236 mmput(mm);
237out:
238 return res;
239}
240
241static int proc_pid_auxv(struct task_struct *task, char *buffer)
242{
243 int res = 0;
244 struct mm_struct *mm = get_task_mm(task);
245 if (mm) {
246 unsigned int nwords = 0;
247 do
248 nwords += 2;
249 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
250 res = nwords * sizeof(mm->saved_auxv[0]);
251 if (res > PAGE_SIZE)
252 res = PAGE_SIZE;
253 memcpy(buffer, mm->saved_auxv, res);
254 mmput(mm);
255 }
256 return res;
257}
258
259
260#ifdef CONFIG_KALLSYMS
261/*
262 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
263 * Returns the resolved symbol. If that fails, simply return the address.
264 */
265static int proc_pid_wchan(struct task_struct *task, char *buffer)
266{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700267 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700268 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 wchan = get_wchan(task);
271
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700272 if (lookup_symbol_name(wchan, symname) < 0)
273 return sprintf(buffer, "%lu", wchan);
274 else
275 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277#endif /* CONFIG_KALLSYMS */
278
279#ifdef CONFIG_SCHEDSTATS
280/*
281 * Provides /proc/PID/schedstat
282 */
283static int proc_pid_schedstat(struct task_struct *task, char *buffer)
284{
Balbir Singh172ba842007-07-09 18:52:00 +0200285 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 task->sched_info.cpu_time,
287 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200288 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290#endif
291
292/* The badness from the OOM killer */
293unsigned long badness(struct task_struct *p, unsigned long uptime);
294static int proc_oom_score(struct task_struct *task, char *buffer)
295{
296 unsigned long points;
297 struct timespec uptime;
298
299 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700300 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700302 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return sprintf(buffer, "%lu\n", points);
304}
305
306/************************************************************************/
307/* Here the fs part begins */
308/************************************************************************/
309
310/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700311static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700313 struct task_struct *task;
314 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700315 /* Allow access to a task's file descriptors if it is us or we
316 * may use ptrace attach to the process and find out that
317 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700318 */
319 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700320 if (task) {
321 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700322 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700323 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700324 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700327static int proc_setattr(struct dentry *dentry, struct iattr *attr)
328{
329 int error;
330 struct inode *inode = dentry->d_inode;
331
332 if (attr->ia_valid & ATTR_MODE)
333 return -EPERM;
334
335 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700336 if (!error)
337 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700338 return error;
339}
340
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800341static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700342 .setattr = proc_setattr,
343};
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500346struct proc_mounts {
347 struct seq_file m;
348 int event;
349};
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static int mounts_open(struct inode *inode, struct file *file)
352{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700353 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700354 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800355 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500356 struct proc_mounts *p;
357 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Eric W. Biederman99f89552006-06-26 00:25:55 -0700359 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700360 rcu_read_lock();
361 nsp = task_nsproxy(task);
362 if (nsp) {
363 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800364 if (ns)
365 get_mnt_ns(ns);
366 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700367 rcu_read_unlock();
368
Eric W. Biederman99f89552006-06-26 00:25:55 -0700369 put_task_struct(task);
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800372 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500373 ret = -ENOMEM;
374 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
375 if (p) {
376 file->private_data = &p->m;
377 ret = seq_open(file, &mounts_op);
378 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800379 p->m.private = ns;
380 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500381 return 0;
382 }
383 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800385 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 return ret;
388}
389
390static int mounts_release(struct inode *inode, struct file *file)
391{
392 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800393 struct mnt_namespace *ns = m->private;
394 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return seq_release(inode, file);
396}
397
Al Viro5addc5d2005-11-07 17:15:49 -0500398static unsigned mounts_poll(struct file *file, poll_table *wait)
399{
400 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800401 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500402 unsigned res = 0;
403
404 poll_wait(file, &ns->poll, wait);
405
406 spin_lock(&vfsmount_lock);
407 if (p->event != ns->event) {
408 p->event = ns->event;
409 res = POLLERR;
410 }
411 spin_unlock(&vfsmount_lock);
412
413 return res;
414}
415
Arjan van de Ven00977a52007-02-12 00:55:34 -0800416static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 .open = mounts_open,
418 .read = seq_read,
419 .llseek = seq_lseek,
420 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500421 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422};
423
Chuck Leverb4629fe2006-03-20 13:44:12 -0500424extern struct seq_operations mountstats_op;
425static int mountstats_open(struct inode *inode, struct file *file)
426{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500427 int ret = seq_open(file, &mountstats_op);
428
429 if (!ret) {
430 struct seq_file *m = file->private_data;
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700431 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800432 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700433 struct task_struct *task = get_proc_task(inode);
434
435 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700436 rcu_read_lock();
437 nsp = task_nsproxy(task);
438 if (nsp) {
439 mnt_ns = nsp->mnt_ns;
440 if (mnt_ns)
441 get_mnt_ns(mnt_ns);
442 }
443 rcu_read_unlock();
444
Eric W. Biederman99f89552006-06-26 00:25:55 -0700445 put_task_struct(task);
446 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500447
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800448 if (mnt_ns)
449 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500450 else {
451 seq_release(inode, file);
452 ret = -EINVAL;
453 }
454 }
455 return ret;
456}
457
Arjan van de Ven00977a52007-02-12 00:55:34 -0800458static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500459 .open = mountstats_open,
460 .read = seq_read,
461 .llseek = seq_lseek,
462 .release = mounts_release,
463};
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
466
467static ssize_t proc_info_read(struct file * file, char __user * buf,
468 size_t count, loff_t *ppos)
469{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800470 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 unsigned long page;
472 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700473 struct task_struct *task = get_proc_task(inode);
474
475 length = -ESRCH;
476 if (!task)
477 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (count > PROC_BLOCK_SIZE)
480 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700481
482 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700483 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 length = PROC_I(inode)->op.proc_read(task, (char*)page);
487
488 if (length >= 0)
489 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
490 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700491out:
492 put_task_struct(task);
493out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return length;
495}
496
Arjan van de Ven00977a52007-02-12 00:55:34 -0800497static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 .read = proc_info_read,
499};
500
501static int mem_open(struct inode* inode, struct file* file)
502{
503 file->private_data = (void*)((long)current->self_exec_id);
504 return 0;
505}
506
507static ssize_t mem_read(struct file * file, char __user * buf,
508 size_t count, loff_t *ppos)
509{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800510 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 char *page;
512 unsigned long src = *ppos;
513 int ret = -ESRCH;
514 struct mm_struct *mm;
515
Eric W. Biederman99f89552006-06-26 00:25:55 -0700516 if (!task)
517 goto out_no_task;
518
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700519 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 goto out;
521
522 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700523 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!page)
525 goto out;
526
527 ret = 0;
528
529 mm = get_task_mm(task);
530 if (!mm)
531 goto out_free;
532
533 ret = -EIO;
534
535 if (file->private_data != (void*)((long)current->self_exec_id))
536 goto out_put;
537
538 ret = 0;
539
540 while (count > 0) {
541 int this_len, retval;
542
543 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
544 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700545 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!ret)
547 ret = -EIO;
548 break;
549 }
550
551 if (copy_to_user(buf, page, retval)) {
552 ret = -EFAULT;
553 break;
554 }
555
556 ret += retval;
557 src += retval;
558 buf += retval;
559 count -= retval;
560 }
561 *ppos = src;
562
563out_put:
564 mmput(mm);
565out_free:
566 free_page((unsigned long) page);
567out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700568 put_task_struct(task);
569out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return ret;
571}
572
573#define mem_write NULL
574
575#ifndef mem_write
576/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800577static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 size_t count, loff_t *ppos)
579{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700580 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800582 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned long dst = *ppos;
584
Eric W. Biederman99f89552006-06-26 00:25:55 -0700585 copied = -ESRCH;
586 if (!task)
587 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Eric W. Biederman99f89552006-06-26 00:25:55 -0700589 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
590 goto out;
591
592 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700593 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700597 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 while (count > 0) {
599 int this_len, retval;
600
601 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
602 if (copy_from_user(page, buf, this_len)) {
603 copied = -EFAULT;
604 break;
605 }
606 retval = access_process_vm(task, dst, page, this_len, 1);
607 if (!retval) {
608 if (!copied)
609 copied = -EIO;
610 break;
611 }
612 copied += retval;
613 buf += retval;
614 dst += retval;
615 count -= retval;
616 }
617 *ppos = dst;
618 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700619out:
620 put_task_struct(task);
621out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return copied;
623}
624#endif
625
626static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
627{
628 switch (orig) {
629 case 0:
630 file->f_pos = offset;
631 break;
632 case 1:
633 file->f_pos += offset;
634 break;
635 default:
636 return -EINVAL;
637 }
638 force_successful_syscall_return();
639 return file->f_pos;
640}
641
Arjan van de Ven00977a52007-02-12 00:55:34 -0800642static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 .llseek = mem_lseek,
644 .read = mem_read,
645 .write = mem_write,
646 .open = mem_open,
647};
648
James Pearson315e28c2007-10-16 23:30:17 -0700649static ssize_t environ_read(struct file *file, char __user *buf,
650 size_t count, loff_t *ppos)
651{
652 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
653 char *page;
654 unsigned long src = *ppos;
655 int ret = -ESRCH;
656 struct mm_struct *mm;
657
658 if (!task)
659 goto out_no_task;
660
661 if (!ptrace_may_attach(task))
662 goto out;
663
664 ret = -ENOMEM;
665 page = (char *)__get_free_page(GFP_TEMPORARY);
666 if (!page)
667 goto out;
668
669 ret = 0;
670
671 mm = get_task_mm(task);
672 if (!mm)
673 goto out_free;
674
675 while (count > 0) {
676 int this_len, retval, max_len;
677
678 this_len = mm->env_end - (mm->env_start + src);
679
680 if (this_len <= 0)
681 break;
682
683 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
684 this_len = (this_len > max_len) ? max_len : this_len;
685
686 retval = access_process_vm(task, (mm->env_start + src),
687 page, this_len, 0);
688
689 if (retval <= 0) {
690 ret = retval;
691 break;
692 }
693
694 if (copy_to_user(buf, page, retval)) {
695 ret = -EFAULT;
696 break;
697 }
698
699 ret += retval;
700 src += retval;
701 buf += retval;
702 count -= retval;
703 }
704 *ppos = src;
705
706 mmput(mm);
707out_free:
708 free_page((unsigned long) page);
709out:
710 put_task_struct(task);
711out_no_task:
712 return ret;
713}
714
715static const struct file_operations proc_environ_operations = {
716 .read = environ_read,
717};
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719static ssize_t oom_adjust_read(struct file *file, char __user *buf,
720 size_t count, loff_t *ppos)
721{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800722 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700723 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700725 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Eric W. Biederman99f89552006-06-26 00:25:55 -0700727 if (!task)
728 return -ESRCH;
729 oom_adjust = task->oomkilladj;
730 put_task_struct(task);
731
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700732 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700733
734 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
738 size_t count, loff_t *ppos)
739{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700740 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700741 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 int oom_adjust;
743
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700744 memset(buffer, 0, sizeof(buffer));
745 if (count > sizeof(buffer) - 1)
746 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (copy_from_user(buffer, buf, count))
748 return -EFAULT;
749 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700750 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
751 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -EINVAL;
753 if (*end == '\n')
754 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800755 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700756 if (!task)
757 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800758 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
759 put_task_struct(task);
760 return -EACCES;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700763 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (end - buffer == 0)
765 return -EIO;
766 return end - buffer;
767}
768
Arjan van de Ven00977a52007-02-12 00:55:34 -0800769static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 .read = oom_adjust_read,
771 .write = oom_adjust_write,
772};
773
David Rientjes4b8df892007-05-08 20:27:01 -0700774#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -0700775static ssize_t clear_refs_write(struct file *file, const char __user *buf,
776 size_t count, loff_t *ppos)
777{
778 struct task_struct *task;
779 char buffer[PROC_NUMBUF], *end;
780 struct mm_struct *mm;
781
782 memset(buffer, 0, sizeof(buffer));
783 if (count > sizeof(buffer) - 1)
784 count = sizeof(buffer) - 1;
785 if (copy_from_user(buffer, buf, count))
786 return -EFAULT;
787 if (!simple_strtol(buffer, &end, 0))
788 return -EINVAL;
789 if (*end == '\n')
790 end++;
791 task = get_proc_task(file->f_path.dentry->d_inode);
792 if (!task)
793 return -ESRCH;
794 mm = get_task_mm(task);
795 if (mm) {
796 clear_refs_smap(mm);
797 mmput(mm);
798 }
799 put_task_struct(task);
800 if (end - buffer == 0)
801 return -EIO;
802 return end - buffer;
803}
804
805static struct file_operations proc_clear_refs_operations = {
806 .write = clear_refs_write,
807};
David Rientjes4b8df892007-05-08 20:27:01 -0700808#endif
David Rientjesb813e932007-05-06 14:49:24 -0700809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810#ifdef CONFIG_AUDITSYSCALL
811#define TMPBUFLEN 21
812static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
813 size_t count, loff_t *ppos)
814{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800815 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700816 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 ssize_t length;
818 char tmpbuf[TMPBUFLEN];
819
Eric W. Biederman99f89552006-06-26 00:25:55 -0700820 if (!task)
821 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
823 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700824 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
826}
827
828static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
829 size_t count, loff_t *ppos)
830{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800831 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 char *page, *tmp;
833 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 uid_t loginuid;
835
836 if (!capable(CAP_AUDIT_CONTROL))
837 return -EPERM;
838
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700839 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return -EPERM;
841
Al Viroe0182902006-05-18 08:28:02 -0400842 if (count >= PAGE_SIZE)
843 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if (*ppos != 0) {
846 /* No partial writes. */
847 return -EINVAL;
848 }
Mel Gormane12ba742007-10-16 01:25:52 -0700849 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!page)
851 return -ENOMEM;
852 length = -EFAULT;
853 if (copy_from_user(page, buf, count))
854 goto out_free_page;
855
Al Viroe0182902006-05-18 08:28:02 -0400856 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 loginuid = simple_strtoul(page, &tmp, 10);
858 if (tmp == page) {
859 length = -EINVAL;
860 goto out_free_page;
861
862 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700863 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (likely(length == 0))
865 length = count;
866
867out_free_page:
868 free_page((unsigned long) page);
869 return length;
870}
871
Arjan van de Ven00977a52007-02-12 00:55:34 -0800872static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 .read = proc_loginuid_read,
874 .write = proc_loginuid_write,
875};
876#endif
877
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800878#ifdef CONFIG_FAULT_INJECTION
879static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
880 size_t count, loff_t *ppos)
881{
882 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
883 char buffer[PROC_NUMBUF];
884 size_t len;
885 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800886
887 if (!task)
888 return -ESRCH;
889 make_it_fail = task->make_it_fail;
890 put_task_struct(task);
891
892 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700893
894 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800895}
896
897static ssize_t proc_fault_inject_write(struct file * file,
898 const char __user * buf, size_t count, loff_t *ppos)
899{
900 struct task_struct *task;
901 char buffer[PROC_NUMBUF], *end;
902 int make_it_fail;
903
904 if (!capable(CAP_SYS_RESOURCE))
905 return -EPERM;
906 memset(buffer, 0, sizeof(buffer));
907 if (count > sizeof(buffer) - 1)
908 count = sizeof(buffer) - 1;
909 if (copy_from_user(buffer, buf, count))
910 return -EFAULT;
911 make_it_fail = simple_strtol(buffer, &end, 0);
912 if (*end == '\n')
913 end++;
914 task = get_proc_task(file->f_dentry->d_inode);
915 if (!task)
916 return -ESRCH;
917 task->make_it_fail = make_it_fail;
918 put_task_struct(task);
919 if (end - buffer == 0)
920 return -EIO;
921 return end - buffer;
922}
923
Arjan van de Ven00977a52007-02-12 00:55:34 -0800924static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800925 .read = proc_fault_inject_read,
926 .write = proc_fault_inject_write,
927};
928#endif
929
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200930#ifdef CONFIG_SCHED_DEBUG
931/*
932 * Print out various scheduling related per-task fields:
933 */
934static int sched_show(struct seq_file *m, void *v)
935{
936 struct inode *inode = m->private;
937 struct task_struct *p;
938
939 WARN_ON(!inode);
940
941 p = get_proc_task(inode);
942 if (!p)
943 return -ESRCH;
944 proc_sched_show_task(p, m);
945
946 put_task_struct(p);
947
948 return 0;
949}
950
951static ssize_t
952sched_write(struct file *file, const char __user *buf,
953 size_t count, loff_t *offset)
954{
955 struct inode *inode = file->f_path.dentry->d_inode;
956 struct task_struct *p;
957
958 WARN_ON(!inode);
959
960 p = get_proc_task(inode);
961 if (!p)
962 return -ESRCH;
963 proc_sched_set_task(p);
964
965 put_task_struct(p);
966
967 return count;
968}
969
970static int sched_open(struct inode *inode, struct file *filp)
971{
972 int ret;
973
974 ret = single_open(filp, sched_show, NULL);
975 if (!ret) {
976 struct seq_file *m = filp->private_data;
977
978 m->private = inode;
979 }
980 return ret;
981}
982
983static const struct file_operations proc_pid_sched_operations = {
984 .open = sched_open,
985 .read = seq_read,
986 .write = sched_write,
987 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -0700988 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +0200989};
990
991#endif
992
Al Viro008b1502005-08-20 00:17:39 +0100993static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct inode *inode = dentry->d_inode;
996 int error = -EACCES;
997
998 /* We don't need a base pointer in the /proc filesystem */
999 path_release(nd);
1000
Eric W. Biederman778c1142006-06-26 00:25:58 -07001001 /* Are we allowed to snoop on the tasks file descriptors? */
1002 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1006 nd->last_type = LAST_BIND;
1007out:
Al Viro008b1502005-08-20 00:17:39 +01001008 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1012 char __user *buffer, int buflen)
1013{
1014 struct inode * inode;
Mel Gormane12ba742007-10-16 01:25:52 -07001015 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1016 char *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 int len;
1018
1019 if (!tmp)
1020 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 inode = dentry->d_inode;
1023 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1024 len = PTR_ERR(path);
1025 if (IS_ERR(path))
1026 goto out;
1027 len = tmp + PAGE_SIZE - 1 - path;
1028
1029 if (len > buflen)
1030 len = buflen;
1031 if (copy_to_user(buffer, path, len))
1032 len = -EFAULT;
1033 out:
1034 free_page((unsigned long)tmp);
1035 return len;
1036}
1037
1038static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1039{
1040 int error = -EACCES;
1041 struct inode *inode = dentry->d_inode;
1042 struct dentry *de;
1043 struct vfsmount *mnt = NULL;
1044
Eric W. Biederman778c1142006-06-26 00:25:58 -07001045 /* Are we allowed to snoop on the tasks file descriptors? */
1046 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1050 if (error)
1051 goto out;
1052
1053 error = do_proc_readlink(de, mnt, buffer, buflen);
1054 dput(de);
1055 mntput(mnt);
1056out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return error;
1058}
1059
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001060static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001062 .follow_link = proc_pid_follow_link,
1063 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064};
1065
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001066
1067/* building an inode */
1068
1069static int task_dumpable(struct task_struct *task)
1070{
1071 int dumpable = 0;
1072 struct mm_struct *mm;
1073
1074 task_lock(task);
1075 mm = task->mm;
1076 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001077 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001078 task_unlock(task);
1079 if(dumpable == 1)
1080 return 1;
1081 return 0;
1082}
1083
1084
Eric W. Biederman61a28782006-10-02 02:18:49 -07001085static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001086{
1087 struct inode * inode;
1088 struct proc_inode *ei;
1089
1090 /* We need a new inode */
1091
1092 inode = new_inode(sb);
1093 if (!inode)
1094 goto out;
1095
1096 /* Common stuff */
1097 ei = PROC_I(inode);
1098 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001099 inode->i_op = &proc_def_inode_operations;
1100
1101 /*
1102 * grab the reference to task.
1103 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001104 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001105 if (!ei->pid)
1106 goto out_unlock;
1107
1108 inode->i_uid = 0;
1109 inode->i_gid = 0;
1110 if (task_dumpable(task)) {
1111 inode->i_uid = task->euid;
1112 inode->i_gid = task->egid;
1113 }
1114 security_task_to_inode(task, inode);
1115
1116out:
1117 return inode;
1118
1119out_unlock:
1120 iput(inode);
1121 return NULL;
1122}
1123
1124static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1125{
1126 struct inode *inode = dentry->d_inode;
1127 struct task_struct *task;
1128 generic_fillattr(inode, stat);
1129
1130 rcu_read_lock();
1131 stat->uid = 0;
1132 stat->gid = 0;
1133 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1134 if (task) {
1135 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1136 task_dumpable(task)) {
1137 stat->uid = task->euid;
1138 stat->gid = task->egid;
1139 }
1140 }
1141 rcu_read_unlock();
1142 return 0;
1143}
1144
1145/* dentry stuff */
1146
1147/*
1148 * Exceptional case: normally we are not allowed to unhash a busy
1149 * directory. In this case, however, we can do it - no aliasing problems
1150 * due to the way we treat inodes.
1151 *
1152 * Rewrite the inode's ownerships here because the owning task may have
1153 * performed a setuid(), etc.
1154 *
1155 * Before the /proc/pid/status file was created the only way to read
1156 * the effective uid of a /process was to stat /proc/pid. Reading
1157 * /proc/pid/status is slow enough that procps and other packages
1158 * kept stating /proc/pid. To keep the rules in /proc simple I have
1159 * made this apply to all per process world readable and executable
1160 * directories.
1161 */
1162static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1163{
1164 struct inode *inode = dentry->d_inode;
1165 struct task_struct *task = get_proc_task(inode);
1166 if (task) {
1167 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1168 task_dumpable(task)) {
1169 inode->i_uid = task->euid;
1170 inode->i_gid = task->egid;
1171 } else {
1172 inode->i_uid = 0;
1173 inode->i_gid = 0;
1174 }
1175 inode->i_mode &= ~(S_ISUID | S_ISGID);
1176 security_task_to_inode(task, inode);
1177 put_task_struct(task);
1178 return 1;
1179 }
1180 d_drop(dentry);
1181 return 0;
1182}
1183
1184static int pid_delete_dentry(struct dentry * dentry)
1185{
1186 /* Is the task we represent dead?
1187 * If so, then don't put the dentry on the lru list,
1188 * kill it immediately.
1189 */
1190 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1191}
1192
1193static struct dentry_operations pid_dentry_operations =
1194{
1195 .d_revalidate = pid_revalidate,
1196 .d_delete = pid_delete_dentry,
1197};
1198
1199/* Lookups */
1200
Eric Dumazetc5141e62007-05-08 00:26:15 -07001201typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1202 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001203
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001204/*
1205 * Fill a directory entry.
1206 *
1207 * If possible create the dcache entry and derive our inode number and
1208 * file type from dcache entry.
1209 *
1210 * Since all of the proc inode numbers are dynamically generated, the inode
1211 * numbers do not exist until the inode is cache. This means creating the
1212 * the dcache entry in readdir is necessary to keep the inode numbers
1213 * reported by readdir in sync with the inode numbers reported
1214 * by stat.
1215 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001216static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1217 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001218 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001219{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001220 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001221 struct inode *inode;
1222 struct qstr qname;
1223 ino_t ino = 0;
1224 unsigned type = DT_UNKNOWN;
1225
1226 qname.name = name;
1227 qname.len = len;
1228 qname.hash = full_name_hash(name, len);
1229
1230 child = d_lookup(dir, &qname);
1231 if (!child) {
1232 struct dentry *new;
1233 new = d_alloc(dir, &qname);
1234 if (new) {
1235 child = instantiate(dir->d_inode, new, task, ptr);
1236 if (child)
1237 dput(new);
1238 else
1239 child = new;
1240 }
1241 }
1242 if (!child || IS_ERR(child) || !child->d_inode)
1243 goto end_instantiate;
1244 inode = child->d_inode;
1245 if (inode) {
1246 ino = inode->i_ino;
1247 type = inode->i_mode >> 12;
1248 }
1249 dput(child);
1250end_instantiate:
1251 if (!ino)
1252 ino = find_inode_number(dir, &qname);
1253 if (!ino)
1254 ino = 1;
1255 return filldir(dirent, name, len, filp->f_pos, ino, type);
1256}
1257
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001258static unsigned name_to_int(struct dentry *dentry)
1259{
1260 const char *name = dentry->d_name.name;
1261 int len = dentry->d_name.len;
1262 unsigned n = 0;
1263
1264 if (len > 1 && *name == '0')
1265 goto out;
1266 while (len-- > 0) {
1267 unsigned c = *name++ - '0';
1268 if (c > 9)
1269 goto out;
1270 if (n >= (~0U-9)/10)
1271 goto out;
1272 n *= 10;
1273 n += c;
1274 }
1275 return n;
1276out:
1277 return ~0U;
1278}
1279
Miklos Szeredi27932742007-05-08 00:26:17 -07001280#define PROC_FDINFO_MAX 64
1281
1282static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1283 struct vfsmount **mnt, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001284{
1285 struct task_struct *task = get_proc_task(inode);
1286 struct files_struct *files = NULL;
1287 struct file *file;
1288 int fd = proc_fd(inode);
1289
1290 if (task) {
1291 files = get_files_struct(task);
1292 put_task_struct(task);
1293 }
1294 if (files) {
1295 /*
1296 * We are not taking a ref to the file structure, so we must
1297 * hold ->file_lock.
1298 */
1299 spin_lock(&files->file_lock);
1300 file = fcheck_files(files, fd);
1301 if (file) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001302 if (mnt)
1303 *mnt = mntget(file->f_path.mnt);
1304 if (dentry)
1305 *dentry = dget(file->f_path.dentry);
1306 if (info)
1307 snprintf(info, PROC_FDINFO_MAX,
1308 "pos:\t%lli\n"
1309 "flags:\t0%o\n",
1310 (long long) file->f_pos,
1311 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001312 spin_unlock(&files->file_lock);
1313 put_files_struct(files);
1314 return 0;
1315 }
1316 spin_unlock(&files->file_lock);
1317 put_files_struct(files);
1318 }
1319 return -ENOENT;
1320}
1321
Miklos Szeredi27932742007-05-08 00:26:17 -07001322static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1323 struct vfsmount **mnt)
1324{
1325 return proc_fd_info(inode, dentry, mnt, NULL);
1326}
1327
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001328static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1329{
1330 struct inode *inode = dentry->d_inode;
1331 struct task_struct *task = get_proc_task(inode);
1332 int fd = proc_fd(inode);
1333 struct files_struct *files;
1334
1335 if (task) {
1336 files = get_files_struct(task);
1337 if (files) {
1338 rcu_read_lock();
1339 if (fcheck_files(files, fd)) {
1340 rcu_read_unlock();
1341 put_files_struct(files);
1342 if (task_dumpable(task)) {
1343 inode->i_uid = task->euid;
1344 inode->i_gid = task->egid;
1345 } else {
1346 inode->i_uid = 0;
1347 inode->i_gid = 0;
1348 }
1349 inode->i_mode &= ~(S_ISUID | S_ISGID);
1350 security_task_to_inode(task, inode);
1351 put_task_struct(task);
1352 return 1;
1353 }
1354 rcu_read_unlock();
1355 put_files_struct(files);
1356 }
1357 put_task_struct(task);
1358 }
1359 d_drop(dentry);
1360 return 0;
1361}
1362
1363static struct dentry_operations tid_fd_dentry_operations =
1364{
1365 .d_revalidate = tid_fd_revalidate,
1366 .d_delete = pid_delete_dentry,
1367};
1368
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001369static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001370 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001371{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001372 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001373 struct file *file;
1374 struct files_struct *files;
1375 struct inode *inode;
1376 struct proc_inode *ei;
1377 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001378
Eric W. Biederman61a28782006-10-02 02:18:49 -07001379 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001380 if (!inode)
1381 goto out;
1382 ei = PROC_I(inode);
1383 ei->fd = fd;
1384 files = get_files_struct(task);
1385 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001386 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001387 inode->i_mode = S_IFLNK;
1388
1389 /*
1390 * We are not taking a ref to the file structure, so we must
1391 * hold ->file_lock.
1392 */
1393 spin_lock(&files->file_lock);
1394 file = fcheck_files(files, fd);
1395 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001396 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001397 if (file->f_mode & 1)
1398 inode->i_mode |= S_IRUSR | S_IXUSR;
1399 if (file->f_mode & 2)
1400 inode->i_mode |= S_IWUSR | S_IXUSR;
1401 spin_unlock(&files->file_lock);
1402 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001403
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001404 inode->i_op = &proc_pid_link_inode_operations;
1405 inode->i_size = 64;
1406 ei->op.proc_get_link = proc_fd_link;
1407 dentry->d_op = &tid_fd_dentry_operations;
1408 d_add(dentry, inode);
1409 /* Close the race of the process dying before we return the dentry */
1410 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001411 error = NULL;
1412
1413 out:
1414 return error;
1415out_unlock:
1416 spin_unlock(&files->file_lock);
1417 put_files_struct(files);
1418out_iput:
1419 iput(inode);
1420 goto out;
1421}
1422
Miklos Szeredi27932742007-05-08 00:26:17 -07001423static struct dentry *proc_lookupfd_common(struct inode *dir,
1424 struct dentry *dentry,
1425 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001426{
1427 struct task_struct *task = get_proc_task(dir);
1428 unsigned fd = name_to_int(dentry);
1429 struct dentry *result = ERR_PTR(-ENOENT);
1430
1431 if (!task)
1432 goto out_no_task;
1433 if (fd == ~0U)
1434 goto out;
1435
Miklos Szeredi27932742007-05-08 00:26:17 -07001436 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001437out:
1438 put_task_struct(task);
1439out_no_task:
1440 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001441}
1442
Miklos Szeredi27932742007-05-08 00:26:17 -07001443static int proc_readfd_common(struct file * filp, void * dirent,
1444 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001446 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001447 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001448 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 unsigned int fd, tid, ino;
1450 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001452 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001455 if (!p)
1456 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 retval = 0;
1458 tid = p->pid;
1459
1460 fd = filp->f_pos;
1461 switch (fd) {
1462 case 0:
1463 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1464 goto out;
1465 filp->f_pos++;
1466 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001467 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1469 goto out;
1470 filp->f_pos++;
1471 default:
1472 files = get_files_struct(p);
1473 if (!files)
1474 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001475 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001476 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001478 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001480 char name[PROC_NUMBUF];
1481 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (!fcheck_files(files, fd))
1484 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001485 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Miklos Szeredi27932742007-05-08 00:26:17 -07001487 len = snprintf(name, sizeof(name), "%d", fd);
1488 if (proc_fill_cache(filp, dirent, filldir,
1489 name, len, instantiate,
1490 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001491 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 break;
1493 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001494 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001496 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 put_files_struct(files);
1498 }
1499out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001500 put_task_struct(p);
1501out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return retval;
1503}
1504
Miklos Szeredi27932742007-05-08 00:26:17 -07001505static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1506 struct nameidata *nd)
1507{
1508 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1509}
1510
1511static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1512{
1513 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1514}
1515
1516static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1517 size_t len, loff_t *ppos)
1518{
1519 char tmp[PROC_FDINFO_MAX];
1520 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1521 if (!err)
1522 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1523 return err;
1524}
1525
1526static const struct file_operations proc_fdinfo_file_operations = {
1527 .open = nonseekable_open,
1528 .read = proc_fdinfo_read,
1529};
1530
Arjan van de Ven00977a52007-02-12 00:55:34 -08001531static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 .read = generic_read_dir,
1533 .readdir = proc_readfd,
1534};
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001537 * /proc/pid/fd needs a special permission handler so that a process can still
1538 * access /proc/self/fd after it has executed a setuid().
1539 */
1540static int proc_fd_permission(struct inode *inode, int mask,
1541 struct nameidata *nd)
1542{
1543 int rv;
1544
1545 rv = generic_permission(inode, mask, NULL);
1546 if (rv == 0)
1547 return 0;
1548 if (task_pid(current) == proc_pid(inode))
1549 rv = 0;
1550 return rv;
1551}
1552
1553/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 * proc directories can do almost nothing..
1555 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001556static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001558 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001559 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560};
1561
Miklos Szeredi27932742007-05-08 00:26:17 -07001562static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1563 struct dentry *dentry, struct task_struct *task, const void *ptr)
1564{
1565 unsigned fd = *(unsigned *)ptr;
1566 struct inode *inode;
1567 struct proc_inode *ei;
1568 struct dentry *error = ERR_PTR(-ENOENT);
1569
1570 inode = proc_pid_make_inode(dir->i_sb, task);
1571 if (!inode)
1572 goto out;
1573 ei = PROC_I(inode);
1574 ei->fd = fd;
1575 inode->i_mode = S_IFREG | S_IRUSR;
1576 inode->i_fop = &proc_fdinfo_file_operations;
1577 dentry->d_op = &tid_fd_dentry_operations;
1578 d_add(dentry, inode);
1579 /* Close the race of the process dying before we return the dentry */
1580 if (tid_fd_revalidate(dentry, NULL))
1581 error = NULL;
1582
1583 out:
1584 return error;
1585}
1586
1587static struct dentry *proc_lookupfdinfo(struct inode *dir,
1588 struct dentry *dentry,
1589 struct nameidata *nd)
1590{
1591 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1592}
1593
1594static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1595{
1596 return proc_readfd_common(filp, dirent, filldir,
1597 proc_fdinfo_instantiate);
1598}
1599
1600static const struct file_operations proc_fdinfo_operations = {
1601 .read = generic_read_dir,
1602 .readdir = proc_readfdinfo,
1603};
1604
1605/*
1606 * proc directories can do almost nothing..
1607 */
1608static const struct inode_operations proc_fdinfo_inode_operations = {
1609 .lookup = proc_lookupfdinfo,
1610 .setattr = proc_setattr,
1611};
1612
1613
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001614static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001615 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001616{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001617 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001618 struct inode *inode;
1619 struct proc_inode *ei;
1620 struct dentry *error = ERR_PTR(-EINVAL);
1621
Eric W. Biederman61a28782006-10-02 02:18:49 -07001622 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001623 if (!inode)
1624 goto out;
1625
1626 ei = PROC_I(inode);
1627 inode->i_mode = p->mode;
1628 if (S_ISDIR(inode->i_mode))
1629 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1630 if (p->iop)
1631 inode->i_op = p->iop;
1632 if (p->fop)
1633 inode->i_fop = p->fop;
1634 ei->op = p->op;
1635 dentry->d_op = &pid_dentry_operations;
1636 d_add(dentry, inode);
1637 /* Close the race of the process dying before we return the dentry */
1638 if (pid_revalidate(dentry, NULL))
1639 error = NULL;
1640out:
1641 return error;
1642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644static struct dentry *proc_pident_lookup(struct inode *dir,
1645 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001646 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001647 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001650 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001651 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001652 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001654 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 inode = NULL;
1656
Eric W. Biederman99f89552006-06-26 00:25:55 -07001657 if (!task)
1658 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001660 /*
1661 * Yes, it does not scale. And it should not. Don't add
1662 * new entries into /proc/<tgid>/ without very good reasons.
1663 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001664 last = &ents[nents - 1];
1665 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (p->len != dentry->d_name.len)
1667 continue;
1668 if (!memcmp(dentry->d_name.name, p->name, p->len))
1669 break;
1670 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001671 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto out;
1673
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001674 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001676 put_task_struct(task);
1677out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001678 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Eric Dumazetc5141e62007-05-08 00:26:15 -07001681static int proc_pident_fill_cache(struct file *filp, void *dirent,
1682 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001683{
1684 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1685 proc_pident_instantiate, task, p);
1686}
1687
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001688static int proc_pident_readdir(struct file *filp,
1689 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001690 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001691{
1692 int i;
1693 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001694 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001695 struct inode *inode = dentry->d_inode;
1696 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001697 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001698 ino_t ino;
1699 int ret;
1700
1701 ret = -ENOENT;
1702 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001703 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001704
1705 ret = 0;
1706 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001707 i = filp->f_pos;
1708 switch (i) {
1709 case 0:
1710 ino = inode->i_ino;
1711 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1712 goto out;
1713 i++;
1714 filp->f_pos++;
1715 /* fall through */
1716 case 1:
1717 ino = parent_ino(dentry);
1718 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1719 goto out;
1720 i++;
1721 filp->f_pos++;
1722 /* fall through */
1723 default:
1724 i -= 2;
1725 if (i >= nents) {
1726 ret = 1;
1727 goto out;
1728 }
1729 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001730 last = &ents[nents - 1];
1731 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001732 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001733 goto out;
1734 filp->f_pos++;
1735 p++;
1736 }
1737 }
1738
1739 ret = 1;
1740out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001741 put_task_struct(task);
1742out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001743 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001747static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1748 size_t count, loff_t *ppos)
1749{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001750 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001751 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001752 ssize_t length;
1753 struct task_struct *task = get_proc_task(inode);
1754
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001755 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001756 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001757
1758 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001759 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001760 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001762 if (length > 0)
1763 length = simple_read_from_buffer(buf, count, ppos, p, length);
1764 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001765 return length;
1766}
1767
1768static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1769 size_t count, loff_t *ppos)
1770{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001771 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001772 char *page;
1773 ssize_t length;
1774 struct task_struct *task = get_proc_task(inode);
1775
1776 length = -ESRCH;
1777 if (!task)
1778 goto out_no_task;
1779 if (count > PAGE_SIZE)
1780 count = PAGE_SIZE;
1781
1782 /* No partial writes. */
1783 length = -EINVAL;
1784 if (*ppos != 0)
1785 goto out;
1786
1787 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001788 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001789 if (!page)
1790 goto out;
1791
1792 length = -EFAULT;
1793 if (copy_from_user(page, buf, count))
1794 goto out_free;
1795
1796 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001797 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001798 (void*)page, count);
1799out_free:
1800 free_page((unsigned long) page);
1801out:
1802 put_task_struct(task);
1803out_no_task:
1804 return length;
1805}
1806
Arjan van de Ven00977a52007-02-12 00:55:34 -08001807static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001808 .read = proc_pid_attr_read,
1809 .write = proc_pid_attr_write,
1810};
1811
Eric Dumazetc5141e62007-05-08 00:26:15 -07001812static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001813 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1814 REG("prev", S_IRUGO, pid_attr),
1815 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1816 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1817 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1818 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001819};
1820
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001821static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 void * dirent, filldir_t filldir)
1823{
1824 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001825 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
Arjan van de Ven00977a52007-02-12 00:55:34 -08001828static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001830 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831};
1832
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001833static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 struct dentry *dentry, struct nameidata *nd)
1835{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001836 return proc_pident_lookup(dir, dentry,
1837 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001840static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001841 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001842 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001843 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844};
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846#endif
1847
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07001848#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1849static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
1850 size_t count, loff_t *ppos)
1851{
1852 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1853 struct mm_struct *mm;
1854 char buffer[PROC_NUMBUF];
1855 size_t len;
1856 int ret;
1857
1858 if (!task)
1859 return -ESRCH;
1860
1861 ret = 0;
1862 mm = get_task_mm(task);
1863 if (mm) {
1864 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
1865 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
1866 MMF_DUMP_FILTER_SHIFT));
1867 mmput(mm);
1868 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
1869 }
1870
1871 put_task_struct(task);
1872
1873 return ret;
1874}
1875
1876static ssize_t proc_coredump_filter_write(struct file *file,
1877 const char __user *buf,
1878 size_t count,
1879 loff_t *ppos)
1880{
1881 struct task_struct *task;
1882 struct mm_struct *mm;
1883 char buffer[PROC_NUMBUF], *end;
1884 unsigned int val;
1885 int ret;
1886 int i;
1887 unsigned long mask;
1888
1889 ret = -EFAULT;
1890 memset(buffer, 0, sizeof(buffer));
1891 if (count > sizeof(buffer) - 1)
1892 count = sizeof(buffer) - 1;
1893 if (copy_from_user(buffer, buf, count))
1894 goto out_no_task;
1895
1896 ret = -EINVAL;
1897 val = (unsigned int)simple_strtoul(buffer, &end, 0);
1898 if (*end == '\n')
1899 end++;
1900 if (end - buffer == 0)
1901 goto out_no_task;
1902
1903 ret = -ESRCH;
1904 task = get_proc_task(file->f_dentry->d_inode);
1905 if (!task)
1906 goto out_no_task;
1907
1908 ret = end - buffer;
1909 mm = get_task_mm(task);
1910 if (!mm)
1911 goto out_no_mm;
1912
1913 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
1914 if (val & mask)
1915 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1916 else
1917 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1918 }
1919
1920 mmput(mm);
1921 out_no_mm:
1922 put_task_struct(task);
1923 out_no_task:
1924 return ret;
1925}
1926
1927static const struct file_operations proc_coredump_filter_operations = {
1928 .read = proc_coredump_filter_read,
1929 .write = proc_coredump_filter_write,
1930};
1931#endif
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933/*
1934 * /proc/self:
1935 */
1936static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1937 int buflen)
1938{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001939 char tmp[PROC_NUMBUF];
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001940 sprintf(tmp, "%d", task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return vfs_readlink(dentry,buffer,buflen,tmp);
1942}
1943
Al Viro008b1502005-08-20 00:17:39 +01001944static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001946 char tmp[PROC_NUMBUF];
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001947 sprintf(tmp, "%d", task_tgid_vnr(current));
Al Viro008b1502005-08-20 00:17:39 +01001948 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001949}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001951static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 .readlink = proc_self_readlink,
1953 .follow_link = proc_self_follow_link,
1954};
1955
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001956/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001957 * proc base
1958 *
1959 * These are the directory entries in the root directory of /proc
1960 * that properly belong to the /proc filesystem, as they describe
1961 * describe something that is process related.
1962 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07001963static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001964 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001965 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001966};
1967
1968/*
1969 * Exceptional case: normally we are not allowed to unhash a busy
1970 * directory. In this case, however, we can do it - no aliasing problems
1971 * due to the way we treat inodes.
1972 */
1973static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1974{
1975 struct inode *inode = dentry->d_inode;
1976 struct task_struct *task = get_proc_task(inode);
1977 if (task) {
1978 put_task_struct(task);
1979 return 1;
1980 }
1981 d_drop(dentry);
1982 return 0;
1983}
1984
1985static struct dentry_operations proc_base_dentry_operations =
1986{
1987 .d_revalidate = proc_base_revalidate,
1988 .d_delete = pid_delete_dentry,
1989};
1990
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001991static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001992 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001993{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001994 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001995 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001996 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001997 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001998
1999 /* Allocate the inode */
2000 error = ERR_PTR(-ENOMEM);
2001 inode = new_inode(dir->i_sb);
2002 if (!inode)
2003 goto out;
2004
2005 /* Initialize the inode */
2006 ei = PROC_I(inode);
2007 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002008
2009 /*
2010 * grab the reference to the task.
2011 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002012 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002013 if (!ei->pid)
2014 goto out_iput;
2015
2016 inode->i_uid = 0;
2017 inode->i_gid = 0;
2018 inode->i_mode = p->mode;
2019 if (S_ISDIR(inode->i_mode))
2020 inode->i_nlink = 2;
2021 if (S_ISLNK(inode->i_mode))
2022 inode->i_size = 64;
2023 if (p->iop)
2024 inode->i_op = p->iop;
2025 if (p->fop)
2026 inode->i_fop = p->fop;
2027 ei->op = p->op;
2028 dentry->d_op = &proc_base_dentry_operations;
2029 d_add(dentry, inode);
2030 error = NULL;
2031out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002032 return error;
2033out_iput:
2034 iput(inode);
2035 goto out;
2036}
2037
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002038static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2039{
2040 struct dentry *error;
2041 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002042 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002043
2044 error = ERR_PTR(-ENOENT);
2045
2046 if (!task)
2047 goto out_no_task;
2048
2049 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002050 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2051 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002052 if (p->len != dentry->d_name.len)
2053 continue;
2054 if (!memcmp(dentry->d_name.name, p->name, p->len))
2055 break;
2056 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002057 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002058 goto out;
2059
2060 error = proc_base_instantiate(dir, dentry, task, p);
2061
2062out:
2063 put_task_struct(task);
2064out_no_task:
2065 return error;
2066}
2067
Eric Dumazetc5141e62007-05-08 00:26:15 -07002068static int proc_base_fill_cache(struct file *filp, void *dirent,
2069 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002070{
2071 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2072 proc_base_instantiate, task, p);
2073}
2074
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002075#ifdef CONFIG_TASK_IO_ACCOUNTING
2076static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2077{
2078 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002079#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002080 "rchar: %llu\n"
2081 "wchar: %llu\n"
2082 "syscr: %llu\n"
2083 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002084#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002085 "read_bytes: %llu\n"
2086 "write_bytes: %llu\n"
2087 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002088#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002089 (unsigned long long)task->rchar,
2090 (unsigned long long)task->wchar,
2091 (unsigned long long)task->syscr,
2092 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002093#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002094 (unsigned long long)task->ioac.read_bytes,
2095 (unsigned long long)task->ioac.write_bytes,
2096 (unsigned long long)task->ioac.cancelled_write_bytes);
2097}
2098#endif
2099
Eric W. Biederman801199c2006-10-02 02:18:48 -07002100/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002101 * Thread groups
2102 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002103static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002104static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002105
Eric Dumazetc5141e62007-05-08 00:26:15 -07002106static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002107 DIR("task", S_IRUGO|S_IXUGO, task),
2108 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002109 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002110 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002111 INF("auxv", S_IRUSR, pid_auxv),
2112 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002113#ifdef CONFIG_SCHED_DEBUG
2114 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2115#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002116 INF("cmdline", S_IRUGO, pid_cmdline),
2117 INF("stat", S_IRUGO, tgid_stat),
2118 INF("statm", S_IRUGO, pid_statm),
2119 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002120#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002121 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002122#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002123 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002124 LNK("cwd", cwd),
2125 LNK("root", root),
2126 LNK("exe", exe),
2127 REG("mounts", S_IRUGO, mounts),
2128 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002129#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002130 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002131 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002132#endif
2133#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002134 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002135#endif
2136#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002137 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002138#endif
2139#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002140 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002141#endif
Paul Menage8793d852007-10-18 23:39:39 -07002142#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002143 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002144#endif
Paul Menagea4243162007-10-18 23:39:35 -07002145#ifdef CONFIG_CGROUPS
2146 REG("cgroup", S_IRUGO, cgroup),
2147#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002148 INF("oom_score", S_IRUGO, oom_score),
2149 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002150#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002151 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002152#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002153#ifdef CONFIG_FAULT_INJECTION
2154 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2155#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002156#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2157 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2158#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002159#ifdef CONFIG_TASK_IO_ACCOUNTING
2160 INF("io", S_IRUGO, pid_io_accounting),
2161#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002162};
2163
2164static int proc_tgid_base_readdir(struct file * filp,
2165 void * dirent, filldir_t filldir)
2166{
2167 return proc_pident_readdir(filp,dirent,filldir,
2168 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2169}
2170
Arjan van de Ven00977a52007-02-12 00:55:34 -08002171static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002172 .read = generic_read_dir,
2173 .readdir = proc_tgid_base_readdir,
2174};
2175
2176static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002177 return proc_pident_lookup(dir, dentry,
2178 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002179}
2180
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002181static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002182 .lookup = proc_tgid_base_lookup,
2183 .getattr = pid_getattr,
2184 .setattr = proc_setattr,
2185};
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07002188 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002190 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002192 * Looks in the dcache for
2193 * /proc/@pid
2194 * /proc/@tgid/task/@pid
2195 * if either directory is present flushes it and all of it'ts children
2196 * from the dcache.
2197 *
2198 * It is safe and reasonable to cache /proc entries for a task until
2199 * that task exits. After that they just clog up the dcache with
2200 * useless entries, possibly causing useful dcache entries to be
2201 * flushed instead. This routine is proved to flush those useless
2202 * dcache entries at process exit time.
2203 *
2204 * NOTE: This routine is just an optimization so it does not guarantee
2205 * that no dcache entries will exist at process exit time it
2206 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002208static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002210 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002211 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002212 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Eric W. Biederman48e64842006-06-26 00:25:48 -07002214 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002215 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2216 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002217 if (dentry) {
2218 shrink_dcache_parent(dentry);
2219 d_drop(dentry);
2220 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002223 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002224 goto out;
2225
2226 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002227 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2228 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002229 if (!leader)
2230 goto out;
2231
2232 name.name = "task";
2233 name.len = strlen(name.name);
2234 dir = d_hash_and_lookup(leader, &name);
2235 if (!dir)
2236 goto out_put_leader;
2237
2238 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002239 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002240 dentry = d_hash_and_lookup(dir, &name);
2241 if (dentry) {
2242 shrink_dcache_parent(dentry);
2243 d_drop(dentry);
2244 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002246
2247 dput(dir);
2248out_put_leader:
2249 dput(leader);
2250out:
2251 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002254/*
2255 * when flushing dentries from proc one need to flush them from global
2256 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2257 * in. this call is supposed to make all this job.
2258 */
2259
2260void proc_flush_task(struct task_struct *task)
2261{
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002262 int i, leader;
2263 struct pid *pid, *tgid;
2264 struct upid *upid;
2265
2266 leader = thread_group_leader(task);
2267 proc_flush_task_mnt(proc_mnt, task->pid, leader ? task->tgid : 0);
2268 pid = task_pid(task);
2269 if (pid->level == 0)
2270 return;
2271
2272 tgid = task_tgid(task);
2273 for (i = 1; i <= pid->level; i++) {
2274 upid = &pid->numbers[i];
2275 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2276 leader ? 0 : tgid->numbers[i].nr);
2277 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002278
2279 upid = &pid->numbers[pid->level];
2280 if (upid->nr == 1)
2281 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002282}
2283
Adrian Bunk9711ef92006-12-06 20:38:31 -08002284static struct dentry *proc_pid_instantiate(struct inode *dir,
2285 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002286 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002287{
2288 struct dentry *error = ERR_PTR(-ENOENT);
2289 struct inode *inode;
2290
Eric W. Biederman61a28782006-10-02 02:18:49 -07002291 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002292 if (!inode)
2293 goto out;
2294
2295 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2296 inode->i_op = &proc_tgid_base_inode_operations;
2297 inode->i_fop = &proc_tgid_base_operations;
2298 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002299 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002300#ifdef CONFIG_SECURITY
2301 inode->i_nlink += 1;
2302#endif
2303
2304 dentry->d_op = &pid_dentry_operations;
2305
2306 d_add(dentry, inode);
2307 /* Close the race of the process dying before we return the dentry */
2308 if (pid_revalidate(dentry, NULL))
2309 error = NULL;
2310out:
2311 return error;
2312}
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2315{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002316 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002319 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Eric W. Biederman801199c2006-10-02 02:18:48 -07002321 result = proc_base_lookup(dir, dentry);
2322 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2323 goto out;
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 tgid = name_to_int(dentry);
2326 if (tgid == ~0U)
2327 goto out;
2328
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002329 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002330 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002331 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (task)
2333 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002334 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (!task)
2336 goto out;
2337
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002338 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002339 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002341 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002345 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002346 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002348static struct task_struct *next_tgid(unsigned int tgid,
2349 struct pid_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002351 struct task_struct *task;
2352 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002354 rcu_read_lock();
2355retry:
2356 task = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002357 pid = find_ge_pid(tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002358 if (pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002359 tgid = pid_nr_ns(pid, ns) + 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002360 task = pid_task(pid, PIDTYPE_PID);
2361 /* What we to know is if the pid we have find is the
2362 * pid of a thread_group_leader. Testing for task
2363 * being a thread_group_leader is the obvious thing
2364 * todo but there is a window when it fails, due to
2365 * the pid transfer logic in de_thread.
2366 *
2367 * So we perform the straight forward test of seeing
2368 * if the pid we have found is the pid of a thread
2369 * group leader, and don't worry if the task we have
2370 * found doesn't happen to be a thread group leader.
2371 * As we don't care in the case of readdir.
2372 */
2373 if (!task || !has_group_leader_pid(task))
2374 goto retry;
2375 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002377 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002378 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002381#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Eric W. Biederman61a28782006-10-02 02:18:49 -07002383static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2384 struct task_struct *task, int tgid)
2385{
2386 char name[PROC_NUMBUF];
2387 int len = snprintf(name, sizeof(name), "%d", tgid);
2388 return proc_fill_cache(filp, dirent, filldir, name, len,
2389 proc_pid_instantiate, task, NULL);
2390}
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/* for the /proc/ directory itself, after non-process stuff has been done */
2393int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2394{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002396 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002397 struct task_struct *task;
2398 int tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002399 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Eric W. Biederman61a28782006-10-02 02:18:49 -07002401 if (!reaper)
2402 goto out_no_task;
2403
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002404 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002405 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002406 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002410 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002411 tgid = filp->f_pos - TGID_OFFSET;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002412 for (task = next_tgid(tgid, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002413 task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002414 put_task_struct(task), task = next_tgid(tgid + 1, ns)) {
2415 tgid = task_pid_nr_ns(task, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002416 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002417 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002418 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002419 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002422 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2423out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002424 put_task_struct(reaper);
2425out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return 0;
2427}
2428
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002429/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002430 * Tasks
2431 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002432static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002433 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002434 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002435 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002436 INF("auxv", S_IRUSR, pid_auxv),
2437 INF("status", S_IRUGO, pid_status),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002438#ifdef CONFIG_SCHED_DEBUG
2439 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2440#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002441 INF("cmdline", S_IRUGO, pid_cmdline),
2442 INF("stat", S_IRUGO, tid_stat),
2443 INF("statm", S_IRUGO, pid_statm),
2444 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002445#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002446 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002447#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002448 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002449 LNK("cwd", cwd),
2450 LNK("root", root),
2451 LNK("exe", exe),
2452 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002453#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002454 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002455 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002456#endif
2457#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002458 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002459#endif
2460#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002461 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002462#endif
2463#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002464 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002465#endif
Paul Menage8793d852007-10-18 23:39:39 -07002466#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002467 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002468#endif
Paul Menagea4243162007-10-18 23:39:35 -07002469#ifdef CONFIG_CGROUPS
2470 REG("cgroup", S_IRUGO, cgroup),
2471#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002472 INF("oom_score", S_IRUGO, oom_score),
2473 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002474#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002475 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002476#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002477#ifdef CONFIG_FAULT_INJECTION
2478 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2479#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002480};
2481
2482static int proc_tid_base_readdir(struct file * filp,
2483 void * dirent, filldir_t filldir)
2484{
2485 return proc_pident_readdir(filp,dirent,filldir,
2486 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2487}
2488
2489static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002490 return proc_pident_lookup(dir, dentry,
2491 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002492}
2493
Arjan van de Ven00977a52007-02-12 00:55:34 -08002494static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002495 .read = generic_read_dir,
2496 .readdir = proc_tid_base_readdir,
2497};
2498
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002499static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002500 .lookup = proc_tid_base_lookup,
2501 .getattr = pid_getattr,
2502 .setattr = proc_setattr,
2503};
2504
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002505static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002506 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002507{
2508 struct dentry *error = ERR_PTR(-ENOENT);
2509 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002510 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002511
2512 if (!inode)
2513 goto out;
2514 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2515 inode->i_op = &proc_tid_base_inode_operations;
2516 inode->i_fop = &proc_tid_base_operations;
2517 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002518 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002519#ifdef CONFIG_SECURITY
2520 inode->i_nlink += 1;
2521#endif
2522
2523 dentry->d_op = &pid_dentry_operations;
2524
2525 d_add(dentry, inode);
2526 /* Close the race of the process dying before we return the dentry */
2527 if (pid_revalidate(dentry, NULL))
2528 error = NULL;
2529out:
2530 return error;
2531}
2532
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002533static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2534{
2535 struct dentry *result = ERR_PTR(-ENOENT);
2536 struct task_struct *task;
2537 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002538 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002539 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002540
2541 if (!leader)
2542 goto out_no_task;
2543
2544 tid = name_to_int(dentry);
2545 if (tid == ~0U)
2546 goto out;
2547
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002548 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002549 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002550 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551 if (task)
2552 get_task_struct(task);
2553 rcu_read_unlock();
2554 if (!task)
2555 goto out;
2556 if (leader->tgid != task->tgid)
2557 goto out_drop_task;
2558
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002559 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002560out_drop_task:
2561 put_task_struct(task);
2562out:
2563 put_task_struct(leader);
2564out_no_task:
2565 return result;
2566}
2567
2568/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002569 * Find the first tid of a thread group to return to user space.
2570 *
2571 * Usually this is just the thread group leader, but if the users
2572 * buffer was too small or there was a seek into the middle of the
2573 * directory we have more work todo.
2574 *
2575 * In the case of a short read we start with find_task_by_pid.
2576 *
2577 * In the case of a seek we start with the leader and walk nr
2578 * threads past it.
2579 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002580static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002581 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002582{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002583 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002584
Eric W. Biedermancc288732006-06-26 00:26:01 -07002585 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002586 /* Attempt to start with the pid of a thread */
2587 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002588 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002589 if (pos && (pos->group_leader == leader))
2590 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002591 }
2592
2593 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002594 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002595 if (nr && nr >= get_nr_threads(leader))
2596 goto out;
2597
2598 /* If we haven't found our starting place yet start
2599 * with the leader and walk nr threads forward.
2600 */
2601 for (pos = leader; nr > 0; --nr) {
2602 pos = next_thread(pos);
2603 if (pos == leader) {
2604 pos = NULL;
2605 goto out;
2606 }
2607 }
2608found:
2609 get_task_struct(pos);
2610out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002611 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002612 return pos;
2613}
2614
2615/*
2616 * Find the next thread in the thread list.
2617 * Return NULL if there is an error or no next thread.
2618 *
2619 * The reference to the input task_struct is released.
2620 */
2621static struct task_struct *next_tid(struct task_struct *start)
2622{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002623 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002624 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002625 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002626 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002627 if (thread_group_leader(pos))
2628 pos = NULL;
2629 else
2630 get_task_struct(pos);
2631 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002632 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002633 put_task_struct(start);
2634 return pos;
2635}
2636
Eric W. Biederman61a28782006-10-02 02:18:49 -07002637static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2638 struct task_struct *task, int tid)
2639{
2640 char name[PROC_NUMBUF];
2641 int len = snprintf(name, sizeof(name), "%d", tid);
2642 return proc_fill_cache(filp, dirent, filldir, name, len,
2643 proc_task_instantiate, task, NULL);
2644}
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646/* for the /proc/TGID/task/ directories */
2647static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2648{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002649 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002651 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002652 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 int retval = -ENOENT;
2654 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002655 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002657 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002659 task = get_proc_task(inode);
2660 if (!task)
2661 goto out_no_task;
2662 rcu_read_lock();
2663 if (pid_alive(task)) {
2664 leader = task->group_leader;
2665 get_task_struct(leader);
2666 }
2667 rcu_read_unlock();
2668 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002669 if (!leader)
2670 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 retval = 0;
2672
2673 switch (pos) {
2674 case 0:
2675 ino = inode->i_ino;
2676 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2677 goto out;
2678 pos++;
2679 /* fall through */
2680 case 1:
2681 ino = parent_ino(dentry);
2682 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2683 goto out;
2684 pos++;
2685 /* fall through */
2686 }
2687
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002688 /* f_version caches the tgid value that the last readdir call couldn't
2689 * return. lseek aka telldir automagically resets f_version to 0.
2690 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002691 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002692 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002693 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002694 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002695 task;
2696 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002697 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002698 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002699 /* returning this tgid failed, save it as the first
2700 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002701 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002702 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
2706out:
2707 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002708 put_task_struct(leader);
2709out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return retval;
2711}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002712
2713static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2714{
2715 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002716 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002717 generic_fillattr(inode, stat);
2718
Eric W. Biederman99f89552006-06-26 00:25:55 -07002719 if (p) {
2720 rcu_read_lock();
2721 stat->nlink += get_nr_threads(p);
2722 rcu_read_unlock();
2723 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002724 }
2725
2726 return 0;
2727}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002728
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002729static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002730 .lookup = proc_task_lookup,
2731 .getattr = proc_task_getattr,
2732 .setattr = proc_setattr,
2733};
2734
Arjan van de Ven00977a52007-02-12 00:55:34 -08002735static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002736 .read = generic_read_dir,
2737 .readdir = proc_task_readdir,
2738};