xref: /openbmc/linux/fs/proc/base.c (revision 9f3acc3140444a900ab280de942291959f0f615d)
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>
561da177e4SLinus Torvalds #include <linux/init.h>
5716f7e0feSRandy Dunlap #include <linux/capability.h>
581da177e4SLinus Torvalds #include <linux/file.h>
59*9f3acc31SAl Viro #include <linux/fdtable.h>
601da177e4SLinus Torvalds #include <linux/string.h>
611da177e4SLinus Torvalds #include <linux/seq_file.h>
621da177e4SLinus Torvalds #include <linux/namei.h>
636b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
641da177e4SLinus Torvalds #include <linux/mm.h>
65b835996fSDipankar Sarma #include <linux/rcupdate.h>
661da177e4SLinus Torvalds #include <linux/kallsyms.h>
67d85f50d5SNeil Horman #include <linux/resource.h>
685096add8SKees Cook #include <linux/module.h>
691da177e4SLinus Torvalds #include <linux/mount.h>
701da177e4SLinus Torvalds #include <linux/security.h>
711da177e4SLinus Torvalds #include <linux/ptrace.h>
72a424316cSPaul Menage #include <linux/cgroup.h>
731da177e4SLinus Torvalds #include <linux/cpuset.h>
741da177e4SLinus Torvalds #include <linux/audit.h>
755addc5ddSAl Viro #include <linux/poll.h>
761651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
778ac773b4SAlexey Dobriyan #include <linux/oom.h>
783cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
7960347f67SPavel Emelyanov #include <linux/pid_namespace.h>
801da177e4SLinus Torvalds #include "internal.h"
811da177e4SLinus Torvalds 
820f2fe20fSEric W. Biederman /* NOTE:
830f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
840f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
850f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
860f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
870f2fe20fSEric W. Biederman  *
880f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
890f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
900f2fe20fSEric W. Biederman  */
910f2fe20fSEric W. Biederman 
921da177e4SLinus Torvalds struct pid_entry {
931da177e4SLinus Torvalds 	char *name;
94c5141e6dSEric Dumazet 	int len;
951da177e4SLinus Torvalds 	mode_t mode;
96c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
9700977a59SArjan van de Ven 	const struct file_operations *fop;
9820cdc894SEric W. Biederman 	union proc_op op;
991da177e4SLinus Torvalds };
1001da177e4SLinus Torvalds 
10161a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
10220cdc894SEric W. Biederman 	.name = (NAME),					\
103c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
10420cdc894SEric W. Biederman 	.mode = MODE,					\
10520cdc894SEric W. Biederman 	.iop  = IOP,					\
10620cdc894SEric W. Biederman 	.fop  = FOP,					\
10720cdc894SEric W. Biederman 	.op   = OP,					\
10820cdc894SEric W. Biederman }
10920cdc894SEric W. Biederman 
11061a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE)							\
11161a28784SEric W. Biederman 	NOD(NAME, (S_IFDIR|(MODE)),						\
11220cdc894SEric W. Biederman 		&proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations,	\
11320cdc894SEric W. Biederman 		{} )
11461a28784SEric W. Biederman #define LNK(NAME, OTYPE)					\
11561a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
11620cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
11720cdc894SEric W. Biederman 		{ .proc_get_link = &proc_##OTYPE##_link } )
11861a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE)				\
11961a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), NULL,		\
12020cdc894SEric W. Biederman 		&proc_##OTYPE##_operations, {})
12161a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE)				\
12261a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
12320cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
12420cdc894SEric W. Biederman 		{ .proc_read = &proc_##OTYPE } )
125be614086SEric W. Biederman #define ONE(NAME, MODE, OTYPE)				\
126be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
127be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
128be614086SEric W. Biederman 		{ .proc_show = &proc_##OTYPE } )
1291da177e4SLinus Torvalds 
1305096add8SKees Cook int maps_protect;
1315096add8SKees Cook EXPORT_SYMBOL(maps_protect);
1325096add8SKees Cook 
1330494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct fs_struct *fs;
1360494f6ecSMiklos Szeredi 	task_lock(task);
1370494f6ecSMiklos Szeredi 	fs = task->fs;
1381da177e4SLinus Torvalds 	if(fs)
1391da177e4SLinus Torvalds 		atomic_inc(&fs->count);
1400494f6ecSMiklos Szeredi 	task_unlock(task);
1410494f6ecSMiklos Szeredi 	return fs;
1420494f6ecSMiklos Szeredi }
1430494f6ecSMiklos Szeredi 
14499f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk)
14599f89551SEric W. Biederman {
14699f89551SEric W. Biederman 	/* Must be called with the rcu_read_lock held */
14799f89551SEric W. Biederman 	unsigned long flags;
14899f89551SEric W. Biederman 	int count = 0;
14999f89551SEric W. Biederman 
15099f89551SEric W. Biederman 	if (lock_task_sighand(tsk, &flags)) {
15199f89551SEric W. Biederman 		count = atomic_read(&tsk->signal->count);
15299f89551SEric W. Biederman 		unlock_task_sighand(tsk, &flags);
15399f89551SEric W. Biederman 	}
15499f89551SEric W. Biederman 	return count;
15599f89551SEric W. Biederman }
15699f89551SEric W. Biederman 
1573dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path)
1580494f6ecSMiklos Szeredi {
15999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
16099f89551SEric W. Biederman 	struct fs_struct *fs = NULL;
1610494f6ecSMiklos Szeredi 	int result = -ENOENT;
16299f89551SEric W. Biederman 
16399f89551SEric W. Biederman 	if (task) {
16499f89551SEric W. Biederman 		fs = get_fs_struct(task);
16599f89551SEric W. Biederman 		put_task_struct(task);
16699f89551SEric W. Biederman 	}
1671da177e4SLinus Torvalds 	if (fs) {
1681da177e4SLinus Torvalds 		read_lock(&fs->lock);
1693dcd25f3SJan Blunck 		*path = fs->pwd;
1703dcd25f3SJan Blunck 		path_get(&fs->pwd);
1711da177e4SLinus Torvalds 		read_unlock(&fs->lock);
1721da177e4SLinus Torvalds 		result = 0;
1731da177e4SLinus Torvalds 		put_fs_struct(fs);
1741da177e4SLinus Torvalds 	}
1751da177e4SLinus Torvalds 	return result;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1783dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path)
1791da177e4SLinus Torvalds {
18099f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
18199f89551SEric W. Biederman 	struct fs_struct *fs = NULL;
1821da177e4SLinus Torvalds 	int result = -ENOENT;
18399f89551SEric W. Biederman 
18499f89551SEric W. Biederman 	if (task) {
18599f89551SEric W. Biederman 		fs = get_fs_struct(task);
18699f89551SEric W. Biederman 		put_task_struct(task);
18799f89551SEric W. Biederman 	}
1881da177e4SLinus Torvalds 	if (fs) {
1891da177e4SLinus Torvalds 		read_lock(&fs->lock);
1903dcd25f3SJan Blunck 		*path = fs->root;
1913dcd25f3SJan Blunck 		path_get(&fs->root);
1921da177e4SLinus Torvalds 		read_unlock(&fs->lock);
1931da177e4SLinus Torvalds 		result = 0;
1941da177e4SLinus Torvalds 		put_fs_struct(fs);
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 	return result;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
199638fa202SRoland McGrath /*
200638fa202SRoland McGrath  * Return zero if current may access user memory in @task, -error if not.
201638fa202SRoland McGrath  */
202638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task)
203638fa202SRoland McGrath {
204638fa202SRoland McGrath 	/*
205638fa202SRoland McGrath 	 * A task can always look at itself, in case it chooses
206638fa202SRoland McGrath 	 * to use system calls instead of load instructions.
207638fa202SRoland McGrath 	 */
208638fa202SRoland McGrath 	if (task == current)
209638fa202SRoland McGrath 		return 0;
210638fa202SRoland McGrath 
211638fa202SRoland McGrath 	/*
212638fa202SRoland McGrath 	 * If current is actively ptrace'ing, and would also be
213638fa202SRoland McGrath 	 * permitted to freshly attach with ptrace now, permit it.
214638fa202SRoland McGrath 	 */
215638fa202SRoland McGrath 	if (task->parent == current && (task->ptrace & PT_PTRACED) &&
216638fa202SRoland McGrath 	    task_is_stopped_or_traced(task) &&
217638fa202SRoland McGrath 	    ptrace_may_attach(task))
218638fa202SRoland McGrath 		return 0;
219638fa202SRoland McGrath 
220638fa202SRoland McGrath 	/*
221638fa202SRoland McGrath 	 * Noone else is allowed.
222638fa202SRoland McGrath 	 */
223638fa202SRoland McGrath 	return -EPERM;
224638fa202SRoland McGrath }
2251da177e4SLinus Torvalds 
226831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task)
227831830b5SAl Viro {
228831830b5SAl Viro 	struct mm_struct *mm = get_task_mm(task);
229831830b5SAl Viro 	if (!mm)
230831830b5SAl Viro 		return NULL;
231831830b5SAl Viro 	down_read(&mm->mmap_sem);
232831830b5SAl Viro 	task_lock(task);
233831830b5SAl Viro 	if (task->mm != mm)
234831830b5SAl Viro 		goto out;
235831830b5SAl Viro 	if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
236831830b5SAl Viro 		goto out;
237831830b5SAl Viro 	task_unlock(task);
238831830b5SAl Viro 	return mm;
239831830b5SAl Viro out:
240831830b5SAl Viro 	task_unlock(task);
241831830b5SAl Viro 	up_read(&mm->mmap_sem);
242831830b5SAl Viro 	mmput(mm);
243831830b5SAl Viro 	return NULL;
244831830b5SAl Viro }
245831830b5SAl Viro 
2461da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2471da177e4SLinus Torvalds {
2481da177e4SLinus Torvalds 	int res = 0;
2491da177e4SLinus Torvalds 	unsigned int len;
2501da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2511da177e4SLinus Torvalds 	if (!mm)
2521da177e4SLinus Torvalds 		goto out;
2531da177e4SLinus Torvalds 	if (!mm->arg_end)
2541da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2591da177e4SLinus Torvalds 		len = PAGE_SIZE;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2641da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2651da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2661da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2671da177e4SLinus Torvalds 		if (len < res) {
2681da177e4SLinus Torvalds 		    res = len;
2691da177e4SLinus Torvalds 		} else {
2701da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2711da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2721da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2731da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2741da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2751da177e4SLinus Torvalds 		}
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds out_mm:
2781da177e4SLinus Torvalds 	mmput(mm);
2791da177e4SLinus Torvalds out:
2801da177e4SLinus Torvalds 	return res;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	int res = 0;
2861da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2871da177e4SLinus Torvalds 	if (mm) {
2881da177e4SLinus Torvalds 		unsigned int nwords = 0;
2891da177e4SLinus Torvalds 		do
2901da177e4SLinus Torvalds 			nwords += 2;
2911da177e4SLinus Torvalds 		while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2921da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2931da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2941da177e4SLinus Torvalds 			res = PAGE_SIZE;
2951da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2961da177e4SLinus Torvalds 		mmput(mm);
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 	return res;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
3031da177e4SLinus Torvalds /*
3041da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
3051da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
3061da177e4SLinus Torvalds  */
3071da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
3081da177e4SLinus Torvalds {
309ffb45122SAlexey Dobriyan 	unsigned long wchan;
3109281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	wchan = get_wchan(task);
3131da177e4SLinus Torvalds 
3149d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
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 
3211da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3241da177e4SLinus Torvalds  */
3251da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3261da177e4SLinus Torvalds {
327172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
3281da177e4SLinus Torvalds 			task->sched_info.cpu_time,
3291da177e4SLinus Torvalds 			task->sched_info.run_delay,
3302d72376bSIngo Molnar 			task->sched_info.pcount);
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds #endif
3331da177e4SLinus Torvalds 
3349745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3359745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3369745512cSArjan van de Ven {
3379745512cSArjan van de Ven 	int i;
33813d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
33913d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3409745512cSArjan van de Ven 
34113d77c37SHiroshi Shimamoto 	if (!task)
34213d77c37SHiroshi Shimamoto 		return -ESRCH;
34313d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3449745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
3459745512cSArjan van de Ven 		if (task->latency_record[i].backtrace[0]) {
3469745512cSArjan van de Ven 			int q;
3479745512cSArjan van de Ven 			seq_printf(m, "%i %li %li ",
3489745512cSArjan van de Ven 				task->latency_record[i].count,
3499745512cSArjan van de Ven 				task->latency_record[i].time,
3509745512cSArjan van de Ven 				task->latency_record[i].max);
3519745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
3529745512cSArjan van de Ven 				char sym[KSYM_NAME_LEN];
3539745512cSArjan van de Ven 				char *c;
3549745512cSArjan van de Ven 				if (!task->latency_record[i].backtrace[q])
3559745512cSArjan van de Ven 					break;
3569745512cSArjan van de Ven 				if (task->latency_record[i].backtrace[q] == ULONG_MAX)
3579745512cSArjan van de Ven 					break;
3589745512cSArjan van de Ven 				sprint_symbol(sym, task->latency_record[i].backtrace[q]);
3599745512cSArjan van de Ven 				c = strchr(sym, '+');
3609745512cSArjan van de Ven 				if (c)
3619745512cSArjan van de Ven 					*c = 0;
3629745512cSArjan van de Ven 				seq_printf(m, "%s ", sym);
3639745512cSArjan van de Ven 			}
3649745512cSArjan van de Ven 			seq_printf(m, "\n");
3659745512cSArjan van de Ven 		}
3669745512cSArjan van de Ven 
3679745512cSArjan van de Ven 	}
36813d77c37SHiroshi Shimamoto 	put_task_struct(task);
3699745512cSArjan van de Ven 	return 0;
3709745512cSArjan van de Ven }
3719745512cSArjan van de Ven 
3729745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3739745512cSArjan van de Ven {
37413d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
375d6643d12SHiroshi Shimamoto }
376d6643d12SHiroshi Shimamoto 
3779745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
3789745512cSArjan van de Ven 			    size_t count, loff_t *offs)
3799745512cSArjan van de Ven {
38013d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
3819745512cSArjan van de Ven 
38213d77c37SHiroshi Shimamoto 	if (!task)
38313d77c37SHiroshi Shimamoto 		return -ESRCH;
3849745512cSArjan van de Ven 	clear_all_latency_tracing(task);
38513d77c37SHiroshi Shimamoto 	put_task_struct(task);
3869745512cSArjan van de Ven 
3879745512cSArjan van de Ven 	return count;
3889745512cSArjan van de Ven }
3899745512cSArjan van de Ven 
3909745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
3919745512cSArjan van de Ven 	.open		= lstats_open,
3929745512cSArjan van de Ven 	.read		= seq_read,
3939745512cSArjan van de Ven 	.write		= lstats_write,
3949745512cSArjan van de Ven 	.llseek		= seq_lseek,
39513d77c37SHiroshi Shimamoto 	.release	= single_release,
3969745512cSArjan van de Ven };
3979745512cSArjan van de Ven 
3989745512cSArjan van de Ven #endif
3999745512cSArjan van de Ven 
4001da177e4SLinus Torvalds /* The badness from the OOM killer */
4011da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime);
4021da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	unsigned long points;
4051da177e4SLinus Torvalds 	struct timespec uptime;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&uptime);
40819c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
4091da177e4SLinus Torvalds 	points = badness(task, uptime.tv_sec);
41019c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
4111da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
414d85f50d5SNeil Horman struct limit_names {
415d85f50d5SNeil Horman 	char *name;
416d85f50d5SNeil Horman 	char *unit;
417d85f50d5SNeil Horman };
418d85f50d5SNeil Horman 
419d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
420d85f50d5SNeil Horman 	[RLIMIT_CPU] = {"Max cpu time", "ms"},
421d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
422d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
423d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
424d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
425d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
426d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
427d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
428d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
429d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
430d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
431d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
432d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
433d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
434d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4358808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
436d85f50d5SNeil Horman };
437d85f50d5SNeil Horman 
438d85f50d5SNeil Horman /* Display limits for a process */
439d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
440d85f50d5SNeil Horman {
441d85f50d5SNeil Horman 	unsigned int i;
442d85f50d5SNeil Horman 	int count = 0;
443d85f50d5SNeil Horman 	unsigned long flags;
444d85f50d5SNeil Horman 	char *bufptr = buffer;
445d85f50d5SNeil Horman 
446d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
447d85f50d5SNeil Horman 
448d85f50d5SNeil Horman 	rcu_read_lock();
449d85f50d5SNeil Horman 	if (!lock_task_sighand(task,&flags)) {
450d85f50d5SNeil Horman 		rcu_read_unlock();
451d85f50d5SNeil Horman 		return 0;
452d85f50d5SNeil Horman 	}
453d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
454d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
455d85f50d5SNeil Horman 	rcu_read_unlock();
456d85f50d5SNeil Horman 
457d85f50d5SNeil Horman 	/*
458d85f50d5SNeil Horman 	 * print the file header
459d85f50d5SNeil Horman 	 */
460d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
461d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
462d85f50d5SNeil Horman 
463d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
464d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
465d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
466d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
467d85f50d5SNeil Horman 		else
468d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
469d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
470d85f50d5SNeil Horman 
471d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
472d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
473d85f50d5SNeil Horman 		else
474d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
475d85f50d5SNeil Horman 					 rlim[i].rlim_max);
476d85f50d5SNeil Horman 
477d85f50d5SNeil Horman 		if (lnames[i].unit)
478d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
479d85f50d5SNeil Horman 					 lnames[i].unit);
480d85f50d5SNeil Horman 		else
481d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
482d85f50d5SNeil Horman 	}
483d85f50d5SNeil Horman 
484d85f50d5SNeil Horman 	return count;
485d85f50d5SNeil Horman }
486d85f50d5SNeil Horman 
4871da177e4SLinus Torvalds /************************************************************************/
4881da177e4SLinus Torvalds /*                       Here the fs part begins                        */
4891da177e4SLinus Torvalds /************************************************************************/
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /* permission checks */
492778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
4931da177e4SLinus Torvalds {
494778c1144SEric W. Biederman 	struct task_struct *task;
495778c1144SEric W. Biederman 	int allowed = 0;
496df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
497df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
498df26c40eSEric W. Biederman 	 * information.
499778c1144SEric W. Biederman 	 */
500778c1144SEric W. Biederman 	task = get_proc_task(inode);
501df26c40eSEric W. Biederman 	if (task) {
502778c1144SEric W. Biederman 		allowed = ptrace_may_attach(task);
503778c1144SEric W. Biederman 		put_task_struct(task);
504df26c40eSEric W. Biederman 	}
505778c1144SEric W. Biederman 	return allowed;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5086d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr)
5096d76fa58SLinus Torvalds {
5106d76fa58SLinus Torvalds 	int error;
5116d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
5126d76fa58SLinus Torvalds 
5136d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
5146d76fa58SLinus Torvalds 		return -EPERM;
5156d76fa58SLinus Torvalds 
5166d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
5176d76fa58SLinus Torvalds 	if (!error)
5186d76fa58SLinus Torvalds 		error = inode_setattr(inode, attr);
5196d76fa58SLinus Torvalds 	return error;
5206d76fa58SLinus Torvalds }
5216d76fa58SLinus Torvalds 
522c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5236d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5246d76fa58SLinus Torvalds };
5256d76fa58SLinus Torvalds 
526a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file,
527a1a2c409SMiklos Szeredi 			      const struct seq_operations *op)
5281da177e4SLinus Torvalds {
52999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
530cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
5316b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
532a1a2c409SMiklos Szeredi 	struct fs_struct *fs = NULL;
533a1a2c409SMiklos Szeredi 	struct path root;
5345addc5ddSAl Viro 	struct proc_mounts *p;
5355addc5ddSAl Viro 	int ret = -EINVAL;
5365addc5ddSAl Viro 
53799f89551SEric W. Biederman 	if (task) {
538cf7b708cSPavel Emelyanov 		rcu_read_lock();
539cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
540cf7b708cSPavel Emelyanov 		if (nsp) {
541cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
5426b3286edSKirill Korotaev 			if (ns)
5436b3286edSKirill Korotaev 				get_mnt_ns(ns);
544863c4702SAlexey Dobriyan 		}
545cf7b708cSPavel Emelyanov 		rcu_read_unlock();
546a1a2c409SMiklos Szeredi 		if (ns)
547a1a2c409SMiklos Szeredi 			fs = get_fs_struct(task);
54899f89551SEric W. Biederman 		put_task_struct(task);
54999f89551SEric W. Biederman 	}
5501da177e4SLinus Torvalds 
551a1a2c409SMiklos Szeredi 	if (!ns)
552a1a2c409SMiklos Szeredi 		goto err;
553a1a2c409SMiklos Szeredi 	if (!fs)
554a1a2c409SMiklos Szeredi 		goto err_put_ns;
555a1a2c409SMiklos Szeredi 
556a1a2c409SMiklos Szeredi 	read_lock(&fs->lock);
557a1a2c409SMiklos Szeredi 	root = fs->root;
558a1a2c409SMiklos Szeredi 	path_get(&root);
559a1a2c409SMiklos Szeredi 	read_unlock(&fs->lock);
560a1a2c409SMiklos Szeredi 	put_fs_struct(fs);
561a1a2c409SMiklos Szeredi 
5625addc5ddSAl Viro 	ret = -ENOMEM;
5635addc5ddSAl Viro 	p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
564a1a2c409SMiklos Szeredi 	if (!p)
565a1a2c409SMiklos Szeredi 		goto err_put_path;
566a1a2c409SMiklos Szeredi 
5675addc5ddSAl Viro 	file->private_data = &p->m;
568a1a2c409SMiklos Szeredi 	ret = seq_open(file, op);
569a1a2c409SMiklos Szeredi 	if (ret)
570a1a2c409SMiklos Szeredi 		goto err_free;
571a1a2c409SMiklos Szeredi 
572a1a2c409SMiklos Szeredi 	p->m.private = p;
573a1a2c409SMiklos Szeredi 	p->ns = ns;
574a1a2c409SMiklos Szeredi 	p->root = root;
5756b3286edSKirill Korotaev 	p->event = ns->event;
576a1a2c409SMiklos Szeredi 
5775addc5ddSAl Viro 	return 0;
578a1a2c409SMiklos Szeredi 
579a1a2c409SMiklos Szeredi  err_free:
5805addc5ddSAl Viro 	kfree(p);
581a1a2c409SMiklos Szeredi  err_put_path:
582a1a2c409SMiklos Szeredi 	path_put(&root);
583a1a2c409SMiklos Szeredi  err_put_ns:
5846b3286edSKirill Korotaev 	put_mnt_ns(ns);
585a1a2c409SMiklos Szeredi  err:
5861da177e4SLinus Torvalds 	return ret;
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
5901da177e4SLinus Torvalds {
591a1a2c409SMiklos Szeredi 	struct proc_mounts *p = file->private_data;
592a1a2c409SMiklos Szeredi 	path_put(&p->root);
593a1a2c409SMiklos Szeredi 	put_mnt_ns(p->ns);
5941da177e4SLinus Torvalds 	return seq_release(inode, file);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5975addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
5985addc5ddSAl Viro {
5995addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
600a1a2c409SMiklos Szeredi 	struct mnt_namespace *ns = p->ns;
6015addc5ddSAl Viro 	unsigned res = 0;
6025addc5ddSAl Viro 
6035addc5ddSAl Viro 	poll_wait(file, &ns->poll, wait);
6045addc5ddSAl Viro 
6055addc5ddSAl Viro 	spin_lock(&vfsmount_lock);
6065addc5ddSAl Viro 	if (p->event != ns->event) {
6075addc5ddSAl Viro 		p->event = ns->event;
6085addc5ddSAl Viro 		res = POLLERR;
6095addc5ddSAl Viro 	}
6105addc5ddSAl Viro 	spin_unlock(&vfsmount_lock);
6115addc5ddSAl Viro 
6125addc5ddSAl Viro 	return res;
6135addc5ddSAl Viro }
6145addc5ddSAl Viro 
615a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file)
616a1a2c409SMiklos Szeredi {
617a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mounts_op);
618a1a2c409SMiklos Szeredi }
619a1a2c409SMiklos Szeredi 
62000977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
6211da177e4SLinus Torvalds 	.open		= mounts_open,
6221da177e4SLinus Torvalds 	.read		= seq_read,
6231da177e4SLinus Torvalds 	.llseek		= seq_lseek,
6241da177e4SLinus Torvalds 	.release	= mounts_release,
6255addc5ddSAl Viro 	.poll		= mounts_poll,
6261da177e4SLinus Torvalds };
6271da177e4SLinus Torvalds 
6282d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file)
6292d4d4864SRam Pai {
6302d4d4864SRam Pai 	return mounts_open_common(inode, file, &mountinfo_op);
6312d4d4864SRam Pai }
6322d4d4864SRam Pai 
6332d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = {
6342d4d4864SRam Pai 	.open		= mountinfo_open,
6352d4d4864SRam Pai 	.read		= seq_read,
6362d4d4864SRam Pai 	.llseek		= seq_lseek,
6372d4d4864SRam Pai 	.release	= mounts_release,
6382d4d4864SRam Pai 	.poll		= mounts_poll,
6392d4d4864SRam Pai };
6402d4d4864SRam Pai 
641b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
642b4629fe2SChuck Lever {
643a1a2c409SMiklos Szeredi 	return mounts_open_common(inode, file, &mountstats_op);
644b4629fe2SChuck Lever }
645b4629fe2SChuck Lever 
64600977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
647b4629fe2SChuck Lever 	.open		= mountstats_open,
648b4629fe2SChuck Lever 	.read		= seq_read,
649b4629fe2SChuck Lever 	.llseek		= seq_lseek,
650b4629fe2SChuck Lever 	.release	= mounts_release,
651b4629fe2SChuck Lever };
652b4629fe2SChuck Lever 
6531da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6561da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6571da177e4SLinus Torvalds {
6582fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6591da177e4SLinus Torvalds 	unsigned long page;
6601da177e4SLinus Torvalds 	ssize_t length;
66199f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
66299f89551SEric W. Biederman 
66399f89551SEric W. Biederman 	length = -ESRCH;
66499f89551SEric W. Biederman 	if (!task)
66599f89551SEric W. Biederman 		goto out_no_task;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6681da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
66999f89551SEric W. Biederman 
67099f89551SEric W. Biederman 	length = -ENOMEM;
671e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
67299f89551SEric W. Biederman 		goto out;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	if (length >= 0)
6771da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6781da177e4SLinus Torvalds 	free_page(page);
67999f89551SEric W. Biederman out:
68099f89551SEric W. Biederman 	put_task_struct(task);
68199f89551SEric W. Biederman out_no_task:
6821da177e4SLinus Torvalds 	return length;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
68500977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6861da177e4SLinus Torvalds 	.read		= proc_info_read,
6871da177e4SLinus Torvalds };
6881da177e4SLinus Torvalds 
689be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
690be614086SEric W. Biederman {
691be614086SEric W. Biederman 	struct inode *inode = m->private;
692be614086SEric W. Biederman 	struct pid_namespace *ns;
693be614086SEric W. Biederman 	struct pid *pid;
694be614086SEric W. Biederman 	struct task_struct *task;
695be614086SEric W. Biederman 	int ret;
696be614086SEric W. Biederman 
697be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
698be614086SEric W. Biederman 	pid = proc_pid(inode);
699be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
700be614086SEric W. Biederman 	if (!task)
701be614086SEric W. Biederman 		return -ESRCH;
702be614086SEric W. Biederman 
703be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
704be614086SEric W. Biederman 
705be614086SEric W. Biederman 	put_task_struct(task);
706be614086SEric W. Biederman 	return ret;
707be614086SEric W. Biederman }
708be614086SEric W. Biederman 
709be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
710be614086SEric W. Biederman {
711be614086SEric W. Biederman 	int ret;
712be614086SEric W. Biederman 	ret = single_open(filp, proc_single_show, NULL);
713be614086SEric W. Biederman 	if (!ret) {
714be614086SEric W. Biederman 		struct seq_file *m = filp->private_data;
715be614086SEric W. Biederman 
716be614086SEric W. Biederman 		m->private = inode;
717be614086SEric W. Biederman 	}
718be614086SEric W. Biederman 	return ret;
719be614086SEric W. Biederman }
720be614086SEric W. Biederman 
721be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
722be614086SEric W. Biederman 	.open		= proc_single_open,
723be614086SEric W. Biederman 	.read		= seq_read,
724be614086SEric W. Biederman 	.llseek		= seq_lseek,
725be614086SEric W. Biederman 	.release	= single_release,
726be614086SEric W. Biederman };
727be614086SEric W. Biederman 
7281da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
7311da177e4SLinus Torvalds 	return 0;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
7351da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
7361da177e4SLinus Torvalds {
7372fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7381da177e4SLinus Torvalds 	char *page;
7391da177e4SLinus Torvalds 	unsigned long src = *ppos;
7401da177e4SLinus Torvalds 	int ret = -ESRCH;
7411da177e4SLinus Torvalds 	struct mm_struct *mm;
7421da177e4SLinus Torvalds 
74399f89551SEric W. Biederman 	if (!task)
74499f89551SEric W. Biederman 		goto out_no_task;
74599f89551SEric W. Biederman 
746638fa202SRoland McGrath 	if (check_mem_permission(task))
7471da177e4SLinus Torvalds 		goto out;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	ret = -ENOMEM;
750e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7511da177e4SLinus Torvalds 	if (!page)
7521da177e4SLinus Torvalds 		goto out;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	ret = 0;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	mm = get_task_mm(task);
7571da177e4SLinus Torvalds 	if (!mm)
7581da177e4SLinus Torvalds 		goto out_free;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	ret = -EIO;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
7631da177e4SLinus Torvalds 		goto out_put;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	ret = 0;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	while (count > 0) {
7681da177e4SLinus Torvalds 		int this_len, retval;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
7711da177e4SLinus Torvalds 		retval = access_process_vm(task, src, page, this_len, 0);
772638fa202SRoland McGrath 		if (!retval || check_mem_permission(task)) {
7731da177e4SLinus Torvalds 			if (!ret)
7741da177e4SLinus Torvalds 				ret = -EIO;
7751da177e4SLinus Torvalds 			break;
7761da177e4SLinus Torvalds 		}
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
7791da177e4SLinus Torvalds 			ret = -EFAULT;
7801da177e4SLinus Torvalds 			break;
7811da177e4SLinus Torvalds 		}
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 		ret += retval;
7841da177e4SLinus Torvalds 		src += retval;
7851da177e4SLinus Torvalds 		buf += retval;
7861da177e4SLinus Torvalds 		count -= retval;
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 	*ppos = src;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds out_put:
7911da177e4SLinus Torvalds 	mmput(mm);
7921da177e4SLinus Torvalds out_free:
7931da177e4SLinus Torvalds 	free_page((unsigned long) page);
7941da177e4SLinus Torvalds out:
79599f89551SEric W. Biederman 	put_task_struct(task);
79699f89551SEric W. Biederman out_no_task:
7971da177e4SLinus Torvalds 	return ret;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds #define mem_write NULL
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds #ifndef mem_write
8031da177e4SLinus Torvalds /* This is a security hazard */
80463967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
8051da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
8061da177e4SLinus Torvalds {
807f7ca54f4SFrederik Deweerdt 	int copied;
8081da177e4SLinus Torvalds 	char *page;
8092fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8101da177e4SLinus Torvalds 	unsigned long dst = *ppos;
8111da177e4SLinus Torvalds 
81299f89551SEric W. Biederman 	copied = -ESRCH;
81399f89551SEric W. Biederman 	if (!task)
81499f89551SEric W. Biederman 		goto out_no_task;
8151da177e4SLinus Torvalds 
816638fa202SRoland McGrath 	if (check_mem_permission(task))
81799f89551SEric W. Biederman 		goto out;
81899f89551SEric W. Biederman 
81999f89551SEric W. Biederman 	copied = -ENOMEM;
820e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
8211da177e4SLinus Torvalds 	if (!page)
82299f89551SEric W. Biederman 		goto out;
8231da177e4SLinus Torvalds 
824f7ca54f4SFrederik Deweerdt 	copied = 0;
8251da177e4SLinus Torvalds 	while (count > 0) {
8261da177e4SLinus Torvalds 		int this_len, retval;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8291da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
8301da177e4SLinus Torvalds 			copied = -EFAULT;
8311da177e4SLinus Torvalds 			break;
8321da177e4SLinus Torvalds 		}
8331da177e4SLinus Torvalds 		retval = access_process_vm(task, dst, page, this_len, 1);
8341da177e4SLinus Torvalds 		if (!retval) {
8351da177e4SLinus Torvalds 			if (!copied)
8361da177e4SLinus Torvalds 				copied = -EIO;
8371da177e4SLinus Torvalds 			break;
8381da177e4SLinus Torvalds 		}
8391da177e4SLinus Torvalds 		copied += retval;
8401da177e4SLinus Torvalds 		buf += retval;
8411da177e4SLinus Torvalds 		dst += retval;
8421da177e4SLinus Torvalds 		count -= retval;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 	*ppos = dst;
8451da177e4SLinus Torvalds 	free_page((unsigned long) page);
84699f89551SEric W. Biederman out:
84799f89551SEric W. Biederman 	put_task_struct(task);
84899f89551SEric W. Biederman out_no_task:
8491da177e4SLinus Torvalds 	return copied;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds #endif
8521da177e4SLinus Torvalds 
85385863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	switch (orig) {
8561da177e4SLinus Torvalds 	case 0:
8571da177e4SLinus Torvalds 		file->f_pos = offset;
8581da177e4SLinus Torvalds 		break;
8591da177e4SLinus Torvalds 	case 1:
8601da177e4SLinus Torvalds 		file->f_pos += offset;
8611da177e4SLinus Torvalds 		break;
8621da177e4SLinus Torvalds 	default:
8631da177e4SLinus Torvalds 		return -EINVAL;
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 	force_successful_syscall_return();
8661da177e4SLinus Torvalds 	return file->f_pos;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
86900977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8701da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8711da177e4SLinus Torvalds 	.read		= mem_read,
8721da177e4SLinus Torvalds 	.write		= mem_write,
8731da177e4SLinus Torvalds 	.open		= mem_open,
8741da177e4SLinus Torvalds };
8751da177e4SLinus Torvalds 
876315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
877315e28c8SJames Pearson 			size_t count, loff_t *ppos)
878315e28c8SJames Pearson {
879315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
880315e28c8SJames Pearson 	char *page;
881315e28c8SJames Pearson 	unsigned long src = *ppos;
882315e28c8SJames Pearson 	int ret = -ESRCH;
883315e28c8SJames Pearson 	struct mm_struct *mm;
884315e28c8SJames Pearson 
885315e28c8SJames Pearson 	if (!task)
886315e28c8SJames Pearson 		goto out_no_task;
887315e28c8SJames Pearson 
888315e28c8SJames Pearson 	if (!ptrace_may_attach(task))
889315e28c8SJames Pearson 		goto out;
890315e28c8SJames Pearson 
891315e28c8SJames Pearson 	ret = -ENOMEM;
892315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
893315e28c8SJames Pearson 	if (!page)
894315e28c8SJames Pearson 		goto out;
895315e28c8SJames Pearson 
896315e28c8SJames Pearson 	ret = 0;
897315e28c8SJames Pearson 
898315e28c8SJames Pearson 	mm = get_task_mm(task);
899315e28c8SJames Pearson 	if (!mm)
900315e28c8SJames Pearson 		goto out_free;
901315e28c8SJames Pearson 
902315e28c8SJames Pearson 	while (count > 0) {
903315e28c8SJames Pearson 		int this_len, retval, max_len;
904315e28c8SJames Pearson 
905315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
906315e28c8SJames Pearson 
907315e28c8SJames Pearson 		if (this_len <= 0)
908315e28c8SJames Pearson 			break;
909315e28c8SJames Pearson 
910315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
911315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
912315e28c8SJames Pearson 
913315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
914315e28c8SJames Pearson 			page, this_len, 0);
915315e28c8SJames Pearson 
916315e28c8SJames Pearson 		if (retval <= 0) {
917315e28c8SJames Pearson 			ret = retval;
918315e28c8SJames Pearson 			break;
919315e28c8SJames Pearson 		}
920315e28c8SJames Pearson 
921315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
922315e28c8SJames Pearson 			ret = -EFAULT;
923315e28c8SJames Pearson 			break;
924315e28c8SJames Pearson 		}
925315e28c8SJames Pearson 
926315e28c8SJames Pearson 		ret += retval;
927315e28c8SJames Pearson 		src += retval;
928315e28c8SJames Pearson 		buf += retval;
929315e28c8SJames Pearson 		count -= retval;
930315e28c8SJames Pearson 	}
931315e28c8SJames Pearson 	*ppos = src;
932315e28c8SJames Pearson 
933315e28c8SJames Pearson 	mmput(mm);
934315e28c8SJames Pearson out_free:
935315e28c8SJames Pearson 	free_page((unsigned long) page);
936315e28c8SJames Pearson out:
937315e28c8SJames Pearson 	put_task_struct(task);
938315e28c8SJames Pearson out_no_task:
939315e28c8SJames Pearson 	return ret;
940315e28c8SJames Pearson }
941315e28c8SJames Pearson 
942315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
943315e28c8SJames Pearson 	.read		= environ_read,
944315e28c8SJames Pearson };
945315e28c8SJames Pearson 
9461da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
9471da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9481da177e4SLinus Torvalds {
9492fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
9508578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
9511da177e4SLinus Torvalds 	size_t len;
95299f89551SEric W. Biederman 	int oom_adjust;
9531da177e4SLinus Torvalds 
95499f89551SEric W. Biederman 	if (!task)
95599f89551SEric W. Biederman 		return -ESRCH;
95699f89551SEric W. Biederman 	oom_adjust = task->oomkilladj;
95799f89551SEric W. Biederman 	put_task_struct(task);
95899f89551SEric W. Biederman 
9598578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
9600c28f287SAkinobu Mita 
9610c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
9651da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9661da177e4SLinus Torvalds {
96799f89551SEric W. Biederman 	struct task_struct *task;
9688578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF], *end;
9691da177e4SLinus Torvalds 	int oom_adjust;
9701da177e4SLinus Torvalds 
9718578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
9728578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
9738578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
9741da177e4SLinus Torvalds 	if (copy_from_user(buffer, buf, count))
9751da177e4SLinus Torvalds 		return -EFAULT;
9761da177e4SLinus Torvalds 	oom_adjust = simple_strtol(buffer, &end, 0);
9778ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
9788ac773b4SAlexey Dobriyan 	     oom_adjust != OOM_DISABLE)
9791da177e4SLinus Torvalds 		return -EINVAL;
9801da177e4SLinus Torvalds 	if (*end == '\n')
9811da177e4SLinus Torvalds 		end++;
9822fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
98399f89551SEric W. Biederman 	if (!task)
98499f89551SEric W. Biederman 		return -ESRCH;
9858fb4fc68SGuillem Jover 	if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
9868fb4fc68SGuillem Jover 		put_task_struct(task);
9878fb4fc68SGuillem Jover 		return -EACCES;
9888fb4fc68SGuillem Jover 	}
9891da177e4SLinus Torvalds 	task->oomkilladj = oom_adjust;
99099f89551SEric W. Biederman 	put_task_struct(task);
9911da177e4SLinus Torvalds 	if (end - buffer == 0)
9921da177e4SLinus Torvalds 		return -EIO;
9931da177e4SLinus Torvalds 	return end - buffer;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
99600977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
9971da177e4SLinus Torvalds 	.read		= oom_adjust_read,
9981da177e4SLinus Torvalds 	.write		= oom_adjust_write,
9991da177e4SLinus Torvalds };
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
10021da177e4SLinus Torvalds #define TMPBUFLEN 21
10031da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
10041da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
10051da177e4SLinus Torvalds {
10062fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
100799f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
10081da177e4SLinus Torvalds 	ssize_t length;
10091da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10101da177e4SLinus Torvalds 
101199f89551SEric W. Biederman 	if (!task)
101299f89551SEric W. Biederman 		return -ESRCH;
10131da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
10140c11b942SAl Viro 				audit_get_loginuid(task));
101599f89551SEric W. Biederman 	put_task_struct(task);
10161da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
10201da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
10211da177e4SLinus Torvalds {
10222fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
10231da177e4SLinus Torvalds 	char *page, *tmp;
10241da177e4SLinus Torvalds 	ssize_t length;
10251da177e4SLinus Torvalds 	uid_t loginuid;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
10281da177e4SLinus Torvalds 		return -EPERM;
10291da177e4SLinus Torvalds 
103013b41b09SEric W. Biederman 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
10311da177e4SLinus Torvalds 		return -EPERM;
10321da177e4SLinus Torvalds 
1033e0182909SAl Viro 	if (count >= PAGE_SIZE)
1034e0182909SAl Viro 		count = PAGE_SIZE - 1;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	if (*ppos != 0) {
10371da177e4SLinus Torvalds 		/* No partial writes. */
10381da177e4SLinus Torvalds 		return -EINVAL;
10391da177e4SLinus Torvalds 	}
1040e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
10411da177e4SLinus Torvalds 	if (!page)
10421da177e4SLinus Torvalds 		return -ENOMEM;
10431da177e4SLinus Torvalds 	length = -EFAULT;
10441da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
10451da177e4SLinus Torvalds 		goto out_free_page;
10461da177e4SLinus Torvalds 
1047e0182909SAl Viro 	page[count] = '\0';
10481da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
10491da177e4SLinus Torvalds 	if (tmp == page) {
10501da177e4SLinus Torvalds 		length = -EINVAL;
10511da177e4SLinus Torvalds 		goto out_free_page;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	}
105499f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
10551da177e4SLinus Torvalds 	if (likely(length == 0))
10561da177e4SLinus Torvalds 		length = count;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds out_free_page:
10591da177e4SLinus Torvalds 	free_page((unsigned long) page);
10601da177e4SLinus Torvalds 	return length;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
106300977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
10641da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
10651da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
10661da177e4SLinus Torvalds };
10671e0bd755SEric Paris 
10681e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
10691e0bd755SEric Paris 				  size_t count, loff_t *ppos)
10701e0bd755SEric Paris {
10711e0bd755SEric Paris 	struct inode * inode = file->f_path.dentry->d_inode;
10721e0bd755SEric Paris 	struct task_struct *task = get_proc_task(inode);
10731e0bd755SEric Paris 	ssize_t length;
10741e0bd755SEric Paris 	char tmpbuf[TMPBUFLEN];
10751e0bd755SEric Paris 
10761e0bd755SEric Paris 	if (!task)
10771e0bd755SEric Paris 		return -ESRCH;
10781e0bd755SEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
10791e0bd755SEric Paris 				audit_get_sessionid(task));
10801e0bd755SEric Paris 	put_task_struct(task);
10811e0bd755SEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10821e0bd755SEric Paris }
10831e0bd755SEric Paris 
10841e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = {
10851e0bd755SEric Paris 	.read		= proc_sessionid_read,
10861e0bd755SEric Paris };
10871da177e4SLinus Torvalds #endif
10881da177e4SLinus Torvalds 
1089f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1090f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1091f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1092f4f154fdSAkinobu Mita {
1093f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1094f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1095f4f154fdSAkinobu Mita 	size_t len;
1096f4f154fdSAkinobu Mita 	int make_it_fail;
1097f4f154fdSAkinobu Mita 
1098f4f154fdSAkinobu Mita 	if (!task)
1099f4f154fdSAkinobu Mita 		return -ESRCH;
1100f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1101f4f154fdSAkinobu Mita 	put_task_struct(task);
1102f4f154fdSAkinobu Mita 
1103f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
11040c28f287SAkinobu Mita 
11050c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1106f4f154fdSAkinobu Mita }
1107f4f154fdSAkinobu Mita 
1108f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1109f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1110f4f154fdSAkinobu Mita {
1111f4f154fdSAkinobu Mita 	struct task_struct *task;
1112f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1113f4f154fdSAkinobu Mita 	int make_it_fail;
1114f4f154fdSAkinobu Mita 
1115f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1116f4f154fdSAkinobu Mita 		return -EPERM;
1117f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1118f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1119f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1120f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1121f4f154fdSAkinobu Mita 		return -EFAULT;
1122f4f154fdSAkinobu Mita 	make_it_fail = simple_strtol(buffer, &end, 0);
1123f4f154fdSAkinobu Mita 	if (*end == '\n')
1124f4f154fdSAkinobu Mita 		end++;
1125f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1126f4f154fdSAkinobu Mita 	if (!task)
1127f4f154fdSAkinobu Mita 		return -ESRCH;
1128f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1129f4f154fdSAkinobu Mita 	put_task_struct(task);
1130f4f154fdSAkinobu Mita 	if (end - buffer == 0)
1131f4f154fdSAkinobu Mita 		return -EIO;
1132f4f154fdSAkinobu Mita 	return end - buffer;
1133f4f154fdSAkinobu Mita }
1134f4f154fdSAkinobu Mita 
113500977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1136f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1137f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
1138f4f154fdSAkinobu Mita };
1139f4f154fdSAkinobu Mita #endif
1140f4f154fdSAkinobu Mita 
11419745512cSArjan van de Ven 
114243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
114343ae34cbSIngo Molnar /*
114443ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
114543ae34cbSIngo Molnar  */
114643ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
114743ae34cbSIngo Molnar {
114843ae34cbSIngo Molnar 	struct inode *inode = m->private;
114943ae34cbSIngo Molnar 	struct task_struct *p;
115043ae34cbSIngo Molnar 
115143ae34cbSIngo Molnar 	WARN_ON(!inode);
115243ae34cbSIngo Molnar 
115343ae34cbSIngo Molnar 	p = get_proc_task(inode);
115443ae34cbSIngo Molnar 	if (!p)
115543ae34cbSIngo Molnar 		return -ESRCH;
115643ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
115743ae34cbSIngo Molnar 
115843ae34cbSIngo Molnar 	put_task_struct(p);
115943ae34cbSIngo Molnar 
116043ae34cbSIngo Molnar 	return 0;
116143ae34cbSIngo Molnar }
116243ae34cbSIngo Molnar 
116343ae34cbSIngo Molnar static ssize_t
116443ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
116543ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
116643ae34cbSIngo Molnar {
116743ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
116843ae34cbSIngo Molnar 	struct task_struct *p;
116943ae34cbSIngo Molnar 
117043ae34cbSIngo Molnar 	WARN_ON(!inode);
117143ae34cbSIngo Molnar 
117243ae34cbSIngo Molnar 	p = get_proc_task(inode);
117343ae34cbSIngo Molnar 	if (!p)
117443ae34cbSIngo Molnar 		return -ESRCH;
117543ae34cbSIngo Molnar 	proc_sched_set_task(p);
117643ae34cbSIngo Molnar 
117743ae34cbSIngo Molnar 	put_task_struct(p);
117843ae34cbSIngo Molnar 
117943ae34cbSIngo Molnar 	return count;
118043ae34cbSIngo Molnar }
118143ae34cbSIngo Molnar 
118243ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
118343ae34cbSIngo Molnar {
118443ae34cbSIngo Molnar 	int ret;
118543ae34cbSIngo Molnar 
118643ae34cbSIngo Molnar 	ret = single_open(filp, sched_show, NULL);
118743ae34cbSIngo Molnar 	if (!ret) {
118843ae34cbSIngo Molnar 		struct seq_file *m = filp->private_data;
118943ae34cbSIngo Molnar 
119043ae34cbSIngo Molnar 		m->private = inode;
119143ae34cbSIngo Molnar 	}
119243ae34cbSIngo Molnar 	return ret;
119343ae34cbSIngo Molnar }
119443ae34cbSIngo Molnar 
119543ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
119643ae34cbSIngo Molnar 	.open		= sched_open,
119743ae34cbSIngo Molnar 	.read		= seq_read,
119843ae34cbSIngo Molnar 	.write		= sched_write,
119943ae34cbSIngo Molnar 	.llseek		= seq_lseek,
12005ea473a1SAlexey Dobriyan 	.release	= single_release,
120143ae34cbSIngo Molnar };
120243ae34cbSIngo Molnar 
120343ae34cbSIngo Molnar #endif
120443ae34cbSIngo Molnar 
1205925d1c40SMatt Helsley /*
1206925d1c40SMatt Helsley  * We added or removed a vma mapping the executable. The vmas are only mapped
1207925d1c40SMatt Helsley  * during exec and are not mapped with the mmap system call.
1208925d1c40SMatt Helsley  * Callers must hold down_write() on the mm's mmap_sem for these
1209925d1c40SMatt Helsley  */
1210925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm)
1211925d1c40SMatt Helsley {
1212925d1c40SMatt Helsley 	mm->num_exe_file_vmas++;
1213925d1c40SMatt Helsley }
1214925d1c40SMatt Helsley 
1215925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm)
1216925d1c40SMatt Helsley {
1217925d1c40SMatt Helsley 	mm->num_exe_file_vmas--;
1218925d1c40SMatt Helsley 	if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1219925d1c40SMatt Helsley 		fput(mm->exe_file);
1220925d1c40SMatt Helsley 		mm->exe_file = NULL;
1221925d1c40SMatt Helsley 	}
1222925d1c40SMatt Helsley 
1223925d1c40SMatt Helsley }
1224925d1c40SMatt Helsley 
1225925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1226925d1c40SMatt Helsley {
1227925d1c40SMatt Helsley 	if (new_exe_file)
1228925d1c40SMatt Helsley 		get_file(new_exe_file);
1229925d1c40SMatt Helsley 	if (mm->exe_file)
1230925d1c40SMatt Helsley 		fput(mm->exe_file);
1231925d1c40SMatt Helsley 	mm->exe_file = new_exe_file;
1232925d1c40SMatt Helsley 	mm->num_exe_file_vmas = 0;
1233925d1c40SMatt Helsley }
1234925d1c40SMatt Helsley 
1235925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm)
1236925d1c40SMatt Helsley {
1237925d1c40SMatt Helsley 	struct file *exe_file;
1238925d1c40SMatt Helsley 
1239925d1c40SMatt Helsley 	/* We need mmap_sem to protect against races with removal of
1240925d1c40SMatt Helsley 	 * VM_EXECUTABLE vmas */
1241925d1c40SMatt Helsley 	down_read(&mm->mmap_sem);
1242925d1c40SMatt Helsley 	exe_file = mm->exe_file;
1243925d1c40SMatt Helsley 	if (exe_file)
1244925d1c40SMatt Helsley 		get_file(exe_file);
1245925d1c40SMatt Helsley 	up_read(&mm->mmap_sem);
1246925d1c40SMatt Helsley 	return exe_file;
1247925d1c40SMatt Helsley }
1248925d1c40SMatt Helsley 
1249925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1250925d1c40SMatt Helsley {
1251925d1c40SMatt Helsley 	/* It's safe to write the exe_file pointer without exe_file_lock because
1252925d1c40SMatt Helsley 	 * this is called during fork when the task is not yet in /proc */
1253925d1c40SMatt Helsley 	newmm->exe_file = get_mm_exe_file(oldmm);
1254925d1c40SMatt Helsley }
1255925d1c40SMatt Helsley 
1256925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path)
1257925d1c40SMatt Helsley {
1258925d1c40SMatt Helsley 	struct task_struct *task;
1259925d1c40SMatt Helsley 	struct mm_struct *mm;
1260925d1c40SMatt Helsley 	struct file *exe_file;
1261925d1c40SMatt Helsley 
1262925d1c40SMatt Helsley 	task = get_proc_task(inode);
1263925d1c40SMatt Helsley 	if (!task)
1264925d1c40SMatt Helsley 		return -ENOENT;
1265925d1c40SMatt Helsley 	mm = get_task_mm(task);
1266925d1c40SMatt Helsley 	put_task_struct(task);
1267925d1c40SMatt Helsley 	if (!mm)
1268925d1c40SMatt Helsley 		return -ENOENT;
1269925d1c40SMatt Helsley 	exe_file = get_mm_exe_file(mm);
1270925d1c40SMatt Helsley 	mmput(mm);
1271925d1c40SMatt Helsley 	if (exe_file) {
1272925d1c40SMatt Helsley 		*exe_path = exe_file->f_path;
1273925d1c40SMatt Helsley 		path_get(&exe_file->f_path);
1274925d1c40SMatt Helsley 		fput(exe_file);
1275925d1c40SMatt Helsley 		return 0;
1276925d1c40SMatt Helsley 	} else
1277925d1c40SMatt Helsley 		return -ENOENT;
1278925d1c40SMatt Helsley }
1279925d1c40SMatt Helsley 
1280008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
12811da177e4SLinus Torvalds {
12821da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
12831da177e4SLinus Torvalds 	int error = -EACCES;
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
12861d957f9bSJan Blunck 	path_put(&nd->path);
12871da177e4SLinus Torvalds 
1288778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1289778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
12901da177e4SLinus Torvalds 		goto out;
12911da177e4SLinus Torvalds 
12923dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
12931da177e4SLinus Torvalds 	nd->last_type = LAST_BIND;
12941da177e4SLinus Torvalds out:
1295008b150aSAl Viro 	return ERR_PTR(error);
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12983dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
12991da177e4SLinus Torvalds {
1300e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
13013dcd25f3SJan Blunck 	char *pathname;
13021da177e4SLinus Torvalds 	int len;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	if (!tmp)
13051da177e4SLinus Torvalds 		return -ENOMEM;
13061da177e4SLinus Torvalds 
1307cf28b486SJan Blunck 	pathname = d_path(path, tmp, PAGE_SIZE);
13083dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
13093dcd25f3SJan Blunck 	if (IS_ERR(pathname))
13101da177e4SLinus Torvalds 		goto out;
13113dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	if (len > buflen)
13141da177e4SLinus Torvalds 		len = buflen;
13153dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
13161da177e4SLinus Torvalds 		len = -EFAULT;
13171da177e4SLinus Torvalds  out:
13181da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
13191da177e4SLinus Torvalds 	return len;
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
13231da177e4SLinus Torvalds {
13241da177e4SLinus Torvalds 	int error = -EACCES;
13251da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
13263dcd25f3SJan Blunck 	struct path path;
13271da177e4SLinus Torvalds 
1328778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1329778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
13301da177e4SLinus Torvalds 		goto out;
13311da177e4SLinus Torvalds 
13323dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &path);
13331da177e4SLinus Torvalds 	if (error)
13341da177e4SLinus Torvalds 		goto out;
13351da177e4SLinus Torvalds 
13363dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
13373dcd25f3SJan Blunck 	path_put(&path);
13381da177e4SLinus Torvalds out:
13391da177e4SLinus Torvalds 	return error;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
1342c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
13431da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
13446d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
13456d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
13461da177e4SLinus Torvalds };
13471da177e4SLinus Torvalds 
134828a6d671SEric W. Biederman 
134928a6d671SEric W. Biederman /* building an inode */
135028a6d671SEric W. Biederman 
135128a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
135228a6d671SEric W. Biederman {
135328a6d671SEric W. Biederman 	int dumpable = 0;
135428a6d671SEric W. Biederman 	struct mm_struct *mm;
135528a6d671SEric W. Biederman 
135628a6d671SEric W. Biederman 	task_lock(task);
135728a6d671SEric W. Biederman 	mm = task->mm;
135828a6d671SEric W. Biederman 	if (mm)
13596c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
136028a6d671SEric W. Biederman 	task_unlock(task);
136128a6d671SEric W. Biederman 	if(dumpable == 1)
136228a6d671SEric W. Biederman 		return 1;
136328a6d671SEric W. Biederman 	return 0;
136428a6d671SEric W. Biederman }
136528a6d671SEric W. Biederman 
136628a6d671SEric W. Biederman 
136761a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
136828a6d671SEric W. Biederman {
136928a6d671SEric W. Biederman 	struct inode * inode;
137028a6d671SEric W. Biederman 	struct proc_inode *ei;
137128a6d671SEric W. Biederman 
137228a6d671SEric W. Biederman 	/* We need a new inode */
137328a6d671SEric W. Biederman 
137428a6d671SEric W. Biederman 	inode = new_inode(sb);
137528a6d671SEric W. Biederman 	if (!inode)
137628a6d671SEric W. Biederman 		goto out;
137728a6d671SEric W. Biederman 
137828a6d671SEric W. Biederman 	/* Common stuff */
137928a6d671SEric W. Biederman 	ei = PROC_I(inode);
138028a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
138128a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
138228a6d671SEric W. Biederman 
138328a6d671SEric W. Biederman 	/*
138428a6d671SEric W. Biederman 	 * grab the reference to task.
138528a6d671SEric W. Biederman 	 */
13861a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
138728a6d671SEric W. Biederman 	if (!ei->pid)
138828a6d671SEric W. Biederman 		goto out_unlock;
138928a6d671SEric W. Biederman 
139028a6d671SEric W. Biederman 	inode->i_uid = 0;
139128a6d671SEric W. Biederman 	inode->i_gid = 0;
139228a6d671SEric W. Biederman 	if (task_dumpable(task)) {
139328a6d671SEric W. Biederman 		inode->i_uid = task->euid;
139428a6d671SEric W. Biederman 		inode->i_gid = task->egid;
139528a6d671SEric W. Biederman 	}
139628a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
139728a6d671SEric W. Biederman 
139828a6d671SEric W. Biederman out:
139928a6d671SEric W. Biederman 	return inode;
140028a6d671SEric W. Biederman 
140128a6d671SEric W. Biederman out_unlock:
140228a6d671SEric W. Biederman 	iput(inode);
140328a6d671SEric W. Biederman 	return NULL;
140428a6d671SEric W. Biederman }
140528a6d671SEric W. Biederman 
140628a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
140728a6d671SEric W. Biederman {
140828a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
140928a6d671SEric W. Biederman 	struct task_struct *task;
141028a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
141128a6d671SEric W. Biederman 
141228a6d671SEric W. Biederman 	rcu_read_lock();
141328a6d671SEric W. Biederman 	stat->uid = 0;
141428a6d671SEric W. Biederman 	stat->gid = 0;
141528a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
141628a6d671SEric W. Biederman 	if (task) {
141728a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
141828a6d671SEric W. Biederman 		    task_dumpable(task)) {
141928a6d671SEric W. Biederman 			stat->uid = task->euid;
142028a6d671SEric W. Biederman 			stat->gid = task->egid;
142128a6d671SEric W. Biederman 		}
142228a6d671SEric W. Biederman 	}
142328a6d671SEric W. Biederman 	rcu_read_unlock();
142428a6d671SEric W. Biederman 	return 0;
142528a6d671SEric W. Biederman }
142628a6d671SEric W. Biederman 
142728a6d671SEric W. Biederman /* dentry stuff */
142828a6d671SEric W. Biederman 
142928a6d671SEric W. Biederman /*
143028a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
143128a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
143228a6d671SEric W. Biederman  * due to the way we treat inodes.
143328a6d671SEric W. Biederman  *
143428a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
143528a6d671SEric W. Biederman  * performed a setuid(), etc.
143628a6d671SEric W. Biederman  *
143728a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
143828a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
143928a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
144028a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
144128a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
144228a6d671SEric W. Biederman  * directories.
144328a6d671SEric W. Biederman  */
144428a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
144528a6d671SEric W. Biederman {
144628a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
144728a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
144828a6d671SEric W. Biederman 	if (task) {
144928a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
145028a6d671SEric W. Biederman 		    task_dumpable(task)) {
145128a6d671SEric W. Biederman 			inode->i_uid = task->euid;
145228a6d671SEric W. Biederman 			inode->i_gid = task->egid;
145328a6d671SEric W. Biederman 		} else {
145428a6d671SEric W. Biederman 			inode->i_uid = 0;
145528a6d671SEric W. Biederman 			inode->i_gid = 0;
145628a6d671SEric W. Biederman 		}
145728a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
145828a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
145928a6d671SEric W. Biederman 		put_task_struct(task);
146028a6d671SEric W. Biederman 		return 1;
146128a6d671SEric W. Biederman 	}
146228a6d671SEric W. Biederman 	d_drop(dentry);
146328a6d671SEric W. Biederman 	return 0;
146428a6d671SEric W. Biederman }
146528a6d671SEric W. Biederman 
146628a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry)
146728a6d671SEric W. Biederman {
146828a6d671SEric W. Biederman 	/* Is the task we represent dead?
146928a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
147028a6d671SEric W. Biederman 	 * kill it immediately.
147128a6d671SEric W. Biederman 	 */
147228a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
147328a6d671SEric W. Biederman }
147428a6d671SEric W. Biederman 
147528a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations =
147628a6d671SEric W. Biederman {
147728a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
147828a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
147928a6d671SEric W. Biederman };
148028a6d671SEric W. Biederman 
148128a6d671SEric W. Biederman /* Lookups */
148228a6d671SEric W. Biederman 
1483c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1484c5141e6dSEric Dumazet 				struct task_struct *, const void *);
148561a28784SEric W. Biederman 
14861c0d04c9SEric W. Biederman /*
14871c0d04c9SEric W. Biederman  * Fill a directory entry.
14881c0d04c9SEric W. Biederman  *
14891c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
14901c0d04c9SEric W. Biederman  * file type from dcache entry.
14911c0d04c9SEric W. Biederman  *
14921c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
14931c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
14941c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
14951c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
14961c0d04c9SEric W. Biederman  * by stat.
14971c0d04c9SEric W. Biederman  */
149861a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
149961a28784SEric W. Biederman 	char *name, int len,
1500c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
150161a28784SEric W. Biederman {
15022fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
150361a28784SEric W. Biederman 	struct inode *inode;
150461a28784SEric W. Biederman 	struct qstr qname;
150561a28784SEric W. Biederman 	ino_t ino = 0;
150661a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
150761a28784SEric W. Biederman 
150861a28784SEric W. Biederman 	qname.name = name;
150961a28784SEric W. Biederman 	qname.len  = len;
151061a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
151161a28784SEric W. Biederman 
151261a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
151361a28784SEric W. Biederman 	if (!child) {
151461a28784SEric W. Biederman 		struct dentry *new;
151561a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
151661a28784SEric W. Biederman 		if (new) {
151761a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
151861a28784SEric W. Biederman 			if (child)
151961a28784SEric W. Biederman 				dput(new);
152061a28784SEric W. Biederman 			else
152161a28784SEric W. Biederman 				child = new;
152261a28784SEric W. Biederman 		}
152361a28784SEric W. Biederman 	}
152461a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
152561a28784SEric W. Biederman 		goto end_instantiate;
152661a28784SEric W. Biederman 	inode = child->d_inode;
152761a28784SEric W. Biederman 	if (inode) {
152861a28784SEric W. Biederman 		ino = inode->i_ino;
152961a28784SEric W. Biederman 		type = inode->i_mode >> 12;
153061a28784SEric W. Biederman 	}
153161a28784SEric W. Biederman 	dput(child);
153261a28784SEric W. Biederman end_instantiate:
153361a28784SEric W. Biederman 	if (!ino)
153461a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
153561a28784SEric W. Biederman 	if (!ino)
153661a28784SEric W. Biederman 		ino = 1;
153761a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
153861a28784SEric W. Biederman }
153961a28784SEric W. Biederman 
154028a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
154128a6d671SEric W. Biederman {
154228a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
154328a6d671SEric W. Biederman 	int len = dentry->d_name.len;
154428a6d671SEric W. Biederman 	unsigned n = 0;
154528a6d671SEric W. Biederman 
154628a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
154728a6d671SEric W. Biederman 		goto out;
154828a6d671SEric W. Biederman 	while (len-- > 0) {
154928a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
155028a6d671SEric W. Biederman 		if (c > 9)
155128a6d671SEric W. Biederman 			goto out;
155228a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
155328a6d671SEric W. Biederman 			goto out;
155428a6d671SEric W. Biederman 		n *= 10;
155528a6d671SEric W. Biederman 		n += c;
155628a6d671SEric W. Biederman 	}
155728a6d671SEric W. Biederman 	return n;
155828a6d671SEric W. Biederman out:
155928a6d671SEric W. Biederman 	return ~0U;
156028a6d671SEric W. Biederman }
156128a6d671SEric W. Biederman 
156227932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
156327932742SMiklos Szeredi 
15643dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
156528a6d671SEric W. Biederman {
156628a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
156728a6d671SEric W. Biederman 	struct files_struct *files = NULL;
156828a6d671SEric W. Biederman 	struct file *file;
156928a6d671SEric W. Biederman 	int fd = proc_fd(inode);
157028a6d671SEric W. Biederman 
157128a6d671SEric W. Biederman 	if (task) {
157228a6d671SEric W. Biederman 		files = get_files_struct(task);
157328a6d671SEric W. Biederman 		put_task_struct(task);
157428a6d671SEric W. Biederman 	}
157528a6d671SEric W. Biederman 	if (files) {
157628a6d671SEric W. Biederman 		/*
157728a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
157828a6d671SEric W. Biederman 		 * hold ->file_lock.
157928a6d671SEric W. Biederman 		 */
158028a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
158128a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
158228a6d671SEric W. Biederman 		if (file) {
15833dcd25f3SJan Blunck 			if (path) {
15843dcd25f3SJan Blunck 				*path = file->f_path;
15853dcd25f3SJan Blunck 				path_get(&file->f_path);
15863dcd25f3SJan Blunck 			}
158727932742SMiklos Szeredi 			if (info)
158827932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
158927932742SMiklos Szeredi 					 "pos:\t%lli\n"
159027932742SMiklos Szeredi 					 "flags:\t0%o\n",
159127932742SMiklos Szeredi 					 (long long) file->f_pos,
159227932742SMiklos Szeredi 					 file->f_flags);
159328a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
159428a6d671SEric W. Biederman 			put_files_struct(files);
159528a6d671SEric W. Biederman 			return 0;
159628a6d671SEric W. Biederman 		}
159728a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
159828a6d671SEric W. Biederman 		put_files_struct(files);
159928a6d671SEric W. Biederman 	}
160028a6d671SEric W. Biederman 	return -ENOENT;
160128a6d671SEric W. Biederman }
160228a6d671SEric W. Biederman 
16033dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path)
160427932742SMiklos Szeredi {
16053dcd25f3SJan Blunck 	return proc_fd_info(inode, path, NULL);
160627932742SMiklos Szeredi }
160727932742SMiklos Szeredi 
160828a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
160928a6d671SEric W. Biederman {
161028a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
161128a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
161228a6d671SEric W. Biederman 	int fd = proc_fd(inode);
161328a6d671SEric W. Biederman 	struct files_struct *files;
161428a6d671SEric W. Biederman 
161528a6d671SEric W. Biederman 	if (task) {
161628a6d671SEric W. Biederman 		files = get_files_struct(task);
161728a6d671SEric W. Biederman 		if (files) {
161828a6d671SEric W. Biederman 			rcu_read_lock();
161928a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
162028a6d671SEric W. Biederman 				rcu_read_unlock();
162128a6d671SEric W. Biederman 				put_files_struct(files);
162228a6d671SEric W. Biederman 				if (task_dumpable(task)) {
162328a6d671SEric W. Biederman 					inode->i_uid = task->euid;
162428a6d671SEric W. Biederman 					inode->i_gid = task->egid;
162528a6d671SEric W. Biederman 				} else {
162628a6d671SEric W. Biederman 					inode->i_uid = 0;
162728a6d671SEric W. Biederman 					inode->i_gid = 0;
162828a6d671SEric W. Biederman 				}
162928a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
163028a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
163128a6d671SEric W. Biederman 				put_task_struct(task);
163228a6d671SEric W. Biederman 				return 1;
163328a6d671SEric W. Biederman 			}
163428a6d671SEric W. Biederman 			rcu_read_unlock();
163528a6d671SEric W. Biederman 			put_files_struct(files);
163628a6d671SEric W. Biederman 		}
163728a6d671SEric W. Biederman 		put_task_struct(task);
163828a6d671SEric W. Biederman 	}
163928a6d671SEric W. Biederman 	d_drop(dentry);
164028a6d671SEric W. Biederman 	return 0;
164128a6d671SEric W. Biederman }
164228a6d671SEric W. Biederman 
164328a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations =
164428a6d671SEric W. Biederman {
164528a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
164628a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
164728a6d671SEric W. Biederman };
164828a6d671SEric W. Biederman 
1649444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1650c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
165128a6d671SEric W. Biederman {
1652c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
165328a6d671SEric W. Biederman 	struct file *file;
165428a6d671SEric W. Biederman 	struct files_struct *files;
165528a6d671SEric W. Biederman  	struct inode *inode;
165628a6d671SEric W. Biederman  	struct proc_inode *ei;
1657444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
165828a6d671SEric W. Biederman 
165961a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
166028a6d671SEric W. Biederman 	if (!inode)
166128a6d671SEric W. Biederman 		goto out;
166228a6d671SEric W. Biederman 	ei = PROC_I(inode);
166328a6d671SEric W. Biederman 	ei->fd = fd;
166428a6d671SEric W. Biederman 	files = get_files_struct(task);
166528a6d671SEric W. Biederman 	if (!files)
1666444ceed8SEric W. Biederman 		goto out_iput;
166728a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
166828a6d671SEric W. Biederman 
166928a6d671SEric W. Biederman 	/*
167028a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
167128a6d671SEric W. Biederman 	 * hold ->file_lock.
167228a6d671SEric W. Biederman 	 */
167328a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
167428a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
167528a6d671SEric W. Biederman 	if (!file)
1676444ceed8SEric W. Biederman 		goto out_unlock;
167728a6d671SEric W. Biederman 	if (file->f_mode & 1)
167828a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
167928a6d671SEric W. Biederman 	if (file->f_mode & 2)
168028a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
168128a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
168228a6d671SEric W. Biederman 	put_files_struct(files);
1683444ceed8SEric W. Biederman 
168428a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
168528a6d671SEric W. Biederman 	inode->i_size = 64;
168628a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
168728a6d671SEric W. Biederman 	dentry->d_op = &tid_fd_dentry_operations;
168828a6d671SEric W. Biederman 	d_add(dentry, inode);
168928a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
169028a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1691444ceed8SEric W. Biederman 		error = NULL;
1692444ceed8SEric W. Biederman 
1693444ceed8SEric W. Biederman  out:
1694444ceed8SEric W. Biederman 	return error;
1695444ceed8SEric W. Biederman out_unlock:
1696444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1697444ceed8SEric W. Biederman 	put_files_struct(files);
1698444ceed8SEric W. Biederman out_iput:
1699444ceed8SEric W. Biederman 	iput(inode);
1700444ceed8SEric W. Biederman 	goto out;
1701444ceed8SEric W. Biederman }
1702444ceed8SEric W. Biederman 
170327932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
170427932742SMiklos Szeredi 					   struct dentry *dentry,
170527932742SMiklos Szeredi 					   instantiate_t instantiate)
1706444ceed8SEric W. Biederman {
1707444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1708444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1709444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1710444ceed8SEric W. Biederman 
1711444ceed8SEric W. Biederman 	if (!task)
1712444ceed8SEric W. Biederman 		goto out_no_task;
1713444ceed8SEric W. Biederman 	if (fd == ~0U)
1714444ceed8SEric W. Biederman 		goto out;
1715444ceed8SEric W. Biederman 
171627932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
171728a6d671SEric W. Biederman out:
171828a6d671SEric W. Biederman 	put_task_struct(task);
171928a6d671SEric W. Biederman out_no_task:
172028a6d671SEric W. Biederman 	return result;
172128a6d671SEric W. Biederman }
172228a6d671SEric W. Biederman 
172327932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
172427932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
17251da177e4SLinus Torvalds {
17262fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
17275634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
172899f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1729457c2510SPavel Emelyanov 	unsigned int fd, ino;
17301da177e4SLinus Torvalds 	int retval;
17311da177e4SLinus Torvalds 	struct files_struct * files;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	retval = -ENOENT;
173499f89551SEric W. Biederman 	if (!p)
173599f89551SEric W. Biederman 		goto out_no_task;
17361da177e4SLinus Torvalds 	retval = 0;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	fd = filp->f_pos;
17391da177e4SLinus Torvalds 	switch (fd) {
17401da177e4SLinus Torvalds 		case 0:
17411da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
17421da177e4SLinus Torvalds 				goto out;
17431da177e4SLinus Torvalds 			filp->f_pos++;
17441da177e4SLinus Torvalds 		case 1:
17455634708bSEric W. Biederman 			ino = parent_ino(dentry);
17461da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
17471da177e4SLinus Torvalds 				goto out;
17481da177e4SLinus Torvalds 			filp->f_pos++;
17491da177e4SLinus Torvalds 		default:
17501da177e4SLinus Torvalds 			files = get_files_struct(p);
17511da177e4SLinus Torvalds 			if (!files)
17521da177e4SLinus Torvalds 				goto out;
1753b835996fSDipankar Sarma 			rcu_read_lock();
17541da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
17559b4f526cSAl Viro 			     fd < files_fdtable(files)->max_fds;
17561da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
175727932742SMiklos Szeredi 				char name[PROC_NUMBUF];
175827932742SMiklos Szeredi 				int len;
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
17611da177e4SLinus Torvalds 					continue;
1762b835996fSDipankar Sarma 				rcu_read_unlock();
17631da177e4SLinus Torvalds 
176427932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
176527932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
176627932742SMiklos Szeredi 						    name, len, instantiate,
176727932742SMiklos Szeredi 						    p, &fd) < 0) {
1768b835996fSDipankar Sarma 					rcu_read_lock();
17691da177e4SLinus Torvalds 					break;
17701da177e4SLinus Torvalds 				}
1771b835996fSDipankar Sarma 				rcu_read_lock();
17721da177e4SLinus Torvalds 			}
1773b835996fSDipankar Sarma 			rcu_read_unlock();
17741da177e4SLinus Torvalds 			put_files_struct(files);
17751da177e4SLinus Torvalds 	}
17761da177e4SLinus Torvalds out:
177799f89551SEric W. Biederman 	put_task_struct(p);
177899f89551SEric W. Biederman out_no_task:
17791da177e4SLinus Torvalds 	return retval;
17801da177e4SLinus Torvalds }
17811da177e4SLinus Torvalds 
178227932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
178327932742SMiklos Szeredi 				    struct nameidata *nd)
178427932742SMiklos Szeredi {
178527932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
178627932742SMiklos Szeredi }
178727932742SMiklos Szeredi 
178827932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
178927932742SMiklos Szeredi {
179027932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
179127932742SMiklos Szeredi }
179227932742SMiklos Szeredi 
179327932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
179427932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
179527932742SMiklos Szeredi {
179627932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
17973dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
179827932742SMiklos Szeredi 	if (!err)
179927932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
180027932742SMiklos Szeredi 	return err;
180127932742SMiklos Szeredi }
180227932742SMiklos Szeredi 
180327932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
180427932742SMiklos Szeredi 	.open		= nonseekable_open,
180527932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
180627932742SMiklos Szeredi };
180727932742SMiklos Szeredi 
180800977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
18091da177e4SLinus Torvalds 	.read		= generic_read_dir,
18101da177e4SLinus Torvalds 	.readdir	= proc_readfd,
18111da177e4SLinus Torvalds };
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds /*
18148948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
18158948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
18168948e11fSAlexey Dobriyan  */
18178948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask,
18188948e11fSAlexey Dobriyan 				struct nameidata *nd)
18198948e11fSAlexey Dobriyan {
18208948e11fSAlexey Dobriyan 	int rv;
18218948e11fSAlexey Dobriyan 
18228948e11fSAlexey Dobriyan 	rv = generic_permission(inode, mask, NULL);
18238948e11fSAlexey Dobriyan 	if (rv == 0)
18248948e11fSAlexey Dobriyan 		return 0;
18258948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
18268948e11fSAlexey Dobriyan 		rv = 0;
18278948e11fSAlexey Dobriyan 	return rv;
18288948e11fSAlexey Dobriyan }
18298948e11fSAlexey Dobriyan 
18308948e11fSAlexey Dobriyan /*
18311da177e4SLinus Torvalds  * proc directories can do almost nothing..
18321da177e4SLinus Torvalds  */
1833c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
18341da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
18358948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
18366d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
18371da177e4SLinus Torvalds };
18381da177e4SLinus Torvalds 
183927932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
184027932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
184127932742SMiklos Szeredi {
184227932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
184327932742SMiklos Szeredi  	struct inode *inode;
184427932742SMiklos Szeredi  	struct proc_inode *ei;
184527932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
184627932742SMiklos Szeredi 
184727932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
184827932742SMiklos Szeredi 	if (!inode)
184927932742SMiklos Szeredi 		goto out;
185027932742SMiklos Szeredi 	ei = PROC_I(inode);
185127932742SMiklos Szeredi 	ei->fd = fd;
185227932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
185327932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
185427932742SMiklos Szeredi 	dentry->d_op = &tid_fd_dentry_operations;
185527932742SMiklos Szeredi 	d_add(dentry, inode);
185627932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
185727932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
185827932742SMiklos Szeredi 		error = NULL;
185927932742SMiklos Szeredi 
186027932742SMiklos Szeredi  out:
186127932742SMiklos Szeredi 	return error;
186227932742SMiklos Szeredi }
186327932742SMiklos Szeredi 
186427932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
186527932742SMiklos Szeredi 					struct dentry *dentry,
186627932742SMiklos Szeredi 					struct nameidata *nd)
186727932742SMiklos Szeredi {
186827932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
186927932742SMiklos Szeredi }
187027932742SMiklos Szeredi 
187127932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
187227932742SMiklos Szeredi {
187327932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
187427932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
187527932742SMiklos Szeredi }
187627932742SMiklos Szeredi 
187727932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
187827932742SMiklos Szeredi 	.read		= generic_read_dir,
187927932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
188027932742SMiklos Szeredi };
188127932742SMiklos Szeredi 
188227932742SMiklos Szeredi /*
188327932742SMiklos Szeredi  * proc directories can do almost nothing..
188427932742SMiklos Szeredi  */
188527932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
188627932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
188727932742SMiklos Szeredi 	.setattr	= proc_setattr,
188827932742SMiklos Szeredi };
188927932742SMiklos Szeredi 
189027932742SMiklos Szeredi 
1891444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
1892c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
1893444ceed8SEric W. Biederman {
1894c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
1895444ceed8SEric W. Biederman 	struct inode *inode;
1896444ceed8SEric W. Biederman 	struct proc_inode *ei;
1897444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-EINVAL);
1898444ceed8SEric W. Biederman 
189961a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
1900444ceed8SEric W. Biederman 	if (!inode)
1901444ceed8SEric W. Biederman 		goto out;
1902444ceed8SEric W. Biederman 
1903444ceed8SEric W. Biederman 	ei = PROC_I(inode);
1904444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
1905444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
1906444ceed8SEric W. Biederman 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
1907444ceed8SEric W. Biederman 	if (p->iop)
1908444ceed8SEric W. Biederman 		inode->i_op = p->iop;
1909444ceed8SEric W. Biederman 	if (p->fop)
1910444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
1911444ceed8SEric W. Biederman 	ei->op = p->op;
1912444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
1913444ceed8SEric W. Biederman 	d_add(dentry, inode);
1914444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
1915444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
1916444ceed8SEric W. Biederman 		error = NULL;
1917444ceed8SEric W. Biederman out:
1918444ceed8SEric W. Biederman 	return error;
1919444ceed8SEric W. Biederman }
1920444ceed8SEric W. Biederman 
19211da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
19221da177e4SLinus Torvalds 					 struct dentry *dentry,
1923c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
19247bcd6b0eSEric W. Biederman 					 unsigned int nents)
19251da177e4SLinus Torvalds {
19261da177e4SLinus Torvalds 	struct inode *inode;
1927cd6a3ce9SEric W. Biederman 	struct dentry *error;
192899f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1929c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
19301da177e4SLinus Torvalds 
1931cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
19321da177e4SLinus Torvalds 	inode = NULL;
19331da177e4SLinus Torvalds 
193499f89551SEric W. Biederman 	if (!task)
193599f89551SEric W. Biederman 		goto out_no_task;
19361da177e4SLinus Torvalds 
193720cdc894SEric W. Biederman 	/*
193820cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
193920cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
194020cdc894SEric W. Biederman 	 */
19417bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
19427bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
19431da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
19441da177e4SLinus Torvalds 			continue;
19451da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
19461da177e4SLinus Torvalds 			break;
19471da177e4SLinus Torvalds 	}
19487bcd6b0eSEric W. Biederman 	if (p > last)
19491da177e4SLinus Torvalds 		goto out;
19501da177e4SLinus Torvalds 
1951444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
19521da177e4SLinus Torvalds out:
195399f89551SEric W. Biederman 	put_task_struct(task);
195499f89551SEric W. Biederman out_no_task:
1955cd6a3ce9SEric W. Biederman 	return error;
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds 
1958c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
1959c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
196061a28784SEric W. Biederman {
196161a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
196261a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
196361a28784SEric W. Biederman }
196461a28784SEric W. Biederman 
196528a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
196628a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
1967c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
196828a6d671SEric W. Biederman {
196928a6d671SEric W. Biederman 	int i;
19702fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
197128a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
197228a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1973c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
197428a6d671SEric W. Biederman 	ino_t ino;
197528a6d671SEric W. Biederman 	int ret;
197628a6d671SEric W. Biederman 
197728a6d671SEric W. Biederman 	ret = -ENOENT;
197828a6d671SEric W. Biederman 	if (!task)
197961a28784SEric W. Biederman 		goto out_no_task;
198028a6d671SEric W. Biederman 
198128a6d671SEric W. Biederman 	ret = 0;
198228a6d671SEric W. Biederman 	i = filp->f_pos;
198328a6d671SEric W. Biederman 	switch (i) {
198428a6d671SEric W. Biederman 	case 0:
198528a6d671SEric W. Biederman 		ino = inode->i_ino;
198628a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
198728a6d671SEric W. Biederman 			goto out;
198828a6d671SEric W. Biederman 		i++;
198928a6d671SEric W. Biederman 		filp->f_pos++;
199028a6d671SEric W. Biederman 		/* fall through */
199128a6d671SEric W. Biederman 	case 1:
199228a6d671SEric W. Biederman 		ino = parent_ino(dentry);
199328a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
199428a6d671SEric W. Biederman 			goto out;
199528a6d671SEric W. Biederman 		i++;
199628a6d671SEric W. Biederman 		filp->f_pos++;
199728a6d671SEric W. Biederman 		/* fall through */
199828a6d671SEric W. Biederman 	default:
199928a6d671SEric W. Biederman 		i -= 2;
200028a6d671SEric W. Biederman 		if (i >= nents) {
200128a6d671SEric W. Biederman 			ret = 1;
200228a6d671SEric W. Biederman 			goto out;
200328a6d671SEric W. Biederman 		}
200428a6d671SEric W. Biederman 		p = ents + i;
20057bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
20067bcd6b0eSEric W. Biederman 		while (p <= last) {
200761a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
200828a6d671SEric W. Biederman 				goto out;
200928a6d671SEric W. Biederman 			filp->f_pos++;
201028a6d671SEric W. Biederman 			p++;
201128a6d671SEric W. Biederman 		}
20121da177e4SLinus Torvalds 	}
20131da177e4SLinus Torvalds 
201428a6d671SEric W. Biederman 	ret = 1;
201528a6d671SEric W. Biederman out:
201661a28784SEric W. Biederman 	put_task_struct(task);
201761a28784SEric W. Biederman out_no_task:
201828a6d671SEric W. Biederman 	return ret;
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
202228a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
202328a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
202428a6d671SEric W. Biederman {
20252fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
202604ff9708SAl Viro 	char *p = NULL;
202728a6d671SEric W. Biederman 	ssize_t length;
202828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
202928a6d671SEric W. Biederman 
203028a6d671SEric W. Biederman 	if (!task)
203104ff9708SAl Viro 		return -ESRCH;
203228a6d671SEric W. Biederman 
203328a6d671SEric W. Biederman 	length = security_getprocattr(task,
20342fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
203504ff9708SAl Viro 				      &p);
203628a6d671SEric W. Biederman 	put_task_struct(task);
203704ff9708SAl Viro 	if (length > 0)
203804ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
203904ff9708SAl Viro 	kfree(p);
204028a6d671SEric W. Biederman 	return length;
204128a6d671SEric W. Biederman }
204228a6d671SEric W. Biederman 
204328a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
204428a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
204528a6d671SEric W. Biederman {
20462fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
204728a6d671SEric W. Biederman 	char *page;
204828a6d671SEric W. Biederman 	ssize_t length;
204928a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
205028a6d671SEric W. Biederman 
205128a6d671SEric W. Biederman 	length = -ESRCH;
205228a6d671SEric W. Biederman 	if (!task)
205328a6d671SEric W. Biederman 		goto out_no_task;
205428a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
205528a6d671SEric W. Biederman 		count = PAGE_SIZE;
205628a6d671SEric W. Biederman 
205728a6d671SEric W. Biederman 	/* No partial writes. */
205828a6d671SEric W. Biederman 	length = -EINVAL;
205928a6d671SEric W. Biederman 	if (*ppos != 0)
206028a6d671SEric W. Biederman 		goto out;
206128a6d671SEric W. Biederman 
206228a6d671SEric W. Biederman 	length = -ENOMEM;
2063e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
206428a6d671SEric W. Biederman 	if (!page)
206528a6d671SEric W. Biederman 		goto out;
206628a6d671SEric W. Biederman 
206728a6d671SEric W. Biederman 	length = -EFAULT;
206828a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
206928a6d671SEric W. Biederman 		goto out_free;
207028a6d671SEric W. Biederman 
207128a6d671SEric W. Biederman 	length = security_setprocattr(task,
20722fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
207328a6d671SEric W. Biederman 				      (void*)page, count);
207428a6d671SEric W. Biederman out_free:
207528a6d671SEric W. Biederman 	free_page((unsigned long) page);
207628a6d671SEric W. Biederman out:
207728a6d671SEric W. Biederman 	put_task_struct(task);
207828a6d671SEric W. Biederman out_no_task:
207928a6d671SEric W. Biederman 	return length;
208028a6d671SEric W. Biederman }
208128a6d671SEric W. Biederman 
208200977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
208328a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
208428a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
208528a6d671SEric W. Biederman };
208628a6d671SEric W. Biederman 
2087c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
208861a28784SEric W. Biederman 	REG("current",    S_IRUGO|S_IWUGO, pid_attr),
208961a28784SEric W. Biederman 	REG("prev",       S_IRUGO,	   pid_attr),
209061a28784SEric W. Biederman 	REG("exec",       S_IRUGO|S_IWUGO, pid_attr),
209161a28784SEric W. Biederman 	REG("fscreate",   S_IRUGO|S_IWUGO, pid_attr),
209261a28784SEric W. Biederman 	REG("keycreate",  S_IRUGO|S_IWUGO, pid_attr),
209361a28784SEric W. Biederman 	REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
209428a6d671SEric W. Biederman };
209528a6d671SEric W. Biederman 
209672d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
20971da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
20981da177e4SLinus Torvalds {
20991da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
210072d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
21011da177e4SLinus Torvalds }
21021da177e4SLinus Torvalds 
210300977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
21041da177e4SLinus Torvalds 	.read		= generic_read_dir,
210572d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
21061da177e4SLinus Torvalds };
21071da177e4SLinus Torvalds 
210872d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
21091da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
21101da177e4SLinus Torvalds {
21117bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
21127bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
2115c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
211672d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
211799f89551SEric W. Biederman 	.getattr	= pid_getattr,
21186d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
21191da177e4SLinus Torvalds };
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds #endif
21221da177e4SLinus Torvalds 
21233cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
21243cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
21253cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
21263cb4a0bbSKawai, Hidehiro {
21273cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
21283cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
21293cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
21303cb4a0bbSKawai, Hidehiro 	size_t len;
21313cb4a0bbSKawai, Hidehiro 	int ret;
21323cb4a0bbSKawai, Hidehiro 
21333cb4a0bbSKawai, Hidehiro 	if (!task)
21343cb4a0bbSKawai, Hidehiro 		return -ESRCH;
21353cb4a0bbSKawai, Hidehiro 
21363cb4a0bbSKawai, Hidehiro 	ret = 0;
21373cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
21383cb4a0bbSKawai, Hidehiro 	if (mm) {
21393cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
21403cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
21413cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
21423cb4a0bbSKawai, Hidehiro 		mmput(mm);
21433cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
21443cb4a0bbSKawai, Hidehiro 	}
21453cb4a0bbSKawai, Hidehiro 
21463cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
21473cb4a0bbSKawai, Hidehiro 
21483cb4a0bbSKawai, Hidehiro 	return ret;
21493cb4a0bbSKawai, Hidehiro }
21503cb4a0bbSKawai, Hidehiro 
21513cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
21523cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
21533cb4a0bbSKawai, Hidehiro 					  size_t count,
21543cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
21553cb4a0bbSKawai, Hidehiro {
21563cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
21573cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
21583cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
21593cb4a0bbSKawai, Hidehiro 	unsigned int val;
21603cb4a0bbSKawai, Hidehiro 	int ret;
21613cb4a0bbSKawai, Hidehiro 	int i;
21623cb4a0bbSKawai, Hidehiro 	unsigned long mask;
21633cb4a0bbSKawai, Hidehiro 
21643cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
21653cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
21663cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
21673cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
21683cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
21693cb4a0bbSKawai, Hidehiro 		goto out_no_task;
21703cb4a0bbSKawai, Hidehiro 
21713cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
21723cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
21733cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
21743cb4a0bbSKawai, Hidehiro 		end++;
21753cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
21763cb4a0bbSKawai, Hidehiro 		goto out_no_task;
21773cb4a0bbSKawai, Hidehiro 
21783cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
21793cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
21803cb4a0bbSKawai, Hidehiro 	if (!task)
21813cb4a0bbSKawai, Hidehiro 		goto out_no_task;
21823cb4a0bbSKawai, Hidehiro 
21833cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
21843cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
21853cb4a0bbSKawai, Hidehiro 	if (!mm)
21863cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
21873cb4a0bbSKawai, Hidehiro 
21883cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
21893cb4a0bbSKawai, Hidehiro 		if (val & mask)
21903cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
21913cb4a0bbSKawai, Hidehiro 		else
21923cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
21933cb4a0bbSKawai, Hidehiro 	}
21943cb4a0bbSKawai, Hidehiro 
21953cb4a0bbSKawai, Hidehiro 	mmput(mm);
21963cb4a0bbSKawai, Hidehiro  out_no_mm:
21973cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
21983cb4a0bbSKawai, Hidehiro  out_no_task:
21993cb4a0bbSKawai, Hidehiro 	return ret;
22003cb4a0bbSKawai, Hidehiro }
22013cb4a0bbSKawai, Hidehiro 
22023cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
22033cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
22043cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
22053cb4a0bbSKawai, Hidehiro };
22063cb4a0bbSKawai, Hidehiro #endif
22073cb4a0bbSKawai, Hidehiro 
22081da177e4SLinus Torvalds /*
22091da177e4SLinus Torvalds  * /proc/self:
22101da177e4SLinus Torvalds  */
22111da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
22121da177e4SLinus Torvalds 			      int buflen)
22131da177e4SLinus Torvalds {
2214488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2215b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
22168578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2217b55fcb22SAndrew Morton 	if (!tgid)
2218488e5bc4SEric W. Biederman 		return -ENOENT;
2219b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
22201da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
22211da177e4SLinus Torvalds }
22221da177e4SLinus Torvalds 
2223008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
22241da177e4SLinus Torvalds {
2225488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2226b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
22278578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2228b55fcb22SAndrew Morton 	if (!tgid)
2229488e5bc4SEric W. Biederman 		return ERR_PTR(-ENOENT);
2230b55fcb22SAndrew Morton 	sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2231008b150aSAl Viro 	return ERR_PTR(vfs_follow_link(nd,tmp));
22321da177e4SLinus Torvalds }
22331da177e4SLinus Torvalds 
2234c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
22351da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
22361da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
22371da177e4SLinus Torvalds };
22381da177e4SLinus Torvalds 
223928a6d671SEric W. Biederman /*
2240801199ceSEric W. Biederman  * proc base
2241801199ceSEric W. Biederman  *
2242801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2243801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2244801199ceSEric W. Biederman  * describe something that is process related.
2245801199ceSEric W. Biederman  */
2246c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
224761a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2248801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2249801199ceSEric W. Biederman };
2250801199ceSEric W. Biederman 
2251801199ceSEric W. Biederman /*
2252801199ceSEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
2253801199ceSEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
2254801199ceSEric W. Biederman  * due to the way we treat inodes.
2255801199ceSEric W. Biederman  */
2256801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2257801199ceSEric W. Biederman {
2258801199ceSEric W. Biederman 	struct inode *inode = dentry->d_inode;
2259801199ceSEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2260801199ceSEric W. Biederman 	if (task) {
2261801199ceSEric W. Biederman 		put_task_struct(task);
2262801199ceSEric W. Biederman 		return 1;
2263801199ceSEric W. Biederman 	}
2264801199ceSEric W. Biederman 	d_drop(dentry);
2265801199ceSEric W. Biederman 	return 0;
2266801199ceSEric W. Biederman }
2267801199ceSEric W. Biederman 
2268801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations =
2269801199ceSEric W. Biederman {
2270801199ceSEric W. Biederman 	.d_revalidate	= proc_base_revalidate,
2271801199ceSEric W. Biederman 	.d_delete	= pid_delete_dentry,
2272801199ceSEric W. Biederman };
2273801199ceSEric W. Biederman 
2274444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2275c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2276801199ceSEric W. Biederman {
2277c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2278801199ceSEric W. Biederman 	struct inode *inode;
2279801199ceSEric W. Biederman 	struct proc_inode *ei;
2280444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-EINVAL);
2281801199ceSEric W. Biederman 
2282801199ceSEric W. Biederman 	/* Allocate the inode */
2283801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2284801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2285801199ceSEric W. Biederman 	if (!inode)
2286801199ceSEric W. Biederman 		goto out;
2287801199ceSEric W. Biederman 
2288801199ceSEric W. Biederman 	/* Initialize the inode */
2289801199ceSEric W. Biederman 	ei = PROC_I(inode);
2290801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2291801199ceSEric W. Biederman 
2292801199ceSEric W. Biederman 	/*
2293801199ceSEric W. Biederman 	 * grab the reference to the task.
2294801199ceSEric W. Biederman 	 */
22951a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2296801199ceSEric W. Biederman 	if (!ei->pid)
2297801199ceSEric W. Biederman 		goto out_iput;
2298801199ceSEric W. Biederman 
2299801199ceSEric W. Biederman 	inode->i_uid = 0;
2300801199ceSEric W. Biederman 	inode->i_gid = 0;
2301801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2302801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2303801199ceSEric W. Biederman 		inode->i_nlink = 2;
2304801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2305801199ceSEric W. Biederman 		inode->i_size = 64;
2306801199ceSEric W. Biederman 	if (p->iop)
2307801199ceSEric W. Biederman 		inode->i_op = p->iop;
2308801199ceSEric W. Biederman 	if (p->fop)
2309801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2310801199ceSEric W. Biederman 	ei->op = p->op;
2311801199ceSEric W. Biederman 	dentry->d_op = &proc_base_dentry_operations;
2312801199ceSEric W. Biederman 	d_add(dentry, inode);
2313801199ceSEric W. Biederman 	error = NULL;
2314801199ceSEric W. Biederman out:
2315801199ceSEric W. Biederman 	return error;
2316801199ceSEric W. Biederman out_iput:
2317801199ceSEric W. Biederman 	iput(inode);
2318801199ceSEric W. Biederman 	goto out;
2319801199ceSEric W. Biederman }
2320801199ceSEric W. Biederman 
2321444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2322444ceed8SEric W. Biederman {
2323444ceed8SEric W. Biederman 	struct dentry *error;
2324444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2325c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2326444ceed8SEric W. Biederman 
2327444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2328444ceed8SEric W. Biederman 
2329444ceed8SEric W. Biederman 	if (!task)
2330444ceed8SEric W. Biederman 		goto out_no_task;
2331444ceed8SEric W. Biederman 
2332444ceed8SEric W. Biederman 	/* Lookup the directory entry */
23337bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
23347bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2335444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2336444ceed8SEric W. Biederman 			continue;
2337444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2338444ceed8SEric W. Biederman 			break;
2339444ceed8SEric W. Biederman 	}
23407bcd6b0eSEric W. Biederman 	if (p > last)
2341444ceed8SEric W. Biederman 		goto out;
2342444ceed8SEric W. Biederman 
2343444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2344444ceed8SEric W. Biederman 
2345444ceed8SEric W. Biederman out:
2346444ceed8SEric W. Biederman 	put_task_struct(task);
2347444ceed8SEric W. Biederman out_no_task:
2348444ceed8SEric W. Biederman 	return error;
2349444ceed8SEric W. Biederman }
2350444ceed8SEric W. Biederman 
2351c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2352c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
235361a28784SEric W. Biederman {
235461a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
235561a28784SEric W. Biederman 				proc_base_instantiate, task, p);
235661a28784SEric W. Biederman }
235761a28784SEric W. Biederman 
2358aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2359aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2360aba76fdbSAndrew Morton {
2361aba76fdbSAndrew Morton 	return sprintf(buffer,
23624b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
2363aba76fdbSAndrew Morton 			"rchar: %llu\n"
2364aba76fdbSAndrew Morton 			"wchar: %llu\n"
2365aba76fdbSAndrew Morton 			"syscr: %llu\n"
2366aba76fdbSAndrew Morton 			"syscw: %llu\n"
23674b98d11bSAlexey Dobriyan #endif
2368aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2369aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2370aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
23714b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
2372aba76fdbSAndrew Morton 			(unsigned long long)task->rchar,
2373aba76fdbSAndrew Morton 			(unsigned long long)task->wchar,
2374aba76fdbSAndrew Morton 			(unsigned long long)task->syscr,
2375aba76fdbSAndrew Morton 			(unsigned long long)task->syscw,
23764b98d11bSAlexey Dobriyan #endif
2377aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.read_bytes,
2378aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.write_bytes,
2379aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.cancelled_write_bytes);
2380aba76fdbSAndrew Morton }
2381aba76fdbSAndrew Morton #endif
2382aba76fdbSAndrew Morton 
2383801199ceSEric W. Biederman /*
238428a6d671SEric W. Biederman  * Thread groups
238528a6d671SEric W. Biederman  */
238600977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2387c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
238820cdc894SEric W. Biederman 
2389c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
239061a28784SEric W. Biederman 	DIR("task",       S_IRUGO|S_IXUGO, task),
239161a28784SEric W. Biederman 	DIR("fd",         S_IRUSR|S_IXUSR, fd),
239227932742SMiklos Szeredi 	DIR("fdinfo",     S_IRUSR|S_IXUSR, fdinfo),
2393b2211a36SAndrew Morton #ifdef CONFIG_NET
23944f42c288SAndre Noll 	DIR("net",        S_IRUGO|S_IXUGO, net),
2395b2211a36SAndrew Morton #endif
2396315e28c8SJames Pearson 	REG("environ",    S_IRUSR, environ),
239761a28784SEric W. Biederman 	INF("auxv",       S_IRUSR, pid_auxv),
2398df5f8314SEric W. Biederman 	ONE("status",     S_IRUGO, pid_status),
2399d85f50d5SNeil Horman 	INF("limits",	  S_IRUSR, pid_limits),
240043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
240143ae34cbSIngo Molnar 	REG("sched",      S_IRUGO|S_IWUSR, pid_sched),
240243ae34cbSIngo Molnar #endif
240361a28784SEric W. Biederman 	INF("cmdline",    S_IRUGO, pid_cmdline),
2404ee992744SEric W. Biederman 	ONE("stat",       S_IRUGO, tgid_stat),
2405a56d3fc7SEric W. Biederman 	ONE("statm",      S_IRUGO, pid_statm),
240661a28784SEric W. Biederman 	REG("maps",       S_IRUGO, maps),
240728a6d671SEric W. Biederman #ifdef CONFIG_NUMA
240861a28784SEric W. Biederman 	REG("numa_maps",  S_IRUGO, numa_maps),
240928a6d671SEric W. Biederman #endif
241061a28784SEric W. Biederman 	REG("mem",        S_IRUSR|S_IWUSR, mem),
241161a28784SEric W. Biederman 	LNK("cwd",        cwd),
241261a28784SEric W. Biederman 	LNK("root",       root),
241361a28784SEric W. Biederman 	LNK("exe",        exe),
241461a28784SEric W. Biederman 	REG("mounts",     S_IRUGO, mounts),
24152d4d4864SRam Pai 	REG("mountinfo",  S_IRUGO, mountinfo),
241661a28784SEric W. Biederman 	REG("mountstats", S_IRUSR, mountstats),
24171e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2418b813e931SDavid Rientjes 	REG("clear_refs", S_IWUSR, clear_refs),
241961a28784SEric W. Biederman 	REG("smaps",      S_IRUGO, smaps),
242085863e47SMatt Mackall 	REG("pagemap",    S_IRUSR, pagemap),
242128a6d671SEric W. Biederman #endif
242228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
242372d9dcfcSEric W. Biederman 	DIR("attr",       S_IRUGO|S_IXUGO, attr_dir),
242428a6d671SEric W. Biederman #endif
242528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
242661a28784SEric W. Biederman 	INF("wchan",      S_IRUGO, pid_wchan),
242728a6d671SEric W. Biederman #endif
242828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
242961a28784SEric W. Biederman 	INF("schedstat",  S_IRUGO, pid_schedstat),
243028a6d671SEric W. Biederman #endif
24319745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
24329745512cSArjan van de Ven 	REG("latency",  S_IRUGO, lstats),
24339745512cSArjan van de Ven #endif
24348793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
243561a28784SEric W. Biederman 	REG("cpuset",     S_IRUGO, cpuset),
243628a6d671SEric W. Biederman #endif
2437a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2438a424316cSPaul Menage 	REG("cgroup",  S_IRUGO, cgroup),
2439a424316cSPaul Menage #endif
244061a28784SEric W. Biederman 	INF("oom_score",  S_IRUGO, oom_score),
244161a28784SEric W. Biederman 	REG("oom_adj",    S_IRUGO|S_IWUSR, oom_adjust),
244228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
244361a28784SEric W. Biederman 	REG("loginuid",   S_IWUSR|S_IRUGO, loginuid),
24441e0bd755SEric Paris 	REG("sessionid",  S_IRUSR, sessionid),
244528a6d671SEric W. Biederman #endif
2446f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2447f4f154fdSAkinobu Mita 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2448f4f154fdSAkinobu Mita #endif
24493cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
24503cb4a0bbSKawai, Hidehiro 	REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
24513cb4a0bbSKawai, Hidehiro #endif
2452aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2453aba76fdbSAndrew Morton 	INF("io",	S_IRUGO, pid_io_accounting),
2454aba76fdbSAndrew Morton #endif
245528a6d671SEric W. Biederman };
245628a6d671SEric W. Biederman 
245728a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
245828a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
245928a6d671SEric W. Biederman {
246028a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
246128a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
246228a6d671SEric W. Biederman }
246328a6d671SEric W. Biederman 
246400977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
246528a6d671SEric W. Biederman 	.read		= generic_read_dir,
246628a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
246728a6d671SEric W. Biederman };
246828a6d671SEric W. Biederman 
246928a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
24707bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
24717bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
247228a6d671SEric W. Biederman }
247328a6d671SEric W. Biederman 
2474c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
247528a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
247628a6d671SEric W. Biederman 	.getattr	= pid_getattr,
247728a6d671SEric W. Biederman 	.setattr	= proc_setattr,
247828a6d671SEric W. Biederman };
247928a6d671SEric W. Biederman 
248060347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
24811da177e4SLinus Torvalds {
248248e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
24838578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
248448e6484dSEric W. Biederman 	struct qstr name;
24851da177e4SLinus Torvalds 
248648e6484dSEric W. Biederman 	name.name = buf;
248760347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
248860347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
248948e6484dSEric W. Biederman 	if (dentry) {
24907766755aSAndrea Arcangeli 		if (!(current->flags & PF_EXITING))
249148e6484dSEric W. Biederman 			shrink_dcache_parent(dentry);
249248e6484dSEric W. Biederman 		d_drop(dentry);
249348e6484dSEric W. Biederman 		dput(dentry);
24941da177e4SLinus Torvalds 	}
24951da177e4SLinus Torvalds 
249660347f67SPavel Emelyanov 	if (tgid == 0)
249748e6484dSEric W. Biederman 		goto out;
24981da177e4SLinus Torvalds 
249948e6484dSEric W. Biederman 	name.name = buf;
250060347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
250160347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
250248e6484dSEric W. Biederman 	if (!leader)
250348e6484dSEric W. Biederman 		goto out;
250448e6484dSEric W. Biederman 
250548e6484dSEric W. Biederman 	name.name = "task";
250648e6484dSEric W. Biederman 	name.len = strlen(name.name);
250748e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
250848e6484dSEric W. Biederman 	if (!dir)
250948e6484dSEric W. Biederman 		goto out_put_leader;
251048e6484dSEric W. Biederman 
251148e6484dSEric W. Biederman 	name.name = buf;
251260347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
251348e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
251448e6484dSEric W. Biederman 	if (dentry) {
251548e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
251648e6484dSEric W. Biederman 		d_drop(dentry);
251748e6484dSEric W. Biederman 		dput(dentry);
25181da177e4SLinus Torvalds 	}
251948e6484dSEric W. Biederman 
252048e6484dSEric W. Biederman 	dput(dir);
252148e6484dSEric W. Biederman out_put_leader:
252248e6484dSEric W. Biederman 	dput(leader);
252348e6484dSEric W. Biederman out:
252448e6484dSEric W. Biederman 	return;
25251da177e4SLinus Torvalds }
25261da177e4SLinus Torvalds 
25270895e91dSRandy Dunlap /**
25280895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
25290895e91dSRandy Dunlap  * @task: task that should be flushed.
25300895e91dSRandy Dunlap  *
25310895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
253260347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
25330895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
25340895e91dSRandy Dunlap  *
25350895e91dSRandy Dunlap  * Looks in the dcache for
25360895e91dSRandy Dunlap  * /proc/@pid
25370895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
25380895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
25390895e91dSRandy Dunlap  * from the dcache.
25400895e91dSRandy Dunlap  *
25410895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
25420895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
25430895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
25440895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
25450895e91dSRandy Dunlap  * dcache entries at process exit time.
25460895e91dSRandy Dunlap  *
25470895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
25480895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
25490895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
255060347f67SPavel Emelyanov  */
255160347f67SPavel Emelyanov 
255260347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
255360347f67SPavel Emelyanov {
25549fcc2d15SEric W. Biederman 	int i;
25559fcc2d15SEric W. Biederman 	struct pid *pid, *tgid = NULL;
2556130f77ecSPavel Emelyanov 	struct upid *upid;
2557130f77ecSPavel Emelyanov 
2558130f77ecSPavel Emelyanov 	pid = task_pid(task);
25599fcc2d15SEric W. Biederman 	if (thread_group_leader(task))
2560130f77ecSPavel Emelyanov 		tgid = task_tgid(task);
25619fcc2d15SEric W. Biederman 
25629fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2563130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2564130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
25659fcc2d15SEric W. Biederman 			tgid ? tgid->numbers[i].nr : 0);
2566130f77ecSPavel Emelyanov 	}
25676f4e6433SPavel Emelyanov 
25686f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
25696f4e6433SPavel Emelyanov 	if (upid->nr == 1)
25706f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
257160347f67SPavel Emelyanov }
257260347f67SPavel Emelyanov 
25739711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
25749711ef99SAdrian Bunk 					   struct dentry * dentry,
2575c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2576444ceed8SEric W. Biederman {
2577444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2578444ceed8SEric W. Biederman 	struct inode *inode;
2579444ceed8SEric W. Biederman 
258061a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2581444ceed8SEric W. Biederman 	if (!inode)
2582444ceed8SEric W. Biederman 		goto out;
2583444ceed8SEric W. Biederman 
2584444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2585444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2586444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2587444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
258827932742SMiklos Szeredi 	inode->i_nlink = 5;
2589444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY
2590444ceed8SEric W. Biederman 	inode->i_nlink += 1;
2591444ceed8SEric W. Biederman #endif
2592444ceed8SEric W. Biederman 
2593444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2594444ceed8SEric W. Biederman 
2595444ceed8SEric W. Biederman 	d_add(dentry, inode);
2596444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2597444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2598444ceed8SEric W. Biederman 		error = NULL;
2599444ceed8SEric W. Biederman out:
2600444ceed8SEric W. Biederman 	return error;
2601444ceed8SEric W. Biederman }
2602444ceed8SEric W. Biederman 
26031da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
26041da177e4SLinus Torvalds {
2605cd6a3ce9SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
26061da177e4SLinus Torvalds 	struct task_struct *task;
26071da177e4SLinus Torvalds 	unsigned tgid;
2608b488893aSPavel Emelyanov 	struct pid_namespace *ns;
26091da177e4SLinus Torvalds 
2610801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
2611801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2612801199ceSEric W. Biederman 		goto out;
2613801199ceSEric W. Biederman 
26141da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
26151da177e4SLinus Torvalds 	if (tgid == ~0U)
26161da177e4SLinus Torvalds 		goto out;
26171da177e4SLinus Torvalds 
2618b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2619de758734SEric W. Biederman 	rcu_read_lock();
2620b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
26211da177e4SLinus Torvalds 	if (task)
26221da177e4SLinus Torvalds 		get_task_struct(task);
2623de758734SEric W. Biederman 	rcu_read_unlock();
26241da177e4SLinus Torvalds 	if (!task)
26251da177e4SLinus Torvalds 		goto out;
26261da177e4SLinus Torvalds 
2627444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
262848e6484dSEric W. Biederman 	put_task_struct(task);
26291da177e4SLinus Torvalds out:
2630cd6a3ce9SEric W. Biederman 	return result;
26311da177e4SLinus Torvalds }
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds /*
26340804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
26350bc58a91SEric W. Biederman  *
26361da177e4SLinus Torvalds  */
263719fd4bb2SEric W. Biederman struct tgid_iter {
263819fd4bb2SEric W. Biederman 	unsigned int tgid;
26390804ef4bSEric W. Biederman 	struct task_struct *task;
264019fd4bb2SEric W. Biederman };
264119fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
264219fd4bb2SEric W. Biederman {
26430804ef4bSEric W. Biederman 	struct pid *pid;
26441da177e4SLinus Torvalds 
264519fd4bb2SEric W. Biederman 	if (iter.task)
264619fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
26470804ef4bSEric W. Biederman 	rcu_read_lock();
26480804ef4bSEric W. Biederman retry:
264919fd4bb2SEric W. Biederman 	iter.task = NULL;
265019fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
26510804ef4bSEric W. Biederman 	if (pid) {
265219fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
265319fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
26540804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
26550804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
26560804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
26570804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
26580804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
26590804ef4bSEric W. Biederman 		 *
26600804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
26610804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
26620804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
26630804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
26640804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
26650bc58a91SEric W. Biederman 		 */
266619fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
266719fd4bb2SEric W. Biederman 			iter.tgid += 1;
26680804ef4bSEric W. Biederman 			goto retry;
266919fd4bb2SEric W. Biederman 		}
267019fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
26711da177e4SLinus Torvalds 	}
2672454cc105SEric W. Biederman 	rcu_read_unlock();
267319fd4bb2SEric W. Biederman 	return iter;
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
26767bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
26771da177e4SLinus Torvalds 
267861a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
267919fd4bb2SEric W. Biederman 	struct tgid_iter iter)
268061a28784SEric W. Biederman {
268161a28784SEric W. Biederman 	char name[PROC_NUMBUF];
268219fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
268361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
268419fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
268561a28784SEric W. Biederman }
268661a28784SEric W. Biederman 
26871da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
26881da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
26891da177e4SLinus Torvalds {
26901da177e4SLinus Torvalds 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
26912fddfeefSJosef "Jeff" Sipek 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
269219fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2693b488893aSPavel Emelyanov 	struct pid_namespace *ns;
26941da177e4SLinus Torvalds 
269561a28784SEric W. Biederman 	if (!reaper)
269661a28784SEric W. Biederman 		goto out_no_task;
269761a28784SEric W. Biederman 
26987bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2699c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
270061a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2701801199ceSEric W. Biederman 			goto out;
27021da177e4SLinus Torvalds 	}
27031da177e4SLinus Torvalds 
2704b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
270519fd4bb2SEric W. Biederman 	iter.task = NULL;
270619fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
270719fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
270819fd4bb2SEric W. Biederman 	     iter.task;
270919fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
271019fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
271119fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
271219fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
27130804ef4bSEric W. Biederman 			goto out;
27141da177e4SLinus Torvalds 		}
27151da177e4SLinus Torvalds 	}
27160804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
27170804ef4bSEric W. Biederman out:
271861a28784SEric W. Biederman 	put_task_struct(reaper);
271961a28784SEric W. Biederman out_no_task:
27201da177e4SLinus Torvalds 	return 0;
27211da177e4SLinus Torvalds }
27221da177e4SLinus Torvalds 
27230bc58a91SEric W. Biederman /*
272428a6d671SEric W. Biederman  * Tasks
272528a6d671SEric W. Biederman  */
2726c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
272761a28784SEric W. Biederman 	DIR("fd",        S_IRUSR|S_IXUSR, fd),
272827932742SMiklos Szeredi 	DIR("fdinfo",    S_IRUSR|S_IXUSR, fdinfo),
2729315e28c8SJames Pearson 	REG("environ",   S_IRUSR, environ),
273061a28784SEric W. Biederman 	INF("auxv",      S_IRUSR, pid_auxv),
2731df5f8314SEric W. Biederman 	ONE("status",    S_IRUGO, pid_status),
2732d85f50d5SNeil Horman 	INF("limits",	 S_IRUSR, pid_limits),
273343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
273443ae34cbSIngo Molnar 	REG("sched",     S_IRUGO|S_IWUSR, pid_sched),
273543ae34cbSIngo Molnar #endif
273661a28784SEric W. Biederman 	INF("cmdline",   S_IRUGO, pid_cmdline),
2737ee992744SEric W. Biederman 	ONE("stat",      S_IRUGO, tid_stat),
2738a56d3fc7SEric W. Biederman 	ONE("statm",     S_IRUGO, pid_statm),
273961a28784SEric W. Biederman 	REG("maps",      S_IRUGO, maps),
274028a6d671SEric W. Biederman #ifdef CONFIG_NUMA
274161a28784SEric W. Biederman 	REG("numa_maps", S_IRUGO, numa_maps),
274228a6d671SEric W. Biederman #endif
274361a28784SEric W. Biederman 	REG("mem",       S_IRUSR|S_IWUSR, mem),
274461a28784SEric W. Biederman 	LNK("cwd",       cwd),
274561a28784SEric W. Biederman 	LNK("root",      root),
274661a28784SEric W. Biederman 	LNK("exe",       exe),
274761a28784SEric W. Biederman 	REG("mounts",    S_IRUGO, mounts),
27482d4d4864SRam Pai 	REG("mountinfo",  S_IRUGO, mountinfo),
27491e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2750b813e931SDavid Rientjes 	REG("clear_refs", S_IWUSR, clear_refs),
275161a28784SEric W. Biederman 	REG("smaps",     S_IRUGO, smaps),
275285863e47SMatt Mackall 	REG("pagemap",    S_IRUSR, pagemap),
275328a6d671SEric W. Biederman #endif
275428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
275572d9dcfcSEric W. Biederman 	DIR("attr",      S_IRUGO|S_IXUGO, attr_dir),
275628a6d671SEric W. Biederman #endif
275728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
275861a28784SEric W. Biederman 	INF("wchan",     S_IRUGO, pid_wchan),
275928a6d671SEric W. Biederman #endif
276028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
276161a28784SEric W. Biederman 	INF("schedstat", S_IRUGO, pid_schedstat),
276228a6d671SEric W. Biederman #endif
27639745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
27649745512cSArjan van de Ven 	REG("latency",  S_IRUGO, lstats),
27659745512cSArjan van de Ven #endif
27668793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
276761a28784SEric W. Biederman 	REG("cpuset",    S_IRUGO, cpuset),
276828a6d671SEric W. Biederman #endif
2769a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2770a424316cSPaul Menage 	REG("cgroup",  S_IRUGO, cgroup),
2771a424316cSPaul Menage #endif
277261a28784SEric W. Biederman 	INF("oom_score", S_IRUGO, oom_score),
277361a28784SEric W. Biederman 	REG("oom_adj",   S_IRUGO|S_IWUSR, oom_adjust),
277428a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
277561a28784SEric W. Biederman 	REG("loginuid",  S_IWUSR|S_IRUGO, loginuid),
27761e0bd755SEric Paris 	REG("sessionid",  S_IRUSR, sessionid),
277728a6d671SEric W. Biederman #endif
2778f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2779f4f154fdSAkinobu Mita 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2780f4f154fdSAkinobu Mita #endif
278128a6d671SEric W. Biederman };
278228a6d671SEric W. Biederman 
278328a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
278428a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
278528a6d671SEric W. Biederman {
278628a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
278728a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
278828a6d671SEric W. Biederman }
278928a6d671SEric W. Biederman 
279028a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
27917bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
27927bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
279328a6d671SEric W. Biederman }
279428a6d671SEric W. Biederman 
279500977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
279628a6d671SEric W. Biederman 	.read		= generic_read_dir,
279728a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
279828a6d671SEric W. Biederman };
279928a6d671SEric W. Biederman 
2800c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
280128a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
280228a6d671SEric W. Biederman 	.getattr	= pid_getattr,
280328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
280428a6d671SEric W. Biederman };
280528a6d671SEric W. Biederman 
2806444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
2807c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2808444ceed8SEric W. Biederman {
2809444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2810444ceed8SEric W. Biederman 	struct inode *inode;
281161a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2812444ceed8SEric W. Biederman 
2813444ceed8SEric W. Biederman 	if (!inode)
2814444ceed8SEric W. Biederman 		goto out;
2815444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2816444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
2817444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
2818444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
281927932742SMiklos Szeredi 	inode->i_nlink = 4;
2820444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY
2821444ceed8SEric W. Biederman 	inode->i_nlink += 1;
2822444ceed8SEric W. Biederman #endif
2823444ceed8SEric W. Biederman 
2824444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2825444ceed8SEric W. Biederman 
2826444ceed8SEric W. Biederman 	d_add(dentry, inode);
2827444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2828444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2829444ceed8SEric W. Biederman 		error = NULL;
2830444ceed8SEric W. Biederman out:
2831444ceed8SEric W. Biederman 	return error;
2832444ceed8SEric W. Biederman }
2833444ceed8SEric W. Biederman 
283428a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
283528a6d671SEric W. Biederman {
283628a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
283728a6d671SEric W. Biederman 	struct task_struct *task;
283828a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
283928a6d671SEric W. Biederman 	unsigned tid;
2840b488893aSPavel Emelyanov 	struct pid_namespace *ns;
284128a6d671SEric W. Biederman 
284228a6d671SEric W. Biederman 	if (!leader)
284328a6d671SEric W. Biederman 		goto out_no_task;
284428a6d671SEric W. Biederman 
284528a6d671SEric W. Biederman 	tid = name_to_int(dentry);
284628a6d671SEric W. Biederman 	if (tid == ~0U)
284728a6d671SEric W. Biederman 		goto out;
284828a6d671SEric W. Biederman 
2849b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
285028a6d671SEric W. Biederman 	rcu_read_lock();
2851b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
285228a6d671SEric W. Biederman 	if (task)
285328a6d671SEric W. Biederman 		get_task_struct(task);
285428a6d671SEric W. Biederman 	rcu_read_unlock();
285528a6d671SEric W. Biederman 	if (!task)
285628a6d671SEric W. Biederman 		goto out;
2857bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
285828a6d671SEric W. Biederman 		goto out_drop_task;
285928a6d671SEric W. Biederman 
2860444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
286128a6d671SEric W. Biederman out_drop_task:
286228a6d671SEric W. Biederman 	put_task_struct(task);
286328a6d671SEric W. Biederman out:
286428a6d671SEric W. Biederman 	put_task_struct(leader);
286528a6d671SEric W. Biederman out_no_task:
286628a6d671SEric W. Biederman 	return result;
286728a6d671SEric W. Biederman }
286828a6d671SEric W. Biederman 
286928a6d671SEric W. Biederman /*
28700bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
28710bc58a91SEric W. Biederman  *
28720bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
28730bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
28740bc58a91SEric W. Biederman  * directory we have more work todo.
28750bc58a91SEric W. Biederman  *
28760bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
28770bc58a91SEric W. Biederman  *
28780bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
28790bc58a91SEric W. Biederman  * threads past it.
28800bc58a91SEric W. Biederman  */
2881cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
2882b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
28830bc58a91SEric W. Biederman {
2884a872ff0cSOleg Nesterov 	struct task_struct *pos;
28850bc58a91SEric W. Biederman 
2886cc288738SEric W. Biederman 	rcu_read_lock();
28870bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
28880bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
2889b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
2890a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
2891a872ff0cSOleg Nesterov 			goto found;
28920bc58a91SEric W. Biederman 	}
28930bc58a91SEric W. Biederman 
28940bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
28950bc58a91SEric W. Biederman 	pos = NULL;
2896a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
2897a872ff0cSOleg Nesterov 		goto out;
2898a872ff0cSOleg Nesterov 
2899a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
2900a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
2901a872ff0cSOleg Nesterov 	 */
2902a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
2903a872ff0cSOleg Nesterov 		pos = next_thread(pos);
2904a872ff0cSOleg Nesterov 		if (pos == leader) {
2905a872ff0cSOleg Nesterov 			pos = NULL;
2906a872ff0cSOleg Nesterov 			goto out;
2907a872ff0cSOleg Nesterov 		}
2908a872ff0cSOleg Nesterov 	}
2909a872ff0cSOleg Nesterov found:
2910a872ff0cSOleg Nesterov 	get_task_struct(pos);
2911a872ff0cSOleg Nesterov out:
2912cc288738SEric W. Biederman 	rcu_read_unlock();
29130bc58a91SEric W. Biederman 	return pos;
29140bc58a91SEric W. Biederman }
29150bc58a91SEric W. Biederman 
29160bc58a91SEric W. Biederman /*
29170bc58a91SEric W. Biederman  * Find the next thread in the thread list.
29180bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
29190bc58a91SEric W. Biederman  *
29200bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
29210bc58a91SEric W. Biederman  */
29220bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
29230bc58a91SEric W. Biederman {
2924c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
2925cc288738SEric W. Biederman 	rcu_read_lock();
2926c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
29270bc58a91SEric W. Biederman 		pos = next_thread(start);
2928c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
29290bc58a91SEric W. Biederman 			pos = NULL;
2930c1df7fb8SOleg Nesterov 		else
2931c1df7fb8SOleg Nesterov 			get_task_struct(pos);
2932c1df7fb8SOleg Nesterov 	}
2933cc288738SEric W. Biederman 	rcu_read_unlock();
29340bc58a91SEric W. Biederman 	put_task_struct(start);
29350bc58a91SEric W. Biederman 	return pos;
29360bc58a91SEric W. Biederman }
29370bc58a91SEric W. Biederman 
293861a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
293961a28784SEric W. Biederman 	struct task_struct *task, int tid)
294061a28784SEric W. Biederman {
294161a28784SEric W. Biederman 	char name[PROC_NUMBUF];
294261a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
294361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
294461a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
294561a28784SEric W. Biederman }
294661a28784SEric W. Biederman 
29471da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
29481da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
29491da177e4SLinus Torvalds {
29502fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
29511da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
29527d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
29530bc58a91SEric W. Biederman 	struct task_struct *task;
29541da177e4SLinus Torvalds 	int retval = -ENOENT;
29551da177e4SLinus Torvalds 	ino_t ino;
29560bc58a91SEric W. Biederman 	int tid;
29571da177e4SLinus Torvalds 	unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2958b488893aSPavel Emelyanov 	struct pid_namespace *ns;
29591da177e4SLinus Torvalds 
29607d895244SGuillaume Chazarain 	task = get_proc_task(inode);
29617d895244SGuillaume Chazarain 	if (!task)
29627d895244SGuillaume Chazarain 		goto out_no_task;
29637d895244SGuillaume Chazarain 	rcu_read_lock();
29647d895244SGuillaume Chazarain 	if (pid_alive(task)) {
29657d895244SGuillaume Chazarain 		leader = task->group_leader;
29667d895244SGuillaume Chazarain 		get_task_struct(leader);
29677d895244SGuillaume Chazarain 	}
29687d895244SGuillaume Chazarain 	rcu_read_unlock();
29697d895244SGuillaume Chazarain 	put_task_struct(task);
297099f89551SEric W. Biederman 	if (!leader)
297199f89551SEric W. Biederman 		goto out_no_task;
29721da177e4SLinus Torvalds 	retval = 0;
29731da177e4SLinus Torvalds 
29741da177e4SLinus Torvalds 	switch (pos) {
29751da177e4SLinus Torvalds 	case 0:
29761da177e4SLinus Torvalds 		ino = inode->i_ino;
29771da177e4SLinus Torvalds 		if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
29781da177e4SLinus Torvalds 			goto out;
29791da177e4SLinus Torvalds 		pos++;
29801da177e4SLinus Torvalds 		/* fall through */
29811da177e4SLinus Torvalds 	case 1:
29821da177e4SLinus Torvalds 		ino = parent_ino(dentry);
29831da177e4SLinus Torvalds 		if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
29841da177e4SLinus Torvalds 			goto out;
29851da177e4SLinus Torvalds 		pos++;
29861da177e4SLinus Torvalds 		/* fall through */
29871da177e4SLinus Torvalds 	}
29881da177e4SLinus Torvalds 
29890bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
29900bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
29910bc58a91SEric W. Biederman 	 */
2992b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
29932b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
29940bc58a91SEric W. Biederman 	filp->f_version = 0;
2995b488893aSPavel Emelyanov 	for (task = first_tid(leader, tid, pos - 2, ns);
29960bc58a91SEric W. Biederman 	     task;
29970bc58a91SEric W. Biederman 	     task = next_tid(task), pos++) {
2998b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
299961a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
30000bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
30010bc58a91SEric W. Biederman 			 * pid for the next readir call */
30022b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
30030bc58a91SEric W. Biederman 			put_task_struct(task);
30041da177e4SLinus Torvalds 			break;
30050bc58a91SEric W. Biederman 		}
30061da177e4SLinus Torvalds 	}
30071da177e4SLinus Torvalds out:
30081da177e4SLinus Torvalds 	filp->f_pos = pos;
300999f89551SEric W. Biederman 	put_task_struct(leader);
301099f89551SEric W. Biederman out_no_task:
30111da177e4SLinus Torvalds 	return retval;
30121da177e4SLinus Torvalds }
30136e66b52bSEric W. Biederman 
30146e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
30156e66b52bSEric W. Biederman {
30166e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
301799f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
30186e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
30196e66b52bSEric W. Biederman 
302099f89551SEric W. Biederman 	if (p) {
302199f89551SEric W. Biederman 		rcu_read_lock();
302299f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
302399f89551SEric W. Biederman 		rcu_read_unlock();
302499f89551SEric W. Biederman 		put_task_struct(p);
30256e66b52bSEric W. Biederman 	}
30266e66b52bSEric W. Biederman 
30276e66b52bSEric W. Biederman 	return 0;
30286e66b52bSEric W. Biederman }
302928a6d671SEric W. Biederman 
3030c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
303128a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
303228a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
303328a6d671SEric W. Biederman 	.setattr	= proc_setattr,
303428a6d671SEric W. Biederman };
303528a6d671SEric W. Biederman 
303600977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
303728a6d671SEric W. Biederman 	.read		= generic_read_dir,
303828a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
303928a6d671SEric W. Biederman };
3040