xref: /openbmc/linux/fs/proc/base.c (revision a63d83f427fbce97a6cea0db2e64b0eb8435cd10)
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>
66*a63d83f4SDavid Rientjes #include <linux/swap.h>
67b835996fSDipankar Sarma #include <linux/rcupdate.h>
681da177e4SLinus Torvalds #include <linux/kallsyms.h>
692ec220e2SKen Chen #include <linux/stacktrace.h>
70d85f50d5SNeil Horman #include <linux/resource.h>
715096add8SKees Cook #include <linux/module.h>
721da177e4SLinus Torvalds #include <linux/mount.h>
731da177e4SLinus Torvalds #include <linux/security.h>
741da177e4SLinus Torvalds #include <linux/ptrace.h>
750d094efeSRoland McGrath #include <linux/tracehook.h>
76a424316cSPaul Menage #include <linux/cgroup.h>
771da177e4SLinus Torvalds #include <linux/cpuset.h>
781da177e4SLinus Torvalds #include <linux/audit.h>
795addc5ddSAl Viro #include <linux/poll.h>
801651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
818ac773b4SAlexey Dobriyan #include <linux/oom.h>
823cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
8360347f67SPavel Emelyanov #include <linux/pid_namespace.h>
845ad4e53bSAl Viro #include <linux/fs_struct.h>
855a0e3ad6STejun Heo #include <linux/slab.h>
861da177e4SLinus Torvalds #include "internal.h"
871da177e4SLinus Torvalds 
880f2fe20fSEric W. Biederman /* NOTE:
890f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
900f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
910f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
920f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
930f2fe20fSEric W. Biederman  *
940f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
950f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
960f2fe20fSEric W. Biederman  */
970f2fe20fSEric W. Biederman 
981da177e4SLinus Torvalds struct pid_entry {
991da177e4SLinus Torvalds 	char *name;
100c5141e6dSEric Dumazet 	int len;
1011da177e4SLinus Torvalds 	mode_t mode;
102c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
10300977a59SArjan van de Ven 	const struct file_operations *fop;
10420cdc894SEric W. Biederman 	union proc_op op;
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
10761a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
10820cdc894SEric W. Biederman 	.name = (NAME),					\
109c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
11020cdc894SEric W. Biederman 	.mode = MODE,					\
11120cdc894SEric W. Biederman 	.iop  = IOP,					\
11220cdc894SEric W. Biederman 	.fop  = FOP,					\
11320cdc894SEric W. Biederman 	.op   = OP,					\
11420cdc894SEric W. Biederman }
11520cdc894SEric W. Biederman 
116631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops)	\
117631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
118631f9c18SAlexey Dobriyan #define LNK(NAME, get_link)					\
11961a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
12020cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
121631f9c18SAlexey Dobriyan 		{ .proc_get_link = get_link } )
122631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops)				\
123631f9c18SAlexey Dobriyan 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
124631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read)				\
12561a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
12620cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
127631f9c18SAlexey Dobriyan 		{ .proc_read = read } )
128631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show)				\
129be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
130be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
131631f9c18SAlexey Dobriyan 		{ .proc_show = show } )
1321da177e4SLinus Torvalds 
133aed54175SVegard Nossum /*
134aed54175SVegard Nossum  * Count the number of hardlinks for the pid_entry table, excluding the .
135aed54175SVegard Nossum  * and .. links.
136aed54175SVegard Nossum  */
137aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
138aed54175SVegard Nossum 	unsigned int n)
139aed54175SVegard Nossum {
140aed54175SVegard Nossum 	unsigned int i;
141aed54175SVegard Nossum 	unsigned int count;
142aed54175SVegard Nossum 
143aed54175SVegard Nossum 	count = 0;
144aed54175SVegard Nossum 	for (i = 0; i < n; ++i) {
145aed54175SVegard Nossum 		if (S_ISDIR(entries[i].mode))
146aed54175SVegard Nossum 			++count;
147aed54175SVegard Nossum 	}
148aed54175SVegard Nossum 
149aed54175SVegard Nossum 	return count;
150aed54175SVegard Nossum }
151aed54175SVegard Nossum 
1527c2c7d99SHugh Dickins static int get_fs_path(struct task_struct *task, struct path *path, bool root)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	struct fs_struct *fs;
1557c2c7d99SHugh Dickins 	int result = -ENOENT;
1567c2c7d99SHugh Dickins 
1570494f6ecSMiklos Szeredi 	task_lock(task);
1580494f6ecSMiklos Szeredi 	fs = task->fs;
1597c2c7d99SHugh Dickins 	if (fs) {
1607c2c7d99SHugh Dickins 		read_lock(&fs->lock);
1617c2c7d99SHugh Dickins 		*path = root ? fs->root : fs->pwd;
1627c2c7d99SHugh Dickins 		path_get(path);
1637c2c7d99SHugh Dickins 		read_unlock(&fs->lock);
1647c2c7d99SHugh Dickins 		result = 0;
1657c2c7d99SHugh Dickins 	}
1660494f6ecSMiklos Szeredi 	task_unlock(task);
1677c2c7d99SHugh Dickins 	return result;
1680494f6ecSMiklos Szeredi }
1690494f6ecSMiklos Szeredi 
1703dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path)
1710494f6ecSMiklos Szeredi {
17299f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1730494f6ecSMiklos Szeredi 	int result = -ENOENT;
17499f89551SEric W. Biederman 
17599f89551SEric W. Biederman 	if (task) {
1767c2c7d99SHugh Dickins 		result = get_fs_path(task, path, 0);
17799f89551SEric W. Biederman 		put_task_struct(task);
17899f89551SEric W. Biederman 	}
1791da177e4SLinus Torvalds 	return result;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1823dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path)
1831da177e4SLinus Torvalds {
18499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1851da177e4SLinus Torvalds 	int result = -ENOENT;
18699f89551SEric W. Biederman 
18799f89551SEric W. Biederman 	if (task) {
1887c2c7d99SHugh Dickins 		result = get_fs_path(task, path, 1);
18999f89551SEric W. Biederman 		put_task_struct(task);
19099f89551SEric W. Biederman 	}
1911da177e4SLinus Torvalds 	return result;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
194638fa202SRoland McGrath /*
195638fa202SRoland McGrath  * Return zero if current may access user memory in @task, -error if not.
196638fa202SRoland McGrath  */
197638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task)
198638fa202SRoland McGrath {
199638fa202SRoland McGrath 	/*
200638fa202SRoland McGrath 	 * A task can always look at itself, in case it chooses
201638fa202SRoland McGrath 	 * to use system calls instead of load instructions.
202638fa202SRoland McGrath 	 */
203638fa202SRoland McGrath 	if (task == current)
204638fa202SRoland McGrath 		return 0;
205638fa202SRoland McGrath 
206638fa202SRoland McGrath 	/*
207638fa202SRoland McGrath 	 * If current is actively ptrace'ing, and would also be
208638fa202SRoland McGrath 	 * permitted to freshly attach with ptrace now, permit it.
209638fa202SRoland McGrath 	 */
2100d094efeSRoland McGrath 	if (task_is_stopped_or_traced(task)) {
2110d094efeSRoland McGrath 		int match;
2120d094efeSRoland McGrath 		rcu_read_lock();
2130d094efeSRoland McGrath 		match = (tracehook_tracer_task(task) == current);
2140d094efeSRoland McGrath 		rcu_read_unlock();
2150d094efeSRoland McGrath 		if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
216638fa202SRoland McGrath 			return 0;
2170d094efeSRoland McGrath 	}
218638fa202SRoland McGrath 
219638fa202SRoland McGrath 	/*
220638fa202SRoland McGrath 	 * Noone else is allowed.
221638fa202SRoland McGrath 	 */
222638fa202SRoland McGrath 	return -EPERM;
223638fa202SRoland McGrath }
2241da177e4SLinus Torvalds 
225831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task)
226831830b5SAl Viro {
227704b836cSOleg Nesterov 	struct mm_struct *mm;
22800f89d21SOleg Nesterov 
229704b836cSOleg Nesterov 	if (mutex_lock_killable(&task->cred_guard_mutex))
230704b836cSOleg Nesterov 		return NULL;
231704b836cSOleg Nesterov 
232704b836cSOleg Nesterov 	mm = get_task_mm(task);
233704b836cSOleg Nesterov 	if (mm && mm != current->mm &&
234704b836cSOleg Nesterov 			!ptrace_may_access(task, PTRACE_MODE_READ)) {
235831830b5SAl Viro 		mmput(mm);
23600f89d21SOleg Nesterov 		mm = NULL;
237831830b5SAl Viro 	}
238704b836cSOleg Nesterov 	mutex_unlock(&task->cred_guard_mutex);
239704b836cSOleg Nesterov 
24013f0feafSOleg Nesterov 	return mm;
24113f0feafSOleg Nesterov }
242831830b5SAl Viro 
2431da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	int res = 0;
2461da177e4SLinus Torvalds 	unsigned int len;
2471da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2481da177e4SLinus Torvalds 	if (!mm)
2491da177e4SLinus Torvalds 		goto out;
2501da177e4SLinus Torvalds 	if (!mm->arg_end)
2511da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2561da177e4SLinus Torvalds 		len = PAGE_SIZE;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2611da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2621da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2631da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2641da177e4SLinus Torvalds 		if (len < res) {
2651da177e4SLinus Torvalds 		    res = len;
2661da177e4SLinus Torvalds 		} else {
2671da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2681da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2691da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2701da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2711da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2721da177e4SLinus Torvalds 		}
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds out_mm:
2751da177e4SLinus Torvalds 	mmput(mm);
2761da177e4SLinus Torvalds out:
2771da177e4SLinus Torvalds 	return res;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	int res = 0;
2831da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2841da177e4SLinus Torvalds 	if (mm) {
2851da177e4SLinus Torvalds 		unsigned int nwords = 0;
286dfe6b7d9SHannes Eder 		do {
2871da177e4SLinus Torvalds 			nwords += 2;
288dfe6b7d9SHannes Eder 		} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2891da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2901da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2911da177e4SLinus Torvalds 			res = PAGE_SIZE;
2921da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2931da177e4SLinus Torvalds 		mmput(mm);
2941da177e4SLinus Torvalds 	}
2951da177e4SLinus Torvalds 	return res;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3001da177e4SLinus Torvalds /*
3011da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
3021da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
3031da177e4SLinus Torvalds  */
3041da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
3051da177e4SLinus Torvalds {
306ffb45122SAlexey Dobriyan 	unsigned long wchan;
3079281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	wchan = get_wchan(task);
3101da177e4SLinus Torvalds 
3119d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
312f83ce3e6SJake Edge 		if (!ptrace_may_access(task, PTRACE_MODE_READ))
313f83ce3e6SJake Edge 			return 0;
314f83ce3e6SJake Edge 		else
3151da177e4SLinus Torvalds 			return sprintf(buffer, "%lu", wchan);
3169d65cb4aSAlexey Dobriyan 	else
3179d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
3201da177e4SLinus Torvalds 
3212ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
3222ec220e2SKen Chen 
3232ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH	64
3242ec220e2SKen Chen 
3252ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
3262ec220e2SKen Chen 			  struct pid *pid, struct task_struct *task)
3272ec220e2SKen Chen {
3282ec220e2SKen Chen 	struct stack_trace trace;
3292ec220e2SKen Chen 	unsigned long *entries;
3302ec220e2SKen Chen 	int i;
3312ec220e2SKen Chen 
3322ec220e2SKen Chen 	entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
3332ec220e2SKen Chen 	if (!entries)
3342ec220e2SKen Chen 		return -ENOMEM;
3352ec220e2SKen Chen 
3362ec220e2SKen Chen 	trace.nr_entries	= 0;
3372ec220e2SKen Chen 	trace.max_entries	= MAX_STACK_TRACE_DEPTH;
3382ec220e2SKen Chen 	trace.entries		= entries;
3392ec220e2SKen Chen 	trace.skip		= 0;
3402ec220e2SKen Chen 	save_stack_trace_tsk(task, &trace);
3412ec220e2SKen Chen 
3422ec220e2SKen Chen 	for (i = 0; i < trace.nr_entries; i++) {
3432ec220e2SKen Chen 		seq_printf(m, "[<%p>] %pS\n",
3442ec220e2SKen Chen 			   (void *)entries[i], (void *)entries[i]);
3452ec220e2SKen Chen 	}
3462ec220e2SKen Chen 	kfree(entries);
3472ec220e2SKen Chen 
3482ec220e2SKen Chen 	return 0;
3492ec220e2SKen Chen }
3502ec220e2SKen Chen #endif
3512ec220e2SKen Chen 
3521da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3551da177e4SLinus Torvalds  */
3561da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3571da177e4SLinus Torvalds {
358172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
359826e08b0SIngo Molnar 			(unsigned long long)task->se.sum_exec_runtime,
360826e08b0SIngo Molnar 			(unsigned long long)task->sched_info.run_delay,
3612d72376bSIngo Molnar 			task->sched_info.pcount);
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds #endif
3641da177e4SLinus Torvalds 
3659745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3669745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3679745512cSArjan van de Ven {
3689745512cSArjan van de Ven 	int i;
36913d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
37013d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3719745512cSArjan van de Ven 
37213d77c37SHiroshi Shimamoto 	if (!task)
37313d77c37SHiroshi Shimamoto 		return -ESRCH;
37413d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3759745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
3769745512cSArjan van de Ven 		if (task->latency_record[i].backtrace[0]) {
3779745512cSArjan van de Ven 			int q;
3789745512cSArjan van de Ven 			seq_printf(m, "%i %li %li ",
3799745512cSArjan van de Ven 				task->latency_record[i].count,
3809745512cSArjan van de Ven 				task->latency_record[i].time,
3819745512cSArjan van de Ven 				task->latency_record[i].max);
3829745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
3839c246247SHugh Dickins 				char sym[KSYM_SYMBOL_LEN];
3849745512cSArjan van de Ven 				char *c;
3859745512cSArjan van de Ven 				if (!task->latency_record[i].backtrace[q])
3869745512cSArjan van de Ven 					break;
3879745512cSArjan van de Ven 				if (task->latency_record[i].backtrace[q] == ULONG_MAX)
3889745512cSArjan van de Ven 					break;
3899745512cSArjan van de Ven 				sprint_symbol(sym, task->latency_record[i].backtrace[q]);
3909745512cSArjan van de Ven 				c = strchr(sym, '+');
3919745512cSArjan van de Ven 				if (c)
3929745512cSArjan van de Ven 					*c = 0;
3939745512cSArjan van de Ven 				seq_printf(m, "%s ", sym);
3949745512cSArjan van de Ven 			}
3959745512cSArjan van de Ven 			seq_printf(m, "\n");
3969745512cSArjan van de Ven 		}
3979745512cSArjan van de Ven 
3989745512cSArjan van de Ven 	}
39913d77c37SHiroshi Shimamoto 	put_task_struct(task);
4009745512cSArjan van de Ven 	return 0;
4019745512cSArjan van de Ven }
4029745512cSArjan van de Ven 
4039745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
4049745512cSArjan van de Ven {
40513d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
406d6643d12SHiroshi Shimamoto }
407d6643d12SHiroshi Shimamoto 
4089745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
4099745512cSArjan van de Ven 			    size_t count, loff_t *offs)
4109745512cSArjan van de Ven {
41113d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
4129745512cSArjan van de Ven 
41313d77c37SHiroshi Shimamoto 	if (!task)
41413d77c37SHiroshi Shimamoto 		return -ESRCH;
4159745512cSArjan van de Ven 	clear_all_latency_tracing(task);
41613d77c37SHiroshi Shimamoto 	put_task_struct(task);
4179745512cSArjan van de Ven 
4189745512cSArjan van de Ven 	return count;
4199745512cSArjan van de Ven }
4209745512cSArjan van de Ven 
4219745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
4229745512cSArjan van de Ven 	.open		= lstats_open,
4239745512cSArjan van de Ven 	.read		= seq_read,
4249745512cSArjan van de Ven 	.write		= lstats_write,
4259745512cSArjan van de Ven 	.llseek		= seq_lseek,
42613d77c37SHiroshi Shimamoto 	.release	= single_release,
4279745512cSArjan van de Ven };
4289745512cSArjan van de Ven 
4299745512cSArjan van de Ven #endif
4309745512cSArjan van de Ven 
4311da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4321da177e4SLinus Torvalds {
433b95c35e7SOleg Nesterov 	unsigned long points = 0;
4341da177e4SLinus Torvalds 
43519c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
436b95c35e7SOleg Nesterov 	if (pid_alive(task))
437*a63d83f4SDavid Rientjes 		points = oom_badness(task, NULL, NULL,
438*a63d83f4SDavid Rientjes 					totalram_pages + total_swap_pages);
43919c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4401da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
443d85f50d5SNeil Horman struct limit_names {
444d85f50d5SNeil Horman 	char *name;
445d85f50d5SNeil Horman 	char *unit;
446d85f50d5SNeil Horman };
447d85f50d5SNeil Horman 
448d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
449cff4edb5SKees Cook 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
450d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
451d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
452d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
453d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
454d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
455d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
456d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
457d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
458d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
459d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
460d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
461d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
462d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
463d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4648808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
465d85f50d5SNeil Horman };
466d85f50d5SNeil Horman 
467d85f50d5SNeil Horman /* Display limits for a process */
468d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
469d85f50d5SNeil Horman {
470d85f50d5SNeil Horman 	unsigned int i;
471d85f50d5SNeil Horman 	int count = 0;
472d85f50d5SNeil Horman 	unsigned long flags;
473d85f50d5SNeil Horman 	char *bufptr = buffer;
474d85f50d5SNeil Horman 
475d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
476d85f50d5SNeil Horman 
477a6bebbc8SLai Jiangshan 	if (!lock_task_sighand(task, &flags))
478d85f50d5SNeil Horman 		return 0;
479d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
480d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
481d85f50d5SNeil Horman 
482d85f50d5SNeil Horman 	/*
483d85f50d5SNeil Horman 	 * print the file header
484d85f50d5SNeil Horman 	 */
485d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
486d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
487d85f50d5SNeil Horman 
488d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
489d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
490d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
491d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
492d85f50d5SNeil Horman 		else
493d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
494d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
495d85f50d5SNeil Horman 
496d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
497d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
498d85f50d5SNeil Horman 		else
499d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
500d85f50d5SNeil Horman 					 rlim[i].rlim_max);
501d85f50d5SNeil Horman 
502d85f50d5SNeil Horman 		if (lnames[i].unit)
503d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
504d85f50d5SNeil Horman 					 lnames[i].unit);
505d85f50d5SNeil Horman 		else
506d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
507d85f50d5SNeil Horman 	}
508d85f50d5SNeil Horman 
509d85f50d5SNeil Horman 	return count;
510d85f50d5SNeil Horman }
511d85f50d5SNeil Horman 
512ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer)
514ebcb6734SRoland McGrath {
515ebcb6734SRoland McGrath 	long nr;
516ebcb6734SRoland McGrath 	unsigned long args[6], sp, pc;
517ebcb6734SRoland McGrath 
518ebcb6734SRoland McGrath 	if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
519ebcb6734SRoland McGrath 		return sprintf(buffer, "running\n");
520ebcb6734SRoland McGrath 
521ebcb6734SRoland McGrath 	if (nr < 0)
522ebcb6734SRoland McGrath 		return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
523ebcb6734SRoland McGrath 
524ebcb6734SRoland McGrath 	return sprintf(buffer,
525ebcb6734SRoland McGrath 		       "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
526ebcb6734SRoland McGrath 		       nr,
527ebcb6734SRoland McGrath 		       args[0], args[1], args[2], args[3], args[4], args[5],
528ebcb6734SRoland McGrath 		       sp, pc);
529ebcb6734SRoland McGrath }
530ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
531ebcb6734SRoland McGrath 
5321da177e4SLinus Torvalds /************************************************************************/
5331da177e4SLinus Torvalds /*                       Here the fs part begins                        */
5341da177e4SLinus Torvalds /************************************************************************/
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds /* permission checks */
537778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
5381da177e4SLinus Torvalds {
539778c1144SEric W. Biederman 	struct task_struct *task;
540778c1144SEric W. Biederman 	int allowed = 0;
541df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
542df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
543df26c40eSEric W. Biederman 	 * information.
544778c1144SEric W. Biederman 	 */
545778c1144SEric W. Biederman 	task = get_proc_task(inode);
546df26c40eSEric W. Biederman 	if (task) {
547006ebb40SStephen Smalley 		allowed = ptrace_may_access(task, PTRACE_MODE_READ);
548778c1144SEric W. Biederman 		put_task_struct(task);
549df26c40eSEric W. Biederman 	}
550778c1144SEric W. Biederman 	return allowed;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5536d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr)
5546d76fa58SLinus Torvalds {
5556d76fa58SLinus Torvalds 	int error;
5566d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5576d76fa58SLinus Torvalds 
5586d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5596d76fa58SLinus Torvalds 		return -EPERM;
5606d76fa58SLinus Torvalds 
5616d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5626d76fa58SLinus Torvalds 	if (!error)
5636d76fa58SLinus Torvalds 		error = inode_setattr(inode, attr);
5646d76fa58SLinus Torvalds 	return error;
5656d76fa58SLinus Torvalds }
5666d76fa58SLinus Torvalds 
567c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5686d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5696d76fa58SLinus Torvalds };
5706d76fa58SLinus Torvalds 
571a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file,
572a1a2c409SMiklos Szeredi 			      const struct seq_operations *op)
5731da177e4SLinus Torvalds {
57499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
575cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
5766b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
577a1a2c409SMiklos Szeredi 	struct path root;
5785addc5ddSAl Viro 	struct proc_mounts *p;
5795addc5ddSAl Viro 	int ret = -EINVAL;
5805addc5ddSAl Viro 
58199f89551SEric W. Biederman 	if (task) {
582cf7b708cSPavel Emelyanov 		rcu_read_lock();
583cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
584cf7b708cSPavel Emelyanov 		if (nsp) {
585cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
5866b3286edSKirill Korotaev 			if (ns)
5876b3286edSKirill Korotaev 				get_mnt_ns(ns);
588863c4702SAlexey Dobriyan 		}
589cf7b708cSPavel Emelyanov 		rcu_read_unlock();
5907c2c7d99SHugh Dickins 		if (ns && get_fs_path(task, &root, 1) == 0)
5917c2c7d99SHugh Dickins 			ret = 0;
59299f89551SEric W. Biederman 		put_task_struct(task);
59399f89551SEric W. Biederman 	}
5941da177e4SLinus Torvalds 
595a1a2c409SMiklos Szeredi 	if (!ns)
596a1a2c409SMiklos Szeredi 		goto err;
5977c2c7d99SHugh Dickins 	if (ret)
598a1a2c409SMiklos Szeredi 		goto err_put_ns;
599a1a2c409SMiklos Szeredi 
6005addc5ddSAl Viro 	ret = -ENOMEM;
6015addc5ddSAl Viro 	p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
602a1a2c409SMiklos Szeredi 	if (!p)
603a1a2c409SMiklos Szeredi 		goto err_put_path;
604a1a2c409SMiklos Szeredi 
6055addc5ddSAl Viro 	file->private_data = &p->m;
606a1a2c409SMiklos Szeredi 	ret = seq_open(file, op);
607a1a2c409SMiklos Szeredi 	if (ret)
608a1a2c409SMiklos Szeredi 		goto err_free;
609a1a2c409SMiklos Szeredi 
610a1a2c409SMiklos Szeredi 	p->m.private = p;
611a1a2c409SMiklos Szeredi 	p->ns = ns;
612a1a2c409SMiklos Szeredi 	p->root = root;
6136b3286edSKirill Korotaev 	p->event = ns->event;
614a1a2c409SMiklos Szeredi 
6155addc5ddSAl Viro 	return 0;
616a1a2c409SMiklos Szeredi 
617a1a2c409SMiklos Szeredi  err_free:
6185addc5ddSAl Viro 	kfree(p);
619a1a2c409SMiklos Szeredi  err_put_path:
620a1a2c409SMiklos Szeredi 	path_put(&root);
621a1a2c409SMiklos Szeredi  err_put_ns:
6226b3286edSKirill Korotaev 	put_mnt_ns(ns);
623a1a2c409SMiklos Szeredi  err:
6241da177e4SLinus Torvalds 	return ret;
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
6281da177e4SLinus Torvalds {
629a1a2c409SMiklos Szeredi 	struct proc_mounts *p = file->private_data;
630a1a2c409SMiklos Szeredi 	path_put(&p->root);
631a1a2c409SMiklos Szeredi 	put_mnt_ns(p->ns);
6321da177e4SLinus Torvalds 	return seq_release(inode, file);
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6355addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
6365addc5ddSAl Viro {
6375addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
63831b07093SKOSAKI Motohiro 	unsigned res = POLLIN | POLLRDNORM;
6395addc5ddSAl Viro 
6409f5596afSAl Viro 	poll_wait(file, &p->ns->poll, wait);
6419f5596afSAl Viro 	if (mnt_had_events(p))
64231b07093SKOSAKI Motohiro 		res |= POLLERR | POLLPRI;
6435addc5ddSAl Viro 
6445addc5ddSAl Viro 	return res;
6455addc5ddSAl Viro }
6465addc5ddSAl Viro 
647a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file)
648a1a2c409SMiklos Szeredi {
649a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mounts_op);
650a1a2c409SMiklos Szeredi }
651a1a2c409SMiklos Szeredi 
65200977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
6531da177e4SLinus Torvalds 	.open		= mounts_open,
6541da177e4SLinus Torvalds 	.read		= seq_read,
6551da177e4SLinus Torvalds 	.llseek		= seq_lseek,
6561da177e4SLinus Torvalds 	.release	= mounts_release,
6575addc5ddSAl Viro 	.poll		= mounts_poll,
6581da177e4SLinus Torvalds };
6591da177e4SLinus Torvalds 
6602d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file)
6612d4d4864SRam Pai {
6622d4d4864SRam Pai 	return mounts_open_common(inode, file, &mountinfo_op);
6632d4d4864SRam Pai }
6642d4d4864SRam Pai 
6652d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = {
6662d4d4864SRam Pai 	.open		= mountinfo_open,
6672d4d4864SRam Pai 	.read		= seq_read,
6682d4d4864SRam Pai 	.llseek		= seq_lseek,
6692d4d4864SRam Pai 	.release	= mounts_release,
6702d4d4864SRam Pai 	.poll		= mounts_poll,
6712d4d4864SRam Pai };
6722d4d4864SRam Pai 
673b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
674b4629fe2SChuck Lever {
675a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mountstats_op);
676b4629fe2SChuck Lever }
677b4629fe2SChuck Lever 
67800977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
679b4629fe2SChuck Lever 	.open		= mountstats_open,
680b4629fe2SChuck Lever 	.read		= seq_read,
681b4629fe2SChuck Lever 	.llseek		= seq_lseek,
682b4629fe2SChuck Lever 	.release	= mounts_release,
683b4629fe2SChuck Lever };
684b4629fe2SChuck Lever 
6851da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6881da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6891da177e4SLinus Torvalds {
6902fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6911da177e4SLinus Torvalds 	unsigned long page;
6921da177e4SLinus Torvalds 	ssize_t length;
69399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
69499f89551SEric W. Biederman 
69599f89551SEric W. Biederman 	length = -ESRCH;
69699f89551SEric W. Biederman 	if (!task)
69799f89551SEric W. Biederman 		goto out_no_task;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
7001da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
70199f89551SEric W. Biederman 
70299f89551SEric W. Biederman 	length = -ENOMEM;
703e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
70499f89551SEric W. Biederman 		goto out;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	if (length >= 0)
7091da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
7101da177e4SLinus Torvalds 	free_page(page);
71199f89551SEric W. Biederman out:
71299f89551SEric W. Biederman 	put_task_struct(task);
71399f89551SEric W. Biederman out_no_task:
7141da177e4SLinus Torvalds 	return length;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
71700977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
7181da177e4SLinus Torvalds 	.read		= proc_info_read,
71987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
7201da177e4SLinus Torvalds };
7211da177e4SLinus Torvalds 
722be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
723be614086SEric W. Biederman {
724be614086SEric W. Biederman 	struct inode *inode = m->private;
725be614086SEric W. Biederman 	struct pid_namespace *ns;
726be614086SEric W. Biederman 	struct pid *pid;
727be614086SEric W. Biederman 	struct task_struct *task;
728be614086SEric W. Biederman 	int ret;
729be614086SEric W. Biederman 
730be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
731be614086SEric W. Biederman 	pid = proc_pid(inode);
732be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
733be614086SEric W. Biederman 	if (!task)
734be614086SEric W. Biederman 		return -ESRCH;
735be614086SEric W. Biederman 
736be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
737be614086SEric W. Biederman 
738be614086SEric W. Biederman 	put_task_struct(task);
739be614086SEric W. Biederman 	return ret;
740be614086SEric W. Biederman }
741be614086SEric W. Biederman 
742be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
743be614086SEric W. Biederman {
744be614086SEric W. Biederman 	int ret;
745be614086SEric W. Biederman 	ret = single_open(filp, proc_single_show, NULL);
746be614086SEric W. Biederman 	if (!ret) {
747be614086SEric W. Biederman 		struct seq_file *m = filp->private_data;
748be614086SEric W. Biederman 
749be614086SEric W. Biederman 		m->private = inode;
750be614086SEric W. Biederman 	}
751be614086SEric W. Biederman 	return ret;
752be614086SEric W. Biederman }
753be614086SEric W. Biederman 
754be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
755be614086SEric W. Biederman 	.open		= proc_single_open,
756be614086SEric W. Biederman 	.read		= seq_read,
757be614086SEric W. Biederman 	.llseek		= seq_lseek,
758be614086SEric W. Biederman 	.release	= single_release,
759be614086SEric W. Biederman };
760be614086SEric W. Biederman 
7611da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
7641da177e4SLinus Torvalds 	return 0;
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
7681da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
7691da177e4SLinus Torvalds {
7702fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7711da177e4SLinus Torvalds 	char *page;
7721da177e4SLinus Torvalds 	unsigned long src = *ppos;
7731da177e4SLinus Torvalds 	int ret = -ESRCH;
7741da177e4SLinus Torvalds 	struct mm_struct *mm;
7751da177e4SLinus Torvalds 
77699f89551SEric W. Biederman 	if (!task)
77799f89551SEric W. Biederman 		goto out_no_task;
77899f89551SEric W. Biederman 
779638fa202SRoland McGrath 	if (check_mem_permission(task))
7801da177e4SLinus Torvalds 		goto out;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	ret = -ENOMEM;
783e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7841da177e4SLinus Torvalds 	if (!page)
7851da177e4SLinus Torvalds 		goto out;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	ret = 0;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	mm = get_task_mm(task);
7901da177e4SLinus Torvalds 	if (!mm)
7911da177e4SLinus Torvalds 		goto out_free;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	ret = -EIO;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
7961da177e4SLinus Torvalds 		goto out_put;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	ret = 0;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	while (count > 0) {
8011da177e4SLinus Torvalds 		int this_len, retval;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8041da177e4SLinus Torvalds 		retval = access_process_vm(task, src, page, this_len, 0);
805638fa202SRoland McGrath 		if (!retval || check_mem_permission(task)) {
8061da177e4SLinus Torvalds 			if (!ret)
8071da177e4SLinus Torvalds 				ret = -EIO;
8081da177e4SLinus Torvalds 			break;
8091da177e4SLinus Torvalds 		}
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
8121da177e4SLinus Torvalds 			ret = -EFAULT;
8131da177e4SLinus Torvalds 			break;
8141da177e4SLinus Torvalds 		}
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 		ret += retval;
8171da177e4SLinus Torvalds 		src += retval;
8181da177e4SLinus Torvalds 		buf += retval;
8191da177e4SLinus Torvalds 		count -= retval;
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds 	*ppos = src;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds out_put:
8241da177e4SLinus Torvalds 	mmput(mm);
8251da177e4SLinus Torvalds out_free:
8261da177e4SLinus Torvalds 	free_page((unsigned long) page);
8271da177e4SLinus Torvalds out:
82899f89551SEric W. Biederman 	put_task_struct(task);
82999f89551SEric W. Biederman out_no_task:
8301da177e4SLinus Torvalds 	return ret;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds #define mem_write NULL
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds #ifndef mem_write
8361da177e4SLinus Torvalds /* This is a security hazard */
83763967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
8381da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
8391da177e4SLinus Torvalds {
840f7ca54f4SFrederik Deweerdt 	int copied;
8411da177e4SLinus Torvalds 	char *page;
8422fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8431da177e4SLinus Torvalds 	unsigned long dst = *ppos;
8441da177e4SLinus Torvalds 
84599f89551SEric W. Biederman 	copied = -ESRCH;
84699f89551SEric W. Biederman 	if (!task)
84799f89551SEric W. Biederman 		goto out_no_task;
8481da177e4SLinus Torvalds 
849638fa202SRoland McGrath 	if (check_mem_permission(task))
85099f89551SEric W. Biederman 		goto out;
85199f89551SEric W. Biederman 
85299f89551SEric W. Biederman 	copied = -ENOMEM;
853e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
8541da177e4SLinus Torvalds 	if (!page)
85599f89551SEric W. Biederman 		goto out;
8561da177e4SLinus Torvalds 
857f7ca54f4SFrederik Deweerdt 	copied = 0;
8581da177e4SLinus Torvalds 	while (count > 0) {
8591da177e4SLinus Torvalds 		int this_len, retval;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8621da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
8631da177e4SLinus Torvalds 			copied = -EFAULT;
8641da177e4SLinus Torvalds 			break;
8651da177e4SLinus Torvalds 		}
8661da177e4SLinus Torvalds 		retval = access_process_vm(task, dst, page, this_len, 1);
8671da177e4SLinus Torvalds 		if (!retval) {
8681da177e4SLinus Torvalds 			if (!copied)
8691da177e4SLinus Torvalds 				copied = -EIO;
8701da177e4SLinus Torvalds 			break;
8711da177e4SLinus Torvalds 		}
8721da177e4SLinus Torvalds 		copied += retval;
8731da177e4SLinus Torvalds 		buf += retval;
8741da177e4SLinus Torvalds 		dst += retval;
8751da177e4SLinus Torvalds 		count -= retval;
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 	*ppos = dst;
8781da177e4SLinus Torvalds 	free_page((unsigned long) page);
87999f89551SEric W. Biederman out:
88099f89551SEric W. Biederman 	put_task_struct(task);
88199f89551SEric W. Biederman out_no_task:
8821da177e4SLinus Torvalds 	return copied;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds #endif
8851da177e4SLinus Torvalds 
88685863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 	switch (orig) {
8891da177e4SLinus Torvalds 	case 0:
8901da177e4SLinus Torvalds 		file->f_pos = offset;
8911da177e4SLinus Torvalds 		break;
8921da177e4SLinus Torvalds 	case 1:
8931da177e4SLinus Torvalds 		file->f_pos += offset;
8941da177e4SLinus Torvalds 		break;
8951da177e4SLinus Torvalds 	default:
8961da177e4SLinus Torvalds 		return -EINVAL;
8971da177e4SLinus Torvalds 	}
8981da177e4SLinus Torvalds 	force_successful_syscall_return();
8991da177e4SLinus Torvalds 	return file->f_pos;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
90200977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
9031da177e4SLinus Torvalds 	.llseek		= mem_lseek,
9041da177e4SLinus Torvalds 	.read		= mem_read,
9051da177e4SLinus Torvalds 	.write		= mem_write,
9061da177e4SLinus Torvalds 	.open		= mem_open,
9071da177e4SLinus Torvalds };
9081da177e4SLinus Torvalds 
909315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
910315e28c8SJames Pearson 			size_t count, loff_t *ppos)
911315e28c8SJames Pearson {
912315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
913315e28c8SJames Pearson 	char *page;
914315e28c8SJames Pearson 	unsigned long src = *ppos;
915315e28c8SJames Pearson 	int ret = -ESRCH;
916315e28c8SJames Pearson 	struct mm_struct *mm;
917315e28c8SJames Pearson 
918315e28c8SJames Pearson 	if (!task)
919315e28c8SJames Pearson 		goto out_no_task;
920315e28c8SJames Pearson 
921006ebb40SStephen Smalley 	if (!ptrace_may_access(task, PTRACE_MODE_READ))
922315e28c8SJames Pearson 		goto out;
923315e28c8SJames Pearson 
924315e28c8SJames Pearson 	ret = -ENOMEM;
925315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
926315e28c8SJames Pearson 	if (!page)
927315e28c8SJames Pearson 		goto out;
928315e28c8SJames Pearson 
929315e28c8SJames Pearson 	ret = 0;
930315e28c8SJames Pearson 
931315e28c8SJames Pearson 	mm = get_task_mm(task);
932315e28c8SJames Pearson 	if (!mm)
933315e28c8SJames Pearson 		goto out_free;
934315e28c8SJames Pearson 
935315e28c8SJames Pearson 	while (count > 0) {
936315e28c8SJames Pearson 		int this_len, retval, max_len;
937315e28c8SJames Pearson 
938315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
939315e28c8SJames Pearson 
940315e28c8SJames Pearson 		if (this_len <= 0)
941315e28c8SJames Pearson 			break;
942315e28c8SJames Pearson 
943315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
944315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
945315e28c8SJames Pearson 
946315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
947315e28c8SJames Pearson 			page, this_len, 0);
948315e28c8SJames Pearson 
949315e28c8SJames Pearson 		if (retval <= 0) {
950315e28c8SJames Pearson 			ret = retval;
951315e28c8SJames Pearson 			break;
952315e28c8SJames Pearson 		}
953315e28c8SJames Pearson 
954315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
955315e28c8SJames Pearson 			ret = -EFAULT;
956315e28c8SJames Pearson 			break;
957315e28c8SJames Pearson 		}
958315e28c8SJames Pearson 
959315e28c8SJames Pearson 		ret += retval;
960315e28c8SJames Pearson 		src += retval;
961315e28c8SJames Pearson 		buf += retval;
962315e28c8SJames Pearson 		count -= retval;
963315e28c8SJames Pearson 	}
964315e28c8SJames Pearson 	*ppos = src;
965315e28c8SJames Pearson 
966315e28c8SJames Pearson 	mmput(mm);
967315e28c8SJames Pearson out_free:
968315e28c8SJames Pearson 	free_page((unsigned long) page);
969315e28c8SJames Pearson out:
970315e28c8SJames Pearson 	put_task_struct(task);
971315e28c8SJames Pearson out_no_task:
972315e28c8SJames Pearson 	return ret;
973315e28c8SJames Pearson }
974315e28c8SJames Pearson 
975315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
976315e28c8SJames Pearson 	.read		= environ_read,
97787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
978315e28c8SJames Pearson };
979315e28c8SJames Pearson 
9801da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
9811da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9821da177e4SLinus Torvalds {
9832fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
9848578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
9851da177e4SLinus Torvalds 	size_t len;
98628b83c51SKOSAKI Motohiro 	int oom_adjust = OOM_DISABLE;
98728b83c51SKOSAKI Motohiro 	unsigned long flags;
9881da177e4SLinus Torvalds 
98999f89551SEric W. Biederman 	if (!task)
99099f89551SEric W. Biederman 		return -ESRCH;
99128b83c51SKOSAKI Motohiro 
99228b83c51SKOSAKI Motohiro 	if (lock_task_sighand(task, &flags)) {
99328b83c51SKOSAKI Motohiro 		oom_adjust = task->signal->oom_adj;
99428b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
99528b83c51SKOSAKI Motohiro 	}
99628b83c51SKOSAKI Motohiro 
99799f89551SEric W. Biederman 	put_task_struct(task);
99899f89551SEric W. Biederman 
9998578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
10000c28f287SAkinobu Mita 
10010c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
10051da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
10061da177e4SLinus Torvalds {
100799f89551SEric W. Biederman 	struct task_struct *task;
10085d863b89SKOSAKI Motohiro 	char buffer[PROC_NUMBUF];
10095d863b89SKOSAKI Motohiro 	long oom_adjust;
101028b83c51SKOSAKI Motohiro 	unsigned long flags;
10115d863b89SKOSAKI Motohiro 	int err;
10121da177e4SLinus Torvalds 
10138578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
10148578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
10158578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
10161da177e4SLinus Torvalds 	if (copy_from_user(buffer, buf, count))
10171da177e4SLinus Torvalds 		return -EFAULT;
10185d863b89SKOSAKI Motohiro 
10195d863b89SKOSAKI Motohiro 	err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
10205d863b89SKOSAKI Motohiro 	if (err)
10215d863b89SKOSAKI Motohiro 		return -EINVAL;
10228ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
10238ac773b4SAlexey Dobriyan 	     oom_adjust != OOM_DISABLE)
10241da177e4SLinus Torvalds 		return -EINVAL;
10255d863b89SKOSAKI Motohiro 
10262fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
102799f89551SEric W. Biederman 	if (!task)
102899f89551SEric W. Biederman 		return -ESRCH;
102928b83c51SKOSAKI Motohiro 	if (!lock_task_sighand(task, &flags)) {
103028b83c51SKOSAKI Motohiro 		put_task_struct(task);
103128b83c51SKOSAKI Motohiro 		return -ESRCH;
103228b83c51SKOSAKI Motohiro 	}
103328b83c51SKOSAKI Motohiro 
103428b83c51SKOSAKI Motohiro 	if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
103528b83c51SKOSAKI Motohiro 		unlock_task_sighand(task, &flags);
10368fb4fc68SGuillem Jover 		put_task_struct(task);
10378fb4fc68SGuillem Jover 		return -EACCES;
10388fb4fc68SGuillem Jover 	}
103928b83c51SKOSAKI Motohiro 
104028b83c51SKOSAKI Motohiro 	task->signal->oom_adj = oom_adjust;
1041*a63d83f4SDavid Rientjes 	/*
1042*a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1043*a63d83f4SDavid Rientjes 	 * value is always attainable.
1044*a63d83f4SDavid Rientjes 	 */
1045*a63d83f4SDavid Rientjes 	if (task->signal->oom_adj == OOM_ADJUST_MAX)
1046*a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1047*a63d83f4SDavid Rientjes 	else
1048*a63d83f4SDavid Rientjes 		task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1049*a63d83f4SDavid Rientjes 								-OOM_DISABLE;
105028b83c51SKOSAKI Motohiro 	unlock_task_sighand(task, &flags);
105199f89551SEric W. Biederman 	put_task_struct(task);
10525d863b89SKOSAKI Motohiro 
10535d863b89SKOSAKI Motohiro 	return count;
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
105600977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
10571da177e4SLinus Torvalds 	.read		= oom_adjust_read,
10581da177e4SLinus Torvalds 	.write		= oom_adjust_write,
105987df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
10601da177e4SLinus Torvalds };
10611da177e4SLinus Torvalds 
1062*a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1063*a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1064*a63d83f4SDavid Rientjes {
1065*a63d83f4SDavid Rientjes 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1066*a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1067*a63d83f4SDavid Rientjes 	int oom_score_adj = OOM_SCORE_ADJ_MIN;
1068*a63d83f4SDavid Rientjes 	unsigned long flags;
1069*a63d83f4SDavid Rientjes 	size_t len;
1070*a63d83f4SDavid Rientjes 
1071*a63d83f4SDavid Rientjes 	if (!task)
1072*a63d83f4SDavid Rientjes 		return -ESRCH;
1073*a63d83f4SDavid Rientjes 	if (lock_task_sighand(task, &flags)) {
1074*a63d83f4SDavid Rientjes 		oom_score_adj = task->signal->oom_score_adj;
1075*a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
1076*a63d83f4SDavid Rientjes 	}
1077*a63d83f4SDavid Rientjes 	put_task_struct(task);
1078*a63d83f4SDavid Rientjes 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1079*a63d83f4SDavid Rientjes 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1080*a63d83f4SDavid Rientjes }
1081*a63d83f4SDavid Rientjes 
1082*a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1083*a63d83f4SDavid Rientjes 					size_t count, loff_t *ppos)
1084*a63d83f4SDavid Rientjes {
1085*a63d83f4SDavid Rientjes 	struct task_struct *task;
1086*a63d83f4SDavid Rientjes 	char buffer[PROC_NUMBUF];
1087*a63d83f4SDavid Rientjes 	unsigned long flags;
1088*a63d83f4SDavid Rientjes 	long oom_score_adj;
1089*a63d83f4SDavid Rientjes 	int err;
1090*a63d83f4SDavid Rientjes 
1091*a63d83f4SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
1092*a63d83f4SDavid Rientjes 	if (count > sizeof(buffer) - 1)
1093*a63d83f4SDavid Rientjes 		count = sizeof(buffer) - 1;
1094*a63d83f4SDavid Rientjes 	if (copy_from_user(buffer, buf, count))
1095*a63d83f4SDavid Rientjes 		return -EFAULT;
1096*a63d83f4SDavid Rientjes 
1097*a63d83f4SDavid Rientjes 	err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1098*a63d83f4SDavid Rientjes 	if (err)
1099*a63d83f4SDavid Rientjes 		return -EINVAL;
1100*a63d83f4SDavid Rientjes 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1101*a63d83f4SDavid Rientjes 			oom_score_adj > OOM_SCORE_ADJ_MAX)
1102*a63d83f4SDavid Rientjes 		return -EINVAL;
1103*a63d83f4SDavid Rientjes 
1104*a63d83f4SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
1105*a63d83f4SDavid Rientjes 	if (!task)
1106*a63d83f4SDavid Rientjes 		return -ESRCH;
1107*a63d83f4SDavid Rientjes 	if (!lock_task_sighand(task, &flags)) {
1108*a63d83f4SDavid Rientjes 		put_task_struct(task);
1109*a63d83f4SDavid Rientjes 		return -ESRCH;
1110*a63d83f4SDavid Rientjes 	}
1111*a63d83f4SDavid Rientjes 	if (oom_score_adj < task->signal->oom_score_adj &&
1112*a63d83f4SDavid Rientjes 			!capable(CAP_SYS_RESOURCE)) {
1113*a63d83f4SDavid Rientjes 		unlock_task_sighand(task, &flags);
1114*a63d83f4SDavid Rientjes 		put_task_struct(task);
1115*a63d83f4SDavid Rientjes 		return -EACCES;
1116*a63d83f4SDavid Rientjes 	}
1117*a63d83f4SDavid Rientjes 
1118*a63d83f4SDavid Rientjes 	task->signal->oom_score_adj = oom_score_adj;
1119*a63d83f4SDavid Rientjes 	/*
1120*a63d83f4SDavid Rientjes 	 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1121*a63d83f4SDavid Rientjes 	 * always attainable.
1122*a63d83f4SDavid Rientjes 	 */
1123*a63d83f4SDavid Rientjes 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1124*a63d83f4SDavid Rientjes 		task->signal->oom_adj = OOM_DISABLE;
1125*a63d83f4SDavid Rientjes 	else
1126*a63d83f4SDavid Rientjes 		task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1127*a63d83f4SDavid Rientjes 							OOM_SCORE_ADJ_MAX;
1128*a63d83f4SDavid Rientjes 	unlock_task_sighand(task, &flags);
1129*a63d83f4SDavid Rientjes 	put_task_struct(task);
1130*a63d83f4SDavid Rientjes 	return count;
1131*a63d83f4SDavid Rientjes }
1132*a63d83f4SDavid Rientjes 
1133*a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = {
1134*a63d83f4SDavid Rientjes 	.read		= oom_score_adj_read,
1135*a63d83f4SDavid Rientjes 	.write		= oom_score_adj_write,
1136*a63d83f4SDavid Rientjes };
1137*a63d83f4SDavid Rientjes 
11381da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
11391da177e4SLinus Torvalds #define TMPBUFLEN 21
11401da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
11411da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
11421da177e4SLinus Torvalds {
11432fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
114499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
11451da177e4SLinus Torvalds 	ssize_t length;
11461da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
11471da177e4SLinus Torvalds 
114899f89551SEric W. Biederman 	if (!task)
114999f89551SEric W. Biederman 		return -ESRCH;
11501da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
11510c11b942SAl Viro 				audit_get_loginuid(task));
115299f89551SEric W. Biederman 	put_task_struct(task);
11531da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
11571da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
11581da177e4SLinus Torvalds {
11592fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
11601da177e4SLinus Torvalds 	char *page, *tmp;
11611da177e4SLinus Torvalds 	ssize_t length;
11621da177e4SLinus Torvalds 	uid_t loginuid;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
11651da177e4SLinus Torvalds 		return -EPERM;
11661da177e4SLinus Torvalds 
11677dc52157SPaul E. McKenney 	rcu_read_lock();
11687dc52157SPaul E. McKenney 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
11697dc52157SPaul E. McKenney 		rcu_read_unlock();
11701da177e4SLinus Torvalds 		return -EPERM;
11717dc52157SPaul E. McKenney 	}
11727dc52157SPaul E. McKenney 	rcu_read_unlock();
11731da177e4SLinus Torvalds 
1174e0182909SAl Viro 	if (count >= PAGE_SIZE)
1175e0182909SAl Viro 		count = PAGE_SIZE - 1;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	if (*ppos != 0) {
11781da177e4SLinus Torvalds 		/* No partial writes. */
11791da177e4SLinus Torvalds 		return -EINVAL;
11801da177e4SLinus Torvalds 	}
1181e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
11821da177e4SLinus Torvalds 	if (!page)
11831da177e4SLinus Torvalds 		return -ENOMEM;
11841da177e4SLinus Torvalds 	length = -EFAULT;
11851da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11861da177e4SLinus Torvalds 		goto out_free_page;
11871da177e4SLinus Torvalds 
1188e0182909SAl Viro 	page[count] = '\0';
11891da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
11901da177e4SLinus Torvalds 	if (tmp == page) {
11911da177e4SLinus Torvalds 		length = -EINVAL;
11921da177e4SLinus Torvalds 		goto out_free_page;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	}
119599f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
11961da177e4SLinus Torvalds 	if (likely(length == 0))
11971da177e4SLinus Torvalds 		length = count;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds out_free_page:
12001da177e4SLinus Torvalds 	free_page((unsigned long) page);
12011da177e4SLinus Torvalds 	return length;
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
120400977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
12051da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
12061da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
120787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12081da177e4SLinus Torvalds };
12091e0bd755SEric Paris 
12101e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
12111e0bd755SEric Paris 				  size_t count, loff_t *ppos)
12121e0bd755SEric Paris {
12131e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
12141e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
12151e0bd755SEric Paris 	ssize_t length;
12161e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
12171e0bd755SEric Paris 
12181e0bd755SEric Paris 	if (!task)
12191e0bd755SEric Paris 		return -ESRCH;
12201e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
12211e0bd755SEric Paris 				audit_get_sessionid(task));
12221e0bd755SEric Paris 	put_task_struct(task);
12231e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
12241e0bd755SEric Paris }
12251e0bd755SEric Paris 
12261e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
12271e0bd755SEric Paris 	.read		= proc_sessionid_read,
122887df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
12291e0bd755SEric Paris };
12301da177e4SLinus Torvalds #endif
12311da177e4SLinus Torvalds 
1232f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1233f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1234f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1235f4f154fdSAkinobu Mita {
1236f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1237f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1238f4f154fdSAkinobu Mita 	size_t len;
1239f4f154fdSAkinobu Mita 	int make_it_fail;
1240f4f154fdSAkinobu Mita 
1241f4f154fdSAkinobu Mita 	if (!task)
1242f4f154fdSAkinobu Mita 		return -ESRCH;
1243f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1244f4f154fdSAkinobu Mita 	put_task_struct(task);
1245f4f154fdSAkinobu Mita 
1246f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
12470c28f287SAkinobu Mita 
12480c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1249f4f154fdSAkinobu Mita }
1250f4f154fdSAkinobu Mita 
1251f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1252f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1253f4f154fdSAkinobu Mita {
1254f4f154fdSAkinobu Mita 	struct task_struct *task;
1255f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1256f4f154fdSAkinobu Mita 	int make_it_fail;
1257f4f154fdSAkinobu Mita 
1258f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1259f4f154fdSAkinobu Mita 		return -EPERM;
1260f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1261f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1262f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1263f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1264f4f154fdSAkinobu Mita 		return -EFAULT;
1265cba8aafeSVincent Li 	make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1266cba8aafeSVincent Li 	if (*end)
1267cba8aafeSVincent Li 		return -EINVAL;
1268f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1269f4f154fdSAkinobu Mita 	if (!task)
1270f4f154fdSAkinobu Mita 		return -ESRCH;
1271f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1272f4f154fdSAkinobu Mita 	put_task_struct(task);
1273cba8aafeSVincent Li 
1274cba8aafeSVincent Li 	return count;
1275f4f154fdSAkinobu Mita }
1276f4f154fdSAkinobu Mita 
127700977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1278f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1279f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
128087df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
1281f4f154fdSAkinobu Mita };
1282f4f154fdSAkinobu Mita #endif
1283f4f154fdSAkinobu Mita 
12849745512cSArjan van de Ven 
128543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
128643ae34cbSIngo Molnar /*
128743ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
128843ae34cbSIngo Molnar  */
128943ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
129043ae34cbSIngo Molnar {
129143ae34cbSIngo Molnar 	struct inode *inode = m->private;
129243ae34cbSIngo Molnar 	struct task_struct *p;
129343ae34cbSIngo Molnar 
129443ae34cbSIngo Molnar 	p = get_proc_task(inode);
129543ae34cbSIngo Molnar 	if (!p)
129643ae34cbSIngo Molnar 		return -ESRCH;
129743ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
129843ae34cbSIngo Molnar 
129943ae34cbSIngo Molnar 	put_task_struct(p);
130043ae34cbSIngo Molnar 
130143ae34cbSIngo Molnar 	return 0;
130243ae34cbSIngo Molnar }
130343ae34cbSIngo Molnar 
130443ae34cbSIngo Molnar static ssize_t
130543ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
130643ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
130743ae34cbSIngo Molnar {
130843ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
130943ae34cbSIngo Molnar 	struct task_struct *p;
131043ae34cbSIngo Molnar 
131143ae34cbSIngo Molnar 	p = get_proc_task(inode);
131243ae34cbSIngo Molnar 	if (!p)
131343ae34cbSIngo Molnar 		return -ESRCH;
131443ae34cbSIngo Molnar 	proc_sched_set_task(p);
131543ae34cbSIngo Molnar 
131643ae34cbSIngo Molnar 	put_task_struct(p);
131743ae34cbSIngo Molnar 
131843ae34cbSIngo Molnar 	return count;
131943ae34cbSIngo Molnar }
132043ae34cbSIngo Molnar 
132143ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
132243ae34cbSIngo Molnar {
132343ae34cbSIngo Molnar 	int ret;
132443ae34cbSIngo Molnar 
132543ae34cbSIngo Molnar 	ret = single_open(filp, sched_show, NULL);
132643ae34cbSIngo Molnar 	if (!ret) {
132743ae34cbSIngo Molnar 		struct seq_file *m = filp->private_data;
132843ae34cbSIngo Molnar 
132943ae34cbSIngo Molnar 		m->private = inode;
133043ae34cbSIngo Molnar 	}
133143ae34cbSIngo Molnar 	return ret;
133243ae34cbSIngo Molnar }
133343ae34cbSIngo Molnar 
133443ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
133543ae34cbSIngo Molnar 	.open		= sched_open,
133643ae34cbSIngo Molnar 	.read		= seq_read,
133743ae34cbSIngo Molnar 	.write		= sched_write,
133843ae34cbSIngo Molnar 	.llseek		= seq_lseek,
13395ea473a1SAlexey Dobriyan 	.release	= single_release,
134043ae34cbSIngo Molnar };
134143ae34cbSIngo Molnar 
134243ae34cbSIngo Molnar #endif
134343ae34cbSIngo Molnar 
13444614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf,
13454614a696Sjohn stultz 				size_t count, loff_t *offset)
13464614a696Sjohn stultz {
13474614a696Sjohn stultz 	struct inode *inode = file->f_path.dentry->d_inode;
13484614a696Sjohn stultz 	struct task_struct *p;
13494614a696Sjohn stultz 	char buffer[TASK_COMM_LEN];
13504614a696Sjohn stultz 
13514614a696Sjohn stultz 	memset(buffer, 0, sizeof(buffer));
13524614a696Sjohn stultz 	if (count > sizeof(buffer) - 1)
13534614a696Sjohn stultz 		count = sizeof(buffer) - 1;
13544614a696Sjohn stultz 	if (copy_from_user(buffer, buf, count))
13554614a696Sjohn stultz 		return -EFAULT;
13564614a696Sjohn stultz 
13574614a696Sjohn stultz 	p = get_proc_task(inode);
13584614a696Sjohn stultz 	if (!p)
13594614a696Sjohn stultz 		return -ESRCH;
13604614a696Sjohn stultz 
13614614a696Sjohn stultz 	if (same_thread_group(current, p))
13624614a696Sjohn stultz 		set_task_comm(p, buffer);
13634614a696Sjohn stultz 	else
13644614a696Sjohn stultz 		count = -EINVAL;
13654614a696Sjohn stultz 
13664614a696Sjohn stultz 	put_task_struct(p);
13674614a696Sjohn stultz 
13684614a696Sjohn stultz 	return count;
13694614a696Sjohn stultz }
13704614a696Sjohn stultz 
13714614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v)
13724614a696Sjohn stultz {
13734614a696Sjohn stultz 	struct inode *inode = m->private;
13744614a696Sjohn stultz 	struct task_struct *p;
13754614a696Sjohn stultz 
13764614a696Sjohn stultz 	p = get_proc_task(inode);
13774614a696Sjohn stultz 	if (!p)
13784614a696Sjohn stultz 		return -ESRCH;
13794614a696Sjohn stultz 
13804614a696Sjohn stultz 	task_lock(p);
13814614a696Sjohn stultz 	seq_printf(m, "%s\n", p->comm);
13824614a696Sjohn stultz 	task_unlock(p);
13834614a696Sjohn stultz 
13844614a696Sjohn stultz 	put_task_struct(p);
13854614a696Sjohn stultz 
13864614a696Sjohn stultz 	return 0;
13874614a696Sjohn stultz }
13884614a696Sjohn stultz 
13894614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp)
13904614a696Sjohn stultz {
13914614a696Sjohn stultz 	int ret;
13924614a696Sjohn stultz 
13934614a696Sjohn stultz 	ret = single_open(filp, comm_show, NULL);
13944614a696Sjohn stultz 	if (!ret) {
13954614a696Sjohn stultz 		struct seq_file *m = filp->private_data;
13964614a696Sjohn stultz 
13974614a696Sjohn stultz 		m->private = inode;
13984614a696Sjohn stultz 	}
13994614a696Sjohn stultz 	return ret;
14004614a696Sjohn stultz }
14014614a696Sjohn stultz 
14024614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = {
14034614a696Sjohn stultz 	.open		= comm_open,
14044614a696Sjohn stultz 	.read		= seq_read,
14054614a696Sjohn stultz 	.write		= comm_write,
14064614a696Sjohn stultz 	.llseek		= seq_lseek,
14074614a696Sjohn stultz 	.release	= single_release,
14084614a696Sjohn stultz };
14094614a696Sjohn stultz 
1410925d1c40SMatt Helsley /*
1411925d1c40SMatt Helsley  * We added or removed a vma mapping the executable. The vmas are only mapped
1412925d1c40SMatt Helsley  * during exec and are not mapped with the mmap system call.
1413925d1c40SMatt Helsley  * Callers must hold down_write() on the mm's mmap_sem for these
1414925d1c40SMatt Helsley  */
1415925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm)
1416925d1c40SMatt Helsley {
1417925d1c40SMatt Helsley 	mm->num_exe_file_vmas++;
1418925d1c40SMatt Helsley }
1419925d1c40SMatt Helsley 
1420925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm)
1421925d1c40SMatt Helsley {
1422925d1c40SMatt Helsley 	mm->num_exe_file_vmas--;
1423925d1c40SMatt Helsley 	if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1424925d1c40SMatt Helsley 		fput(mm->exe_file);
1425925d1c40SMatt Helsley 		mm->exe_file = NULL;
1426925d1c40SMatt Helsley 	}
1427925d1c40SMatt Helsley 
1428925d1c40SMatt Helsley }
1429925d1c40SMatt Helsley 
1430925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1431925d1c40SMatt Helsley {
1432925d1c40SMatt Helsley 	if (new_exe_file)
1433925d1c40SMatt Helsley 		get_file(new_exe_file);
1434925d1c40SMatt Helsley 	if (mm->exe_file)
1435925d1c40SMatt Helsley 		fput(mm->exe_file);
1436925d1c40SMatt Helsley 	mm->exe_file = new_exe_file;
1437925d1c40SMatt Helsley 	mm->num_exe_file_vmas = 0;
1438925d1c40SMatt Helsley }
1439925d1c40SMatt Helsley 
1440925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm)
1441925d1c40SMatt Helsley {
1442925d1c40SMatt Helsley 	struct file *exe_file;
1443925d1c40SMatt Helsley 
1444925d1c40SMatt Helsley 	/* We need mmap_sem to protect against races with removal of
1445925d1c40SMatt Helsley 	 * VM_EXECUTABLE vmas */
1446925d1c40SMatt Helsley 	down_read(&mm->mmap_sem);
1447925d1c40SMatt Helsley 	exe_file = mm->exe_file;
1448925d1c40SMatt Helsley 	if (exe_file)
1449925d1c40SMatt Helsley 		get_file(exe_file);
1450925d1c40SMatt Helsley 	up_read(&mm->mmap_sem);
1451925d1c40SMatt Helsley 	return exe_file;
1452925d1c40SMatt Helsley }
1453925d1c40SMatt Helsley 
1454925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1455925d1c40SMatt Helsley {
1456925d1c40SMatt Helsley 	/* It's safe to write the exe_file pointer without exe_file_lock because
1457925d1c40SMatt Helsley 	 * this is called during fork when the task is not yet in /proc */
1458925d1c40SMatt Helsley 	newmm->exe_file = get_mm_exe_file(oldmm);
1459925d1c40SMatt Helsley }
1460925d1c40SMatt Helsley 
1461925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path)
1462925d1c40SMatt Helsley {
1463925d1c40SMatt Helsley 	struct task_struct *task;
1464925d1c40SMatt Helsley 	struct mm_struct *mm;
1465925d1c40SMatt Helsley 	struct file *exe_file;
1466925d1c40SMatt Helsley 
1467925d1c40SMatt Helsley 	task = get_proc_task(inode);
1468925d1c40SMatt Helsley 	if (!task)
1469925d1c40SMatt Helsley 		return -ENOENT;
1470925d1c40SMatt Helsley 	mm = get_task_mm(task);
1471925d1c40SMatt Helsley 	put_task_struct(task);
1472925d1c40SMatt Helsley 	if (!mm)
1473925d1c40SMatt Helsley 		return -ENOENT;
1474925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1475925d1c40SMatt Helsley 	mmput(mm);
1476925d1c40SMatt Helsley 	if (exe_file) {
1477925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1478925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1479925d1c40SMatt Helsley 		fput(exe_file);
1480925d1c40SMatt Helsley 		return 0;
1481925d1c40SMatt Helsley 	} else
1482925d1c40SMatt Helsley 		return -ENOENT;
1483925d1c40SMatt Helsley }
1484925d1c40SMatt Helsley 
1485008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
14861da177e4SLinus Torvalds {
14871da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14881da177e4SLinus Torvalds 	int error = -EACCES;
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
14911d957f9bSJan Blunck 	path_put(&nd->path);
14921da177e4SLinus Torvalds 
1493778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1494778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
14951da177e4SLinus Torvalds 		goto out;
14961da177e4SLinus Torvalds 
14973dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
14981da177e4SLinus Torvalds out:
1499008b150aSAl Viro 	return ERR_PTR(error);
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
15023dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
15031da177e4SLinus Torvalds {
1504e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
15053dcd25f3SJan Blunck 	char *pathname;
15061da177e4SLinus Torvalds 	int len;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	if (!tmp)
15091da177e4SLinus Torvalds 		return -ENOMEM;
15101da177e4SLinus Torvalds 
1511cf28b486SJan Blunck 	pathname = d_path(path, tmp, PAGE_SIZE);
15123dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
15133dcd25f3SJan Blunck 	if (IS_ERR(pathname))
15141da177e4SLinus Torvalds 		goto out;
15153dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	if (len > buflen)
15181da177e4SLinus Torvalds 		len = buflen;
15193dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
15201da177e4SLinus Torvalds 		len = -EFAULT;
15211da177e4SLinus Torvalds  out:
15221da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
15231da177e4SLinus Torvalds 	return len;
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
15271da177e4SLinus Torvalds {
15281da177e4SLinus Torvalds 	int error = -EACCES;
15291da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
15303dcd25f3SJan Blunck 	struct path path;
15311da177e4SLinus Torvalds 
1532778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1533778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
15341da177e4SLinus Torvalds 		goto out;
15351da177e4SLinus Torvalds 
15363dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &path);
15371da177e4SLinus Torvalds 	if (error)
15381da177e4SLinus Torvalds 		goto out;
15391da177e4SLinus Torvalds 
15403dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
15413dcd25f3SJan Blunck 	path_put(&path);
15421da177e4SLinus Torvalds out:
15431da177e4SLinus Torvalds 	return error;
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
1546c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
15471da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
15486d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
15496d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
15501da177e4SLinus Torvalds };
15511da177e4SLinus Torvalds 
155228a6d671SEric W. Biederman 
155328a6d671SEric W. Biederman /* building an inode */
155428a6d671SEric W. Biederman 
155528a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
155628a6d671SEric W. Biederman {
155728a6d671SEric W. Biederman 	int dumpable = 0;
155828a6d671SEric W. Biederman 	struct mm_struct *mm;
155928a6d671SEric W. Biederman 
156028a6d671SEric W. Biederman 	task_lock(task);
156128a6d671SEric W. Biederman 	mm = task->mm;
156228a6d671SEric W. Biederman 	if (mm)
15636c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
156428a6d671SEric W. Biederman 	task_unlock(task);
156528a6d671SEric W. Biederman 	if(dumpable == 1)
156628a6d671SEric W. Biederman 		return 1;
156728a6d671SEric W. Biederman 	return 0;
156828a6d671SEric W. Biederman }
156928a6d671SEric W. Biederman 
157028a6d671SEric W. Biederman 
157161a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
157228a6d671SEric W. Biederman {
157328a6d671SEric W. Biederman 	struct inode * inode;
157428a6d671SEric W. Biederman 	struct proc_inode *ei;
1575c69e8d9cSDavid Howells 	const struct cred *cred;
157628a6d671SEric W. Biederman 
157728a6d671SEric W. Biederman 	/* We need a new inode */
157828a6d671SEric W. Biederman 
157928a6d671SEric W. Biederman 	inode = new_inode(sb);
158028a6d671SEric W. Biederman 	if (!inode)
158128a6d671SEric W. Biederman 		goto out;
158228a6d671SEric W. Biederman 
158328a6d671SEric W. Biederman 	/* Common stuff */
158428a6d671SEric W. Biederman 	ei = PROC_I(inode);
158528a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
158628a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
158728a6d671SEric W. Biederman 
158828a6d671SEric W. Biederman 	/*
158928a6d671SEric W. Biederman 	 * grab the reference to task.
159028a6d671SEric W. Biederman 	 */
15911a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
159228a6d671SEric W. Biederman 	if (!ei->pid)
159328a6d671SEric W. Biederman 		goto out_unlock;
159428a6d671SEric W. Biederman 
159528a6d671SEric W. Biederman 	if (task_dumpable(task)) {
1596c69e8d9cSDavid Howells 		rcu_read_lock();
1597c69e8d9cSDavid Howells 		cred = __task_cred(task);
1598c69e8d9cSDavid Howells 		inode->i_uid = cred->euid;
1599c69e8d9cSDavid Howells 		inode->i_gid = cred->egid;
1600c69e8d9cSDavid Howells 		rcu_read_unlock();
160128a6d671SEric W. Biederman 	}
160228a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
160328a6d671SEric W. Biederman 
160428a6d671SEric W. Biederman out:
160528a6d671SEric W. Biederman 	return inode;
160628a6d671SEric W. Biederman 
160728a6d671SEric W. Biederman out_unlock:
160828a6d671SEric W. Biederman 	iput(inode);
160928a6d671SEric W. Biederman 	return NULL;
161028a6d671SEric W. Biederman }
161128a6d671SEric W. Biederman 
161228a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
161328a6d671SEric W. Biederman {
161428a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
161528a6d671SEric W. Biederman 	struct task_struct *task;
1616c69e8d9cSDavid Howells 	const struct cred *cred;
1617c69e8d9cSDavid Howells 
161828a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
161928a6d671SEric W. Biederman 
162028a6d671SEric W. Biederman 	rcu_read_lock();
162128a6d671SEric W. Biederman 	stat->uid = 0;
162228a6d671SEric W. Biederman 	stat->gid = 0;
162328a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
162428a6d671SEric W. Biederman 	if (task) {
162528a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
162628a6d671SEric W. Biederman 		    task_dumpable(task)) {
1627c69e8d9cSDavid Howells 			cred = __task_cred(task);
1628c69e8d9cSDavid Howells 			stat->uid = cred->euid;
1629c69e8d9cSDavid Howells 			stat->gid = cred->egid;
163028a6d671SEric W. Biederman 		}
163128a6d671SEric W. Biederman 	}
163228a6d671SEric W. Biederman 	rcu_read_unlock();
163328a6d671SEric W. Biederman 	return 0;
163428a6d671SEric W. Biederman }
163528a6d671SEric W. Biederman 
163628a6d671SEric W. Biederman /* dentry stuff */
163728a6d671SEric W. Biederman 
163828a6d671SEric W. Biederman /*
163928a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
164028a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
164128a6d671SEric W. Biederman  * due to the way we treat inodes.
164228a6d671SEric W. Biederman  *
164328a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
164428a6d671SEric W. Biederman  * performed a setuid(), etc.
164528a6d671SEric W. Biederman  *
164628a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
164728a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
164828a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
164928a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
165028a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
165128a6d671SEric W. Biederman  * directories.
165228a6d671SEric W. Biederman  */
165328a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
165428a6d671SEric W. Biederman {
165528a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
165628a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1657c69e8d9cSDavid Howells 	const struct cred *cred;
1658c69e8d9cSDavid Howells 
165928a6d671SEric W. Biederman 	if (task) {
166028a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
166128a6d671SEric W. Biederman 		    task_dumpable(task)) {
1662c69e8d9cSDavid Howells 			rcu_read_lock();
1663c69e8d9cSDavid Howells 			cred = __task_cred(task);
1664c69e8d9cSDavid Howells 			inode->i_uid = cred->euid;
1665c69e8d9cSDavid Howells 			inode->i_gid = cred->egid;
1666c69e8d9cSDavid Howells 			rcu_read_unlock();
166728a6d671SEric W. Biederman 		} else {
166828a6d671SEric W. Biederman 			inode->i_uid = 0;
166928a6d671SEric W. Biederman 			inode->i_gid = 0;
167028a6d671SEric W. Biederman 		}
167128a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
167228a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
167328a6d671SEric W. Biederman 		put_task_struct(task);
167428a6d671SEric W. Biederman 		return 1;
167528a6d671SEric W. Biederman 	}
167628a6d671SEric W. Biederman 	d_drop(dentry);
167728a6d671SEric W. Biederman 	return 0;
167828a6d671SEric W. Biederman }
167928a6d671SEric W. Biederman 
168028a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry)
168128a6d671SEric W. Biederman {
168228a6d671SEric W. Biederman 	/* Is the task we represent dead?
168328a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
168428a6d671SEric W. Biederman 	 * kill it immediately.
168528a6d671SEric W. Biederman 	 */
168628a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
168728a6d671SEric W. Biederman }
168828a6d671SEric W. Biederman 
1689d72f71ebSAl Viro static const struct dentry_operations pid_dentry_operations =
169028a6d671SEric W. Biederman {
169128a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
169228a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
169328a6d671SEric W. Biederman };
169428a6d671SEric W. Biederman 
169528a6d671SEric W. Biederman /* Lookups */
169628a6d671SEric W. Biederman 
1697c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1698c5141e6dSEric Dumazet 				struct task_struct *, const void *);
169961a28784SEric W. Biederman 
17001c0d04c9SEric W. Biederman /*
17011c0d04c9SEric W. Biederman  * Fill a directory entry.
17021c0d04c9SEric W. Biederman  *
17031c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
17041c0d04c9SEric W. Biederman  * file type from dcache entry.
17051c0d04c9SEric W. Biederman  *
17061c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
17071c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
17081c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
17091c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
17101c0d04c9SEric W. Biederman  * by stat.
17111c0d04c9SEric W. Biederman  */
171261a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
171361a28784SEric W. Biederman 	char *name, int len,
1714c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
171561a28784SEric W. Biederman {
17162fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
171761a28784SEric W. Biederman 	struct inode *inode;
171861a28784SEric W. Biederman 	struct qstr qname;
171961a28784SEric W. Biederman 	ino_t ino = 0;
172061a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
172161a28784SEric W. Biederman 
172261a28784SEric W. Biederman 	qname.name = name;
172361a28784SEric W. Biederman 	qname.len  = len;
172461a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
172561a28784SEric W. Biederman 
172661a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
172761a28784SEric W. Biederman 	if (!child) {
172861a28784SEric W. Biederman 		struct dentry *new;
172961a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
173061a28784SEric W. Biederman 		if (new) {
173161a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
173261a28784SEric W. Biederman 			if (child)
173361a28784SEric W. Biederman 				dput(new);
173461a28784SEric W. Biederman 			else
173561a28784SEric W. Biederman 				child = new;
173661a28784SEric W. Biederman 		}
173761a28784SEric W. Biederman 	}
173861a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
173961a28784SEric W. Biederman 		goto end_instantiate;
174061a28784SEric W. Biederman 	inode = child->d_inode;
174161a28784SEric W. Biederman 	if (inode) {
174261a28784SEric W. Biederman 		ino = inode->i_ino;
174361a28784SEric W. Biederman 		type = inode->i_mode >> 12;
174461a28784SEric W. Biederman 	}
174561a28784SEric W. Biederman 	dput(child);
174661a28784SEric W. Biederman end_instantiate:
174761a28784SEric W. Biederman 	if (!ino)
174861a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
174961a28784SEric W. Biederman 	if (!ino)
175061a28784SEric W. Biederman 		ino = 1;
175161a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
175261a28784SEric W. Biederman }
175361a28784SEric W. Biederman 
175428a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
175528a6d671SEric W. Biederman {
175628a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
175728a6d671SEric W. Biederman 	int len = dentry->d_name.len;
175828a6d671SEric W. Biederman 	unsigned n = 0;
175928a6d671SEric W. Biederman 
176028a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
176128a6d671SEric W. Biederman 		goto out;
176228a6d671SEric W. Biederman 	while (len-- > 0) {
176328a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
176428a6d671SEric W. Biederman 		if (c > 9)
176528a6d671SEric W. Biederman 			goto out;
176628a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
176728a6d671SEric W. Biederman 			goto out;
176828a6d671SEric W. Biederman 		n *= 10;
176928a6d671SEric W. Biederman 		n += c;
177028a6d671SEric W. Biederman 	}
177128a6d671SEric W. Biederman 	return n;
177228a6d671SEric W. Biederman out:
177328a6d671SEric W. Biederman 	return ~0U;
177428a6d671SEric W. Biederman }
177528a6d671SEric W. Biederman 
177627932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
177727932742SMiklos Szeredi 
17783dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
177928a6d671SEric W. Biederman {
178028a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
178128a6d671SEric W. Biederman 	struct files_struct *files = NULL;
178228a6d671SEric W. Biederman 	struct file *file;
178328a6d671SEric W. Biederman 	int fd = proc_fd(inode);
178428a6d671SEric W. Biederman 
178528a6d671SEric W. Biederman 	if (task) {
178628a6d671SEric W. Biederman 		files = get_files_struct(task);
178728a6d671SEric W. Biederman 		put_task_struct(task);
178828a6d671SEric W. Biederman 	}
178928a6d671SEric W. Biederman 	if (files) {
179028a6d671SEric W. Biederman 		/*
179128a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
179228a6d671SEric W. Biederman 		 * hold ->file_lock.
179328a6d671SEric W. Biederman 		 */
179428a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
179528a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
179628a6d671SEric W. Biederman 		if (file) {
17973dcd25f3SJan Blunck 			if (path) {
17983dcd25f3SJan Blunck 				*path = file->f_path;
17993dcd25f3SJan Blunck 				path_get(&file->f_path);
18003dcd25f3SJan Blunck 			}
180127932742SMiklos Szeredi 			if (info)
180227932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
180327932742SMiklos Szeredi 					 "pos:\t%lli\n"
180427932742SMiklos Szeredi 					 "flags:\t0%o\n",
180527932742SMiklos Szeredi 					 (long long) file->f_pos,
180627932742SMiklos Szeredi 					 file->f_flags);
180728a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
180828a6d671SEric W. Biederman 			put_files_struct(files);
180928a6d671SEric W. Biederman 			return 0;
181028a6d671SEric W. Biederman 		}
181128a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
181228a6d671SEric W. Biederman 		put_files_struct(files);
181328a6d671SEric W. Biederman 	}
181428a6d671SEric W. Biederman 	return -ENOENT;
181528a6d671SEric W. Biederman }
181628a6d671SEric W. Biederman 
18173dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path)
181827932742SMiklos Szeredi {
18193dcd25f3SJan Blunck 	return proc_fd_info(inode, path, NULL);
182027932742SMiklos Szeredi }
182127932742SMiklos Szeredi 
182228a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
182328a6d671SEric W. Biederman {
182428a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
182528a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
182628a6d671SEric W. Biederman 	int fd = proc_fd(inode);
182728a6d671SEric W. Biederman 	struct files_struct *files;
1828c69e8d9cSDavid Howells 	const struct cred *cred;
182928a6d671SEric W. Biederman 
183028a6d671SEric W. Biederman 	if (task) {
183128a6d671SEric W. Biederman 		files = get_files_struct(task);
183228a6d671SEric W. Biederman 		if (files) {
183328a6d671SEric W. Biederman 			rcu_read_lock();
183428a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
183528a6d671SEric W. Biederman 				rcu_read_unlock();
183628a6d671SEric W. Biederman 				put_files_struct(files);
183728a6d671SEric W. Biederman 				if (task_dumpable(task)) {
1838c69e8d9cSDavid Howells 					rcu_read_lock();
1839c69e8d9cSDavid Howells 					cred = __task_cred(task);
1840c69e8d9cSDavid Howells 					inode->i_uid = cred->euid;
1841c69e8d9cSDavid Howells 					inode->i_gid = cred->egid;
1842c69e8d9cSDavid Howells 					rcu_read_unlock();
184328a6d671SEric W. Biederman 				} else {
184428a6d671SEric W. Biederman 					inode->i_uid = 0;
184528a6d671SEric W. Biederman 					inode->i_gid = 0;
184628a6d671SEric W. Biederman 				}
184728a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
184828a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
184928a6d671SEric W. Biederman 				put_task_struct(task);
185028a6d671SEric W. Biederman 				return 1;
185128a6d671SEric W. Biederman 			}
185228a6d671SEric W. Biederman 			rcu_read_unlock();
185328a6d671SEric W. Biederman 			put_files_struct(files);
185428a6d671SEric W. Biederman 		}
185528a6d671SEric W. Biederman 		put_task_struct(task);
185628a6d671SEric W. Biederman 	}
185728a6d671SEric W. Biederman 	d_drop(dentry);
185828a6d671SEric W. Biederman 	return 0;
185928a6d671SEric W. Biederman }
186028a6d671SEric W. Biederman 
1861d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations =
186228a6d671SEric W. Biederman {
186328a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
186428a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
186528a6d671SEric W. Biederman };
186628a6d671SEric W. Biederman 
1867444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1868c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
186928a6d671SEric W. Biederman {
1870c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
187128a6d671SEric W. Biederman 	struct file *file;
187228a6d671SEric W. Biederman 	struct files_struct *files;
187328a6d671SEric W. Biederman  	struct inode *inode;
187428a6d671SEric W. Biederman  	struct proc_inode *ei;
1875444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
187628a6d671SEric W. Biederman 
187761a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
187828a6d671SEric W. Biederman 	if (!inode)
187928a6d671SEric W. Biederman 		goto out;
188028a6d671SEric W. Biederman 	ei = PROC_I(inode);
188128a6d671SEric W. Biederman 	ei->fd = fd;
188228a6d671SEric W. Biederman 	files = get_files_struct(task);
188328a6d671SEric W. Biederman 	if (!files)
1884444ceed8SEric W. Biederman 		goto out_iput;
188528a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
188628a6d671SEric W. Biederman 
188728a6d671SEric W. Biederman 	/*
188828a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
188928a6d671SEric W. Biederman 	 * hold ->file_lock.
189028a6d671SEric W. Biederman 	 */
189128a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
189228a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
189328a6d671SEric W. Biederman 	if (!file)
1894444ceed8SEric W. Biederman 		goto out_unlock;
1895aeb5d727SAl Viro 	if (file->f_mode & FMODE_READ)
189628a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
1897aeb5d727SAl Viro 	if (file->f_mode & FMODE_WRITE)
189828a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
189928a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
190028a6d671SEric W. Biederman 	put_files_struct(files);
1901444ceed8SEric W. Biederman 
190228a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
190328a6d671SEric W. Biederman 	inode->i_size = 64;
190428a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
190528a6d671SEric W. Biederman 	dentry->d_op = &tid_fd_dentry_operations;
190628a6d671SEric W. Biederman 	d_add(dentry, inode);
190728a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
190828a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1909444ceed8SEric W. Biederman 		error = NULL;
1910444ceed8SEric W. Biederman 
1911444ceed8SEric W. Biederman  out:
1912444ceed8SEric W. Biederman 	return error;
1913444ceed8SEric W. Biederman out_unlock:
1914444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1915444ceed8SEric W. Biederman 	put_files_struct(files);
1916444ceed8SEric W. Biederman out_iput:
1917444ceed8SEric W. Biederman 	iput(inode);
1918444ceed8SEric W. Biederman 	goto out;
1919444ceed8SEric W. Biederman }
1920444ceed8SEric W. Biederman 
192127932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
192227932742SMiklos Szeredi 					   struct dentry *dentry,
192327932742SMiklos Szeredi 					   instantiate_t instantiate)
1924444ceed8SEric W. Biederman {
1925444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1926444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1927444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1928444ceed8SEric W. Biederman 
1929444ceed8SEric W. Biederman 	if (!task)
1930444ceed8SEric W. Biederman 		goto out_no_task;
1931444ceed8SEric W. Biederman 	if (fd == ~0U)
1932444ceed8SEric W. Biederman 		goto out;
1933444ceed8SEric W. Biederman 
193427932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
193528a6d671SEric W. Biederman out:
193628a6d671SEric W. Biederman 	put_task_struct(task);
193728a6d671SEric W. Biederman out_no_task:
193828a6d671SEric W. Biederman 	return result;
193928a6d671SEric W. Biederman }
194028a6d671SEric W. Biederman 
194127932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
194227932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
19431da177e4SLinus Torvalds {
19442fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
19455634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
194699f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1947457c2510SPavel Emelyanov 	unsigned int fd, ino;
19481da177e4SLinus Torvalds 	int retval;
19491da177e4SLinus Torvalds 	struct files_struct * files;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	retval = -ENOENT;
195299f89551SEric W. Biederman 	if (!p)
195399f89551SEric W. Biederman 		goto out_no_task;
19541da177e4SLinus Torvalds 	retval = 0;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	fd = filp->f_pos;
19571da177e4SLinus Torvalds 	switch (fd) {
19581da177e4SLinus Torvalds 		case 0:
19591da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
19601da177e4SLinus Torvalds 				goto out;
19611da177e4SLinus Torvalds 			filp->f_pos++;
19621da177e4SLinus Torvalds 		case 1:
19635634708bSEric W. Biederman 			ino = parent_ino(dentry);
19641da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
19651da177e4SLinus Torvalds 				goto out;
19661da177e4SLinus Torvalds 			filp->f_pos++;
19671da177e4SLinus Torvalds 		default:
19681da177e4SLinus Torvalds 			files = get_files_struct(p);
19691da177e4SLinus Torvalds 			if (!files)
19701da177e4SLinus Torvalds 				goto out;
1971b835996fSDipankar Sarma 			rcu_read_lock();
19721da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
19739b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
19741da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
197527932742SMiklos Szeredi 				char name[PROC_NUMBUF];
197627932742SMiklos Szeredi 				int len;
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
19791da177e4SLinus Torvalds 					continue;
1980b835996fSDipankar Sarma 				rcu_read_unlock();
19811da177e4SLinus Torvalds 
198227932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
198327932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
198427932742SMiklos Szeredi 						    name, len, instantiate,
198527932742SMiklos Szeredi 						    p, &fd) < 0) {
1986b835996fSDipankar Sarma 					rcu_read_lock();
19871da177e4SLinus Torvalds 					break;
19881da177e4SLinus Torvalds 				}
1989b835996fSDipankar Sarma 				rcu_read_lock();
19901da177e4SLinus Torvalds 			}
1991b835996fSDipankar Sarma 			rcu_read_unlock();
19921da177e4SLinus Torvalds 			put_files_struct(files);
19931da177e4SLinus Torvalds 	}
19941da177e4SLinus Torvalds out:
199599f89551SEric W. Biederman 	put_task_struct(p);
199699f89551SEric W. Biederman out_no_task:
19971da177e4SLinus Torvalds 	return retval;
19981da177e4SLinus Torvalds }
19991da177e4SLinus Torvalds 
200027932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
200127932742SMiklos Szeredi 				    struct nameidata *nd)
200227932742SMiklos Szeredi {
200327932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
200427932742SMiklos Szeredi }
200527932742SMiklos Szeredi 
200627932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
200727932742SMiklos Szeredi {
200827932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
200927932742SMiklos Szeredi }
201027932742SMiklos Szeredi 
201127932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
201227932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
201327932742SMiklos Szeredi {
201427932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
20153dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
201627932742SMiklos Szeredi 	if (!err)
201727932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
201827932742SMiklos Szeredi 	return err;
201927932742SMiklos Szeredi }
202027932742SMiklos Szeredi 
202127932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
202227932742SMiklos Szeredi 	.open           = nonseekable_open,
202327932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
202427932742SMiklos Szeredi };
202527932742SMiklos Szeredi 
202600977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
20271da177e4SLinus Torvalds 	.read		= generic_read_dir,
20281da177e4SLinus Torvalds 	.readdir	= proc_readfd,
20291da177e4SLinus Torvalds };
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds /*
20328948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
20338948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
20348948e11fSAlexey Dobriyan  */
2035e6305c43SAl Viro static int proc_fd_permission(struct inode *inode, int mask)
20368948e11fSAlexey Dobriyan {
20378948e11fSAlexey Dobriyan 	int rv;
20388948e11fSAlexey Dobriyan 
20398948e11fSAlexey Dobriyan 	rv = generic_permission(inode, mask, NULL);
20408948e11fSAlexey Dobriyan 	if (rv == 0)
20418948e11fSAlexey Dobriyan 		return 0;
20428948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
20438948e11fSAlexey Dobriyan 		rv = 0;
20448948e11fSAlexey Dobriyan 	return rv;
20458948e11fSAlexey Dobriyan }
20468948e11fSAlexey Dobriyan 
20478948e11fSAlexey Dobriyan /*
20481da177e4SLinus Torvalds  * proc directories can do almost nothing..
20491da177e4SLinus Torvalds  */
2050c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
20511da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
20528948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
20536d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
20541da177e4SLinus Torvalds };
20551da177e4SLinus Torvalds 
205627932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
205727932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
205827932742SMiklos Szeredi {
205927932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
206027932742SMiklos Szeredi  	struct inode *inode;
206127932742SMiklos Szeredi  	struct proc_inode *ei;
206227932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
206327932742SMiklos Szeredi 
206427932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
206527932742SMiklos Szeredi 	if (!inode)
206627932742SMiklos Szeredi 		goto out;
206727932742SMiklos Szeredi 	ei = PROC_I(inode);
206827932742SMiklos Szeredi 	ei->fd = fd;
206927932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
207027932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
207127932742SMiklos Szeredi 	dentry->d_op = &tid_fd_dentry_operations;
207227932742SMiklos Szeredi 	d_add(dentry, inode);
207327932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
207427932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
207527932742SMiklos Szeredi 		error = NULL;
207627932742SMiklos Szeredi 
207727932742SMiklos Szeredi  out:
207827932742SMiklos Szeredi 	return error;
207927932742SMiklos Szeredi }
208027932742SMiklos Szeredi 
208127932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
208227932742SMiklos Szeredi 					struct dentry *dentry,
208327932742SMiklos Szeredi 					struct nameidata *nd)
208427932742SMiklos Szeredi {
208527932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
208627932742SMiklos Szeredi }
208727932742SMiklos Szeredi 
208827932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
208927932742SMiklos Szeredi {
209027932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
209127932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
209227932742SMiklos Szeredi }
209327932742SMiklos Szeredi 
209427932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
209527932742SMiklos Szeredi 	.read		= generic_read_dir,
209627932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
209727932742SMiklos Szeredi };
209827932742SMiklos Szeredi 
209927932742SMiklos Szeredi /*
210027932742SMiklos Szeredi  * proc directories can do almost nothing..
210127932742SMiklos Szeredi  */
210227932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
210327932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
210427932742SMiklos Szeredi 	.setattr	= proc_setattr,
210527932742SMiklos Szeredi };
210627932742SMiklos Szeredi 
210727932742SMiklos Szeredi 
2108444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
2109c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2110444ceed8SEric W. Biederman {
2111c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2112444ceed8SEric W. Biederman 	struct inode *inode;
2113444ceed8SEric W. Biederman 	struct proc_inode *ei;
2114bd6daba9SKOSAKI Motohiro 	struct dentry *error = ERR_PTR(-ENOENT);
2115444ceed8SEric W. Biederman 
211661a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2117444ceed8SEric W. Biederman 	if (!inode)
2118444ceed8SEric W. Biederman 		goto out;
2119444ceed8SEric W. Biederman 
2120444ceed8SEric W. Biederman 	ei = PROC_I(inode);
2121444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
2122444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2123444ceed8SEric W. Biederman 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
2124444ceed8SEric W. Biederman 	if (p->iop)
2125444ceed8SEric W. Biederman 		inode->i_op = p->iop;
2126444ceed8SEric W. Biederman 	if (p->fop)
2127444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
2128444ceed8SEric W. Biederman 	ei->op = p->op;
2129444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2130444ceed8SEric W. Biederman 	d_add(dentry, inode);
2131444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2132444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2133444ceed8SEric W. Biederman 		error = NULL;
2134444ceed8SEric W. Biederman out:
2135444ceed8SEric W. Biederman 	return error;
2136444ceed8SEric W. Biederman }
2137444ceed8SEric W. Biederman 
21381da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
21391da177e4SLinus Torvalds 					 struct dentry *dentry,
2140c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
21417bcd6b0eSEric W. Biederman 					 unsigned int nents)
21421da177e4SLinus Torvalds {
2143cd6a3ce9SEric W. Biederman 	struct dentry *error;
214499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2145c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
21461da177e4SLinus Torvalds 
2147cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
21481da177e4SLinus Torvalds 
214999f89551SEric W. Biederman 	if (!task)
215099f89551SEric W. Biederman 		goto out_no_task;
21511da177e4SLinus Torvalds 
215220cdc894SEric W. Biederman 	/*
215320cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
215420cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
215520cdc894SEric W. Biederman 	 */
21567bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
21577bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
21581da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
21591da177e4SLinus Torvalds 			continue;
21601da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
21611da177e4SLinus Torvalds 			break;
21621da177e4SLinus Torvalds 	}
21637bcd6b0eSEric W. Biederman 	if (p > last)
21641da177e4SLinus Torvalds 		goto out;
21651da177e4SLinus Torvalds 
2166444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
21671da177e4SLinus Torvalds out:
216899f89551SEric W. Biederman 	put_task_struct(task);
216999f89551SEric W. Biederman out_no_task:
2170cd6a3ce9SEric W. Biederman 	return error;
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds 
2173c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
2174c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
217561a28784SEric W. Biederman {
217661a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
217761a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
217861a28784SEric W. Biederman }
217961a28784SEric W. Biederman 
218028a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
218128a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
2182c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
218328a6d671SEric W. Biederman {
218428a6d671SEric W. Biederman 	int i;
21852fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
218628a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
218728a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2188c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
218928a6d671SEric W. Biederman 	ino_t ino;
219028a6d671SEric W. Biederman 	int ret;
219128a6d671SEric W. Biederman 
219228a6d671SEric W. Biederman 	ret = -ENOENT;
219328a6d671SEric W. Biederman 	if (!task)
219461a28784SEric W. Biederman 		goto out_no_task;
219528a6d671SEric W. Biederman 
219628a6d671SEric W. Biederman 	ret = 0;
219728a6d671SEric W. Biederman 	i = filp->f_pos;
219828a6d671SEric W. Biederman 	switch (i) {
219928a6d671SEric W. Biederman 	case 0:
220028a6d671SEric W. Biederman 		ino = inode->i_ino;
220128a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
220228a6d671SEric W. Biederman 			goto out;
220328a6d671SEric W. Biederman 		i++;
220428a6d671SEric W. Biederman 		filp->f_pos++;
220528a6d671SEric W. Biederman 		/* fall through */
220628a6d671SEric W. Biederman 	case 1:
220728a6d671SEric W. Biederman 		ino = parent_ino(dentry);
220828a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
220928a6d671SEric W. Biederman 			goto out;
221028a6d671SEric W. Biederman 		i++;
221128a6d671SEric W. Biederman 		filp->f_pos++;
221228a6d671SEric W. Biederman 		/* fall through */
221328a6d671SEric W. Biederman 	default:
221428a6d671SEric W. Biederman 		i -= 2;
221528a6d671SEric W. Biederman 		if (i >= nents) {
221628a6d671SEric W. Biederman 			ret = 1;
221728a6d671SEric W. Biederman 			goto out;
221828a6d671SEric W. Biederman 		}
221928a6d671SEric W. Biederman 		p = ents + i;
22207bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
22217bcd6b0eSEric W. Biederman 		while (p <= last) {
222261a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
222328a6d671SEric W. Biederman 				goto out;
222428a6d671SEric W. Biederman 			filp->f_pos++;
222528a6d671SEric W. Biederman 			p++;
222628a6d671SEric W. Biederman 		}
22271da177e4SLinus Torvalds 	}
22281da177e4SLinus Torvalds 
222928a6d671SEric W. Biederman 	ret = 1;
223028a6d671SEric W. Biederman out:
223161a28784SEric W. Biederman 	put_task_struct(task);
223261a28784SEric W. Biederman out_no_task:
223328a6d671SEric W. Biederman 	return ret;
22341da177e4SLinus Torvalds }
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
223728a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
223828a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
223928a6d671SEric W. Biederman {
22402fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
224104ff9708SAl Viro 	char *p = NULL;
224228a6d671SEric W. Biederman 	ssize_t length;
224328a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
224428a6d671SEric W. Biederman 
224528a6d671SEric W. Biederman 	if (!task)
224604ff9708SAl Viro 		return -ESRCH;
224728a6d671SEric W. Biederman 
224828a6d671SEric W. Biederman 	length = security_getprocattr(task,
22492fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
225004ff9708SAl Viro 				      &p);
225128a6d671SEric W. Biederman 	put_task_struct(task);
225204ff9708SAl Viro 	if (length > 0)
225304ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
225404ff9708SAl Viro 	kfree(p);
225528a6d671SEric W. Biederman 	return length;
225628a6d671SEric W. Biederman }
225728a6d671SEric W. Biederman 
225828a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
225928a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
226028a6d671SEric W. Biederman {
22612fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
226228a6d671SEric W. Biederman 	char *page;
226328a6d671SEric W. Biederman 	ssize_t length;
226428a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
226528a6d671SEric W. Biederman 
226628a6d671SEric W. Biederman 	length = -ESRCH;
226728a6d671SEric W. Biederman 	if (!task)
226828a6d671SEric W. Biederman 		goto out_no_task;
226928a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
227028a6d671SEric W. Biederman 		count = PAGE_SIZE;
227128a6d671SEric W. Biederman 
227228a6d671SEric W. Biederman 	/* No partial writes. */
227328a6d671SEric W. Biederman 	length = -EINVAL;
227428a6d671SEric W. Biederman 	if (*ppos != 0)
227528a6d671SEric W. Biederman 		goto out;
227628a6d671SEric W. Biederman 
227728a6d671SEric W. Biederman 	length = -ENOMEM;
2278e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
227928a6d671SEric W. Biederman 	if (!page)
228028a6d671SEric W. Biederman 		goto out;
228128a6d671SEric W. Biederman 
228228a6d671SEric W. Biederman 	length = -EFAULT;
228328a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
228428a6d671SEric W. Biederman 		goto out_free;
228528a6d671SEric W. Biederman 
2286107db7c7SDavid Howells 	/* Guard against adverse ptrace interaction */
2287107db7c7SDavid Howells 	length = mutex_lock_interruptible(&task->cred_guard_mutex);
2288107db7c7SDavid Howells 	if (length < 0)
2289107db7c7SDavid Howells 		goto out_free;
2290107db7c7SDavid Howells 
229128a6d671SEric W. Biederman 	length = security_setprocattr(task,
22922fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
229328a6d671SEric W. Biederman 				      (void*)page, count);
2294107db7c7SDavid Howells 	mutex_unlock(&task->cred_guard_mutex);
229528a6d671SEric W. Biederman out_free:
229628a6d671SEric W. Biederman 	free_page((unsigned long) page);
229728a6d671SEric W. Biederman out:
229828a6d671SEric W. Biederman 	put_task_struct(task);
229928a6d671SEric W. Biederman out_no_task:
230028a6d671SEric W. Biederman 	return length;
230128a6d671SEric W. Biederman }
230228a6d671SEric W. Biederman 
230300977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
230428a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
230528a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
230687df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
230728a6d671SEric W. Biederman };
230828a6d671SEric W. Biederman 
2309c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
2310631f9c18SAlexey Dobriyan 	REG("current",    S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2311631f9c18SAlexey Dobriyan 	REG("prev",       S_IRUGO,	   proc_pid_attr_operations),
2312631f9c18SAlexey Dobriyan 	REG("exec",       S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2313631f9c18SAlexey Dobriyan 	REG("fscreate",   S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2314631f9c18SAlexey Dobriyan 	REG("keycreate",  S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2315631f9c18SAlexey Dobriyan 	REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
231628a6d671SEric W. Biederman };
231728a6d671SEric W. Biederman 
231872d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
23191da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
23201da177e4SLinus Torvalds {
23211da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
232272d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
23231da177e4SLinus Torvalds }
23241da177e4SLinus Torvalds 
232500977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
23261da177e4SLinus Torvalds 	.read		= generic_read_dir,
232772d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
23281da177e4SLinus Torvalds };
23291da177e4SLinus Torvalds 
233072d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
23311da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
23321da177e4SLinus Torvalds {
23337bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
23347bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
23351da177e4SLinus Torvalds }
23361da177e4SLinus Torvalds 
2337c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
233872d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
233999f89551SEric W. Biederman 	.getattr	= pid_getattr,
23406d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
23411da177e4SLinus Torvalds };
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds #endif
23441da177e4SLinus Torvalds 
2345698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
23463cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
23473cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
23483cb4a0bbSKawai, Hidehiro {
23493cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
23503cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
23513cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
23523cb4a0bbSKawai, Hidehiro 	size_t len;
23533cb4a0bbSKawai, Hidehiro 	int ret;
23543cb4a0bbSKawai, Hidehiro 
23553cb4a0bbSKawai, Hidehiro 	if (!task)
23563cb4a0bbSKawai, Hidehiro 		return -ESRCH;
23573cb4a0bbSKawai, Hidehiro 
23583cb4a0bbSKawai, Hidehiro 	ret = 0;
23593cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
23603cb4a0bbSKawai, Hidehiro 	if (mm) {
23613cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
23623cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
23633cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
23643cb4a0bbSKawai, Hidehiro 		mmput(mm);
23653cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
23663cb4a0bbSKawai, Hidehiro 	}
23673cb4a0bbSKawai, Hidehiro 
23683cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
23693cb4a0bbSKawai, Hidehiro 
23703cb4a0bbSKawai, Hidehiro 	return ret;
23713cb4a0bbSKawai, Hidehiro }
23723cb4a0bbSKawai, Hidehiro 
23733cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
23743cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
23753cb4a0bbSKawai, Hidehiro 					  size_t count,
23763cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
23773cb4a0bbSKawai, Hidehiro {
23783cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
23793cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
23803cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
23813cb4a0bbSKawai, Hidehiro 	unsigned int val;
23823cb4a0bbSKawai, Hidehiro 	int ret;
23833cb4a0bbSKawai, Hidehiro 	int i;
23843cb4a0bbSKawai, Hidehiro 	unsigned long mask;
23853cb4a0bbSKawai, Hidehiro 
23863cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
23873cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
23883cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
23893cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
23903cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
23913cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23923cb4a0bbSKawai, Hidehiro 
23933cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
23943cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
23953cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
23963cb4a0bbSKawai, Hidehiro 		end++;
23973cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
23983cb4a0bbSKawai, Hidehiro 		goto out_no_task;
23993cb4a0bbSKawai, Hidehiro 
24003cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
24013cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
24023cb4a0bbSKawai, Hidehiro 	if (!task)
24033cb4a0bbSKawai, Hidehiro 		goto out_no_task;
24043cb4a0bbSKawai, Hidehiro 
24053cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
24063cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
24073cb4a0bbSKawai, Hidehiro 	if (!mm)
24083cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
24093cb4a0bbSKawai, Hidehiro 
24103cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
24113cb4a0bbSKawai, Hidehiro 		if (val & mask)
24123cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
24133cb4a0bbSKawai, Hidehiro 		else
24143cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
24153cb4a0bbSKawai, Hidehiro 	}
24163cb4a0bbSKawai, Hidehiro 
24173cb4a0bbSKawai, Hidehiro 	mmput(mm);
24183cb4a0bbSKawai, Hidehiro  out_no_mm:
24193cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
24203cb4a0bbSKawai, Hidehiro  out_no_task:
24213cb4a0bbSKawai, Hidehiro 	return ret;
24223cb4a0bbSKawai, Hidehiro }
24233cb4a0bbSKawai, Hidehiro 
24243cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
24253cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
24263cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
242787df8424SArnd Bergmann 	.llseek		= generic_file_llseek,
24283cb4a0bbSKawai, Hidehiro };
24293cb4a0bbSKawai, Hidehiro #endif
24303cb4a0bbSKawai, Hidehiro 
24311da177e4SLinus Torvalds /*
24321da177e4SLinus Torvalds  * /proc/self:
24331da177e4SLinus Torvalds  */
24341da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
24351da177e4SLinus Torvalds 			      int buflen)
24361da177e4SLinus Torvalds {
2437488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2438b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
24398578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2440b55fcb22SAndrew Morton 	if (!tgid)
2441488e5bc4SEric W. Biederman 		return -ENOENT;
2442b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
24431da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
24441da177e4SLinus Torvalds }
24451da177e4SLinus Torvalds 
2446008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
24471da177e4SLinus Torvalds {
2448488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2449b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
24507fee4868SAl Viro 	char *name = ERR_PTR(-ENOENT);
24517fee4868SAl Viro 	if (tgid) {
24527fee4868SAl Viro 		name = __getname();
24537fee4868SAl Viro 		if (!name)
24547fee4868SAl Viro 			name = ERR_PTR(-ENOMEM);
24557fee4868SAl Viro 		else
24567fee4868SAl Viro 			sprintf(name, "%d", tgid);
24577fee4868SAl Viro 	}
24587fee4868SAl Viro 	nd_set_link(nd, name);
24597fee4868SAl Viro 	return NULL;
24607fee4868SAl Viro }
24617fee4868SAl Viro 
24627fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
24637fee4868SAl Viro 				void *cookie)
24647fee4868SAl Viro {
24657fee4868SAl Viro 	char *s = nd_get_link(nd);
24667fee4868SAl Viro 	if (!IS_ERR(s))
24677fee4868SAl Viro 		__putname(s);
24681da177e4SLinus Torvalds }
24691da177e4SLinus Torvalds 
2470c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
24711da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
24721da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
24737fee4868SAl Viro 	.put_link	= proc_self_put_link,
24741da177e4SLinus Torvalds };
24751da177e4SLinus Torvalds 
247628a6d671SEric W. Biederman /*
2477801199ceSEric W. Biederman  * proc base
2478801199ceSEric W. Biederman  *
2479801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2480801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2481801199ceSEric W. Biederman  * describe something that is process related.
2482801199ceSEric W. Biederman  */
2483c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
248461a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2485801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2486801199ceSEric W. Biederman };
2487801199ceSEric W. Biederman 
2488801199ceSEric W. Biederman /*
2489801199ceSEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
2490801199ceSEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
2491801199ceSEric W. Biederman  * due to the way we treat inodes.
2492801199ceSEric W. Biederman  */
2493801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2494801199ceSEric W. Biederman {
2495801199ceSEric W. Biederman 	struct inode *inode = dentry->d_inode;
2496801199ceSEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2497801199ceSEric W. Biederman 	if (task) {
2498801199ceSEric W. Biederman 		put_task_struct(task);
2499801199ceSEric W. Biederman 		return 1;
2500801199ceSEric W. Biederman 	}
2501801199ceSEric W. Biederman 	d_drop(dentry);
2502801199ceSEric W. Biederman 	return 0;
2503801199ceSEric W. Biederman }
2504801199ceSEric W. Biederman 
2505d72f71ebSAl Viro static const struct dentry_operations proc_base_dentry_operations =
2506801199ceSEric W. Biederman {
2507801199ceSEric W. Biederman 	.d_revalidate	= proc_base_revalidate,
2508801199ceSEric W. Biederman 	.d_delete	= pid_delete_dentry,
2509801199ceSEric W. Biederman };
2510801199ceSEric W. Biederman 
2511444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2512c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2513801199ceSEric W. Biederman {
2514c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2515801199ceSEric W. Biederman 	struct inode *inode;
2516801199ceSEric W. Biederman 	struct proc_inode *ei;
251773d36460SDan Carpenter 	struct dentry *error;
2518801199ceSEric W. Biederman 
2519801199ceSEric W. Biederman 	/* Allocate the inode */
2520801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2521801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2522801199ceSEric W. Biederman 	if (!inode)
2523801199ceSEric W. Biederman 		goto out;
2524801199ceSEric W. Biederman 
2525801199ceSEric W. Biederman 	/* Initialize the inode */
2526801199ceSEric W. Biederman 	ei = PROC_I(inode);
2527801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2528801199ceSEric W. Biederman 
2529801199ceSEric W. Biederman 	/*
2530801199ceSEric W. Biederman 	 * grab the reference to the task.
2531801199ceSEric W. Biederman 	 */
25321a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2533801199ceSEric W. Biederman 	if (!ei->pid)
2534801199ceSEric W. Biederman 		goto out_iput;
2535801199ceSEric W. Biederman 
2536801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2537801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2538801199ceSEric W. Biederman 		inode->i_nlink = 2;
2539801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2540801199ceSEric W. Biederman 		inode->i_size = 64;
2541801199ceSEric W. Biederman 	if (p->iop)
2542801199ceSEric W. Biederman 		inode->i_op = p->iop;
2543801199ceSEric W. Biederman 	if (p->fop)
2544801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2545801199ceSEric W. Biederman 	ei->op = p->op;
2546801199ceSEric W. Biederman 	dentry->d_op = &proc_base_dentry_operations;
2547801199ceSEric W. Biederman 	d_add(dentry, inode);
2548801199ceSEric W. Biederman 	error = NULL;
2549801199ceSEric W. Biederman out:
2550801199ceSEric W. Biederman 	return error;
2551801199ceSEric W. Biederman out_iput:
2552801199ceSEric W. Biederman 	iput(inode);
2553801199ceSEric W. Biederman 	goto out;
2554801199ceSEric W. Biederman }
2555801199ceSEric W. Biederman 
2556444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2557444ceed8SEric W. Biederman {
2558444ceed8SEric W. Biederman 	struct dentry *error;
2559444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2560c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2561444ceed8SEric W. Biederman 
2562444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2563444ceed8SEric W. Biederman 
2564444ceed8SEric W. Biederman 	if (!task)
2565444ceed8SEric W. Biederman 		goto out_no_task;
2566444ceed8SEric W. Biederman 
2567444ceed8SEric W. Biederman 	/* Lookup the directory entry */
25687bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
25697bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2570444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2571444ceed8SEric W. Biederman 			continue;
2572444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2573444ceed8SEric W. Biederman 			break;
2574444ceed8SEric W. Biederman 	}
25757bcd6b0eSEric W. Biederman 	if (p > last)
2576444ceed8SEric W. Biederman 		goto out;
2577444ceed8SEric W. Biederman 
2578444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2579444ceed8SEric W. Biederman 
2580444ceed8SEric W. Biederman out:
2581444ceed8SEric W. Biederman 	put_task_struct(task);
2582444ceed8SEric W. Biederman out_no_task:
2583444ceed8SEric W. Biederman 	return error;
2584444ceed8SEric W. Biederman }
2585444ceed8SEric W. Biederman 
2586c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2587c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
258861a28784SEric W. Biederman {
258961a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
259061a28784SEric W. Biederman 				proc_base_instantiate, task, p);
259161a28784SEric W. Biederman }
259261a28784SEric W. Biederman 
2593aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2594297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2595aba76fdbSAndrew Morton {
2596940389b8SAndrea Righi 	struct task_io_accounting acct = task->ioac;
2597297c5d92SAndrea Righi 	unsigned long flags;
2598297c5d92SAndrea Righi 
25995995477aSAndrea Righi 	if (whole && lock_task_sighand(task, &flags)) {
2600b2d002dbSAndrea Righi 		struct task_struct *t = task;
2601297c5d92SAndrea Righi 
26025995477aSAndrea Righi 		task_io_accounting_add(&acct, &task->signal->ioac);
26035995477aSAndrea Righi 		while_each_thread(task, t)
26045995477aSAndrea Righi 			task_io_accounting_add(&acct, &t->ioac);
2605297c5d92SAndrea Righi 
2606297c5d92SAndrea Righi 		unlock_task_sighand(task, &flags);
2607297c5d92SAndrea Righi 	}
2608aba76fdbSAndrew Morton 	return sprintf(buffer,
2609aba76fdbSAndrew Morton 			"rchar: %llu\n"
2610aba76fdbSAndrew Morton 			"wchar: %llu\n"
2611aba76fdbSAndrew Morton 			"syscr: %llu\n"
2612aba76fdbSAndrew Morton 			"syscw: %llu\n"
2613aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2614aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2615aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
26167c44319dSAlexander Beregalov 			(unsigned long long)acct.rchar,
26177c44319dSAlexander Beregalov 			(unsigned long long)acct.wchar,
26187c44319dSAlexander Beregalov 			(unsigned long long)acct.syscr,
26197c44319dSAlexander Beregalov 			(unsigned long long)acct.syscw,
26207c44319dSAlexander Beregalov 			(unsigned long long)acct.read_bytes,
26217c44319dSAlexander Beregalov 			(unsigned long long)acct.write_bytes,
26227c44319dSAlexander Beregalov 			(unsigned long long)acct.cancelled_write_bytes);
2623aba76fdbSAndrew Morton }
2624297c5d92SAndrea Righi 
2625297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2626297c5d92SAndrea Righi {
2627297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 0);
2628297c5d92SAndrea Righi }
2629297c5d92SAndrea Righi 
2630297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2631297c5d92SAndrea Righi {
2632297c5d92SAndrea Righi 	return do_io_accounting(task, buffer, 1);
2633297c5d92SAndrea Righi }
2634297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */
2635aba76fdbSAndrew Morton 
263647830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
263747830723SKees Cook 				struct pid *pid, struct task_struct *task)
263847830723SKees Cook {
263947830723SKees Cook 	seq_printf(m, "%08x\n", task->personality);
264047830723SKees Cook 	return 0;
264147830723SKees Cook }
264247830723SKees Cook 
2643801199ceSEric W. Biederman /*
264428a6d671SEric W. Biederman  * Thread groups
264528a6d671SEric W. Biederman  */
264600977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2647c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
264820cdc894SEric W. Biederman 
2649c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
2650631f9c18SAlexey Dobriyan 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2651631f9c18SAlexey Dobriyan 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2652631f9c18SAlexey Dobriyan 	DIR("fdinfo",     S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2653b2211a36SAndrew Morton #ifdef CONFIG_NET
2654631f9c18SAlexey Dobriyan 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2655b2211a36SAndrew Morton #endif
2656631f9c18SAlexey Dobriyan 	REG("environ",    S_IRUSR, proc_environ_operations),
2657631f9c18SAlexey Dobriyan 	INF("auxv",       S_IRUSR, proc_pid_auxv),
2658631f9c18SAlexey Dobriyan 	ONE("status",     S_IRUGO, proc_pid_status),
2659631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
2660631f9c18SAlexey Dobriyan 	INF("limits",	  S_IRUSR, proc_pid_limits),
266143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2662631f9c18SAlexey Dobriyan 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
266343ae34cbSIngo Molnar #endif
26644614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2665ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2666631f9c18SAlexey Dobriyan 	INF("syscall",    S_IRUSR, proc_pid_syscall),
2667ebcb6734SRoland McGrath #endif
2668631f9c18SAlexey Dobriyan 	INF("cmdline",    S_IRUGO, proc_pid_cmdline),
2669631f9c18SAlexey Dobriyan 	ONE("stat",       S_IRUGO, proc_tgid_stat),
2670631f9c18SAlexey Dobriyan 	ONE("statm",      S_IRUGO, proc_pid_statm),
2671631f9c18SAlexey Dobriyan 	REG("maps",       S_IRUGO, proc_maps_operations),
267228a6d671SEric W. Biederman #ifdef CONFIG_NUMA
2673631f9c18SAlexey Dobriyan 	REG("numa_maps",  S_IRUGO, proc_numa_maps_operations),
267428a6d671SEric W. Biederman #endif
2675631f9c18SAlexey Dobriyan 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
2676631f9c18SAlexey Dobriyan 	LNK("cwd",        proc_cwd_link),
2677631f9c18SAlexey Dobriyan 	LNK("root",       proc_root_link),
2678631f9c18SAlexey Dobriyan 	LNK("exe",        proc_exe_link),
2679631f9c18SAlexey Dobriyan 	REG("mounts",     S_IRUGO, proc_mounts_operations),
2680631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
2681631f9c18SAlexey Dobriyan 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
26821e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2683631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2684631f9c18SAlexey Dobriyan 	REG("smaps",      S_IRUGO, proc_smaps_operations),
2685631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
268628a6d671SEric W. Biederman #endif
268728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
2688631f9c18SAlexey Dobriyan 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
268928a6d671SEric W. Biederman #endif
269028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
2691631f9c18SAlexey Dobriyan 	INF("wchan",      S_IRUGO, proc_pid_wchan),
269228a6d671SEric W. Biederman #endif
26932ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
26942ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
269528a6d671SEric W. Biederman #endif
269628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
2697631f9c18SAlexey Dobriyan 	INF("schedstat",  S_IRUGO, proc_pid_schedstat),
269828a6d671SEric W. Biederman #endif
26999745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
2700631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
27019745512cSArjan van de Ven #endif
27028793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
2703631f9c18SAlexey Dobriyan 	REG("cpuset",     S_IRUGO, proc_cpuset_operations),
270428a6d671SEric W. Biederman #endif
2705a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2706631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
2707a424316cSPaul Menage #endif
2708631f9c18SAlexey Dobriyan 	INF("oom_score",  S_IRUGO, proc_oom_score),
2709631f9c18SAlexey Dobriyan 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2710*a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
271128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
2712631f9c18SAlexey Dobriyan 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
2713631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
271428a6d671SEric W. Biederman #endif
2715f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2716631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2717f4f154fdSAkinobu Mita #endif
2718698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE
2719631f9c18SAlexey Dobriyan 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
27203cb4a0bbSKawai, Hidehiro #endif
2721aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2722631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tgid_io_accounting),
2723aba76fdbSAndrew Morton #endif
272428a6d671SEric W. Biederman };
272528a6d671SEric W. Biederman 
272628a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
272728a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
272828a6d671SEric W. Biederman {
272928a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
273028a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
273128a6d671SEric W. Biederman }
273228a6d671SEric W. Biederman 
273300977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
273428a6d671SEric W. Biederman 	.read		= generic_read_dir,
273528a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
273628a6d671SEric W. Biederman };
273728a6d671SEric W. Biederman 
273828a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
27397bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
27407bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
274128a6d671SEric W. Biederman }
274228a6d671SEric W. Biederman 
2743c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
274428a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
274528a6d671SEric W. Biederman 	.getattr	= pid_getattr,
274628a6d671SEric W. Biederman 	.setattr	= proc_setattr,
274728a6d671SEric W. Biederman };
274828a6d671SEric W. Biederman 
274960347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
27501da177e4SLinus Torvalds {
275148e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
27528578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
275348e6484dSEric W. Biederman 	struct qstr name;
27541da177e4SLinus Torvalds 
275548e6484dSEric W. Biederman 	name.name = buf;
275660347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
275760347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
275848e6484dSEric W. Biederman 	if (dentry) {
275948e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
276048e6484dSEric W. Biederman 		d_drop(dentry);
276148e6484dSEric W. Biederman 		dput(dentry);
27621da177e4SLinus Torvalds 	}
27631da177e4SLinus Torvalds 
276448e6484dSEric W. Biederman 	name.name = buf;
276560347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
276660347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
276748e6484dSEric W. Biederman 	if (!leader)
276848e6484dSEric W. Biederman 		goto out;
276948e6484dSEric W. Biederman 
277048e6484dSEric W. Biederman 	name.name = "task";
277148e6484dSEric W. Biederman 	name.len = strlen(name.name);
277248e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
277348e6484dSEric W. Biederman 	if (!dir)
277448e6484dSEric W. Biederman 		goto out_put_leader;
277548e6484dSEric W. Biederman 
277648e6484dSEric W. Biederman 	name.name = buf;
277760347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
277848e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
277948e6484dSEric W. Biederman 	if (dentry) {
278048e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
278148e6484dSEric W. Biederman 		d_drop(dentry);
278248e6484dSEric W. Biederman 		dput(dentry);
27831da177e4SLinus Torvalds 	}
278448e6484dSEric W. Biederman 
278548e6484dSEric W. Biederman 	dput(dir);
278648e6484dSEric W. Biederman out_put_leader:
278748e6484dSEric W. Biederman 	dput(leader);
278848e6484dSEric W. Biederman out:
278948e6484dSEric W. Biederman 	return;
27901da177e4SLinus Torvalds }
27911da177e4SLinus Torvalds 
27920895e91dSRandy Dunlap /**
27930895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
27940895e91dSRandy Dunlap  * @task: task that should be flushed.
27950895e91dSRandy Dunlap  *
27960895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
279760347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
27980895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
27990895e91dSRandy Dunlap  *
28000895e91dSRandy Dunlap  * Looks in the dcache for
28010895e91dSRandy Dunlap  * /proc/@pid
28020895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
28030895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
28040895e91dSRandy Dunlap  * from the dcache.
28050895e91dSRandy Dunlap  *
28060895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
28070895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
28080895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
28090895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
28100895e91dSRandy Dunlap  * dcache entries at process exit time.
28110895e91dSRandy Dunlap  *
28120895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
28130895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
28140895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
281560347f67SPavel Emelyanov  */
281660347f67SPavel Emelyanov 
281760347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
281860347f67SPavel Emelyanov {
28199fcc2d15SEric W. Biederman 	int i;
28209b4d1cbeSOleg Nesterov 	struct pid *pid, *tgid;
2821130f77ecSPavel Emelyanov 	struct upid *upid;
2822130f77ecSPavel Emelyanov 
2823130f77ecSPavel Emelyanov 	pid = task_pid(task);
2824130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
28259fcc2d15SEric W. Biederman 
28269fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2827130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2828130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
28299b4d1cbeSOleg Nesterov 					tgid->numbers[i].nr);
2830130f77ecSPavel Emelyanov 	}
28316f4e6433SPavel Emelyanov 
28326f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
28336f4e6433SPavel Emelyanov 	if (upid->nr == 1)
28346f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
283560347f67SPavel Emelyanov }
283660347f67SPavel Emelyanov 
28379711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
28389711ef99SAdrian Bunk 					   struct dentry * dentry,
2839c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2840444ceed8SEric W. Biederman {
2841444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2842444ceed8SEric W. Biederman 	struct inode *inode;
2843444ceed8SEric W. Biederman 
284461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2845444ceed8SEric W. Biederman 	if (!inode)
2846444ceed8SEric W. Biederman 		goto out;
2847444ceed8SEric W. Biederman 
2848444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2849444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2850444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2851444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
2852aed54175SVegard Nossum 
2853aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2854aed54175SVegard Nossum 		ARRAY_SIZE(tgid_base_stuff));
2855444ceed8SEric W. Biederman 
2856444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2857444ceed8SEric W. Biederman 
2858444ceed8SEric W. Biederman 	d_add(dentry, inode);
2859444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2860444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2861444ceed8SEric W. Biederman 		error = NULL;
2862444ceed8SEric W. Biederman out:
2863444ceed8SEric W. Biederman 	return error;
2864444ceed8SEric W. Biederman }
2865444ceed8SEric W. Biederman 
28661da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
28671da177e4SLinus Torvalds {
286873d36460SDan Carpenter 	struct dentry *result;
28691da177e4SLinus Torvalds 	struct task_struct *task;
28701da177e4SLinus Torvalds 	unsigned tgid;
2871b488893aSPavel Emelyanov 	struct pid_namespace *ns;
28721da177e4SLinus Torvalds 
2873801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
2874801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2875801199ceSEric W. Biederman 		goto out;
2876801199ceSEric W. Biederman 
28771da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
28781da177e4SLinus Torvalds 	if (tgid == ~0U)
28791da177e4SLinus Torvalds 		goto out;
28801da177e4SLinus Torvalds 
2881b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2882de758734SEric W. Biederman 	rcu_read_lock();
2883b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
28841da177e4SLinus Torvalds 	if (task)
28851da177e4SLinus Torvalds 		get_task_struct(task);
2886de758734SEric W. Biederman 	rcu_read_unlock();
28871da177e4SLinus Torvalds 	if (!task)
28881da177e4SLinus Torvalds 		goto out;
28891da177e4SLinus Torvalds 
2890444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
289148e6484dSEric W. Biederman 	put_task_struct(task);
28921da177e4SLinus Torvalds out:
2893cd6a3ce9SEric W. Biederman 	return result;
28941da177e4SLinus Torvalds }
28951da177e4SLinus Torvalds 
28961da177e4SLinus Torvalds /*
28970804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
28980bc58a91SEric W. Biederman  *
28991da177e4SLinus Torvalds  */
290019fd4bb2SEric W. Biederman struct tgid_iter {
290119fd4bb2SEric W. Biederman 	unsigned int tgid;
29020804ef4bSEric W. Biederman 	struct task_struct *task;
290319fd4bb2SEric W. Biederman };
290419fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
290519fd4bb2SEric W. Biederman {
29060804ef4bSEric W. Biederman 	struct pid *pid;
29071da177e4SLinus Torvalds 
290819fd4bb2SEric W. Biederman 	if (iter.task)
290919fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
29100804ef4bSEric W. Biederman 	rcu_read_lock();
29110804ef4bSEric W. Biederman retry:
291219fd4bb2SEric W. Biederman 	iter.task = NULL;
291319fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
29140804ef4bSEric W. Biederman 	if (pid) {
291519fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
291619fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
29170804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
29180804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
29190804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
29200804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
29210804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
29220804ef4bSEric W. Biederman 		 *
29230804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
29240804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
29250804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
29260804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
29270804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
29280bc58a91SEric W. Biederman 		 */
292919fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
293019fd4bb2SEric W. Biederman 			iter.tgid += 1;
29310804ef4bSEric W. Biederman 			goto retry;
293219fd4bb2SEric W. Biederman 		}
293319fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
29341da177e4SLinus Torvalds 	}
2935454cc105SEric W. Biederman 	rcu_read_unlock();
293619fd4bb2SEric W. Biederman 	return iter;
29371da177e4SLinus Torvalds }
29381da177e4SLinus Torvalds 
29397bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
29401da177e4SLinus Torvalds 
294161a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
294219fd4bb2SEric W. Biederman 	struct tgid_iter iter)
294361a28784SEric W. Biederman {
294461a28784SEric W. Biederman 	char name[PROC_NUMBUF];
294519fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
294661a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
294719fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
294861a28784SEric W. Biederman }
294961a28784SEric W. Biederman 
29501da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
29511da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
29521da177e4SLinus Torvalds {
29531da177e4SLinus Torvalds 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
29542fddfeefSJosef "Jeff" Sipek 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
295519fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2956b488893aSPavel Emelyanov 	struct pid_namespace *ns;
29571da177e4SLinus Torvalds 
295861a28784SEric W. Biederman 	if (!reaper)
295961a28784SEric W. Biederman 		goto out_no_task;
296061a28784SEric W. Biederman 
29617bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2962c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
296361a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2964801199ceSEric W. Biederman 			goto out;
29651da177e4SLinus Torvalds 	}
29661da177e4SLinus Torvalds 
2967b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
296819fd4bb2SEric W. Biederman 	iter.task = NULL;
296919fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
297019fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
297119fd4bb2SEric W. Biederman 	     iter.task;
297219fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
297319fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
297419fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
297519fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
29760804ef4bSEric W. Biederman 			goto out;
29771da177e4SLinus Torvalds 		}
29781da177e4SLinus Torvalds 	}
29790804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
29800804ef4bSEric W. Biederman out:
298161a28784SEric W. Biederman 	put_task_struct(reaper);
298261a28784SEric W. Biederman out_no_task:
29831da177e4SLinus Torvalds 	return 0;
29841da177e4SLinus Torvalds }
29851da177e4SLinus Torvalds 
29860bc58a91SEric W. Biederman /*
298728a6d671SEric W. Biederman  * Tasks
298828a6d671SEric W. Biederman  */
2989c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
2990631f9c18SAlexey Dobriyan 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
29913835541dSJerome Marchand 	DIR("fdinfo",    S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2992631f9c18SAlexey Dobriyan 	REG("environ",   S_IRUSR, proc_environ_operations),
2993631f9c18SAlexey Dobriyan 	INF("auxv",      S_IRUSR, proc_pid_auxv),
2994631f9c18SAlexey Dobriyan 	ONE("status",    S_IRUGO, proc_pid_status),
2995631f9c18SAlexey Dobriyan 	ONE("personality", S_IRUSR, proc_pid_personality),
2996631f9c18SAlexey Dobriyan 	INF("limits",	 S_IRUSR, proc_pid_limits),
299743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
2998631f9c18SAlexey Dobriyan 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
299943ae34cbSIngo Molnar #endif
30004614a696Sjohn stultz 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3001ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3002631f9c18SAlexey Dobriyan 	INF("syscall",   S_IRUSR, proc_pid_syscall),
3003ebcb6734SRoland McGrath #endif
3004631f9c18SAlexey Dobriyan 	INF("cmdline",   S_IRUGO, proc_pid_cmdline),
3005631f9c18SAlexey Dobriyan 	ONE("stat",      S_IRUGO, proc_tid_stat),
3006631f9c18SAlexey Dobriyan 	ONE("statm",     S_IRUGO, proc_pid_statm),
3007631f9c18SAlexey Dobriyan 	REG("maps",      S_IRUGO, proc_maps_operations),
300828a6d671SEric W. Biederman #ifdef CONFIG_NUMA
3009631f9c18SAlexey Dobriyan 	REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
301028a6d671SEric W. Biederman #endif
3011631f9c18SAlexey Dobriyan 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3012631f9c18SAlexey Dobriyan 	LNK("cwd",       proc_cwd_link),
3013631f9c18SAlexey Dobriyan 	LNK("root",      proc_root_link),
3014631f9c18SAlexey Dobriyan 	LNK("exe",       proc_exe_link),
3015631f9c18SAlexey Dobriyan 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3016631f9c18SAlexey Dobriyan 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
30171e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
3018631f9c18SAlexey Dobriyan 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3019631f9c18SAlexey Dobriyan 	REG("smaps",     S_IRUGO, proc_smaps_operations),
3020631f9c18SAlexey Dobriyan 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
302128a6d671SEric W. Biederman #endif
302228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
3023631f9c18SAlexey Dobriyan 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
302428a6d671SEric W. Biederman #endif
302528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
3026631f9c18SAlexey Dobriyan 	INF("wchan",     S_IRUGO, proc_pid_wchan),
302728a6d671SEric W. Biederman #endif
30282ec220e2SKen Chen #ifdef CONFIG_STACKTRACE
30292ec220e2SKen Chen 	ONE("stack",      S_IRUSR, proc_pid_stack),
303028a6d671SEric W. Biederman #endif
303128a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
3032631f9c18SAlexey Dobriyan 	INF("schedstat", S_IRUGO, proc_pid_schedstat),
303328a6d671SEric W. Biederman #endif
30349745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3035631f9c18SAlexey Dobriyan 	REG("latency",  S_IRUGO, proc_lstats_operations),
30369745512cSArjan van de Ven #endif
30378793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
3038631f9c18SAlexey Dobriyan 	REG("cpuset",    S_IRUGO, proc_cpuset_operations),
303928a6d671SEric W. Biederman #endif
3040a424316cSPaul Menage #ifdef CONFIG_CGROUPS
3041631f9c18SAlexey Dobriyan 	REG("cgroup",  S_IRUGO, proc_cgroup_operations),
3042a424316cSPaul Menage #endif
3043631f9c18SAlexey Dobriyan 	INF("oom_score", S_IRUGO, proc_oom_score),
3044631f9c18SAlexey Dobriyan 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3045*a63d83f4SDavid Rientjes 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
304628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
3047631f9c18SAlexey Dobriyan 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
3048631f9c18SAlexey Dobriyan 	REG("sessionid",  S_IRUSR, proc_sessionid_operations),
304928a6d671SEric W. Biederman #endif
3050f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
3051631f9c18SAlexey Dobriyan 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3052f4f154fdSAkinobu Mita #endif
3053297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING
3054631f9c18SAlexey Dobriyan 	INF("io",	S_IRUGO, proc_tid_io_accounting),
3055297c5d92SAndrea Righi #endif
305628a6d671SEric W. Biederman };
305728a6d671SEric W. Biederman 
305828a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
305928a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
306028a6d671SEric W. Biederman {
306128a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
306228a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
306328a6d671SEric W. Biederman }
306428a6d671SEric W. Biederman 
306528a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
30667bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
30677bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
306828a6d671SEric W. Biederman }
306928a6d671SEric W. Biederman 
307000977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
307128a6d671SEric W. Biederman 	.read		= generic_read_dir,
307228a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
307328a6d671SEric W. Biederman };
307428a6d671SEric W. Biederman 
3075c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
307628a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
307728a6d671SEric W. Biederman 	.getattr	= pid_getattr,
307828a6d671SEric W. Biederman 	.setattr	= proc_setattr,
307928a6d671SEric W. Biederman };
308028a6d671SEric W. Biederman 
3081444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
3082c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
3083444ceed8SEric W. Biederman {
3084444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
3085444ceed8SEric W. Biederman 	struct inode *inode;
308661a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
3087444ceed8SEric W. Biederman 
3088444ceed8SEric W. Biederman 	if (!inode)
3089444ceed8SEric W. Biederman 		goto out;
3090444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3091444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
3092444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
3093444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
3094aed54175SVegard Nossum 
3095aed54175SVegard Nossum 	inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3096aed54175SVegard Nossum 		ARRAY_SIZE(tid_base_stuff));
3097444ceed8SEric W. Biederman 
3098444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
3099444ceed8SEric W. Biederman 
3100444ceed8SEric W. Biederman 	d_add(dentry, inode);
3101444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
3102444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
3103444ceed8SEric W. Biederman 		error = NULL;
3104444ceed8SEric W. Biederman out:
3105444ceed8SEric W. Biederman 	return error;
3106444ceed8SEric W. Biederman }
3107444ceed8SEric W. Biederman 
310828a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
310928a6d671SEric W. Biederman {
311028a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
311128a6d671SEric W. Biederman 	struct task_struct *task;
311228a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
311328a6d671SEric W. Biederman 	unsigned tid;
3114b488893aSPavel Emelyanov 	struct pid_namespace *ns;
311528a6d671SEric W. Biederman 
311628a6d671SEric W. Biederman 	if (!leader)
311728a6d671SEric W. Biederman 		goto out_no_task;
311828a6d671SEric W. Biederman 
311928a6d671SEric W. Biederman 	tid = name_to_int(dentry);
312028a6d671SEric W. Biederman 	if (tid == ~0U)
312128a6d671SEric W. Biederman 		goto out;
312228a6d671SEric W. Biederman 
3123b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
312428a6d671SEric W. Biederman 	rcu_read_lock();
3125b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
312628a6d671SEric W. Biederman 	if (task)
312728a6d671SEric W. Biederman 		get_task_struct(task);
312828a6d671SEric W. Biederman 	rcu_read_unlock();
312928a6d671SEric W. Biederman 	if (!task)
313028a6d671SEric W. Biederman 		goto out;
3131bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
313228a6d671SEric W. Biederman 		goto out_drop_task;
313328a6d671SEric W. Biederman 
3134444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
313528a6d671SEric W. Biederman out_drop_task:
313628a6d671SEric W. Biederman 	put_task_struct(task);
313728a6d671SEric W. Biederman out:
313828a6d671SEric W. Biederman 	put_task_struct(leader);
313928a6d671SEric W. Biederman out_no_task:
314028a6d671SEric W. Biederman 	return result;
314128a6d671SEric W. Biederman }
314228a6d671SEric W. Biederman 
314328a6d671SEric W. Biederman /*
31440bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
31450bc58a91SEric W. Biederman  *
31460bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
31470bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
31480bc58a91SEric W. Biederman  * directory we have more work todo.
31490bc58a91SEric W. Biederman  *
31500bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
31510bc58a91SEric W. Biederman  *
31520bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
31530bc58a91SEric W. Biederman  * threads past it.
31540bc58a91SEric W. Biederman  */
3155cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
3156b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
31570bc58a91SEric W. Biederman {
3158a872ff0cSOleg Nesterov 	struct task_struct *pos;
31590bc58a91SEric W. Biederman 
3160cc288738SEric W. Biederman 	rcu_read_lock();
31610bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
31620bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
3163b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
3164a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
3165a872ff0cSOleg Nesterov 			goto found;
31660bc58a91SEric W. Biederman 	}
31670bc58a91SEric W. Biederman 
31680bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
31690bc58a91SEric W. Biederman 	pos = NULL;
3170a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
3171a872ff0cSOleg Nesterov 		goto out;
3172a872ff0cSOleg Nesterov 
3173a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
3174a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
3175a872ff0cSOleg Nesterov 	 */
3176a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
3177a872ff0cSOleg Nesterov 		pos = next_thread(pos);
3178a872ff0cSOleg Nesterov 		if (pos == leader) {
3179a872ff0cSOleg Nesterov 			pos = NULL;
3180a872ff0cSOleg Nesterov 			goto out;
3181a872ff0cSOleg Nesterov 		}
3182a872ff0cSOleg Nesterov 	}
3183a872ff0cSOleg Nesterov found:
3184a872ff0cSOleg Nesterov 	get_task_struct(pos);
3185a872ff0cSOleg Nesterov out:
3186cc288738SEric W. Biederman 	rcu_read_unlock();
31870bc58a91SEric W. Biederman 	return pos;
31880bc58a91SEric W. Biederman }
31890bc58a91SEric W. Biederman 
31900bc58a91SEric W. Biederman /*
31910bc58a91SEric W. Biederman  * Find the next thread in the thread list.
31920bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
31930bc58a91SEric W. Biederman  *
31940bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
31950bc58a91SEric W. Biederman  */
31960bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
31970bc58a91SEric W. Biederman {
3198c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
3199cc288738SEric W. Biederman 	rcu_read_lock();
3200c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
32010bc58a91SEric W. Biederman 		pos = next_thread(start);
3202c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
32030bc58a91SEric W. Biederman 			pos = NULL;
3204c1df7fb8SOleg Nesterov 		else
3205c1df7fb8SOleg Nesterov 			get_task_struct(pos);
3206c1df7fb8SOleg Nesterov 	}
3207cc288738SEric W. Biederman 	rcu_read_unlock();
32080bc58a91SEric W. Biederman 	put_task_struct(start);
32090bc58a91SEric W. Biederman 	return pos;
32100bc58a91SEric W. Biederman }
32110bc58a91SEric W. Biederman 
321261a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
321361a28784SEric W. Biederman 	struct task_struct *task, int tid)
321461a28784SEric W. Biederman {
321561a28784SEric W. Biederman 	char name[PROC_NUMBUF];
321661a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
321761a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
321861a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
321961a28784SEric W. Biederman }
322061a28784SEric W. Biederman 
32211da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
32221da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
32231da177e4SLinus Torvalds {
32242fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
32251da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
32267d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
32270bc58a91SEric W. Biederman 	struct task_struct *task;
32281da177e4SLinus Torvalds 	int retval = -ENOENT;
32291da177e4SLinus Torvalds 	ino_t ino;
32300bc58a91SEric W. Biederman 	int tid;
3231b488893aSPavel Emelyanov 	struct pid_namespace *ns;
32321da177e4SLinus Torvalds 
32337d895244SGuillaume Chazarain 	task = get_proc_task(inode);
32347d895244SGuillaume Chazarain 	if (!task)
32357d895244SGuillaume Chazarain 		goto out_no_task;
32367d895244SGuillaume Chazarain 	rcu_read_lock();
32377d895244SGuillaume Chazarain 	if (pid_alive(task)) {
32387d895244SGuillaume Chazarain 		leader = task->group_leader;
32397d895244SGuillaume Chazarain 		get_task_struct(leader);
32407d895244SGuillaume Chazarain 	}
32417d895244SGuillaume Chazarain 	rcu_read_unlock();
32427d895244SGuillaume Chazarain 	put_task_struct(task);
324399f89551SEric W. Biederman 	if (!leader)
324499f89551SEric W. Biederman 		goto out_no_task;
32451da177e4SLinus Torvalds 	retval = 0;
32461da177e4SLinus Torvalds 
3247ee568b25SLinus Torvalds 	switch ((unsigned long)filp->f_pos) {
32481da177e4SLinus Torvalds 	case 0:
32491da177e4SLinus Torvalds 		ino = inode->i_ino;
3250ee6f779bSZhang Le 		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
32511da177e4SLinus Torvalds 			goto out;
3252ee6f779bSZhang Le 		filp->f_pos++;
32531da177e4SLinus Torvalds 		/* fall through */
32541da177e4SLinus Torvalds 	case 1:
32551da177e4SLinus Torvalds 		ino = parent_ino(dentry);
3256ee6f779bSZhang Le 		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
32571da177e4SLinus Torvalds 			goto out;
3258ee6f779bSZhang Le 		filp->f_pos++;
32591da177e4SLinus Torvalds 		/* fall through */
32601da177e4SLinus Torvalds 	}
32611da177e4SLinus Torvalds 
32620bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
32630bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
32640bc58a91SEric W. Biederman 	 */
3265b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
32662b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
32670bc58a91SEric W. Biederman 	filp->f_version = 0;
3268ee6f779bSZhang Le 	for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
32690bc58a91SEric W. Biederman 	     task;
3270ee6f779bSZhang Le 	     task = next_tid(task), filp->f_pos++) {
3271b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
327261a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
32730bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
32740bc58a91SEric W. Biederman 			 * pid for the next readir call */
32752b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
32760bc58a91SEric W. Biederman 			put_task_struct(task);
32771da177e4SLinus Torvalds 			break;
32780bc58a91SEric W. Biederman 		}
32791da177e4SLinus Torvalds 	}
32801da177e4SLinus Torvalds out:
328199f89551SEric W. Biederman 	put_task_struct(leader);
328299f89551SEric W. Biederman out_no_task:
32831da177e4SLinus Torvalds 	return retval;
32841da177e4SLinus Torvalds }
32856e66b52bSEric W. Biederman 
32866e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
32876e66b52bSEric W. Biederman {
32886e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
328999f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
32906e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
32916e66b52bSEric W. Biederman 
329299f89551SEric W. Biederman 	if (p) {
329399f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
329499f89551SEric W. Biederman 		put_task_struct(p);
32956e66b52bSEric W. Biederman 	}
32966e66b52bSEric W. Biederman 
32976e66b52bSEric W. Biederman 	return 0;
32986e66b52bSEric W. Biederman }
329928a6d671SEric W. Biederman 
3300c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
330128a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
330228a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
330328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
330428a6d671SEric W. Biederman };
330528a6d671SEric W. Biederman 
330600977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
330728a6d671SEric W. Biederman 	.read		= generic_read_dir,
330828a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
330928a6d671SEric W. Biederman };
3310