xref: /openbmc/linux/fs/proc/base.c (revision 13d77c37)
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>
591da177e4SLinus Torvalds #include <linux/string.h>
601da177e4SLinus Torvalds #include <linux/seq_file.h>
611da177e4SLinus Torvalds #include <linux/namei.h>
626b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
631da177e4SLinus Torvalds #include <linux/mm.h>
64b835996fSDipankar Sarma #include <linux/rcupdate.h>
651da177e4SLinus Torvalds #include <linux/kallsyms.h>
66d85f50d5SNeil Horman #include <linux/resource.h>
675096add8SKees Cook #include <linux/module.h>
681da177e4SLinus Torvalds #include <linux/mount.h>
691da177e4SLinus Torvalds #include <linux/security.h>
701da177e4SLinus Torvalds #include <linux/ptrace.h>
71a424316cSPaul Menage #include <linux/cgroup.h>
721da177e4SLinus Torvalds #include <linux/cpuset.h>
731da177e4SLinus Torvalds #include <linux/audit.h>
745addc5ddSAl Viro #include <linux/poll.h>
751651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
768ac773b4SAlexey Dobriyan #include <linux/oom.h>
773cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
7860347f67SPavel Emelyanov #include <linux/pid_namespace.h>
791da177e4SLinus Torvalds #include "internal.h"
801da177e4SLinus Torvalds 
810f2fe20fSEric W. Biederman /* NOTE:
820f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
830f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
840f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
850f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
860f2fe20fSEric W. Biederman  *
870f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
880f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
890f2fe20fSEric W. Biederman  */
900f2fe20fSEric W. Biederman 
911da177e4SLinus Torvalds struct pid_entry {
921da177e4SLinus Torvalds 	char *name;
93c5141e6dSEric Dumazet 	int len;
941da177e4SLinus Torvalds 	mode_t mode;
95c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
9600977a59SArjan van de Ven 	const struct file_operations *fop;
9720cdc894SEric W. Biederman 	union proc_op op;
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
10061a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
10120cdc894SEric W. Biederman 	.name = (NAME),					\
102c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
10320cdc894SEric W. Biederman 	.mode = MODE,					\
10420cdc894SEric W. Biederman 	.iop  = IOP,					\
10520cdc894SEric W. Biederman 	.fop  = FOP,					\
10620cdc894SEric W. Biederman 	.op   = OP,					\
10720cdc894SEric W. Biederman }
10820cdc894SEric W. Biederman 
10961a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE)							\
11061a28784SEric W. Biederman 	NOD(NAME, (S_IFDIR|(MODE)),						\
11120cdc894SEric W. Biederman 		&proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations,	\
11220cdc894SEric W. Biederman 		{} )
11361a28784SEric W. Biederman #define LNK(NAME, OTYPE)					\
11461a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
11520cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
11620cdc894SEric W. Biederman 		{ .proc_get_link = &proc_##OTYPE##_link } )
11761a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE)				\
11861a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), NULL,		\
11920cdc894SEric W. Biederman 		&proc_##OTYPE##_operations, {})
12061a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE)				\
12161a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
12220cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
12320cdc894SEric W. Biederman 		{ .proc_read = &proc_##OTYPE } )
124be614086SEric W. Biederman #define ONE(NAME, MODE, OTYPE)				\
125be614086SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
126be614086SEric W. Biederman 		NULL, &proc_single_file_operations,	\
127be614086SEric W. Biederman 		{ .proc_show = &proc_##OTYPE } )
1281da177e4SLinus Torvalds 
1295096add8SKees Cook int maps_protect;
1305096add8SKees Cook EXPORT_SYMBOL(maps_protect);
1315096add8SKees Cook 
1320494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	struct fs_struct *fs;
1350494f6ecSMiklos Szeredi 	task_lock(task);
1360494f6ecSMiklos Szeredi 	fs = task->fs;
1371da177e4SLinus Torvalds 	if(fs)
1381da177e4SLinus Torvalds 		atomic_inc(&fs->count);
1390494f6ecSMiklos Szeredi 	task_unlock(task);
1400494f6ecSMiklos Szeredi 	return fs;
1410494f6ecSMiklos Szeredi }
1420494f6ecSMiklos Szeredi 
14399f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk)
14499f89551SEric W. Biederman {
14599f89551SEric W. Biederman 	/* Must be called with the rcu_read_lock held */
14699f89551SEric W. Biederman 	unsigned long flags;
14799f89551SEric W. Biederman 	int count = 0;
14899f89551SEric W. Biederman 
14999f89551SEric W. Biederman 	if (lock_task_sighand(tsk, &flags)) {
15099f89551SEric W. Biederman 		count = atomic_read(&tsk->signal->count);
15199f89551SEric W. Biederman 		unlock_task_sighand(tsk, &flags);
15299f89551SEric W. Biederman 	}
15399f89551SEric W. Biederman 	return count;
15499f89551SEric W. Biederman }
15599f89551SEric W. Biederman 
1563dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path)
1570494f6ecSMiklos Szeredi {
15899f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
15999f89551SEric W. Biederman 	struct fs_struct *fs = NULL;
1600494f6ecSMiklos Szeredi 	int result = -ENOENT;
16199f89551SEric W. Biederman 
16299f89551SEric W. Biederman 	if (task) {
16399f89551SEric W. Biederman 		fs = get_fs_struct(task);
16499f89551SEric W. Biederman 		put_task_struct(task);
16599f89551SEric W. Biederman 	}
1661da177e4SLinus Torvalds 	if (fs) {
1671da177e4SLinus Torvalds 		read_lock(&fs->lock);
1683dcd25f3SJan Blunck 		*path = fs->pwd;
1693dcd25f3SJan Blunck 		path_get(&fs->pwd);
1701da177e4SLinus Torvalds 		read_unlock(&fs->lock);
1711da177e4SLinus Torvalds 		result = 0;
1721da177e4SLinus Torvalds 		put_fs_struct(fs);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 	return result;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1773dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path)
1781da177e4SLinus Torvalds {
17999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
18099f89551SEric W. Biederman 	struct fs_struct *fs = NULL;
1811da177e4SLinus Torvalds 	int result = -ENOENT;
18299f89551SEric W. Biederman 
18399f89551SEric W. Biederman 	if (task) {
18499f89551SEric W. Biederman 		fs = get_fs_struct(task);
18599f89551SEric W. Biederman 		put_task_struct(task);
18699f89551SEric W. Biederman 	}
1871da177e4SLinus Torvalds 	if (fs) {
1881da177e4SLinus Torvalds 		read_lock(&fs->lock);
1893dcd25f3SJan Blunck 		*path = fs->root;
1903dcd25f3SJan Blunck 		path_get(&fs->root);
1911da177e4SLinus Torvalds 		read_unlock(&fs->lock);
1921da177e4SLinus Torvalds 		result = 0;
1931da177e4SLinus Torvalds 		put_fs_struct(fs);
1941da177e4SLinus Torvalds 	}
1951da177e4SLinus Torvalds 	return result;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds #define MAY_PTRACE(task) \
1991da177e4SLinus Torvalds 	(task == current || \
2001da177e4SLinus Torvalds 	(task->parent == current && \
2011da177e4SLinus Torvalds 	(task->ptrace & PT_PTRACED) && \
2026d8982d9SMatthew Wilcox 	 (task_is_stopped_or_traced(task)) && \
2031da177e4SLinus Torvalds 	 security_ptrace(current,task) == 0))
2041da177e4SLinus Torvalds 
205831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task)
206831830b5SAl Viro {
207831830b5SAl Viro 	struct mm_struct *mm = get_task_mm(task);
208831830b5SAl Viro 	if (!mm)
209831830b5SAl Viro 		return NULL;
210831830b5SAl Viro 	down_read(&mm->mmap_sem);
211831830b5SAl Viro 	task_lock(task);
212831830b5SAl Viro 	if (task->mm != mm)
213831830b5SAl Viro 		goto out;
214831830b5SAl Viro 	if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
215831830b5SAl Viro 		goto out;
216831830b5SAl Viro 	task_unlock(task);
217831830b5SAl Viro 	return mm;
218831830b5SAl Viro out:
219831830b5SAl Viro 	task_unlock(task);
220831830b5SAl Viro 	up_read(&mm->mmap_sem);
221831830b5SAl Viro 	mmput(mm);
222831830b5SAl Viro 	return NULL;
223831830b5SAl Viro }
224831830b5SAl Viro 
2251da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	int res = 0;
2281da177e4SLinus Torvalds 	unsigned int len;
2291da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2301da177e4SLinus Torvalds 	if (!mm)
2311da177e4SLinus Torvalds 		goto out;
2321da177e4SLinus Torvalds 	if (!mm->arg_end)
2331da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2381da177e4SLinus Torvalds 		len = PAGE_SIZE;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2431da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2441da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2451da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2461da177e4SLinus Torvalds 		if (len < res) {
2471da177e4SLinus Torvalds 		    res = len;
2481da177e4SLinus Torvalds 		} else {
2491da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2501da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2511da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2521da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2531da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2541da177e4SLinus Torvalds 		}
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds out_mm:
2571da177e4SLinus Torvalds 	mmput(mm);
2581da177e4SLinus Torvalds out:
2591da177e4SLinus Torvalds 	return res;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	int res = 0;
2651da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2661da177e4SLinus Torvalds 	if (mm) {
2671da177e4SLinus Torvalds 		unsigned int nwords = 0;
2681da177e4SLinus Torvalds 		do
2691da177e4SLinus Torvalds 			nwords += 2;
2701da177e4SLinus Torvalds 		while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2711da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2721da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2731da177e4SLinus Torvalds 			res = PAGE_SIZE;
2741da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2751da177e4SLinus Torvalds 		mmput(mm);
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 	return res;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2821da177e4SLinus Torvalds /*
2831da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2841da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2851da177e4SLinus Torvalds  */
2861da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
2871da177e4SLinus Torvalds {
288ffb45122SAlexey Dobriyan 	unsigned long wchan;
2899281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	wchan = get_wchan(task);
2921da177e4SLinus Torvalds 
2939d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
2941da177e4SLinus Torvalds 		return sprintf(buffer, "%lu", wchan);
2959d65cb4aSAlexey Dobriyan 	else
2969d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
3011da177e4SLinus Torvalds /*
3021da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
3031da177e4SLinus Torvalds  */
3041da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3051da177e4SLinus Torvalds {
306172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
3071da177e4SLinus Torvalds 			task->sched_info.cpu_time,
3081da177e4SLinus Torvalds 			task->sched_info.run_delay,
3092d72376bSIngo Molnar 			task->sched_info.pcount);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds #endif
3121da177e4SLinus Torvalds 
3139745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3149745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3159745512cSArjan van de Ven {
3169745512cSArjan van de Ven 	int i;
31713d77c37SHiroshi Shimamoto 	struct inode *inode = m->private;
31813d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(inode);
3199745512cSArjan van de Ven 
32013d77c37SHiroshi Shimamoto 	if (!task)
32113d77c37SHiroshi Shimamoto 		return -ESRCH;
32213d77c37SHiroshi Shimamoto 	seq_puts(m, "Latency Top version : v0.1\n");
3239745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
3249745512cSArjan van de Ven 		if (task->latency_record[i].backtrace[0]) {
3259745512cSArjan van de Ven 			int q;
3269745512cSArjan van de Ven 			seq_printf(m, "%i %li %li ",
3279745512cSArjan van de Ven 				task->latency_record[i].count,
3289745512cSArjan van de Ven 				task->latency_record[i].time,
3299745512cSArjan van de Ven 				task->latency_record[i].max);
3309745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
3319745512cSArjan van de Ven 				char sym[KSYM_NAME_LEN];
3329745512cSArjan van de Ven 				char *c;
3339745512cSArjan van de Ven 				if (!task->latency_record[i].backtrace[q])
3349745512cSArjan van de Ven 					break;
3359745512cSArjan van de Ven 				if (task->latency_record[i].backtrace[q] == ULONG_MAX)
3369745512cSArjan van de Ven 					break;
3379745512cSArjan van de Ven 				sprint_symbol(sym, task->latency_record[i].backtrace[q]);
3389745512cSArjan van de Ven 				c = strchr(sym, '+');
3399745512cSArjan van de Ven 				if (c)
3409745512cSArjan van de Ven 					*c = 0;
3419745512cSArjan van de Ven 				seq_printf(m, "%s ", sym);
3429745512cSArjan van de Ven 			}
3439745512cSArjan van de Ven 			seq_printf(m, "\n");
3449745512cSArjan van de Ven 		}
3459745512cSArjan van de Ven 
3469745512cSArjan van de Ven 	}
34713d77c37SHiroshi Shimamoto 	put_task_struct(task);
3489745512cSArjan van de Ven 	return 0;
3499745512cSArjan van de Ven }
3509745512cSArjan van de Ven 
3519745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3529745512cSArjan van de Ven {
35313d77c37SHiroshi Shimamoto 	return single_open(file, lstats_show_proc, inode);
354d6643d12SHiroshi Shimamoto }
355d6643d12SHiroshi Shimamoto 
3569745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
3579745512cSArjan van de Ven 			    size_t count, loff_t *offs)
3589745512cSArjan van de Ven {
35913d77c37SHiroshi Shimamoto 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
3609745512cSArjan van de Ven 
36113d77c37SHiroshi Shimamoto 	if (!task)
36213d77c37SHiroshi Shimamoto 		return -ESRCH;
3639745512cSArjan van de Ven 	clear_all_latency_tracing(task);
36413d77c37SHiroshi Shimamoto 	put_task_struct(task);
3659745512cSArjan van de Ven 
3669745512cSArjan van de Ven 	return count;
3679745512cSArjan van de Ven }
3689745512cSArjan van de Ven 
3699745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
3709745512cSArjan van de Ven 	.open		= lstats_open,
3719745512cSArjan van de Ven 	.read		= seq_read,
3729745512cSArjan van de Ven 	.write		= lstats_write,
3739745512cSArjan van de Ven 	.llseek		= seq_lseek,
37413d77c37SHiroshi Shimamoto 	.release	= single_release,
3759745512cSArjan van de Ven };
3769745512cSArjan van de Ven 
3779745512cSArjan van de Ven #endif
3789745512cSArjan van de Ven 
3791da177e4SLinus Torvalds /* The badness from the OOM killer */
3801da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime);
3811da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	unsigned long points;
3841da177e4SLinus Torvalds 	struct timespec uptime;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&uptime);
38719c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
3881da177e4SLinus Torvalds 	points = badness(task, uptime.tv_sec);
38919c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
3901da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
393d85f50d5SNeil Horman struct limit_names {
394d85f50d5SNeil Horman 	char *name;
395d85f50d5SNeil Horman 	char *unit;
396d85f50d5SNeil Horman };
397d85f50d5SNeil Horman 
398d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
399d85f50d5SNeil Horman 	[RLIMIT_CPU] = {"Max cpu time", "ms"},
400d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
401d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
402d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
403d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
404d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
405d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
406d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
407d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
408d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
409d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
410d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
411d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
412d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
413d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
4148808117cSEugene Teo 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
415d85f50d5SNeil Horman };
416d85f50d5SNeil Horman 
417d85f50d5SNeil Horman /* Display limits for a process */
418d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
419d85f50d5SNeil Horman {
420d85f50d5SNeil Horman 	unsigned int i;
421d85f50d5SNeil Horman 	int count = 0;
422d85f50d5SNeil Horman 	unsigned long flags;
423d85f50d5SNeil Horman 	char *bufptr = buffer;
424d85f50d5SNeil Horman 
425d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
426d85f50d5SNeil Horman 
427d85f50d5SNeil Horman 	rcu_read_lock();
428d85f50d5SNeil Horman 	if (!lock_task_sighand(task,&flags)) {
429d85f50d5SNeil Horman 		rcu_read_unlock();
430d85f50d5SNeil Horman 		return 0;
431d85f50d5SNeil Horman 	}
432d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
433d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
434d85f50d5SNeil Horman 	rcu_read_unlock();
435d85f50d5SNeil Horman 
436d85f50d5SNeil Horman 	/*
437d85f50d5SNeil Horman 	 * print the file header
438d85f50d5SNeil Horman 	 */
439d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
440d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
441d85f50d5SNeil Horman 
442d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
443d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
444d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
445d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
446d85f50d5SNeil Horman 		else
447d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
448d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
449d85f50d5SNeil Horman 
450d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
451d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
452d85f50d5SNeil Horman 		else
453d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
454d85f50d5SNeil Horman 					 rlim[i].rlim_max);
455d85f50d5SNeil Horman 
456d85f50d5SNeil Horman 		if (lnames[i].unit)
457d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
458d85f50d5SNeil Horman 					 lnames[i].unit);
459d85f50d5SNeil Horman 		else
460d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
461d85f50d5SNeil Horman 	}
462d85f50d5SNeil Horman 
463d85f50d5SNeil Horman 	return count;
464d85f50d5SNeil Horman }
465d85f50d5SNeil Horman 
4661da177e4SLinus Torvalds /************************************************************************/
4671da177e4SLinus Torvalds /*                       Here the fs part begins                        */
4681da177e4SLinus Torvalds /************************************************************************/
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds /* permission checks */
471778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
4721da177e4SLinus Torvalds {
473778c1144SEric W. Biederman 	struct task_struct *task;
474778c1144SEric W. Biederman 	int allowed = 0;
475df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
476df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
477df26c40eSEric W. Biederman 	 * information.
478778c1144SEric W. Biederman 	 */
479778c1144SEric W. Biederman 	task = get_proc_task(inode);
480df26c40eSEric W. Biederman 	if (task) {
481778c1144SEric W. Biederman 		allowed = ptrace_may_attach(task);
482778c1144SEric W. Biederman 		put_task_struct(task);
483df26c40eSEric W. Biederman 	}
484778c1144SEric W. Biederman 	return allowed;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4876d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr)
4886d76fa58SLinus Torvalds {
4896d76fa58SLinus Torvalds 	int error;
4906d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
4916d76fa58SLinus Torvalds 
4926d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
4936d76fa58SLinus Torvalds 		return -EPERM;
4946d76fa58SLinus Torvalds 
4956d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
4966d76fa58SLinus Torvalds 	if (!error)
4976d76fa58SLinus Torvalds 		error = inode_setattr(inode, attr);
4986d76fa58SLinus Torvalds 	return error;
4996d76fa58SLinus Torvalds }
5006d76fa58SLinus Torvalds 
501c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5026d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5036d76fa58SLinus Torvalds };
5046d76fa58SLinus Torvalds 
50503a44825SJan Engelhardt extern const struct seq_operations mounts_op;
5065addc5ddSAl Viro struct proc_mounts {
5075addc5ddSAl Viro 	struct seq_file m;
5085addc5ddSAl Viro 	int event;
5095addc5ddSAl Viro };
5105addc5ddSAl Viro 
5111da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file)
5121da177e4SLinus Torvalds {
51399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
514cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
5156b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
5165addc5ddSAl Viro 	struct proc_mounts *p;
5175addc5ddSAl Viro 	int ret = -EINVAL;
5185addc5ddSAl Viro 
51999f89551SEric W. Biederman 	if (task) {
520cf7b708cSPavel Emelyanov 		rcu_read_lock();
521cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
522cf7b708cSPavel Emelyanov 		if (nsp) {
523cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
5246b3286edSKirill Korotaev 			if (ns)
5256b3286edSKirill Korotaev 				get_mnt_ns(ns);
526863c4702SAlexey Dobriyan 		}
527cf7b708cSPavel Emelyanov 		rcu_read_unlock();
528cf7b708cSPavel Emelyanov 
52999f89551SEric W. Biederman 		put_task_struct(task);
53099f89551SEric W. Biederman 	}
5311da177e4SLinus Torvalds 
5326b3286edSKirill Korotaev 	if (ns) {
5335addc5ddSAl Viro 		ret = -ENOMEM;
5345addc5ddSAl Viro 		p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
5355addc5ddSAl Viro 		if (p) {
5365addc5ddSAl Viro 			file->private_data = &p->m;
5375addc5ddSAl Viro 			ret = seq_open(file, &mounts_op);
5385addc5ddSAl Viro 			if (!ret) {
5396b3286edSKirill Korotaev 				p->m.private = ns;
5406b3286edSKirill Korotaev 				p->event = ns->event;
5415addc5ddSAl Viro 				return 0;
5421da177e4SLinus Torvalds 			}
5435addc5ddSAl Viro 			kfree(p);
5445addc5ddSAl Viro 		}
5456b3286edSKirill Korotaev 		put_mnt_ns(ns);
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds 	return ret;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	struct seq_file *m = file->private_data;
5536b3286edSKirill Korotaev 	struct mnt_namespace *ns = m->private;
5546b3286edSKirill Korotaev 	put_mnt_ns(ns);
5551da177e4SLinus Torvalds 	return seq_release(inode, file);
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5585addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
5595addc5ddSAl Viro {
5605addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
5616b3286edSKirill Korotaev 	struct mnt_namespace *ns = p->m.private;
5625addc5ddSAl Viro 	unsigned res = 0;
5635addc5ddSAl Viro 
5645addc5ddSAl Viro 	poll_wait(file, &ns->poll, wait);
5655addc5ddSAl Viro 
5665addc5ddSAl Viro 	spin_lock(&vfsmount_lock);
5675addc5ddSAl Viro 	if (p->event != ns->event) {
5685addc5ddSAl Viro 		p->event = ns->event;
5695addc5ddSAl Viro 		res = POLLERR;
5705addc5ddSAl Viro 	}
5715addc5ddSAl Viro 	spin_unlock(&vfsmount_lock);
5725addc5ddSAl Viro 
5735addc5ddSAl Viro 	return res;
5745addc5ddSAl Viro }
5755addc5ddSAl Viro 
57600977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
5771da177e4SLinus Torvalds 	.open		= mounts_open,
5781da177e4SLinus Torvalds 	.read		= seq_read,
5791da177e4SLinus Torvalds 	.llseek		= seq_lseek,
5801da177e4SLinus Torvalds 	.release	= mounts_release,
5815addc5ddSAl Viro 	.poll		= mounts_poll,
5821da177e4SLinus Torvalds };
5831da177e4SLinus Torvalds 
58403a44825SJan Engelhardt extern const struct seq_operations mountstats_op;
585b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
586b4629fe2SChuck Lever {
587b4629fe2SChuck Lever 	int ret = seq_open(file, &mountstats_op);
588b4629fe2SChuck Lever 
589b4629fe2SChuck Lever 	if (!ret) {
590b4629fe2SChuck Lever 		struct seq_file *m = file->private_data;
591cf7b708cSPavel Emelyanov 		struct nsproxy *nsp;
5926b3286edSKirill Korotaev 		struct mnt_namespace *mnt_ns = NULL;
59399f89551SEric W. Biederman 		struct task_struct *task = get_proc_task(inode);
59499f89551SEric W. Biederman 
59599f89551SEric W. Biederman 		if (task) {
596cf7b708cSPavel Emelyanov 			rcu_read_lock();
597cf7b708cSPavel Emelyanov 			nsp = task_nsproxy(task);
598cf7b708cSPavel Emelyanov 			if (nsp) {
599cf7b708cSPavel Emelyanov 				mnt_ns = nsp->mnt_ns;
6006b3286edSKirill Korotaev 				if (mnt_ns)
6016b3286edSKirill Korotaev 					get_mnt_ns(mnt_ns);
602cf7b708cSPavel Emelyanov 			}
603cf7b708cSPavel Emelyanov 			rcu_read_unlock();
604cf7b708cSPavel Emelyanov 
60599f89551SEric W. Biederman 			put_task_struct(task);
60699f89551SEric W. Biederman 		}
607b4629fe2SChuck Lever 
6086b3286edSKirill Korotaev 		if (mnt_ns)
6096b3286edSKirill Korotaev 			m->private = mnt_ns;
610b4629fe2SChuck Lever 		else {
611b4629fe2SChuck Lever 			seq_release(inode, file);
612b4629fe2SChuck Lever 			ret = -EINVAL;
613b4629fe2SChuck Lever 		}
614b4629fe2SChuck Lever 	}
615b4629fe2SChuck Lever 	return ret;
616b4629fe2SChuck Lever }
617b4629fe2SChuck Lever 
61800977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
619b4629fe2SChuck Lever 	.open		= mountstats_open,
620b4629fe2SChuck Lever 	.read		= seq_read,
621b4629fe2SChuck Lever 	.llseek		= seq_lseek,
622b4629fe2SChuck Lever 	.release	= mounts_release,
623b4629fe2SChuck Lever };
624b4629fe2SChuck Lever 
6251da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6281da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6291da177e4SLinus Torvalds {
6302fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6311da177e4SLinus Torvalds 	unsigned long page;
6321da177e4SLinus Torvalds 	ssize_t length;
63399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
63499f89551SEric W. Biederman 
63599f89551SEric W. Biederman 	length = -ESRCH;
63699f89551SEric W. Biederman 	if (!task)
63799f89551SEric W. Biederman 		goto out_no_task;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6401da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
64199f89551SEric W. Biederman 
64299f89551SEric W. Biederman 	length = -ENOMEM;
643e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
64499f89551SEric W. Biederman 		goto out;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	if (length >= 0)
6491da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6501da177e4SLinus Torvalds 	free_page(page);
65199f89551SEric W. Biederman out:
65299f89551SEric W. Biederman 	put_task_struct(task);
65399f89551SEric W. Biederman out_no_task:
6541da177e4SLinus Torvalds 	return length;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
65700977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6581da177e4SLinus Torvalds 	.read		= proc_info_read,
6591da177e4SLinus Torvalds };
6601da177e4SLinus Torvalds 
661be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v)
662be614086SEric W. Biederman {
663be614086SEric W. Biederman 	struct inode *inode = m->private;
664be614086SEric W. Biederman 	struct pid_namespace *ns;
665be614086SEric W. Biederman 	struct pid *pid;
666be614086SEric W. Biederman 	struct task_struct *task;
667be614086SEric W. Biederman 	int ret;
668be614086SEric W. Biederman 
669be614086SEric W. Biederman 	ns = inode->i_sb->s_fs_info;
670be614086SEric W. Biederman 	pid = proc_pid(inode);
671be614086SEric W. Biederman 	task = get_pid_task(pid, PIDTYPE_PID);
672be614086SEric W. Biederman 	if (!task)
673be614086SEric W. Biederman 		return -ESRCH;
674be614086SEric W. Biederman 
675be614086SEric W. Biederman 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
676be614086SEric W. Biederman 
677be614086SEric W. Biederman 	put_task_struct(task);
678be614086SEric W. Biederman 	return ret;
679be614086SEric W. Biederman }
680be614086SEric W. Biederman 
681be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp)
682be614086SEric W. Biederman {
683be614086SEric W. Biederman 	int ret;
684be614086SEric W. Biederman 	ret = single_open(filp, proc_single_show, NULL);
685be614086SEric W. Biederman 	if (!ret) {
686be614086SEric W. Biederman 		struct seq_file *m = filp->private_data;
687be614086SEric W. Biederman 
688be614086SEric W. Biederman 		m->private = inode;
689be614086SEric W. Biederman 	}
690be614086SEric W. Biederman 	return ret;
691be614086SEric W. Biederman }
692be614086SEric W. Biederman 
693be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = {
694be614086SEric W. Biederman 	.open		= proc_single_open,
695be614086SEric W. Biederman 	.read		= seq_read,
696be614086SEric W. Biederman 	.llseek		= seq_lseek,
697be614086SEric W. Biederman 	.release	= single_release,
698be614086SEric W. Biederman };
699be614086SEric W. Biederman 
7001da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
7031da177e4SLinus Torvalds 	return 0;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
7071da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
7081da177e4SLinus Torvalds {
7092fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7101da177e4SLinus Torvalds 	char *page;
7111da177e4SLinus Torvalds 	unsigned long src = *ppos;
7121da177e4SLinus Torvalds 	int ret = -ESRCH;
7131da177e4SLinus Torvalds 	struct mm_struct *mm;
7141da177e4SLinus Torvalds 
71599f89551SEric W. Biederman 	if (!task)
71699f89551SEric W. Biederman 		goto out_no_task;
71799f89551SEric W. Biederman 
718ab8d11beSMiklos Szeredi 	if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
7191da177e4SLinus Torvalds 		goto out;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	ret = -ENOMEM;
722e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7231da177e4SLinus Torvalds 	if (!page)
7241da177e4SLinus Torvalds 		goto out;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	ret = 0;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	mm = get_task_mm(task);
7291da177e4SLinus Torvalds 	if (!mm)
7301da177e4SLinus Torvalds 		goto out_free;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	ret = -EIO;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
7351da177e4SLinus Torvalds 		goto out_put;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	ret = 0;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	while (count > 0) {
7401da177e4SLinus Torvalds 		int this_len, retval;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
7431da177e4SLinus Torvalds 		retval = access_process_vm(task, src, page, this_len, 0);
744ab8d11beSMiklos Szeredi 		if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
7451da177e4SLinus Torvalds 			if (!ret)
7461da177e4SLinus Torvalds 				ret = -EIO;
7471da177e4SLinus Torvalds 			break;
7481da177e4SLinus Torvalds 		}
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
7511da177e4SLinus Torvalds 			ret = -EFAULT;
7521da177e4SLinus Torvalds 			break;
7531da177e4SLinus Torvalds 		}
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 		ret += retval;
7561da177e4SLinus Torvalds 		src += retval;
7571da177e4SLinus Torvalds 		buf += retval;
7581da177e4SLinus Torvalds 		count -= retval;
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds 	*ppos = src;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds out_put:
7631da177e4SLinus Torvalds 	mmput(mm);
7641da177e4SLinus Torvalds out_free:
7651da177e4SLinus Torvalds 	free_page((unsigned long) page);
7661da177e4SLinus Torvalds out:
76799f89551SEric W. Biederman 	put_task_struct(task);
76899f89551SEric W. Biederman out_no_task:
7691da177e4SLinus Torvalds 	return ret;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds #define mem_write NULL
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds #ifndef mem_write
7751da177e4SLinus Torvalds /* This is a security hazard */
77663967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
7771da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
7781da177e4SLinus Torvalds {
779f7ca54f4SFrederik Deweerdt 	int copied;
7801da177e4SLinus Torvalds 	char *page;
7812fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7821da177e4SLinus Torvalds 	unsigned long dst = *ppos;
7831da177e4SLinus Torvalds 
78499f89551SEric W. Biederman 	copied = -ESRCH;
78599f89551SEric W. Biederman 	if (!task)
78699f89551SEric W. Biederman 		goto out_no_task;
7871da177e4SLinus Torvalds 
78899f89551SEric W. Biederman 	if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
78999f89551SEric W. Biederman 		goto out;
79099f89551SEric W. Biederman 
79199f89551SEric W. Biederman 	copied = -ENOMEM;
792e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7931da177e4SLinus Torvalds 	if (!page)
79499f89551SEric W. Biederman 		goto out;
7951da177e4SLinus Torvalds 
796f7ca54f4SFrederik Deweerdt 	copied = 0;
7971da177e4SLinus Torvalds 	while (count > 0) {
7981da177e4SLinus Torvalds 		int this_len, retval;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
8011da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
8021da177e4SLinus Torvalds 			copied = -EFAULT;
8031da177e4SLinus Torvalds 			break;
8041da177e4SLinus Torvalds 		}
8051da177e4SLinus Torvalds 		retval = access_process_vm(task, dst, page, this_len, 1);
8061da177e4SLinus Torvalds 		if (!retval) {
8071da177e4SLinus Torvalds 			if (!copied)
8081da177e4SLinus Torvalds 				copied = -EIO;
8091da177e4SLinus Torvalds 			break;
8101da177e4SLinus Torvalds 		}
8111da177e4SLinus Torvalds 		copied += retval;
8121da177e4SLinus Torvalds 		buf += retval;
8131da177e4SLinus Torvalds 		dst += retval;
8141da177e4SLinus Torvalds 		count -= retval;
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 	*ppos = dst;
8171da177e4SLinus Torvalds 	free_page((unsigned long) page);
81899f89551SEric W. Biederman out:
81999f89551SEric W. Biederman 	put_task_struct(task);
82099f89551SEric W. Biederman out_no_task:
8211da177e4SLinus Torvalds 	return copied;
8221da177e4SLinus Torvalds }
8231da177e4SLinus Torvalds #endif
8241da177e4SLinus Torvalds 
82585863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
8261da177e4SLinus Torvalds {
8271da177e4SLinus Torvalds 	switch (orig) {
8281da177e4SLinus Torvalds 	case 0:
8291da177e4SLinus Torvalds 		file->f_pos = offset;
8301da177e4SLinus Torvalds 		break;
8311da177e4SLinus Torvalds 	case 1:
8321da177e4SLinus Torvalds 		file->f_pos += offset;
8331da177e4SLinus Torvalds 		break;
8341da177e4SLinus Torvalds 	default:
8351da177e4SLinus Torvalds 		return -EINVAL;
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds 	force_successful_syscall_return();
8381da177e4SLinus Torvalds 	return file->f_pos;
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds 
84100977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8421da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8431da177e4SLinus Torvalds 	.read		= mem_read,
8441da177e4SLinus Torvalds 	.write		= mem_write,
8451da177e4SLinus Torvalds 	.open		= mem_open,
8461da177e4SLinus Torvalds };
8471da177e4SLinus Torvalds 
848315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
849315e28c8SJames Pearson 			size_t count, loff_t *ppos)
850315e28c8SJames Pearson {
851315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
852315e28c8SJames Pearson 	char *page;
853315e28c8SJames Pearson 	unsigned long src = *ppos;
854315e28c8SJames Pearson 	int ret = -ESRCH;
855315e28c8SJames Pearson 	struct mm_struct *mm;
856315e28c8SJames Pearson 
857315e28c8SJames Pearson 	if (!task)
858315e28c8SJames Pearson 		goto out_no_task;
859315e28c8SJames Pearson 
860315e28c8SJames Pearson 	if (!ptrace_may_attach(task))
861315e28c8SJames Pearson 		goto out;
862315e28c8SJames Pearson 
863315e28c8SJames Pearson 	ret = -ENOMEM;
864315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
865315e28c8SJames Pearson 	if (!page)
866315e28c8SJames Pearson 		goto out;
867315e28c8SJames Pearson 
868315e28c8SJames Pearson 	ret = 0;
869315e28c8SJames Pearson 
870315e28c8SJames Pearson 	mm = get_task_mm(task);
871315e28c8SJames Pearson 	if (!mm)
872315e28c8SJames Pearson 		goto out_free;
873315e28c8SJames Pearson 
874315e28c8SJames Pearson 	while (count > 0) {
875315e28c8SJames Pearson 		int this_len, retval, max_len;
876315e28c8SJames Pearson 
877315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
878315e28c8SJames Pearson 
879315e28c8SJames Pearson 		if (this_len <= 0)
880315e28c8SJames Pearson 			break;
881315e28c8SJames Pearson 
882315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
883315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
884315e28c8SJames Pearson 
885315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
886315e28c8SJames Pearson 			page, this_len, 0);
887315e28c8SJames Pearson 
888315e28c8SJames Pearson 		if (retval <= 0) {
889315e28c8SJames Pearson 			ret = retval;
890315e28c8SJames Pearson 			break;
891315e28c8SJames Pearson 		}
892315e28c8SJames Pearson 
893315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
894315e28c8SJames Pearson 			ret = -EFAULT;
895315e28c8SJames Pearson 			break;
896315e28c8SJames Pearson 		}
897315e28c8SJames Pearson 
898315e28c8SJames Pearson 		ret += retval;
899315e28c8SJames Pearson 		src += retval;
900315e28c8SJames Pearson 		buf += retval;
901315e28c8SJames Pearson 		count -= retval;
902315e28c8SJames Pearson 	}
903315e28c8SJames Pearson 	*ppos = src;
904315e28c8SJames Pearson 
905315e28c8SJames Pearson 	mmput(mm);
906315e28c8SJames Pearson out_free:
907315e28c8SJames Pearson 	free_page((unsigned long) page);
908315e28c8SJames Pearson out:
909315e28c8SJames Pearson 	put_task_struct(task);
910315e28c8SJames Pearson out_no_task:
911315e28c8SJames Pearson 	return ret;
912315e28c8SJames Pearson }
913315e28c8SJames Pearson 
914315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
915315e28c8SJames Pearson 	.read		= environ_read,
916315e28c8SJames Pearson };
917315e28c8SJames Pearson 
9181da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
9191da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9201da177e4SLinus Torvalds {
9212fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
9228578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
9231da177e4SLinus Torvalds 	size_t len;
92499f89551SEric W. Biederman 	int oom_adjust;
9251da177e4SLinus Torvalds 
92699f89551SEric W. Biederman 	if (!task)
92799f89551SEric W. Biederman 		return -ESRCH;
92899f89551SEric W. Biederman 	oom_adjust = task->oomkilladj;
92999f89551SEric W. Biederman 	put_task_struct(task);
93099f89551SEric W. Biederman 
9318578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
9320c28f287SAkinobu Mita 
9330c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
9371da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
9381da177e4SLinus Torvalds {
93999f89551SEric W. Biederman 	struct task_struct *task;
9408578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF], *end;
9411da177e4SLinus Torvalds 	int oom_adjust;
9421da177e4SLinus Torvalds 
9438578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
9448578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
9458578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
9461da177e4SLinus Torvalds 	if (copy_from_user(buffer, buf, count))
9471da177e4SLinus Torvalds 		return -EFAULT;
9481da177e4SLinus Torvalds 	oom_adjust = simple_strtol(buffer, &end, 0);
9498ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
9508ac773b4SAlexey Dobriyan 	     oom_adjust != OOM_DISABLE)
9511da177e4SLinus Torvalds 		return -EINVAL;
9521da177e4SLinus Torvalds 	if (*end == '\n')
9531da177e4SLinus Torvalds 		end++;
9542fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
95599f89551SEric W. Biederman 	if (!task)
95699f89551SEric W. Biederman 		return -ESRCH;
9578fb4fc68SGuillem Jover 	if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
9588fb4fc68SGuillem Jover 		put_task_struct(task);
9598fb4fc68SGuillem Jover 		return -EACCES;
9608fb4fc68SGuillem Jover 	}
9611da177e4SLinus Torvalds 	task->oomkilladj = oom_adjust;
96299f89551SEric W. Biederman 	put_task_struct(task);
9631da177e4SLinus Torvalds 	if (end - buffer == 0)
9641da177e4SLinus Torvalds 		return -EIO;
9651da177e4SLinus Torvalds 	return end - buffer;
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds 
96800977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
9691da177e4SLinus Torvalds 	.read		= oom_adjust_read,
9701da177e4SLinus Torvalds 	.write		= oom_adjust_write,
9711da177e4SLinus Torvalds };
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
9741da177e4SLinus Torvalds #define TMPBUFLEN 21
9751da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
9761da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
9771da177e4SLinus Torvalds {
9782fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
97999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
9801da177e4SLinus Torvalds 	ssize_t length;
9811da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
9821da177e4SLinus Torvalds 
98399f89551SEric W. Biederman 	if (!task)
98499f89551SEric W. Biederman 		return -ESRCH;
9851da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
9860c11b942SAl Viro 				audit_get_loginuid(task));
98799f89551SEric W. Biederman 	put_task_struct(task);
9881da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
9921da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
9931da177e4SLinus Torvalds {
9942fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
9951da177e4SLinus Torvalds 	char *page, *tmp;
9961da177e4SLinus Torvalds 	ssize_t length;
9971da177e4SLinus Torvalds 	uid_t loginuid;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
10001da177e4SLinus Torvalds 		return -EPERM;
10011da177e4SLinus Torvalds 
100213b41b09SEric W. Biederman 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
10031da177e4SLinus Torvalds 		return -EPERM;
10041da177e4SLinus Torvalds 
1005e0182909SAl Viro 	if (count >= PAGE_SIZE)
1006e0182909SAl Viro 		count = PAGE_SIZE - 1;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	if (*ppos != 0) {
10091da177e4SLinus Torvalds 		/* No partial writes. */
10101da177e4SLinus Torvalds 		return -EINVAL;
10111da177e4SLinus Torvalds 	}
1012e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
10131da177e4SLinus Torvalds 	if (!page)
10141da177e4SLinus Torvalds 		return -ENOMEM;
10151da177e4SLinus Torvalds 	length = -EFAULT;
10161da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
10171da177e4SLinus Torvalds 		goto out_free_page;
10181da177e4SLinus Torvalds 
1019e0182909SAl Viro 	page[count] = '\0';
10201da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
10211da177e4SLinus Torvalds 	if (tmp == page) {
10221da177e4SLinus Torvalds 		length = -EINVAL;
10231da177e4SLinus Torvalds 		goto out_free_page;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	}
102699f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
10271da177e4SLinus Torvalds 	if (likely(length == 0))
10281da177e4SLinus Torvalds 		length = count;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds out_free_page:
10311da177e4SLinus Torvalds 	free_page((unsigned long) page);
10321da177e4SLinus Torvalds 	return length;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
103500977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
10361da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
10371da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
10381da177e4SLinus Torvalds };
10391da177e4SLinus Torvalds #endif
10401da177e4SLinus Torvalds 
1041f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1042f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1043f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1044f4f154fdSAkinobu Mita {
1045f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1046f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1047f4f154fdSAkinobu Mita 	size_t len;
1048f4f154fdSAkinobu Mita 	int make_it_fail;
1049f4f154fdSAkinobu Mita 
1050f4f154fdSAkinobu Mita 	if (!task)
1051f4f154fdSAkinobu Mita 		return -ESRCH;
1052f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1053f4f154fdSAkinobu Mita 	put_task_struct(task);
1054f4f154fdSAkinobu Mita 
1055f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
10560c28f287SAkinobu Mita 
10570c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1058f4f154fdSAkinobu Mita }
1059f4f154fdSAkinobu Mita 
1060f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1061f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1062f4f154fdSAkinobu Mita {
1063f4f154fdSAkinobu Mita 	struct task_struct *task;
1064f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1065f4f154fdSAkinobu Mita 	int make_it_fail;
1066f4f154fdSAkinobu Mita 
1067f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1068f4f154fdSAkinobu Mita 		return -EPERM;
1069f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1070f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1071f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1072f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1073f4f154fdSAkinobu Mita 		return -EFAULT;
1074f4f154fdSAkinobu Mita 	make_it_fail = simple_strtol(buffer, &end, 0);
1075f4f154fdSAkinobu Mita 	if (*end == '\n')
1076f4f154fdSAkinobu Mita 		end++;
1077f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1078f4f154fdSAkinobu Mita 	if (!task)
1079f4f154fdSAkinobu Mita 		return -ESRCH;
1080f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1081f4f154fdSAkinobu Mita 	put_task_struct(task);
1082f4f154fdSAkinobu Mita 	if (end - buffer == 0)
1083f4f154fdSAkinobu Mita 		return -EIO;
1084f4f154fdSAkinobu Mita 	return end - buffer;
1085f4f154fdSAkinobu Mita }
1086f4f154fdSAkinobu Mita 
108700977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1088f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1089f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
1090f4f154fdSAkinobu Mita };
1091f4f154fdSAkinobu Mita #endif
1092f4f154fdSAkinobu Mita 
10939745512cSArjan van de Ven 
109443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
109543ae34cbSIngo Molnar /*
109643ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
109743ae34cbSIngo Molnar  */
109843ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
109943ae34cbSIngo Molnar {
110043ae34cbSIngo Molnar 	struct inode *inode = m->private;
110143ae34cbSIngo Molnar 	struct task_struct *p;
110243ae34cbSIngo Molnar 
110343ae34cbSIngo Molnar 	WARN_ON(!inode);
110443ae34cbSIngo Molnar 
110543ae34cbSIngo Molnar 	p = get_proc_task(inode);
110643ae34cbSIngo Molnar 	if (!p)
110743ae34cbSIngo Molnar 		return -ESRCH;
110843ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
110943ae34cbSIngo Molnar 
111043ae34cbSIngo Molnar 	put_task_struct(p);
111143ae34cbSIngo Molnar 
111243ae34cbSIngo Molnar 	return 0;
111343ae34cbSIngo Molnar }
111443ae34cbSIngo Molnar 
111543ae34cbSIngo Molnar static ssize_t
111643ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
111743ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
111843ae34cbSIngo Molnar {
111943ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
112043ae34cbSIngo Molnar 	struct task_struct *p;
112143ae34cbSIngo Molnar 
112243ae34cbSIngo Molnar 	WARN_ON(!inode);
112343ae34cbSIngo Molnar 
112443ae34cbSIngo Molnar 	p = get_proc_task(inode);
112543ae34cbSIngo Molnar 	if (!p)
112643ae34cbSIngo Molnar 		return -ESRCH;
112743ae34cbSIngo Molnar 	proc_sched_set_task(p);
112843ae34cbSIngo Molnar 
112943ae34cbSIngo Molnar 	put_task_struct(p);
113043ae34cbSIngo Molnar 
113143ae34cbSIngo Molnar 	return count;
113243ae34cbSIngo Molnar }
113343ae34cbSIngo Molnar 
113443ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
113543ae34cbSIngo Molnar {
113643ae34cbSIngo Molnar 	int ret;
113743ae34cbSIngo Molnar 
113843ae34cbSIngo Molnar 	ret = single_open(filp, sched_show, NULL);
113943ae34cbSIngo Molnar 	if (!ret) {
114043ae34cbSIngo Molnar 		struct seq_file *m = filp->private_data;
114143ae34cbSIngo Molnar 
114243ae34cbSIngo Molnar 		m->private = inode;
114343ae34cbSIngo Molnar 	}
114443ae34cbSIngo Molnar 	return ret;
114543ae34cbSIngo Molnar }
114643ae34cbSIngo Molnar 
114743ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
114843ae34cbSIngo Molnar 	.open		= sched_open,
114943ae34cbSIngo Molnar 	.read		= seq_read,
115043ae34cbSIngo Molnar 	.write		= sched_write,
115143ae34cbSIngo Molnar 	.llseek		= seq_lseek,
11525ea473a1SAlexey Dobriyan 	.release	= single_release,
115343ae34cbSIngo Molnar };
115443ae34cbSIngo Molnar 
115543ae34cbSIngo Molnar #endif
115643ae34cbSIngo Molnar 
1157008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
11601da177e4SLinus Torvalds 	int error = -EACCES;
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
11631d957f9bSJan Blunck 	path_put(&nd->path);
11641da177e4SLinus Torvalds 
1165778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1166778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
11671da177e4SLinus Torvalds 		goto out;
11681da177e4SLinus Torvalds 
11693dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
11701da177e4SLinus Torvalds 	nd->last_type = LAST_BIND;
11711da177e4SLinus Torvalds out:
1172008b150aSAl Viro 	return ERR_PTR(error);
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
11753dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
11761da177e4SLinus Torvalds {
1177e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
11783dcd25f3SJan Blunck 	char *pathname;
11791da177e4SLinus Torvalds 	int len;
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	if (!tmp)
11821da177e4SLinus Torvalds 		return -ENOMEM;
11831da177e4SLinus Torvalds 
1184cf28b486SJan Blunck 	pathname = d_path(path, tmp, PAGE_SIZE);
11853dcd25f3SJan Blunck 	len = PTR_ERR(pathname);
11863dcd25f3SJan Blunck 	if (IS_ERR(pathname))
11871da177e4SLinus Torvalds 		goto out;
11883dcd25f3SJan Blunck 	len = tmp + PAGE_SIZE - 1 - pathname;
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	if (len > buflen)
11911da177e4SLinus Torvalds 		len = buflen;
11923dcd25f3SJan Blunck 	if (copy_to_user(buffer, pathname, len))
11931da177e4SLinus Torvalds 		len = -EFAULT;
11941da177e4SLinus Torvalds  out:
11951da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
11961da177e4SLinus Torvalds 	return len;
11971da177e4SLinus Torvalds }
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
12001da177e4SLinus Torvalds {
12011da177e4SLinus Torvalds 	int error = -EACCES;
12021da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
12033dcd25f3SJan Blunck 	struct path path;
12041da177e4SLinus Torvalds 
1205778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1206778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
12071da177e4SLinus Torvalds 		goto out;
12081da177e4SLinus Torvalds 
12093dcd25f3SJan Blunck 	error = PROC_I(inode)->op.proc_get_link(inode, &path);
12101da177e4SLinus Torvalds 	if (error)
12111da177e4SLinus Torvalds 		goto out;
12121da177e4SLinus Torvalds 
12133dcd25f3SJan Blunck 	error = do_proc_readlink(&path, buffer, buflen);
12143dcd25f3SJan Blunck 	path_put(&path);
12151da177e4SLinus Torvalds out:
12161da177e4SLinus Torvalds 	return error;
12171da177e4SLinus Torvalds }
12181da177e4SLinus Torvalds 
1219c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
12201da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
12216d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
12226d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
12231da177e4SLinus Torvalds };
12241da177e4SLinus Torvalds 
122528a6d671SEric W. Biederman 
122628a6d671SEric W. Biederman /* building an inode */
122728a6d671SEric W. Biederman 
122828a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
122928a6d671SEric W. Biederman {
123028a6d671SEric W. Biederman 	int dumpable = 0;
123128a6d671SEric W. Biederman 	struct mm_struct *mm;
123228a6d671SEric W. Biederman 
123328a6d671SEric W. Biederman 	task_lock(task);
123428a6d671SEric W. Biederman 	mm = task->mm;
123528a6d671SEric W. Biederman 	if (mm)
12366c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
123728a6d671SEric W. Biederman 	task_unlock(task);
123828a6d671SEric W. Biederman 	if(dumpable == 1)
123928a6d671SEric W. Biederman 		return 1;
124028a6d671SEric W. Biederman 	return 0;
124128a6d671SEric W. Biederman }
124228a6d671SEric W. Biederman 
124328a6d671SEric W. Biederman 
124461a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
124528a6d671SEric W. Biederman {
124628a6d671SEric W. Biederman 	struct inode * inode;
124728a6d671SEric W. Biederman 	struct proc_inode *ei;
124828a6d671SEric W. Biederman 
124928a6d671SEric W. Biederman 	/* We need a new inode */
125028a6d671SEric W. Biederman 
125128a6d671SEric W. Biederman 	inode = new_inode(sb);
125228a6d671SEric W. Biederman 	if (!inode)
125328a6d671SEric W. Biederman 		goto out;
125428a6d671SEric W. Biederman 
125528a6d671SEric W. Biederman 	/* Common stuff */
125628a6d671SEric W. Biederman 	ei = PROC_I(inode);
125728a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
125828a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
125928a6d671SEric W. Biederman 
126028a6d671SEric W. Biederman 	/*
126128a6d671SEric W. Biederman 	 * grab the reference to task.
126228a6d671SEric W. Biederman 	 */
12631a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
126428a6d671SEric W. Biederman 	if (!ei->pid)
126528a6d671SEric W. Biederman 		goto out_unlock;
126628a6d671SEric W. Biederman 
126728a6d671SEric W. Biederman 	inode->i_uid = 0;
126828a6d671SEric W. Biederman 	inode->i_gid = 0;
126928a6d671SEric W. Biederman 	if (task_dumpable(task)) {
127028a6d671SEric W. Biederman 		inode->i_uid = task->euid;
127128a6d671SEric W. Biederman 		inode->i_gid = task->egid;
127228a6d671SEric W. Biederman 	}
127328a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
127428a6d671SEric W. Biederman 
127528a6d671SEric W. Biederman out:
127628a6d671SEric W. Biederman 	return inode;
127728a6d671SEric W. Biederman 
127828a6d671SEric W. Biederman out_unlock:
127928a6d671SEric W. Biederman 	iput(inode);
128028a6d671SEric W. Biederman 	return NULL;
128128a6d671SEric W. Biederman }
128228a6d671SEric W. Biederman 
128328a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
128428a6d671SEric W. Biederman {
128528a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
128628a6d671SEric W. Biederman 	struct task_struct *task;
128728a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
128828a6d671SEric W. Biederman 
128928a6d671SEric W. Biederman 	rcu_read_lock();
129028a6d671SEric W. Biederman 	stat->uid = 0;
129128a6d671SEric W. Biederman 	stat->gid = 0;
129228a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
129328a6d671SEric W. Biederman 	if (task) {
129428a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
129528a6d671SEric W. Biederman 		    task_dumpable(task)) {
129628a6d671SEric W. Biederman 			stat->uid = task->euid;
129728a6d671SEric W. Biederman 			stat->gid = task->egid;
129828a6d671SEric W. Biederman 		}
129928a6d671SEric W. Biederman 	}
130028a6d671SEric W. Biederman 	rcu_read_unlock();
130128a6d671SEric W. Biederman 	return 0;
130228a6d671SEric W. Biederman }
130328a6d671SEric W. Biederman 
130428a6d671SEric W. Biederman /* dentry stuff */
130528a6d671SEric W. Biederman 
130628a6d671SEric W. Biederman /*
130728a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
130828a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
130928a6d671SEric W. Biederman  * due to the way we treat inodes.
131028a6d671SEric W. Biederman  *
131128a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
131228a6d671SEric W. Biederman  * performed a setuid(), etc.
131328a6d671SEric W. Biederman  *
131428a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
131528a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
131628a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
131728a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
131828a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
131928a6d671SEric W. Biederman  * directories.
132028a6d671SEric W. Biederman  */
132128a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
132228a6d671SEric W. Biederman {
132328a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
132428a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
132528a6d671SEric W. Biederman 	if (task) {
132628a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
132728a6d671SEric W. Biederman 		    task_dumpable(task)) {
132828a6d671SEric W. Biederman 			inode->i_uid = task->euid;
132928a6d671SEric W. Biederman 			inode->i_gid = task->egid;
133028a6d671SEric W. Biederman 		} else {
133128a6d671SEric W. Biederman 			inode->i_uid = 0;
133228a6d671SEric W. Biederman 			inode->i_gid = 0;
133328a6d671SEric W. Biederman 		}
133428a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
133528a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
133628a6d671SEric W. Biederman 		put_task_struct(task);
133728a6d671SEric W. Biederman 		return 1;
133828a6d671SEric W. Biederman 	}
133928a6d671SEric W. Biederman 	d_drop(dentry);
134028a6d671SEric W. Biederman 	return 0;
134128a6d671SEric W. Biederman }
134228a6d671SEric W. Biederman 
134328a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry)
134428a6d671SEric W. Biederman {
134528a6d671SEric W. Biederman 	/* Is the task we represent dead?
134628a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
134728a6d671SEric W. Biederman 	 * kill it immediately.
134828a6d671SEric W. Biederman 	 */
134928a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
135028a6d671SEric W. Biederman }
135128a6d671SEric W. Biederman 
135228a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations =
135328a6d671SEric W. Biederman {
135428a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
135528a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
135628a6d671SEric W. Biederman };
135728a6d671SEric W. Biederman 
135828a6d671SEric W. Biederman /* Lookups */
135928a6d671SEric W. Biederman 
1360c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1361c5141e6dSEric Dumazet 				struct task_struct *, const void *);
136261a28784SEric W. Biederman 
13631c0d04c9SEric W. Biederman /*
13641c0d04c9SEric W. Biederman  * Fill a directory entry.
13651c0d04c9SEric W. Biederman  *
13661c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
13671c0d04c9SEric W. Biederman  * file type from dcache entry.
13681c0d04c9SEric W. Biederman  *
13691c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
13701c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
13711c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
13721c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
13731c0d04c9SEric W. Biederman  * by stat.
13741c0d04c9SEric W. Biederman  */
137561a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
137661a28784SEric W. Biederman 	char *name, int len,
1377c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
137861a28784SEric W. Biederman {
13792fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
138061a28784SEric W. Biederman 	struct inode *inode;
138161a28784SEric W. Biederman 	struct qstr qname;
138261a28784SEric W. Biederman 	ino_t ino = 0;
138361a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
138461a28784SEric W. Biederman 
138561a28784SEric W. Biederman 	qname.name = name;
138661a28784SEric W. Biederman 	qname.len  = len;
138761a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
138861a28784SEric W. Biederman 
138961a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
139061a28784SEric W. Biederman 	if (!child) {
139161a28784SEric W. Biederman 		struct dentry *new;
139261a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
139361a28784SEric W. Biederman 		if (new) {
139461a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
139561a28784SEric W. Biederman 			if (child)
139661a28784SEric W. Biederman 				dput(new);
139761a28784SEric W. Biederman 			else
139861a28784SEric W. Biederman 				child = new;
139961a28784SEric W. Biederman 		}
140061a28784SEric W. Biederman 	}
140161a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
140261a28784SEric W. Biederman 		goto end_instantiate;
140361a28784SEric W. Biederman 	inode = child->d_inode;
140461a28784SEric W. Biederman 	if (inode) {
140561a28784SEric W. Biederman 		ino = inode->i_ino;
140661a28784SEric W. Biederman 		type = inode->i_mode >> 12;
140761a28784SEric W. Biederman 	}
140861a28784SEric W. Biederman 	dput(child);
140961a28784SEric W. Biederman end_instantiate:
141061a28784SEric W. Biederman 	if (!ino)
141161a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
141261a28784SEric W. Biederman 	if (!ino)
141361a28784SEric W. Biederman 		ino = 1;
141461a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
141561a28784SEric W. Biederman }
141661a28784SEric W. Biederman 
141728a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
141828a6d671SEric W. Biederman {
141928a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
142028a6d671SEric W. Biederman 	int len = dentry->d_name.len;
142128a6d671SEric W. Biederman 	unsigned n = 0;
142228a6d671SEric W. Biederman 
142328a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
142428a6d671SEric W. Biederman 		goto out;
142528a6d671SEric W. Biederman 	while (len-- > 0) {
142628a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
142728a6d671SEric W. Biederman 		if (c > 9)
142828a6d671SEric W. Biederman 			goto out;
142928a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
143028a6d671SEric W. Biederman 			goto out;
143128a6d671SEric W. Biederman 		n *= 10;
143228a6d671SEric W. Biederman 		n += c;
143328a6d671SEric W. Biederman 	}
143428a6d671SEric W. Biederman 	return n;
143528a6d671SEric W. Biederman out:
143628a6d671SEric W. Biederman 	return ~0U;
143728a6d671SEric W. Biederman }
143828a6d671SEric W. Biederman 
143927932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
144027932742SMiklos Szeredi 
14413dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info)
144228a6d671SEric W. Biederman {
144328a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
144428a6d671SEric W. Biederman 	struct files_struct *files = NULL;
144528a6d671SEric W. Biederman 	struct file *file;
144628a6d671SEric W. Biederman 	int fd = proc_fd(inode);
144728a6d671SEric W. Biederman 
144828a6d671SEric W. Biederman 	if (task) {
144928a6d671SEric W. Biederman 		files = get_files_struct(task);
145028a6d671SEric W. Biederman 		put_task_struct(task);
145128a6d671SEric W. Biederman 	}
145228a6d671SEric W. Biederman 	if (files) {
145328a6d671SEric W. Biederman 		/*
145428a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
145528a6d671SEric W. Biederman 		 * hold ->file_lock.
145628a6d671SEric W. Biederman 		 */
145728a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
145828a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
145928a6d671SEric W. Biederman 		if (file) {
14603dcd25f3SJan Blunck 			if (path) {
14613dcd25f3SJan Blunck 				*path = file->f_path;
14623dcd25f3SJan Blunck 				path_get(&file->f_path);
14633dcd25f3SJan Blunck 			}
146427932742SMiklos Szeredi 			if (info)
146527932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
146627932742SMiklos Szeredi 					 "pos:\t%lli\n"
146727932742SMiklos Szeredi 					 "flags:\t0%o\n",
146827932742SMiklos Szeredi 					 (long long) file->f_pos,
146927932742SMiklos Szeredi 					 file->f_flags);
147028a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
147128a6d671SEric W. Biederman 			put_files_struct(files);
147228a6d671SEric W. Biederman 			return 0;
147328a6d671SEric W. Biederman 		}
147428a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
147528a6d671SEric W. Biederman 		put_files_struct(files);
147628a6d671SEric W. Biederman 	}
147728a6d671SEric W. Biederman 	return -ENOENT;
147828a6d671SEric W. Biederman }
147928a6d671SEric W. Biederman 
14803dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path)
148127932742SMiklos Szeredi {
14823dcd25f3SJan Blunck 	return proc_fd_info(inode, path, NULL);
148327932742SMiklos Szeredi }
148427932742SMiklos Szeredi 
148528a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
148628a6d671SEric W. Biederman {
148728a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
148828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
148928a6d671SEric W. Biederman 	int fd = proc_fd(inode);
149028a6d671SEric W. Biederman 	struct files_struct *files;
149128a6d671SEric W. Biederman 
149228a6d671SEric W. Biederman 	if (task) {
149328a6d671SEric W. Biederman 		files = get_files_struct(task);
149428a6d671SEric W. Biederman 		if (files) {
149528a6d671SEric W. Biederman 			rcu_read_lock();
149628a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
149728a6d671SEric W. Biederman 				rcu_read_unlock();
149828a6d671SEric W. Biederman 				put_files_struct(files);
149928a6d671SEric W. Biederman 				if (task_dumpable(task)) {
150028a6d671SEric W. Biederman 					inode->i_uid = task->euid;
150128a6d671SEric W. Biederman 					inode->i_gid = task->egid;
150228a6d671SEric W. Biederman 				} else {
150328a6d671SEric W. Biederman 					inode->i_uid = 0;
150428a6d671SEric W. Biederman 					inode->i_gid = 0;
150528a6d671SEric W. Biederman 				}
150628a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
150728a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
150828a6d671SEric W. Biederman 				put_task_struct(task);
150928a6d671SEric W. Biederman 				return 1;
151028a6d671SEric W. Biederman 			}
151128a6d671SEric W. Biederman 			rcu_read_unlock();
151228a6d671SEric W. Biederman 			put_files_struct(files);
151328a6d671SEric W. Biederman 		}
151428a6d671SEric W. Biederman 		put_task_struct(task);
151528a6d671SEric W. Biederman 	}
151628a6d671SEric W. Biederman 	d_drop(dentry);
151728a6d671SEric W. Biederman 	return 0;
151828a6d671SEric W. Biederman }
151928a6d671SEric W. Biederman 
152028a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations =
152128a6d671SEric W. Biederman {
152228a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
152328a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
152428a6d671SEric W. Biederman };
152528a6d671SEric W. Biederman 
1526444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1527c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
152828a6d671SEric W. Biederman {
1529c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
153028a6d671SEric W. Biederman 	struct file *file;
153128a6d671SEric W. Biederman 	struct files_struct *files;
153228a6d671SEric W. Biederman  	struct inode *inode;
153328a6d671SEric W. Biederman  	struct proc_inode *ei;
1534444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
153528a6d671SEric W. Biederman 
153661a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
153728a6d671SEric W. Biederman 	if (!inode)
153828a6d671SEric W. Biederman 		goto out;
153928a6d671SEric W. Biederman 	ei = PROC_I(inode);
154028a6d671SEric W. Biederman 	ei->fd = fd;
154128a6d671SEric W. Biederman 	files = get_files_struct(task);
154228a6d671SEric W. Biederman 	if (!files)
1543444ceed8SEric W. Biederman 		goto out_iput;
154428a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
154528a6d671SEric W. Biederman 
154628a6d671SEric W. Biederman 	/*
154728a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
154828a6d671SEric W. Biederman 	 * hold ->file_lock.
154928a6d671SEric W. Biederman 	 */
155028a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
155128a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
155228a6d671SEric W. Biederman 	if (!file)
1553444ceed8SEric W. Biederman 		goto out_unlock;
155428a6d671SEric W. Biederman 	if (file->f_mode & 1)
155528a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
155628a6d671SEric W. Biederman 	if (file->f_mode & 2)
155728a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
155828a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
155928a6d671SEric W. Biederman 	put_files_struct(files);
1560444ceed8SEric W. Biederman 
156128a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
156228a6d671SEric W. Biederman 	inode->i_size = 64;
156328a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
156428a6d671SEric W. Biederman 	dentry->d_op = &tid_fd_dentry_operations;
156528a6d671SEric W. Biederman 	d_add(dentry, inode);
156628a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
156728a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1568444ceed8SEric W. Biederman 		error = NULL;
1569444ceed8SEric W. Biederman 
1570444ceed8SEric W. Biederman  out:
1571444ceed8SEric W. Biederman 	return error;
1572444ceed8SEric W. Biederman out_unlock:
1573444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1574444ceed8SEric W. Biederman 	put_files_struct(files);
1575444ceed8SEric W. Biederman out_iput:
1576444ceed8SEric W. Biederman 	iput(inode);
1577444ceed8SEric W. Biederman 	goto out;
1578444ceed8SEric W. Biederman }
1579444ceed8SEric W. Biederman 
158027932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
158127932742SMiklos Szeredi 					   struct dentry *dentry,
158227932742SMiklos Szeredi 					   instantiate_t instantiate)
1583444ceed8SEric W. Biederman {
1584444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1585444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1586444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1587444ceed8SEric W. Biederman 
1588444ceed8SEric W. Biederman 	if (!task)
1589444ceed8SEric W. Biederman 		goto out_no_task;
1590444ceed8SEric W. Biederman 	if (fd == ~0U)
1591444ceed8SEric W. Biederman 		goto out;
1592444ceed8SEric W. Biederman 
159327932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
159428a6d671SEric W. Biederman out:
159528a6d671SEric W. Biederman 	put_task_struct(task);
159628a6d671SEric W. Biederman out_no_task:
159728a6d671SEric W. Biederman 	return result;
159828a6d671SEric W. Biederman }
159928a6d671SEric W. Biederman 
160027932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
160127932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
16021da177e4SLinus Torvalds {
16032fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
16045634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
160599f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1606457c2510SPavel Emelyanov 	unsigned int fd, ino;
16071da177e4SLinus Torvalds 	int retval;
16081da177e4SLinus Torvalds 	struct files_struct * files;
1609badf1662SDipankar Sarma 	struct fdtable *fdt;
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	retval = -ENOENT;
161299f89551SEric W. Biederman 	if (!p)
161399f89551SEric W. Biederman 		goto out_no_task;
16141da177e4SLinus Torvalds 	retval = 0;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	fd = filp->f_pos;
16171da177e4SLinus Torvalds 	switch (fd) {
16181da177e4SLinus Torvalds 		case 0:
16191da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
16201da177e4SLinus Torvalds 				goto out;
16211da177e4SLinus Torvalds 			filp->f_pos++;
16221da177e4SLinus Torvalds 		case 1:
16235634708bSEric W. Biederman 			ino = parent_ino(dentry);
16241da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
16251da177e4SLinus Torvalds 				goto out;
16261da177e4SLinus Torvalds 			filp->f_pos++;
16271da177e4SLinus Torvalds 		default:
16281da177e4SLinus Torvalds 			files = get_files_struct(p);
16291da177e4SLinus Torvalds 			if (!files)
16301da177e4SLinus Torvalds 				goto out;
1631b835996fSDipankar Sarma 			rcu_read_lock();
1632badf1662SDipankar Sarma 			fdt = files_fdtable(files);
16331da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
1634badf1662SDipankar Sarma 			     fd < fdt->max_fds;
16351da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
163627932742SMiklos Szeredi 				char name[PROC_NUMBUF];
163727932742SMiklos Szeredi 				int len;
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
16401da177e4SLinus Torvalds 					continue;
1641b835996fSDipankar Sarma 				rcu_read_unlock();
16421da177e4SLinus Torvalds 
164327932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
164427932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
164527932742SMiklos Szeredi 						    name, len, instantiate,
164627932742SMiklos Szeredi 						    p, &fd) < 0) {
1647b835996fSDipankar Sarma 					rcu_read_lock();
16481da177e4SLinus Torvalds 					break;
16491da177e4SLinus Torvalds 				}
1650b835996fSDipankar Sarma 				rcu_read_lock();
16511da177e4SLinus Torvalds 			}
1652b835996fSDipankar Sarma 			rcu_read_unlock();
16531da177e4SLinus Torvalds 			put_files_struct(files);
16541da177e4SLinus Torvalds 	}
16551da177e4SLinus Torvalds out:
165699f89551SEric W. Biederman 	put_task_struct(p);
165799f89551SEric W. Biederman out_no_task:
16581da177e4SLinus Torvalds 	return retval;
16591da177e4SLinus Torvalds }
16601da177e4SLinus Torvalds 
166127932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
166227932742SMiklos Szeredi 				    struct nameidata *nd)
166327932742SMiklos Szeredi {
166427932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
166527932742SMiklos Szeredi }
166627932742SMiklos Szeredi 
166727932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
166827932742SMiklos Szeredi {
166927932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
167027932742SMiklos Szeredi }
167127932742SMiklos Szeredi 
167227932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
167327932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
167427932742SMiklos Szeredi {
167527932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
16763dcd25f3SJan Blunck 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
167727932742SMiklos Szeredi 	if (!err)
167827932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
167927932742SMiklos Szeredi 	return err;
168027932742SMiklos Szeredi }
168127932742SMiklos Szeredi 
168227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
168327932742SMiklos Szeredi 	.open		= nonseekable_open,
168427932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
168527932742SMiklos Szeredi };
168627932742SMiklos Szeredi 
168700977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
16881da177e4SLinus Torvalds 	.read		= generic_read_dir,
16891da177e4SLinus Torvalds 	.readdir	= proc_readfd,
16901da177e4SLinus Torvalds };
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds /*
16938948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
16948948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
16958948e11fSAlexey Dobriyan  */
16968948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask,
16978948e11fSAlexey Dobriyan 				struct nameidata *nd)
16988948e11fSAlexey Dobriyan {
16998948e11fSAlexey Dobriyan 	int rv;
17008948e11fSAlexey Dobriyan 
17018948e11fSAlexey Dobriyan 	rv = generic_permission(inode, mask, NULL);
17028948e11fSAlexey Dobriyan 	if (rv == 0)
17038948e11fSAlexey Dobriyan 		return 0;
17048948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
17058948e11fSAlexey Dobriyan 		rv = 0;
17068948e11fSAlexey Dobriyan 	return rv;
17078948e11fSAlexey Dobriyan }
17088948e11fSAlexey Dobriyan 
17098948e11fSAlexey Dobriyan /*
17101da177e4SLinus Torvalds  * proc directories can do almost nothing..
17111da177e4SLinus Torvalds  */
1712c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
17131da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
17148948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
17156d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
17161da177e4SLinus Torvalds };
17171da177e4SLinus Torvalds 
171827932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
171927932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
172027932742SMiklos Szeredi {
172127932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
172227932742SMiklos Szeredi  	struct inode *inode;
172327932742SMiklos Szeredi  	struct proc_inode *ei;
172427932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
172527932742SMiklos Szeredi 
172627932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
172727932742SMiklos Szeredi 	if (!inode)
172827932742SMiklos Szeredi 		goto out;
172927932742SMiklos Szeredi 	ei = PROC_I(inode);
173027932742SMiklos Szeredi 	ei->fd = fd;
173127932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
173227932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
173327932742SMiklos Szeredi 	dentry->d_op = &tid_fd_dentry_operations;
173427932742SMiklos Szeredi 	d_add(dentry, inode);
173527932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
173627932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
173727932742SMiklos Szeredi 		error = NULL;
173827932742SMiklos Szeredi 
173927932742SMiklos Szeredi  out:
174027932742SMiklos Szeredi 	return error;
174127932742SMiklos Szeredi }
174227932742SMiklos Szeredi 
174327932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
174427932742SMiklos Szeredi 					struct dentry *dentry,
174527932742SMiklos Szeredi 					struct nameidata *nd)
174627932742SMiklos Szeredi {
174727932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
174827932742SMiklos Szeredi }
174927932742SMiklos Szeredi 
175027932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
175127932742SMiklos Szeredi {
175227932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
175327932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
175427932742SMiklos Szeredi }
175527932742SMiklos Szeredi 
175627932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
175727932742SMiklos Szeredi 	.read		= generic_read_dir,
175827932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
175927932742SMiklos Szeredi };
176027932742SMiklos Szeredi 
176127932742SMiklos Szeredi /*
176227932742SMiklos Szeredi  * proc directories can do almost nothing..
176327932742SMiklos Szeredi  */
176427932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
176527932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
176627932742SMiklos Szeredi 	.setattr	= proc_setattr,
176727932742SMiklos Szeredi };
176827932742SMiklos Szeredi 
176927932742SMiklos Szeredi 
1770444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
1771c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
1772444ceed8SEric W. Biederman {
1773c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
1774444ceed8SEric W. Biederman 	struct inode *inode;
1775444ceed8SEric W. Biederman 	struct proc_inode *ei;
1776444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-EINVAL);
1777444ceed8SEric W. Biederman 
177861a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
1779444ceed8SEric W. Biederman 	if (!inode)
1780444ceed8SEric W. Biederman 		goto out;
1781444ceed8SEric W. Biederman 
1782444ceed8SEric W. Biederman 	ei = PROC_I(inode);
1783444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
1784444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
1785444ceed8SEric W. Biederman 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
1786444ceed8SEric W. Biederman 	if (p->iop)
1787444ceed8SEric W. Biederman 		inode->i_op = p->iop;
1788444ceed8SEric W. Biederman 	if (p->fop)
1789444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
1790444ceed8SEric W. Biederman 	ei->op = p->op;
1791444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
1792444ceed8SEric W. Biederman 	d_add(dentry, inode);
1793444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
1794444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
1795444ceed8SEric W. Biederman 		error = NULL;
1796444ceed8SEric W. Biederman out:
1797444ceed8SEric W. Biederman 	return error;
1798444ceed8SEric W. Biederman }
1799444ceed8SEric W. Biederman 
18001da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
18011da177e4SLinus Torvalds 					 struct dentry *dentry,
1802c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
18037bcd6b0eSEric W. Biederman 					 unsigned int nents)
18041da177e4SLinus Torvalds {
18051da177e4SLinus Torvalds 	struct inode *inode;
1806cd6a3ce9SEric W. Biederman 	struct dentry *error;
180799f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1808c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
18091da177e4SLinus Torvalds 
1810cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
18111da177e4SLinus Torvalds 	inode = NULL;
18121da177e4SLinus Torvalds 
181399f89551SEric W. Biederman 	if (!task)
181499f89551SEric W. Biederman 		goto out_no_task;
18151da177e4SLinus Torvalds 
181620cdc894SEric W. Biederman 	/*
181720cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
181820cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
181920cdc894SEric W. Biederman 	 */
18207bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
18217bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
18221da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
18231da177e4SLinus Torvalds 			continue;
18241da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
18251da177e4SLinus Torvalds 			break;
18261da177e4SLinus Torvalds 	}
18277bcd6b0eSEric W. Biederman 	if (p > last)
18281da177e4SLinus Torvalds 		goto out;
18291da177e4SLinus Torvalds 
1830444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
18311da177e4SLinus Torvalds out:
183299f89551SEric W. Biederman 	put_task_struct(task);
183399f89551SEric W. Biederman out_no_task:
1834cd6a3ce9SEric W. Biederman 	return error;
18351da177e4SLinus Torvalds }
18361da177e4SLinus Torvalds 
1837c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
1838c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
183961a28784SEric W. Biederman {
184061a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
184161a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
184261a28784SEric W. Biederman }
184361a28784SEric W. Biederman 
184428a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
184528a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
1846c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
184728a6d671SEric W. Biederman {
184828a6d671SEric W. Biederman 	int i;
18492fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
185028a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
185128a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1852c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
185328a6d671SEric W. Biederman 	ino_t ino;
185428a6d671SEric W. Biederman 	int ret;
185528a6d671SEric W. Biederman 
185628a6d671SEric W. Biederman 	ret = -ENOENT;
185728a6d671SEric W. Biederman 	if (!task)
185861a28784SEric W. Biederman 		goto out_no_task;
185928a6d671SEric W. Biederman 
186028a6d671SEric W. Biederman 	ret = 0;
186128a6d671SEric W. Biederman 	i = filp->f_pos;
186228a6d671SEric W. Biederman 	switch (i) {
186328a6d671SEric W. Biederman 	case 0:
186428a6d671SEric W. Biederman 		ino = inode->i_ino;
186528a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
186628a6d671SEric W. Biederman 			goto out;
186728a6d671SEric W. Biederman 		i++;
186828a6d671SEric W. Biederman 		filp->f_pos++;
186928a6d671SEric W. Biederman 		/* fall through */
187028a6d671SEric W. Biederman 	case 1:
187128a6d671SEric W. Biederman 		ino = parent_ino(dentry);
187228a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
187328a6d671SEric W. Biederman 			goto out;
187428a6d671SEric W. Biederman 		i++;
187528a6d671SEric W. Biederman 		filp->f_pos++;
187628a6d671SEric W. Biederman 		/* fall through */
187728a6d671SEric W. Biederman 	default:
187828a6d671SEric W. Biederman 		i -= 2;
187928a6d671SEric W. Biederman 		if (i >= nents) {
188028a6d671SEric W. Biederman 			ret = 1;
188128a6d671SEric W. Biederman 			goto out;
188228a6d671SEric W. Biederman 		}
188328a6d671SEric W. Biederman 		p = ents + i;
18847bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
18857bcd6b0eSEric W. Biederman 		while (p <= last) {
188661a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
188728a6d671SEric W. Biederman 				goto out;
188828a6d671SEric W. Biederman 			filp->f_pos++;
188928a6d671SEric W. Biederman 			p++;
189028a6d671SEric W. Biederman 		}
18911da177e4SLinus Torvalds 	}
18921da177e4SLinus Torvalds 
189328a6d671SEric W. Biederman 	ret = 1;
189428a6d671SEric W. Biederman out:
189561a28784SEric W. Biederman 	put_task_struct(task);
189661a28784SEric W. Biederman out_no_task:
189728a6d671SEric W. Biederman 	return ret;
18981da177e4SLinus Torvalds }
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
190128a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
190228a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
190328a6d671SEric W. Biederman {
19042fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
190504ff9708SAl Viro 	char *p = NULL;
190628a6d671SEric W. Biederman 	ssize_t length;
190728a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
190828a6d671SEric W. Biederman 
190928a6d671SEric W. Biederman 	if (!task)
191004ff9708SAl Viro 		return -ESRCH;
191128a6d671SEric W. Biederman 
191228a6d671SEric W. Biederman 	length = security_getprocattr(task,
19132fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
191404ff9708SAl Viro 				      &p);
191528a6d671SEric W. Biederman 	put_task_struct(task);
191604ff9708SAl Viro 	if (length > 0)
191704ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
191804ff9708SAl Viro 	kfree(p);
191928a6d671SEric W. Biederman 	return length;
192028a6d671SEric W. Biederman }
192128a6d671SEric W. Biederman 
192228a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
192328a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
192428a6d671SEric W. Biederman {
19252fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
192628a6d671SEric W. Biederman 	char *page;
192728a6d671SEric W. Biederman 	ssize_t length;
192828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
192928a6d671SEric W. Biederman 
193028a6d671SEric W. Biederman 	length = -ESRCH;
193128a6d671SEric W. Biederman 	if (!task)
193228a6d671SEric W. Biederman 		goto out_no_task;
193328a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
193428a6d671SEric W. Biederman 		count = PAGE_SIZE;
193528a6d671SEric W. Biederman 
193628a6d671SEric W. Biederman 	/* No partial writes. */
193728a6d671SEric W. Biederman 	length = -EINVAL;
193828a6d671SEric W. Biederman 	if (*ppos != 0)
193928a6d671SEric W. Biederman 		goto out;
194028a6d671SEric W. Biederman 
194128a6d671SEric W. Biederman 	length = -ENOMEM;
1942e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
194328a6d671SEric W. Biederman 	if (!page)
194428a6d671SEric W. Biederman 		goto out;
194528a6d671SEric W. Biederman 
194628a6d671SEric W. Biederman 	length = -EFAULT;
194728a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
194828a6d671SEric W. Biederman 		goto out_free;
194928a6d671SEric W. Biederman 
195028a6d671SEric W. Biederman 	length = security_setprocattr(task,
19512fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
195228a6d671SEric W. Biederman 				      (void*)page, count);
195328a6d671SEric W. Biederman out_free:
195428a6d671SEric W. Biederman 	free_page((unsigned long) page);
195528a6d671SEric W. Biederman out:
195628a6d671SEric W. Biederman 	put_task_struct(task);
195728a6d671SEric W. Biederman out_no_task:
195828a6d671SEric W. Biederman 	return length;
195928a6d671SEric W. Biederman }
196028a6d671SEric W. Biederman 
196100977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
196228a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
196328a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
196428a6d671SEric W. Biederman };
196528a6d671SEric W. Biederman 
1966c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
196761a28784SEric W. Biederman 	REG("current",    S_IRUGO|S_IWUGO, pid_attr),
196861a28784SEric W. Biederman 	REG("prev",       S_IRUGO,	   pid_attr),
196961a28784SEric W. Biederman 	REG("exec",       S_IRUGO|S_IWUGO, pid_attr),
197061a28784SEric W. Biederman 	REG("fscreate",   S_IRUGO|S_IWUGO, pid_attr),
197161a28784SEric W. Biederman 	REG("keycreate",  S_IRUGO|S_IWUGO, pid_attr),
197261a28784SEric W. Biederman 	REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
197328a6d671SEric W. Biederman };
197428a6d671SEric W. Biederman 
197572d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
19761da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
19771da177e4SLinus Torvalds {
19781da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
197972d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
198200977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
19831da177e4SLinus Torvalds 	.read		= generic_read_dir,
198472d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
19851da177e4SLinus Torvalds };
19861da177e4SLinus Torvalds 
198772d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
19881da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
19891da177e4SLinus Torvalds {
19907bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
19917bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
19921da177e4SLinus Torvalds }
19931da177e4SLinus Torvalds 
1994c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
199572d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
199699f89551SEric W. Biederman 	.getattr	= pid_getattr,
19976d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
19981da177e4SLinus Torvalds };
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds #endif
20011da177e4SLinus Torvalds 
20023cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
20033cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
20043cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
20053cb4a0bbSKawai, Hidehiro {
20063cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
20073cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
20083cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
20093cb4a0bbSKawai, Hidehiro 	size_t len;
20103cb4a0bbSKawai, Hidehiro 	int ret;
20113cb4a0bbSKawai, Hidehiro 
20123cb4a0bbSKawai, Hidehiro 	if (!task)
20133cb4a0bbSKawai, Hidehiro 		return -ESRCH;
20143cb4a0bbSKawai, Hidehiro 
20153cb4a0bbSKawai, Hidehiro 	ret = 0;
20163cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
20173cb4a0bbSKawai, Hidehiro 	if (mm) {
20183cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
20193cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
20203cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
20213cb4a0bbSKawai, Hidehiro 		mmput(mm);
20223cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
20233cb4a0bbSKawai, Hidehiro 	}
20243cb4a0bbSKawai, Hidehiro 
20253cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
20263cb4a0bbSKawai, Hidehiro 
20273cb4a0bbSKawai, Hidehiro 	return ret;
20283cb4a0bbSKawai, Hidehiro }
20293cb4a0bbSKawai, Hidehiro 
20303cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
20313cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
20323cb4a0bbSKawai, Hidehiro 					  size_t count,
20333cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
20343cb4a0bbSKawai, Hidehiro {
20353cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
20363cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
20373cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
20383cb4a0bbSKawai, Hidehiro 	unsigned int val;
20393cb4a0bbSKawai, Hidehiro 	int ret;
20403cb4a0bbSKawai, Hidehiro 	int i;
20413cb4a0bbSKawai, Hidehiro 	unsigned long mask;
20423cb4a0bbSKawai, Hidehiro 
20433cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
20443cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
20453cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
20463cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
20473cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
20483cb4a0bbSKawai, Hidehiro 		goto out_no_task;
20493cb4a0bbSKawai, Hidehiro 
20503cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
20513cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
20523cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
20533cb4a0bbSKawai, Hidehiro 		end++;
20543cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
20553cb4a0bbSKawai, Hidehiro 		goto out_no_task;
20563cb4a0bbSKawai, Hidehiro 
20573cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
20583cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
20593cb4a0bbSKawai, Hidehiro 	if (!task)
20603cb4a0bbSKawai, Hidehiro 		goto out_no_task;
20613cb4a0bbSKawai, Hidehiro 
20623cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
20633cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
20643cb4a0bbSKawai, Hidehiro 	if (!mm)
20653cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
20663cb4a0bbSKawai, Hidehiro 
20673cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
20683cb4a0bbSKawai, Hidehiro 		if (val & mask)
20693cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
20703cb4a0bbSKawai, Hidehiro 		else
20713cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
20723cb4a0bbSKawai, Hidehiro 	}
20733cb4a0bbSKawai, Hidehiro 
20743cb4a0bbSKawai, Hidehiro 	mmput(mm);
20753cb4a0bbSKawai, Hidehiro  out_no_mm:
20763cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
20773cb4a0bbSKawai, Hidehiro  out_no_task:
20783cb4a0bbSKawai, Hidehiro 	return ret;
20793cb4a0bbSKawai, Hidehiro }
20803cb4a0bbSKawai, Hidehiro 
20813cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
20823cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
20833cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
20843cb4a0bbSKawai, Hidehiro };
20853cb4a0bbSKawai, Hidehiro #endif
20863cb4a0bbSKawai, Hidehiro 
20871da177e4SLinus Torvalds /*
20881da177e4SLinus Torvalds  * /proc/self:
20891da177e4SLinus Torvalds  */
20901da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
20911da177e4SLinus Torvalds 			      int buflen)
20921da177e4SLinus Torvalds {
2093488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2094b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
20958578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2096b55fcb22SAndrew Morton 	if (!tgid)
2097488e5bc4SEric W. Biederman 		return -ENOENT;
2098b55fcb22SAndrew Morton 	sprintf(tmp, "%d", tgid);
20991da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds 
2102008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
21031da177e4SLinus Torvalds {
2104488e5bc4SEric W. Biederman 	struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2105b55fcb22SAndrew Morton 	pid_t tgid = task_tgid_nr_ns(current, ns);
21068578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2107b55fcb22SAndrew Morton 	if (!tgid)
2108488e5bc4SEric W. Biederman 		return ERR_PTR(-ENOENT);
2109b55fcb22SAndrew Morton 	sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2110008b150aSAl Viro 	return ERR_PTR(vfs_follow_link(nd,tmp));
21111da177e4SLinus Torvalds }
21121da177e4SLinus Torvalds 
2113c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
21141da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
21151da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
21161da177e4SLinus Torvalds };
21171da177e4SLinus Torvalds 
211828a6d671SEric W. Biederman /*
2119801199ceSEric W. Biederman  * proc base
2120801199ceSEric W. Biederman  *
2121801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2122801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2123801199ceSEric W. Biederman  * describe something that is process related.
2124801199ceSEric W. Biederman  */
2125c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
212661a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2127801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2128801199ceSEric W. Biederman };
2129801199ceSEric W. Biederman 
2130801199ceSEric W. Biederman /*
2131801199ceSEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
2132801199ceSEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
2133801199ceSEric W. Biederman  * due to the way we treat inodes.
2134801199ceSEric W. Biederman  */
2135801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2136801199ceSEric W. Biederman {
2137801199ceSEric W. Biederman 	struct inode *inode = dentry->d_inode;
2138801199ceSEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2139801199ceSEric W. Biederman 	if (task) {
2140801199ceSEric W. Biederman 		put_task_struct(task);
2141801199ceSEric W. Biederman 		return 1;
2142801199ceSEric W. Biederman 	}
2143801199ceSEric W. Biederman 	d_drop(dentry);
2144801199ceSEric W. Biederman 	return 0;
2145801199ceSEric W. Biederman }
2146801199ceSEric W. Biederman 
2147801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations =
2148801199ceSEric W. Biederman {
2149801199ceSEric W. Biederman 	.d_revalidate	= proc_base_revalidate,
2150801199ceSEric W. Biederman 	.d_delete	= pid_delete_dentry,
2151801199ceSEric W. Biederman };
2152801199ceSEric W. Biederman 
2153444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2154c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2155801199ceSEric W. Biederman {
2156c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2157801199ceSEric W. Biederman 	struct inode *inode;
2158801199ceSEric W. Biederman 	struct proc_inode *ei;
2159444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-EINVAL);
2160801199ceSEric W. Biederman 
2161801199ceSEric W. Biederman 	/* Allocate the inode */
2162801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2163801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2164801199ceSEric W. Biederman 	if (!inode)
2165801199ceSEric W. Biederman 		goto out;
2166801199ceSEric W. Biederman 
2167801199ceSEric W. Biederman 	/* Initialize the inode */
2168801199ceSEric W. Biederman 	ei = PROC_I(inode);
2169801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2170801199ceSEric W. Biederman 
2171801199ceSEric W. Biederman 	/*
2172801199ceSEric W. Biederman 	 * grab the reference to the task.
2173801199ceSEric W. Biederman 	 */
21741a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2175801199ceSEric W. Biederman 	if (!ei->pid)
2176801199ceSEric W. Biederman 		goto out_iput;
2177801199ceSEric W. Biederman 
2178801199ceSEric W. Biederman 	inode->i_uid = 0;
2179801199ceSEric W. Biederman 	inode->i_gid = 0;
2180801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2181801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2182801199ceSEric W. Biederman 		inode->i_nlink = 2;
2183801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2184801199ceSEric W. Biederman 		inode->i_size = 64;
2185801199ceSEric W. Biederman 	if (p->iop)
2186801199ceSEric W. Biederman 		inode->i_op = p->iop;
2187801199ceSEric W. Biederman 	if (p->fop)
2188801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2189801199ceSEric W. Biederman 	ei->op = p->op;
2190801199ceSEric W. Biederman 	dentry->d_op = &proc_base_dentry_operations;
2191801199ceSEric W. Biederman 	d_add(dentry, inode);
2192801199ceSEric W. Biederman 	error = NULL;
2193801199ceSEric W. Biederman out:
2194801199ceSEric W. Biederman 	return error;
2195801199ceSEric W. Biederman out_iput:
2196801199ceSEric W. Biederman 	iput(inode);
2197801199ceSEric W. Biederman 	goto out;
2198801199ceSEric W. Biederman }
2199801199ceSEric W. Biederman 
2200444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2201444ceed8SEric W. Biederman {
2202444ceed8SEric W. Biederman 	struct dentry *error;
2203444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2204c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2205444ceed8SEric W. Biederman 
2206444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2207444ceed8SEric W. Biederman 
2208444ceed8SEric W. Biederman 	if (!task)
2209444ceed8SEric W. Biederman 		goto out_no_task;
2210444ceed8SEric W. Biederman 
2211444ceed8SEric W. Biederman 	/* Lookup the directory entry */
22127bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
22137bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2214444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2215444ceed8SEric W. Biederman 			continue;
2216444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2217444ceed8SEric W. Biederman 			break;
2218444ceed8SEric W. Biederman 	}
22197bcd6b0eSEric W. Biederman 	if (p > last)
2220444ceed8SEric W. Biederman 		goto out;
2221444ceed8SEric W. Biederman 
2222444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2223444ceed8SEric W. Biederman 
2224444ceed8SEric W. Biederman out:
2225444ceed8SEric W. Biederman 	put_task_struct(task);
2226444ceed8SEric W. Biederman out_no_task:
2227444ceed8SEric W. Biederman 	return error;
2228444ceed8SEric W. Biederman }
2229444ceed8SEric W. Biederman 
2230c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2231c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
223261a28784SEric W. Biederman {
223361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
223461a28784SEric W. Biederman 				proc_base_instantiate, task, p);
223561a28784SEric W. Biederman }
223661a28784SEric W. Biederman 
2237aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2238aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2239aba76fdbSAndrew Morton {
2240aba76fdbSAndrew Morton 	return sprintf(buffer,
22414b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
2242aba76fdbSAndrew Morton 			"rchar: %llu\n"
2243aba76fdbSAndrew Morton 			"wchar: %llu\n"
2244aba76fdbSAndrew Morton 			"syscr: %llu\n"
2245aba76fdbSAndrew Morton 			"syscw: %llu\n"
22464b98d11bSAlexey Dobriyan #endif
2247aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2248aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2249aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
22504b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
2251aba76fdbSAndrew Morton 			(unsigned long long)task->rchar,
2252aba76fdbSAndrew Morton 			(unsigned long long)task->wchar,
2253aba76fdbSAndrew Morton 			(unsigned long long)task->syscr,
2254aba76fdbSAndrew Morton 			(unsigned long long)task->syscw,
22554b98d11bSAlexey Dobriyan #endif
2256aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.read_bytes,
2257aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.write_bytes,
2258aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.cancelled_write_bytes);
2259aba76fdbSAndrew Morton }
2260aba76fdbSAndrew Morton #endif
2261aba76fdbSAndrew Morton 
2262801199ceSEric W. Biederman /*
226328a6d671SEric W. Biederman  * Thread groups
226428a6d671SEric W. Biederman  */
226500977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2266c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
226720cdc894SEric W. Biederman 
2268c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
226961a28784SEric W. Biederman 	DIR("task",       S_IRUGO|S_IXUGO, task),
227061a28784SEric W. Biederman 	DIR("fd",         S_IRUSR|S_IXUSR, fd),
227127932742SMiklos Szeredi 	DIR("fdinfo",     S_IRUSR|S_IXUSR, fdinfo),
2272315e28c8SJames Pearson 	REG("environ",    S_IRUSR, environ),
227361a28784SEric W. Biederman 	INF("auxv",       S_IRUSR, pid_auxv),
2274df5f8314SEric W. Biederman 	ONE("status",     S_IRUGO, pid_status),
2275d85f50d5SNeil Horman 	INF("limits",	  S_IRUSR, pid_limits),
227643ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
227743ae34cbSIngo Molnar 	REG("sched",      S_IRUGO|S_IWUSR, pid_sched),
227843ae34cbSIngo Molnar #endif
227961a28784SEric W. Biederman 	INF("cmdline",    S_IRUGO, pid_cmdline),
2280ee992744SEric W. Biederman 	ONE("stat",       S_IRUGO, tgid_stat),
2281a56d3fc7SEric W. Biederman 	ONE("statm",      S_IRUGO, pid_statm),
228261a28784SEric W. Biederman 	REG("maps",       S_IRUGO, maps),
228328a6d671SEric W. Biederman #ifdef CONFIG_NUMA
228461a28784SEric W. Biederman 	REG("numa_maps",  S_IRUGO, numa_maps),
228528a6d671SEric W. Biederman #endif
228661a28784SEric W. Biederman 	REG("mem",        S_IRUSR|S_IWUSR, mem),
228761a28784SEric W. Biederman 	LNK("cwd",        cwd),
228861a28784SEric W. Biederman 	LNK("root",       root),
228961a28784SEric W. Biederman 	LNK("exe",        exe),
229061a28784SEric W. Biederman 	REG("mounts",     S_IRUGO, mounts),
229161a28784SEric W. Biederman 	REG("mountstats", S_IRUSR, mountstats),
22921e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2293b813e931SDavid Rientjes 	REG("clear_refs", S_IWUSR, clear_refs),
229461a28784SEric W. Biederman 	REG("smaps",      S_IRUGO, smaps),
229585863e47SMatt Mackall 	REG("pagemap",    S_IRUSR, pagemap),
229628a6d671SEric W. Biederman #endif
229728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
229872d9dcfcSEric W. Biederman 	DIR("attr",       S_IRUGO|S_IXUGO, attr_dir),
229928a6d671SEric W. Biederman #endif
230028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
230161a28784SEric W. Biederman 	INF("wchan",      S_IRUGO, pid_wchan),
230228a6d671SEric W. Biederman #endif
230328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
230461a28784SEric W. Biederman 	INF("schedstat",  S_IRUGO, pid_schedstat),
230528a6d671SEric W. Biederman #endif
23069745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
23079745512cSArjan van de Ven 	REG("latency",  S_IRUGO, lstats),
23089745512cSArjan van de Ven #endif
23098793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
231061a28784SEric W. Biederman 	REG("cpuset",     S_IRUGO, cpuset),
231128a6d671SEric W. Biederman #endif
2312a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2313a424316cSPaul Menage 	REG("cgroup",  S_IRUGO, cgroup),
2314a424316cSPaul Menage #endif
231561a28784SEric W. Biederman 	INF("oom_score",  S_IRUGO, oom_score),
231661a28784SEric W. Biederman 	REG("oom_adj",    S_IRUGO|S_IWUSR, oom_adjust),
231728a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
231861a28784SEric W. Biederman 	REG("loginuid",   S_IWUSR|S_IRUGO, loginuid),
231928a6d671SEric W. Biederman #endif
2320f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2321f4f154fdSAkinobu Mita 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2322f4f154fdSAkinobu Mita #endif
23233cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
23243cb4a0bbSKawai, Hidehiro 	REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
23253cb4a0bbSKawai, Hidehiro #endif
2326aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2327aba76fdbSAndrew Morton 	INF("io",	S_IRUGO, pid_io_accounting),
2328aba76fdbSAndrew Morton #endif
232928a6d671SEric W. Biederman };
233028a6d671SEric W. Biederman 
233128a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
233228a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
233328a6d671SEric W. Biederman {
233428a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
233528a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
233628a6d671SEric W. Biederman }
233728a6d671SEric W. Biederman 
233800977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
233928a6d671SEric W. Biederman 	.read		= generic_read_dir,
234028a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
234128a6d671SEric W. Biederman };
234228a6d671SEric W. Biederman 
234328a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
23447bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
23457bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
234628a6d671SEric W. Biederman }
234728a6d671SEric W. Biederman 
2348c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
234928a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
235028a6d671SEric W. Biederman 	.getattr	= pid_getattr,
235128a6d671SEric W. Biederman 	.setattr	= proc_setattr,
235228a6d671SEric W. Biederman };
235328a6d671SEric W. Biederman 
235460347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
23551da177e4SLinus Torvalds {
235648e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
23578578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
235848e6484dSEric W. Biederman 	struct qstr name;
23591da177e4SLinus Torvalds 
236048e6484dSEric W. Biederman 	name.name = buf;
236160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
236260347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
236348e6484dSEric W. Biederman 	if (dentry) {
23647766755aSAndrea Arcangeli 		if (!(current->flags & PF_EXITING))
236548e6484dSEric W. Biederman 			shrink_dcache_parent(dentry);
236648e6484dSEric W. Biederman 		d_drop(dentry);
236748e6484dSEric W. Biederman 		dput(dentry);
23681da177e4SLinus Torvalds 	}
23691da177e4SLinus Torvalds 
237060347f67SPavel Emelyanov 	if (tgid == 0)
237148e6484dSEric W. Biederman 		goto out;
23721da177e4SLinus Torvalds 
237348e6484dSEric W. Biederman 	name.name = buf;
237460347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
237560347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
237648e6484dSEric W. Biederman 	if (!leader)
237748e6484dSEric W. Biederman 		goto out;
237848e6484dSEric W. Biederman 
237948e6484dSEric W. Biederman 	name.name = "task";
238048e6484dSEric W. Biederman 	name.len = strlen(name.name);
238148e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
238248e6484dSEric W. Biederman 	if (!dir)
238348e6484dSEric W. Biederman 		goto out_put_leader;
238448e6484dSEric W. Biederman 
238548e6484dSEric W. Biederman 	name.name = buf;
238660347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
238748e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
238848e6484dSEric W. Biederman 	if (dentry) {
238948e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
239048e6484dSEric W. Biederman 		d_drop(dentry);
239148e6484dSEric W. Biederman 		dput(dentry);
23921da177e4SLinus Torvalds 	}
239348e6484dSEric W. Biederman 
239448e6484dSEric W. Biederman 	dput(dir);
239548e6484dSEric W. Biederman out_put_leader:
239648e6484dSEric W. Biederman 	dput(leader);
239748e6484dSEric W. Biederman out:
239848e6484dSEric W. Biederman 	return;
23991da177e4SLinus Torvalds }
24001da177e4SLinus Torvalds 
24010895e91dSRandy Dunlap /**
24020895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
24030895e91dSRandy Dunlap  * @task: task that should be flushed.
24040895e91dSRandy Dunlap  *
24050895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
240660347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
24070895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
24080895e91dSRandy Dunlap  *
24090895e91dSRandy Dunlap  * Looks in the dcache for
24100895e91dSRandy Dunlap  * /proc/@pid
24110895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
24120895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
24130895e91dSRandy Dunlap  * from the dcache.
24140895e91dSRandy Dunlap  *
24150895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
24160895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
24170895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
24180895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
24190895e91dSRandy Dunlap  * dcache entries at process exit time.
24200895e91dSRandy Dunlap  *
24210895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
24220895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
24230895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
242460347f67SPavel Emelyanov  */
242560347f67SPavel Emelyanov 
242660347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
242760347f67SPavel Emelyanov {
24289fcc2d15SEric W. Biederman 	int i;
24299fcc2d15SEric W. Biederman 	struct pid *pid, *tgid = NULL;
2430130f77ecSPavel Emelyanov 	struct upid *upid;
2431130f77ecSPavel Emelyanov 
2432130f77ecSPavel Emelyanov 	pid = task_pid(task);
24339fcc2d15SEric W. Biederman 	if (thread_group_leader(task))
2434130f77ecSPavel Emelyanov 		tgid = task_tgid(task);
24359fcc2d15SEric W. Biederman 
24369fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2437130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2438130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
24399fcc2d15SEric W. Biederman 			tgid ? tgid->numbers[i].nr : 0);
2440130f77ecSPavel Emelyanov 	}
24416f4e6433SPavel Emelyanov 
24426f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
24436f4e6433SPavel Emelyanov 	if (upid->nr == 1)
24446f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
244560347f67SPavel Emelyanov }
244660347f67SPavel Emelyanov 
24479711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
24489711ef99SAdrian Bunk 					   struct dentry * dentry,
2449c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2450444ceed8SEric W. Biederman {
2451444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2452444ceed8SEric W. Biederman 	struct inode *inode;
2453444ceed8SEric W. Biederman 
245461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2455444ceed8SEric W. Biederman 	if (!inode)
2456444ceed8SEric W. Biederman 		goto out;
2457444ceed8SEric W. Biederman 
2458444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2459444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2460444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2461444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
246227932742SMiklos Szeredi 	inode->i_nlink = 5;
2463444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY
2464444ceed8SEric W. Biederman 	inode->i_nlink += 1;
2465444ceed8SEric W. Biederman #endif
2466444ceed8SEric W. Biederman 
2467444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2468444ceed8SEric W. Biederman 
2469444ceed8SEric W. Biederman 	d_add(dentry, inode);
2470444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2471444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2472444ceed8SEric W. Biederman 		error = NULL;
2473444ceed8SEric W. Biederman out:
2474444ceed8SEric W. Biederman 	return error;
2475444ceed8SEric W. Biederman }
2476444ceed8SEric W. Biederman 
24771da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
24781da177e4SLinus Torvalds {
2479cd6a3ce9SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
24801da177e4SLinus Torvalds 	struct task_struct *task;
24811da177e4SLinus Torvalds 	unsigned tgid;
2482b488893aSPavel Emelyanov 	struct pid_namespace *ns;
24831da177e4SLinus Torvalds 
2484801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
2485801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2486801199ceSEric W. Biederman 		goto out;
2487801199ceSEric W. Biederman 
24881da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
24891da177e4SLinus Torvalds 	if (tgid == ~0U)
24901da177e4SLinus Torvalds 		goto out;
24911da177e4SLinus Torvalds 
2492b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2493de758734SEric W. Biederman 	rcu_read_lock();
2494b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
24951da177e4SLinus Torvalds 	if (task)
24961da177e4SLinus Torvalds 		get_task_struct(task);
2497de758734SEric W. Biederman 	rcu_read_unlock();
24981da177e4SLinus Torvalds 	if (!task)
24991da177e4SLinus Torvalds 		goto out;
25001da177e4SLinus Torvalds 
2501444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
250248e6484dSEric W. Biederman 	put_task_struct(task);
25031da177e4SLinus Torvalds out:
2504cd6a3ce9SEric W. Biederman 	return result;
25051da177e4SLinus Torvalds }
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds /*
25080804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
25090bc58a91SEric W. Biederman  *
25101da177e4SLinus Torvalds  */
251119fd4bb2SEric W. Biederman struct tgid_iter {
251219fd4bb2SEric W. Biederman 	unsigned int tgid;
25130804ef4bSEric W. Biederman 	struct task_struct *task;
251419fd4bb2SEric W. Biederman };
251519fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
251619fd4bb2SEric W. Biederman {
25170804ef4bSEric W. Biederman 	struct pid *pid;
25181da177e4SLinus Torvalds 
251919fd4bb2SEric W. Biederman 	if (iter.task)
252019fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
25210804ef4bSEric W. Biederman 	rcu_read_lock();
25220804ef4bSEric W. Biederman retry:
252319fd4bb2SEric W. Biederman 	iter.task = NULL;
252419fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
25250804ef4bSEric W. Biederman 	if (pid) {
252619fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
252719fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
25280804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
25290804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
25300804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
25310804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
25320804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
25330804ef4bSEric W. Biederman 		 *
25340804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
25350804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
25360804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
25370804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
25380804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
25390bc58a91SEric W. Biederman 		 */
254019fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
254119fd4bb2SEric W. Biederman 			iter.tgid += 1;
25420804ef4bSEric W. Biederman 			goto retry;
254319fd4bb2SEric W. Biederman 		}
254419fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
25451da177e4SLinus Torvalds 	}
2546454cc105SEric W. Biederman 	rcu_read_unlock();
254719fd4bb2SEric W. Biederman 	return iter;
25481da177e4SLinus Torvalds }
25491da177e4SLinus Torvalds 
25507bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
25511da177e4SLinus Torvalds 
255261a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
255319fd4bb2SEric W. Biederman 	struct tgid_iter iter)
255461a28784SEric W. Biederman {
255561a28784SEric W. Biederman 	char name[PROC_NUMBUF];
255619fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
255761a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
255819fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
255961a28784SEric W. Biederman }
256061a28784SEric W. Biederman 
25611da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
25621da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
25631da177e4SLinus Torvalds {
25641da177e4SLinus Torvalds 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
25652fddfeefSJosef "Jeff" Sipek 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
256619fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2567b488893aSPavel Emelyanov 	struct pid_namespace *ns;
25681da177e4SLinus Torvalds 
256961a28784SEric W. Biederman 	if (!reaper)
257061a28784SEric W. Biederman 		goto out_no_task;
257161a28784SEric W. Biederman 
25727bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2573c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
257461a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2575801199ceSEric W. Biederman 			goto out;
25761da177e4SLinus Torvalds 	}
25771da177e4SLinus Torvalds 
2578b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
257919fd4bb2SEric W. Biederman 	iter.task = NULL;
258019fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
258119fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
258219fd4bb2SEric W. Biederman 	     iter.task;
258319fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
258419fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
258519fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
258619fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
25870804ef4bSEric W. Biederman 			goto out;
25881da177e4SLinus Torvalds 		}
25891da177e4SLinus Torvalds 	}
25900804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
25910804ef4bSEric W. Biederman out:
259261a28784SEric W. Biederman 	put_task_struct(reaper);
259361a28784SEric W. Biederman out_no_task:
25941da177e4SLinus Torvalds 	return 0;
25951da177e4SLinus Torvalds }
25961da177e4SLinus Torvalds 
25970bc58a91SEric W. Biederman /*
259828a6d671SEric W. Biederman  * Tasks
259928a6d671SEric W. Biederman  */
2600c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
260161a28784SEric W. Biederman 	DIR("fd",        S_IRUSR|S_IXUSR, fd),
260227932742SMiklos Szeredi 	DIR("fdinfo",    S_IRUSR|S_IXUSR, fdinfo),
2603315e28c8SJames Pearson 	REG("environ",   S_IRUSR, environ),
260461a28784SEric W. Biederman 	INF("auxv",      S_IRUSR, pid_auxv),
2605df5f8314SEric W. Biederman 	ONE("status",    S_IRUGO, pid_status),
2606d85f50d5SNeil Horman 	INF("limits",	 S_IRUSR, pid_limits),
260743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
260843ae34cbSIngo Molnar 	REG("sched",     S_IRUGO|S_IWUSR, pid_sched),
260943ae34cbSIngo Molnar #endif
261061a28784SEric W. Biederman 	INF("cmdline",   S_IRUGO, pid_cmdline),
2611ee992744SEric W. Biederman 	ONE("stat",      S_IRUGO, tid_stat),
2612a56d3fc7SEric W. Biederman 	ONE("statm",     S_IRUGO, pid_statm),
261361a28784SEric W. Biederman 	REG("maps",      S_IRUGO, maps),
261428a6d671SEric W. Biederman #ifdef CONFIG_NUMA
261561a28784SEric W. Biederman 	REG("numa_maps", S_IRUGO, numa_maps),
261628a6d671SEric W. Biederman #endif
261761a28784SEric W. Biederman 	REG("mem",       S_IRUSR|S_IWUSR, mem),
261861a28784SEric W. Biederman 	LNK("cwd",       cwd),
261961a28784SEric W. Biederman 	LNK("root",      root),
262061a28784SEric W. Biederman 	LNK("exe",       exe),
262161a28784SEric W. Biederman 	REG("mounts",    S_IRUGO, mounts),
26221e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR
2623b813e931SDavid Rientjes 	REG("clear_refs", S_IWUSR, clear_refs),
262461a28784SEric W. Biederman 	REG("smaps",     S_IRUGO, smaps),
262585863e47SMatt Mackall 	REG("pagemap",    S_IRUSR, pagemap),
262628a6d671SEric W. Biederman #endif
262728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
262872d9dcfcSEric W. Biederman 	DIR("attr",      S_IRUGO|S_IXUGO, attr_dir),
262928a6d671SEric W. Biederman #endif
263028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
263161a28784SEric W. Biederman 	INF("wchan",     S_IRUGO, pid_wchan),
263228a6d671SEric W. Biederman #endif
263328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
263461a28784SEric W. Biederman 	INF("schedstat", S_IRUGO, pid_schedstat),
263528a6d671SEric W. Biederman #endif
26369745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
26379745512cSArjan van de Ven 	REG("latency",  S_IRUGO, lstats),
26389745512cSArjan van de Ven #endif
26398793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
264061a28784SEric W. Biederman 	REG("cpuset",    S_IRUGO, cpuset),
264128a6d671SEric W. Biederman #endif
2642a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2643a424316cSPaul Menage 	REG("cgroup",  S_IRUGO, cgroup),
2644a424316cSPaul Menage #endif
264561a28784SEric W. Biederman 	INF("oom_score", S_IRUGO, oom_score),
264661a28784SEric W. Biederman 	REG("oom_adj",   S_IRUGO|S_IWUSR, oom_adjust),
264728a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
264861a28784SEric W. Biederman 	REG("loginuid",  S_IWUSR|S_IRUGO, loginuid),
264928a6d671SEric W. Biederman #endif
2650f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2651f4f154fdSAkinobu Mita 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2652f4f154fdSAkinobu Mita #endif
265328a6d671SEric W. Biederman };
265428a6d671SEric W. Biederman 
265528a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
265628a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
265728a6d671SEric W. Biederman {
265828a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
265928a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
266028a6d671SEric W. Biederman }
266128a6d671SEric W. Biederman 
266228a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
26637bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26647bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
266528a6d671SEric W. Biederman }
266628a6d671SEric W. Biederman 
266700977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
266828a6d671SEric W. Biederman 	.read		= generic_read_dir,
266928a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
267028a6d671SEric W. Biederman };
267128a6d671SEric W. Biederman 
2672c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
267328a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
267428a6d671SEric W. Biederman 	.getattr	= pid_getattr,
267528a6d671SEric W. Biederman 	.setattr	= proc_setattr,
267628a6d671SEric W. Biederman };
267728a6d671SEric W. Biederman 
2678444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
2679c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2680444ceed8SEric W. Biederman {
2681444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2682444ceed8SEric W. Biederman 	struct inode *inode;
268361a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2684444ceed8SEric W. Biederman 
2685444ceed8SEric W. Biederman 	if (!inode)
2686444ceed8SEric W. Biederman 		goto out;
2687444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2688444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
2689444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
2690444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
269127932742SMiklos Szeredi 	inode->i_nlink = 4;
2692444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY
2693444ceed8SEric W. Biederman 	inode->i_nlink += 1;
2694444ceed8SEric W. Biederman #endif
2695444ceed8SEric W. Biederman 
2696444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2697444ceed8SEric W. Biederman 
2698444ceed8SEric W. Biederman 	d_add(dentry, inode);
2699444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2700444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2701444ceed8SEric W. Biederman 		error = NULL;
2702444ceed8SEric W. Biederman out:
2703444ceed8SEric W. Biederman 	return error;
2704444ceed8SEric W. Biederman }
2705444ceed8SEric W. Biederman 
270628a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
270728a6d671SEric W. Biederman {
270828a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
270928a6d671SEric W. Biederman 	struct task_struct *task;
271028a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
271128a6d671SEric W. Biederman 	unsigned tid;
2712b488893aSPavel Emelyanov 	struct pid_namespace *ns;
271328a6d671SEric W. Biederman 
271428a6d671SEric W. Biederman 	if (!leader)
271528a6d671SEric W. Biederman 		goto out_no_task;
271628a6d671SEric W. Biederman 
271728a6d671SEric W. Biederman 	tid = name_to_int(dentry);
271828a6d671SEric W. Biederman 	if (tid == ~0U)
271928a6d671SEric W. Biederman 		goto out;
272028a6d671SEric W. Biederman 
2721b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
272228a6d671SEric W. Biederman 	rcu_read_lock();
2723b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
272428a6d671SEric W. Biederman 	if (task)
272528a6d671SEric W. Biederman 		get_task_struct(task);
272628a6d671SEric W. Biederman 	rcu_read_unlock();
272728a6d671SEric W. Biederman 	if (!task)
272828a6d671SEric W. Biederman 		goto out;
2729bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
273028a6d671SEric W. Biederman 		goto out_drop_task;
273128a6d671SEric W. Biederman 
2732444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
273328a6d671SEric W. Biederman out_drop_task:
273428a6d671SEric W. Biederman 	put_task_struct(task);
273528a6d671SEric W. Biederman out:
273628a6d671SEric W. Biederman 	put_task_struct(leader);
273728a6d671SEric W. Biederman out_no_task:
273828a6d671SEric W. Biederman 	return result;
273928a6d671SEric W. Biederman }
274028a6d671SEric W. Biederman 
274128a6d671SEric W. Biederman /*
27420bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
27430bc58a91SEric W. Biederman  *
27440bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
27450bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
27460bc58a91SEric W. Biederman  * directory we have more work todo.
27470bc58a91SEric W. Biederman  *
27480bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
27490bc58a91SEric W. Biederman  *
27500bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
27510bc58a91SEric W. Biederman  * threads past it.
27520bc58a91SEric W. Biederman  */
2753cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
2754b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
27550bc58a91SEric W. Biederman {
2756a872ff0cSOleg Nesterov 	struct task_struct *pos;
27570bc58a91SEric W. Biederman 
2758cc288738SEric W. Biederman 	rcu_read_lock();
27590bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
27600bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
2761b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
2762a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
2763a872ff0cSOleg Nesterov 			goto found;
27640bc58a91SEric W. Biederman 	}
27650bc58a91SEric W. Biederman 
27660bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
27670bc58a91SEric W. Biederman 	pos = NULL;
2768a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
2769a872ff0cSOleg Nesterov 		goto out;
2770a872ff0cSOleg Nesterov 
2771a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
2772a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
2773a872ff0cSOleg Nesterov 	 */
2774a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
2775a872ff0cSOleg Nesterov 		pos = next_thread(pos);
2776a872ff0cSOleg Nesterov 		if (pos == leader) {
2777a872ff0cSOleg Nesterov 			pos = NULL;
2778a872ff0cSOleg Nesterov 			goto out;
2779a872ff0cSOleg Nesterov 		}
2780a872ff0cSOleg Nesterov 	}
2781a872ff0cSOleg Nesterov found:
2782a872ff0cSOleg Nesterov 	get_task_struct(pos);
2783a872ff0cSOleg Nesterov out:
2784cc288738SEric W. Biederman 	rcu_read_unlock();
27850bc58a91SEric W. Biederman 	return pos;
27860bc58a91SEric W. Biederman }
27870bc58a91SEric W. Biederman 
27880bc58a91SEric W. Biederman /*
27890bc58a91SEric W. Biederman  * Find the next thread in the thread list.
27900bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
27910bc58a91SEric W. Biederman  *
27920bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
27930bc58a91SEric W. Biederman  */
27940bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
27950bc58a91SEric W. Biederman {
2796c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
2797cc288738SEric W. Biederman 	rcu_read_lock();
2798c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
27990bc58a91SEric W. Biederman 		pos = next_thread(start);
2800c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
28010bc58a91SEric W. Biederman 			pos = NULL;
2802c1df7fb8SOleg Nesterov 		else
2803c1df7fb8SOleg Nesterov 			get_task_struct(pos);
2804c1df7fb8SOleg Nesterov 	}
2805cc288738SEric W. Biederman 	rcu_read_unlock();
28060bc58a91SEric W. Biederman 	put_task_struct(start);
28070bc58a91SEric W. Biederman 	return pos;
28080bc58a91SEric W. Biederman }
28090bc58a91SEric W. Biederman 
281061a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
281161a28784SEric W. Biederman 	struct task_struct *task, int tid)
281261a28784SEric W. Biederman {
281361a28784SEric W. Biederman 	char name[PROC_NUMBUF];
281461a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
281561a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
281661a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
281761a28784SEric W. Biederman }
281861a28784SEric W. Biederman 
28191da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
28201da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
28211da177e4SLinus Torvalds {
28222fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
28231da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
28247d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
28250bc58a91SEric W. Biederman 	struct task_struct *task;
28261da177e4SLinus Torvalds 	int retval = -ENOENT;
28271da177e4SLinus Torvalds 	ino_t ino;
28280bc58a91SEric W. Biederman 	int tid;
28291da177e4SLinus Torvalds 	unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2830b488893aSPavel Emelyanov 	struct pid_namespace *ns;
28311da177e4SLinus Torvalds 
28327d895244SGuillaume Chazarain 	task = get_proc_task(inode);
28337d895244SGuillaume Chazarain 	if (!task)
28347d895244SGuillaume Chazarain 		goto out_no_task;
28357d895244SGuillaume Chazarain 	rcu_read_lock();
28367d895244SGuillaume Chazarain 	if (pid_alive(task)) {
28377d895244SGuillaume Chazarain 		leader = task->group_leader;
28387d895244SGuillaume Chazarain 		get_task_struct(leader);
28397d895244SGuillaume Chazarain 	}
28407d895244SGuillaume Chazarain 	rcu_read_unlock();
28417d895244SGuillaume Chazarain 	put_task_struct(task);
284299f89551SEric W. Biederman 	if (!leader)
284399f89551SEric W. Biederman 		goto out_no_task;
28441da177e4SLinus Torvalds 	retval = 0;
28451da177e4SLinus Torvalds 
28461da177e4SLinus Torvalds 	switch (pos) {
28471da177e4SLinus Torvalds 	case 0:
28481da177e4SLinus Torvalds 		ino = inode->i_ino;
28491da177e4SLinus Torvalds 		if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
28501da177e4SLinus Torvalds 			goto out;
28511da177e4SLinus Torvalds 		pos++;
28521da177e4SLinus Torvalds 		/* fall through */
28531da177e4SLinus Torvalds 	case 1:
28541da177e4SLinus Torvalds 		ino = parent_ino(dentry);
28551da177e4SLinus Torvalds 		if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
28561da177e4SLinus Torvalds 			goto out;
28571da177e4SLinus Torvalds 		pos++;
28581da177e4SLinus Torvalds 		/* fall through */
28591da177e4SLinus Torvalds 	}
28601da177e4SLinus Torvalds 
28610bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
28620bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
28630bc58a91SEric W. Biederman 	 */
2864b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
28652b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
28660bc58a91SEric W. Biederman 	filp->f_version = 0;
2867b488893aSPavel Emelyanov 	for (task = first_tid(leader, tid, pos - 2, ns);
28680bc58a91SEric W. Biederman 	     task;
28690bc58a91SEric W. Biederman 	     task = next_tid(task), pos++) {
2870b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
287161a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
28720bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
28730bc58a91SEric W. Biederman 			 * pid for the next readir call */
28742b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
28750bc58a91SEric W. Biederman 			put_task_struct(task);
28761da177e4SLinus Torvalds 			break;
28770bc58a91SEric W. Biederman 		}
28781da177e4SLinus Torvalds 	}
28791da177e4SLinus Torvalds out:
28801da177e4SLinus Torvalds 	filp->f_pos = pos;
288199f89551SEric W. Biederman 	put_task_struct(leader);
288299f89551SEric W. Biederman out_no_task:
28831da177e4SLinus Torvalds 	return retval;
28841da177e4SLinus Torvalds }
28856e66b52bSEric W. Biederman 
28866e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
28876e66b52bSEric W. Biederman {
28886e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
288999f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
28906e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
28916e66b52bSEric W. Biederman 
289299f89551SEric W. Biederman 	if (p) {
289399f89551SEric W. Biederman 		rcu_read_lock();
289499f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
289599f89551SEric W. Biederman 		rcu_read_unlock();
289699f89551SEric W. Biederman 		put_task_struct(p);
28976e66b52bSEric W. Biederman 	}
28986e66b52bSEric W. Biederman 
28996e66b52bSEric W. Biederman 	return 0;
29006e66b52bSEric W. Biederman }
290128a6d671SEric W. Biederman 
2902c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
290328a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
290428a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
290528a6d671SEric W. Biederman 	.setattr	= proc_setattr,
290628a6d671SEric W. Biederman };
290728a6d671SEric W. Biederman 
290800977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
290928a6d671SEric W. Biederman 	.read		= generic_read_dir,
291028a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
291128a6d671SEric W. Biederman };
2912