xref: /openbmc/linux/fs/proc/base.c (revision 73d3646029138743989b6c213628484301a72eb1)
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>
845a0e3ad6STejun Heo #include <linux/slab.h>
851da177e4SLinus Torvalds #include "internal.h"
861da177e4SLinus Torvalds 
870f2fe20fSEric W. Biederman /* NOTE:
880f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
890f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
900f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
910f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
920f2fe20fSEric W. Biederman  *
930f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
940f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
950f2fe20fSEric W. Biederman  */
960f2fe20fSEric W. Biederman 
971da177e4SLinus Torvalds struct pid_entry {
981da177e4SLinus Torvalds 	char *name;
99c5141e6dSEric Dumazet 	int len;
1001da177e4SLinus Torvalds 	mode_t mode;
101c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
10200977a59SArjan van de Ven 	const struct file_operations *fop;
10320cdc894SEric W. Biederman 	union proc_op op;
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
10661a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
10720cdc894SEric W. Biederman 	.name = (NAME),					\
108c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
10920cdc894SEric W. Biederman 	.mode = MODE,					\
11020cdc894SEric W. Biederman 	.iop  = IOP,					\
11120cdc894SEric W. Biederman 	.fop  = FOP,					\
11220cdc894SEric W. Biederman 	.op   = OP,					\
11320cdc894SEric W. Biederman }
11420cdc894SEric W. Biederman 
115631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
116631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
117631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
11861a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
11920cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
120631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
121631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
122631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
123631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
12461a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
12520cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
126631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
127631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
128be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
129be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
130631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1311da177e4SLinus Torvalds 
132aed54175SVegard Nossum /*
133aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
134aed54175SVegard Nossum  * and .. links.
135aed54175SVegard Nossum  */
136aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
137aed54175SVegard Nossum 	unsigned int n)
138aed54175SVegard Nossum {
139aed54175SVegard Nossum 	unsigned int i;
140aed54175SVegard Nossum 	unsigned int count;
141aed54175SVegard Nossum 
142aed54175SVegard Nossum 	count = 0;
143aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
144aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
145aed54175SVegard Nossum 			++count;
146aed54175SVegard Nossum 	}
147aed54175SVegard Nossum 
148aed54175SVegard Nossum 	return count;
149aed54175SVegard Nossum }
150aed54175SVegard Nossum 
1517c2c7d99SHugh Dickins static int get_fs_path(struct task_struct *task, struct path *path, bool root)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	struct fs_struct *fs;
1547c2c7d99SHugh Dickins 	int result = -ENOENT;
1557c2c7d99SHugh Dickins 
1560494f6ecSMiklos Szeredi 	task_lock(task);
1570494f6ecSMiklos Szeredi 	fs = task->fs;
1587c2c7d99SHugh Dickins 	if (fs) {
1597c2c7d99SHugh Dickins 		read_lock(&fs->lock);
1607c2c7d99SHugh Dickins 		*path = root ? fs->root : fs->pwd;
1617c2c7d99SHugh Dickins 		path_get(path);
1627c2c7d99SHugh Dickins 		read_unlock(&fs->lock);
1637c2c7d99SHugh Dickins 		result = 0;
1647c2c7d99SHugh Dickins 	}
1650494f6ecSMiklos Szeredi 	task_unlock(task);
1667c2c7d99SHugh Dickins 	return result;
1670494f6ecSMiklos Szeredi }
1680494f6ecSMiklos Szeredi 
1693dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path)
1700494f6ecSMiklos Szeredi {
17199f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1720494f6ecSMiklos Szeredi 	int result = -ENOENT;
17399f89551SEric W. Biederman 
17499f89551SEric W. Biederman 	if (task) {
1757c2c7d99SHugh Dickins 		result = get_fs_path(task, path, 0);
17699f89551SEric W. Biederman 		put_task_struct(task);
17799f89551SEric W. Biederman 	}
1781da177e4SLinus Torvalds 	return result;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1813dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path)
1821da177e4SLinus Torvalds {
18399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1841da177e4SLinus Torvalds 	int result = -ENOENT;
18599f89551SEric W. Biederman 
18699f89551SEric W. Biederman 	if (task) {
1877c2c7d99SHugh Dickins 		result = get_fs_path(task, path, 1);
18899f89551SEric W. Biederman 		put_task_struct(task);
18999f89551SEric W. Biederman 	}
1901da177e4SLinus Torvalds 	return result;
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
193638fa202SRoland McGrath /*
194638fa202SRoland McGrath  * Return zero if current may access user memory in @task, -error if not.
195638fa202SRoland McGrath  */
196638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task)
197638fa202SRoland McGrath {
198638fa202SRoland McGrath 	/*
199638fa202SRoland McGrath 	 * A task can always look at itself, in case it chooses
200638fa202SRoland McGrath 	 * to use system calls instead of load instructions.
201638fa202SRoland McGrath 	 */
202638fa202SRoland McGrath 	if (task == current)
203638fa202SRoland McGrath 		return 0;
204638fa202SRoland McGrath 
205638fa202SRoland McGrath 	/*
206638fa202SRoland McGrath 	 * If current is actively ptrace'ing, and would also be
207638fa202SRoland McGrath 	 * permitted to freshly attach with ptrace now, permit it.
208638fa202SRoland McGrath 	 */
2090d094efeSRoland McGrath 	if (task_is_stopped_or_traced(task)) {
2100d094efeSRoland McGrath 		int match;
2110d094efeSRoland McGrath 		rcu_read_lock();
2120d094efeSRoland McGrath 		match = (tracehook_tracer_task(task) == current);
2130d094efeSRoland McGrath 		rcu_read_unlock();
2140d094efeSRoland McGrath 		if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
215638fa202SRoland McGrath 			return 0;
2160d094efeSRoland McGrath 	}
217638fa202SRoland McGrath 
218638fa202SRoland McGrath 	/*
219638fa202SRoland McGrath 	 * Noone else is allowed.
220638fa202SRoland McGrath 	 */
221638fa202SRoland McGrath 	return -EPERM;
222638fa202SRoland McGrath }
2231da177e4SLinus Torvalds 
224831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task)
225831830b5SAl Viro {
226704b836cSOleg Nesterov 	struct mm_struct *mm;
22700f89d21SOleg Nesterov 
228704b836cSOleg Nesterov 	if (mutex_lock_killable(&task->cred_guard_mutex))
229704b836cSOleg Nesterov 		return NULL;
230704b836cSOleg Nesterov 
231704b836cSOleg Nesterov 	mm = get_task_mm(task);
232704b836cSOleg Nesterov 	if (mm && mm != current->mm &&
233704b836cSOleg Nesterov 			!ptrace_may_access(task, PTRACE_MODE_READ)) {
234831830b5SAl Viro 		mmput(mm);
23500f89d21SOleg Nesterov 		mm = NULL;
236831830b5SAl Viro 	}
237704b836cSOleg Nesterov 	mutex_unlock(&task->cred_guard_mutex);
238704b836cSOleg Nesterov 
23913f0feafSOleg Nesterov 	return mm;
24013f0feafSOleg Nesterov }
241831830b5SAl Viro 
2421da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	int res = 0;
2451da177e4SLinus Torvalds 	unsigned int len;
2461da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2471da177e4SLinus Torvalds 	if (!mm)
2481da177e4SLinus Torvalds 		goto out;
2491da177e4SLinus Torvalds 	if (!mm->arg_end)
2501da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2551da177e4SLinus Torvalds 		len = PAGE_SIZE;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2601da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2611da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2621da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2631da177e4SLinus Torvalds 		if (len < res) {
2641da177e4SLinus Torvalds 		    res = len;
2651da177e4SLinus Torvalds 		} else {
2661da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2671da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2681da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2691da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2701da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2711da177e4SLinus Torvalds 		}
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds out_mm:
2741da177e4SLinus Torvalds 	mmput(mm);
2751da177e4SLinus Torvalds out:
2761da177e4SLinus Torvalds 	return res;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	int res = 0;
2821da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2831da177e4SLinus Torvalds 	if (mm) {
2841da177e4SLinus Torvalds 		unsigned int nwords = 0;
285dfe6b7d9SHannes Eder 		do {
2861da177e4SLinus Torvalds 			nwords += 2;
287dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2881da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2891da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2901da177e4SLinus Torvalds 			res = PAGE_SIZE;
2911da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2921da177e4SLinus Torvalds 		mmput(mm);
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 	return res;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
3011da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
3021da177e4SLinus Torvalds  */
3031da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
3041da177e4SLinus Torvalds {
305ffb45122SAlexey Dobriyan 	unsigned long wchan;
3069281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	wchan = get_wchan(task);
3091da177e4SLinus Torvalds 
3109d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
311f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
312f83ce3e6SJake Edge 			return 0;
313f83ce3e6SJake Edge 		else
3141da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
3159d65cb4aSAlexey Dobriyan 	else
3169d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
3191da177e4SLinus Torvalds 
3202ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3212ec220e2SKen Chen 
3222ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3232ec220e2SKen Chen 
3242ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3252ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3262ec220e2SKen Chen {
3272ec220e2SKen Chen 	struct stack_trace trace;
3282ec220e2SKen Chen 	unsigned long *entries;
3292ec220e2SKen Chen 	int i;
3302ec220e2SKen Chen 
3312ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3322ec220e2SKen Chen 	if (!entries)
3332ec220e2SKen Chen 		return -ENOMEM;
3342ec220e2SKen Chen 
3352ec220e2SKen Chen 	trace.nr_entries	= 0;
3362ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3372ec220e2SKen Chen 	trace.entries		= entries;
3382ec220e2SKen Chen 	trace.skip		= 0;
3392ec220e2SKen Chen 	save_stack_trace_tsk(task, &trace);
3402ec220e2SKen Chen 
3412ec220e2SKen Chen 	for (i = 0; i < trace.nr_entries; i++) {
3422ec220e2SKen Chen 		seq_printf(m, "[<%p>] %pS\n",
3432ec220e2SKen Chen 			   (void *)entries[i], (void *)entries[i]);
3442ec220e2SKen Chen 	}
3452ec220e2SKen Chen 	kfree(entries);
3462ec220e2SKen Chen 
3472ec220e2SKen Chen 	return 0;
3482ec220e2SKen Chen }
3492ec220e2SKen Chen #endif
3502ec220e2SKen Chen 
3511da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3521da177e4SLinus Torvalds /*
3531da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3541da177e4SLinus Torvalds  */
3551da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3561da177e4SLinus Torvalds {
357172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
358826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
359826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3602d72376bSIngo Molnar 			task->sched_info.pcount);
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds #endif
3631da177e4SLinus Torvalds 
3649745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3659745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3669745512cSArjan van de Ven {
3679745512cSArjan van de Ven 	int i;
36813d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
36913d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3709745512cSArjan van de Ven 
37113d77c37SHiroshi Shimamoto 	if (!task)
37213d77c37SHiroshi Shimamoto 		return -ESRCH;
37313d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3749745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
3759745512cSArjan van de Ven 		if (task->latency_record[i].backtrace[0]) {
3769745512cSArjan van de Ven 			int q;
3779745512cSArjan van de Ven 			seq_printf(m, "%i %li %li ",
3789745512cSArjan van de Ven 				task->latency_record[i].count,
3799745512cSArjan van de Ven 				task->latency_record[i].time,
3809745512cSArjan van de Ven 				task->latency_record[i].max);
3819745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
3829c246247SHugh Dickins 				char sym[KSYM_SYMBOL_LEN];
3839745512cSArjan van de Ven 				char *c;
3849745512cSArjan van de Ven 				if (!task->latency_record[i].backtrace[q])
3859745512cSArjan van de Ven 					break;
3869745512cSArjan van de Ven 				if (task->latency_record[i].backtrace[q] == ULONG_MAX)
3879745512cSArjan van de Ven 					break;
3889745512cSArjan van de Ven 				sprint_symbol(sym, task->latency_record[i].backtrace[q]);
3899745512cSArjan van de Ven 				c = strchr(sym, '+');
3909745512cSArjan van de Ven 				if (c)
3919745512cSArjan van de Ven 					*c = 0;
3929745512cSArjan van de Ven 				seq_printf(m, "%s ", sym);
3939745512cSArjan van de Ven 			}
3949745512cSArjan van de Ven 			seq_printf(m, "\n");
3959745512cSArjan van de Ven 		}
3969745512cSArjan van de Ven 
3979745512cSArjan van de Ven 	}
39813d77c37SHiroshi Shimamoto 	put_task_struct(task);
3999745512cSArjan van de Ven 	return 0;
4009745512cSArjan van de Ven }
4019745512cSArjan van de Ven 
4029745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
4039745512cSArjan van de Ven {
40413d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
405d6643d12SHiroshi Shimamoto }
406d6643d12SHiroshi Shimamoto 
4079745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
4089745512cSArjan van de Ven 			    size_t count, loff_t *offs)
4099745512cSArjan van de Ven {
41013d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
4119745512cSArjan van de Ven 
41213d77c37SHiroshi Shimamoto 	if (!task)
41313d77c37SHiroshi Shimamoto 		return -ESRCH;
4149745512cSArjan van de Ven 	clear_all_latency_tracing(task);
41513d77c37SHiroshi Shimamoto 	put_task_struct(task);
4169745512cSArjan van de Ven 
4179745512cSArjan van de Ven 	return count;
4189745512cSArjan van de Ven }
4199745512cSArjan van de Ven 
4209745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
4219745512cSArjan van de Ven 	.open		= lstats_open,
4229745512cSArjan van de Ven 	.read		= seq_read,
4239745512cSArjan van de Ven 	.write		= lstats_write,
4249745512cSArjan van de Ven 	.llseek		= seq_lseek,
42513d77c37SHiroshi Shimamoto 	.release	= single_release,
4269745512cSArjan van de Ven };
4279745512cSArjan van de Ven 
4289745512cSArjan van de Ven #endif
4299745512cSArjan van de Ven 
4301da177e4SLinus Torvalds /* The badness from the OOM killer */
4311da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime);
4321da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4331da177e4SLinus Torvalds {
434b95c35e7SOleg Nesterov 	unsigned long points = 0;
4351da177e4SLinus Torvalds 	struct timespec uptime;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&uptime);
43819c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
439b95c35e7SOleg Nesterov 	if (pid_alive(task))
440b95c35e7SOleg Nesterov 		points = badness(task, uptime.tv_sec);
44119c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4421da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
445d85f50d5SNeil Horman struct limit_names {
446d85f50d5SNeil Horman 	char *name;
447d85f50d5SNeil Horman 	char *unit;
448d85f50d5SNeil Horman };
449d85f50d5SNeil Horman 
450d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
451cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
452d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
453d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
454d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
455d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
456d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
457d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
458d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
459d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
460d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
461d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
462d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
463d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
464d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
465d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4668808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
467d85f50d5SNeil Horman };
468d85f50d5SNeil Horman 
469d85f50d5SNeil Horman /* Display limits for a process */
470d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
471d85f50d5SNeil Horman {
472d85f50d5SNeil Horman 	unsigned int i;
473d85f50d5SNeil Horman 	int count = 0;
474d85f50d5SNeil Horman 	unsigned long flags;
475d85f50d5SNeil Horman 	char *bufptr = buffer;
476d85f50d5SNeil Horman 
477d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
478d85f50d5SNeil Horman 
479a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
480d85f50d5SNeil Horman 		return 0;
481d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
482d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
483d85f50d5SNeil Horman 
484d85f50d5SNeil Horman 	/*
485d85f50d5SNeil Horman 	 * print the file header
486d85f50d5SNeil Horman 	 */
487d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
488d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
489d85f50d5SNeil Horman 
490d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
491d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
492d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
493d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
494d85f50d5SNeil Horman 		else
495d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
496d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
497d85f50d5SNeil Horman 
498d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
499d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
500d85f50d5SNeil Horman 		else
501d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
502d85f50d5SNeil Horman 					 rlim[i].rlim_max);
503d85f50d5SNeil Horman 
504d85f50d5SNeil Horman 		if (lnames[i].unit)
505d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
506d85f50d5SNeil Horman 					 lnames[i].unit);
507d85f50d5SNeil Horman 		else
508d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
509d85f50d5SNeil Horman 	}
510d85f50d5SNeil Horman 
511d85f50d5SNeil Horman 	return count;
512d85f50d5SNeil Horman }
513d85f50d5SNeil Horman 
514ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
515ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
516ebcb6734SRoland McGrath {
517ebcb6734SRoland McGrath 	long nr;
518ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
519ebcb6734SRoland McGrath 
520ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
521ebcb6734SRoland McGrath 		return sprintf(buffer, "running\n");
522ebcb6734SRoland McGrath 
523ebcb6734SRoland McGrath 	if (nr < 0)
524ebcb6734SRoland McGrath 		return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
525ebcb6734SRoland McGrath 
526ebcb6734SRoland McGrath 	return sprintf(buffer,
527ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
528ebcb6734SRoland McGrath 		       nr,
529ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
530ebcb6734SRoland McGrath 		       sp, pc);
531ebcb6734SRoland McGrath }
532ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
533ebcb6734SRoland McGrath 
5341da177e4SLinus Torvalds /************************************************************************/
5351da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5361da177e4SLinus Torvalds /************************************************************************/
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds /* permission checks */
539778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5401da177e4SLinus Torvalds {
541778c1144SEric W. Biederman 	struct task_struct *task;
542778c1144SEric W. Biederman 	int allowed = 0;
543df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
544df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
545df26c40eSEric W. Biederman 	 * information.
546778c1144SEric W. Biederman 	 */
547778c1144SEric W. Biederman 	task = get_proc_task(inode);
548df26c40eSEric W. Biederman 	if (task) {
549006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
550778c1144SEric W. Biederman 		put_task_struct(task);
551df26c40eSEric W. Biederman 	}
552778c1144SEric W. Biederman 	return allowed;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
5556d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr)
5566d76fa58SLinus Torvalds {
5576d76fa58SLinus Torvalds 	int error;
5586d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5596d76fa58SLinus Torvalds 
5606d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5616d76fa58SLinus Torvalds 		return -EPERM;
5626d76fa58SLinus Torvalds 
5636d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5646d76fa58SLinus Torvalds 	if (!error)
5656d76fa58SLinus Torvalds 		error = inode_setattr(inode, attr);
5666d76fa58SLinus Torvalds 	return error;
5676d76fa58SLinus Torvalds }
5686d76fa58SLinus Torvalds 
569c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5706d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5716d76fa58SLinus Torvalds };
5726d76fa58SLinus Torvalds 
573a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file,
574a1a2c409SMiklos Szeredi 			      const struct seq_operations *op)
5751da177e4SLinus Torvalds {
57699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
577cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
5786b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
579a1a2c409SMiklos Szeredi 	struct path root;
5805addc5ddSAl Viro 	struct proc_mounts *p;
5815addc5ddSAl Viro 	int ret = -EINVAL;
5825addc5ddSAl Viro 
58399f89551SEric W. Biederman 	if (task) {
584cf7b708cSPavel Emelyanov 		rcu_read_lock();
585cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
586cf7b708cSPavel Emelyanov 		if (nsp) {
587cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
5886b3286edSKirill Korotaev 			if (ns)
5896b3286edSKirill Korotaev 				get_mnt_ns(ns);
590863c4702SAlexey Dobriyan 		}
591cf7b708cSPavel Emelyanov 		rcu_read_unlock();
5927c2c7d99SHugh Dickins 		if (ns && get_fs_path(task, &root, 1) == 0)
5937c2c7d99SHugh Dickins 			ret = 0;
59499f89551SEric W. Biederman 		put_task_struct(task);
59599f89551SEric W. Biederman 	}
5961da177e4SLinus Torvalds 
597a1a2c409SMiklos Szeredi 	if (!ns)
598a1a2c409SMiklos Szeredi 		goto err;
5997c2c7d99SHugh Dickins 	if (ret)
600a1a2c409SMiklos Szeredi 		goto err_put_ns;
601a1a2c409SMiklos Szeredi 
6025addc5ddSAl Viro 	ret = -ENOMEM;
6035addc5ddSAl Viro 	p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
604a1a2c409SMiklos Szeredi 	if (!p)
605a1a2c409SMiklos Szeredi 		goto err_put_path;
606a1a2c409SMiklos Szeredi 
6075addc5ddSAl Viro 	file->private_data = &p->m;
608a1a2c409SMiklos Szeredi 	ret = seq_open(file, op);
609a1a2c409SMiklos Szeredi 	if (ret)
610a1a2c409SMiklos Szeredi 		goto err_free;
611a1a2c409SMiklos Szeredi 
612a1a2c409SMiklos Szeredi 	p->m.private = p;
613a1a2c409SMiklos Szeredi 	p->ns = ns;
614a1a2c409SMiklos Szeredi 	p->root = root;
6156b3286edSKirill Korotaev 	p->event = ns->event;
616a1a2c409SMiklos Szeredi 
6175addc5ddSAl Viro 	return 0;
618a1a2c409SMiklos Szeredi 
619a1a2c409SMiklos Szeredi  err_free:
6205addc5ddSAl Viro 	kfree(p);
621a1a2c409SMiklos Szeredi  err_put_path:
622a1a2c409SMiklos Szeredi 	path_put(&root);
623a1a2c409SMiklos Szeredi  err_put_ns:
6246b3286edSKirill Korotaev 	put_mnt_ns(ns);
625a1a2c409SMiklos Szeredi  err:
6261da177e4SLinus Torvalds 	return ret;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
6301da177e4SLinus Torvalds {
631a1a2c409SMiklos Szeredi 	struct proc_mounts *p = file->private_data;
632a1a2c409SMiklos Szeredi 	path_put(&p->root);
633a1a2c409SMiklos Szeredi 	put_mnt_ns(p->ns);
6341da177e4SLinus Torvalds 	return seq_release(inode, file);
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
6375addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
6385addc5ddSAl Viro {
6395addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
64031b07093SKOSAKI Motohiro 	unsigned res = POLLIN | POLLRDNORM;
6415addc5ddSAl Viro 
6429f5596afSAl Viro 	poll_wait(file, &p->ns->poll, wait);
6439f5596afSAl Viro 	if (mnt_had_events(p))
64431b07093SKOSAKI Motohiro 		res |= POLLERR | POLLPRI;
6455addc5ddSAl Viro 
6465addc5ddSAl Viro 	return res;
6475addc5ddSAl Viro }
6485addc5ddSAl Viro 
649a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file)
650a1a2c409SMiklos Szeredi {
651a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mounts_op);
652a1a2c409SMiklos Szeredi }
653a1a2c409SMiklos Szeredi 
65400977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
6551da177e4SLinus Torvalds 	.open		= mounts_open,
6561da177e4SLinus Torvalds 	.read		= seq_read,
6571da177e4SLinus Torvalds 	.llseek		= seq_lseek,
6581da177e4SLinus Torvalds 	.release	= mounts_release,
6595addc5ddSAl Viro 	.poll		= mounts_poll,
6601da177e4SLinus Torvalds };
6611da177e4SLinus Torvalds 
6622d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file)
6632d4d4864SRam Pai {
6642d4d4864SRam Pai 	return mounts_open_common(inode, file, &mountinfo_op);
6652d4d4864SRam Pai }
6662d4d4864SRam Pai 
6672d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = {
6682d4d4864SRam Pai 	.open		= mountinfo_open,
6692d4d4864SRam Pai 	.read		= seq_read,
6702d4d4864SRam Pai 	.llseek		= seq_lseek,
6712d4d4864SRam Pai 	.release	= mounts_release,
6722d4d4864SRam Pai 	.poll		= mounts_poll,
6732d4d4864SRam Pai };
6742d4d4864SRam Pai 
675b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
676b4629fe2SChuck Lever {
677a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mountstats_op);
678b4629fe2SChuck Lever }
679b4629fe2SChuck Lever 
68000977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
681b4629fe2SChuck Lever 	.open		= mountstats_open,
682b4629fe2SChuck Lever 	.read		= seq_read,
683b4629fe2SChuck Lever 	.llseek		= seq_lseek,
684b4629fe2SChuck Lever 	.release	= mounts_release,
685b4629fe2SChuck Lever };
686b4629fe2SChuck Lever 
6871da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6901da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6911da177e4SLinus Torvalds {
6922fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6931da177e4SLinus Torvalds 	unsigned long page;
6941da177e4SLinus Torvalds 	ssize_t length;
69599f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
69699f89551SEric W. Biederman 
69799f89551SEric W. Biederman 	length = -ESRCH;
69899f89551SEric W. Biederman 	if (!task)
69999f89551SEric W. Biederman 		goto out_no_task;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
7021da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
70399f89551SEric W. Biederman 
70499f89551SEric W. Biederman 	length = -ENOMEM;
705e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
70699f89551SEric W. Biederman 		goto out;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	if (length >= 0)
7111da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
7121da177e4SLinus Torvalds 	free_page(page);
71399f89551SEric W. Biederman out:
71499f89551SEric W. Biederman 	put_task_struct(task);
71599f89551SEric W. Biederman out_no_task:
7161da177e4SLinus Torvalds 	return length;
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
71900977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
7201da177e4SLinus Torvalds 	.read		= proc_info_read,
72187df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
7221da177e4SLinus Torvalds };
7231da177e4SLinus Torvalds 
724be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
725be614086SEric W. Biederman {
726be614086SEric W. Biederman 	struct inode *inode = m->private;
727be614086SEric W. Biederman 	struct pid_namespace *ns;
728be614086SEric W. Biederman 	struct pid *pid;
729be614086SEric W. Biederman 	struct task_struct *task;
730be614086SEric W. Biederman 	int ret;
731be614086SEric W. Biederman 
732be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
733be614086SEric W. Biederman 	pid = proc_pid(inode);
734be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
735be614086SEric W. Biederman 	if (!task)
736be614086SEric W. Biederman 		return -ESRCH;
737be614086SEric W. Biederman 
738be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
739be614086SEric W. Biederman 
740be614086SEric W. Biederman 	put_task_struct(task);
741be614086SEric W. Biederman 	return ret;
742be614086SEric W. Biederman }
743be614086SEric W. Biederman 
744be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
745be614086SEric W. Biederman {
746be614086SEric W. Biederman 	int ret;
747be614086SEric W. Biederman 	ret = single_open(filp, proc_single_show, NULL);
748be614086SEric W. Biederman 	if (!ret) {
749be614086SEric W. Biederman 		struct seq_file *m = filp->private_data;
750be614086SEric W. Biederman 
751be614086SEric W. Biederman 		m->private = inode;
752be614086SEric W. Biederman 	}
753be614086SEric W. Biederman 	return ret;
754be614086SEric W. Biederman }
755be614086SEric W. Biederman 
756be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
757be614086SEric W. Biederman 	.open		= proc_single_open,
758be614086SEric W. Biederman 	.read		= seq_read,
759be614086SEric W. Biederman 	.llseek		= seq_lseek,
760be614086SEric W. Biederman 	.release	= single_release,
761be614086SEric W. Biederman };
762be614086SEric W. Biederman 
7631da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
7641da177e4SLinus Torvalds {
7651da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
7661da177e4SLinus Torvalds 	return 0;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
7701da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
7711da177e4SLinus Torvalds {
7722fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7731da177e4SLinus Torvalds 	char *page;
7741da177e4SLinus Torvalds 	unsigned long src = *ppos;
7751da177e4SLinus Torvalds 	int ret = -ESRCH;
7761da177e4SLinus Torvalds 	struct mm_struct *mm;
7771da177e4SLinus Torvalds 
77899f89551SEric W. Biederman 	if (!task)
77999f89551SEric W. Biederman 		goto out_no_task;
78099f89551SEric W. Biederman 
781638fa202SRoland McGrath 	if (check_mem_permission(task))
7821da177e4SLinus Torvalds 		goto out;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	ret = -ENOMEM;
785e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7861da177e4SLinus Torvalds 	if (!page)
7871da177e4SLinus Torvalds 		goto out;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	ret = 0;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	mm = get_task_mm(task);
7921da177e4SLinus Torvalds 	if (!mm)
7931da177e4SLinus Torvalds 		goto out_free;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	ret = -EIO;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
7981da177e4SLinus Torvalds 		goto out_put;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	ret = 0;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	while (count > 0) {
8031da177e4SLinus Torvalds 		int this_len, retval;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8061da177e4SLinus Torvalds 		retval = access_process_vm(task, src, page, this_len, 0);
807638fa202SRoland McGrath 		if (!retval || check_mem_permission(task)) {
8081da177e4SLinus Torvalds 			if (!ret)
8091da177e4SLinus Torvalds 				ret = -EIO;
8101da177e4SLinus Torvalds 			break;
8111da177e4SLinus Torvalds 		}
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
8141da177e4SLinus Torvalds 			ret = -EFAULT;
8151da177e4SLinus Torvalds 			break;
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 		ret += retval;
8191da177e4SLinus Torvalds 		src += retval;
8201da177e4SLinus Torvalds 		buf += retval;
8211da177e4SLinus Torvalds 		count -= retval;
8221da177e4SLinus Torvalds 	}
8231da177e4SLinus Torvalds 	*ppos = src;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds out_put:
8261da177e4SLinus Torvalds 	mmput(mm);
8271da177e4SLinus Torvalds out_free:
8281da177e4SLinus Torvalds 	free_page((unsigned long) page);
8291da177e4SLinus Torvalds out:
83099f89551SEric W. Biederman 	put_task_struct(task);
83199f89551SEric W. Biederman out_no_task:
8321da177e4SLinus Torvalds 	return ret;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds #define mem_write NULL
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds #ifndef mem_write
8381da177e4SLinus Torvalds /* This is a security hazard */
83963967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
8401da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
8411da177e4SLinus Torvalds {
842f7ca54f4SFrederik Deweerdt 	int copied;
8431da177e4SLinus Torvalds 	char *page;
8442fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8451da177e4SLinus Torvalds 	unsigned long dst = *ppos;
8461da177e4SLinus Torvalds 
84799f89551SEric W. Biederman 	copied = -ESRCH;
84899f89551SEric W. Biederman 	if (!task)
84999f89551SEric W. Biederman 		goto out_no_task;
8501da177e4SLinus Torvalds 
851638fa202SRoland McGrath 	if (check_mem_permission(task))
85299f89551SEric W. Biederman 		goto out;
85399f89551SEric W. Biederman 
85499f89551SEric W. Biederman 	copied = -ENOMEM;
855e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
8561da177e4SLinus Torvalds 	if (!page)
85799f89551SEric W. Biederman 		goto out;
8581da177e4SLinus Torvalds 
859f7ca54f4SFrederik Deweerdt 	copied = 0;
8601da177e4SLinus Torvalds 	while (count > 0) {
8611da177e4SLinus Torvalds 		int this_len, retval;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8641da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
8651da177e4SLinus Torvalds 			copied = -EFAULT;
8661da177e4SLinus Torvalds 			break;
8671da177e4SLinus Torvalds 		}
8681da177e4SLinus Torvalds 		retval = access_process_vm(task, dst, page, this_len, 1);
8691da177e4SLinus Torvalds 		if (!retval) {
8701da177e4SLinus Torvalds 			if (!copied)
8711da177e4SLinus Torvalds 				copied = -EIO;
8721da177e4SLinus Torvalds 			break;
8731da177e4SLinus Torvalds 		}
8741da177e4SLinus Torvalds 		copied += retval;
8751da177e4SLinus Torvalds 		buf += retval;
8761da177e4SLinus Torvalds 		dst += retval;
8771da177e4SLinus Torvalds 		count -= retval;
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds 	*ppos = dst;
8801da177e4SLinus Torvalds 	free_page((unsigned long) page);
88199f89551SEric W. Biederman out:
88299f89551SEric W. Biederman 	put_task_struct(task);
88399f89551SEric W. Biederman out_no_task:
8841da177e4SLinus Torvalds 	return copied;
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds #endif
8871da177e4SLinus Torvalds 
88885863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds 	switch (orig) {
8911da177e4SLinus Torvalds 	case 0:
8921da177e4SLinus Torvalds 		file->f_pos = offset;
8931da177e4SLinus Torvalds 		break;
8941da177e4SLinus Torvalds 	case 1:
8951da177e4SLinus Torvalds 		file->f_pos += offset;
8961da177e4SLinus Torvalds 		break;
8971da177e4SLinus Torvalds 	default:
8981da177e4SLinus Torvalds 		return -EINVAL;
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 	force_successful_syscall_return();
9011da177e4SLinus Torvalds 	return file->f_pos;
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
90400977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
9051da177e4SLinus Torvalds 	.llseek		= mem_lseek,
9061da177e4SLinus Torvalds 	.read		= mem_read,
9071da177e4SLinus Torvalds 	.write		= mem_write,
9081da177e4SLinus Torvalds 	.open		= mem_open,
9091da177e4SLinus Torvalds };
9101da177e4SLinus Torvalds 
911315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
912315e28c8SJames Pearson 			size_t count, loff_t *ppos)
913315e28c8SJames Pearson {
914315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
915315e28c8SJames Pearson 	char *page;
916315e28c8SJames Pearson 	unsigned long src = *ppos;
917315e28c8SJames Pearson 	int ret = -ESRCH;
918315e28c8SJames Pearson 	struct mm_struct *mm;
919315e28c8SJames Pearson 
920315e28c8SJames Pearson 	if (!task)
921315e28c8SJames Pearson 		goto out_no_task;
922315e28c8SJames Pearson 
923006ebb40SStephen Smalley 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
924315e28c8SJames Pearson 		goto out;
925315e28c8SJames Pearson 
926315e28c8SJames Pearson 	ret = -ENOMEM;
927315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
928315e28c8SJames Pearson 	if (!page)
929315e28c8SJames Pearson 		goto out;
930315e28c8SJames Pearson 
931315e28c8SJames Pearson 	ret = 0;
932315e28c8SJames Pearson 
933315e28c8SJames Pearson 	mm = get_task_mm(task);
934315e28c8SJames Pearson 	if (!mm)
935315e28c8SJames Pearson 		goto out_free;
936315e28c8SJames Pearson 
937315e28c8SJames Pearson 	while (count > 0) {
938315e28c8SJames Pearson 		int this_len, retval, max_len;
939315e28c8SJames Pearson 
940315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
941315e28c8SJames Pearson 
942315e28c8SJames Pearson 		if (this_len <= 0)
943315e28c8SJames Pearson 			break;
944315e28c8SJames Pearson 
945315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
946315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
947315e28c8SJames Pearson 
948315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
949315e28c8SJames Pearson 			page, this_len, 0);
950315e28c8SJames Pearson 
951315e28c8SJames Pearson 		if (retval <= 0) {
952315e28c8SJames Pearson 			ret = retval;
953315e28c8SJames Pearson 			break;
954315e28c8SJames Pearson 		}
955315e28c8SJames Pearson 
956315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
957315e28c8SJames Pearson 			ret = -EFAULT;
958315e28c8SJames Pearson 			break;
959315e28c8SJames Pearson 		}
960315e28c8SJames Pearson 
961315e28c8SJames Pearson 		ret += retval;
962315e28c8SJames Pearson 		src += retval;
963315e28c8SJames Pearson 		buf += retval;
964315e28c8SJames Pearson 		count -= retval;
965315e28c8SJames Pearson 	}
966315e28c8SJames Pearson 	*ppos = src;
967315e28c8SJames Pearson 
968315e28c8SJames Pearson 	mmput(mm);
969315e28c8SJames Pearson out_free:
970315e28c8SJames Pearson 	free_page((unsigned long) page);
971315e28c8SJames Pearson out:
972315e28c8SJames Pearson 	put_task_struct(task);
973315e28c8SJames Pearson out_no_task:
974315e28c8SJames Pearson 	return ret;
975315e28c8SJames Pearson }
976315e28c8SJames Pearson 
977315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
978315e28c8SJames Pearson 	.read		= environ_read,
97987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
980315e28c8SJames Pearson };
981315e28c8SJames Pearson 
9821da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
9831da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9841da177e4SLinus Torvalds {
9852fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
9868578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
9871da177e4SLinus Torvalds 	size_t len;
98828b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
98928b83c51SKOSAKI Motohiro 	unsigned long flags;
9901da177e4SLinus Torvalds 
99199f89551SEric W. Biederman 	if (!task)
99299f89551SEric W. Biederman 		return -ESRCH;
99328b83c51SKOSAKI Motohiro 
99428b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
99528b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
99628b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
99728b83c51SKOSAKI Motohiro 	}
99828b83c51SKOSAKI Motohiro 
99999f89551SEric W. Biederman 	put_task_struct(task);
100099f89551SEric W. Biederman 
10018578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
10020c28f287SAkinobu Mita 
10030c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
10071da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
10081da177e4SLinus Torvalds {
100999f89551SEric W. Biederman 	struct task_struct *task;
10105d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
10115d863b89SKOSAKI Motohiro 	long oom_adjust;
101228b83c51SKOSAKI Motohiro 	unsigned long flags;
10135d863b89SKOSAKI Motohiro 	int err;
10141da177e4SLinus Torvalds 
10158578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
10168578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
10178578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
10181da177e4SLinus Torvalds 	if (copy_from_user(buffer, buf, count))
10191da177e4SLinus Torvalds 		return -EFAULT;
10205d863b89SKOSAKI Motohiro 
10215d863b89SKOSAKI Motohiro 	err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
10225d863b89SKOSAKI Motohiro 	if (err)
10235d863b89SKOSAKI Motohiro 		return -EINVAL;
10248ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
10258ac773b4SAlexey Dobriyan 	     oom_adjust != OOM_DISABLE)
10261da177e4SLinus Torvalds 		return -EINVAL;
10275d863b89SKOSAKI Motohiro 
10282fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
102999f89551SEric W. Biederman 	if (!task)
103099f89551SEric W. Biederman 		return -ESRCH;
103128b83c51SKOSAKI Motohiro 	if (!lock_task_sighand(task, &flags)) {
103228b83c51SKOSAKI Motohiro 		put_task_struct(task);
103328b83c51SKOSAKI Motohiro 		return -ESRCH;
103428b83c51SKOSAKI Motohiro 	}
103528b83c51SKOSAKI Motohiro 
103628b83c51SKOSAKI Motohiro 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
103728b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
10388fb4fc68SGuillem Jover 		put_task_struct(task);
10398fb4fc68SGuillem Jover 		return -EACCES;
10408fb4fc68SGuillem Jover 	}
104128b83c51SKOSAKI Motohiro 
104228b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
104328b83c51SKOSAKI Motohiro 
104428b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
104599f89551SEric W. Biederman 	put_task_struct(task);
10465d863b89SKOSAKI Motohiro 
10475d863b89SKOSAKI Motohiro 	return count;
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
105000977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
10511da177e4SLinus Torvalds 	.read		= oom_adjust_read,
10521da177e4SLinus Torvalds 	.write		= oom_adjust_write,
105387df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
10541da177e4SLinus Torvalds };
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10571da177e4SLinus Torvalds #define TMPBUFLEN 21
10581da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10591da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10601da177e4SLinus Torvalds {
10612fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
106299f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10631da177e4SLinus Torvalds 	ssize_t length;
10641da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10651da177e4SLinus Torvalds 
106699f89551SEric W. Biederman 	if (!task)
106799f89551SEric W. Biederman 		return -ESRCH;
10681da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
10690c11b942SAl Viro 				audit_get_loginuid(task));
107099f89551SEric W. Biederman 	put_task_struct(task);
10711da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10751da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
10761da177e4SLinus Torvalds {
10772fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
10781da177e4SLinus Torvalds 	char *page, *tmp;
10791da177e4SLinus Torvalds 	ssize_t length;
10801da177e4SLinus Torvalds 	uid_t loginuid;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
10831da177e4SLinus Torvalds 		return -EPERM;
10841da177e4SLinus Torvalds 
10857dc52157SPaul E. McKenney 	rcu_read_lock();
10867dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
10877dc52157SPaul E. McKenney 		rcu_read_unlock();
10881da177e4SLinus Torvalds 		return -EPERM;
10897dc52157SPaul E. McKenney 	}
10907dc52157SPaul E. McKenney 	rcu_read_unlock();
10911da177e4SLinus Torvalds 
1092e0182909SAl Viro 	if (count >= PAGE_SIZE)
1093e0182909SAl Viro 		count = PAGE_SIZE - 1;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	if (*ppos != 0) {
10961da177e4SLinus Torvalds 		/* No partial writes. */
10971da177e4SLinus Torvalds 		return -EINVAL;
10981da177e4SLinus Torvalds 	}
1099e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11001da177e4SLinus Torvalds 	if (!page)
11011da177e4SLinus Torvalds 		return -ENOMEM;
11021da177e4SLinus Torvalds 	length = -EFAULT;
11031da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11041da177e4SLinus Torvalds 		goto out_free_page;
11051da177e4SLinus Torvalds 
1106e0182909SAl Viro 	page[count] = '\0';
11071da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11081da177e4SLinus Torvalds 	if (tmp == page) {
11091da177e4SLinus Torvalds 		length = -EINVAL;
11101da177e4SLinus Torvalds 		goto out_free_page;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	}
111399f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
11141da177e4SLinus Torvalds 	if (likely(length == 0))
11151da177e4SLinus Torvalds 		length = count;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds out_free_page:
11181da177e4SLinus Torvalds 	free_page((unsigned long) page);
11191da177e4SLinus Torvalds 	return length;
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds 
112200977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
11231da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
11241da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
112587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11261da177e4SLinus Torvalds };
11271e0bd755SEric Paris 
11281e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
11291e0bd755SEric Paris 				  size_t count, loff_t *ppos)
11301e0bd755SEric Paris {
11311e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
11321e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
11331e0bd755SEric Paris 	ssize_t length;
11341e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
11351e0bd755SEric Paris 
11361e0bd755SEric Paris 	if (!task)
11371e0bd755SEric Paris 		return -ESRCH;
11381e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11391e0bd755SEric Paris 				audit_get_sessionid(task));
11401e0bd755SEric Paris 	put_task_struct(task);
11411e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11421e0bd755SEric Paris }
11431e0bd755SEric Paris 
11441e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
11451e0bd755SEric Paris 	.read		= proc_sessionid_read,
114687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
11471e0bd755SEric Paris };
11481da177e4SLinus Torvalds #endif
11491da177e4SLinus Torvalds 
1150f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1151f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1152f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1153f4f154fdSAkinobu Mita {
1154f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1155f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1156f4f154fdSAkinobu Mita 	size_t len;
1157f4f154fdSAkinobu Mita 	int make_it_fail;
1158f4f154fdSAkinobu Mita 
1159f4f154fdSAkinobu Mita 	if (!task)
1160f4f154fdSAkinobu Mita 		return -ESRCH;
1161f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1162f4f154fdSAkinobu Mita 	put_task_struct(task);
1163f4f154fdSAkinobu Mita 
1164f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11650c28f287SAkinobu Mita 
11660c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1167f4f154fdSAkinobu Mita }
1168f4f154fdSAkinobu Mita 
1169f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1170f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1171f4f154fdSAkinobu Mita {
1172f4f154fdSAkinobu Mita 	struct task_struct *task;
1173f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1174f4f154fdSAkinobu Mita 	int make_it_fail;
1175f4f154fdSAkinobu Mita 
1176f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1177f4f154fdSAkinobu Mita 		return -EPERM;
1178f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1179f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1180f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1181f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1182f4f154fdSAkinobu Mita 		return -EFAULT;
1183cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1184cba8aafeSVincent Li 	if (*end)
1185cba8aafeSVincent Li 		return -EINVAL;
1186f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1187f4f154fdSAkinobu Mita 	if (!task)
1188f4f154fdSAkinobu Mita 		return -ESRCH;
1189f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1190f4f154fdSAkinobu Mita 	put_task_struct(task);
1191cba8aafeSVincent Li 
1192cba8aafeSVincent Li 	return count;
1193f4f154fdSAkinobu Mita }
1194f4f154fdSAkinobu Mita 
119500977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1196f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1197f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
119887df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1199f4f154fdSAkinobu Mita };
1200f4f154fdSAkinobu Mita #endif
1201f4f154fdSAkinobu Mita 
12029745512cSArjan van de Ven 
120343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
120443ae34cbSIngo Molnar /*
120543ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
120643ae34cbSIngo Molnar  */
120743ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
120843ae34cbSIngo Molnar {
120943ae34cbSIngo Molnar 	struct inode *inode = m->private;
121043ae34cbSIngo Molnar 	struct task_struct *p;
121143ae34cbSIngo Molnar 
121243ae34cbSIngo Molnar 	p = get_proc_task(inode);
121343ae34cbSIngo Molnar 	if (!p)
121443ae34cbSIngo Molnar 		return -ESRCH;
121543ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
121643ae34cbSIngo Molnar 
121743ae34cbSIngo Molnar 	put_task_struct(p);
121843ae34cbSIngo Molnar 
121943ae34cbSIngo Molnar 	return 0;
122043ae34cbSIngo Molnar }
122143ae34cbSIngo Molnar 
122243ae34cbSIngo Molnar static ssize_t
122343ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
122443ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
122543ae34cbSIngo Molnar {
122643ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
122743ae34cbSIngo Molnar 	struct task_struct *p;
122843ae34cbSIngo Molnar 
122943ae34cbSIngo Molnar 	p = get_proc_task(inode);
123043ae34cbSIngo Molnar 	if (!p)
123143ae34cbSIngo Molnar 		return -ESRCH;
123243ae34cbSIngo Molnar 	proc_sched_set_task(p);
123343ae34cbSIngo Molnar 
123443ae34cbSIngo Molnar 	put_task_struct(p);
123543ae34cbSIngo Molnar 
123643ae34cbSIngo Molnar 	return count;
123743ae34cbSIngo Molnar }
123843ae34cbSIngo Molnar 
123943ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
124043ae34cbSIngo Molnar {
124143ae34cbSIngo Molnar 	int ret;
124243ae34cbSIngo Molnar 
124343ae34cbSIngo Molnar 	ret = single_open(filp, sched_show, NULL);
124443ae34cbSIngo Molnar 	if (!ret) {
124543ae34cbSIngo Molnar 		struct seq_file *m = filp->private_data;
124643ae34cbSIngo Molnar 
124743ae34cbSIngo Molnar 		m->private = inode;
124843ae34cbSIngo Molnar 	}
124943ae34cbSIngo Molnar 	return ret;
125043ae34cbSIngo Molnar }
125143ae34cbSIngo Molnar 
125243ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
125343ae34cbSIngo Molnar 	.open		= sched_open,
125443ae34cbSIngo Molnar 	.read		= seq_read,
125543ae34cbSIngo Molnar 	.write		= sched_write,
125643ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12575ea473a1SAlexey Dobriyan 	.release	= single_release,
125843ae34cbSIngo Molnar };
125943ae34cbSIngo Molnar 
126043ae34cbSIngo Molnar #endif
126143ae34cbSIngo Molnar 
12624614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
12634614a696Sjohn stultz 				size_t count, loff_t *offset)
12644614a696Sjohn stultz {
12654614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
12664614a696Sjohn stultz 	struct task_struct *p;
12674614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
12684614a696Sjohn stultz 
12694614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
12704614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
12714614a696Sjohn stultz 		count = sizeof(buffer) - 1;
12724614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
12734614a696Sjohn stultz 		return -EFAULT;
12744614a696Sjohn stultz 
12754614a696Sjohn stultz 	p = get_proc_task(inode);
12764614a696Sjohn stultz 	if (!p)
12774614a696Sjohn stultz 		return -ESRCH;
12784614a696Sjohn stultz 
12794614a696Sjohn stultz 	if (same_thread_group(current, p))
12804614a696Sjohn stultz 		set_task_comm(p, buffer);
12814614a696Sjohn stultz 	else
12824614a696Sjohn stultz 		count = -EINVAL;
12834614a696Sjohn stultz 
12844614a696Sjohn stultz 	put_task_struct(p);
12854614a696Sjohn stultz 
12864614a696Sjohn stultz 	return count;
12874614a696Sjohn stultz }
12884614a696Sjohn stultz 
12894614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
12904614a696Sjohn stultz {
12914614a696Sjohn stultz 	struct inode *inode = m->private;
12924614a696Sjohn stultz 	struct task_struct *p;
12934614a696Sjohn stultz 
12944614a696Sjohn stultz 	p = get_proc_task(inode);
12954614a696Sjohn stultz 	if (!p)
12964614a696Sjohn stultz 		return -ESRCH;
12974614a696Sjohn stultz 
12984614a696Sjohn stultz 	task_lock(p);
12994614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13004614a696Sjohn stultz 	task_unlock(p);
13014614a696Sjohn stultz 
13024614a696Sjohn stultz 	put_task_struct(p);
13034614a696Sjohn stultz 
13044614a696Sjohn stultz 	return 0;
13054614a696Sjohn stultz }
13064614a696Sjohn stultz 
13074614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
13084614a696Sjohn stultz {
13094614a696Sjohn stultz 	int ret;
13104614a696Sjohn stultz 
13114614a696Sjohn stultz 	ret = single_open(filp, comm_show, NULL);
13124614a696Sjohn stultz 	if (!ret) {
13134614a696Sjohn stultz 		struct seq_file *m = filp->private_data;
13144614a696Sjohn stultz 
13154614a696Sjohn stultz 		m->private = inode;
13164614a696Sjohn stultz 	}
13174614a696Sjohn stultz 	return ret;
13184614a696Sjohn stultz }
13194614a696Sjohn stultz 
13204614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
13214614a696Sjohn stultz 	.open		= comm_open,
13224614a696Sjohn stultz 	.read		= seq_read,
13234614a696Sjohn stultz 	.write		= comm_write,
13244614a696Sjohn stultz 	.llseek		= seq_lseek,
13254614a696Sjohn stultz 	.release	= single_release,
13264614a696Sjohn stultz };
13274614a696Sjohn stultz 
1328925d1c40SMatt Helsley /*
1329925d1c40SMatt Helsley  * We added or removed a vma mapping the executable. The vmas are only mapped
1330925d1c40SMatt Helsley  * during exec and are not mapped with the mmap system call.
1331925d1c40SMatt Helsley  * Callers must hold down_write() on the mm's mmap_sem for these
1332925d1c40SMatt Helsley  */
1333925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm)
1334925d1c40SMatt Helsley {
1335925d1c40SMatt Helsley 	mm->num_exe_file_vmas++;
1336925d1c40SMatt Helsley }
1337925d1c40SMatt Helsley 
1338925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm)
1339925d1c40SMatt Helsley {
1340925d1c40SMatt Helsley 	mm->num_exe_file_vmas--;
1341925d1c40SMatt Helsley 	if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1342925d1c40SMatt Helsley 		fput(mm->exe_file);
1343925d1c40SMatt Helsley 		mm->exe_file = NULL;
1344925d1c40SMatt Helsley 	}
1345925d1c40SMatt Helsley 
1346925d1c40SMatt Helsley }
1347925d1c40SMatt Helsley 
1348925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1349925d1c40SMatt Helsley {
1350925d1c40SMatt Helsley 	if (new_exe_file)
1351925d1c40SMatt Helsley 		get_file(new_exe_file);
1352925d1c40SMatt Helsley 	if (mm->exe_file)
1353925d1c40SMatt Helsley 		fput(mm->exe_file);
1354925d1c40SMatt Helsley 	mm->exe_file = new_exe_file;
1355925d1c40SMatt Helsley 	mm->num_exe_file_vmas = 0;
1356925d1c40SMatt Helsley }
1357925d1c40SMatt Helsley 
1358925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm)
1359925d1c40SMatt Helsley {
1360925d1c40SMatt Helsley 	struct file *exe_file;
1361925d1c40SMatt Helsley 
1362925d1c40SMatt Helsley 	/* We need mmap_sem to protect against races with removal of
1363925d1c40SMatt Helsley 	 * VM_EXECUTABLE vmas */
1364925d1c40SMatt Helsley 	down_read(&mm->mmap_sem);
1365925d1c40SMatt Helsley 	exe_file = mm->exe_file;
1366925d1c40SMatt Helsley 	if (exe_file)
1367925d1c40SMatt Helsley 		get_file(exe_file);
1368925d1c40SMatt Helsley 	up_read(&mm->mmap_sem);
1369925d1c40SMatt Helsley 	return exe_file;
1370925d1c40SMatt Helsley }
1371925d1c40SMatt Helsley 
1372925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1373925d1c40SMatt Helsley {
1374925d1c40SMatt Helsley 	/* It's safe to write the exe_file pointer without exe_file_lock because
1375925d1c40SMatt Helsley 	 * this is called during fork when the task is not yet in /proc */
1376925d1c40SMatt Helsley 	newmm->exe_file = get_mm_exe_file(oldmm);
1377925d1c40SMatt Helsley }
1378925d1c40SMatt Helsley 
1379925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path)
1380925d1c40SMatt Helsley {
1381925d1c40SMatt Helsley 	struct task_struct *task;
1382925d1c40SMatt Helsley 	struct mm_struct *mm;
1383925d1c40SMatt Helsley 	struct file *exe_file;
1384925d1c40SMatt Helsley 
1385925d1c40SMatt Helsley 	task = get_proc_task(inode);
1386925d1c40SMatt Helsley 	if (!task)
1387925d1c40SMatt Helsley 		return -ENOENT;
1388925d1c40SMatt Helsley 	mm = get_task_mm(task);
1389925d1c40SMatt Helsley 	put_task_struct(task);
1390925d1c40SMatt Helsley 	if (!mm)
1391925d1c40SMatt Helsley 		return -ENOENT;
1392925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1393925d1c40SMatt Helsley 	mmput(mm);
1394925d1c40SMatt Helsley 	if (exe_file) {
1395925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1396925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1397925d1c40SMatt Helsley 		fput(exe_file);
1398925d1c40SMatt Helsley 		return 0;
1399925d1c40SMatt Helsley 	} else
1400925d1c40SMatt Helsley 		return -ENOENT;
1401925d1c40SMatt Helsley }
1402925d1c40SMatt Helsley 
1403008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14041da177e4SLinus Torvalds {
14051da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14061da177e4SLinus Torvalds 	int error = -EACCES;
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
14091d957f9bSJan Blunck 	path_put(&nd->path);
14101da177e4SLinus Torvalds 
1411778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1412778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14131da177e4SLinus Torvalds 		goto out;
14141da177e4SLinus Torvalds 
14153dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
14161da177e4SLinus Torvalds out:
1417008b150aSAl Viro 	return ERR_PTR(error);
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds 
14203dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
14211da177e4SLinus Torvalds {
1422e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
14233dcd25f3SJan Blunck 	char *pathname;
14241da177e4SLinus Torvalds 	int len;
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 	if (!tmp)
14271da177e4SLinus Torvalds 		return -ENOMEM;
14281da177e4SLinus Torvalds 
1429cf28b486SJan Blunck 	pathname = d_path(path, tmp, PAGE_SIZE);
14303dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
14313dcd25f3SJan Blunck 	if (IS_ERR(pathname))
14321da177e4SLinus Torvalds 		goto out;
14333dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	if (len > buflen)
14361da177e4SLinus Torvalds 		len = buflen;
14373dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
14381da177e4SLinus Torvalds 		len = -EFAULT;
14391da177e4SLinus Torvalds  out:
14401da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
14411da177e4SLinus Torvalds 	return len;
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
14451da177e4SLinus Torvalds {
14461da177e4SLinus Torvalds 	int error = -EACCES;
14471da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14483dcd25f3SJan Blunck 	struct path path;
14491da177e4SLinus Torvalds 
1450778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1451778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14521da177e4SLinus Torvalds 		goto out;
14531da177e4SLinus Torvalds 
14543dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &path);
14551da177e4SLinus Torvalds 	if (error)
14561da177e4SLinus Torvalds 		goto out;
14571da177e4SLinus Torvalds 
14583dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
14593dcd25f3SJan Blunck 	path_put(&path);
14601da177e4SLinus Torvalds out:
14611da177e4SLinus Torvalds 	return error;
14621da177e4SLinus Torvalds }
14631da177e4SLinus Torvalds 
1464c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
14651da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
14666d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
14676d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
14681da177e4SLinus Torvalds };
14691da177e4SLinus Torvalds 
147028a6d671SEric W. Biederman 
147128a6d671SEric W. Biederman /* building an inode */
147228a6d671SEric W. Biederman 
147328a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
147428a6d671SEric W. Biederman {
147528a6d671SEric W. Biederman 	int dumpable = 0;
147628a6d671SEric W. Biederman 	struct mm_struct *mm;
147728a6d671SEric W. Biederman 
147828a6d671SEric W. Biederman 	task_lock(task);
147928a6d671SEric W. Biederman 	mm = task->mm;
148028a6d671SEric W. Biederman 	if (mm)
14816c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
148228a6d671SEric W. Biederman 	task_unlock(task);
148328a6d671SEric W. Biederman 	if(dumpable == 1)
148428a6d671SEric W. Biederman 		return 1;
148528a6d671SEric W. Biederman 	return 0;
148628a6d671SEric W. Biederman }
148728a6d671SEric W. Biederman 
148828a6d671SEric W. Biederman 
148961a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
149028a6d671SEric W. Biederman {
149128a6d671SEric W. Biederman 	struct inode * inode;
149228a6d671SEric W. Biederman 	struct proc_inode *ei;
1493c69e8d9cSDavid Howells 	const struct cred *cred;
149428a6d671SEric W. Biederman 
149528a6d671SEric W. Biederman 	/* We need a new inode */
149628a6d671SEric W. Biederman 
149728a6d671SEric W. Biederman 	inode = new_inode(sb);
149828a6d671SEric W. Biederman 	if (!inode)
149928a6d671SEric W. Biederman 		goto out;
150028a6d671SEric W. Biederman 
150128a6d671SEric W. Biederman 	/* Common stuff */
150228a6d671SEric W. Biederman 	ei = PROC_I(inode);
150328a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
150428a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
150528a6d671SEric W. Biederman 
150628a6d671SEric W. Biederman 	/*
150728a6d671SEric W. Biederman 	 * grab the reference to task.
150828a6d671SEric W. Biederman 	 */
15091a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
151028a6d671SEric W. Biederman 	if (!ei->pid)
151128a6d671SEric W. Biederman 		goto out_unlock;
151228a6d671SEric W. Biederman 
151328a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1514c69e8d9cSDavid Howells 		rcu_read_lock();
1515c69e8d9cSDavid Howells 		cred = __task_cred(task);
1516c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1517c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1518c69e8d9cSDavid Howells 		rcu_read_unlock();
151928a6d671SEric W. Biederman 	}
152028a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
152128a6d671SEric W. Biederman 
152228a6d671SEric W. Biederman out:
152328a6d671SEric W. Biederman 	return inode;
152428a6d671SEric W. Biederman 
152528a6d671SEric W. Biederman out_unlock:
152628a6d671SEric W. Biederman 	iput(inode);
152728a6d671SEric W. Biederman 	return NULL;
152828a6d671SEric W. Biederman }
152928a6d671SEric W. Biederman 
153028a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
153128a6d671SEric W. Biederman {
153228a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
153328a6d671SEric W. Biederman 	struct task_struct *task;
1534c69e8d9cSDavid Howells 	const struct cred *cred;
1535c69e8d9cSDavid Howells 
153628a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
153728a6d671SEric W. Biederman 
153828a6d671SEric W. Biederman 	rcu_read_lock();
153928a6d671SEric W. Biederman 	stat->uid = 0;
154028a6d671SEric W. Biederman 	stat->gid = 0;
154128a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
154228a6d671SEric W. Biederman 	if (task) {
154328a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
154428a6d671SEric W. Biederman 		    task_dumpable(task)) {
1545c69e8d9cSDavid Howells 			cred = __task_cred(task);
1546c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1547c69e8d9cSDavid Howells 			stat->gid = cred->egid;
154828a6d671SEric W. Biederman 		}
154928a6d671SEric W. Biederman 	}
155028a6d671SEric W. Biederman 	rcu_read_unlock();
155128a6d671SEric W. Biederman 	return 0;
155228a6d671SEric W. Biederman }
155328a6d671SEric W. Biederman 
155428a6d671SEric W. Biederman /* dentry stuff */
155528a6d671SEric W. Biederman 
155628a6d671SEric W. Biederman /*
155728a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
155828a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
155928a6d671SEric W. Biederman  * due to the way we treat inodes.
156028a6d671SEric W. Biederman  *
156128a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
156228a6d671SEric W. Biederman  * performed a setuid(), etc.
156328a6d671SEric W. Biederman  *
156428a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
156528a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
156628a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
156728a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
156828a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
156928a6d671SEric W. Biederman  * directories.
157028a6d671SEric W. Biederman  */
157128a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
157228a6d671SEric W. Biederman {
157328a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
157428a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1575c69e8d9cSDavid Howells 	const struct cred *cred;
1576c69e8d9cSDavid Howells 
157728a6d671SEric W. Biederman 	if (task) {
157828a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
157928a6d671SEric W. Biederman 		    task_dumpable(task)) {
1580c69e8d9cSDavid Howells 			rcu_read_lock();
1581c69e8d9cSDavid Howells 			cred = __task_cred(task);
1582c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1583c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1584c69e8d9cSDavid Howells 			rcu_read_unlock();
158528a6d671SEric W. Biederman 		} else {
158628a6d671SEric W. Biederman 			inode->i_uid = 0;
158728a6d671SEric W. Biederman 			inode->i_gid = 0;
158828a6d671SEric W. Biederman 		}
158928a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
159028a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
159128a6d671SEric W. Biederman 		put_task_struct(task);
159228a6d671SEric W. Biederman 		return 1;
159328a6d671SEric W. Biederman 	}
159428a6d671SEric W. Biederman 	d_drop(dentry);
159528a6d671SEric W. Biederman 	return 0;
159628a6d671SEric W. Biederman }
159728a6d671SEric W. Biederman 
159828a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry)
159928a6d671SEric W. Biederman {
160028a6d671SEric W. Biederman 	/* Is the task we represent dead?
160128a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
160228a6d671SEric W. Biederman 	 * kill it immediately.
160328a6d671SEric W. Biederman 	 */
160428a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
160528a6d671SEric W. Biederman }
160628a6d671SEric W. Biederman 
1607d72f71ebSAl Viro static const struct dentry_operations pid_dentry_operations =
160828a6d671SEric W. Biederman {
160928a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
161028a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
161128a6d671SEric W. Biederman };
161228a6d671SEric W. Biederman 
161328a6d671SEric W. Biederman /* Lookups */
161428a6d671SEric W. Biederman 
1615c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1616c5141e6dSEric Dumazet 				struct task_struct *, const void *);
161761a28784SEric W. Biederman 
16181c0d04c9SEric W. Biederman /*
16191c0d04c9SEric W. Biederman  * Fill a directory entry.
16201c0d04c9SEric W. Biederman  *
16211c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
16221c0d04c9SEric W. Biederman  * file type from dcache entry.
16231c0d04c9SEric W. Biederman  *
16241c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
16251c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
16261c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
16271c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
16281c0d04c9SEric W. Biederman  * by stat.
16291c0d04c9SEric W. Biederman  */
163061a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
163161a28784SEric W. Biederman 	char *name, int len,
1632c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
163361a28784SEric W. Biederman {
16342fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
163561a28784SEric W. Biederman 	struct inode *inode;
163661a28784SEric W. Biederman 	struct qstr qname;
163761a28784SEric W. Biederman 	ino_t ino = 0;
163861a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
163961a28784SEric W. Biederman 
164061a28784SEric W. Biederman 	qname.name = name;
164161a28784SEric W. Biederman 	qname.len  = len;
164261a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
164361a28784SEric W. Biederman 
164461a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
164561a28784SEric W. Biederman 	if (!child) {
164661a28784SEric W. Biederman 		struct dentry *new;
164761a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
164861a28784SEric W. Biederman 		if (new) {
164961a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
165061a28784SEric W. Biederman 			if (child)
165161a28784SEric W. Biederman 				dput(new);
165261a28784SEric W. Biederman 			else
165361a28784SEric W. Biederman 				child = new;
165461a28784SEric W. Biederman 		}
165561a28784SEric W. Biederman 	}
165661a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
165761a28784SEric W. Biederman 		goto end_instantiate;
165861a28784SEric W. Biederman 	inode = child->d_inode;
165961a28784SEric W. Biederman 	if (inode) {
166061a28784SEric W. Biederman 		ino = inode->i_ino;
166161a28784SEric W. Biederman 		type = inode->i_mode >> 12;
166261a28784SEric W. Biederman 	}
166361a28784SEric W. Biederman 	dput(child);
166461a28784SEric W. Biederman end_instantiate:
166561a28784SEric W. Biederman 	if (!ino)
166661a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
166761a28784SEric W. Biederman 	if (!ino)
166861a28784SEric W. Biederman 		ino = 1;
166961a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
167061a28784SEric W. Biederman }
167161a28784SEric W. Biederman 
167228a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
167328a6d671SEric W. Biederman {
167428a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
167528a6d671SEric W. Biederman 	int len = dentry->d_name.len;
167628a6d671SEric W. Biederman 	unsigned n = 0;
167728a6d671SEric W. Biederman 
167828a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
167928a6d671SEric W. Biederman 		goto out;
168028a6d671SEric W. Biederman 	while (len-- > 0) {
168128a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
168228a6d671SEric W. Biederman 		if (c > 9)
168328a6d671SEric W. Biederman 			goto out;
168428a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
168528a6d671SEric W. Biederman 			goto out;
168628a6d671SEric W. Biederman 		n *= 10;
168728a6d671SEric W. Biederman 		n += c;
168828a6d671SEric W. Biederman 	}
168928a6d671SEric W. Biederman 	return n;
169028a6d671SEric W. Biederman out:
169128a6d671SEric W. Biederman 	return ~0U;
169228a6d671SEric W. Biederman }
169328a6d671SEric W. Biederman 
169427932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
169527932742SMiklos Szeredi 
16963dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
169728a6d671SEric W. Biederman {
169828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
169928a6d671SEric W. Biederman 	struct files_struct *files = NULL;
170028a6d671SEric W. Biederman 	struct file *file;
170128a6d671SEric W. Biederman 	int fd = proc_fd(inode);
170228a6d671SEric W. Biederman 
170328a6d671SEric W. Biederman 	if (task) {
170428a6d671SEric W. Biederman 		files = get_files_struct(task);
170528a6d671SEric W. Biederman 		put_task_struct(task);
170628a6d671SEric W. Biederman 	}
170728a6d671SEric W. Biederman 	if (files) {
170828a6d671SEric W. Biederman 		/*
170928a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
171028a6d671SEric W. Biederman 		 * hold ->file_lock.
171128a6d671SEric W. Biederman 		 */
171228a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
171328a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
171428a6d671SEric W. Biederman 		if (file) {
17153dcd25f3SJan Blunck 			if (path) {
17163dcd25f3SJan Blunck 				*path = file->f_path;
17173dcd25f3SJan Blunck 				path_get(&file->f_path);
17183dcd25f3SJan Blunck 			}
171927932742SMiklos Szeredi 			if (info)
172027932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
172127932742SMiklos Szeredi 					 "pos:\t%lli\n"
172227932742SMiklos Szeredi 					 "flags:\t0%o\n",
172327932742SMiklos Szeredi 					 (long long) file->f_pos,
172427932742SMiklos Szeredi 					 file->f_flags);
172528a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
172628a6d671SEric W. Biederman 			put_files_struct(files);
172728a6d671SEric W. Biederman 			return 0;
172828a6d671SEric W. Biederman 		}
172928a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
173028a6d671SEric W. Biederman 		put_files_struct(files);
173128a6d671SEric W. Biederman 	}
173228a6d671SEric W. Biederman 	return -ENOENT;
173328a6d671SEric W. Biederman }
173428a6d671SEric W. Biederman 
17353dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path)
173627932742SMiklos Szeredi {
17373dcd25f3SJan Blunck 	return proc_fd_info(inode, path, NULL);
173827932742SMiklos Szeredi }
173927932742SMiklos Szeredi 
174028a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
174128a6d671SEric W. Biederman {
174228a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
174328a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
174428a6d671SEric W. Biederman 	int fd = proc_fd(inode);
174528a6d671SEric W. Biederman 	struct files_struct *files;
1746c69e8d9cSDavid Howells 	const struct cred *cred;
174728a6d671SEric W. Biederman 
174828a6d671SEric W. Biederman 	if (task) {
174928a6d671SEric W. Biederman 		files = get_files_struct(task);
175028a6d671SEric W. Biederman 		if (files) {
175128a6d671SEric W. Biederman 			rcu_read_lock();
175228a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
175328a6d671SEric W. Biederman 				rcu_read_unlock();
175428a6d671SEric W. Biederman 				put_files_struct(files);
175528a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1756c69e8d9cSDavid Howells 					rcu_read_lock();
1757c69e8d9cSDavid Howells 					cred = __task_cred(task);
1758c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1759c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1760c69e8d9cSDavid Howells 					rcu_read_unlock();
176128a6d671SEric W. Biederman 				} else {
176228a6d671SEric W. Biederman 					inode->i_uid = 0;
176328a6d671SEric W. Biederman 					inode->i_gid = 0;
176428a6d671SEric W. Biederman 				}
176528a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
176628a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
176728a6d671SEric W. Biederman 				put_task_struct(task);
176828a6d671SEric W. Biederman 				return 1;
176928a6d671SEric W. Biederman 			}
177028a6d671SEric W. Biederman 			rcu_read_unlock();
177128a6d671SEric W. Biederman 			put_files_struct(files);
177228a6d671SEric W. Biederman 		}
177328a6d671SEric W. Biederman 		put_task_struct(task);
177428a6d671SEric W. Biederman 	}
177528a6d671SEric W. Biederman 	d_drop(dentry);
177628a6d671SEric W. Biederman 	return 0;
177728a6d671SEric W. Biederman }
177828a6d671SEric W. Biederman 
1779d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
178028a6d671SEric W. Biederman {
178128a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
178228a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
178328a6d671SEric W. Biederman };
178428a6d671SEric W. Biederman 
1785444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1786c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
178728a6d671SEric W. Biederman {
1788c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
178928a6d671SEric W. Biederman 	struct file *file;
179028a6d671SEric W. Biederman 	struct files_struct *files;
179128a6d671SEric W. Biederman  	struct inode *inode;
179228a6d671SEric W. Biederman  	struct proc_inode *ei;
1793444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
179428a6d671SEric W. Biederman 
179561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
179628a6d671SEric W. Biederman 	if (!inode)
179728a6d671SEric W. Biederman 		goto out;
179828a6d671SEric W. Biederman 	ei = PROC_I(inode);
179928a6d671SEric W. Biederman 	ei->fd = fd;
180028a6d671SEric W. Biederman 	files = get_files_struct(task);
180128a6d671SEric W. Biederman 	if (!files)
1802444ceed8SEric W. Biederman 		goto out_iput;
180328a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
180428a6d671SEric W. Biederman 
180528a6d671SEric W. Biederman 	/*
180628a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
180728a6d671SEric W. Biederman 	 * hold ->file_lock.
180828a6d671SEric W. Biederman 	 */
180928a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
181028a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
181128a6d671SEric W. Biederman 	if (!file)
1812444ceed8SEric W. Biederman 		goto out_unlock;
1813aeb5d727SAl Viro 	if (file->f_mode & FMODE_READ)
181428a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
1815aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
181628a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
181728a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
181828a6d671SEric W. Biederman 	put_files_struct(files);
1819444ceed8SEric W. Biederman 
182028a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
182128a6d671SEric W. Biederman 	inode->i_size = 64;
182228a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
182328a6d671SEric W. Biederman 	dentry->d_op = &tid_fd_dentry_operations;
182428a6d671SEric W. Biederman 	d_add(dentry, inode);
182528a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
182628a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1827444ceed8SEric W. Biederman 		error = NULL;
1828444ceed8SEric W. Biederman 
1829444ceed8SEric W. Biederman  out:
1830444ceed8SEric W. Biederman 	return error;
1831444ceed8SEric W. Biederman out_unlock:
1832444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1833444ceed8SEric W. Biederman 	put_files_struct(files);
1834444ceed8SEric W. Biederman out_iput:
1835444ceed8SEric W. Biederman 	iput(inode);
1836444ceed8SEric W. Biederman 	goto out;
1837444ceed8SEric W. Biederman }
1838444ceed8SEric W. Biederman 
183927932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
184027932742SMiklos Szeredi 					   struct dentry *dentry,
184127932742SMiklos Szeredi 					   instantiate_t instantiate)
1842444ceed8SEric W. Biederman {
1843444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1844444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1845444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1846444ceed8SEric W. Biederman 
1847444ceed8SEric W. Biederman 	if (!task)
1848444ceed8SEric W. Biederman 		goto out_no_task;
1849444ceed8SEric W. Biederman 	if (fd == ~0U)
1850444ceed8SEric W. Biederman 		goto out;
1851444ceed8SEric W. Biederman 
185227932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
185328a6d671SEric W. Biederman out:
185428a6d671SEric W. Biederman 	put_task_struct(task);
185528a6d671SEric W. Biederman out_no_task:
185628a6d671SEric W. Biederman 	return result;
185728a6d671SEric W. Biederman }
185828a6d671SEric W. Biederman 
185927932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
186027932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
18611da177e4SLinus Torvalds {
18622fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
18635634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
186499f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1865457c2510SPavel Emelyanov 	unsigned int fd, ino;
18661da177e4SLinus Torvalds 	int retval;
18671da177e4SLinus Torvalds 	struct files_struct * files;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	retval = -ENOENT;
187099f89551SEric W. Biederman 	if (!p)
187199f89551SEric W. Biederman 		goto out_no_task;
18721da177e4SLinus Torvalds 	retval = 0;
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	fd = filp->f_pos;
18751da177e4SLinus Torvalds 	switch (fd) {
18761da177e4SLinus Torvalds 		case 0:
18771da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
18781da177e4SLinus Torvalds 				goto out;
18791da177e4SLinus Torvalds 			filp->f_pos++;
18801da177e4SLinus Torvalds 		case 1:
18815634708bSEric W. Biederman 			ino = parent_ino(dentry);
18821da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
18831da177e4SLinus Torvalds 				goto out;
18841da177e4SLinus Torvalds 			filp->f_pos++;
18851da177e4SLinus Torvalds 		default:
18861da177e4SLinus Torvalds 			files = get_files_struct(p);
18871da177e4SLinus Torvalds 			if (!files)
18881da177e4SLinus Torvalds 				goto out;
1889b835996fSDipankar Sarma 			rcu_read_lock();
18901da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
18919b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
18921da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
189327932742SMiklos Szeredi 				char name[PROC_NUMBUF];
189427932742SMiklos Szeredi 				int len;
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
18971da177e4SLinus Torvalds 					continue;
1898b835996fSDipankar Sarma 				rcu_read_unlock();
18991da177e4SLinus Torvalds 
190027932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
190127932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
190227932742SMiklos Szeredi 						    name, len, instantiate,
190327932742SMiklos Szeredi 						    p, &fd) < 0) {
1904b835996fSDipankar Sarma 					rcu_read_lock();
19051da177e4SLinus Torvalds 					break;
19061da177e4SLinus Torvalds 				}
1907b835996fSDipankar Sarma 				rcu_read_lock();
19081da177e4SLinus Torvalds 			}
1909b835996fSDipankar Sarma 			rcu_read_unlock();
19101da177e4SLinus Torvalds 			put_files_struct(files);
19111da177e4SLinus Torvalds 	}
19121da177e4SLinus Torvalds out:
191399f89551SEric W. Biederman 	put_task_struct(p);
191499f89551SEric W. Biederman out_no_task:
19151da177e4SLinus Torvalds 	return retval;
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
191827932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
191927932742SMiklos Szeredi 				    struct nameidata *nd)
192027932742SMiklos Szeredi {
192127932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
192227932742SMiklos Szeredi }
192327932742SMiklos Szeredi 
192427932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
192527932742SMiklos Szeredi {
192627932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
192727932742SMiklos Szeredi }
192827932742SMiklos Szeredi 
192927932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
193027932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
193127932742SMiklos Szeredi {
193227932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
19333dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
193427932742SMiklos Szeredi 	if (!err)
193527932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
193627932742SMiklos Szeredi 	return err;
193727932742SMiklos Szeredi }
193827932742SMiklos Szeredi 
193927932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
194027932742SMiklos Szeredi 	.open           = nonseekable_open,
194127932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
194227932742SMiklos Szeredi };
194327932742SMiklos Szeredi 
194400977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
19451da177e4SLinus Torvalds 	.read		= generic_read_dir,
19461da177e4SLinus Torvalds 	.readdir	= proc_readfd,
19471da177e4SLinus Torvalds };
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds /*
19508948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
19518948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
19528948e11fSAlexey Dobriyan  */
1953e6305c43SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
19548948e11fSAlexey Dobriyan {
19558948e11fSAlexey Dobriyan 	int rv;
19568948e11fSAlexey Dobriyan 
19578948e11fSAlexey Dobriyan 	rv = generic_permission(inode, mask, NULL);
19588948e11fSAlexey Dobriyan 	if (rv == 0)
19598948e11fSAlexey Dobriyan 		return 0;
19608948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
19618948e11fSAlexey Dobriyan 		rv = 0;
19628948e11fSAlexey Dobriyan 	return rv;
19638948e11fSAlexey Dobriyan }
19648948e11fSAlexey Dobriyan 
19658948e11fSAlexey Dobriyan /*
19661da177e4SLinus Torvalds  * proc directories can do almost nothing..
19671da177e4SLinus Torvalds  */
1968c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
19691da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
19708948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
19716d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
19721da177e4SLinus Torvalds };
19731da177e4SLinus Torvalds 
197427932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
197527932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
197627932742SMiklos Szeredi {
197727932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
197827932742SMiklos Szeredi  	struct inode *inode;
197927932742SMiklos Szeredi  	struct proc_inode *ei;
198027932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
198127932742SMiklos Szeredi 
198227932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
198327932742SMiklos Szeredi 	if (!inode)
198427932742SMiklos Szeredi 		goto out;
198527932742SMiklos Szeredi 	ei = PROC_I(inode);
198627932742SMiklos Szeredi 	ei->fd = fd;
198727932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
198827932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
198927932742SMiklos Szeredi 	dentry->d_op = &tid_fd_dentry_operations;
199027932742SMiklos Szeredi 	d_add(dentry, inode);
199127932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
199227932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
199327932742SMiklos Szeredi 		error = NULL;
199427932742SMiklos Szeredi 
199527932742SMiklos Szeredi  out:
199627932742SMiklos Szeredi 	return error;
199727932742SMiklos Szeredi }
199827932742SMiklos Szeredi 
199927932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
200027932742SMiklos Szeredi 					struct dentry *dentry,
200127932742SMiklos Szeredi 					struct nameidata *nd)
200227932742SMiklos Szeredi {
200327932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
200427932742SMiklos Szeredi }
200527932742SMiklos Szeredi 
200627932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
200727932742SMiklos Szeredi {
200827932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
200927932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
201027932742SMiklos Szeredi }
201127932742SMiklos Szeredi 
201227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
201327932742SMiklos Szeredi 	.read		= generic_read_dir,
201427932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
201527932742SMiklos Szeredi };
201627932742SMiklos Szeredi 
201727932742SMiklos Szeredi /*
201827932742SMiklos Szeredi  * proc directories can do almost nothing..
201927932742SMiklos Szeredi  */
202027932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
202127932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
202227932742SMiklos Szeredi 	.setattr	= proc_setattr,
202327932742SMiklos Szeredi };
202427932742SMiklos Szeredi 
202527932742SMiklos Szeredi 
2026444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2027c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2028444ceed8SEric W. Biederman {
2029c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2030444ceed8SEric W. Biederman 	struct inode *inode;
2031444ceed8SEric W. Biederman 	struct proc_inode *ei;
2032bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2033444ceed8SEric W. Biederman 
203461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2035444ceed8SEric W. Biederman 	if (!inode)
2036444ceed8SEric W. Biederman 		goto out;
2037444ceed8SEric W. Biederman 
2038444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2039444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2040444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2041444ceed8SEric W. Biederman 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
2042444ceed8SEric W. Biederman 	if (p->iop)
2043444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2044444ceed8SEric W. Biederman 	if (p->fop)
2045444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2046444ceed8SEric W. Biederman 	ei->op = p->op;
2047444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2048444ceed8SEric W. Biederman 	d_add(dentry, inode);
2049444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2050444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2051444ceed8SEric W. Biederman 		error = NULL;
2052444ceed8SEric W. Biederman out:
2053444ceed8SEric W. Biederman 	return error;
2054444ceed8SEric W. Biederman }
2055444ceed8SEric W. Biederman 
20561da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
20571da177e4SLinus Torvalds 					 struct dentry *dentry,
2058c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
20597bcd6b0eSEric W. Biederman 					 unsigned int nents)
20601da177e4SLinus Torvalds {
2061cd6a3ce9SEric W. Biederman 	struct dentry *error;
206299f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2063c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
20641da177e4SLinus Torvalds 
2065cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
20661da177e4SLinus Torvalds 
206799f89551SEric W. Biederman 	if (!task)
206899f89551SEric W. Biederman 		goto out_no_task;
20691da177e4SLinus Torvalds 
207020cdc894SEric W. Biederman 	/*
207120cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
207220cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
207320cdc894SEric W. Biederman 	 */
20747bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
20757bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
20761da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
20771da177e4SLinus Torvalds 			continue;
20781da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
20791da177e4SLinus Torvalds 			break;
20801da177e4SLinus Torvalds 	}
20817bcd6b0eSEric W. Biederman 	if (p > last)
20821da177e4SLinus Torvalds 		goto out;
20831da177e4SLinus Torvalds 
2084444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
20851da177e4SLinus Torvalds out:
208699f89551SEric W. Biederman 	put_task_struct(task);
208799f89551SEric W. Biederman out_no_task:
2088cd6a3ce9SEric W. Biederman 	return error;
20891da177e4SLinus Torvalds }
20901da177e4SLinus Torvalds 
2091c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2092c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
209361a28784SEric W. Biederman {
209461a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
209561a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
209661a28784SEric W. Biederman }
209761a28784SEric W. Biederman 
209828a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
209928a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2100c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
210128a6d671SEric W. Biederman {
210228a6d671SEric W. Biederman 	int i;
21032fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
210428a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
210528a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2106c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
210728a6d671SEric W. Biederman 	ino_t ino;
210828a6d671SEric W. Biederman 	int ret;
210928a6d671SEric W. Biederman 
211028a6d671SEric W. Biederman 	ret = -ENOENT;
211128a6d671SEric W. Biederman 	if (!task)
211261a28784SEric W. Biederman 		goto out_no_task;
211328a6d671SEric W. Biederman 
211428a6d671SEric W. Biederman 	ret = 0;
211528a6d671SEric W. Biederman 	i = filp->f_pos;
211628a6d671SEric W. Biederman 	switch (i) {
211728a6d671SEric W. Biederman 	case 0:
211828a6d671SEric W. Biederman 		ino = inode->i_ino;
211928a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
212028a6d671SEric W. Biederman 			goto out;
212128a6d671SEric W. Biederman 		i++;
212228a6d671SEric W. Biederman 		filp->f_pos++;
212328a6d671SEric W. Biederman 		/* fall through */
212428a6d671SEric W. Biederman 	case 1:
212528a6d671SEric W. Biederman 		ino = parent_ino(dentry);
212628a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
212728a6d671SEric W. Biederman 			goto out;
212828a6d671SEric W. Biederman 		i++;
212928a6d671SEric W. Biederman 		filp->f_pos++;
213028a6d671SEric W. Biederman 		/* fall through */
213128a6d671SEric W. Biederman 	default:
213228a6d671SEric W. Biederman 		i -= 2;
213328a6d671SEric W. Biederman 		if (i >= nents) {
213428a6d671SEric W. Biederman 			ret = 1;
213528a6d671SEric W. Biederman 			goto out;
213628a6d671SEric W. Biederman 		}
213728a6d671SEric W. Biederman 		p = ents + i;
21387bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
21397bcd6b0eSEric W. Biederman 		while (p <= last) {
214061a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
214128a6d671SEric W. Biederman 				goto out;
214228a6d671SEric W. Biederman 			filp->f_pos++;
214328a6d671SEric W. Biederman 			p++;
214428a6d671SEric W. Biederman 		}
21451da177e4SLinus Torvalds 	}
21461da177e4SLinus Torvalds 
214728a6d671SEric W. Biederman 	ret = 1;
214828a6d671SEric W. Biederman out:
214961a28784SEric W. Biederman 	put_task_struct(task);
215061a28784SEric W. Biederman out_no_task:
215128a6d671SEric W. Biederman 	return ret;
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
215528a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
215628a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
215728a6d671SEric W. Biederman {
21582fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
215904ff9708SAl Viro 	char *p = NULL;
216028a6d671SEric W. Biederman 	ssize_t length;
216128a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
216228a6d671SEric W. Biederman 
216328a6d671SEric W. Biederman 	if (!task)
216404ff9708SAl Viro 		return -ESRCH;
216528a6d671SEric W. Biederman 
216628a6d671SEric W. Biederman 	length = security_getprocattr(task,
21672fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
216804ff9708SAl Viro 				      &p);
216928a6d671SEric W. Biederman 	put_task_struct(task);
217004ff9708SAl Viro 	if (length > 0)
217104ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
217204ff9708SAl Viro 	kfree(p);
217328a6d671SEric W. Biederman 	return length;
217428a6d671SEric W. Biederman }
217528a6d671SEric W. Biederman 
217628a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
217728a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
217828a6d671SEric W. Biederman {
21792fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
218028a6d671SEric W. Biederman 	char *page;
218128a6d671SEric W. Biederman 	ssize_t length;
218228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
218328a6d671SEric W. Biederman 
218428a6d671SEric W. Biederman 	length = -ESRCH;
218528a6d671SEric W. Biederman 	if (!task)
218628a6d671SEric W. Biederman 		goto out_no_task;
218728a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
218828a6d671SEric W. Biederman 		count = PAGE_SIZE;
218928a6d671SEric W. Biederman 
219028a6d671SEric W. Biederman 	/* No partial writes. */
219128a6d671SEric W. Biederman 	length = -EINVAL;
219228a6d671SEric W. Biederman 	if (*ppos != 0)
219328a6d671SEric W. Biederman 		goto out;
219428a6d671SEric W. Biederman 
219528a6d671SEric W. Biederman 	length = -ENOMEM;
2196e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
219728a6d671SEric W. Biederman 	if (!page)
219828a6d671SEric W. Biederman 		goto out;
219928a6d671SEric W. Biederman 
220028a6d671SEric W. Biederman 	length = -EFAULT;
220128a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
220228a6d671SEric W. Biederman 		goto out_free;
220328a6d671SEric W. Biederman 
2204107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
2205107db7c7SDavid Howells 	length = mutex_lock_interruptible(&task->cred_guard_mutex);
2206107db7c7SDavid Howells 	if (length < 0)
2207107db7c7SDavid Howells 		goto out_free;
2208107db7c7SDavid Howells 
220928a6d671SEric W. Biederman 	length = security_setprocattr(task,
22102fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
221128a6d671SEric W. Biederman 				      (void*)page, count);
2212107db7c7SDavid Howells 	mutex_unlock(&task->cred_guard_mutex);
221328a6d671SEric W. Biederman out_free:
221428a6d671SEric W. Biederman 	free_page((unsigned long) page);
221528a6d671SEric W. Biederman out:
221628a6d671SEric W. Biederman 	put_task_struct(task);
221728a6d671SEric W. Biederman out_no_task:
221828a6d671SEric W. Biederman 	return length;
221928a6d671SEric W. Biederman }
222028a6d671SEric W. Biederman 
222100977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
222228a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
222328a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
222487df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
222528a6d671SEric W. Biederman };
222628a6d671SEric W. Biederman 
2227c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2228631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2229631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2230631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2231631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2232631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2233631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
223428a6d671SEric W. Biederman };
223528a6d671SEric W. Biederman 
223672d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
22371da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
22381da177e4SLinus Torvalds {
22391da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
224072d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
22411da177e4SLinus Torvalds }
22421da177e4SLinus Torvalds 
224300977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
22441da177e4SLinus Torvalds 	.read		= generic_read_dir,
224572d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
22461da177e4SLinus Torvalds };
22471da177e4SLinus Torvalds 
224872d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
22491da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
22501da177e4SLinus Torvalds {
22517bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
22527bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
2255c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
225672d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
225799f89551SEric W. Biederman 	.getattr	= pid_getattr,
22586d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
22591da177e4SLinus Torvalds };
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds #endif
22621da177e4SLinus Torvalds 
2263698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
22643cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
22653cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
22663cb4a0bbSKawai, Hidehiro {
22673cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
22683cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
22693cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
22703cb4a0bbSKawai, Hidehiro 	size_t len;
22713cb4a0bbSKawai, Hidehiro 	int ret;
22723cb4a0bbSKawai, Hidehiro 
22733cb4a0bbSKawai, Hidehiro 	if (!task)
22743cb4a0bbSKawai, Hidehiro 		return -ESRCH;
22753cb4a0bbSKawai, Hidehiro 
22763cb4a0bbSKawai, Hidehiro 	ret = 0;
22773cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
22783cb4a0bbSKawai, Hidehiro 	if (mm) {
22793cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
22803cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
22813cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
22823cb4a0bbSKawai, Hidehiro 		mmput(mm);
22833cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
22843cb4a0bbSKawai, Hidehiro 	}
22853cb4a0bbSKawai, Hidehiro 
22863cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
22873cb4a0bbSKawai, Hidehiro 
22883cb4a0bbSKawai, Hidehiro 	return ret;
22893cb4a0bbSKawai, Hidehiro }
22903cb4a0bbSKawai, Hidehiro 
22913cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
22923cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
22933cb4a0bbSKawai, Hidehiro 					  size_t count,
22943cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
22953cb4a0bbSKawai, Hidehiro {
22963cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
22973cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
22983cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
22993cb4a0bbSKawai, Hidehiro 	unsigned int val;
23003cb4a0bbSKawai, Hidehiro 	int ret;
23013cb4a0bbSKawai, Hidehiro 	int i;
23023cb4a0bbSKawai, Hidehiro 	unsigned long mask;
23033cb4a0bbSKawai, Hidehiro 
23043cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
23053cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
23063cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
23073cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
23083cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
23093cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23103cb4a0bbSKawai, Hidehiro 
23113cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
23123cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
23133cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
23143cb4a0bbSKawai, Hidehiro 		end++;
23153cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
23163cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23173cb4a0bbSKawai, Hidehiro 
23183cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
23193cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
23203cb4a0bbSKawai, Hidehiro 	if (!task)
23213cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23223cb4a0bbSKawai, Hidehiro 
23233cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
23243cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
23253cb4a0bbSKawai, Hidehiro 	if (!mm)
23263cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
23273cb4a0bbSKawai, Hidehiro 
23283cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
23293cb4a0bbSKawai, Hidehiro 		if (val & mask)
23303cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23313cb4a0bbSKawai, Hidehiro 		else
23323cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
23333cb4a0bbSKawai, Hidehiro 	}
23343cb4a0bbSKawai, Hidehiro 
23353cb4a0bbSKawai, Hidehiro 	mmput(mm);
23363cb4a0bbSKawai, Hidehiro  out_no_mm:
23373cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
23383cb4a0bbSKawai, Hidehiro  out_no_task:
23393cb4a0bbSKawai, Hidehiro 	return ret;
23403cb4a0bbSKawai, Hidehiro }
23413cb4a0bbSKawai, Hidehiro 
23423cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
23433cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
23443cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
234587df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
23463cb4a0bbSKawai, Hidehiro };
23473cb4a0bbSKawai, Hidehiro #endif
23483cb4a0bbSKawai, Hidehiro 
23491da177e4SLinus Torvalds /*
23501da177e4SLinus Torvalds  * /proc/self:
23511da177e4SLinus Torvalds  */
23521da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
23531da177e4SLinus Torvalds 			      int buflen)
23541da177e4SLinus Torvalds {
2355488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2356b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
23578578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2358b55fcb22SAndrew Morton 	if (!tgid)
2359488e5bc4SEric W. Biederman 		return -ENOENT;
2360b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
23611da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
23621da177e4SLinus Torvalds }
23631da177e4SLinus Torvalds 
2364008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
23651da177e4SLinus Torvalds {
2366488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2367b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
23687fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
23697fee4868SAl Viro 	if (tgid) {
23707fee4868SAl Viro 		name = __getname();
23717fee4868SAl Viro 		if (!name)
23727fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
23737fee4868SAl Viro 		else
23747fee4868SAl Viro 			sprintf(name, "%d", tgid);
23757fee4868SAl Viro 	}
23767fee4868SAl Viro 	nd_set_link(nd, name);
23777fee4868SAl Viro 	return NULL;
23787fee4868SAl Viro }
23797fee4868SAl Viro 
23807fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
23817fee4868SAl Viro 				void *cookie)
23827fee4868SAl Viro {
23837fee4868SAl Viro 	char *s = nd_get_link(nd);
23847fee4868SAl Viro 	if (!IS_ERR(s))
23857fee4868SAl Viro 		__putname(s);
23861da177e4SLinus Torvalds }
23871da177e4SLinus Torvalds 
2388c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
23891da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
23901da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
23917fee4868SAl Viro 	.put_link	= proc_self_put_link,
23921da177e4SLinus Torvalds };
23931da177e4SLinus Torvalds 
239428a6d671SEric W. Biederman /*
2395801199ceSEric W. Biederman  * proc base
2396801199ceSEric W. Biederman  *
2397801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2398801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2399801199ceSEric W. Biederman  * describe something that is process related.
2400801199ceSEric W. Biederman  */
2401c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
240261a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2403801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2404801199ceSEric W. Biederman };
2405801199ceSEric W. Biederman 
2406801199ceSEric W. Biederman /*
2407801199ceSEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
2408801199ceSEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
2409801199ceSEric W. Biederman  * due to the way we treat inodes.
2410801199ceSEric W. Biederman  */
2411801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2412801199ceSEric W. Biederman {
2413801199ceSEric W. Biederman 	struct inode *inode = dentry->d_inode;
2414801199ceSEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2415801199ceSEric W. Biederman 	if (task) {
2416801199ceSEric W. Biederman 		put_task_struct(task);
2417801199ceSEric W. Biederman 		return 1;
2418801199ceSEric W. Biederman 	}
2419801199ceSEric W. Biederman 	d_drop(dentry);
2420801199ceSEric W. Biederman 	return 0;
2421801199ceSEric W. Biederman }
2422801199ceSEric W. Biederman 
2423d72f71ebSAl Viro static const struct dentry_operations proc_base_dentry_operations =
2424801199ceSEric W. Biederman {
2425801199ceSEric W. Biederman 	.d_revalidate	= proc_base_revalidate,
2426801199ceSEric W. Biederman 	.d_delete	= pid_delete_dentry,
2427801199ceSEric W. Biederman };
2428801199ceSEric W. Biederman 
2429444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2430c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2431801199ceSEric W. Biederman {
2432c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2433801199ceSEric W. Biederman 	struct inode *inode;
2434801199ceSEric W. Biederman 	struct proc_inode *ei;
2435*73d36460SDan Carpenter 	struct dentry *error;
2436801199ceSEric W. Biederman 
2437801199ceSEric W. Biederman 	/* Allocate the inode */
2438801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2439801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2440801199ceSEric W. Biederman 	if (!inode)
2441801199ceSEric W. Biederman 		goto out;
2442801199ceSEric W. Biederman 
2443801199ceSEric W. Biederman 	/* Initialize the inode */
2444801199ceSEric W. Biederman 	ei = PROC_I(inode);
2445801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2446801199ceSEric W. Biederman 
2447801199ceSEric W. Biederman 	/*
2448801199ceSEric W. Biederman 	 * grab the reference to the task.
2449801199ceSEric W. Biederman 	 */
24501a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2451801199ceSEric W. Biederman 	if (!ei->pid)
2452801199ceSEric W. Biederman 		goto out_iput;
2453801199ceSEric W. Biederman 
2454801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2455801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2456801199ceSEric W. Biederman 		inode->i_nlink = 2;
2457801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2458801199ceSEric W. Biederman 		inode->i_size = 64;
2459801199ceSEric W. Biederman 	if (p->iop)
2460801199ceSEric W. Biederman 		inode->i_op = p->iop;
2461801199ceSEric W. Biederman 	if (p->fop)
2462801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2463801199ceSEric W. Biederman 	ei->op = p->op;
2464801199ceSEric W. Biederman 	dentry->d_op = &proc_base_dentry_operations;
2465801199ceSEric W. Biederman 	d_add(dentry, inode);
2466801199ceSEric W. Biederman 	error = NULL;
2467801199ceSEric W. Biederman out:
2468801199ceSEric W. Biederman 	return error;
2469801199ceSEric W. Biederman out_iput:
2470801199ceSEric W. Biederman 	iput(inode);
2471801199ceSEric W. Biederman 	goto out;
2472801199ceSEric W. Biederman }
2473801199ceSEric W. Biederman 
2474444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2475444ceed8SEric W. Biederman {
2476444ceed8SEric W. Biederman 	struct dentry *error;
2477444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2478c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2479444ceed8SEric W. Biederman 
2480444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2481444ceed8SEric W. Biederman 
2482444ceed8SEric W. Biederman 	if (!task)
2483444ceed8SEric W. Biederman 		goto out_no_task;
2484444ceed8SEric W. Biederman 
2485444ceed8SEric W. Biederman 	/* Lookup the directory entry */
24867bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
24877bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2488444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2489444ceed8SEric W. Biederman 			continue;
2490444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2491444ceed8SEric W. Biederman 			break;
2492444ceed8SEric W. Biederman 	}
24937bcd6b0eSEric W. Biederman 	if (p > last)
2494444ceed8SEric W. Biederman 		goto out;
2495444ceed8SEric W. Biederman 
2496444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2497444ceed8SEric W. Biederman 
2498444ceed8SEric W. Biederman out:
2499444ceed8SEric W. Biederman 	put_task_struct(task);
2500444ceed8SEric W. Biederman out_no_task:
2501444ceed8SEric W. Biederman 	return error;
2502444ceed8SEric W. Biederman }
2503444ceed8SEric W. Biederman 
2504c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2505c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
250661a28784SEric W. Biederman {
250761a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
250861a28784SEric W. Biederman 				proc_base_instantiate, task, p);
250961a28784SEric W. Biederman }
251061a28784SEric W. Biederman 
2511aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2512297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2513aba76fdbSAndrew Morton {
2514940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2515297c5d92SAndrea Righi 	unsigned long flags;
2516297c5d92SAndrea Righi 
25175995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2518b2d002dbSAndrea Righi 		struct task_struct *t = task;
2519297c5d92SAndrea Righi 
25205995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
25215995477aSAndrea Righi 		while_each_thread(task, t)
25225995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2523297c5d92SAndrea Righi 
2524297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2525297c5d92SAndrea Righi 	}
2526aba76fdbSAndrew Morton 	return sprintf(buffer,
2527aba76fdbSAndrew Morton 			"rchar: %llu\n"
2528aba76fdbSAndrew Morton 			"wchar: %llu\n"
2529aba76fdbSAndrew Morton 			"syscr: %llu\n"
2530aba76fdbSAndrew Morton 			"syscw: %llu\n"
2531aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2532aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2533aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
25347c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
25357c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
25367c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
25377c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
25387c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
25397c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
25407c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2541aba76fdbSAndrew Morton }
2542297c5d92SAndrea Righi 
2543297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2544297c5d92SAndrea Righi {
2545297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2546297c5d92SAndrea Righi }
2547297c5d92SAndrea Righi 
2548297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2549297c5d92SAndrea Righi {
2550297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2551297c5d92SAndrea Righi }
2552297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2553aba76fdbSAndrew Morton 
255447830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
255547830723SKees Cook 				struct pid *pid, struct task_struct *task)
255647830723SKees Cook {
255747830723SKees Cook 	seq_printf(m, "%08x\n", task->personality);
255847830723SKees Cook 	return 0;
255947830723SKees Cook }
256047830723SKees Cook 
2561801199ceSEric W. Biederman /*
256228a6d671SEric W. Biederman  * Thread groups
256328a6d671SEric W. Biederman  */
256400977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2565c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
256620cdc894SEric W. Biederman 
2567c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2568631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2569631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2570631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2571b2211a36SAndrew Morton #ifdef CONFIG_NET
2572631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2573b2211a36SAndrew Morton #endif
2574631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2575631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
2576631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
2577631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
2578631f9c18SAlexey Dobriyan 	INF("limits",	  S_IRUSR, proc_pid_limits),
257943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2580631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
258143ae34cbSIngo Molnar #endif
25824614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2583ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2584631f9c18SAlexey Dobriyan 	INF("syscall",    S_IRUSR, proc_pid_syscall),
2585ebcb6734SRoland McGrath #endif
2586631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2587631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2588631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2589631f9c18SAlexey Dobriyan 	REG("maps",       S_IRUGO, proc_maps_operations),
259028a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2591631f9c18SAlexey Dobriyan 	REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
259228a6d671SEric W. Biederman #endif
2593631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2594631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2595631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2596631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2597631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2598631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2599631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
26001e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2601631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2602631f9c18SAlexey Dobriyan 	REG("smaps",      S_IRUGO, proc_smaps_operations),
2603631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
260428a6d671SEric W. Biederman #endif
260528a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2606631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
260728a6d671SEric W. Biederman #endif
260828a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2609631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
261028a6d671SEric W. Biederman #endif
26112ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
26122ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
261328a6d671SEric W. Biederman #endif
261428a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2615631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
261628a6d671SEric W. Biederman #endif
26179745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2618631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
26199745512cSArjan van de Ven #endif
26208793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2621631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
262228a6d671SEric W. Biederman #endif
2623a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2624631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2625a424316cSPaul Menage #endif
2626631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
2627631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
262828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2629631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2630631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
263128a6d671SEric W. Biederman #endif
2632f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2633631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2634f4f154fdSAkinobu Mita #endif
2635698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2636631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
26373cb4a0bbSKawai, Hidehiro #endif
2638aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2639631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tgid_io_accounting),
2640aba76fdbSAndrew Morton #endif
264128a6d671SEric W. Biederman };
264228a6d671SEric W. Biederman 
264328a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
264428a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
264528a6d671SEric W. Biederman {
264628a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
264728a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
264828a6d671SEric W. Biederman }
264928a6d671SEric W. Biederman 
265000977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
265128a6d671SEric W. Biederman 	.read		= generic_read_dir,
265228a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
265328a6d671SEric W. Biederman };
265428a6d671SEric W. Biederman 
265528a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
26567bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26577bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
265828a6d671SEric W. Biederman }
265928a6d671SEric W. Biederman 
2660c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
266128a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
266228a6d671SEric W. Biederman 	.getattr	= pid_getattr,
266328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
266428a6d671SEric W. Biederman };
266528a6d671SEric W. Biederman 
266660347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
26671da177e4SLinus Torvalds {
266848e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
26698578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
267048e6484dSEric W. Biederman 	struct qstr name;
26711da177e4SLinus Torvalds 
267248e6484dSEric W. Biederman 	name.name = buf;
267360347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
267460347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
267548e6484dSEric W. Biederman 	if (dentry) {
267648e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
267748e6484dSEric W. Biederman 		d_drop(dentry);
267848e6484dSEric W. Biederman 		dput(dentry);
26791da177e4SLinus Torvalds 	}
26801da177e4SLinus Torvalds 
268148e6484dSEric W. Biederman 	name.name = buf;
268260347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
268360347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
268448e6484dSEric W. Biederman 	if (!leader)
268548e6484dSEric W. Biederman 		goto out;
268648e6484dSEric W. Biederman 
268748e6484dSEric W. Biederman 	name.name = "task";
268848e6484dSEric W. Biederman 	name.len = strlen(name.name);
268948e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
269048e6484dSEric W. Biederman 	if (!dir)
269148e6484dSEric W. Biederman 		goto out_put_leader;
269248e6484dSEric W. Biederman 
269348e6484dSEric W. Biederman 	name.name = buf;
269460347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
269548e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
269648e6484dSEric W. Biederman 	if (dentry) {
269748e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
269848e6484dSEric W. Biederman 		d_drop(dentry);
269948e6484dSEric W. Biederman 		dput(dentry);
27001da177e4SLinus Torvalds 	}
270148e6484dSEric W. Biederman 
270248e6484dSEric W. Biederman 	dput(dir);
270348e6484dSEric W. Biederman out_put_leader:
270448e6484dSEric W. Biederman 	dput(leader);
270548e6484dSEric W. Biederman out:
270648e6484dSEric W. Biederman 	return;
27071da177e4SLinus Torvalds }
27081da177e4SLinus Torvalds 
27090895e91dSRandy Dunlap /**
27100895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
27110895e91dSRandy Dunlap  * @task: task that should be flushed.
27120895e91dSRandy Dunlap  *
27130895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
271460347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
27150895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
27160895e91dSRandy Dunlap  *
27170895e91dSRandy Dunlap  * Looks in the dcache for
27180895e91dSRandy Dunlap  * /proc/@pid
27190895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
27200895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
27210895e91dSRandy Dunlap  * from the dcache.
27220895e91dSRandy Dunlap  *
27230895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
27240895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
27250895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
27260895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
27270895e91dSRandy Dunlap  * dcache entries at process exit time.
27280895e91dSRandy Dunlap  *
27290895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
27300895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
27310895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
273260347f67SPavel Emelyanov  */
273360347f67SPavel Emelyanov 
273460347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
273560347f67SPavel Emelyanov {
27369fcc2d15SEric W. Biederman 	int i;
27379b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2738130f77ecSPavel Emelyanov 	struct upid *upid;
2739130f77ecSPavel Emelyanov 
2740130f77ecSPavel Emelyanov 	pid = task_pid(task);
2741130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
27429fcc2d15SEric W. Biederman 
27439fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2744130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2745130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
27469b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2747130f77ecSPavel Emelyanov 	}
27486f4e6433SPavel Emelyanov 
27496f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
27506f4e6433SPavel Emelyanov 	if (upid->nr == 1)
27516f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
275260347f67SPavel Emelyanov }
275360347f67SPavel Emelyanov 
27549711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
27559711ef99SAdrian Bunk 					   struct dentry * dentry,
2756c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2757444ceed8SEric W. Biederman {
2758444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2759444ceed8SEric W. Biederman 	struct inode *inode;
2760444ceed8SEric W. Biederman 
276161a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2762444ceed8SEric W. Biederman 	if (!inode)
2763444ceed8SEric W. Biederman 		goto out;
2764444ceed8SEric W. Biederman 
2765444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2766444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2767444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2768444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2769aed54175SVegard Nossum 
2770aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2771aed54175SVegard Nossum 		ARRAY_SIZE(tgid_base_stuff));
2772444ceed8SEric W. Biederman 
2773444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2774444ceed8SEric W. Biederman 
2775444ceed8SEric W. Biederman 	d_add(dentry, inode);
2776444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2777444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2778444ceed8SEric W. Biederman 		error = NULL;
2779444ceed8SEric W. Biederman out:
2780444ceed8SEric W. Biederman 	return error;
2781444ceed8SEric W. Biederman }
2782444ceed8SEric W. Biederman 
27831da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
27841da177e4SLinus Torvalds {
2785*73d36460SDan Carpenter 	struct dentry *result;
27861da177e4SLinus Torvalds 	struct task_struct *task;
27871da177e4SLinus Torvalds 	unsigned tgid;
2788b488893aSPavel Emelyanov 	struct pid_namespace *ns;
27891da177e4SLinus Torvalds 
2790801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
2791801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2792801199ceSEric W. Biederman 		goto out;
2793801199ceSEric W. Biederman 
27941da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
27951da177e4SLinus Torvalds 	if (tgid == ~0U)
27961da177e4SLinus Torvalds 		goto out;
27971da177e4SLinus Torvalds 
2798b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2799de758734SEric W. Biederman 	rcu_read_lock();
2800b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
28011da177e4SLinus Torvalds 	if (task)
28021da177e4SLinus Torvalds 		get_task_struct(task);
2803de758734SEric W. Biederman 	rcu_read_unlock();
28041da177e4SLinus Torvalds 	if (!task)
28051da177e4SLinus Torvalds 		goto out;
28061da177e4SLinus Torvalds 
2807444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
280848e6484dSEric W. Biederman 	put_task_struct(task);
28091da177e4SLinus Torvalds out:
2810cd6a3ce9SEric W. Biederman 	return result;
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds 
28131da177e4SLinus Torvalds /*
28140804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
28150bc58a91SEric W. Biederman  *
28161da177e4SLinus Torvalds  */
281719fd4bb2SEric W. Biederman struct tgid_iter {
281819fd4bb2SEric W. Biederman 	unsigned int tgid;
28190804ef4bSEric W. Biederman 	struct task_struct *task;
282019fd4bb2SEric W. Biederman };
282119fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
282219fd4bb2SEric W. Biederman {
28230804ef4bSEric W. Biederman 	struct pid *pid;
28241da177e4SLinus Torvalds 
282519fd4bb2SEric W. Biederman 	if (iter.task)
282619fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
28270804ef4bSEric W. Biederman 	rcu_read_lock();
28280804ef4bSEric W. Biederman retry:
282919fd4bb2SEric W. Biederman 	iter.task = NULL;
283019fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
28310804ef4bSEric W. Biederman 	if (pid) {
283219fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
283319fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
28340804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
28350804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
28360804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
28370804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
28380804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
28390804ef4bSEric W. Biederman 		 *
28400804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
28410804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
28420804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
28430804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
28440804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
28450bc58a91SEric W. Biederman 		 */
284619fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
284719fd4bb2SEric W. Biederman 			iter.tgid += 1;
28480804ef4bSEric W. Biederman 			goto retry;
284919fd4bb2SEric W. Biederman 		}
285019fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
28511da177e4SLinus Torvalds 	}
2852454cc105SEric W. Biederman 	rcu_read_unlock();
285319fd4bb2SEric W. Biederman 	return iter;
28541da177e4SLinus Torvalds }
28551da177e4SLinus Torvalds 
28567bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
28571da177e4SLinus Torvalds 
285861a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
285919fd4bb2SEric W. Biederman 	struct tgid_iter iter)
286061a28784SEric W. Biederman {
286161a28784SEric W. Biederman 	char name[PROC_NUMBUF];
286219fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
286361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
286419fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
286561a28784SEric W. Biederman }
286661a28784SEric W. Biederman 
28671da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
28681da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
28691da177e4SLinus Torvalds {
28701da177e4SLinus Torvalds 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
28712fddfeefSJosef "Jeff" Sipek 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
287219fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2873b488893aSPavel Emelyanov 	struct pid_namespace *ns;
28741da177e4SLinus Torvalds 
287561a28784SEric W. Biederman 	if (!reaper)
287661a28784SEric W. Biederman 		goto out_no_task;
287761a28784SEric W. Biederman 
28787bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2879c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
288061a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2881801199ceSEric W. Biederman 			goto out;
28821da177e4SLinus Torvalds 	}
28831da177e4SLinus Torvalds 
2884b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
288519fd4bb2SEric W. Biederman 	iter.task = NULL;
288619fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
288719fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
288819fd4bb2SEric W. Biederman 	     iter.task;
288919fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
289019fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
289119fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
289219fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
28930804ef4bSEric W. Biederman 			goto out;
28941da177e4SLinus Torvalds 		}
28951da177e4SLinus Torvalds 	}
28960804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
28970804ef4bSEric W. Biederman out:
289861a28784SEric W. Biederman 	put_task_struct(reaper);
289961a28784SEric W. Biederman out_no_task:
29001da177e4SLinus Torvalds 	return 0;
29011da177e4SLinus Torvalds }
29021da177e4SLinus Torvalds 
29030bc58a91SEric W. Biederman /*
290428a6d671SEric W. Biederman  * Tasks
290528a6d671SEric W. Biederman  */
2906c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
2907631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
29083835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2909631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
2910631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
2911631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
2912631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
2913631f9c18SAlexey Dobriyan 	INF("limits",	 S_IRUSR, proc_pid_limits),
291443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2915631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
291643ae34cbSIngo Molnar #endif
29174614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2918ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2919631f9c18SAlexey Dobriyan 	INF("syscall",   S_IRUSR, proc_pid_syscall),
2920ebcb6734SRoland McGrath #endif
2921631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
2922631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
2923631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
2924631f9c18SAlexey Dobriyan 	REG("maps",      S_IRUGO, proc_maps_operations),
292528a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2926631f9c18SAlexey Dobriyan 	REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
292728a6d671SEric W. Biederman #endif
2928631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
2929631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
2930631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
2931631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
2932631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
2933631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
29341e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2935631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2936631f9c18SAlexey Dobriyan 	REG("smaps",     S_IRUGO, proc_smaps_operations),
2937631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
293828a6d671SEric W. Biederman #endif
293928a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2940631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
294128a6d671SEric W. Biederman #endif
294228a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2943631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
294428a6d671SEric W. Biederman #endif
29452ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
29462ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
294728a6d671SEric W. Biederman #endif
294828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2949631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
295028a6d671SEric W. Biederman #endif
29519745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2952631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
29539745512cSArjan van de Ven #endif
29548793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2955631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
295628a6d671SEric W. Biederman #endif
2957a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2958631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2959a424316cSPaul Menage #endif
2960631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
2961631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
296228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2963631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
2964631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUSR, proc_sessionid_operations),
296528a6d671SEric W. Biederman #endif
2966f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2967631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2968f4f154fdSAkinobu Mita #endif
2969297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
2970631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tid_io_accounting),
2971297c5d92SAndrea Righi #endif
297228a6d671SEric W. Biederman };
297328a6d671SEric W. Biederman 
297428a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
297528a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
297628a6d671SEric W. Biederman {
297728a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
297828a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
297928a6d671SEric W. Biederman }
298028a6d671SEric W. Biederman 
298128a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
29827bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
29837bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
298428a6d671SEric W. Biederman }
298528a6d671SEric W. Biederman 
298600977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
298728a6d671SEric W. Biederman 	.read		= generic_read_dir,
298828a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
298928a6d671SEric W. Biederman };
299028a6d671SEric W. Biederman 
2991c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
299228a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
299328a6d671SEric W. Biederman 	.getattr	= pid_getattr,
299428a6d671SEric W. Biederman 	.setattr	= proc_setattr,
299528a6d671SEric W. Biederman };
299628a6d671SEric W. Biederman 
2997444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
2998c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2999444ceed8SEric W. Biederman {
3000444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3001444ceed8SEric W. Biederman 	struct inode *inode;
300261a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3003444ceed8SEric W. Biederman 
3004444ceed8SEric W. Biederman 	if (!inode)
3005444ceed8SEric W. Biederman 		goto out;
3006444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3007444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3008444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3009444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3010aed54175SVegard Nossum 
3011aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3012aed54175SVegard Nossum 		ARRAY_SIZE(tid_base_stuff));
3013444ceed8SEric W. Biederman 
3014444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
3015444ceed8SEric W. Biederman 
3016444ceed8SEric W. Biederman 	d_add(dentry, inode);
3017444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3018444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3019444ceed8SEric W. Biederman 		error = NULL;
3020444ceed8SEric W. Biederman out:
3021444ceed8SEric W. Biederman 	return error;
3022444ceed8SEric W. Biederman }
3023444ceed8SEric W. Biederman 
302428a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
302528a6d671SEric W. Biederman {
302628a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
302728a6d671SEric W. Biederman 	struct task_struct *task;
302828a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
302928a6d671SEric W. Biederman 	unsigned tid;
3030b488893aSPavel Emelyanov 	struct pid_namespace *ns;
303128a6d671SEric W. Biederman 
303228a6d671SEric W. Biederman 	if (!leader)
303328a6d671SEric W. Biederman 		goto out_no_task;
303428a6d671SEric W. Biederman 
303528a6d671SEric W. Biederman 	tid = name_to_int(dentry);
303628a6d671SEric W. Biederman 	if (tid == ~0U)
303728a6d671SEric W. Biederman 		goto out;
303828a6d671SEric W. Biederman 
3039b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
304028a6d671SEric W. Biederman 	rcu_read_lock();
3041b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
304228a6d671SEric W. Biederman 	if (task)
304328a6d671SEric W. Biederman 		get_task_struct(task);
304428a6d671SEric W. Biederman 	rcu_read_unlock();
304528a6d671SEric W. Biederman 	if (!task)
304628a6d671SEric W. Biederman 		goto out;
3047bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
304828a6d671SEric W. Biederman 		goto out_drop_task;
304928a6d671SEric W. Biederman 
3050444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
305128a6d671SEric W. Biederman out_drop_task:
305228a6d671SEric W. Biederman 	put_task_struct(task);
305328a6d671SEric W. Biederman out:
305428a6d671SEric W. Biederman 	put_task_struct(leader);
305528a6d671SEric W. Biederman out_no_task:
305628a6d671SEric W. Biederman 	return result;
305728a6d671SEric W. Biederman }
305828a6d671SEric W. Biederman 
305928a6d671SEric W. Biederman /*
30600bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
30610bc58a91SEric W. Biederman  *
30620bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
30630bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
30640bc58a91SEric W. Biederman  * directory we have more work todo.
30650bc58a91SEric W. Biederman  *
30660bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
30670bc58a91SEric W. Biederman  *
30680bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
30690bc58a91SEric W. Biederman  * threads past it.
30700bc58a91SEric W. Biederman  */
3071cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3072b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
30730bc58a91SEric W. Biederman {
3074a872ff0cSOleg Nesterov 	struct task_struct *pos;
30750bc58a91SEric W. Biederman 
3076cc288738SEric W. Biederman 	rcu_read_lock();
30770bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
30780bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3079b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3080a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3081a872ff0cSOleg Nesterov 			goto found;
30820bc58a91SEric W. Biederman 	}
30830bc58a91SEric W. Biederman 
30840bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
30850bc58a91SEric W. Biederman 	pos = NULL;
3086a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3087a872ff0cSOleg Nesterov 		goto out;
3088a872ff0cSOleg Nesterov 
3089a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3090a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3091a872ff0cSOleg Nesterov 	 */
3092a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3093a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3094a872ff0cSOleg Nesterov 		if (pos == leader) {
3095a872ff0cSOleg Nesterov 			pos = NULL;
3096a872ff0cSOleg Nesterov 			goto out;
3097a872ff0cSOleg Nesterov 		}
3098a872ff0cSOleg Nesterov 	}
3099a872ff0cSOleg Nesterov found:
3100a872ff0cSOleg Nesterov 	get_task_struct(pos);
3101a872ff0cSOleg Nesterov out:
3102cc288738SEric W. Biederman 	rcu_read_unlock();
31030bc58a91SEric W. Biederman 	return pos;
31040bc58a91SEric W. Biederman }
31050bc58a91SEric W. Biederman 
31060bc58a91SEric W. Biederman /*
31070bc58a91SEric W. Biederman  * Find the next thread in the thread list.
31080bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
31090bc58a91SEric W. Biederman  *
31100bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
31110bc58a91SEric W. Biederman  */
31120bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
31130bc58a91SEric W. Biederman {
3114c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3115cc288738SEric W. Biederman 	rcu_read_lock();
3116c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
31170bc58a91SEric W. Biederman 		pos = next_thread(start);
3118c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
31190bc58a91SEric W. Biederman 			pos = NULL;
3120c1df7fb8SOleg Nesterov 		else
3121c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3122c1df7fb8SOleg Nesterov 	}
3123cc288738SEric W. Biederman 	rcu_read_unlock();
31240bc58a91SEric W. Biederman 	put_task_struct(start);
31250bc58a91SEric W. Biederman 	return pos;
31260bc58a91SEric W. Biederman }
31270bc58a91SEric W. Biederman 
312861a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
312961a28784SEric W. Biederman 	struct task_struct *task, int tid)
313061a28784SEric W. Biederman {
313161a28784SEric W. Biederman 	char name[PROC_NUMBUF];
313261a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
313361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
313461a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
313561a28784SEric W. Biederman }
313661a28784SEric W. Biederman 
31371da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
31381da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
31391da177e4SLinus Torvalds {
31402fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
31411da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
31427d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
31430bc58a91SEric W. Biederman 	struct task_struct *task;
31441da177e4SLinus Torvalds 	int retval = -ENOENT;
31451da177e4SLinus Torvalds 	ino_t ino;
31460bc58a91SEric W. Biederman 	int tid;
3147b488893aSPavel Emelyanov 	struct pid_namespace *ns;
31481da177e4SLinus Torvalds 
31497d895244SGuillaume Chazarain 	task = get_proc_task(inode);
31507d895244SGuillaume Chazarain 	if (!task)
31517d895244SGuillaume Chazarain 		goto out_no_task;
31527d895244SGuillaume Chazarain 	rcu_read_lock();
31537d895244SGuillaume Chazarain 	if (pid_alive(task)) {
31547d895244SGuillaume Chazarain 		leader = task->group_leader;
31557d895244SGuillaume Chazarain 		get_task_struct(leader);
31567d895244SGuillaume Chazarain 	}
31577d895244SGuillaume Chazarain 	rcu_read_unlock();
31587d895244SGuillaume Chazarain 	put_task_struct(task);
315999f89551SEric W. Biederman 	if (!leader)
316099f89551SEric W. Biederman 		goto out_no_task;
31611da177e4SLinus Torvalds 	retval = 0;
31621da177e4SLinus Torvalds 
3163ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
31641da177e4SLinus Torvalds 	case 0:
31651da177e4SLinus Torvalds 		ino = inode->i_ino;
3166ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
31671da177e4SLinus Torvalds 			goto out;
3168ee6f779bSZhang Le 		filp->f_pos++;
31691da177e4SLinus Torvalds 		/* fall through */
31701da177e4SLinus Torvalds 	case 1:
31711da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3172ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
31731da177e4SLinus Torvalds 			goto out;
3174ee6f779bSZhang Le 		filp->f_pos++;
31751da177e4SLinus Torvalds 		/* fall through */
31761da177e4SLinus Torvalds 	}
31771da177e4SLinus Torvalds 
31780bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
31790bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
31800bc58a91SEric W. Biederman 	 */
3181b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
31822b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
31830bc58a91SEric W. Biederman 	filp->f_version = 0;
3184ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
31850bc58a91SEric W. Biederman 	     task;
3186ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3187b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
318861a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
31890bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
31900bc58a91SEric W. Biederman 			 * pid for the next readir call */
31912b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
31920bc58a91SEric W. Biederman 			put_task_struct(task);
31931da177e4SLinus Torvalds 			break;
31940bc58a91SEric W. Biederman 		}
31951da177e4SLinus Torvalds 	}
31961da177e4SLinus Torvalds out:
319799f89551SEric W. Biederman 	put_task_struct(leader);
319899f89551SEric W. Biederman out_no_task:
31991da177e4SLinus Torvalds 	return retval;
32001da177e4SLinus Torvalds }
32016e66b52bSEric W. Biederman 
32026e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
32036e66b52bSEric W. Biederman {
32046e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
320599f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
32066e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
32076e66b52bSEric W. Biederman 
320899f89551SEric W. Biederman 	if (p) {
320999f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
321099f89551SEric W. Biederman 		put_task_struct(p);
32116e66b52bSEric W. Biederman 	}
32126e66b52bSEric W. Biederman 
32136e66b52bSEric W. Biederman 	return 0;
32146e66b52bSEric W. Biederman }
321528a6d671SEric W. Biederman 
3216c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
321728a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
321828a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
321928a6d671SEric W. Biederman 	.setattr	= proc_setattr,
322028a6d671SEric W. Biederman };
322128a6d671SEric W. Biederman 
322200977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
322328a6d671SEric W. Biederman 	.read		= generic_read_dir,
322428a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
322528a6d671SEric W. Biederman };
3226