blob: 54c2e5621afb654c26e82b29c0ef7eef90a31686 [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>
Neil Hormand85f50d2007-10-18 23:40:37 -070066#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070067#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/mount.h>
69#include <linux/security.h>
70#include <linux/ptrace.h>
Paul Menagea4243162007-10-18 23:39:35 -070071#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/cpuset.h>
73#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050074#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070075#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070076#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070077#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070078#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include "internal.h"
80
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070081/* NOTE:
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
86 *
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
89 */
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Eric W. Biederman8578cea2006-06-26 00:25:54 -070092/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070093#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070097 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080099 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800100 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700101 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Eric W. Biederman61a28782006-10-02 02:18:49 -0700104#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700105 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700106 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700107 .mode = MODE, \
108 .iop = IOP, \
109 .fop = FOP, \
110 .op = OP, \
111}
112
Eric W. Biederman61a28782006-10-02 02:18:49 -0700113#define DIR(NAME, MODE, OTYPE) \
114 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
116 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700117#define LNK(NAME, OTYPE) \
118 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 &proc_pid_link_inode_operations, NULL, \
120 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700121#define REG(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700123 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700124#define INF(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700126 NULL, &proc_info_file_operations, \
127 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Kees Cook5096add2007-05-08 00:26:04 -0700129int maps_protect;
130EXPORT_SYMBOL(maps_protect);
131
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700132static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700135 task_lock(task);
136 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if(fs)
138 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700139 task_unlock(task);
140 return fs;
141}
142
Eric W. Biederman99f89552006-06-26 00:25:55 -0700143static int get_nr_threads(struct task_struct *tsk)
144{
145 /* Must be called with the rcu_read_lock held */
146 unsigned long flags;
147 int count = 0;
148
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
152 }
153 return count;
154}
155
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700156static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
157{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700160 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700161
162 if (task) {
163 fs = get_fs_struct(task);
164 put_task_struct(task);
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (fs) {
167 read_lock(&fs->lock);
168 *mnt = mntget(fs->pwdmnt);
169 *dentry = dget(fs->pwd);
170 read_unlock(&fs->lock);
171 result = 0;
172 put_fs_struct(fs);
173 }
174 return result;
175}
176
177static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
178{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182
183 if (task) {
184 fs = get_fs_struct(task);
185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (fs) {
188 read_lock(&fs->lock);
189 *mnt = mntget(fs->rootmnt);
190 *dentry = dget(fs->root);
191 read_unlock(&fs->lock);
192 result = 0;
193 put_fs_struct(fs);
194 }
195 return result;
196}
197
198#define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
202 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
203 security_ptrace(current,task) == 0))
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static int proc_pid_cmdline(struct task_struct *task, char * buffer)
206{
207 int res = 0;
208 unsigned int len;
209 struct mm_struct *mm = get_task_mm(task);
210 if (!mm)
211 goto out;
212 if (!mm->arg_end)
213 goto out_mm; /* Shh! No looking before we're done */
214
215 len = mm->arg_end - mm->arg_start;
216
217 if (len > PAGE_SIZE)
218 len = PAGE_SIZE;
219
220 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
221
222 // If the nul at the end of args has been overwritten, then
223 // assume application is using setproctitle(3).
224 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
225 len = strnlen(buffer, res);
226 if (len < res) {
227 res = len;
228 } else {
229 len = mm->env_end - mm->env_start;
230 if (len > PAGE_SIZE - res)
231 len = PAGE_SIZE - res;
232 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
233 res = strnlen(buffer, res);
234 }
235 }
236out_mm:
237 mmput(mm);
238out:
239 return res;
240}
241
242static int proc_pid_auxv(struct task_struct *task, char *buffer)
243{
244 int res = 0;
245 struct mm_struct *mm = get_task_mm(task);
246 if (mm) {
247 unsigned int nwords = 0;
248 do
249 nwords += 2;
250 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
251 res = nwords * sizeof(mm->saved_auxv[0]);
252 if (res > PAGE_SIZE)
253 res = PAGE_SIZE;
254 memcpy(buffer, mm->saved_auxv, res);
255 mmput(mm);
256 }
257 return res;
258}
259
260
261#ifdef CONFIG_KALLSYMS
262/*
263 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
264 * Returns the resolved symbol. If that fails, simply return the address.
265 */
266static int proc_pid_wchan(struct task_struct *task, char *buffer)
267{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700268 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700269 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 wchan = get_wchan(task);
272
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700273 if (lookup_symbol_name(wchan, symname) < 0)
274 return sprintf(buffer, "%lu", wchan);
275 else
276 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278#endif /* CONFIG_KALLSYMS */
279
280#ifdef CONFIG_SCHEDSTATS
281/*
282 * Provides /proc/PID/schedstat
283 */
284static int proc_pid_schedstat(struct task_struct *task, char *buffer)
285{
Balbir Singh172ba842007-07-09 18:52:00 +0200286 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 task->sched_info.cpu_time,
288 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200289 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291#endif
292
293/* The badness from the OOM killer */
294unsigned long badness(struct task_struct *p, unsigned long uptime);
295static int proc_oom_score(struct task_struct *task, char *buffer)
296{
297 unsigned long points;
298 struct timespec uptime;
299
300 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700301 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700303 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return sprintf(buffer, "%lu\n", points);
305}
306
Neil Hormand85f50d2007-10-18 23:40:37 -0700307struct limit_names {
308 char *name;
309 char *unit;
310};
311
312static const struct limit_names lnames[RLIM_NLIMITS] = {
313 [RLIMIT_CPU] = {"Max cpu time", "ms"},
314 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
315 [RLIMIT_DATA] = {"Max data size", "bytes"},
316 [RLIMIT_STACK] = {"Max stack size", "bytes"},
317 [RLIMIT_CORE] = {"Max core file size", "bytes"},
318 [RLIMIT_RSS] = {"Max resident set", "bytes"},
319 [RLIMIT_NPROC] = {"Max processes", "processes"},
320 [RLIMIT_NOFILE] = {"Max open files", "files"},
321 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
322 [RLIMIT_AS] = {"Max address space", "bytes"},
323 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
324 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
325 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
326 [RLIMIT_NICE] = {"Max nice priority", NULL},
327 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
328};
329
330/* Display limits for a process */
331static int proc_pid_limits(struct task_struct *task, char *buffer)
332{
333 unsigned int i;
334 int count = 0;
335 unsigned long flags;
336 char *bufptr = buffer;
337
338 struct rlimit rlim[RLIM_NLIMITS];
339
340 rcu_read_lock();
341 if (!lock_task_sighand(task,&flags)) {
342 rcu_read_unlock();
343 return 0;
344 }
345 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
346 unlock_task_sighand(task, &flags);
347 rcu_read_unlock();
348
349 /*
350 * print the file header
351 */
352 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
353 "Limit", "Soft Limit", "Hard Limit", "Units");
354
355 for (i = 0; i < RLIM_NLIMITS; i++) {
356 if (rlim[i].rlim_cur == RLIM_INFINITY)
357 count += sprintf(&bufptr[count], "%-25s %-20s ",
358 lnames[i].name, "unlimited");
359 else
360 count += sprintf(&bufptr[count], "%-25s %-20lu ",
361 lnames[i].name, rlim[i].rlim_cur);
362
363 if (rlim[i].rlim_max == RLIM_INFINITY)
364 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
365 else
366 count += sprintf(&bufptr[count], "%-20lu ",
367 rlim[i].rlim_max);
368
369 if (lnames[i].unit)
370 count += sprintf(&bufptr[count], "%-10s\n",
371 lnames[i].unit);
372 else
373 count += sprintf(&bufptr[count], "\n");
374 }
375
376 return count;
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/************************************************************************/
380/* Here the fs part begins */
381/************************************************************************/
382
383/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700384static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700386 struct task_struct *task;
387 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700388 /* Allow access to a task's file descriptors if it is us or we
389 * may use ptrace attach to the process and find out that
390 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700391 */
392 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700393 if (task) {
394 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700395 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700396 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700397 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700400static int proc_setattr(struct dentry *dentry, struct iattr *attr)
401{
402 int error;
403 struct inode *inode = dentry->d_inode;
404
405 if (attr->ia_valid & ATTR_MODE)
406 return -EPERM;
407
408 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700409 if (!error)
410 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700411 return error;
412}
413
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800414static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700415 .setattr = proc_setattr,
416};
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500419struct proc_mounts {
420 struct seq_file m;
421 int event;
422};
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static int mounts_open(struct inode *inode, struct file *file)
425{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700426 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700427 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800428 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500429 struct proc_mounts *p;
430 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Eric W. Biederman99f89552006-06-26 00:25:55 -0700432 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700433 rcu_read_lock();
434 nsp = task_nsproxy(task);
435 if (nsp) {
436 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800437 if (ns)
438 get_mnt_ns(ns);
439 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700440 rcu_read_unlock();
441
Eric W. Biederman99f89552006-06-26 00:25:55 -0700442 put_task_struct(task);
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800445 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500446 ret = -ENOMEM;
447 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
448 if (p) {
449 file->private_data = &p->m;
450 ret = seq_open(file, &mounts_op);
451 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800452 p->m.private = ns;
453 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500454 return 0;
455 }
456 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800458 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 return ret;
461}
462
463static int mounts_release(struct inode *inode, struct file *file)
464{
465 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800466 struct mnt_namespace *ns = m->private;
467 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return seq_release(inode, file);
469}
470
Al Viro5addc5d2005-11-07 17:15:49 -0500471static unsigned mounts_poll(struct file *file, poll_table *wait)
472{
473 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800474 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500475 unsigned res = 0;
476
477 poll_wait(file, &ns->poll, wait);
478
479 spin_lock(&vfsmount_lock);
480 if (p->event != ns->event) {
481 p->event = ns->event;
482 res = POLLERR;
483 }
484 spin_unlock(&vfsmount_lock);
485
486 return res;
487}
488
Arjan van de Ven00977a52007-02-12 00:55:34 -0800489static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 .open = mounts_open,
491 .read = seq_read,
492 .llseek = seq_lseek,
493 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500494 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495};
496
Chuck Leverb4629fe2006-03-20 13:44:12 -0500497extern struct seq_operations mountstats_op;
498static int mountstats_open(struct inode *inode, struct file *file)
499{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500500 int ret = seq_open(file, &mountstats_op);
501
502 if (!ret) {
503 struct seq_file *m = file->private_data;
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700504 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800505 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700506 struct task_struct *task = get_proc_task(inode);
507
508 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700509 rcu_read_lock();
510 nsp = task_nsproxy(task);
511 if (nsp) {
512 mnt_ns = nsp->mnt_ns;
513 if (mnt_ns)
514 get_mnt_ns(mnt_ns);
515 }
516 rcu_read_unlock();
517
Eric W. Biederman99f89552006-06-26 00:25:55 -0700518 put_task_struct(task);
519 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500520
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800521 if (mnt_ns)
522 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500523 else {
524 seq_release(inode, file);
525 ret = -EINVAL;
526 }
527 }
528 return ret;
529}
530
Arjan van de Ven00977a52007-02-12 00:55:34 -0800531static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500532 .open = mountstats_open,
533 .read = seq_read,
534 .llseek = seq_lseek,
535 .release = mounts_release,
536};
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
539
540static ssize_t proc_info_read(struct file * file, char __user * buf,
541 size_t count, loff_t *ppos)
542{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800543 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 unsigned long page;
545 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700546 struct task_struct *task = get_proc_task(inode);
547
548 length = -ESRCH;
549 if (!task)
550 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (count > PROC_BLOCK_SIZE)
553 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700554
555 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700556 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 length = PROC_I(inode)->op.proc_read(task, (char*)page);
560
561 if (length >= 0)
562 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
563 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700564out:
565 put_task_struct(task);
566out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return length;
568}
569
Arjan van de Ven00977a52007-02-12 00:55:34 -0800570static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 .read = proc_info_read,
572};
573
574static int mem_open(struct inode* inode, struct file* file)
575{
576 file->private_data = (void*)((long)current->self_exec_id);
577 return 0;
578}
579
580static ssize_t mem_read(struct file * file, char __user * buf,
581 size_t count, loff_t *ppos)
582{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800583 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 char *page;
585 unsigned long src = *ppos;
586 int ret = -ESRCH;
587 struct mm_struct *mm;
588
Eric W. Biederman99f89552006-06-26 00:25:55 -0700589 if (!task)
590 goto out_no_task;
591
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700592 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 goto out;
594
595 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700596 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!page)
598 goto out;
599
600 ret = 0;
601
602 mm = get_task_mm(task);
603 if (!mm)
604 goto out_free;
605
606 ret = -EIO;
607
608 if (file->private_data != (void*)((long)current->self_exec_id))
609 goto out_put;
610
611 ret = 0;
612
613 while (count > 0) {
614 int this_len, retval;
615
616 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
617 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700618 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!ret)
620 ret = -EIO;
621 break;
622 }
623
624 if (copy_to_user(buf, page, retval)) {
625 ret = -EFAULT;
626 break;
627 }
628
629 ret += retval;
630 src += retval;
631 buf += retval;
632 count -= retval;
633 }
634 *ppos = src;
635
636out_put:
637 mmput(mm);
638out_free:
639 free_page((unsigned long) page);
640out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700641 put_task_struct(task);
642out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return ret;
644}
645
646#define mem_write NULL
647
648#ifndef mem_write
649/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800650static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 size_t count, loff_t *ppos)
652{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700653 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800655 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 unsigned long dst = *ppos;
657
Eric W. Biederman99f89552006-06-26 00:25:55 -0700658 copied = -ESRCH;
659 if (!task)
660 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Eric W. Biederman99f89552006-06-26 00:25:55 -0700662 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
663 goto out;
664
665 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700666 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700668 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700670 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 while (count > 0) {
672 int this_len, retval;
673
674 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
675 if (copy_from_user(page, buf, this_len)) {
676 copied = -EFAULT;
677 break;
678 }
679 retval = access_process_vm(task, dst, page, this_len, 1);
680 if (!retval) {
681 if (!copied)
682 copied = -EIO;
683 break;
684 }
685 copied += retval;
686 buf += retval;
687 dst += retval;
688 count -= retval;
689 }
690 *ppos = dst;
691 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700692out:
693 put_task_struct(task);
694out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return copied;
696}
697#endif
698
699static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
700{
701 switch (orig) {
702 case 0:
703 file->f_pos = offset;
704 break;
705 case 1:
706 file->f_pos += offset;
707 break;
708 default:
709 return -EINVAL;
710 }
711 force_successful_syscall_return();
712 return file->f_pos;
713}
714
Arjan van de Ven00977a52007-02-12 00:55:34 -0800715static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 .llseek = mem_lseek,
717 .read = mem_read,
718 .write = mem_write,
719 .open = mem_open,
720};
721
James Pearson315e28c2007-10-16 23:30:17 -0700722static ssize_t environ_read(struct file *file, char __user *buf,
723 size_t count, loff_t *ppos)
724{
725 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
726 char *page;
727 unsigned long src = *ppos;
728 int ret = -ESRCH;
729 struct mm_struct *mm;
730
731 if (!task)
732 goto out_no_task;
733
734 if (!ptrace_may_attach(task))
735 goto out;
736
737 ret = -ENOMEM;
738 page = (char *)__get_free_page(GFP_TEMPORARY);
739 if (!page)
740 goto out;
741
742 ret = 0;
743
744 mm = get_task_mm(task);
745 if (!mm)
746 goto out_free;
747
748 while (count > 0) {
749 int this_len, retval, max_len;
750
751 this_len = mm->env_end - (mm->env_start + src);
752
753 if (this_len <= 0)
754 break;
755
756 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
757 this_len = (this_len > max_len) ? max_len : this_len;
758
759 retval = access_process_vm(task, (mm->env_start + src),
760 page, this_len, 0);
761
762 if (retval <= 0) {
763 ret = retval;
764 break;
765 }
766
767 if (copy_to_user(buf, page, retval)) {
768 ret = -EFAULT;
769 break;
770 }
771
772 ret += retval;
773 src += retval;
774 buf += retval;
775 count -= retval;
776 }
777 *ppos = src;
778
779 mmput(mm);
780out_free:
781 free_page((unsigned long) page);
782out:
783 put_task_struct(task);
784out_no_task:
785 return ret;
786}
787
788static const struct file_operations proc_environ_operations = {
789 .read = environ_read,
790};
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792static ssize_t oom_adjust_read(struct file *file, char __user *buf,
793 size_t count, loff_t *ppos)
794{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800795 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700796 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700798 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Eric W. Biederman99f89552006-06-26 00:25:55 -0700800 if (!task)
801 return -ESRCH;
802 oom_adjust = task->oomkilladj;
803 put_task_struct(task);
804
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700805 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700806
807 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
811 size_t count, loff_t *ppos)
812{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700813 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700814 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 int oom_adjust;
816
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700817 memset(buffer, 0, sizeof(buffer));
818 if (count > sizeof(buffer) - 1)
819 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (copy_from_user(buffer, buf, count))
821 return -EFAULT;
822 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700823 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
824 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -EINVAL;
826 if (*end == '\n')
827 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800828 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700829 if (!task)
830 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800831 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
832 put_task_struct(task);
833 return -EACCES;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700836 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (end - buffer == 0)
838 return -EIO;
839 return end - buffer;
840}
841
Arjan van de Ven00977a52007-02-12 00:55:34 -0800842static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 .read = oom_adjust_read,
844 .write = oom_adjust_write,
845};
846
David Rientjes4b8df892007-05-08 20:27:01 -0700847#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -0700848static ssize_t clear_refs_write(struct file *file, const char __user *buf,
849 size_t count, loff_t *ppos)
850{
851 struct task_struct *task;
852 char buffer[PROC_NUMBUF], *end;
853 struct mm_struct *mm;
854
855 memset(buffer, 0, sizeof(buffer));
856 if (count > sizeof(buffer) - 1)
857 count = sizeof(buffer) - 1;
858 if (copy_from_user(buffer, buf, count))
859 return -EFAULT;
860 if (!simple_strtol(buffer, &end, 0))
861 return -EINVAL;
862 if (*end == '\n')
863 end++;
864 task = get_proc_task(file->f_path.dentry->d_inode);
865 if (!task)
866 return -ESRCH;
867 mm = get_task_mm(task);
868 if (mm) {
869 clear_refs_smap(mm);
870 mmput(mm);
871 }
872 put_task_struct(task);
873 if (end - buffer == 0)
874 return -EIO;
875 return end - buffer;
876}
877
878static struct file_operations proc_clear_refs_operations = {
879 .write = clear_refs_write,
880};
David Rientjes4b8df892007-05-08 20:27:01 -0700881#endif
David Rientjesb813e932007-05-06 14:49:24 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883#ifdef CONFIG_AUDITSYSCALL
884#define TMPBUFLEN 21
885static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
886 size_t count, loff_t *ppos)
887{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800888 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700889 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ssize_t length;
891 char tmpbuf[TMPBUFLEN];
892
Eric W. Biederman99f89552006-06-26 00:25:55 -0700893 if (!task)
894 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
896 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700897 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
899}
900
901static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
902 size_t count, loff_t *ppos)
903{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800904 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 char *page, *tmp;
906 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 uid_t loginuid;
908
909 if (!capable(CAP_AUDIT_CONTROL))
910 return -EPERM;
911
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700912 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -EPERM;
914
Al Viroe0182902006-05-18 08:28:02 -0400915 if (count >= PAGE_SIZE)
916 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (*ppos != 0) {
919 /* No partial writes. */
920 return -EINVAL;
921 }
Mel Gormane12ba742007-10-16 01:25:52 -0700922 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!page)
924 return -ENOMEM;
925 length = -EFAULT;
926 if (copy_from_user(page, buf, count))
927 goto out_free_page;
928
Al Viroe0182902006-05-18 08:28:02 -0400929 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 loginuid = simple_strtoul(page, &tmp, 10);
931 if (tmp == page) {
932 length = -EINVAL;
933 goto out_free_page;
934
935 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700936 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (likely(length == 0))
938 length = count;
939
940out_free_page:
941 free_page((unsigned long) page);
942 return length;
943}
944
Arjan van de Ven00977a52007-02-12 00:55:34 -0800945static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 .read = proc_loginuid_read,
947 .write = proc_loginuid_write,
948};
949#endif
950
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800951#ifdef CONFIG_FAULT_INJECTION
952static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
953 size_t count, loff_t *ppos)
954{
955 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
956 char buffer[PROC_NUMBUF];
957 size_t len;
958 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800959
960 if (!task)
961 return -ESRCH;
962 make_it_fail = task->make_it_fail;
963 put_task_struct(task);
964
965 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700966
967 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800968}
969
970static ssize_t proc_fault_inject_write(struct file * file,
971 const char __user * buf, size_t count, loff_t *ppos)
972{
973 struct task_struct *task;
974 char buffer[PROC_NUMBUF], *end;
975 int make_it_fail;
976
977 if (!capable(CAP_SYS_RESOURCE))
978 return -EPERM;
979 memset(buffer, 0, sizeof(buffer));
980 if (count > sizeof(buffer) - 1)
981 count = sizeof(buffer) - 1;
982 if (copy_from_user(buffer, buf, count))
983 return -EFAULT;
984 make_it_fail = simple_strtol(buffer, &end, 0);
985 if (*end == '\n')
986 end++;
987 task = get_proc_task(file->f_dentry->d_inode);
988 if (!task)
989 return -ESRCH;
990 task->make_it_fail = make_it_fail;
991 put_task_struct(task);
992 if (end - buffer == 0)
993 return -EIO;
994 return end - buffer;
995}
996
Arjan van de Ven00977a52007-02-12 00:55:34 -0800997static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800998 .read = proc_fault_inject_read,
999 .write = proc_fault_inject_write,
1000};
1001#endif
1002
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001003#ifdef CONFIG_SCHED_DEBUG
1004/*
1005 * Print out various scheduling related per-task fields:
1006 */
1007static int sched_show(struct seq_file *m, void *v)
1008{
1009 struct inode *inode = m->private;
1010 struct task_struct *p;
1011
1012 WARN_ON(!inode);
1013
1014 p = get_proc_task(inode);
1015 if (!p)
1016 return -ESRCH;
1017 proc_sched_show_task(p, m);
1018
1019 put_task_struct(p);
1020
1021 return 0;
1022}
1023
1024static ssize_t
1025sched_write(struct file *file, const char __user *buf,
1026 size_t count, loff_t *offset)
1027{
1028 struct inode *inode = file->f_path.dentry->d_inode;
1029 struct task_struct *p;
1030
1031 WARN_ON(!inode);
1032
1033 p = get_proc_task(inode);
1034 if (!p)
1035 return -ESRCH;
1036 proc_sched_set_task(p);
1037
1038 put_task_struct(p);
1039
1040 return count;
1041}
1042
1043static int sched_open(struct inode *inode, struct file *filp)
1044{
1045 int ret;
1046
1047 ret = single_open(filp, sched_show, NULL);
1048 if (!ret) {
1049 struct seq_file *m = filp->private_data;
1050
1051 m->private = inode;
1052 }
1053 return ret;
1054}
1055
1056static const struct file_operations proc_pid_sched_operations = {
1057 .open = sched_open,
1058 .read = seq_read,
1059 .write = sched_write,
1060 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001061 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001062};
1063
1064#endif
1065
Al Viro008b1502005-08-20 00:17:39 +01001066static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct inode *inode = dentry->d_inode;
1069 int error = -EACCES;
1070
1071 /* We don't need a base pointer in the /proc filesystem */
1072 path_release(nd);
1073
Eric W. Biederman778c1142006-06-26 00:25:58 -07001074 /* Are we allowed to snoop on the tasks file descriptors? */
1075 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1079 nd->last_type = LAST_BIND;
1080out:
Al Viro008b1502005-08-20 00:17:39 +01001081 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1085 char __user *buffer, int buflen)
1086{
1087 struct inode * inode;
Mel Gormane12ba742007-10-16 01:25:52 -07001088 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1089 char *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int len;
1091
1092 if (!tmp)
1093 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 inode = dentry->d_inode;
1096 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1097 len = PTR_ERR(path);
1098 if (IS_ERR(path))
1099 goto out;
1100 len = tmp + PAGE_SIZE - 1 - path;
1101
1102 if (len > buflen)
1103 len = buflen;
1104 if (copy_to_user(buffer, path, len))
1105 len = -EFAULT;
1106 out:
1107 free_page((unsigned long)tmp);
1108 return len;
1109}
1110
1111static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1112{
1113 int error = -EACCES;
1114 struct inode *inode = dentry->d_inode;
1115 struct dentry *de;
1116 struct vfsmount *mnt = NULL;
1117
Eric W. Biederman778c1142006-06-26 00:25:58 -07001118 /* Are we allowed to snoop on the tasks file descriptors? */
1119 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1123 if (error)
1124 goto out;
1125
1126 error = do_proc_readlink(de, mnt, buffer, buflen);
1127 dput(de);
1128 mntput(mnt);
1129out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return error;
1131}
1132
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001133static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001135 .follow_link = proc_pid_follow_link,
1136 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137};
1138
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001139
1140/* building an inode */
1141
1142static int task_dumpable(struct task_struct *task)
1143{
1144 int dumpable = 0;
1145 struct mm_struct *mm;
1146
1147 task_lock(task);
1148 mm = task->mm;
1149 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001150 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001151 task_unlock(task);
1152 if(dumpable == 1)
1153 return 1;
1154 return 0;
1155}
1156
1157
Eric W. Biederman61a28782006-10-02 02:18:49 -07001158static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001159{
1160 struct inode * inode;
1161 struct proc_inode *ei;
1162
1163 /* We need a new inode */
1164
1165 inode = new_inode(sb);
1166 if (!inode)
1167 goto out;
1168
1169 /* Common stuff */
1170 ei = PROC_I(inode);
1171 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001172 inode->i_op = &proc_def_inode_operations;
1173
1174 /*
1175 * grab the reference to task.
1176 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001177 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001178 if (!ei->pid)
1179 goto out_unlock;
1180
1181 inode->i_uid = 0;
1182 inode->i_gid = 0;
1183 if (task_dumpable(task)) {
1184 inode->i_uid = task->euid;
1185 inode->i_gid = task->egid;
1186 }
1187 security_task_to_inode(task, inode);
1188
1189out:
1190 return inode;
1191
1192out_unlock:
1193 iput(inode);
1194 return NULL;
1195}
1196
1197static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1198{
1199 struct inode *inode = dentry->d_inode;
1200 struct task_struct *task;
1201 generic_fillattr(inode, stat);
1202
1203 rcu_read_lock();
1204 stat->uid = 0;
1205 stat->gid = 0;
1206 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1207 if (task) {
1208 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1209 task_dumpable(task)) {
1210 stat->uid = task->euid;
1211 stat->gid = task->egid;
1212 }
1213 }
1214 rcu_read_unlock();
1215 return 0;
1216}
1217
1218/* dentry stuff */
1219
1220/*
1221 * Exceptional case: normally we are not allowed to unhash a busy
1222 * directory. In this case, however, we can do it - no aliasing problems
1223 * due to the way we treat inodes.
1224 *
1225 * Rewrite the inode's ownerships here because the owning task may have
1226 * performed a setuid(), etc.
1227 *
1228 * Before the /proc/pid/status file was created the only way to read
1229 * the effective uid of a /process was to stat /proc/pid. Reading
1230 * /proc/pid/status is slow enough that procps and other packages
1231 * kept stating /proc/pid. To keep the rules in /proc simple I have
1232 * made this apply to all per process world readable and executable
1233 * directories.
1234 */
1235static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1236{
1237 struct inode *inode = dentry->d_inode;
1238 struct task_struct *task = get_proc_task(inode);
1239 if (task) {
1240 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1241 task_dumpable(task)) {
1242 inode->i_uid = task->euid;
1243 inode->i_gid = task->egid;
1244 } else {
1245 inode->i_uid = 0;
1246 inode->i_gid = 0;
1247 }
1248 inode->i_mode &= ~(S_ISUID | S_ISGID);
1249 security_task_to_inode(task, inode);
1250 put_task_struct(task);
1251 return 1;
1252 }
1253 d_drop(dentry);
1254 return 0;
1255}
1256
1257static int pid_delete_dentry(struct dentry * dentry)
1258{
1259 /* Is the task we represent dead?
1260 * If so, then don't put the dentry on the lru list,
1261 * kill it immediately.
1262 */
1263 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1264}
1265
1266static struct dentry_operations pid_dentry_operations =
1267{
1268 .d_revalidate = pid_revalidate,
1269 .d_delete = pid_delete_dentry,
1270};
1271
1272/* Lookups */
1273
Eric Dumazetc5141e62007-05-08 00:26:15 -07001274typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1275 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001276
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001277/*
1278 * Fill a directory entry.
1279 *
1280 * If possible create the dcache entry and derive our inode number and
1281 * file type from dcache entry.
1282 *
1283 * Since all of the proc inode numbers are dynamically generated, the inode
1284 * numbers do not exist until the inode is cache. This means creating the
1285 * the dcache entry in readdir is necessary to keep the inode numbers
1286 * reported by readdir in sync with the inode numbers reported
1287 * by stat.
1288 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001289static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1290 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001291 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001292{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001293 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001294 struct inode *inode;
1295 struct qstr qname;
1296 ino_t ino = 0;
1297 unsigned type = DT_UNKNOWN;
1298
1299 qname.name = name;
1300 qname.len = len;
1301 qname.hash = full_name_hash(name, len);
1302
1303 child = d_lookup(dir, &qname);
1304 if (!child) {
1305 struct dentry *new;
1306 new = d_alloc(dir, &qname);
1307 if (new) {
1308 child = instantiate(dir->d_inode, new, task, ptr);
1309 if (child)
1310 dput(new);
1311 else
1312 child = new;
1313 }
1314 }
1315 if (!child || IS_ERR(child) || !child->d_inode)
1316 goto end_instantiate;
1317 inode = child->d_inode;
1318 if (inode) {
1319 ino = inode->i_ino;
1320 type = inode->i_mode >> 12;
1321 }
1322 dput(child);
1323end_instantiate:
1324 if (!ino)
1325 ino = find_inode_number(dir, &qname);
1326 if (!ino)
1327 ino = 1;
1328 return filldir(dirent, name, len, filp->f_pos, ino, type);
1329}
1330
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001331static unsigned name_to_int(struct dentry *dentry)
1332{
1333 const char *name = dentry->d_name.name;
1334 int len = dentry->d_name.len;
1335 unsigned n = 0;
1336
1337 if (len > 1 && *name == '0')
1338 goto out;
1339 while (len-- > 0) {
1340 unsigned c = *name++ - '0';
1341 if (c > 9)
1342 goto out;
1343 if (n >= (~0U-9)/10)
1344 goto out;
1345 n *= 10;
1346 n += c;
1347 }
1348 return n;
1349out:
1350 return ~0U;
1351}
1352
Miklos Szeredi27932742007-05-08 00:26:17 -07001353#define PROC_FDINFO_MAX 64
1354
1355static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1356 struct vfsmount **mnt, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001357{
1358 struct task_struct *task = get_proc_task(inode);
1359 struct files_struct *files = NULL;
1360 struct file *file;
1361 int fd = proc_fd(inode);
1362
1363 if (task) {
1364 files = get_files_struct(task);
1365 put_task_struct(task);
1366 }
1367 if (files) {
1368 /*
1369 * We are not taking a ref to the file structure, so we must
1370 * hold ->file_lock.
1371 */
1372 spin_lock(&files->file_lock);
1373 file = fcheck_files(files, fd);
1374 if (file) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001375 if (mnt)
1376 *mnt = mntget(file->f_path.mnt);
1377 if (dentry)
1378 *dentry = dget(file->f_path.dentry);
1379 if (info)
1380 snprintf(info, PROC_FDINFO_MAX,
1381 "pos:\t%lli\n"
1382 "flags:\t0%o\n",
1383 (long long) file->f_pos,
1384 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001385 spin_unlock(&files->file_lock);
1386 put_files_struct(files);
1387 return 0;
1388 }
1389 spin_unlock(&files->file_lock);
1390 put_files_struct(files);
1391 }
1392 return -ENOENT;
1393}
1394
Miklos Szeredi27932742007-05-08 00:26:17 -07001395static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1396 struct vfsmount **mnt)
1397{
1398 return proc_fd_info(inode, dentry, mnt, NULL);
1399}
1400
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001401static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1402{
1403 struct inode *inode = dentry->d_inode;
1404 struct task_struct *task = get_proc_task(inode);
1405 int fd = proc_fd(inode);
1406 struct files_struct *files;
1407
1408 if (task) {
1409 files = get_files_struct(task);
1410 if (files) {
1411 rcu_read_lock();
1412 if (fcheck_files(files, fd)) {
1413 rcu_read_unlock();
1414 put_files_struct(files);
1415 if (task_dumpable(task)) {
1416 inode->i_uid = task->euid;
1417 inode->i_gid = task->egid;
1418 } else {
1419 inode->i_uid = 0;
1420 inode->i_gid = 0;
1421 }
1422 inode->i_mode &= ~(S_ISUID | S_ISGID);
1423 security_task_to_inode(task, inode);
1424 put_task_struct(task);
1425 return 1;
1426 }
1427 rcu_read_unlock();
1428 put_files_struct(files);
1429 }
1430 put_task_struct(task);
1431 }
1432 d_drop(dentry);
1433 return 0;
1434}
1435
1436static struct dentry_operations tid_fd_dentry_operations =
1437{
1438 .d_revalidate = tid_fd_revalidate,
1439 .d_delete = pid_delete_dentry,
1440};
1441
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001442static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001443 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001444{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001445 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001446 struct file *file;
1447 struct files_struct *files;
1448 struct inode *inode;
1449 struct proc_inode *ei;
1450 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001451
Eric W. Biederman61a28782006-10-02 02:18:49 -07001452 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001453 if (!inode)
1454 goto out;
1455 ei = PROC_I(inode);
1456 ei->fd = fd;
1457 files = get_files_struct(task);
1458 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001459 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001460 inode->i_mode = S_IFLNK;
1461
1462 /*
1463 * We are not taking a ref to the file structure, so we must
1464 * hold ->file_lock.
1465 */
1466 spin_lock(&files->file_lock);
1467 file = fcheck_files(files, fd);
1468 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001469 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001470 if (file->f_mode & 1)
1471 inode->i_mode |= S_IRUSR | S_IXUSR;
1472 if (file->f_mode & 2)
1473 inode->i_mode |= S_IWUSR | S_IXUSR;
1474 spin_unlock(&files->file_lock);
1475 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001476
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001477 inode->i_op = &proc_pid_link_inode_operations;
1478 inode->i_size = 64;
1479 ei->op.proc_get_link = proc_fd_link;
1480 dentry->d_op = &tid_fd_dentry_operations;
1481 d_add(dentry, inode);
1482 /* Close the race of the process dying before we return the dentry */
1483 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001484 error = NULL;
1485
1486 out:
1487 return error;
1488out_unlock:
1489 spin_unlock(&files->file_lock);
1490 put_files_struct(files);
1491out_iput:
1492 iput(inode);
1493 goto out;
1494}
1495
Miklos Szeredi27932742007-05-08 00:26:17 -07001496static struct dentry *proc_lookupfd_common(struct inode *dir,
1497 struct dentry *dentry,
1498 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001499{
1500 struct task_struct *task = get_proc_task(dir);
1501 unsigned fd = name_to_int(dentry);
1502 struct dentry *result = ERR_PTR(-ENOENT);
1503
1504 if (!task)
1505 goto out_no_task;
1506 if (fd == ~0U)
1507 goto out;
1508
Miklos Szeredi27932742007-05-08 00:26:17 -07001509 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001510out:
1511 put_task_struct(task);
1512out_no_task:
1513 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001514}
1515
Miklos Szeredi27932742007-05-08 00:26:17 -07001516static int proc_readfd_common(struct file * filp, void * dirent,
1517 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001519 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001520 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001521 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 unsigned int fd, tid, ino;
1523 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001525 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001528 if (!p)
1529 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 retval = 0;
1531 tid = p->pid;
1532
1533 fd = filp->f_pos;
1534 switch (fd) {
1535 case 0:
1536 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1537 goto out;
1538 filp->f_pos++;
1539 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001540 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1542 goto out;
1543 filp->f_pos++;
1544 default:
1545 files = get_files_struct(p);
1546 if (!files)
1547 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001548 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001549 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001551 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001553 char name[PROC_NUMBUF];
1554 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 if (!fcheck_files(files, fd))
1557 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001558 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Miklos Szeredi27932742007-05-08 00:26:17 -07001560 len = snprintf(name, sizeof(name), "%d", fd);
1561 if (proc_fill_cache(filp, dirent, filldir,
1562 name, len, instantiate,
1563 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001564 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 break;
1566 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001567 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001569 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 put_files_struct(files);
1571 }
1572out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001573 put_task_struct(p);
1574out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return retval;
1576}
1577
Miklos Szeredi27932742007-05-08 00:26:17 -07001578static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1579 struct nameidata *nd)
1580{
1581 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1582}
1583
1584static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1585{
1586 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1587}
1588
1589static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1590 size_t len, loff_t *ppos)
1591{
1592 char tmp[PROC_FDINFO_MAX];
1593 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1594 if (!err)
1595 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1596 return err;
1597}
1598
1599static const struct file_operations proc_fdinfo_file_operations = {
1600 .open = nonseekable_open,
1601 .read = proc_fdinfo_read,
1602};
1603
Arjan van de Ven00977a52007-02-12 00:55:34 -08001604static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 .read = generic_read_dir,
1606 .readdir = proc_readfd,
1607};
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001610 * /proc/pid/fd needs a special permission handler so that a process can still
1611 * access /proc/self/fd after it has executed a setuid().
1612 */
1613static int proc_fd_permission(struct inode *inode, int mask,
1614 struct nameidata *nd)
1615{
1616 int rv;
1617
1618 rv = generic_permission(inode, mask, NULL);
1619 if (rv == 0)
1620 return 0;
1621 if (task_pid(current) == proc_pid(inode))
1622 rv = 0;
1623 return rv;
1624}
1625
1626/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * proc directories can do almost nothing..
1628 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001629static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001631 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001632 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633};
1634
Miklos Szeredi27932742007-05-08 00:26:17 -07001635static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1636 struct dentry *dentry, struct task_struct *task, const void *ptr)
1637{
1638 unsigned fd = *(unsigned *)ptr;
1639 struct inode *inode;
1640 struct proc_inode *ei;
1641 struct dentry *error = ERR_PTR(-ENOENT);
1642
1643 inode = proc_pid_make_inode(dir->i_sb, task);
1644 if (!inode)
1645 goto out;
1646 ei = PROC_I(inode);
1647 ei->fd = fd;
1648 inode->i_mode = S_IFREG | S_IRUSR;
1649 inode->i_fop = &proc_fdinfo_file_operations;
1650 dentry->d_op = &tid_fd_dentry_operations;
1651 d_add(dentry, inode);
1652 /* Close the race of the process dying before we return the dentry */
1653 if (tid_fd_revalidate(dentry, NULL))
1654 error = NULL;
1655
1656 out:
1657 return error;
1658}
1659
1660static struct dentry *proc_lookupfdinfo(struct inode *dir,
1661 struct dentry *dentry,
1662 struct nameidata *nd)
1663{
1664 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1665}
1666
1667static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1668{
1669 return proc_readfd_common(filp, dirent, filldir,
1670 proc_fdinfo_instantiate);
1671}
1672
1673static const struct file_operations proc_fdinfo_operations = {
1674 .read = generic_read_dir,
1675 .readdir = proc_readfdinfo,
1676};
1677
1678/*
1679 * proc directories can do almost nothing..
1680 */
1681static const struct inode_operations proc_fdinfo_inode_operations = {
1682 .lookup = proc_lookupfdinfo,
1683 .setattr = proc_setattr,
1684};
1685
1686
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001687static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001688 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001689{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001690 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001691 struct inode *inode;
1692 struct proc_inode *ei;
1693 struct dentry *error = ERR_PTR(-EINVAL);
1694
Eric W. Biederman61a28782006-10-02 02:18:49 -07001695 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001696 if (!inode)
1697 goto out;
1698
1699 ei = PROC_I(inode);
1700 inode->i_mode = p->mode;
1701 if (S_ISDIR(inode->i_mode))
1702 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1703 if (p->iop)
1704 inode->i_op = p->iop;
1705 if (p->fop)
1706 inode->i_fop = p->fop;
1707 ei->op = p->op;
1708 dentry->d_op = &pid_dentry_operations;
1709 d_add(dentry, inode);
1710 /* Close the race of the process dying before we return the dentry */
1711 if (pid_revalidate(dentry, NULL))
1712 error = NULL;
1713out:
1714 return error;
1715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717static struct dentry *proc_pident_lookup(struct inode *dir,
1718 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001719 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001720 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001723 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001724 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001725 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001727 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 inode = NULL;
1729
Eric W. Biederman99f89552006-06-26 00:25:55 -07001730 if (!task)
1731 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001733 /*
1734 * Yes, it does not scale. And it should not. Don't add
1735 * new entries into /proc/<tgid>/ without very good reasons.
1736 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001737 last = &ents[nents - 1];
1738 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (p->len != dentry->d_name.len)
1740 continue;
1741 if (!memcmp(dentry->d_name.name, p->name, p->len))
1742 break;
1743 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001744 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto out;
1746
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001747 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001749 put_task_struct(task);
1750out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001751 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Eric Dumazetc5141e62007-05-08 00:26:15 -07001754static int proc_pident_fill_cache(struct file *filp, void *dirent,
1755 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001756{
1757 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1758 proc_pident_instantiate, task, p);
1759}
1760
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761static int proc_pident_readdir(struct file *filp,
1762 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001763 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001764{
1765 int i;
1766 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001767 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768 struct inode *inode = dentry->d_inode;
1769 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001770 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001771 ino_t ino;
1772 int ret;
1773
1774 ret = -ENOENT;
1775 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001776 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001777
1778 ret = 0;
1779 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001780 i = filp->f_pos;
1781 switch (i) {
1782 case 0:
1783 ino = inode->i_ino;
1784 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1785 goto out;
1786 i++;
1787 filp->f_pos++;
1788 /* fall through */
1789 case 1:
1790 ino = parent_ino(dentry);
1791 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1792 goto out;
1793 i++;
1794 filp->f_pos++;
1795 /* fall through */
1796 default:
1797 i -= 2;
1798 if (i >= nents) {
1799 ret = 1;
1800 goto out;
1801 }
1802 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001803 last = &ents[nents - 1];
1804 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001805 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001806 goto out;
1807 filp->f_pos++;
1808 p++;
1809 }
1810 }
1811
1812 ret = 1;
1813out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001814 put_task_struct(task);
1815out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001820static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1821 size_t count, loff_t *ppos)
1822{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001823 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001824 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001825 ssize_t length;
1826 struct task_struct *task = get_proc_task(inode);
1827
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001828 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001829 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001830
1831 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001832 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001833 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001834 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001835 if (length > 0)
1836 length = simple_read_from_buffer(buf, count, ppos, p, length);
1837 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001838 return length;
1839}
1840
1841static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1842 size_t count, loff_t *ppos)
1843{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001844 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001845 char *page;
1846 ssize_t length;
1847 struct task_struct *task = get_proc_task(inode);
1848
1849 length = -ESRCH;
1850 if (!task)
1851 goto out_no_task;
1852 if (count > PAGE_SIZE)
1853 count = PAGE_SIZE;
1854
1855 /* No partial writes. */
1856 length = -EINVAL;
1857 if (*ppos != 0)
1858 goto out;
1859
1860 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001861 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001862 if (!page)
1863 goto out;
1864
1865 length = -EFAULT;
1866 if (copy_from_user(page, buf, count))
1867 goto out_free;
1868
1869 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001870 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001871 (void*)page, count);
1872out_free:
1873 free_page((unsigned long) page);
1874out:
1875 put_task_struct(task);
1876out_no_task:
1877 return length;
1878}
1879
Arjan van de Ven00977a52007-02-12 00:55:34 -08001880static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001881 .read = proc_pid_attr_read,
1882 .write = proc_pid_attr_write,
1883};
1884
Eric Dumazetc5141e62007-05-08 00:26:15 -07001885static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001886 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1887 REG("prev", S_IRUGO, pid_attr),
1888 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1889 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1890 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1891 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001892};
1893
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001894static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 void * dirent, filldir_t filldir)
1896{
1897 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001898 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Arjan van de Ven00977a52007-02-12 00:55:34 -08001901static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001903 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904};
1905
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001906static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 struct dentry *dentry, struct nameidata *nd)
1908{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001909 return proc_pident_lookup(dir, dentry,
1910 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001913static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001914 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001915 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001916 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917};
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919#endif
1920
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07001921#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1922static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
1923 size_t count, loff_t *ppos)
1924{
1925 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1926 struct mm_struct *mm;
1927 char buffer[PROC_NUMBUF];
1928 size_t len;
1929 int ret;
1930
1931 if (!task)
1932 return -ESRCH;
1933
1934 ret = 0;
1935 mm = get_task_mm(task);
1936 if (mm) {
1937 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
1938 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
1939 MMF_DUMP_FILTER_SHIFT));
1940 mmput(mm);
1941 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
1942 }
1943
1944 put_task_struct(task);
1945
1946 return ret;
1947}
1948
1949static ssize_t proc_coredump_filter_write(struct file *file,
1950 const char __user *buf,
1951 size_t count,
1952 loff_t *ppos)
1953{
1954 struct task_struct *task;
1955 struct mm_struct *mm;
1956 char buffer[PROC_NUMBUF], *end;
1957 unsigned int val;
1958 int ret;
1959 int i;
1960 unsigned long mask;
1961
1962 ret = -EFAULT;
1963 memset(buffer, 0, sizeof(buffer));
1964 if (count > sizeof(buffer) - 1)
1965 count = sizeof(buffer) - 1;
1966 if (copy_from_user(buffer, buf, count))
1967 goto out_no_task;
1968
1969 ret = -EINVAL;
1970 val = (unsigned int)simple_strtoul(buffer, &end, 0);
1971 if (*end == '\n')
1972 end++;
1973 if (end - buffer == 0)
1974 goto out_no_task;
1975
1976 ret = -ESRCH;
1977 task = get_proc_task(file->f_dentry->d_inode);
1978 if (!task)
1979 goto out_no_task;
1980
1981 ret = end - buffer;
1982 mm = get_task_mm(task);
1983 if (!mm)
1984 goto out_no_mm;
1985
1986 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
1987 if (val & mask)
1988 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1989 else
1990 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1991 }
1992
1993 mmput(mm);
1994 out_no_mm:
1995 put_task_struct(task);
1996 out_no_task:
1997 return ret;
1998}
1999
2000static const struct file_operations proc_coredump_filter_operations = {
2001 .read = proc_coredump_filter_read,
2002 .write = proc_coredump_filter_write,
2003};
2004#endif
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006/*
2007 * /proc/self:
2008 */
2009static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2010 int buflen)
2011{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002012 char tmp[PROC_NUMBUF];
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002013 sprintf(tmp, "%d", task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return vfs_readlink(dentry,buffer,buflen,tmp);
2015}
2016
Al Viro008b1502005-08-20 00:17:39 +01002017static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002019 char tmp[PROC_NUMBUF];
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002020 sprintf(tmp, "%d", task_tgid_vnr(current));
Al Viro008b1502005-08-20 00:17:39 +01002021 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002022}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002024static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 .readlink = proc_self_readlink,
2026 .follow_link = proc_self_follow_link,
2027};
2028
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002029/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002030 * proc base
2031 *
2032 * These are the directory entries in the root directory of /proc
2033 * that properly belong to the /proc filesystem, as they describe
2034 * describe something that is process related.
2035 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002036static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002037 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002038 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002039};
2040
2041/*
2042 * Exceptional case: normally we are not allowed to unhash a busy
2043 * directory. In this case, however, we can do it - no aliasing problems
2044 * due to the way we treat inodes.
2045 */
2046static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2047{
2048 struct inode *inode = dentry->d_inode;
2049 struct task_struct *task = get_proc_task(inode);
2050 if (task) {
2051 put_task_struct(task);
2052 return 1;
2053 }
2054 d_drop(dentry);
2055 return 0;
2056}
2057
2058static struct dentry_operations proc_base_dentry_operations =
2059{
2060 .d_revalidate = proc_base_revalidate,
2061 .d_delete = pid_delete_dentry,
2062};
2063
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002064static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002065 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002066{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002067 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002068 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002069 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002070 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002071
2072 /* Allocate the inode */
2073 error = ERR_PTR(-ENOMEM);
2074 inode = new_inode(dir->i_sb);
2075 if (!inode)
2076 goto out;
2077
2078 /* Initialize the inode */
2079 ei = PROC_I(inode);
2080 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002081
2082 /*
2083 * grab the reference to the task.
2084 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002085 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002086 if (!ei->pid)
2087 goto out_iput;
2088
2089 inode->i_uid = 0;
2090 inode->i_gid = 0;
2091 inode->i_mode = p->mode;
2092 if (S_ISDIR(inode->i_mode))
2093 inode->i_nlink = 2;
2094 if (S_ISLNK(inode->i_mode))
2095 inode->i_size = 64;
2096 if (p->iop)
2097 inode->i_op = p->iop;
2098 if (p->fop)
2099 inode->i_fop = p->fop;
2100 ei->op = p->op;
2101 dentry->d_op = &proc_base_dentry_operations;
2102 d_add(dentry, inode);
2103 error = NULL;
2104out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002105 return error;
2106out_iput:
2107 iput(inode);
2108 goto out;
2109}
2110
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002111static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2112{
2113 struct dentry *error;
2114 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002115 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002116
2117 error = ERR_PTR(-ENOENT);
2118
2119 if (!task)
2120 goto out_no_task;
2121
2122 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002123 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2124 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002125 if (p->len != dentry->d_name.len)
2126 continue;
2127 if (!memcmp(dentry->d_name.name, p->name, p->len))
2128 break;
2129 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002130 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002131 goto out;
2132
2133 error = proc_base_instantiate(dir, dentry, task, p);
2134
2135out:
2136 put_task_struct(task);
2137out_no_task:
2138 return error;
2139}
2140
Eric Dumazetc5141e62007-05-08 00:26:15 -07002141static int proc_base_fill_cache(struct file *filp, void *dirent,
2142 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002143{
2144 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2145 proc_base_instantiate, task, p);
2146}
2147
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002148#ifdef CONFIG_TASK_IO_ACCOUNTING
2149static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2150{
2151 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002152#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002153 "rchar: %llu\n"
2154 "wchar: %llu\n"
2155 "syscr: %llu\n"
2156 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002157#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002158 "read_bytes: %llu\n"
2159 "write_bytes: %llu\n"
2160 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002161#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002162 (unsigned long long)task->rchar,
2163 (unsigned long long)task->wchar,
2164 (unsigned long long)task->syscr,
2165 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002166#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002167 (unsigned long long)task->ioac.read_bytes,
2168 (unsigned long long)task->ioac.write_bytes,
2169 (unsigned long long)task->ioac.cancelled_write_bytes);
2170}
2171#endif
2172
Eric W. Biederman801199c2006-10-02 02:18:48 -07002173/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002174 * Thread groups
2175 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002176static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002177static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002178
Eric Dumazetc5141e62007-05-08 00:26:15 -07002179static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002180 DIR("task", S_IRUGO|S_IXUGO, task),
2181 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002182 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002183 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002184 INF("auxv", S_IRUSR, pid_auxv),
2185 INF("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002186 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002187#ifdef CONFIG_SCHED_DEBUG
2188 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2189#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002190 INF("cmdline", S_IRUGO, pid_cmdline),
2191 INF("stat", S_IRUGO, tgid_stat),
2192 INF("statm", S_IRUGO, pid_statm),
2193 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002194#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002195 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002196#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002197 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002198 LNK("cwd", cwd),
2199 LNK("root", root),
2200 LNK("exe", exe),
2201 REG("mounts", S_IRUGO, mounts),
2202 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002203#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002204 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002205 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002206#endif
2207#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002208 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002209#endif
2210#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002211 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002212#endif
2213#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002214 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002215#endif
Paul Menage8793d852007-10-18 23:39:39 -07002216#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002217 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002218#endif
Paul Menagea4243162007-10-18 23:39:35 -07002219#ifdef CONFIG_CGROUPS
2220 REG("cgroup", S_IRUGO, cgroup),
2221#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002222 INF("oom_score", S_IRUGO, oom_score),
2223 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002224#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002225 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002226#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002227#ifdef CONFIG_FAULT_INJECTION
2228 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2229#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002230#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2231 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2232#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002233#ifdef CONFIG_TASK_IO_ACCOUNTING
2234 INF("io", S_IRUGO, pid_io_accounting),
2235#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002236};
2237
2238static int proc_tgid_base_readdir(struct file * filp,
2239 void * dirent, filldir_t filldir)
2240{
2241 return proc_pident_readdir(filp,dirent,filldir,
2242 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2243}
2244
Arjan van de Ven00977a52007-02-12 00:55:34 -08002245static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002246 .read = generic_read_dir,
2247 .readdir = proc_tgid_base_readdir,
2248};
2249
2250static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002251 return proc_pident_lookup(dir, dentry,
2252 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002253}
2254
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002255static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002256 .lookup = proc_tgid_base_lookup,
2257 .getattr = pid_getattr,
2258 .setattr = proc_setattr,
2259};
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07002262 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002264 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07002266 * Looks in the dcache for
2267 * /proc/@pid
2268 * /proc/@tgid/task/@pid
2269 * if either directory is present flushes it and all of it'ts children
2270 * from the dcache.
2271 *
2272 * It is safe and reasonable to cache /proc entries for a task until
2273 * that task exits. After that they just clog up the dcache with
2274 * useless entries, possibly causing useful dcache entries to be
2275 * flushed instead. This routine is proved to flush those useless
2276 * dcache entries at process exit time.
2277 *
2278 * NOTE: This routine is just an optimization so it does not guarantee
2279 * that no dcache entries will exist at process exit time it
2280 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002282static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002284 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002285 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002286 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Eric W. Biederman48e64842006-06-26 00:25:48 -07002288 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002289 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2290 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002291 if (dentry) {
2292 shrink_dcache_parent(dentry);
2293 d_drop(dentry);
2294 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002297 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002298 goto out;
2299
2300 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002301 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2302 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002303 if (!leader)
2304 goto out;
2305
2306 name.name = "task";
2307 name.len = strlen(name.name);
2308 dir = d_hash_and_lookup(leader, &name);
2309 if (!dir)
2310 goto out_put_leader;
2311
2312 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002313 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002314 dentry = d_hash_and_lookup(dir, &name);
2315 if (dentry) {
2316 shrink_dcache_parent(dentry);
2317 d_drop(dentry);
2318 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002320
2321 dput(dir);
2322out_put_leader:
2323 dput(leader);
2324out:
2325 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002328/*
2329 * when flushing dentries from proc one need to flush them from global
2330 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2331 * in. this call is supposed to make all this job.
2332 */
2333
2334void proc_flush_task(struct task_struct *task)
2335{
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002336 int i, leader;
2337 struct pid *pid, *tgid;
2338 struct upid *upid;
2339
2340 leader = thread_group_leader(task);
2341 proc_flush_task_mnt(proc_mnt, task->pid, leader ? task->tgid : 0);
2342 pid = task_pid(task);
2343 if (pid->level == 0)
2344 return;
2345
2346 tgid = task_tgid(task);
2347 for (i = 1; i <= pid->level; i++) {
2348 upid = &pid->numbers[i];
2349 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2350 leader ? 0 : tgid->numbers[i].nr);
2351 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002352
2353 upid = &pid->numbers[pid->level];
2354 if (upid->nr == 1)
2355 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002356}
2357
Adrian Bunk9711ef92006-12-06 20:38:31 -08002358static struct dentry *proc_pid_instantiate(struct inode *dir,
2359 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002360 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002361{
2362 struct dentry *error = ERR_PTR(-ENOENT);
2363 struct inode *inode;
2364
Eric W. Biederman61a28782006-10-02 02:18:49 -07002365 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002366 if (!inode)
2367 goto out;
2368
2369 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2370 inode->i_op = &proc_tgid_base_inode_operations;
2371 inode->i_fop = &proc_tgid_base_operations;
2372 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002373 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002374#ifdef CONFIG_SECURITY
2375 inode->i_nlink += 1;
2376#endif
2377
2378 dentry->d_op = &pid_dentry_operations;
2379
2380 d_add(dentry, inode);
2381 /* Close the race of the process dying before we return the dentry */
2382 if (pid_revalidate(dentry, NULL))
2383 error = NULL;
2384out:
2385 return error;
2386}
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2389{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002390 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002393 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Eric W. Biederman801199c2006-10-02 02:18:48 -07002395 result = proc_base_lookup(dir, dentry);
2396 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2397 goto out;
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 tgid = name_to_int(dentry);
2400 if (tgid == ~0U)
2401 goto out;
2402
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002403 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002404 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002405 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (task)
2407 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002408 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (!task)
2410 goto out;
2411
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002412 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002413 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002415 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002419 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002420 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002422static struct task_struct *next_tgid(unsigned int tgid,
2423 struct pid_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002425 struct task_struct *task;
2426 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002428 rcu_read_lock();
2429retry:
2430 task = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002431 pid = find_ge_pid(tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002432 if (pid) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002433 tgid = pid_nr_ns(pid, ns) + 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002434 task = pid_task(pid, PIDTYPE_PID);
2435 /* What we to know is if the pid we have find is the
2436 * pid of a thread_group_leader. Testing for task
2437 * being a thread_group_leader is the obvious thing
2438 * todo but there is a window when it fails, due to
2439 * the pid transfer logic in de_thread.
2440 *
2441 * So we perform the straight forward test of seeing
2442 * if the pid we have found is the pid of a thread
2443 * group leader, and don't worry if the task we have
2444 * found doesn't happen to be a thread group leader.
2445 * As we don't care in the case of readdir.
2446 */
2447 if (!task || !has_group_leader_pid(task))
2448 goto retry;
2449 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002451 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002452 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002455#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Eric W. Biederman61a28782006-10-02 02:18:49 -07002457static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2458 struct task_struct *task, int tgid)
2459{
2460 char name[PROC_NUMBUF];
2461 int len = snprintf(name, sizeof(name), "%d", tgid);
2462 return proc_fill_cache(filp, dirent, filldir, name, len,
2463 proc_pid_instantiate, task, NULL);
2464}
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466/* for the /proc/ directory itself, after non-process stuff has been done */
2467int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2468{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002470 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002471 struct task_struct *task;
2472 int tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002473 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Eric W. Biederman61a28782006-10-02 02:18:49 -07002475 if (!reaper)
2476 goto out_no_task;
2477
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002478 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002479 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002480 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002484 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002485 tgid = filp->f_pos - TGID_OFFSET;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002486 for (task = next_tgid(tgid, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002487 task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002488 put_task_struct(task), task = next_tgid(tgid + 1, ns)) {
2489 tgid = task_pid_nr_ns(task, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002490 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002491 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002492 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002493 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002496 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2497out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002498 put_task_struct(reaper);
2499out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501}
2502
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002503/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002504 * Tasks
2505 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002506static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002507 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002508 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002509 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002510 INF("auxv", S_IRUSR, pid_auxv),
2511 INF("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002512 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002513#ifdef CONFIG_SCHED_DEBUG
2514 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2515#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002516 INF("cmdline", S_IRUGO, pid_cmdline),
2517 INF("stat", S_IRUGO, tid_stat),
2518 INF("statm", S_IRUGO, pid_statm),
2519 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002520#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002521 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002522#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002523 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002524 LNK("cwd", cwd),
2525 LNK("root", root),
2526 LNK("exe", exe),
2527 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002528#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002529 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002530 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531#endif
2532#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002533 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002534#endif
2535#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002536 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002537#endif
2538#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002539 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002540#endif
Paul Menage8793d852007-10-18 23:39:39 -07002541#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002542 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002543#endif
Paul Menagea4243162007-10-18 23:39:35 -07002544#ifdef CONFIG_CGROUPS
2545 REG("cgroup", S_IRUGO, cgroup),
2546#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002547 INF("oom_score", S_IRUGO, oom_score),
2548 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002549#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002550 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002552#ifdef CONFIG_FAULT_INJECTION
2553 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2554#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002555};
2556
2557static int proc_tid_base_readdir(struct file * filp,
2558 void * dirent, filldir_t filldir)
2559{
2560 return proc_pident_readdir(filp,dirent,filldir,
2561 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2562}
2563
2564static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002565 return proc_pident_lookup(dir, dentry,
2566 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002567}
2568
Arjan van de Ven00977a52007-02-12 00:55:34 -08002569static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002570 .read = generic_read_dir,
2571 .readdir = proc_tid_base_readdir,
2572};
2573
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002574static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002575 .lookup = proc_tid_base_lookup,
2576 .getattr = pid_getattr,
2577 .setattr = proc_setattr,
2578};
2579
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002580static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002581 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002582{
2583 struct dentry *error = ERR_PTR(-ENOENT);
2584 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002585 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002586
2587 if (!inode)
2588 goto out;
2589 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2590 inode->i_op = &proc_tid_base_inode_operations;
2591 inode->i_fop = &proc_tid_base_operations;
2592 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002593 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002594#ifdef CONFIG_SECURITY
2595 inode->i_nlink += 1;
2596#endif
2597
2598 dentry->d_op = &pid_dentry_operations;
2599
2600 d_add(dentry, inode);
2601 /* Close the race of the process dying before we return the dentry */
2602 if (pid_revalidate(dentry, NULL))
2603 error = NULL;
2604out:
2605 return error;
2606}
2607
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002608static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2609{
2610 struct dentry *result = ERR_PTR(-ENOENT);
2611 struct task_struct *task;
2612 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002613 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002614 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002615
2616 if (!leader)
2617 goto out_no_task;
2618
2619 tid = name_to_int(dentry);
2620 if (tid == ~0U)
2621 goto out;
2622
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002623 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002624 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002625 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002626 if (task)
2627 get_task_struct(task);
2628 rcu_read_unlock();
2629 if (!task)
2630 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002631 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002632 goto out_drop_task;
2633
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002634 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002635out_drop_task:
2636 put_task_struct(task);
2637out:
2638 put_task_struct(leader);
2639out_no_task:
2640 return result;
2641}
2642
2643/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002644 * Find the first tid of a thread group to return to user space.
2645 *
2646 * Usually this is just the thread group leader, but if the users
2647 * buffer was too small or there was a seek into the middle of the
2648 * directory we have more work todo.
2649 *
2650 * In the case of a short read we start with find_task_by_pid.
2651 *
2652 * In the case of a seek we start with the leader and walk nr
2653 * threads past it.
2654 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002655static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002656 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002657{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002658 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002659
Eric W. Biedermancc288732006-06-26 00:26:01 -07002660 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002661 /* Attempt to start with the pid of a thread */
2662 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002663 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002664 if (pos && (pos->group_leader == leader))
2665 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002666 }
2667
2668 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002669 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002670 if (nr && nr >= get_nr_threads(leader))
2671 goto out;
2672
2673 /* If we haven't found our starting place yet start
2674 * with the leader and walk nr threads forward.
2675 */
2676 for (pos = leader; nr > 0; --nr) {
2677 pos = next_thread(pos);
2678 if (pos == leader) {
2679 pos = NULL;
2680 goto out;
2681 }
2682 }
2683found:
2684 get_task_struct(pos);
2685out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002686 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002687 return pos;
2688}
2689
2690/*
2691 * Find the next thread in the thread list.
2692 * Return NULL if there is an error or no next thread.
2693 *
2694 * The reference to the input task_struct is released.
2695 */
2696static struct task_struct *next_tid(struct task_struct *start)
2697{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002698 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002699 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002700 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002701 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002702 if (thread_group_leader(pos))
2703 pos = NULL;
2704 else
2705 get_task_struct(pos);
2706 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002707 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002708 put_task_struct(start);
2709 return pos;
2710}
2711
Eric W. Biederman61a28782006-10-02 02:18:49 -07002712static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2713 struct task_struct *task, int tid)
2714{
2715 char name[PROC_NUMBUF];
2716 int len = snprintf(name, sizeof(name), "%d", tid);
2717 return proc_fill_cache(filp, dirent, filldir, name, len,
2718 proc_task_instantiate, task, NULL);
2719}
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721/* for the /proc/TGID/task/ directories */
2722static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2723{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002724 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002726 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002727 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 int retval = -ENOENT;
2729 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002730 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002732 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002734 task = get_proc_task(inode);
2735 if (!task)
2736 goto out_no_task;
2737 rcu_read_lock();
2738 if (pid_alive(task)) {
2739 leader = task->group_leader;
2740 get_task_struct(leader);
2741 }
2742 rcu_read_unlock();
2743 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002744 if (!leader)
2745 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 retval = 0;
2747
2748 switch (pos) {
2749 case 0:
2750 ino = inode->i_ino;
2751 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2752 goto out;
2753 pos++;
2754 /* fall through */
2755 case 1:
2756 ino = parent_ino(dentry);
2757 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2758 goto out;
2759 pos++;
2760 /* fall through */
2761 }
2762
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002763 /* f_version caches the tgid value that the last readdir call couldn't
2764 * return. lseek aka telldir automagically resets f_version to 0.
2765 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002766 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002767 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002768 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002769 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002770 task;
2771 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002772 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002773 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002774 /* returning this tgid failed, save it as the first
2775 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002776 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002777 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
2781out:
2782 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002783 put_task_struct(leader);
2784out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return retval;
2786}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002787
2788static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2789{
2790 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002791 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002792 generic_fillattr(inode, stat);
2793
Eric W. Biederman99f89552006-06-26 00:25:55 -07002794 if (p) {
2795 rcu_read_lock();
2796 stat->nlink += get_nr_threads(p);
2797 rcu_read_unlock();
2798 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002799 }
2800
2801 return 0;
2802}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002803
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002804static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002805 .lookup = proc_task_lookup,
2806 .getattr = proc_task_getattr,
2807 .setattr = proc_setattr,
2808};
2809
Arjan van de Ven00977a52007-02-12 00:55:34 -08002810static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002811 .read = generic_read_dir,
2812 .readdir = proc_task_readdir,
2813};