xref: /openbmc/linux/fs/proc/base.c (revision 7dc52157982ab771f40e3c0b7dc55b954c3c2d19)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/proc/base.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992 Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  proc base directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
91da177e4SLinus Torvalds  *  Instead of using magical inumbers to determine the kind of object
101da177e4SLinus Torvalds  *  we allocate and fill in-core inodes upon lookup. They don't even
111da177e4SLinus Torvalds  *  go into icache. We cache the reference to task_struct upon lookup too.
121da177e4SLinus Torvalds  *  Eventually it should become a filesystem in its own. We don't use the
131da177e4SLinus Torvalds  *  rest of procfs anymore.
14e070ad49SMauricio Lin  *
15e070ad49SMauricio Lin  *
16e070ad49SMauricio Lin  *  Changelog:
17e070ad49SMauricio Lin  *  17-Jan-2005
18e070ad49SMauricio Lin  *  Allan Bezerra
19e070ad49SMauricio Lin  *  Bruna Moreira <bruna.moreira@indt.org.br>
20e070ad49SMauricio Lin  *  Edjard Mota <edjard.mota@indt.org.br>
21e070ad49SMauricio Lin  *  Ilias Biris <ilias.biris@indt.org.br>
22e070ad49SMauricio Lin  *  Mauricio Lin <mauricio.lin@indt.org.br>
23e070ad49SMauricio Lin  *
24e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25e070ad49SMauricio Lin  *
26e070ad49SMauricio Lin  *  A new process specific entry (smaps) included in /proc. It shows the
27e070ad49SMauricio Lin  *  size of rss for each memory area. The maps entry lacks information
28e070ad49SMauricio Lin  *  about physical memory size (rss) for each mapped file, i.e.,
29e070ad49SMauricio Lin  *  rss information for executables and library files.
30e070ad49SMauricio Lin  *  This additional information is useful for any tools that need to know
31e070ad49SMauricio Lin  *  about physical memory consumption for a process specific library.
32e070ad49SMauricio Lin  *
33e070ad49SMauricio Lin  *  Changelog:
34e070ad49SMauricio Lin  *  21-Feb-2005
35e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36e070ad49SMauricio Lin  *  Pud inclusion in the page table walking.
37e070ad49SMauricio Lin  *
38e070ad49SMauricio Lin  *  ChangeLog:
39e070ad49SMauricio Lin  *  10-Mar-2005
40e070ad49SMauricio Lin  *  10LE Instituto Nokia de Tecnologia - INdT:
41e070ad49SMauricio Lin  *  A better way to walks through the page table as suggested by Hugh Dickins.
42e070ad49SMauricio Lin  *
43e070ad49SMauricio Lin  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44e070ad49SMauricio Lin  *  Smaps information related to shared, private, clean and dirty pages.
45e070ad49SMauricio Lin  *
46e070ad49SMauricio Lin  *  Paul Mundt <paul.mundt@nokia.com>:
47e070ad49SMauricio Lin  *  Overall revision about smaps.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <asm/uaccess.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <linux/errno.h>
531da177e4SLinus Torvalds #include <linux/time.h>
541da177e4SLinus Torvalds #include <linux/proc_fs.h>
551da177e4SLinus Torvalds #include <linux/stat.h>
565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h>
571da177e4SLinus Torvalds #include <linux/init.h>
5816f7e0feSRandy Dunlap #include <linux/capability.h>
591da177e4SLinus Torvalds #include <linux/file.h>
609f3acc31SAl Viro #include <linux/fdtable.h>
611da177e4SLinus Torvalds #include <linux/string.h>
621da177e4SLinus Torvalds #include <linux/seq_file.h>
631da177e4SLinus Torvalds #include <linux/namei.h>
646b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
651da177e4SLinus Torvalds #include <linux/mm.h>
66b835996fSDipankar Sarma #include <linux/rcupdate.h>
671da177e4SLinus Torvalds #include <linux/kallsyms.h>
682ec220e2SKen Chen #include <linux/stacktrace.h>
69d85f50d5SNeil Horman #include <linux/resource.h>
705096add8SKees Cook #include <linux/module.h>
711da177e4SLinus Torvalds #include <linux/mount.h>
721da177e4SLinus Torvalds #include <linux/security.h>
731da177e4SLinus Torvalds #include <linux/ptrace.h>
740d094efeSRoland McGrath #include <linux/tracehook.h>
75a424316cSPaul Menage #include <linux/cgroup.h>
761da177e4SLinus Torvalds #include <linux/cpuset.h>
771da177e4SLinus Torvalds #include <linux/audit.h>
785addc5ddSAl Viro #include <linux/poll.h>
791651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
808ac773b4SAlexey Dobriyan #include <linux/oom.h>
813cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8260347f67SPavel Emelyanov #include <linux/pid_namespace.h>
835ad4e53bSAl Viro #include <linux/fs_struct.h>
841da177e4SLinus Torvalds #include "internal.h"
851da177e4SLinus Torvalds 
860f2fe20fSEric W. Biederman /* NOTE:
870f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
880f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
890f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
900f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
910f2fe20fSEric W. Biederman  *
920f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
930f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
940f2fe20fSEric W. Biederman  */
950f2fe20fSEric W. Biederman 
961da177e4SLinus Torvalds struct pid_entry {
971da177e4SLinus Torvalds 	char *name;
98c5141e6dSEric Dumazet 	int len;
991da177e4SLinus Torvalds 	mode_t mode;
100c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
10100977a59SArjan van de Ven 	const struct file_operations *fop;
10220cdc894SEric W. Biederman 	union proc_op op;
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
10561a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
10620cdc894SEric W. Biederman 	.name = (NAME),					\
107c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
10820cdc894SEric W. Biederman 	.mode = MODE,					\
10920cdc894SEric W. Biederman 	.iop  = IOP,					\
11020cdc894SEric W. Biederman 	.fop  = FOP,					\
11120cdc894SEric W. Biederman 	.op   = OP,					\
11220cdc894SEric W. Biederman }
11320cdc894SEric W. Biederman 
114631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
115631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
116631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
11761a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
11820cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
119631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
120631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
121631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
122631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
12361a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
12420cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
125631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
126631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
127be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
128be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
129631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1301da177e4SLinus Torvalds 
131aed54175SVegard Nossum /*
132aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
133aed54175SVegard Nossum  * and .. links.
134aed54175SVegard Nossum  */
135aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
136aed54175SVegard Nossum 	unsigned int n)
137aed54175SVegard Nossum {
138aed54175SVegard Nossum 	unsigned int i;
139aed54175SVegard Nossum 	unsigned int count;
140aed54175SVegard Nossum 
141aed54175SVegard Nossum 	count = 0;
142aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
143aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
144aed54175SVegard Nossum 			++count;
145aed54175SVegard Nossum 	}
146aed54175SVegard Nossum 
147aed54175SVegard Nossum 	return count;
148aed54175SVegard Nossum }
149aed54175SVegard Nossum 
1507c2c7d99SHugh Dickins static int get_fs_path(struct task_struct *task, struct path *path, bool root)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	struct fs_struct *fs;
1537c2c7d99SHugh Dickins 	int result = -ENOENT;
1547c2c7d99SHugh Dickins 
1550494f6ecSMiklos Szeredi 	task_lock(task);
1560494f6ecSMiklos Szeredi 	fs = task->fs;
1577c2c7d99SHugh Dickins 	if (fs) {
1587c2c7d99SHugh Dickins 		read_lock(&fs->lock);
1597c2c7d99SHugh Dickins 		*path = root ? fs->root : fs->pwd;
1607c2c7d99SHugh Dickins 		path_get(path);
1617c2c7d99SHugh Dickins 		read_unlock(&fs->lock);
1627c2c7d99SHugh Dickins 		result = 0;
1637c2c7d99SHugh Dickins 	}
1640494f6ecSMiklos Szeredi 	task_unlock(task);
1657c2c7d99SHugh Dickins 	return result;
1660494f6ecSMiklos Szeredi }
1670494f6ecSMiklos Szeredi 
16899f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk)
16999f89551SEric W. Biederman {
17099f89551SEric W. Biederman 	unsigned long flags;
17199f89551SEric W. Biederman 	int count = 0;
17299f89551SEric W. Biederman 
17399f89551SEric W. Biederman 	if (lock_task_sighand(tsk, &flags)) {
17499f89551SEric W. Biederman 		count = atomic_read(&tsk->signal->count);
17599f89551SEric W. Biederman 		unlock_task_sighand(tsk, &flags);
17699f89551SEric W. Biederman 	}
17799f89551SEric W. Biederman 	return count;
17899f89551SEric W. Biederman }
17999f89551SEric W. Biederman 
1803dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path)
1810494f6ecSMiklos Szeredi {
18299f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1830494f6ecSMiklos Szeredi 	int result = -ENOENT;
18499f89551SEric W. Biederman 
18599f89551SEric W. Biederman 	if (task) {
1867c2c7d99SHugh Dickins 		result = get_fs_path(task, path, 0);
18799f89551SEric W. Biederman 		put_task_struct(task);
18899f89551SEric W. Biederman 	}
1891da177e4SLinus Torvalds 	return result;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1923dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path)
1931da177e4SLinus Torvalds {
19499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1951da177e4SLinus Torvalds 	int result = -ENOENT;
19699f89551SEric W. Biederman 
19799f89551SEric W. Biederman 	if (task) {
1987c2c7d99SHugh Dickins 		result = get_fs_path(task, path, 1);
19999f89551SEric W. Biederman 		put_task_struct(task);
20099f89551SEric W. Biederman 	}
2011da177e4SLinus Torvalds 	return result;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
204638fa202SRoland McGrath /*
205638fa202SRoland McGrath  * Return zero if current may access user memory in @task, -error if not.
206638fa202SRoland McGrath  */
207638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task)
208638fa202SRoland McGrath {
209638fa202SRoland McGrath 	/*
210638fa202SRoland McGrath 	 * A task can always look at itself, in case it chooses
211638fa202SRoland McGrath 	 * to use system calls instead of load instructions.
212638fa202SRoland McGrath 	 */
213638fa202SRoland McGrath 	if (task == current)
214638fa202SRoland McGrath 		return 0;
215638fa202SRoland McGrath 
216638fa202SRoland McGrath 	/*
217638fa202SRoland McGrath 	 * If current is actively ptrace'ing, and would also be
218638fa202SRoland McGrath 	 * permitted to freshly attach with ptrace now, permit it.
219638fa202SRoland McGrath 	 */
2200d094efeSRoland McGrath 	if (task_is_stopped_or_traced(task)) {
2210d094efeSRoland McGrath 		int match;
2220d094efeSRoland McGrath 		rcu_read_lock();
2230d094efeSRoland McGrath 		match = (tracehook_tracer_task(task) == current);
2240d094efeSRoland McGrath 		rcu_read_unlock();
2250d094efeSRoland McGrath 		if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
226638fa202SRoland McGrath 			return 0;
2270d094efeSRoland McGrath 	}
228638fa202SRoland McGrath 
229638fa202SRoland McGrath 	/*
230638fa202SRoland McGrath 	 * Noone else is allowed.
231638fa202SRoland McGrath 	 */
232638fa202SRoland McGrath 	return -EPERM;
233638fa202SRoland McGrath }
2341da177e4SLinus Torvalds 
235831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task)
236831830b5SAl Viro {
237704b836cSOleg Nesterov 	struct mm_struct *mm;
23800f89d21SOleg Nesterov 
239704b836cSOleg Nesterov 	if (mutex_lock_killable(&task->cred_guard_mutex))
240704b836cSOleg Nesterov 		return NULL;
241704b836cSOleg Nesterov 
242704b836cSOleg Nesterov 	mm = get_task_mm(task);
243704b836cSOleg Nesterov 	if (mm && mm != current->mm &&
244704b836cSOleg Nesterov 			!ptrace_may_access(task, PTRACE_MODE_READ)) {
245831830b5SAl Viro 		mmput(mm);
24600f89d21SOleg Nesterov 		mm = NULL;
247831830b5SAl Viro 	}
248704b836cSOleg Nesterov 	mutex_unlock(&task->cred_guard_mutex);
249704b836cSOleg Nesterov 
25013f0feafSOleg Nesterov 	return mm;
25113f0feafSOleg Nesterov }
252831830b5SAl Viro 
2531da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	int res = 0;
2561da177e4SLinus Torvalds 	unsigned int len;
2571da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2581da177e4SLinus Torvalds 	if (!mm)
2591da177e4SLinus Torvalds 		goto out;
2601da177e4SLinus Torvalds 	if (!mm->arg_end)
2611da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2661da177e4SLinus Torvalds 		len = PAGE_SIZE;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2711da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2721da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2731da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2741da177e4SLinus Torvalds 		if (len < res) {
2751da177e4SLinus Torvalds 		    res = len;
2761da177e4SLinus Torvalds 		} else {
2771da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2781da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2791da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2801da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2811da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds out_mm:
2851da177e4SLinus Torvalds 	mmput(mm);
2861da177e4SLinus Torvalds out:
2871da177e4SLinus Torvalds 	return res;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	int res = 0;
2931da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2941da177e4SLinus Torvalds 	if (mm) {
2951da177e4SLinus Torvalds 		unsigned int nwords = 0;
296dfe6b7d9SHannes Eder 		do {
2971da177e4SLinus Torvalds 			nwords += 2;
298dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2991da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
3001da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
3011da177e4SLinus Torvalds 			res = PAGE_SIZE;
3021da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
3031da177e4SLinus Torvalds 		mmput(mm);
3041da177e4SLinus Torvalds 	}
3051da177e4SLinus Torvalds 	return res;
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3101da177e4SLinus Torvalds /*
3111da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
3121da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
3131da177e4SLinus Torvalds  */
3141da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
3151da177e4SLinus Torvalds {
316ffb45122SAlexey Dobriyan 	unsigned long wchan;
3179281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	wchan = get_wchan(task);
3201da177e4SLinus Torvalds 
3219d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
322f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
323f83ce3e6SJake Edge 			return 0;
324f83ce3e6SJake Edge 		else
3251da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
3269d65cb4aSAlexey Dobriyan 	else
3279d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
3301da177e4SLinus Torvalds 
3312ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3322ec220e2SKen Chen 
3332ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3342ec220e2SKen Chen 
3352ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3362ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3372ec220e2SKen Chen {
3382ec220e2SKen Chen 	struct stack_trace trace;
3392ec220e2SKen Chen 	unsigned long *entries;
3402ec220e2SKen Chen 	int i;
3412ec220e2SKen Chen 
3422ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3432ec220e2SKen Chen 	if (!entries)
3442ec220e2SKen Chen 		return -ENOMEM;
3452ec220e2SKen Chen 
3462ec220e2SKen Chen 	trace.nr_entries	= 0;
3472ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3482ec220e2SKen Chen 	trace.entries		= entries;
3492ec220e2SKen Chen 	trace.skip		= 0;
3502ec220e2SKen Chen 	save_stack_trace_tsk(task, &trace);
3512ec220e2SKen Chen 
3522ec220e2SKen Chen 	for (i = 0; i < trace.nr_entries; i++) {
3532ec220e2SKen Chen 		seq_printf(m, "[<%p>] %pS\n",
3542ec220e2SKen Chen 			   (void *)entries[i], (void *)entries[i]);
3552ec220e2SKen Chen 	}
3562ec220e2SKen Chen 	kfree(entries);
3572ec220e2SKen Chen 
3582ec220e2SKen Chen 	return 0;
3592ec220e2SKen Chen }
3602ec220e2SKen Chen #endif
3612ec220e2SKen Chen 
3621da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3631da177e4SLinus Torvalds /*
3641da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3651da177e4SLinus Torvalds  */
3661da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3671da177e4SLinus Torvalds {
368172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
369826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
370826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3712d72376bSIngo Molnar 			task->sched_info.pcount);
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds #endif
3741da177e4SLinus Torvalds 
3759745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3769745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3779745512cSArjan van de Ven {
3789745512cSArjan van de Ven 	int i;
37913d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
38013d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3819745512cSArjan van de Ven 
38213d77c37SHiroshi Shimamoto 	if (!task)
38313d77c37SHiroshi Shimamoto 		return -ESRCH;
38413d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3859745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
3869745512cSArjan van de Ven 		if (task->latency_record[i].backtrace[0]) {
3879745512cSArjan van de Ven 			int q;
3889745512cSArjan van de Ven 			seq_printf(m, "%i %li %li ",
3899745512cSArjan van de Ven 				task->latency_record[i].count,
3909745512cSArjan van de Ven 				task->latency_record[i].time,
3919745512cSArjan van de Ven 				task->latency_record[i].max);
3929745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
3939c246247SHugh Dickins 				char sym[KSYM_SYMBOL_LEN];
3949745512cSArjan van de Ven 				char *c;
3959745512cSArjan van de Ven 				if (!task->latency_record[i].backtrace[q])
3969745512cSArjan van de Ven 					break;
3979745512cSArjan van de Ven 				if (task->latency_record[i].backtrace[q] == ULONG_MAX)
3989745512cSArjan van de Ven 					break;
3999745512cSArjan van de Ven 				sprint_symbol(sym, task->latency_record[i].backtrace[q]);
4009745512cSArjan van de Ven 				c = strchr(sym, '+');
4019745512cSArjan van de Ven 				if (c)
4029745512cSArjan van de Ven 					*c = 0;
4039745512cSArjan van de Ven 				seq_printf(m, "%s ", sym);
4049745512cSArjan van de Ven 			}
4059745512cSArjan van de Ven 			seq_printf(m, "\n");
4069745512cSArjan van de Ven 		}
4079745512cSArjan van de Ven 
4089745512cSArjan van de Ven 	}
40913d77c37SHiroshi Shimamoto 	put_task_struct(task);
4109745512cSArjan van de Ven 	return 0;
4119745512cSArjan van de Ven }
4129745512cSArjan van de Ven 
4139745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
4149745512cSArjan van de Ven {
41513d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
416d6643d12SHiroshi Shimamoto }
417d6643d12SHiroshi Shimamoto 
4189745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
4199745512cSArjan van de Ven 			    size_t count, loff_t *offs)
4209745512cSArjan van de Ven {
42113d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
4229745512cSArjan van de Ven 
42313d77c37SHiroshi Shimamoto 	if (!task)
42413d77c37SHiroshi Shimamoto 		return -ESRCH;
4259745512cSArjan van de Ven 	clear_all_latency_tracing(task);
42613d77c37SHiroshi Shimamoto 	put_task_struct(task);
4279745512cSArjan van de Ven 
4289745512cSArjan van de Ven 	return count;
4299745512cSArjan van de Ven }
4309745512cSArjan van de Ven 
4319745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
4329745512cSArjan van de Ven 	.open		= lstats_open,
4339745512cSArjan van de Ven 	.read		= seq_read,
4349745512cSArjan van de Ven 	.write		= lstats_write,
4359745512cSArjan van de Ven 	.llseek		= seq_lseek,
43613d77c37SHiroshi Shimamoto 	.release	= single_release,
4379745512cSArjan van de Ven };
4389745512cSArjan van de Ven 
4399745512cSArjan van de Ven #endif
4409745512cSArjan van de Ven 
4411da177e4SLinus Torvalds /* The badness from the OOM killer */
4421da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime);
4431da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	unsigned long points;
4461da177e4SLinus Torvalds 	struct timespec uptime;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&uptime);
44919c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
450495789a5SKOSAKI Motohiro 	points = badness(task->group_leader, uptime.tv_sec);
45119c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4521da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
455d85f50d5SNeil Horman struct limit_names {
456d85f50d5SNeil Horman 	char *name;
457d85f50d5SNeil Horman 	char *unit;
458d85f50d5SNeil Horman };
459d85f50d5SNeil Horman 
460d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
461cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
462d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
463d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
464d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
465d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
466d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
467d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
468d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
469d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
470d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
471d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
472d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
473d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
474d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
475d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4768808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
477d85f50d5SNeil Horman };
478d85f50d5SNeil Horman 
479d85f50d5SNeil Horman /* Display limits for a process */
480d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
481d85f50d5SNeil Horman {
482d85f50d5SNeil Horman 	unsigned int i;
483d85f50d5SNeil Horman 	int count = 0;
484d85f50d5SNeil Horman 	unsigned long flags;
485d85f50d5SNeil Horman 	char *bufptr = buffer;
486d85f50d5SNeil Horman 
487d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
488d85f50d5SNeil Horman 
489a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
490d85f50d5SNeil Horman 		return 0;
491d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
492d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
493d85f50d5SNeil Horman 
494d85f50d5SNeil Horman 	/*
495d85f50d5SNeil Horman 	 * print the file header
496d85f50d5SNeil Horman 	 */
497d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
498d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
499d85f50d5SNeil Horman 
500d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
501d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
502d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
503d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
504d85f50d5SNeil Horman 		else
505d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
506d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
507d85f50d5SNeil Horman 
508d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
509d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
510d85f50d5SNeil Horman 		else
511d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
512d85f50d5SNeil Horman 					 rlim[i].rlim_max);
513d85f50d5SNeil Horman 
514d85f50d5SNeil Horman 		if (lnames[i].unit)
515d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
516d85f50d5SNeil Horman 					 lnames[i].unit);
517d85f50d5SNeil Horman 		else
518d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
519d85f50d5SNeil Horman 	}
520d85f50d5SNeil Horman 
521d85f50d5SNeil Horman 	return count;
522d85f50d5SNeil Horman }
523d85f50d5SNeil Horman 
524ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
525ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
526ebcb6734SRoland McGrath {
527ebcb6734SRoland McGrath 	long nr;
528ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
529ebcb6734SRoland McGrath 
530ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
531ebcb6734SRoland McGrath 		return sprintf(buffer, "running\n");
532ebcb6734SRoland McGrath 
533ebcb6734SRoland McGrath 	if (nr < 0)
534ebcb6734SRoland McGrath 		return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
535ebcb6734SRoland McGrath 
536ebcb6734SRoland McGrath 	return sprintf(buffer,
537ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
538ebcb6734SRoland McGrath 		       nr,
539ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
540ebcb6734SRoland McGrath 		       sp, pc);
541ebcb6734SRoland McGrath }
542ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
543ebcb6734SRoland McGrath 
5441da177e4SLinus Torvalds /************************************************************************/
5451da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5461da177e4SLinus Torvalds /************************************************************************/
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds /* permission checks */
549778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5501da177e4SLinus Torvalds {
551778c1144SEric W. Biederman 	struct task_struct *task;
552778c1144SEric W. Biederman 	int allowed = 0;
553df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
554df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
555df26c40eSEric W. Biederman 	 * information.
556778c1144SEric W. Biederman 	 */
557778c1144SEric W. Biederman 	task = get_proc_task(inode);
558df26c40eSEric W. Biederman 	if (task) {
559006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
560778c1144SEric W. Biederman 		put_task_struct(task);
561df26c40eSEric W. Biederman 	}
562778c1144SEric W. Biederman 	return allowed;
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5656d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr)
5666d76fa58SLinus Torvalds {
5676d76fa58SLinus Torvalds 	int error;
5686d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5696d76fa58SLinus Torvalds 
5706d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5716d76fa58SLinus Torvalds 		return -EPERM;
5726d76fa58SLinus Torvalds 
5736d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5746d76fa58SLinus Torvalds 	if (!error)
5756d76fa58SLinus Torvalds 		error = inode_setattr(inode, attr);
5766d76fa58SLinus Torvalds 	return error;
5776d76fa58SLinus Torvalds }
5786d76fa58SLinus Torvalds 
579c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5806d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5816d76fa58SLinus Torvalds };
5826d76fa58SLinus Torvalds 
583a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file,
584a1a2c409SMiklos Szeredi 			      const struct seq_operations *op)
5851da177e4SLinus Torvalds {
58699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
587cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
5886b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
589a1a2c409SMiklos Szeredi 	struct path root;
5905addc5ddSAl Viro 	struct proc_mounts *p;
5915addc5ddSAl Viro 	int ret = -EINVAL;
5925addc5ddSAl Viro 
59399f89551SEric W. Biederman 	if (task) {
594cf7b708cSPavel Emelyanov 		rcu_read_lock();
595cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
596cf7b708cSPavel Emelyanov 		if (nsp) {
597cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
5986b3286edSKirill Korotaev 			if (ns)
5996b3286edSKirill Korotaev 				get_mnt_ns(ns);
600863c4702SAlexey Dobriyan 		}
601cf7b708cSPavel Emelyanov 		rcu_read_unlock();
6027c2c7d99SHugh Dickins 		if (ns && get_fs_path(task, &root, 1) == 0)
6037c2c7d99SHugh Dickins 			ret = 0;
60499f89551SEric W. Biederman 		put_task_struct(task);
60599f89551SEric W. Biederman 	}
6061da177e4SLinus Torvalds 
607a1a2c409SMiklos Szeredi 	if (!ns)
608a1a2c409SMiklos Szeredi 		goto err;
6097c2c7d99SHugh Dickins 	if (ret)
610a1a2c409SMiklos Szeredi 		goto err_put_ns;
611a1a2c409SMiklos Szeredi 
6125addc5ddSAl Viro 	ret = -ENOMEM;
6135addc5ddSAl Viro 	p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
614a1a2c409SMiklos Szeredi 	if (!p)
615a1a2c409SMiklos Szeredi 		goto err_put_path;
616a1a2c409SMiklos Szeredi 
6175addc5ddSAl Viro 	file->private_data = &p->m;
618a1a2c409SMiklos Szeredi 	ret = seq_open(file, op);
619a1a2c409SMiklos Szeredi 	if (ret)
620a1a2c409SMiklos Szeredi 		goto err_free;
621a1a2c409SMiklos Szeredi 
622a1a2c409SMiklos Szeredi 	p->m.private = p;
623a1a2c409SMiklos Szeredi 	p->ns = ns;
624a1a2c409SMiklos Szeredi 	p->root = root;
6256b3286edSKirill Korotaev 	p->event = ns->event;
626a1a2c409SMiklos Szeredi 
6275addc5ddSAl Viro 	return 0;
628a1a2c409SMiklos Szeredi 
629a1a2c409SMiklos Szeredi  err_free:
6305addc5ddSAl Viro 	kfree(p);
631a1a2c409SMiklos Szeredi  err_put_path:
632a1a2c409SMiklos Szeredi 	path_put(&root);
633a1a2c409SMiklos Szeredi  err_put_ns:
6346b3286edSKirill Korotaev 	put_mnt_ns(ns);
635a1a2c409SMiklos Szeredi  err:
6361da177e4SLinus Torvalds 	return ret;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
6401da177e4SLinus Torvalds {
641a1a2c409SMiklos Szeredi 	struct proc_mounts *p = file->private_data;
642a1a2c409SMiklos Szeredi 	path_put(&p->root);
643a1a2c409SMiklos Szeredi 	put_mnt_ns(p->ns);
6441da177e4SLinus Torvalds 	return seq_release(inode, file);
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6475addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
6485addc5ddSAl Viro {
6495addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
650a1a2c409SMiklos Szeredi 	struct mnt_namespace *ns = p->ns;
65131b07093SKOSAKI Motohiro 	unsigned res = POLLIN | POLLRDNORM;
6525addc5ddSAl Viro 
6535addc5ddSAl Viro 	poll_wait(file, &ns->poll, wait);
6545addc5ddSAl Viro 
6555addc5ddSAl Viro 	spin_lock(&vfsmount_lock);
6565addc5ddSAl Viro 	if (p->event != ns->event) {
6575addc5ddSAl Viro 		p->event = ns->event;
65831b07093SKOSAKI Motohiro 		res |= POLLERR | POLLPRI;
6595addc5ddSAl Viro 	}
6605addc5ddSAl Viro 	spin_unlock(&vfsmount_lock);
6615addc5ddSAl Viro 
6625addc5ddSAl Viro 	return res;
6635addc5ddSAl Viro }
6645addc5ddSAl Viro 
665a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file)
666a1a2c409SMiklos Szeredi {
667a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mounts_op);
668a1a2c409SMiklos Szeredi }
669a1a2c409SMiklos Szeredi 
67000977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
6711da177e4SLinus Torvalds 	.open		= mounts_open,
6721da177e4SLinus Torvalds 	.read		= seq_read,
6731da177e4SLinus Torvalds 	.llseek		= seq_lseek,
6741da177e4SLinus Torvalds 	.release	= mounts_release,
6755addc5ddSAl Viro 	.poll		= mounts_poll,
6761da177e4SLinus Torvalds };
6771da177e4SLinus Torvalds 
6782d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file)
6792d4d4864SRam Pai {
6802d4d4864SRam Pai 	return mounts_open_common(inode, file, &mountinfo_op);
6812d4d4864SRam Pai }
6822d4d4864SRam Pai 
6832d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = {
6842d4d4864SRam Pai 	.open		= mountinfo_open,
6852d4d4864SRam Pai 	.read		= seq_read,
6862d4d4864SRam Pai 	.llseek		= seq_lseek,
6872d4d4864SRam Pai 	.release	= mounts_release,
6882d4d4864SRam Pai 	.poll		= mounts_poll,
6892d4d4864SRam Pai };
6902d4d4864SRam Pai 
691b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
692b4629fe2SChuck Lever {
693a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mountstats_op);
694b4629fe2SChuck Lever }
695b4629fe2SChuck Lever 
69600977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
697b4629fe2SChuck Lever 	.open		= mountstats_open,
698b4629fe2SChuck Lever 	.read		= seq_read,
699b4629fe2SChuck Lever 	.llseek		= seq_lseek,
700b4629fe2SChuck Lever 	.release	= mounts_release,
701b4629fe2SChuck Lever };
702b4629fe2SChuck Lever 
7031da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
7061da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
7071da177e4SLinus Torvalds {
7082fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
7091da177e4SLinus Torvalds 	unsigned long page;
7101da177e4SLinus Torvalds 	ssize_t length;
71199f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
71299f89551SEric W. Biederman 
71399f89551SEric W. Biederman 	length = -ESRCH;
71499f89551SEric W. Biederman 	if (!task)
71599f89551SEric W. Biederman 		goto out_no_task;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
7181da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
71999f89551SEric W. Biederman 
72099f89551SEric W. Biederman 	length = -ENOMEM;
721e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
72299f89551SEric W. Biederman 		goto out;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	if (length >= 0)
7271da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
7281da177e4SLinus Torvalds 	free_page(page);
72999f89551SEric W. Biederman out:
73099f89551SEric W. Biederman 	put_task_struct(task);
73199f89551SEric W. Biederman out_no_task:
7321da177e4SLinus Torvalds 	return length;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
73500977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
7361da177e4SLinus Torvalds 	.read		= proc_info_read,
7371da177e4SLinus Torvalds };
7381da177e4SLinus Torvalds 
739be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
740be614086SEric W. Biederman {
741be614086SEric W. Biederman 	struct inode *inode = m->private;
742be614086SEric W. Biederman 	struct pid_namespace *ns;
743be614086SEric W. Biederman 	struct pid *pid;
744be614086SEric W. Biederman 	struct task_struct *task;
745be614086SEric W. Biederman 	int ret;
746be614086SEric W. Biederman 
747be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
748be614086SEric W. Biederman 	pid = proc_pid(inode);
749be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
750be614086SEric W. Biederman 	if (!task)
751be614086SEric W. Biederman 		return -ESRCH;
752be614086SEric W. Biederman 
753be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
754be614086SEric W. Biederman 
755be614086SEric W. Biederman 	put_task_struct(task);
756be614086SEric W. Biederman 	return ret;
757be614086SEric W. Biederman }
758be614086SEric W. Biederman 
759be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
760be614086SEric W. Biederman {
761be614086SEric W. Biederman 	int ret;
762be614086SEric W. Biederman 	ret = single_open(filp, proc_single_show, NULL);
763be614086SEric W. Biederman 	if (!ret) {
764be614086SEric W. Biederman 		struct seq_file *m = filp->private_data;
765be614086SEric W. Biederman 
766be614086SEric W. Biederman 		m->private = inode;
767be614086SEric W. Biederman 	}
768be614086SEric W. Biederman 	return ret;
769be614086SEric W. Biederman }
770be614086SEric W. Biederman 
771be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
772be614086SEric W. Biederman 	.open		= proc_single_open,
773be614086SEric W. Biederman 	.read		= seq_read,
774be614086SEric W. Biederman 	.llseek		= seq_lseek,
775be614086SEric W. Biederman 	.release	= single_release,
776be614086SEric W. Biederman };
777be614086SEric W. Biederman 
7781da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
7811da177e4SLinus Torvalds 	return 0;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
7851da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
7861da177e4SLinus Torvalds {
7872fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7881da177e4SLinus Torvalds 	char *page;
7891da177e4SLinus Torvalds 	unsigned long src = *ppos;
7901da177e4SLinus Torvalds 	int ret = -ESRCH;
7911da177e4SLinus Torvalds 	struct mm_struct *mm;
7921da177e4SLinus Torvalds 
79399f89551SEric W. Biederman 	if (!task)
79499f89551SEric W. Biederman 		goto out_no_task;
79599f89551SEric W. Biederman 
796638fa202SRoland McGrath 	if (check_mem_permission(task))
7971da177e4SLinus Torvalds 		goto out;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	ret = -ENOMEM;
800e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
8011da177e4SLinus Torvalds 	if (!page)
8021da177e4SLinus Torvalds 		goto out;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	ret = 0;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	mm = get_task_mm(task);
8071da177e4SLinus Torvalds 	if (!mm)
8081da177e4SLinus Torvalds 		goto out_free;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	ret = -EIO;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
8131da177e4SLinus Torvalds 		goto out_put;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	ret = 0;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	while (count > 0) {
8181da177e4SLinus Torvalds 		int this_len, retval;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8211da177e4SLinus Torvalds 		retval = access_process_vm(task, src, page, this_len, 0);
822638fa202SRoland McGrath 		if (!retval || check_mem_permission(task)) {
8231da177e4SLinus Torvalds 			if (!ret)
8241da177e4SLinus Torvalds 				ret = -EIO;
8251da177e4SLinus Torvalds 			break;
8261da177e4SLinus Torvalds 		}
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
8291da177e4SLinus Torvalds 			ret = -EFAULT;
8301da177e4SLinus Torvalds 			break;
8311da177e4SLinus Torvalds 		}
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 		ret += retval;
8341da177e4SLinus Torvalds 		src += retval;
8351da177e4SLinus Torvalds 		buf += retval;
8361da177e4SLinus Torvalds 		count -= retval;
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds 	*ppos = src;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds out_put:
8411da177e4SLinus Torvalds 	mmput(mm);
8421da177e4SLinus Torvalds out_free:
8431da177e4SLinus Torvalds 	free_page((unsigned long) page);
8441da177e4SLinus Torvalds out:
84599f89551SEric W. Biederman 	put_task_struct(task);
84699f89551SEric W. Biederman out_no_task:
8471da177e4SLinus Torvalds 	return ret;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds #define mem_write NULL
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds #ifndef mem_write
8531da177e4SLinus Torvalds /* This is a security hazard */
85463967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
8551da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
8561da177e4SLinus Torvalds {
857f7ca54f4SFrederik Deweerdt 	int copied;
8581da177e4SLinus Torvalds 	char *page;
8592fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8601da177e4SLinus Torvalds 	unsigned long dst = *ppos;
8611da177e4SLinus Torvalds 
86299f89551SEric W. Biederman 	copied = -ESRCH;
86399f89551SEric W. Biederman 	if (!task)
86499f89551SEric W. Biederman 		goto out_no_task;
8651da177e4SLinus Torvalds 
866638fa202SRoland McGrath 	if (check_mem_permission(task))
86799f89551SEric W. Biederman 		goto out;
86899f89551SEric W. Biederman 
86999f89551SEric W. Biederman 	copied = -ENOMEM;
870e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
8711da177e4SLinus Torvalds 	if (!page)
87299f89551SEric W. Biederman 		goto out;
8731da177e4SLinus Torvalds 
874f7ca54f4SFrederik Deweerdt 	copied = 0;
8751da177e4SLinus Torvalds 	while (count > 0) {
8761da177e4SLinus Torvalds 		int this_len, retval;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8791da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
8801da177e4SLinus Torvalds 			copied = -EFAULT;
8811da177e4SLinus Torvalds 			break;
8821da177e4SLinus Torvalds 		}
8831da177e4SLinus Torvalds 		retval = access_process_vm(task, dst, page, this_len, 1);
8841da177e4SLinus Torvalds 		if (!retval) {
8851da177e4SLinus Torvalds 			if (!copied)
8861da177e4SLinus Torvalds 				copied = -EIO;
8871da177e4SLinus Torvalds 			break;
8881da177e4SLinus Torvalds 		}
8891da177e4SLinus Torvalds 		copied += retval;
8901da177e4SLinus Torvalds 		buf += retval;
8911da177e4SLinus Torvalds 		dst += retval;
8921da177e4SLinus Torvalds 		count -= retval;
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 	*ppos = dst;
8951da177e4SLinus Torvalds 	free_page((unsigned long) page);
89699f89551SEric W. Biederman out:
89799f89551SEric W. Biederman 	put_task_struct(task);
89899f89551SEric W. Biederman out_no_task:
8991da177e4SLinus Torvalds 	return copied;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds #endif
9021da177e4SLinus Torvalds 
90385863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
9041da177e4SLinus Torvalds {
9051da177e4SLinus Torvalds 	switch (orig) {
9061da177e4SLinus Torvalds 	case 0:
9071da177e4SLinus Torvalds 		file->f_pos = offset;
9081da177e4SLinus Torvalds 		break;
9091da177e4SLinus Torvalds 	case 1:
9101da177e4SLinus Torvalds 		file->f_pos += offset;
9111da177e4SLinus Torvalds 		break;
9121da177e4SLinus Torvalds 	default:
9131da177e4SLinus Torvalds 		return -EINVAL;
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 	force_successful_syscall_return();
9161da177e4SLinus Torvalds 	return file->f_pos;
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
91900977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
9201da177e4SLinus Torvalds 	.llseek		= mem_lseek,
9211da177e4SLinus Torvalds 	.read		= mem_read,
9221da177e4SLinus Torvalds 	.write		= mem_write,
9231da177e4SLinus Torvalds 	.open		= mem_open,
9241da177e4SLinus Torvalds };
9251da177e4SLinus Torvalds 
926315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
927315e28c8SJames Pearson 			size_t count, loff_t *ppos)
928315e28c8SJames Pearson {
929315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
930315e28c8SJames Pearson 	char *page;
931315e28c8SJames Pearson 	unsigned long src = *ppos;
932315e28c8SJames Pearson 	int ret = -ESRCH;
933315e28c8SJames Pearson 	struct mm_struct *mm;
934315e28c8SJames Pearson 
935315e28c8SJames Pearson 	if (!task)
936315e28c8SJames Pearson 		goto out_no_task;
937315e28c8SJames Pearson 
938006ebb40SStephen Smalley 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
939315e28c8SJames Pearson 		goto out;
940315e28c8SJames Pearson 
941315e28c8SJames Pearson 	ret = -ENOMEM;
942315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
943315e28c8SJames Pearson 	if (!page)
944315e28c8SJames Pearson 		goto out;
945315e28c8SJames Pearson 
946315e28c8SJames Pearson 	ret = 0;
947315e28c8SJames Pearson 
948315e28c8SJames Pearson 	mm = get_task_mm(task);
949315e28c8SJames Pearson 	if (!mm)
950315e28c8SJames Pearson 		goto out_free;
951315e28c8SJames Pearson 
952315e28c8SJames Pearson 	while (count > 0) {
953315e28c8SJames Pearson 		int this_len, retval, max_len;
954315e28c8SJames Pearson 
955315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
956315e28c8SJames Pearson 
957315e28c8SJames Pearson 		if (this_len <= 0)
958315e28c8SJames Pearson 			break;
959315e28c8SJames Pearson 
960315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
961315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
962315e28c8SJames Pearson 
963315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
964315e28c8SJames Pearson 			page, this_len, 0);
965315e28c8SJames Pearson 
966315e28c8SJames Pearson 		if (retval <= 0) {
967315e28c8SJames Pearson 			ret = retval;
968315e28c8SJames Pearson 			break;
969315e28c8SJames Pearson 		}
970315e28c8SJames Pearson 
971315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
972315e28c8SJames Pearson 			ret = -EFAULT;
973315e28c8SJames Pearson 			break;
974315e28c8SJames Pearson 		}
975315e28c8SJames Pearson 
976315e28c8SJames Pearson 		ret += retval;
977315e28c8SJames Pearson 		src += retval;
978315e28c8SJames Pearson 		buf += retval;
979315e28c8SJames Pearson 		count -= retval;
980315e28c8SJames Pearson 	}
981315e28c8SJames Pearson 	*ppos = src;
982315e28c8SJames Pearson 
983315e28c8SJames Pearson 	mmput(mm);
984315e28c8SJames Pearson out_free:
985315e28c8SJames Pearson 	free_page((unsigned long) page);
986315e28c8SJames Pearson out:
987315e28c8SJames Pearson 	put_task_struct(task);
988315e28c8SJames Pearson out_no_task:
989315e28c8SJames Pearson 	return ret;
990315e28c8SJames Pearson }
991315e28c8SJames Pearson 
992315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
993315e28c8SJames Pearson 	.read		= environ_read,
994315e28c8SJames Pearson };
995315e28c8SJames Pearson 
9961da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
9971da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9981da177e4SLinus Torvalds {
9992fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
10008578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
10011da177e4SLinus Torvalds 	size_t len;
100228b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
100328b83c51SKOSAKI Motohiro 	unsigned long flags;
10041da177e4SLinus Torvalds 
100599f89551SEric W. Biederman 	if (!task)
100699f89551SEric W. Biederman 		return -ESRCH;
100728b83c51SKOSAKI Motohiro 
100828b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
100928b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
101028b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
101128b83c51SKOSAKI Motohiro 	}
101228b83c51SKOSAKI Motohiro 
101399f89551SEric W. Biederman 	put_task_struct(task);
101499f89551SEric W. Biederman 
10158578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
10160c28f287SAkinobu Mita 
10170c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
10211da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
10221da177e4SLinus Torvalds {
102399f89551SEric W. Biederman 	struct task_struct *task;
10245d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
10255d863b89SKOSAKI Motohiro 	long oom_adjust;
102628b83c51SKOSAKI Motohiro 	unsigned long flags;
10275d863b89SKOSAKI Motohiro 	int err;
10281da177e4SLinus Torvalds 
10298578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
10308578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
10318578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
10321da177e4SLinus Torvalds 	if (copy_from_user(buffer, buf, count))
10331da177e4SLinus Torvalds 		return -EFAULT;
10345d863b89SKOSAKI Motohiro 
10355d863b89SKOSAKI Motohiro 	err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
10365d863b89SKOSAKI Motohiro 	if (err)
10375d863b89SKOSAKI Motohiro 		return -EINVAL;
10388ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
10398ac773b4SAlexey Dobriyan 	     oom_adjust != OOM_DISABLE)
10401da177e4SLinus Torvalds 		return -EINVAL;
10415d863b89SKOSAKI Motohiro 
10422fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
104399f89551SEric W. Biederman 	if (!task)
104499f89551SEric W. Biederman 		return -ESRCH;
104528b83c51SKOSAKI Motohiro 	if (!lock_task_sighand(task, &flags)) {
104628b83c51SKOSAKI Motohiro 		put_task_struct(task);
104728b83c51SKOSAKI Motohiro 		return -ESRCH;
104828b83c51SKOSAKI Motohiro 	}
104928b83c51SKOSAKI Motohiro 
105028b83c51SKOSAKI Motohiro 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
105128b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
10528fb4fc68SGuillem Jover 		put_task_struct(task);
10538fb4fc68SGuillem Jover 		return -EACCES;
10548fb4fc68SGuillem Jover 	}
105528b83c51SKOSAKI Motohiro 
105628b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
105728b83c51SKOSAKI Motohiro 
105828b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
105999f89551SEric W. Biederman 	put_task_struct(task);
10605d863b89SKOSAKI Motohiro 
10615d863b89SKOSAKI Motohiro 	return count;
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds 
106400977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
10651da177e4SLinus Torvalds 	.read		= oom_adjust_read,
10661da177e4SLinus Torvalds 	.write		= oom_adjust_write,
10671da177e4SLinus Torvalds };
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10701da177e4SLinus Torvalds #define TMPBUFLEN 21
10711da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10721da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10731da177e4SLinus Torvalds {
10742fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
107599f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10761da177e4SLinus Torvalds 	ssize_t length;
10771da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10781da177e4SLinus Torvalds 
107999f89551SEric W. Biederman 	if (!task)
108099f89551SEric W. Biederman 		return -ESRCH;
10811da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
10820c11b942SAl Viro 				audit_get_loginuid(task));
108399f89551SEric W. Biederman 	put_task_struct(task);
10841da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10881da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
10891da177e4SLinus Torvalds {
10902fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
10911da177e4SLinus Torvalds 	char *page, *tmp;
10921da177e4SLinus Torvalds 	ssize_t length;
10931da177e4SLinus Torvalds 	uid_t loginuid;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
10961da177e4SLinus Torvalds 		return -EPERM;
10971da177e4SLinus Torvalds 
1098*7dc52157SPaul E. McKenney 	rcu_read_lock();
1099*7dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1100*7dc52157SPaul E. McKenney 		rcu_read_unlock();
11011da177e4SLinus Torvalds 		return -EPERM;
1102*7dc52157SPaul E. McKenney 	}
1103*7dc52157SPaul E. McKenney 	rcu_read_unlock();
11041da177e4SLinus Torvalds 
1105e0182909SAl Viro 	if (count >= PAGE_SIZE)
1106e0182909SAl Viro 		count = PAGE_SIZE - 1;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	if (*ppos != 0) {
11091da177e4SLinus Torvalds 		/* No partial writes. */
11101da177e4SLinus Torvalds 		return -EINVAL;
11111da177e4SLinus Torvalds 	}
1112e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11131da177e4SLinus Torvalds 	if (!page)
11141da177e4SLinus Torvalds 		return -ENOMEM;
11151da177e4SLinus Torvalds 	length = -EFAULT;
11161da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11171da177e4SLinus Torvalds 		goto out_free_page;
11181da177e4SLinus Torvalds 
1119e0182909SAl Viro 	page[count] = '\0';
11201da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11211da177e4SLinus Torvalds 	if (tmp == page) {
11221da177e4SLinus Torvalds 		length = -EINVAL;
11231da177e4SLinus Torvalds 		goto out_free_page;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	}
112699f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
11271da177e4SLinus Torvalds 	if (likely(length == 0))
11281da177e4SLinus Torvalds 		length = count;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds out_free_page:
11311da177e4SLinus Torvalds 	free_page((unsigned long) page);
11321da177e4SLinus Torvalds 	return length;
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds 
113500977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11361da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11371da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
11381da177e4SLinus Torvalds };
11391e0bd755SEric Paris 
11401e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
11411e0bd755SEric Paris 				  size_t count, loff_t *ppos)
11421e0bd755SEric Paris {
11431e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
11441e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
11451e0bd755SEric Paris 	ssize_t length;
11461e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
11471e0bd755SEric Paris 
11481e0bd755SEric Paris 	if (!task)
11491e0bd755SEric Paris 		return -ESRCH;
11501e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11511e0bd755SEric Paris 				audit_get_sessionid(task));
11521e0bd755SEric Paris 	put_task_struct(task);
11531e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11541e0bd755SEric Paris }
11551e0bd755SEric Paris 
11561e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11571e0bd755SEric Paris 	.read		= proc_sessionid_read,
11581e0bd755SEric Paris };
11591da177e4SLinus Torvalds #endif
11601da177e4SLinus Torvalds 
1161f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1162f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1163f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1164f4f154fdSAkinobu Mita {
1165f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1166f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1167f4f154fdSAkinobu Mita 	size_t len;
1168f4f154fdSAkinobu Mita 	int make_it_fail;
1169f4f154fdSAkinobu Mita 
1170f4f154fdSAkinobu Mita 	if (!task)
1171f4f154fdSAkinobu Mita 		return -ESRCH;
1172f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1173f4f154fdSAkinobu Mita 	put_task_struct(task);
1174f4f154fdSAkinobu Mita 
1175f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11760c28f287SAkinobu Mita 
11770c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1178f4f154fdSAkinobu Mita }
1179f4f154fdSAkinobu Mita 
1180f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1181f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1182f4f154fdSAkinobu Mita {
1183f4f154fdSAkinobu Mita 	struct task_struct *task;
1184f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1185f4f154fdSAkinobu Mita 	int make_it_fail;
1186f4f154fdSAkinobu Mita 
1187f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1188f4f154fdSAkinobu Mita 		return -EPERM;
1189f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1190f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1191f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1192f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1193f4f154fdSAkinobu Mita 		return -EFAULT;
1194cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1195cba8aafeSVincent Li 	if (*end)
1196cba8aafeSVincent Li 		return -EINVAL;
1197f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1198f4f154fdSAkinobu Mita 	if (!task)
1199f4f154fdSAkinobu Mita 		return -ESRCH;
1200f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1201f4f154fdSAkinobu Mita 	put_task_struct(task);
1202cba8aafeSVincent Li 
1203cba8aafeSVincent Li 	return count;
1204f4f154fdSAkinobu Mita }
1205f4f154fdSAkinobu Mita 
120600977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1207f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1208f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
1209f4f154fdSAkinobu Mita };
1210f4f154fdSAkinobu Mita #endif
1211f4f154fdSAkinobu Mita 
12129745512cSArjan van de Ven 
121343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
121443ae34cbSIngo Molnar /*
121543ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
121643ae34cbSIngo Molnar  */
121743ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
121843ae34cbSIngo Molnar {
121943ae34cbSIngo Molnar 	struct inode *inode = m->private;
122043ae34cbSIngo Molnar 	struct task_struct *p;
122143ae34cbSIngo Molnar 
122243ae34cbSIngo Molnar 	p = get_proc_task(inode);
122343ae34cbSIngo Molnar 	if (!p)
122443ae34cbSIngo Molnar 		return -ESRCH;
122543ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
122643ae34cbSIngo Molnar 
122743ae34cbSIngo Molnar 	put_task_struct(p);
122843ae34cbSIngo Molnar 
122943ae34cbSIngo Molnar 	return 0;
123043ae34cbSIngo Molnar }
123143ae34cbSIngo Molnar 
123243ae34cbSIngo Molnar static ssize_t
123343ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
123443ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
123543ae34cbSIngo Molnar {
123643ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
123743ae34cbSIngo Molnar 	struct task_struct *p;
123843ae34cbSIngo Molnar 
123943ae34cbSIngo Molnar 	p = get_proc_task(inode);
124043ae34cbSIngo Molnar 	if (!p)
124143ae34cbSIngo Molnar 		return -ESRCH;
124243ae34cbSIngo Molnar 	proc_sched_set_task(p);
124343ae34cbSIngo Molnar 
124443ae34cbSIngo Molnar 	put_task_struct(p);
124543ae34cbSIngo Molnar 
124643ae34cbSIngo Molnar 	return count;
124743ae34cbSIngo Molnar }
124843ae34cbSIngo Molnar 
124943ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
125043ae34cbSIngo Molnar {
125143ae34cbSIngo Molnar 	int ret;
125243ae34cbSIngo Molnar 
125343ae34cbSIngo Molnar 	ret = single_open(filp, sched_show, NULL);
125443ae34cbSIngo Molnar 	if (!ret) {
125543ae34cbSIngo Molnar 		struct seq_file *m = filp->private_data;
125643ae34cbSIngo Molnar 
125743ae34cbSIngo Molnar 		m->private = inode;
125843ae34cbSIngo Molnar 	}
125943ae34cbSIngo Molnar 	return ret;
126043ae34cbSIngo Molnar }
126143ae34cbSIngo Molnar 
126243ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
126343ae34cbSIngo Molnar 	.open		= sched_open,
126443ae34cbSIngo Molnar 	.read		= seq_read,
126543ae34cbSIngo Molnar 	.write		= sched_write,
126643ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12675ea473a1SAlexey Dobriyan 	.release	= single_release,
126843ae34cbSIngo Molnar };
126943ae34cbSIngo Molnar 
127043ae34cbSIngo Molnar #endif
127143ae34cbSIngo Molnar 
12724614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
12734614a696Sjohn stultz 				size_t count, loff_t *offset)
12744614a696Sjohn stultz {
12754614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
12764614a696Sjohn stultz 	struct task_struct *p;
12774614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
12784614a696Sjohn stultz 
12794614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
12804614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
12814614a696Sjohn stultz 		count = sizeof(buffer) - 1;
12824614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
12834614a696Sjohn stultz 		return -EFAULT;
12844614a696Sjohn stultz 
12854614a696Sjohn stultz 	p = get_proc_task(inode);
12864614a696Sjohn stultz 	if (!p)
12874614a696Sjohn stultz 		return -ESRCH;
12884614a696Sjohn stultz 
12894614a696Sjohn stultz 	if (same_thread_group(current, p))
12904614a696Sjohn stultz 		set_task_comm(p, buffer);
12914614a696Sjohn stultz 	else
12924614a696Sjohn stultz 		count = -EINVAL;
12934614a696Sjohn stultz 
12944614a696Sjohn stultz 	put_task_struct(p);
12954614a696Sjohn stultz 
12964614a696Sjohn stultz 	return count;
12974614a696Sjohn stultz }
12984614a696Sjohn stultz 
12994614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13004614a696Sjohn stultz {
13014614a696Sjohn stultz 	struct inode *inode = m->private;
13024614a696Sjohn stultz 	struct task_struct *p;
13034614a696Sjohn stultz 
13044614a696Sjohn stultz 	p = get_proc_task(inode);
13054614a696Sjohn stultz 	if (!p)
13064614a696Sjohn stultz 		return -ESRCH;
13074614a696Sjohn stultz 
13084614a696Sjohn stultz 	task_lock(p);
13094614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13104614a696Sjohn stultz 	task_unlock(p);
13114614a696Sjohn stultz 
13124614a696Sjohn stultz 	put_task_struct(p);
13134614a696Sjohn stultz 
13144614a696Sjohn stultz 	return 0;
13154614a696Sjohn stultz }
13164614a696Sjohn stultz 
13174614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
13184614a696Sjohn stultz {
13194614a696Sjohn stultz 	int ret;
13204614a696Sjohn stultz 
13214614a696Sjohn stultz 	ret = single_open(filp, comm_show, NULL);
13224614a696Sjohn stultz 	if (!ret) {
13234614a696Sjohn stultz 		struct seq_file *m = filp->private_data;
13244614a696Sjohn stultz 
13254614a696Sjohn stultz 		m->private = inode;
13264614a696Sjohn stultz 	}
13274614a696Sjohn stultz 	return ret;
13284614a696Sjohn stultz }
13294614a696Sjohn stultz 
13304614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
13314614a696Sjohn stultz 	.open		= comm_open,
13324614a696Sjohn stultz 	.read		= seq_read,
13334614a696Sjohn stultz 	.write		= comm_write,
13344614a696Sjohn stultz 	.llseek		= seq_lseek,
13354614a696Sjohn stultz 	.release	= single_release,
13364614a696Sjohn stultz };
13374614a696Sjohn stultz 
1338925d1c40SMatt Helsley /*
1339925d1c40SMatt Helsley  * We added or removed a vma mapping the executable. The vmas are only mapped
1340925d1c40SMatt Helsley  * during exec and are not mapped with the mmap system call.
1341925d1c40SMatt Helsley  * Callers must hold down_write() on the mm's mmap_sem for these
1342925d1c40SMatt Helsley  */
1343925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm)
1344925d1c40SMatt Helsley {
1345925d1c40SMatt Helsley 	mm->num_exe_file_vmas++;
1346925d1c40SMatt Helsley }
1347925d1c40SMatt Helsley 
1348925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm)
1349925d1c40SMatt Helsley {
1350925d1c40SMatt Helsley 	mm->num_exe_file_vmas--;
1351925d1c40SMatt Helsley 	if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1352925d1c40SMatt Helsley 		fput(mm->exe_file);
1353925d1c40SMatt Helsley 		mm->exe_file = NULL;
1354925d1c40SMatt Helsley 	}
1355925d1c40SMatt Helsley 
1356925d1c40SMatt Helsley }
1357925d1c40SMatt Helsley 
1358925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1359925d1c40SMatt Helsley {
1360925d1c40SMatt Helsley 	if (new_exe_file)
1361925d1c40SMatt Helsley 		get_file(new_exe_file);
1362925d1c40SMatt Helsley 	if (mm->exe_file)
1363925d1c40SMatt Helsley 		fput(mm->exe_file);
1364925d1c40SMatt Helsley 	mm->exe_file = new_exe_file;
1365925d1c40SMatt Helsley 	mm->num_exe_file_vmas = 0;
1366925d1c40SMatt Helsley }
1367925d1c40SMatt Helsley 
1368925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm)
1369925d1c40SMatt Helsley {
1370925d1c40SMatt Helsley 	struct file *exe_file;
1371925d1c40SMatt Helsley 
1372925d1c40SMatt Helsley 	/* We need mmap_sem to protect against races with removal of
1373925d1c40SMatt Helsley 	 * VM_EXECUTABLE vmas */
1374925d1c40SMatt Helsley 	down_read(&mm->mmap_sem);
1375925d1c40SMatt Helsley 	exe_file = mm->exe_file;
1376925d1c40SMatt Helsley 	if (exe_file)
1377925d1c40SMatt Helsley 		get_file(exe_file);
1378925d1c40SMatt Helsley 	up_read(&mm->mmap_sem);
1379925d1c40SMatt Helsley 	return exe_file;
1380925d1c40SMatt Helsley }
1381925d1c40SMatt Helsley 
1382925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1383925d1c40SMatt Helsley {
1384925d1c40SMatt Helsley 	/* It's safe to write the exe_file pointer without exe_file_lock because
1385925d1c40SMatt Helsley 	 * this is called during fork when the task is not yet in /proc */
1386925d1c40SMatt Helsley 	newmm->exe_file = get_mm_exe_file(oldmm);
1387925d1c40SMatt Helsley }
1388925d1c40SMatt Helsley 
1389925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path)
1390925d1c40SMatt Helsley {
1391925d1c40SMatt Helsley 	struct task_struct *task;
1392925d1c40SMatt Helsley 	struct mm_struct *mm;
1393925d1c40SMatt Helsley 	struct file *exe_file;
1394925d1c40SMatt Helsley 
1395925d1c40SMatt Helsley 	task = get_proc_task(inode);
1396925d1c40SMatt Helsley 	if (!task)
1397925d1c40SMatt Helsley 		return -ENOENT;
1398925d1c40SMatt Helsley 	mm = get_task_mm(task);
1399925d1c40SMatt Helsley 	put_task_struct(task);
1400925d1c40SMatt Helsley 	if (!mm)
1401925d1c40SMatt Helsley 		return -ENOENT;
1402925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1403925d1c40SMatt Helsley 	mmput(mm);
1404925d1c40SMatt Helsley 	if (exe_file) {
1405925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1406925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1407925d1c40SMatt Helsley 		fput(exe_file);
1408925d1c40SMatt Helsley 		return 0;
1409925d1c40SMatt Helsley 	} else
1410925d1c40SMatt Helsley 		return -ENOENT;
1411925d1c40SMatt Helsley }
1412925d1c40SMatt Helsley 
1413008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14141da177e4SLinus Torvalds {
14151da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14161da177e4SLinus Torvalds 	int error = -EACCES;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
14191d957f9bSJan Blunck 	path_put(&nd->path);
14201da177e4SLinus Torvalds 
1421778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1422778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14231da177e4SLinus Torvalds 		goto out;
14241da177e4SLinus Torvalds 
14253dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
14261da177e4SLinus Torvalds out:
1427008b150aSAl Viro 	return ERR_PTR(error);
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds 
14303dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14311da177e4SLinus Torvalds {
1432e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14333dcd25f3SJan Blunck 	char *pathname;
14341da177e4SLinus Torvalds 	int len;
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 	if (!tmp)
14371da177e4SLinus Torvalds 		return -ENOMEM;
14381da177e4SLinus Torvalds 
1439cf28b486SJan Blunck 	pathname = d_path(path, tmp, PAGE_SIZE);
14403dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14413dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14421da177e4SLinus Torvalds 		goto out;
14433dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	if (len > buflen)
14461da177e4SLinus Torvalds 		len = buflen;
14473dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14481da177e4SLinus Torvalds 		len = -EFAULT;
14491da177e4SLinus Torvalds  out:
14501da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14511da177e4SLinus Torvalds 	return len;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14551da177e4SLinus Torvalds {
14561da177e4SLinus Torvalds 	int error = -EACCES;
14571da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14583dcd25f3SJan Blunck 	struct path path;
14591da177e4SLinus Torvalds 
1460778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1461778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14621da177e4SLinus Torvalds 		goto out;
14631da177e4SLinus Torvalds 
14643dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &path);
14651da177e4SLinus Torvalds 	if (error)
14661da177e4SLinus Torvalds 		goto out;
14671da177e4SLinus Torvalds 
14683dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14693dcd25f3SJan Blunck 	path_put(&path);
14701da177e4SLinus Torvalds out:
14711da177e4SLinus Torvalds 	return error;
14721da177e4SLinus Torvalds }
14731da177e4SLinus Torvalds 
1474c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
14751da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
14766d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
14776d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
14781da177e4SLinus Torvalds };
14791da177e4SLinus Torvalds 
148028a6d671SEric W. Biederman 
148128a6d671SEric W. Biederman /* building an inode */
148228a6d671SEric W. Biederman 
148328a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
148428a6d671SEric W. Biederman {
148528a6d671SEric W. Biederman 	int dumpable = 0;
148628a6d671SEric W. Biederman 	struct mm_struct *mm;
148728a6d671SEric W. Biederman 
148828a6d671SEric W. Biederman 	task_lock(task);
148928a6d671SEric W. Biederman 	mm = task->mm;
149028a6d671SEric W. Biederman 	if (mm)
14916c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
149228a6d671SEric W. Biederman 	task_unlock(task);
149328a6d671SEric W. Biederman 	if(dumpable == 1)
149428a6d671SEric W. Biederman 		return 1;
149528a6d671SEric W. Biederman 	return 0;
149628a6d671SEric W. Biederman }
149728a6d671SEric W. Biederman 
149828a6d671SEric W. Biederman 
149961a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
150028a6d671SEric W. Biederman {
150128a6d671SEric W. Biederman 	struct inode * inode;
150228a6d671SEric W. Biederman 	struct proc_inode *ei;
1503c69e8d9cSDavid Howells 	const struct cred *cred;
150428a6d671SEric W. Biederman 
150528a6d671SEric W. Biederman 	/* We need a new inode */
150628a6d671SEric W. Biederman 
150728a6d671SEric W. Biederman 	inode = new_inode(sb);
150828a6d671SEric W. Biederman 	if (!inode)
150928a6d671SEric W. Biederman 		goto out;
151028a6d671SEric W. Biederman 
151128a6d671SEric W. Biederman 	/* Common stuff */
151228a6d671SEric W. Biederman 	ei = PROC_I(inode);
151328a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
151428a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
151528a6d671SEric W. Biederman 
151628a6d671SEric W. Biederman 	/*
151728a6d671SEric W. Biederman 	 * grab the reference to task.
151828a6d671SEric W. Biederman 	 */
15191a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
152028a6d671SEric W. Biederman 	if (!ei->pid)
152128a6d671SEric W. Biederman 		goto out_unlock;
152228a6d671SEric W. Biederman 
152328a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1524c69e8d9cSDavid Howells 		rcu_read_lock();
1525c69e8d9cSDavid Howells 		cred = __task_cred(task);
1526c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1527c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1528c69e8d9cSDavid Howells 		rcu_read_unlock();
152928a6d671SEric W. Biederman 	}
153028a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
153128a6d671SEric W. Biederman 
153228a6d671SEric W. Biederman out:
153328a6d671SEric W. Biederman 	return inode;
153428a6d671SEric W. Biederman 
153528a6d671SEric W. Biederman out_unlock:
153628a6d671SEric W. Biederman 	iput(inode);
153728a6d671SEric W. Biederman 	return NULL;
153828a6d671SEric W. Biederman }
153928a6d671SEric W. Biederman 
154028a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
154128a6d671SEric W. Biederman {
154228a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
154328a6d671SEric W. Biederman 	struct task_struct *task;
1544c69e8d9cSDavid Howells 	const struct cred *cred;
1545c69e8d9cSDavid Howells 
154628a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
154728a6d671SEric W. Biederman 
154828a6d671SEric W. Biederman 	rcu_read_lock();
154928a6d671SEric W. Biederman 	stat->uid = 0;
155028a6d671SEric W. Biederman 	stat->gid = 0;
155128a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
155228a6d671SEric W. Biederman 	if (task) {
155328a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
155428a6d671SEric W. Biederman 		    task_dumpable(task)) {
1555c69e8d9cSDavid Howells 			cred = __task_cred(task);
1556c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1557c69e8d9cSDavid Howells 			stat->gid = cred->egid;
155828a6d671SEric W. Biederman 		}
155928a6d671SEric W. Biederman 	}
156028a6d671SEric W. Biederman 	rcu_read_unlock();
156128a6d671SEric W. Biederman 	return 0;
156228a6d671SEric W. Biederman }
156328a6d671SEric W. Biederman 
156428a6d671SEric W. Biederman /* dentry stuff */
156528a6d671SEric W. Biederman 
156628a6d671SEric W. Biederman /*
156728a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
156828a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
156928a6d671SEric W. Biederman  * due to the way we treat inodes.
157028a6d671SEric W. Biederman  *
157128a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
157228a6d671SEric W. Biederman  * performed a setuid(), etc.
157328a6d671SEric W. Biederman  *
157428a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
157528a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
157628a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
157728a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
157828a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
157928a6d671SEric W. Biederman  * directories.
158028a6d671SEric W. Biederman  */
158128a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
158228a6d671SEric W. Biederman {
158328a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
158428a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1585c69e8d9cSDavid Howells 	const struct cred *cred;
1586c69e8d9cSDavid Howells 
158728a6d671SEric W. Biederman 	if (task) {
158828a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
158928a6d671SEric W. Biederman 		    task_dumpable(task)) {
1590c69e8d9cSDavid Howells 			rcu_read_lock();
1591c69e8d9cSDavid Howells 			cred = __task_cred(task);
1592c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1593c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1594c69e8d9cSDavid Howells 			rcu_read_unlock();
159528a6d671SEric W. Biederman 		} else {
159628a6d671SEric W. Biederman 			inode->i_uid = 0;
159728a6d671SEric W. Biederman 			inode->i_gid = 0;
159828a6d671SEric W. Biederman 		}
159928a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
160028a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
160128a6d671SEric W. Biederman 		put_task_struct(task);
160228a6d671SEric W. Biederman 		return 1;
160328a6d671SEric W. Biederman 	}
160428a6d671SEric W. Biederman 	d_drop(dentry);
160528a6d671SEric W. Biederman 	return 0;
160628a6d671SEric W. Biederman }
160728a6d671SEric W. Biederman 
160828a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry)
160928a6d671SEric W. Biederman {
161028a6d671SEric W. Biederman 	/* Is the task we represent dead?
161128a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
161228a6d671SEric W. Biederman 	 * kill it immediately.
161328a6d671SEric W. Biederman 	 */
161428a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
161528a6d671SEric W. Biederman }
161628a6d671SEric W. Biederman 
1617d72f71ebSAl Viro static const struct dentry_operations pid_dentry_operations =
161828a6d671SEric W. Biederman {
161928a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
162028a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
162128a6d671SEric W. Biederman };
162228a6d671SEric W. Biederman 
162328a6d671SEric W. Biederman /* Lookups */
162428a6d671SEric W. Biederman 
1625c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1626c5141e6dSEric Dumazet 				struct task_struct *, const void *);
162761a28784SEric W. Biederman 
16281c0d04c9SEric W. Biederman /*
16291c0d04c9SEric W. Biederman  * Fill a directory entry.
16301c0d04c9SEric W. Biederman  *
16311c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16321c0d04c9SEric W. Biederman  * file type from dcache entry.
16331c0d04c9SEric W. Biederman  *
16341c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16351c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16361c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16371c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16381c0d04c9SEric W. Biederman  * by stat.
16391c0d04c9SEric W. Biederman  */
164061a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
164161a28784SEric W. Biederman 	char *name, int len,
1642c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
164361a28784SEric W. Biederman {
16442fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
164561a28784SEric W. Biederman 	struct inode *inode;
164661a28784SEric W. Biederman 	struct qstr qname;
164761a28784SEric W. Biederman 	ino_t ino = 0;
164861a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
164961a28784SEric W. Biederman 
165061a28784SEric W. Biederman 	qname.name = name;
165161a28784SEric W. Biederman 	qname.len  = len;
165261a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
165361a28784SEric W. Biederman 
165461a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
165561a28784SEric W. Biederman 	if (!child) {
165661a28784SEric W. Biederman 		struct dentry *new;
165761a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
165861a28784SEric W. Biederman 		if (new) {
165961a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
166061a28784SEric W. Biederman 			if (child)
166161a28784SEric W. Biederman 				dput(new);
166261a28784SEric W. Biederman 			else
166361a28784SEric W. Biederman 				child = new;
166461a28784SEric W. Biederman 		}
166561a28784SEric W. Biederman 	}
166661a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
166761a28784SEric W. Biederman 		goto end_instantiate;
166861a28784SEric W. Biederman 	inode = child->d_inode;
166961a28784SEric W. Biederman 	if (inode) {
167061a28784SEric W. Biederman 		ino = inode->i_ino;
167161a28784SEric W. Biederman 		type = inode->i_mode >> 12;
167261a28784SEric W. Biederman 	}
167361a28784SEric W. Biederman 	dput(child);
167461a28784SEric W. Biederman end_instantiate:
167561a28784SEric W. Biederman 	if (!ino)
167661a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
167761a28784SEric W. Biederman 	if (!ino)
167861a28784SEric W. Biederman 		ino = 1;
167961a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
168061a28784SEric W. Biederman }
168161a28784SEric W. Biederman 
168228a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
168328a6d671SEric W. Biederman {
168428a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
168528a6d671SEric W. Biederman 	int len = dentry->d_name.len;
168628a6d671SEric W. Biederman 	unsigned n = 0;
168728a6d671SEric W. Biederman 
168828a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
168928a6d671SEric W. Biederman 		goto out;
169028a6d671SEric W. Biederman 	while (len-- > 0) {
169128a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
169228a6d671SEric W. Biederman 		if (c > 9)
169328a6d671SEric W. Biederman 			goto out;
169428a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
169528a6d671SEric W. Biederman 			goto out;
169628a6d671SEric W. Biederman 		n *= 10;
169728a6d671SEric W. Biederman 		n += c;
169828a6d671SEric W. Biederman 	}
169928a6d671SEric W. Biederman 	return n;
170028a6d671SEric W. Biederman out:
170128a6d671SEric W. Biederman 	return ~0U;
170228a6d671SEric W. Biederman }
170328a6d671SEric W. Biederman 
170427932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
170527932742SMiklos Szeredi 
17063dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
170728a6d671SEric W. Biederman {
170828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
170928a6d671SEric W. Biederman 	struct files_struct *files = NULL;
171028a6d671SEric W. Biederman 	struct file *file;
171128a6d671SEric W. Biederman 	int fd = proc_fd(inode);
171228a6d671SEric W. Biederman 
171328a6d671SEric W. Biederman 	if (task) {
171428a6d671SEric W. Biederman 		files = get_files_struct(task);
171528a6d671SEric W. Biederman 		put_task_struct(task);
171628a6d671SEric W. Biederman 	}
171728a6d671SEric W. Biederman 	if (files) {
171828a6d671SEric W. Biederman 		/*
171928a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
172028a6d671SEric W. Biederman 		 * hold ->file_lock.
172128a6d671SEric W. Biederman 		 */
172228a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
172328a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
172428a6d671SEric W. Biederman 		if (file) {
17253dcd25f3SJan Blunck 			if (path) {
17263dcd25f3SJan Blunck 				*path = file->f_path;
17273dcd25f3SJan Blunck 				path_get(&file->f_path);
17283dcd25f3SJan Blunck 			}
172927932742SMiklos Szeredi 			if (info)
173027932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
173127932742SMiklos Szeredi 					 "pos:\t%lli\n"
173227932742SMiklos Szeredi 					 "flags:\t0%o\n",
173327932742SMiklos Szeredi 					 (long long) file->f_pos,
173427932742SMiklos Szeredi 					 file->f_flags);
173528a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
173628a6d671SEric W. Biederman 			put_files_struct(files);
173728a6d671SEric W. Biederman 			return 0;
173828a6d671SEric W. Biederman 		}
173928a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
174028a6d671SEric W. Biederman 		put_files_struct(files);
174128a6d671SEric W. Biederman 	}
174228a6d671SEric W. Biederman 	return -ENOENT;
174328a6d671SEric W. Biederman }
174428a6d671SEric W. Biederman 
17453dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path)
174627932742SMiklos Szeredi {
17473dcd25f3SJan Blunck 	return proc_fd_info(inode, path, NULL);
174827932742SMiklos Szeredi }
174927932742SMiklos Szeredi 
175028a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
175128a6d671SEric W. Biederman {
175228a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
175328a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
175428a6d671SEric W. Biederman 	int fd = proc_fd(inode);
175528a6d671SEric W. Biederman 	struct files_struct *files;
1756c69e8d9cSDavid Howells 	const struct cred *cred;
175728a6d671SEric W. Biederman 
175828a6d671SEric W. Biederman 	if (task) {
175928a6d671SEric W. Biederman 		files = get_files_struct(task);
176028a6d671SEric W. Biederman 		if (files) {
176128a6d671SEric W. Biederman 			rcu_read_lock();
176228a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
176328a6d671SEric W. Biederman 				rcu_read_unlock();
176428a6d671SEric W. Biederman 				put_files_struct(files);
176528a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1766c69e8d9cSDavid Howells 					rcu_read_lock();
1767c69e8d9cSDavid Howells 					cred = __task_cred(task);
1768c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1769c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1770c69e8d9cSDavid Howells 					rcu_read_unlock();
177128a6d671SEric W. Biederman 				} else {
177228a6d671SEric W. Biederman 					inode->i_uid = 0;
177328a6d671SEric W. Biederman 					inode->i_gid = 0;
177428a6d671SEric W. Biederman 				}
177528a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
177628a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
177728a6d671SEric W. Biederman 				put_task_struct(task);
177828a6d671SEric W. Biederman 				return 1;
177928a6d671SEric W. Biederman 			}
178028a6d671SEric W. Biederman 			rcu_read_unlock();
178128a6d671SEric W. Biederman 			put_files_struct(files);
178228a6d671SEric W. Biederman 		}
178328a6d671SEric W. Biederman 		put_task_struct(task);
178428a6d671SEric W. Biederman 	}
178528a6d671SEric W. Biederman 	d_drop(dentry);
178628a6d671SEric W. Biederman 	return 0;
178728a6d671SEric W. Biederman }
178828a6d671SEric W. Biederman 
1789d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
179028a6d671SEric W. Biederman {
179128a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
179228a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
179328a6d671SEric W. Biederman };
179428a6d671SEric W. Biederman 
1795444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1796c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
179728a6d671SEric W. Biederman {
1798c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
179928a6d671SEric W. Biederman 	struct file *file;
180028a6d671SEric W. Biederman 	struct files_struct *files;
180128a6d671SEric W. Biederman  	struct inode *inode;
180228a6d671SEric W. Biederman  	struct proc_inode *ei;
1803444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
180428a6d671SEric W. Biederman 
180561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
180628a6d671SEric W. Biederman 	if (!inode)
180728a6d671SEric W. Biederman 		goto out;
180828a6d671SEric W. Biederman 	ei = PROC_I(inode);
180928a6d671SEric W. Biederman 	ei->fd = fd;
181028a6d671SEric W. Biederman 	files = get_files_struct(task);
181128a6d671SEric W. Biederman 	if (!files)
1812444ceed8SEric W. Biederman 		goto out_iput;
181328a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
181428a6d671SEric W. Biederman 
181528a6d671SEric W. Biederman 	/*
181628a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
181728a6d671SEric W. Biederman 	 * hold ->file_lock.
181828a6d671SEric W. Biederman 	 */
181928a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
182028a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
182128a6d671SEric W. Biederman 	if (!file)
1822444ceed8SEric W. Biederman 		goto out_unlock;
1823aeb5d727SAl Viro 	if (file->f_mode & FMODE_READ)
182428a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
1825aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
182628a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
182728a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
182828a6d671SEric W. Biederman 	put_files_struct(files);
1829444ceed8SEric W. Biederman 
183028a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
183128a6d671SEric W. Biederman 	inode->i_size = 64;
183228a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
183328a6d671SEric W. Biederman 	dentry->d_op = &tid_fd_dentry_operations;
183428a6d671SEric W. Biederman 	d_add(dentry, inode);
183528a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
183628a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1837444ceed8SEric W. Biederman 		error = NULL;
1838444ceed8SEric W. Biederman 
1839444ceed8SEric W. Biederman  out:
1840444ceed8SEric W. Biederman 	return error;
1841444ceed8SEric W. Biederman out_unlock:
1842444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1843444ceed8SEric W. Biederman 	put_files_struct(files);
1844444ceed8SEric W. Biederman out_iput:
1845444ceed8SEric W. Biederman 	iput(inode);
1846444ceed8SEric W. Biederman 	goto out;
1847444ceed8SEric W. Biederman }
1848444ceed8SEric W. Biederman 
184927932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
185027932742SMiklos Szeredi 					   struct dentry *dentry,
185127932742SMiklos Szeredi 					   instantiate_t instantiate)
1852444ceed8SEric W. Biederman {
1853444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1854444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1855444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1856444ceed8SEric W. Biederman 
1857444ceed8SEric W. Biederman 	if (!task)
1858444ceed8SEric W. Biederman 		goto out_no_task;
1859444ceed8SEric W. Biederman 	if (fd == ~0U)
1860444ceed8SEric W. Biederman 		goto out;
1861444ceed8SEric W. Biederman 
186227932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
186328a6d671SEric W. Biederman out:
186428a6d671SEric W. Biederman 	put_task_struct(task);
186528a6d671SEric W. Biederman out_no_task:
186628a6d671SEric W. Biederman 	return result;
186728a6d671SEric W. Biederman }
186828a6d671SEric W. Biederman 
186927932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
187027932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
18711da177e4SLinus Torvalds {
18722fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
18735634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
187499f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1875457c2510SPavel Emelyanov 	unsigned int fd, ino;
18761da177e4SLinus Torvalds 	int retval;
18771da177e4SLinus Torvalds 	struct files_struct * files;
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	retval = -ENOENT;
188099f89551SEric W. Biederman 	if (!p)
188199f89551SEric W. Biederman 		goto out_no_task;
18821da177e4SLinus Torvalds 	retval = 0;
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 	fd = filp->f_pos;
18851da177e4SLinus Torvalds 	switch (fd) {
18861da177e4SLinus Torvalds 		case 0:
18871da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
18881da177e4SLinus Torvalds 				goto out;
18891da177e4SLinus Torvalds 			filp->f_pos++;
18901da177e4SLinus Torvalds 		case 1:
18915634708bSEric W. Biederman 			ino = parent_ino(dentry);
18921da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
18931da177e4SLinus Torvalds 				goto out;
18941da177e4SLinus Torvalds 			filp->f_pos++;
18951da177e4SLinus Torvalds 		default:
18961da177e4SLinus Torvalds 			files = get_files_struct(p);
18971da177e4SLinus Torvalds 			if (!files)
18981da177e4SLinus Torvalds 				goto out;
1899b835996fSDipankar Sarma 			rcu_read_lock();
19001da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
19019b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
19021da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
190327932742SMiklos Szeredi 				char name[PROC_NUMBUF];
190427932742SMiklos Szeredi 				int len;
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
19071da177e4SLinus Torvalds 					continue;
1908b835996fSDipankar Sarma 				rcu_read_unlock();
19091da177e4SLinus Torvalds 
191027932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
191127932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
191227932742SMiklos Szeredi 						    name, len, instantiate,
191327932742SMiklos Szeredi 						    p, &fd) < 0) {
1914b835996fSDipankar Sarma 					rcu_read_lock();
19151da177e4SLinus Torvalds 					break;
19161da177e4SLinus Torvalds 				}
1917b835996fSDipankar Sarma 				rcu_read_lock();
19181da177e4SLinus Torvalds 			}
1919b835996fSDipankar Sarma 			rcu_read_unlock();
19201da177e4SLinus Torvalds 			put_files_struct(files);
19211da177e4SLinus Torvalds 	}
19221da177e4SLinus Torvalds out:
192399f89551SEric W. Biederman 	put_task_struct(p);
192499f89551SEric W. Biederman out_no_task:
19251da177e4SLinus Torvalds 	return retval;
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds 
192827932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
192927932742SMiklos Szeredi 				    struct nameidata *nd)
193027932742SMiklos Szeredi {
193127932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
193227932742SMiklos Szeredi }
193327932742SMiklos Szeredi 
193427932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
193527932742SMiklos Szeredi {
193627932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
193727932742SMiklos Szeredi }
193827932742SMiklos Szeredi 
193927932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
194027932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
194127932742SMiklos Szeredi {
194227932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
19433dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
194427932742SMiklos Szeredi 	if (!err)
194527932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
194627932742SMiklos Szeredi 	return err;
194727932742SMiklos Szeredi }
194827932742SMiklos Szeredi 
194927932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
195027932742SMiklos Szeredi 	.open		= nonseekable_open,
195127932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
195227932742SMiklos Szeredi };
195327932742SMiklos Szeredi 
195400977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
19551da177e4SLinus Torvalds 	.read		= generic_read_dir,
19561da177e4SLinus Torvalds 	.readdir	= proc_readfd,
19571da177e4SLinus Torvalds };
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds /*
19608948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
19618948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
19628948e11fSAlexey Dobriyan  */
1963e6305c43SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
19648948e11fSAlexey Dobriyan {
19658948e11fSAlexey Dobriyan 	int rv;
19668948e11fSAlexey Dobriyan 
19678948e11fSAlexey Dobriyan 	rv = generic_permission(inode, mask, NULL);
19688948e11fSAlexey Dobriyan 	if (rv == 0)
19698948e11fSAlexey Dobriyan 		return 0;
19708948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
19718948e11fSAlexey Dobriyan 		rv = 0;
19728948e11fSAlexey Dobriyan 	return rv;
19738948e11fSAlexey Dobriyan }
19748948e11fSAlexey Dobriyan 
19758948e11fSAlexey Dobriyan /*
19761da177e4SLinus Torvalds  * proc directories can do almost nothing..
19771da177e4SLinus Torvalds  */
1978c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
19791da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
19808948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
19816d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
19821da177e4SLinus Torvalds };
19831da177e4SLinus Torvalds 
198427932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
198527932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
198627932742SMiklos Szeredi {
198727932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
198827932742SMiklos Szeredi  	struct inode *inode;
198927932742SMiklos Szeredi  	struct proc_inode *ei;
199027932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
199127932742SMiklos Szeredi 
199227932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
199327932742SMiklos Szeredi 	if (!inode)
199427932742SMiklos Szeredi 		goto out;
199527932742SMiklos Szeredi 	ei = PROC_I(inode);
199627932742SMiklos Szeredi 	ei->fd = fd;
199727932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
199827932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
199927932742SMiklos Szeredi 	dentry->d_op = &tid_fd_dentry_operations;
200027932742SMiklos Szeredi 	d_add(dentry, inode);
200127932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
200227932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
200327932742SMiklos Szeredi 		error = NULL;
200427932742SMiklos Szeredi 
200527932742SMiklos Szeredi  out:
200627932742SMiklos Szeredi 	return error;
200727932742SMiklos Szeredi }
200827932742SMiklos Szeredi 
200927932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
201027932742SMiklos Szeredi 					struct dentry *dentry,
201127932742SMiklos Szeredi 					struct nameidata *nd)
201227932742SMiklos Szeredi {
201327932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
201427932742SMiklos Szeredi }
201527932742SMiklos Szeredi 
201627932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
201727932742SMiklos Szeredi {
201827932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
201927932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
202027932742SMiklos Szeredi }
202127932742SMiklos Szeredi 
202227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
202327932742SMiklos Szeredi 	.read		= generic_read_dir,
202427932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
202527932742SMiklos Szeredi };
202627932742SMiklos Szeredi 
202727932742SMiklos Szeredi /*
202827932742SMiklos Szeredi  * proc directories can do almost nothing..
202927932742SMiklos Szeredi  */
203027932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
203127932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
203227932742SMiklos Szeredi 	.setattr	= proc_setattr,
203327932742SMiklos Szeredi };
203427932742SMiklos Szeredi 
203527932742SMiklos Szeredi 
2036444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2037c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2038444ceed8SEric W. Biederman {
2039c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2040444ceed8SEric W. Biederman 	struct inode *inode;
2041444ceed8SEric W. Biederman 	struct proc_inode *ei;
2042bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2043444ceed8SEric W. Biederman 
204461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2045444ceed8SEric W. Biederman 	if (!inode)
2046444ceed8SEric W. Biederman 		goto out;
2047444ceed8SEric W. Biederman 
2048444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2049444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2050444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2051444ceed8SEric W. Biederman 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
2052444ceed8SEric W. Biederman 	if (p->iop)
2053444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2054444ceed8SEric W. Biederman 	if (p->fop)
2055444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2056444ceed8SEric W. Biederman 	ei->op = p->op;
2057444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2058444ceed8SEric W. Biederman 	d_add(dentry, inode);
2059444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2060444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2061444ceed8SEric W. Biederman 		error = NULL;
2062444ceed8SEric W. Biederman out:
2063444ceed8SEric W. Biederman 	return error;
2064444ceed8SEric W. Biederman }
2065444ceed8SEric W. Biederman 
20661da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
20671da177e4SLinus Torvalds 					 struct dentry *dentry,
2068c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
20697bcd6b0eSEric W. Biederman 					 unsigned int nents)
20701da177e4SLinus Torvalds {
2071cd6a3ce9SEric W. Biederman 	struct dentry *error;
207299f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2073c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
20741da177e4SLinus Torvalds 
2075cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
20761da177e4SLinus Torvalds 
207799f89551SEric W. Biederman 	if (!task)
207899f89551SEric W. Biederman 		goto out_no_task;
20791da177e4SLinus Torvalds 
208020cdc894SEric W. Biederman 	/*
208120cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
208220cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
208320cdc894SEric W. Biederman 	 */
20847bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
20857bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
20861da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
20871da177e4SLinus Torvalds 			continue;
20881da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
20891da177e4SLinus Torvalds 			break;
20901da177e4SLinus Torvalds 	}
20917bcd6b0eSEric W. Biederman 	if (p > last)
20921da177e4SLinus Torvalds 		goto out;
20931da177e4SLinus Torvalds 
2094444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
20951da177e4SLinus Torvalds out:
209699f89551SEric W. Biederman 	put_task_struct(task);
209799f89551SEric W. Biederman out_no_task:
2098cd6a3ce9SEric W. Biederman 	return error;
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
2101c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2102c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
210361a28784SEric W. Biederman {
210461a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
210561a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
210661a28784SEric W. Biederman }
210761a28784SEric W. Biederman 
210828a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
210928a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2110c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
211128a6d671SEric W. Biederman {
211228a6d671SEric W. Biederman 	int i;
21132fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
211428a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
211528a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2116c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
211728a6d671SEric W. Biederman 	ino_t ino;
211828a6d671SEric W. Biederman 	int ret;
211928a6d671SEric W. Biederman 
212028a6d671SEric W. Biederman 	ret = -ENOENT;
212128a6d671SEric W. Biederman 	if (!task)
212261a28784SEric W. Biederman 		goto out_no_task;
212328a6d671SEric W. Biederman 
212428a6d671SEric W. Biederman 	ret = 0;
212528a6d671SEric W. Biederman 	i = filp->f_pos;
212628a6d671SEric W. Biederman 	switch (i) {
212728a6d671SEric W. Biederman 	case 0:
212828a6d671SEric W. Biederman 		ino = inode->i_ino;
212928a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
213028a6d671SEric W. Biederman 			goto out;
213128a6d671SEric W. Biederman 		i++;
213228a6d671SEric W. Biederman 		filp->f_pos++;
213328a6d671SEric W. Biederman 		/* fall through */
213428a6d671SEric W. Biederman 	case 1:
213528a6d671SEric W. Biederman 		ino = parent_ino(dentry);
213628a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
213728a6d671SEric W. Biederman 			goto out;
213828a6d671SEric W. Biederman 		i++;
213928a6d671SEric W. Biederman 		filp->f_pos++;
214028a6d671SEric W. Biederman 		/* fall through */
214128a6d671SEric W. Biederman 	default:
214228a6d671SEric W. Biederman 		i -= 2;
214328a6d671SEric W. Biederman 		if (i >= nents) {
214428a6d671SEric W. Biederman 			ret = 1;
214528a6d671SEric W. Biederman 			goto out;
214628a6d671SEric W. Biederman 		}
214728a6d671SEric W. Biederman 		p = ents + i;
21487bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
21497bcd6b0eSEric W. Biederman 		while (p <= last) {
215061a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
215128a6d671SEric W. Biederman 				goto out;
215228a6d671SEric W. Biederman 			filp->f_pos++;
215328a6d671SEric W. Biederman 			p++;
215428a6d671SEric W. Biederman 		}
21551da177e4SLinus Torvalds 	}
21561da177e4SLinus Torvalds 
215728a6d671SEric W. Biederman 	ret = 1;
215828a6d671SEric W. Biederman out:
215961a28784SEric W. Biederman 	put_task_struct(task);
216061a28784SEric W. Biederman out_no_task:
216128a6d671SEric W. Biederman 	return ret;
21621da177e4SLinus Torvalds }
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
216528a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
216628a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
216728a6d671SEric W. Biederman {
21682fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
216904ff9708SAl Viro 	char *p = NULL;
217028a6d671SEric W. Biederman 	ssize_t length;
217128a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
217228a6d671SEric W. Biederman 
217328a6d671SEric W. Biederman 	if (!task)
217404ff9708SAl Viro 		return -ESRCH;
217528a6d671SEric W. Biederman 
217628a6d671SEric W. Biederman 	length = security_getprocattr(task,
21772fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
217804ff9708SAl Viro 				      &p);
217928a6d671SEric W. Biederman 	put_task_struct(task);
218004ff9708SAl Viro 	if (length > 0)
218104ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
218204ff9708SAl Viro 	kfree(p);
218328a6d671SEric W. Biederman 	return length;
218428a6d671SEric W. Biederman }
218528a6d671SEric W. Biederman 
218628a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
218728a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
218828a6d671SEric W. Biederman {
21892fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
219028a6d671SEric W. Biederman 	char *page;
219128a6d671SEric W. Biederman 	ssize_t length;
219228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
219328a6d671SEric W. Biederman 
219428a6d671SEric W. Biederman 	length = -ESRCH;
219528a6d671SEric W. Biederman 	if (!task)
219628a6d671SEric W. Biederman 		goto out_no_task;
219728a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
219828a6d671SEric W. Biederman 		count = PAGE_SIZE;
219928a6d671SEric W. Biederman 
220028a6d671SEric W. Biederman 	/* No partial writes. */
220128a6d671SEric W. Biederman 	length = -EINVAL;
220228a6d671SEric W. Biederman 	if (*ppos != 0)
220328a6d671SEric W. Biederman 		goto out;
220428a6d671SEric W. Biederman 
220528a6d671SEric W. Biederman 	length = -ENOMEM;
2206e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
220728a6d671SEric W. Biederman 	if (!page)
220828a6d671SEric W. Biederman 		goto out;
220928a6d671SEric W. Biederman 
221028a6d671SEric W. Biederman 	length = -EFAULT;
221128a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
221228a6d671SEric W. Biederman 		goto out_free;
221328a6d671SEric W. Biederman 
2214107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
2215107db7c7SDavid Howells 	length = mutex_lock_interruptible(&task->cred_guard_mutex);
2216107db7c7SDavid Howells 	if (length < 0)
2217107db7c7SDavid Howells 		goto out_free;
2218107db7c7SDavid Howells 
221928a6d671SEric W. Biederman 	length = security_setprocattr(task,
22202fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
222128a6d671SEric W. Biederman 				      (void*)page, count);
2222107db7c7SDavid Howells 	mutex_unlock(&task->cred_guard_mutex);
222328a6d671SEric W. Biederman out_free:
222428a6d671SEric W. Biederman 	free_page((unsigned long) page);
222528a6d671SEric W. Biederman out:
222628a6d671SEric W. Biederman 	put_task_struct(task);
222728a6d671SEric W. Biederman out_no_task:
222828a6d671SEric W. Biederman 	return length;
222928a6d671SEric W. Biederman }
223028a6d671SEric W. Biederman 
223100977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
223228a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
223328a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
223428a6d671SEric W. Biederman };
223528a6d671SEric W. Biederman 
2236c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2237631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2238631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2239631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2240631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2241631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2242631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
224328a6d671SEric W. Biederman };
224428a6d671SEric W. Biederman 
224572d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
22461da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
22471da177e4SLinus Torvalds {
22481da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
224972d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
22501da177e4SLinus Torvalds }
22511da177e4SLinus Torvalds 
225200977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
22531da177e4SLinus Torvalds 	.read		= generic_read_dir,
225472d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
22551da177e4SLinus Torvalds };
22561da177e4SLinus Torvalds 
225772d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
22581da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
22591da177e4SLinus Torvalds {
22607bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
22617bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
22621da177e4SLinus Torvalds }
22631da177e4SLinus Torvalds 
2264c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
226572d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
226699f89551SEric W. Biederman 	.getattr	= pid_getattr,
22676d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
22681da177e4SLinus Torvalds };
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds #endif
22711da177e4SLinus Torvalds 
2272698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
22733cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
22743cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
22753cb4a0bbSKawai, Hidehiro {
22763cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
22773cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
22783cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
22793cb4a0bbSKawai, Hidehiro 	size_t len;
22803cb4a0bbSKawai, Hidehiro 	int ret;
22813cb4a0bbSKawai, Hidehiro 
22823cb4a0bbSKawai, Hidehiro 	if (!task)
22833cb4a0bbSKawai, Hidehiro 		return -ESRCH;
22843cb4a0bbSKawai, Hidehiro 
22853cb4a0bbSKawai, Hidehiro 	ret = 0;
22863cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
22873cb4a0bbSKawai, Hidehiro 	if (mm) {
22883cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
22893cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
22903cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
22913cb4a0bbSKawai, Hidehiro 		mmput(mm);
22923cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
22933cb4a0bbSKawai, Hidehiro 	}
22943cb4a0bbSKawai, Hidehiro 
22953cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
22963cb4a0bbSKawai, Hidehiro 
22973cb4a0bbSKawai, Hidehiro 	return ret;
22983cb4a0bbSKawai, Hidehiro }
22993cb4a0bbSKawai, Hidehiro 
23003cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
23013cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
23023cb4a0bbSKawai, Hidehiro 					  size_t count,
23033cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
23043cb4a0bbSKawai, Hidehiro {
23053cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
23063cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
23073cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
23083cb4a0bbSKawai, Hidehiro 	unsigned int val;
23093cb4a0bbSKawai, Hidehiro 	int ret;
23103cb4a0bbSKawai, Hidehiro 	int i;
23113cb4a0bbSKawai, Hidehiro 	unsigned long mask;
23123cb4a0bbSKawai, Hidehiro 
23133cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
23143cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
23153cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
23163cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
23173cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
23183cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23193cb4a0bbSKawai, Hidehiro 
23203cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
23213cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
23223cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
23233cb4a0bbSKawai, Hidehiro 		end++;
23243cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
23253cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23263cb4a0bbSKawai, Hidehiro 
23273cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
23283cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
23293cb4a0bbSKawai, Hidehiro 	if (!task)
23303cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23313cb4a0bbSKawai, Hidehiro 
23323cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
23333cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
23343cb4a0bbSKawai, Hidehiro 	if (!mm)
23353cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
23363cb4a0bbSKawai, Hidehiro 
23373cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
23383cb4a0bbSKawai, Hidehiro 		if (val & mask)
23393cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23403cb4a0bbSKawai, Hidehiro 		else
23413cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23423cb4a0bbSKawai, Hidehiro 	}
23433cb4a0bbSKawai, Hidehiro 
23443cb4a0bbSKawai, Hidehiro 	mmput(mm);
23453cb4a0bbSKawai, Hidehiro  out_no_mm:
23463cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
23473cb4a0bbSKawai, Hidehiro  out_no_task:
23483cb4a0bbSKawai, Hidehiro 	return ret;
23493cb4a0bbSKawai, Hidehiro }
23503cb4a0bbSKawai, Hidehiro 
23513cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
23523cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
23533cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
23543cb4a0bbSKawai, Hidehiro };
23553cb4a0bbSKawai, Hidehiro #endif
23563cb4a0bbSKawai, Hidehiro 
23571da177e4SLinus Torvalds /*
23581da177e4SLinus Torvalds  * /proc/self:
23591da177e4SLinus Torvalds  */
23601da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
23611da177e4SLinus Torvalds 			      int buflen)
23621da177e4SLinus Torvalds {
2363488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2364b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
23658578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2366b55fcb22SAndrew Morton 	if (!tgid)
2367488e5bc4SEric W. Biederman 		return -ENOENT;
2368b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
23691da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
23701da177e4SLinus Torvalds }
23711da177e4SLinus Torvalds 
2372008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
23731da177e4SLinus Torvalds {
2374488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2375b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
23767fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
23777fee4868SAl Viro 	if (tgid) {
23787fee4868SAl Viro 		name = __getname();
23797fee4868SAl Viro 		if (!name)
23807fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
23817fee4868SAl Viro 		else
23827fee4868SAl Viro 			sprintf(name, "%d", tgid);
23837fee4868SAl Viro 	}
23847fee4868SAl Viro 	nd_set_link(nd, name);
23857fee4868SAl Viro 	return NULL;
23867fee4868SAl Viro }
23877fee4868SAl Viro 
23887fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
23897fee4868SAl Viro 				void *cookie)
23907fee4868SAl Viro {
23917fee4868SAl Viro 	char *s = nd_get_link(nd);
23927fee4868SAl Viro 	if (!IS_ERR(s))
23937fee4868SAl Viro 		__putname(s);
23941da177e4SLinus Torvalds }
23951da177e4SLinus Torvalds 
2396c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
23971da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
23981da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
23997fee4868SAl Viro 	.put_link	= proc_self_put_link,
24001da177e4SLinus Torvalds };
24011da177e4SLinus Torvalds 
240228a6d671SEric W. Biederman /*
2403801199ceSEric W. Biederman  * proc base
2404801199ceSEric W. Biederman  *
2405801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2406801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2407801199ceSEric W. Biederman  * describe something that is process related.
2408801199ceSEric W. Biederman  */
2409c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
241061a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2411801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2412801199ceSEric W. Biederman };
2413801199ceSEric W. Biederman 
2414801199ceSEric W. Biederman /*
2415801199ceSEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
2416801199ceSEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
2417801199ceSEric W. Biederman  * due to the way we treat inodes.
2418801199ceSEric W. Biederman  */
2419801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2420801199ceSEric W. Biederman {
2421801199ceSEric W. Biederman 	struct inode *inode = dentry->d_inode;
2422801199ceSEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2423801199ceSEric W. Biederman 	if (task) {
2424801199ceSEric W. Biederman 		put_task_struct(task);
2425801199ceSEric W. Biederman 		return 1;
2426801199ceSEric W. Biederman 	}
2427801199ceSEric W. Biederman 	d_drop(dentry);
2428801199ceSEric W. Biederman 	return 0;
2429801199ceSEric W. Biederman }
2430801199ceSEric W. Biederman 
2431d72f71ebSAl Viro static const struct dentry_operations proc_base_dentry_operations =
2432801199ceSEric W. Biederman {
2433801199ceSEric W. Biederman 	.d_revalidate	= proc_base_revalidate,
2434801199ceSEric W. Biederman 	.d_delete	= pid_delete_dentry,
2435801199ceSEric W. Biederman };
2436801199ceSEric W. Biederman 
2437444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2438c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2439801199ceSEric W. Biederman {
2440c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2441801199ceSEric W. Biederman 	struct inode *inode;
2442801199ceSEric W. Biederman 	struct proc_inode *ei;
2443444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-EINVAL);
2444801199ceSEric W. Biederman 
2445801199ceSEric W. Biederman 	/* Allocate the inode */
2446801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2447801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2448801199ceSEric W. Biederman 	if (!inode)
2449801199ceSEric W. Biederman 		goto out;
2450801199ceSEric W. Biederman 
2451801199ceSEric W. Biederman 	/* Initialize the inode */
2452801199ceSEric W. Biederman 	ei = PROC_I(inode);
2453801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2454801199ceSEric W. Biederman 
2455801199ceSEric W. Biederman 	/*
2456801199ceSEric W. Biederman 	 * grab the reference to the task.
2457801199ceSEric W. Biederman 	 */
24581a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2459801199ceSEric W. Biederman 	if (!ei->pid)
2460801199ceSEric W. Biederman 		goto out_iput;
2461801199ceSEric W. Biederman 
2462801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2463801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2464801199ceSEric W. Biederman 		inode->i_nlink = 2;
2465801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2466801199ceSEric W. Biederman 		inode->i_size = 64;
2467801199ceSEric W. Biederman 	if (p->iop)
2468801199ceSEric W. Biederman 		inode->i_op = p->iop;
2469801199ceSEric W. Biederman 	if (p->fop)
2470801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2471801199ceSEric W. Biederman 	ei->op = p->op;
2472801199ceSEric W. Biederman 	dentry->d_op = &proc_base_dentry_operations;
2473801199ceSEric W. Biederman 	d_add(dentry, inode);
2474801199ceSEric W. Biederman 	error = NULL;
2475801199ceSEric W. Biederman out:
2476801199ceSEric W. Biederman 	return error;
2477801199ceSEric W. Biederman out_iput:
2478801199ceSEric W. Biederman 	iput(inode);
2479801199ceSEric W. Biederman 	goto out;
2480801199ceSEric W. Biederman }
2481801199ceSEric W. Biederman 
2482444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2483444ceed8SEric W. Biederman {
2484444ceed8SEric W. Biederman 	struct dentry *error;
2485444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2486c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2487444ceed8SEric W. Biederman 
2488444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2489444ceed8SEric W. Biederman 
2490444ceed8SEric W. Biederman 	if (!task)
2491444ceed8SEric W. Biederman 		goto out_no_task;
2492444ceed8SEric W. Biederman 
2493444ceed8SEric W. Biederman 	/* Lookup the directory entry */
24947bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
24957bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2496444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2497444ceed8SEric W. Biederman 			continue;
2498444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2499444ceed8SEric W. Biederman 			break;
2500444ceed8SEric W. Biederman 	}
25017bcd6b0eSEric W. Biederman 	if (p > last)
2502444ceed8SEric W. Biederman 		goto out;
2503444ceed8SEric W. Biederman 
2504444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2505444ceed8SEric W. Biederman 
2506444ceed8SEric W. Biederman out:
2507444ceed8SEric W. Biederman 	put_task_struct(task);
2508444ceed8SEric W. Biederman out_no_task:
2509444ceed8SEric W. Biederman 	return error;
2510444ceed8SEric W. Biederman }
2511444ceed8SEric W. Biederman 
2512c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2513c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
251461a28784SEric W. Biederman {
251561a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
251661a28784SEric W. Biederman 				proc_base_instantiate, task, p);
251761a28784SEric W. Biederman }
251861a28784SEric W. Biederman 
2519aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2520297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2521aba76fdbSAndrew Morton {
2522940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2523297c5d92SAndrea Righi 	unsigned long flags;
2524297c5d92SAndrea Righi 
25255995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2526b2d002dbSAndrea Righi 		struct task_struct *t = task;
2527297c5d92SAndrea Righi 
25285995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
25295995477aSAndrea Righi 		while_each_thread(task, t)
25305995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2531297c5d92SAndrea Righi 
2532297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2533297c5d92SAndrea Righi 	}
2534aba76fdbSAndrew Morton 	return sprintf(buffer,
2535aba76fdbSAndrew Morton 			"rchar: %llu\n"
2536aba76fdbSAndrew Morton 			"wchar: %llu\n"
2537aba76fdbSAndrew Morton 			"syscr: %llu\n"
2538aba76fdbSAndrew Morton 			"syscw: %llu\n"
2539aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2540aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2541aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
25427c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
25437c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
25447c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
25457c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
25467c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
25477c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
25487c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2549aba76fdbSAndrew Morton }
2550297c5d92SAndrea Righi 
2551297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2552297c5d92SAndrea Righi {
2553297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2554297c5d92SAndrea Righi }
2555297c5d92SAndrea Righi 
2556297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2557297c5d92SAndrea Righi {
2558297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2559297c5d92SAndrea Righi }
2560297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2561aba76fdbSAndrew Morton 
256247830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
256347830723SKees Cook 				struct pid *pid, struct task_struct *task)
256447830723SKees Cook {
256547830723SKees Cook 	seq_printf(m, "%08x\n", task->personality);
256647830723SKees Cook 	return 0;
256747830723SKees Cook }
256847830723SKees Cook 
2569801199ceSEric W. Biederman /*
257028a6d671SEric W. Biederman  * Thread groups
257128a6d671SEric W. Biederman  */
257200977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2573c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
257420cdc894SEric W. Biederman 
2575c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2576631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2577631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2578631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2579b2211a36SAndrew Morton #ifdef CONFIG_NET
2580631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2581b2211a36SAndrew Morton #endif
2582631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2583631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
2584631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
2585631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
2586631f9c18SAlexey Dobriyan 	INF("limits",	  S_IRUSR, proc_pid_limits),
258743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2588631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
258943ae34cbSIngo Molnar #endif
25904614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2591ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2592631f9c18SAlexey Dobriyan 	INF("syscall",    S_IRUSR, proc_pid_syscall),
2593ebcb6734SRoland McGrath #endif
2594631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2595631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2596631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2597631f9c18SAlexey Dobriyan 	REG("maps",       S_IRUGO, proc_maps_operations),
259828a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2599631f9c18SAlexey Dobriyan 	REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
260028a6d671SEric W. Biederman #endif
2601631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2602631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2603631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2604631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2605631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2606631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2607631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
26081e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2609631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2610631f9c18SAlexey Dobriyan 	REG("smaps",      S_IRUGO, proc_smaps_operations),
2611631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
261228a6d671SEric W. Biederman #endif
261328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2614631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
261528a6d671SEric W. Biederman #endif
261628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2617631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
261828a6d671SEric W. Biederman #endif
26192ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
26202ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
262128a6d671SEric W. Biederman #endif
262228a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2623631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
262428a6d671SEric W. Biederman #endif
26259745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2626631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
26279745512cSArjan van de Ven #endif
26288793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2629631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
263028a6d671SEric W. Biederman #endif
2631a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2632631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2633a424316cSPaul Menage #endif
2634631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
2635631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
263628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2637631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2638631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
263928a6d671SEric W. Biederman #endif
2640f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2641631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2642f4f154fdSAkinobu Mita #endif
2643698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2644631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
26453cb4a0bbSKawai, Hidehiro #endif
2646aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2647631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tgid_io_accounting),
2648aba76fdbSAndrew Morton #endif
264928a6d671SEric W. Biederman };
265028a6d671SEric W. Biederman 
265128a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
265228a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
265328a6d671SEric W. Biederman {
265428a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
265528a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
265628a6d671SEric W. Biederman }
265728a6d671SEric W. Biederman 
265800977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
265928a6d671SEric W. Biederman 	.read		= generic_read_dir,
266028a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
266128a6d671SEric W. Biederman };
266228a6d671SEric W. Biederman 
266328a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
26647bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26657bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
266628a6d671SEric W. Biederman }
266728a6d671SEric W. Biederman 
2668c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
266928a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
267028a6d671SEric W. Biederman 	.getattr	= pid_getattr,
267128a6d671SEric W. Biederman 	.setattr	= proc_setattr,
267228a6d671SEric W. Biederman };
267328a6d671SEric W. Biederman 
267460347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
26751da177e4SLinus Torvalds {
267648e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
26778578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
267848e6484dSEric W. Biederman 	struct qstr name;
26791da177e4SLinus Torvalds 
268048e6484dSEric W. Biederman 	name.name = buf;
268160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
268260347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
268348e6484dSEric W. Biederman 	if (dentry) {
268448e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
268548e6484dSEric W. Biederman 		d_drop(dentry);
268648e6484dSEric W. Biederman 		dput(dentry);
26871da177e4SLinus Torvalds 	}
26881da177e4SLinus Torvalds 
268948e6484dSEric W. Biederman 	name.name = buf;
269060347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
269160347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
269248e6484dSEric W. Biederman 	if (!leader)
269348e6484dSEric W. Biederman 		goto out;
269448e6484dSEric W. Biederman 
269548e6484dSEric W. Biederman 	name.name = "task";
269648e6484dSEric W. Biederman 	name.len = strlen(name.name);
269748e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
269848e6484dSEric W. Biederman 	if (!dir)
269948e6484dSEric W. Biederman 		goto out_put_leader;
270048e6484dSEric W. Biederman 
270148e6484dSEric W. Biederman 	name.name = buf;
270260347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
270348e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
270448e6484dSEric W. Biederman 	if (dentry) {
270548e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
270648e6484dSEric W. Biederman 		d_drop(dentry);
270748e6484dSEric W. Biederman 		dput(dentry);
27081da177e4SLinus Torvalds 	}
270948e6484dSEric W. Biederman 
271048e6484dSEric W. Biederman 	dput(dir);
271148e6484dSEric W. Biederman out_put_leader:
271248e6484dSEric W. Biederman 	dput(leader);
271348e6484dSEric W. Biederman out:
271448e6484dSEric W. Biederman 	return;
27151da177e4SLinus Torvalds }
27161da177e4SLinus Torvalds 
27170895e91dSRandy Dunlap /**
27180895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
27190895e91dSRandy Dunlap  * @task: task that should be flushed.
27200895e91dSRandy Dunlap  *
27210895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
272260347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
27230895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
27240895e91dSRandy Dunlap  *
27250895e91dSRandy Dunlap  * Looks in the dcache for
27260895e91dSRandy Dunlap  * /proc/@pid
27270895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
27280895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
27290895e91dSRandy Dunlap  * from the dcache.
27300895e91dSRandy Dunlap  *
27310895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
27320895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
27330895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
27340895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
27350895e91dSRandy Dunlap  * dcache entries at process exit time.
27360895e91dSRandy Dunlap  *
27370895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
27380895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
27390895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
274060347f67SPavel Emelyanov  */
274160347f67SPavel Emelyanov 
274260347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
274360347f67SPavel Emelyanov {
27449fcc2d15SEric W. Biederman 	int i;
27459b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2746130f77ecSPavel Emelyanov 	struct upid *upid;
2747130f77ecSPavel Emelyanov 
2748130f77ecSPavel Emelyanov 	pid = task_pid(task);
2749130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
27509fcc2d15SEric W. Biederman 
27519fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2752130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2753130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
27549b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2755130f77ecSPavel Emelyanov 	}
27566f4e6433SPavel Emelyanov 
27576f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
27586f4e6433SPavel Emelyanov 	if (upid->nr == 1)
27596f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
276060347f67SPavel Emelyanov }
276160347f67SPavel Emelyanov 
27629711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
27639711ef99SAdrian Bunk 					   struct dentry * dentry,
2764c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2765444ceed8SEric W. Biederman {
2766444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2767444ceed8SEric W. Biederman 	struct inode *inode;
2768444ceed8SEric W. Biederman 
276961a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2770444ceed8SEric W. Biederman 	if (!inode)
2771444ceed8SEric W. Biederman 		goto out;
2772444ceed8SEric W. Biederman 
2773444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2774444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2775444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2776444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2777aed54175SVegard Nossum 
2778aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2779aed54175SVegard Nossum 		ARRAY_SIZE(tgid_base_stuff));
2780444ceed8SEric W. Biederman 
2781444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2782444ceed8SEric W. Biederman 
2783444ceed8SEric W. Biederman 	d_add(dentry, inode);
2784444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2785444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2786444ceed8SEric W. Biederman 		error = NULL;
2787444ceed8SEric W. Biederman out:
2788444ceed8SEric W. Biederman 	return error;
2789444ceed8SEric W. Biederman }
2790444ceed8SEric W. Biederman 
27911da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
27921da177e4SLinus Torvalds {
2793cd6a3ce9SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
27941da177e4SLinus Torvalds 	struct task_struct *task;
27951da177e4SLinus Torvalds 	unsigned tgid;
2796b488893aSPavel Emelyanov 	struct pid_namespace *ns;
27971da177e4SLinus Torvalds 
2798801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
2799801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2800801199ceSEric W. Biederman 		goto out;
2801801199ceSEric W. Biederman 
28021da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
28031da177e4SLinus Torvalds 	if (tgid == ~0U)
28041da177e4SLinus Torvalds 		goto out;
28051da177e4SLinus Torvalds 
2806b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2807de758734SEric W. Biederman 	rcu_read_lock();
2808b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
28091da177e4SLinus Torvalds 	if (task)
28101da177e4SLinus Torvalds 		get_task_struct(task);
2811de758734SEric W. Biederman 	rcu_read_unlock();
28121da177e4SLinus Torvalds 	if (!task)
28131da177e4SLinus Torvalds 		goto out;
28141da177e4SLinus Torvalds 
2815444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
281648e6484dSEric W. Biederman 	put_task_struct(task);
28171da177e4SLinus Torvalds out:
2818cd6a3ce9SEric W. Biederman 	return result;
28191da177e4SLinus Torvalds }
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds /*
28220804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
28230bc58a91SEric W. Biederman  *
28241da177e4SLinus Torvalds  */
282519fd4bb2SEric W. Biederman struct tgid_iter {
282619fd4bb2SEric W. Biederman 	unsigned int tgid;
28270804ef4bSEric W. Biederman 	struct task_struct *task;
282819fd4bb2SEric W. Biederman };
282919fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
283019fd4bb2SEric W. Biederman {
28310804ef4bSEric W. Biederman 	struct pid *pid;
28321da177e4SLinus Torvalds 
283319fd4bb2SEric W. Biederman 	if (iter.task)
283419fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
28350804ef4bSEric W. Biederman 	rcu_read_lock();
28360804ef4bSEric W. Biederman retry:
283719fd4bb2SEric W. Biederman 	iter.task = NULL;
283819fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
28390804ef4bSEric W. Biederman 	if (pid) {
284019fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
284119fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
28420804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
28430804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
28440804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
28450804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
28460804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
28470804ef4bSEric W. Biederman 		 *
28480804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
28490804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
28500804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
28510804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
28520804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
28530bc58a91SEric W. Biederman 		 */
285419fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
285519fd4bb2SEric W. Biederman 			iter.tgid += 1;
28560804ef4bSEric W. Biederman 			goto retry;
285719fd4bb2SEric W. Biederman 		}
285819fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
28591da177e4SLinus Torvalds 	}
2860454cc105SEric W. Biederman 	rcu_read_unlock();
286119fd4bb2SEric W. Biederman 	return iter;
28621da177e4SLinus Torvalds }
28631da177e4SLinus Torvalds 
28647bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
28651da177e4SLinus Torvalds 
286661a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
286719fd4bb2SEric W. Biederman 	struct tgid_iter iter)
286861a28784SEric W. Biederman {
286961a28784SEric W. Biederman 	char name[PROC_NUMBUF];
287019fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
287161a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
287219fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
287361a28784SEric W. Biederman }
287461a28784SEric W. Biederman 
28751da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
28761da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
28771da177e4SLinus Torvalds {
28781da177e4SLinus Torvalds 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
28792fddfeefSJosef "Jeff" Sipek 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
288019fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2881b488893aSPavel Emelyanov 	struct pid_namespace *ns;
28821da177e4SLinus Torvalds 
288361a28784SEric W. Biederman 	if (!reaper)
288461a28784SEric W. Biederman 		goto out_no_task;
288561a28784SEric W. Biederman 
28867bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2887c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
288861a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2889801199ceSEric W. Biederman 			goto out;
28901da177e4SLinus Torvalds 	}
28911da177e4SLinus Torvalds 
2892b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
289319fd4bb2SEric W. Biederman 	iter.task = NULL;
289419fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
289519fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
289619fd4bb2SEric W. Biederman 	     iter.task;
289719fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
289819fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
289919fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
290019fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
29010804ef4bSEric W. Biederman 			goto out;
29021da177e4SLinus Torvalds 		}
29031da177e4SLinus Torvalds 	}
29040804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
29050804ef4bSEric W. Biederman out:
290661a28784SEric W. Biederman 	put_task_struct(reaper);
290761a28784SEric W. Biederman out_no_task:
29081da177e4SLinus Torvalds 	return 0;
29091da177e4SLinus Torvalds }
29101da177e4SLinus Torvalds 
29110bc58a91SEric W. Biederman /*
291228a6d671SEric W. Biederman  * Tasks
291328a6d671SEric W. Biederman  */
2914c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
2915631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2916631f9c18SAlexey Dobriyan 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2917631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
2918631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
2919631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
2920631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
2921631f9c18SAlexey Dobriyan 	INF("limits",	 S_IRUSR, proc_pid_limits),
292243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2923631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
292443ae34cbSIngo Molnar #endif
29254614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2926ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2927631f9c18SAlexey Dobriyan 	INF("syscall",   S_IRUSR, proc_pid_syscall),
2928ebcb6734SRoland McGrath #endif
2929631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
2930631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
2931631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
2932631f9c18SAlexey Dobriyan 	REG("maps",      S_IRUGO, proc_maps_operations),
293328a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2934631f9c18SAlexey Dobriyan 	REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
293528a6d671SEric W. Biederman #endif
2936631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
2937631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
2938631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
2939631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
2940631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
2941631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
29421e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2943631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2944631f9c18SAlexey Dobriyan 	REG("smaps",     S_IRUGO, proc_smaps_operations),
2945631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
294628a6d671SEric W. Biederman #endif
294728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2948631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
294928a6d671SEric W. Biederman #endif
295028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2951631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
295228a6d671SEric W. Biederman #endif
29532ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
29542ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
295528a6d671SEric W. Biederman #endif
295628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2957631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
295828a6d671SEric W. Biederman #endif
29599745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2960631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
29619745512cSArjan van de Ven #endif
29628793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2963631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
296428a6d671SEric W. Biederman #endif
2965a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2966631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2967a424316cSPaul Menage #endif
2968631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
2969631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
297028a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2971631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
2972631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUSR, proc_sessionid_operations),
297328a6d671SEric W. Biederman #endif
2974f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2975631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2976f4f154fdSAkinobu Mita #endif
2977297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
2978631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tid_io_accounting),
2979297c5d92SAndrea Righi #endif
298028a6d671SEric W. Biederman };
298128a6d671SEric W. Biederman 
298228a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
298328a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
298428a6d671SEric W. Biederman {
298528a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
298628a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
298728a6d671SEric W. Biederman }
298828a6d671SEric W. Biederman 
298928a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
29907bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
29917bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
299228a6d671SEric W. Biederman }
299328a6d671SEric W. Biederman 
299400977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
299528a6d671SEric W. Biederman 	.read		= generic_read_dir,
299628a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
299728a6d671SEric W. Biederman };
299828a6d671SEric W. Biederman 
2999c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
300028a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
300128a6d671SEric W. Biederman 	.getattr	= pid_getattr,
300228a6d671SEric W. Biederman 	.setattr	= proc_setattr,
300328a6d671SEric W. Biederman };
300428a6d671SEric W. Biederman 
3005444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3006c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3007444ceed8SEric W. Biederman {
3008444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3009444ceed8SEric W. Biederman 	struct inode *inode;
301061a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3011444ceed8SEric W. Biederman 
3012444ceed8SEric W. Biederman 	if (!inode)
3013444ceed8SEric W. Biederman 		goto out;
3014444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3015444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3016444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3017444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3018aed54175SVegard Nossum 
3019aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3020aed54175SVegard Nossum 		ARRAY_SIZE(tid_base_stuff));
3021444ceed8SEric W. Biederman 
3022444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
3023444ceed8SEric W. Biederman 
3024444ceed8SEric W. Biederman 	d_add(dentry, inode);
3025444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3026444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3027444ceed8SEric W. Biederman 		error = NULL;
3028444ceed8SEric W. Biederman out:
3029444ceed8SEric W. Biederman 	return error;
3030444ceed8SEric W. Biederman }
3031444ceed8SEric W. Biederman 
303228a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
303328a6d671SEric W. Biederman {
303428a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
303528a6d671SEric W. Biederman 	struct task_struct *task;
303628a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
303728a6d671SEric W. Biederman 	unsigned tid;
3038b488893aSPavel Emelyanov 	struct pid_namespace *ns;
303928a6d671SEric W. Biederman 
304028a6d671SEric W. Biederman 	if (!leader)
304128a6d671SEric W. Biederman 		goto out_no_task;
304228a6d671SEric W. Biederman 
304328a6d671SEric W. Biederman 	tid = name_to_int(dentry);
304428a6d671SEric W. Biederman 	if (tid == ~0U)
304528a6d671SEric W. Biederman 		goto out;
304628a6d671SEric W. Biederman 
3047b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
304828a6d671SEric W. Biederman 	rcu_read_lock();
3049b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
305028a6d671SEric W. Biederman 	if (task)
305128a6d671SEric W. Biederman 		get_task_struct(task);
305228a6d671SEric W. Biederman 	rcu_read_unlock();
305328a6d671SEric W. Biederman 	if (!task)
305428a6d671SEric W. Biederman 		goto out;
3055bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
305628a6d671SEric W. Biederman 		goto out_drop_task;
305728a6d671SEric W. Biederman 
3058444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
305928a6d671SEric W. Biederman out_drop_task:
306028a6d671SEric W. Biederman 	put_task_struct(task);
306128a6d671SEric W. Biederman out:
306228a6d671SEric W. Biederman 	put_task_struct(leader);
306328a6d671SEric W. Biederman out_no_task:
306428a6d671SEric W. Biederman 	return result;
306528a6d671SEric W. Biederman }
306628a6d671SEric W. Biederman 
306728a6d671SEric W. Biederman /*
30680bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
30690bc58a91SEric W. Biederman  *
30700bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
30710bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
30720bc58a91SEric W. Biederman  * directory we have more work todo.
30730bc58a91SEric W. Biederman  *
30740bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
30750bc58a91SEric W. Biederman  *
30760bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
30770bc58a91SEric W. Biederman  * threads past it.
30780bc58a91SEric W. Biederman  */
3079cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3080b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
30810bc58a91SEric W. Biederman {
3082a872ff0cSOleg Nesterov 	struct task_struct *pos;
30830bc58a91SEric W. Biederman 
3084cc288738SEric W. Biederman 	rcu_read_lock();
30850bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
30860bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3087b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3088a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3089a872ff0cSOleg Nesterov 			goto found;
30900bc58a91SEric W. Biederman 	}
30910bc58a91SEric W. Biederman 
30920bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
30930bc58a91SEric W. Biederman 	pos = NULL;
3094a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3095a872ff0cSOleg Nesterov 		goto out;
3096a872ff0cSOleg Nesterov 
3097a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3098a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3099a872ff0cSOleg Nesterov 	 */
3100a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3101a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3102a872ff0cSOleg Nesterov 		if (pos == leader) {
3103a872ff0cSOleg Nesterov 			pos = NULL;
3104a872ff0cSOleg Nesterov 			goto out;
3105a872ff0cSOleg Nesterov 		}
3106a872ff0cSOleg Nesterov 	}
3107a872ff0cSOleg Nesterov found:
3108a872ff0cSOleg Nesterov 	get_task_struct(pos);
3109a872ff0cSOleg Nesterov out:
3110cc288738SEric W. Biederman 	rcu_read_unlock();
31110bc58a91SEric W. Biederman 	return pos;
31120bc58a91SEric W. Biederman }
31130bc58a91SEric W. Biederman 
31140bc58a91SEric W. Biederman /*
31150bc58a91SEric W. Biederman  * Find the next thread in the thread list.
31160bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
31170bc58a91SEric W. Biederman  *
31180bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
31190bc58a91SEric W. Biederman  */
31200bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
31210bc58a91SEric W. Biederman {
3122c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3123cc288738SEric W. Biederman 	rcu_read_lock();
3124c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
31250bc58a91SEric W. Biederman 		pos = next_thread(start);
3126c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
31270bc58a91SEric W. Biederman 			pos = NULL;
3128c1df7fb8SOleg Nesterov 		else
3129c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3130c1df7fb8SOleg Nesterov 	}
3131cc288738SEric W. Biederman 	rcu_read_unlock();
31320bc58a91SEric W. Biederman 	put_task_struct(start);
31330bc58a91SEric W. Biederman 	return pos;
31340bc58a91SEric W. Biederman }
31350bc58a91SEric W. Biederman 
313661a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
313761a28784SEric W. Biederman 	struct task_struct *task, int tid)
313861a28784SEric W. Biederman {
313961a28784SEric W. Biederman 	char name[PROC_NUMBUF];
314061a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
314161a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
314261a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
314361a28784SEric W. Biederman }
314461a28784SEric W. Biederman 
31451da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
31461da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
31471da177e4SLinus Torvalds {
31482fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
31491da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
31507d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
31510bc58a91SEric W. Biederman 	struct task_struct *task;
31521da177e4SLinus Torvalds 	int retval = -ENOENT;
31531da177e4SLinus Torvalds 	ino_t ino;
31540bc58a91SEric W. Biederman 	int tid;
3155b488893aSPavel Emelyanov 	struct pid_namespace *ns;
31561da177e4SLinus Torvalds 
31577d895244SGuillaume Chazarain 	task = get_proc_task(inode);
31587d895244SGuillaume Chazarain 	if (!task)
31597d895244SGuillaume Chazarain 		goto out_no_task;
31607d895244SGuillaume Chazarain 	rcu_read_lock();
31617d895244SGuillaume Chazarain 	if (pid_alive(task)) {
31627d895244SGuillaume Chazarain 		leader = task->group_leader;
31637d895244SGuillaume Chazarain 		get_task_struct(leader);
31647d895244SGuillaume Chazarain 	}
31657d895244SGuillaume Chazarain 	rcu_read_unlock();
31667d895244SGuillaume Chazarain 	put_task_struct(task);
316799f89551SEric W. Biederman 	if (!leader)
316899f89551SEric W. Biederman 		goto out_no_task;
31691da177e4SLinus Torvalds 	retval = 0;
31701da177e4SLinus Torvalds 
3171ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
31721da177e4SLinus Torvalds 	case 0:
31731da177e4SLinus Torvalds 		ino = inode->i_ino;
3174ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
31751da177e4SLinus Torvalds 			goto out;
3176ee6f779bSZhang Le 		filp->f_pos++;
31771da177e4SLinus Torvalds 		/* fall through */
31781da177e4SLinus Torvalds 	case 1:
31791da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3180ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
31811da177e4SLinus Torvalds 			goto out;
3182ee6f779bSZhang Le 		filp->f_pos++;
31831da177e4SLinus Torvalds 		/* fall through */
31841da177e4SLinus Torvalds 	}
31851da177e4SLinus Torvalds 
31860bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
31870bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
31880bc58a91SEric W. Biederman 	 */
3189b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
31902b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
31910bc58a91SEric W. Biederman 	filp->f_version = 0;
3192ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
31930bc58a91SEric W. Biederman 	     task;
3194ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3195b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
319661a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
31970bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
31980bc58a91SEric W. Biederman 			 * pid for the next readir call */
31992b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
32000bc58a91SEric W. Biederman 			put_task_struct(task);
32011da177e4SLinus Torvalds 			break;
32020bc58a91SEric W. Biederman 		}
32031da177e4SLinus Torvalds 	}
32041da177e4SLinus Torvalds out:
320599f89551SEric W. Biederman 	put_task_struct(leader);
320699f89551SEric W. Biederman out_no_task:
32071da177e4SLinus Torvalds 	return retval;
32081da177e4SLinus Torvalds }
32096e66b52bSEric W. Biederman 
32106e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
32116e66b52bSEric W. Biederman {
32126e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
321399f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
32146e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
32156e66b52bSEric W. Biederman 
321699f89551SEric W. Biederman 	if (p) {
321799f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
321899f89551SEric W. Biederman 		put_task_struct(p);
32196e66b52bSEric W. Biederman 	}
32206e66b52bSEric W. Biederman 
32216e66b52bSEric W. Biederman 	return 0;
32226e66b52bSEric W. Biederman }
322328a6d671SEric W. Biederman 
3224c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
322528a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
322628a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
322728a6d671SEric W. Biederman 	.setattr	= proc_setattr,
322828a6d671SEric W. Biederman };
322928a6d671SEric W. Biederman 
323000977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
323128a6d671SEric W. Biederman 	.read		= generic_read_dir,
323228a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
323328a6d671SEric W. Biederman };
3234