xref: /openbmc/linux/fs/proc/base.c (revision d85f50d5e1aa99ab082035f94265847521819e58)
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>
66*d85f50d5SNeil 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 
928578cea7SEric W. Biederman /* Worst case buffer size needed for holding an integer. */
930187f879SAndrew Morton #define PROC_NUMBUF 13
948578cea7SEric W. Biederman 
951da177e4SLinus Torvalds struct pid_entry {
961da177e4SLinus Torvalds 	char *name;
97c5141e6dSEric Dumazet 	int len;
981da177e4SLinus Torvalds 	mode_t mode;
99c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
10000977a59SArjan van de Ven 	const struct file_operations *fop;
10120cdc894SEric W. Biederman 	union proc_op op;
1021da177e4SLinus Torvalds };
1031da177e4SLinus Torvalds 
10461a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
10520cdc894SEric W. Biederman 	.name = (NAME),					\
106c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
10720cdc894SEric W. Biederman 	.mode = MODE,					\
10820cdc894SEric W. Biederman 	.iop  = IOP,					\
10920cdc894SEric W. Biederman 	.fop  = FOP,					\
11020cdc894SEric W. Biederman 	.op   = OP,					\
11120cdc894SEric W. Biederman }
11220cdc894SEric W. Biederman 
11361a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE)							\
11461a28784SEric W. Biederman 	NOD(NAME, (S_IFDIR|(MODE)),						\
11520cdc894SEric W. Biederman 		&proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations,	\
11620cdc894SEric W. Biederman 		{} )
11761a28784SEric W. Biederman #define LNK(NAME, OTYPE)					\
11861a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
11920cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
12020cdc894SEric W. Biederman 		{ .proc_get_link = &proc_##OTYPE##_link } )
12161a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE)				\
12261a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), NULL,		\
12320cdc894SEric W. Biederman 		&proc_##OTYPE##_operations, {})
12461a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE)				\
12561a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
12620cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
12720cdc894SEric W. Biederman 		{ .proc_read = &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 
1560494f6ecSMiklos Szeredi static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
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);
1681da177e4SLinus Torvalds 		*mnt = mntget(fs->pwdmnt);
1691da177e4SLinus Torvalds 		*dentry = dget(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 
1771da177e4SLinus Torvalds static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
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);
1891da177e4SLinus Torvalds 		*mnt = mntget(fs->rootmnt);
1901da177e4SLinus Torvalds 		*dentry = dget(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) && \
2021da177e4SLinus Torvalds 	 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
2031da177e4SLinus Torvalds 	 security_ptrace(current,task) == 0))
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	int res = 0;
2081da177e4SLinus Torvalds 	unsigned int len;
2091da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2101da177e4SLinus Torvalds 	if (!mm)
2111da177e4SLinus Torvalds 		goto out;
2121da177e4SLinus Torvalds 	if (!mm->arg_end)
2131da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2181da177e4SLinus Torvalds 		len = PAGE_SIZE;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2231da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2241da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2251da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2261da177e4SLinus Torvalds 		if (len < res) {
2271da177e4SLinus Torvalds 		    res = len;
2281da177e4SLinus Torvalds 		} else {
2291da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2301da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2311da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2321da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2331da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2341da177e4SLinus Torvalds 		}
2351da177e4SLinus Torvalds 	}
2361da177e4SLinus Torvalds out_mm:
2371da177e4SLinus Torvalds 	mmput(mm);
2381da177e4SLinus Torvalds out:
2391da177e4SLinus Torvalds 	return res;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	int res = 0;
2451da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2461da177e4SLinus Torvalds 	if (mm) {
2471da177e4SLinus Torvalds 		unsigned int nwords = 0;
2481da177e4SLinus Torvalds 		do
2491da177e4SLinus Torvalds 			nwords += 2;
2501da177e4SLinus Torvalds 		while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2511da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2521da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2531da177e4SLinus Torvalds 			res = PAGE_SIZE;
2541da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2551da177e4SLinus Torvalds 		mmput(mm);
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 	return res;
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2621da177e4SLinus Torvalds /*
2631da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2641da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2651da177e4SLinus Torvalds  */
2661da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
2671da177e4SLinus Torvalds {
268ffb45122SAlexey Dobriyan 	unsigned long wchan;
2699281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	wchan = get_wchan(task);
2721da177e4SLinus Torvalds 
2739d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
2741da177e4SLinus Torvalds 		return sprintf(buffer, "%lu", wchan);
2759d65cb4aSAlexey Dobriyan 	else
2769d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
2811da177e4SLinus Torvalds /*
2821da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
2831da177e4SLinus Torvalds  */
2841da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
2851da177e4SLinus Torvalds {
286172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
2871da177e4SLinus Torvalds 			task->sched_info.cpu_time,
2881da177e4SLinus Torvalds 			task->sched_info.run_delay,
2892d72376bSIngo Molnar 			task->sched_info.pcount);
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds #endif
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds /* The badness from the OOM killer */
2941da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime);
2951da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	unsigned long points;
2981da177e4SLinus Torvalds 	struct timespec uptime;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&uptime);
30119c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
3021da177e4SLinus Torvalds 	points = badness(task, uptime.tv_sec);
30319c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
3041da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
307*d85f50d5SNeil Horman struct limit_names {
308*d85f50d5SNeil Horman 	char *name;
309*d85f50d5SNeil Horman 	char *unit;
310*d85f50d5SNeil Horman };
311*d85f50d5SNeil Horman 
312*d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
313*d85f50d5SNeil Horman 	[RLIMIT_CPU] = {"Max cpu time", "ms"},
314*d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
315*d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
316*d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
317*d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
318*d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
319*d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
320*d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
321*d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
322*d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
323*d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
324*d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
325*d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
326*d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
327*d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
328*d85f50d5SNeil Horman };
329*d85f50d5SNeil Horman 
330*d85f50d5SNeil Horman /* Display limits for a process */
331*d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
332*d85f50d5SNeil Horman {
333*d85f50d5SNeil Horman 	unsigned int i;
334*d85f50d5SNeil Horman 	int count = 0;
335*d85f50d5SNeil Horman 	unsigned long flags;
336*d85f50d5SNeil Horman 	char *bufptr = buffer;
337*d85f50d5SNeil Horman 
338*d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
339*d85f50d5SNeil Horman 
340*d85f50d5SNeil Horman 	rcu_read_lock();
341*d85f50d5SNeil Horman 	if (!lock_task_sighand(task,&flags)) {
342*d85f50d5SNeil Horman 		rcu_read_unlock();
343*d85f50d5SNeil Horman 		return 0;
344*d85f50d5SNeil Horman 	}
345*d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
346*d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
347*d85f50d5SNeil Horman 	rcu_read_unlock();
348*d85f50d5SNeil Horman 
349*d85f50d5SNeil Horman 	/*
350*d85f50d5SNeil Horman 	 * print the file header
351*d85f50d5SNeil Horman 	 */
352*d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
353*d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
354*d85f50d5SNeil Horman 
355*d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
356*d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
357*d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
358*d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
359*d85f50d5SNeil Horman 		else
360*d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
361*d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
362*d85f50d5SNeil Horman 
363*d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
364*d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
365*d85f50d5SNeil Horman 		else
366*d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
367*d85f50d5SNeil Horman 					 rlim[i].rlim_max);
368*d85f50d5SNeil Horman 
369*d85f50d5SNeil Horman 		if (lnames[i].unit)
370*d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
371*d85f50d5SNeil Horman 					 lnames[i].unit);
372*d85f50d5SNeil Horman 		else
373*d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
374*d85f50d5SNeil Horman 	}
375*d85f50d5SNeil Horman 
376*d85f50d5SNeil Horman 	return count;
377*d85f50d5SNeil Horman }
378*d85f50d5SNeil Horman 
3791da177e4SLinus Torvalds /************************************************************************/
3801da177e4SLinus Torvalds /*                       Here the fs part begins                        */
3811da177e4SLinus Torvalds /************************************************************************/
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /* permission checks */
384778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
3851da177e4SLinus Torvalds {
386778c1144SEric W. Biederman 	struct task_struct *task;
387778c1144SEric W. Biederman 	int allowed = 0;
388df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
389df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
390df26c40eSEric W. Biederman 	 * information.
391778c1144SEric W. Biederman 	 */
392778c1144SEric W. Biederman 	task = get_proc_task(inode);
393df26c40eSEric W. Biederman 	if (task) {
394778c1144SEric W. Biederman 		allowed = ptrace_may_attach(task);
395778c1144SEric W. Biederman 		put_task_struct(task);
396df26c40eSEric W. Biederman 	}
397778c1144SEric W. Biederman 	return allowed;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4006d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr)
4016d76fa58SLinus Torvalds {
4026d76fa58SLinus Torvalds 	int error;
4036d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
4046d76fa58SLinus Torvalds 
4056d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
4066d76fa58SLinus Torvalds 		return -EPERM;
4076d76fa58SLinus Torvalds 
4086d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
4096d76fa58SLinus Torvalds 	if (!error)
4106d76fa58SLinus Torvalds 		error = inode_setattr(inode, attr);
4116d76fa58SLinus Torvalds 	return error;
4126d76fa58SLinus Torvalds }
4136d76fa58SLinus Torvalds 
414c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
4156d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
4166d76fa58SLinus Torvalds };
4176d76fa58SLinus Torvalds 
4181da177e4SLinus Torvalds extern struct seq_operations mounts_op;
4195addc5ddSAl Viro struct proc_mounts {
4205addc5ddSAl Viro 	struct seq_file m;
4215addc5ddSAl Viro 	int event;
4225addc5ddSAl Viro };
4235addc5ddSAl Viro 
4241da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file)
4251da177e4SLinus Torvalds {
42699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
427cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
4286b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
4295addc5ddSAl Viro 	struct proc_mounts *p;
4305addc5ddSAl Viro 	int ret = -EINVAL;
4315addc5ddSAl Viro 
43299f89551SEric W. Biederman 	if (task) {
433cf7b708cSPavel Emelyanov 		rcu_read_lock();
434cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
435cf7b708cSPavel Emelyanov 		if (nsp) {
436cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
4376b3286edSKirill Korotaev 			if (ns)
4386b3286edSKirill Korotaev 				get_mnt_ns(ns);
439863c4702SAlexey Dobriyan 		}
440cf7b708cSPavel Emelyanov 		rcu_read_unlock();
441cf7b708cSPavel Emelyanov 
44299f89551SEric W. Biederman 		put_task_struct(task);
44399f89551SEric W. Biederman 	}
4441da177e4SLinus Torvalds 
4456b3286edSKirill Korotaev 	if (ns) {
4465addc5ddSAl Viro 		ret = -ENOMEM;
4475addc5ddSAl Viro 		p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
4485addc5ddSAl Viro 		if (p) {
4495addc5ddSAl Viro 			file->private_data = &p->m;
4505addc5ddSAl Viro 			ret = seq_open(file, &mounts_op);
4515addc5ddSAl Viro 			if (!ret) {
4526b3286edSKirill Korotaev 				p->m.private = ns;
4536b3286edSKirill Korotaev 				p->event = ns->event;
4545addc5ddSAl Viro 				return 0;
4551da177e4SLinus Torvalds 			}
4565addc5ddSAl Viro 			kfree(p);
4575addc5ddSAl Viro 		}
4586b3286edSKirill Korotaev 		put_mnt_ns(ns);
4591da177e4SLinus Torvalds 	}
4601da177e4SLinus Torvalds 	return ret;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	struct seq_file *m = file->private_data;
4666b3286edSKirill Korotaev 	struct mnt_namespace *ns = m->private;
4676b3286edSKirill Korotaev 	put_mnt_ns(ns);
4681da177e4SLinus Torvalds 	return seq_release(inode, file);
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4715addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
4725addc5ddSAl Viro {
4735addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
4746b3286edSKirill Korotaev 	struct mnt_namespace *ns = p->m.private;
4755addc5ddSAl Viro 	unsigned res = 0;
4765addc5ddSAl Viro 
4775addc5ddSAl Viro 	poll_wait(file, &ns->poll, wait);
4785addc5ddSAl Viro 
4795addc5ddSAl Viro 	spin_lock(&vfsmount_lock);
4805addc5ddSAl Viro 	if (p->event != ns->event) {
4815addc5ddSAl Viro 		p->event = ns->event;
4825addc5ddSAl Viro 		res = POLLERR;
4835addc5ddSAl Viro 	}
4845addc5ddSAl Viro 	spin_unlock(&vfsmount_lock);
4855addc5ddSAl Viro 
4865addc5ddSAl Viro 	return res;
4875addc5ddSAl Viro }
4885addc5ddSAl Viro 
48900977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
4901da177e4SLinus Torvalds 	.open		= mounts_open,
4911da177e4SLinus Torvalds 	.read		= seq_read,
4921da177e4SLinus Torvalds 	.llseek		= seq_lseek,
4931da177e4SLinus Torvalds 	.release	= mounts_release,
4945addc5ddSAl Viro 	.poll		= mounts_poll,
4951da177e4SLinus Torvalds };
4961da177e4SLinus Torvalds 
497b4629fe2SChuck Lever extern struct seq_operations mountstats_op;
498b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
499b4629fe2SChuck Lever {
500b4629fe2SChuck Lever 	int ret = seq_open(file, &mountstats_op);
501b4629fe2SChuck Lever 
502b4629fe2SChuck Lever 	if (!ret) {
503b4629fe2SChuck Lever 		struct seq_file *m = file->private_data;
504cf7b708cSPavel Emelyanov 		struct nsproxy *nsp;
5056b3286edSKirill Korotaev 		struct mnt_namespace *mnt_ns = NULL;
50699f89551SEric W. Biederman 		struct task_struct *task = get_proc_task(inode);
50799f89551SEric W. Biederman 
50899f89551SEric W. Biederman 		if (task) {
509cf7b708cSPavel Emelyanov 			rcu_read_lock();
510cf7b708cSPavel Emelyanov 			nsp = task_nsproxy(task);
511cf7b708cSPavel Emelyanov 			if (nsp) {
512cf7b708cSPavel Emelyanov 				mnt_ns = nsp->mnt_ns;
5136b3286edSKirill Korotaev 				if (mnt_ns)
5146b3286edSKirill Korotaev 					get_mnt_ns(mnt_ns);
515cf7b708cSPavel Emelyanov 			}
516cf7b708cSPavel Emelyanov 			rcu_read_unlock();
517cf7b708cSPavel Emelyanov 
51899f89551SEric W. Biederman 			put_task_struct(task);
51999f89551SEric W. Biederman 		}
520b4629fe2SChuck Lever 
5216b3286edSKirill Korotaev 		if (mnt_ns)
5226b3286edSKirill Korotaev 			m->private = mnt_ns;
523b4629fe2SChuck Lever 		else {
524b4629fe2SChuck Lever 			seq_release(inode, file);
525b4629fe2SChuck Lever 			ret = -EINVAL;
526b4629fe2SChuck Lever 		}
527b4629fe2SChuck Lever 	}
528b4629fe2SChuck Lever 	return ret;
529b4629fe2SChuck Lever }
530b4629fe2SChuck Lever 
53100977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
532b4629fe2SChuck Lever 	.open		= mountstats_open,
533b4629fe2SChuck Lever 	.read		= seq_read,
534b4629fe2SChuck Lever 	.llseek		= seq_lseek,
535b4629fe2SChuck Lever 	.release	= mounts_release,
536b4629fe2SChuck Lever };
537b4629fe2SChuck Lever 
5381da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
5411da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
5421da177e4SLinus Torvalds {
5432fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
5441da177e4SLinus Torvalds 	unsigned long page;
5451da177e4SLinus Torvalds 	ssize_t length;
54699f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
54799f89551SEric W. Biederman 
54899f89551SEric W. Biederman 	length = -ESRCH;
54999f89551SEric W. Biederman 	if (!task)
55099f89551SEric W. Biederman 		goto out_no_task;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
5531da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
55499f89551SEric W. Biederman 
55599f89551SEric W. Biederman 	length = -ENOMEM;
556e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
55799f89551SEric W. Biederman 		goto out;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	if (length >= 0)
5621da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
5631da177e4SLinus Torvalds 	free_page(page);
56499f89551SEric W. Biederman out:
56599f89551SEric W. Biederman 	put_task_struct(task);
56699f89551SEric W. Biederman out_no_task:
5671da177e4SLinus Torvalds 	return length;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
57000977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
5711da177e4SLinus Torvalds 	.read		= proc_info_read,
5721da177e4SLinus Torvalds };
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
5771da177e4SLinus Torvalds 	return 0;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
5811da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
5821da177e4SLinus Torvalds {
5832fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
5841da177e4SLinus Torvalds 	char *page;
5851da177e4SLinus Torvalds 	unsigned long src = *ppos;
5861da177e4SLinus Torvalds 	int ret = -ESRCH;
5871da177e4SLinus Torvalds 	struct mm_struct *mm;
5881da177e4SLinus Torvalds 
58999f89551SEric W. Biederman 	if (!task)
59099f89551SEric W. Biederman 		goto out_no_task;
59199f89551SEric W. Biederman 
592ab8d11beSMiklos Szeredi 	if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
5931da177e4SLinus Torvalds 		goto out;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	ret = -ENOMEM;
596e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
5971da177e4SLinus Torvalds 	if (!page)
5981da177e4SLinus Torvalds 		goto out;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	ret = 0;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	mm = get_task_mm(task);
6031da177e4SLinus Torvalds 	if (!mm)
6041da177e4SLinus Torvalds 		goto out_free;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	ret = -EIO;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
6091da177e4SLinus Torvalds 		goto out_put;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	ret = 0;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	while (count > 0) {
6141da177e4SLinus Torvalds 		int this_len, retval;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
6171da177e4SLinus Torvalds 		retval = access_process_vm(task, src, page, this_len, 0);
618ab8d11beSMiklos Szeredi 		if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
6191da177e4SLinus Torvalds 			if (!ret)
6201da177e4SLinus Torvalds 				ret = -EIO;
6211da177e4SLinus Torvalds 			break;
6221da177e4SLinus Torvalds 		}
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
6251da177e4SLinus Torvalds 			ret = -EFAULT;
6261da177e4SLinus Torvalds 			break;
6271da177e4SLinus Torvalds 		}
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 		ret += retval;
6301da177e4SLinus Torvalds 		src += retval;
6311da177e4SLinus Torvalds 		buf += retval;
6321da177e4SLinus Torvalds 		count -= retval;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 	*ppos = src;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds out_put:
6371da177e4SLinus Torvalds 	mmput(mm);
6381da177e4SLinus Torvalds out_free:
6391da177e4SLinus Torvalds 	free_page((unsigned long) page);
6401da177e4SLinus Torvalds out:
64199f89551SEric W. Biederman 	put_task_struct(task);
64299f89551SEric W. Biederman out_no_task:
6431da177e4SLinus Torvalds 	return ret;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds #define mem_write NULL
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds #ifndef mem_write
6491da177e4SLinus Torvalds /* This is a security hazard */
65063967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
6511da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
6521da177e4SLinus Torvalds {
653f7ca54f4SFrederik Deweerdt 	int copied;
6541da177e4SLinus Torvalds 	char *page;
6552fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
6561da177e4SLinus Torvalds 	unsigned long dst = *ppos;
6571da177e4SLinus Torvalds 
65899f89551SEric W. Biederman 	copied = -ESRCH;
65999f89551SEric W. Biederman 	if (!task)
66099f89551SEric W. Biederman 		goto out_no_task;
6611da177e4SLinus Torvalds 
66299f89551SEric W. Biederman 	if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
66399f89551SEric W. Biederman 		goto out;
66499f89551SEric W. Biederman 
66599f89551SEric W. Biederman 	copied = -ENOMEM;
666e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
6671da177e4SLinus Torvalds 	if (!page)
66899f89551SEric W. Biederman 		goto out;
6691da177e4SLinus Torvalds 
670f7ca54f4SFrederik Deweerdt 	copied = 0;
6711da177e4SLinus Torvalds 	while (count > 0) {
6721da177e4SLinus Torvalds 		int this_len, retval;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
6751da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
6761da177e4SLinus Torvalds 			copied = -EFAULT;
6771da177e4SLinus Torvalds 			break;
6781da177e4SLinus Torvalds 		}
6791da177e4SLinus Torvalds 		retval = access_process_vm(task, dst, page, this_len, 1);
6801da177e4SLinus Torvalds 		if (!retval) {
6811da177e4SLinus Torvalds 			if (!copied)
6821da177e4SLinus Torvalds 				copied = -EIO;
6831da177e4SLinus Torvalds 			break;
6841da177e4SLinus Torvalds 		}
6851da177e4SLinus Torvalds 		copied += retval;
6861da177e4SLinus Torvalds 		buf += retval;
6871da177e4SLinus Torvalds 		dst += retval;
6881da177e4SLinus Torvalds 		count -= retval;
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds 	*ppos = dst;
6911da177e4SLinus Torvalds 	free_page((unsigned long) page);
69299f89551SEric W. Biederman out:
69399f89551SEric W. Biederman 	put_task_struct(task);
69499f89551SEric W. Biederman out_no_task:
6951da177e4SLinus Torvalds 	return copied;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds #endif
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	switch (orig) {
7021da177e4SLinus Torvalds 	case 0:
7031da177e4SLinus Torvalds 		file->f_pos = offset;
7041da177e4SLinus Torvalds 		break;
7051da177e4SLinus Torvalds 	case 1:
7061da177e4SLinus Torvalds 		file->f_pos += offset;
7071da177e4SLinus Torvalds 		break;
7081da177e4SLinus Torvalds 	default:
7091da177e4SLinus Torvalds 		return -EINVAL;
7101da177e4SLinus Torvalds 	}
7111da177e4SLinus Torvalds 	force_successful_syscall_return();
7121da177e4SLinus Torvalds 	return file->f_pos;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
71500977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
7161da177e4SLinus Torvalds 	.llseek		= mem_lseek,
7171da177e4SLinus Torvalds 	.read		= mem_read,
7181da177e4SLinus Torvalds 	.write		= mem_write,
7191da177e4SLinus Torvalds 	.open		= mem_open,
7201da177e4SLinus Torvalds };
7211da177e4SLinus Torvalds 
722315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
723315e28c8SJames Pearson 			size_t count, loff_t *ppos)
724315e28c8SJames Pearson {
725315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
726315e28c8SJames Pearson 	char *page;
727315e28c8SJames Pearson 	unsigned long src = *ppos;
728315e28c8SJames Pearson 	int ret = -ESRCH;
729315e28c8SJames Pearson 	struct mm_struct *mm;
730315e28c8SJames Pearson 
731315e28c8SJames Pearson 	if (!task)
732315e28c8SJames Pearson 		goto out_no_task;
733315e28c8SJames Pearson 
734315e28c8SJames Pearson 	if (!ptrace_may_attach(task))
735315e28c8SJames Pearson 		goto out;
736315e28c8SJames Pearson 
737315e28c8SJames Pearson 	ret = -ENOMEM;
738315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
739315e28c8SJames Pearson 	if (!page)
740315e28c8SJames Pearson 		goto out;
741315e28c8SJames Pearson 
742315e28c8SJames Pearson 	ret = 0;
743315e28c8SJames Pearson 
744315e28c8SJames Pearson 	mm = get_task_mm(task);
745315e28c8SJames Pearson 	if (!mm)
746315e28c8SJames Pearson 		goto out_free;
747315e28c8SJames Pearson 
748315e28c8SJames Pearson 	while (count > 0) {
749315e28c8SJames Pearson 		int this_len, retval, max_len;
750315e28c8SJames Pearson 
751315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
752315e28c8SJames Pearson 
753315e28c8SJames Pearson 		if (this_len <= 0)
754315e28c8SJames Pearson 			break;
755315e28c8SJames Pearson 
756315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
757315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
758315e28c8SJames Pearson 
759315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
760315e28c8SJames Pearson 			page, this_len, 0);
761315e28c8SJames Pearson 
762315e28c8SJames Pearson 		if (retval <= 0) {
763315e28c8SJames Pearson 			ret = retval;
764315e28c8SJames Pearson 			break;
765315e28c8SJames Pearson 		}
766315e28c8SJames Pearson 
767315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
768315e28c8SJames Pearson 			ret = -EFAULT;
769315e28c8SJames Pearson 			break;
770315e28c8SJames Pearson 		}
771315e28c8SJames Pearson 
772315e28c8SJames Pearson 		ret += retval;
773315e28c8SJames Pearson 		src += retval;
774315e28c8SJames Pearson 		buf += retval;
775315e28c8SJames Pearson 		count -= retval;
776315e28c8SJames Pearson 	}
777315e28c8SJames Pearson 	*ppos = src;
778315e28c8SJames Pearson 
779315e28c8SJames Pearson 	mmput(mm);
780315e28c8SJames Pearson out_free:
781315e28c8SJames Pearson 	free_page((unsigned long) page);
782315e28c8SJames Pearson out:
783315e28c8SJames Pearson 	put_task_struct(task);
784315e28c8SJames Pearson out_no_task:
785315e28c8SJames Pearson 	return ret;
786315e28c8SJames Pearson }
787315e28c8SJames Pearson 
788315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
789315e28c8SJames Pearson 	.read		= environ_read,
790315e28c8SJames Pearson };
791315e28c8SJames Pearson 
7921da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
7931da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
7941da177e4SLinus Torvalds {
7952fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7968578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
7971da177e4SLinus Torvalds 	size_t len;
79899f89551SEric W. Biederman 	int oom_adjust;
7991da177e4SLinus Torvalds 
80099f89551SEric W. Biederman 	if (!task)
80199f89551SEric W. Biederman 		return -ESRCH;
80299f89551SEric W. Biederman 	oom_adjust = task->oomkilladj;
80399f89551SEric W. Biederman 	put_task_struct(task);
80499f89551SEric W. Biederman 
8058578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
8060c28f287SAkinobu Mita 
8070c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
8111da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
8121da177e4SLinus Torvalds {
81399f89551SEric W. Biederman 	struct task_struct *task;
8148578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF], *end;
8151da177e4SLinus Torvalds 	int oom_adjust;
8161da177e4SLinus Torvalds 
8178578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
8188578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
8198578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
8201da177e4SLinus Torvalds 	if (copy_from_user(buffer, buf, count))
8211da177e4SLinus Torvalds 		return -EFAULT;
8221da177e4SLinus Torvalds 	oom_adjust = simple_strtol(buffer, &end, 0);
8238ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
8248ac773b4SAlexey Dobriyan 	     oom_adjust != OOM_DISABLE)
8251da177e4SLinus Torvalds 		return -EINVAL;
8261da177e4SLinus Torvalds 	if (*end == '\n')
8271da177e4SLinus Torvalds 		end++;
8282fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
82999f89551SEric W. Biederman 	if (!task)
83099f89551SEric W. Biederman 		return -ESRCH;
8318fb4fc68SGuillem Jover 	if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
8328fb4fc68SGuillem Jover 		put_task_struct(task);
8338fb4fc68SGuillem Jover 		return -EACCES;
8348fb4fc68SGuillem Jover 	}
8351da177e4SLinus Torvalds 	task->oomkilladj = oom_adjust;
83699f89551SEric W. Biederman 	put_task_struct(task);
8371da177e4SLinus Torvalds 	if (end - buffer == 0)
8381da177e4SLinus Torvalds 		return -EIO;
8391da177e4SLinus Torvalds 	return end - buffer;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
84200977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
8431da177e4SLinus Torvalds 	.read		= oom_adjust_read,
8441da177e4SLinus Torvalds 	.write		= oom_adjust_write,
8451da177e4SLinus Torvalds };
8461da177e4SLinus Torvalds 
8474b8df891SDavid Rientjes #ifdef CONFIG_MMU
848b813e931SDavid Rientjes static ssize_t clear_refs_write(struct file *file, const char __user *buf,
849b813e931SDavid Rientjes 				size_t count, loff_t *ppos)
850b813e931SDavid Rientjes {
851b813e931SDavid Rientjes 	struct task_struct *task;
852b813e931SDavid Rientjes 	char buffer[PROC_NUMBUF], *end;
853b813e931SDavid Rientjes 	struct mm_struct *mm;
854b813e931SDavid Rientjes 
855b813e931SDavid Rientjes 	memset(buffer, 0, sizeof(buffer));
856b813e931SDavid Rientjes 	if (count > sizeof(buffer) - 1)
857b813e931SDavid Rientjes 		count = sizeof(buffer) - 1;
858b813e931SDavid Rientjes 	if (copy_from_user(buffer, buf, count))
859b813e931SDavid Rientjes 		return -EFAULT;
860b813e931SDavid Rientjes 	if (!simple_strtol(buffer, &end, 0))
861b813e931SDavid Rientjes 		return -EINVAL;
862b813e931SDavid Rientjes 	if (*end == '\n')
863b813e931SDavid Rientjes 		end++;
864b813e931SDavid Rientjes 	task = get_proc_task(file->f_path.dentry->d_inode);
865b813e931SDavid Rientjes 	if (!task)
866b813e931SDavid Rientjes 		return -ESRCH;
867b813e931SDavid Rientjes 	mm = get_task_mm(task);
868b813e931SDavid Rientjes 	if (mm) {
869b813e931SDavid Rientjes 		clear_refs_smap(mm);
870b813e931SDavid Rientjes 		mmput(mm);
871b813e931SDavid Rientjes 	}
872b813e931SDavid Rientjes 	put_task_struct(task);
873b813e931SDavid Rientjes 	if (end - buffer == 0)
874b813e931SDavid Rientjes 		return -EIO;
875b813e931SDavid Rientjes 	return end - buffer;
876b813e931SDavid Rientjes }
877b813e931SDavid Rientjes 
878b813e931SDavid Rientjes static struct file_operations proc_clear_refs_operations = {
879b813e931SDavid Rientjes 	.write		= clear_refs_write,
880b813e931SDavid Rientjes };
8814b8df891SDavid Rientjes #endif
882b813e931SDavid Rientjes 
8831da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
8841da177e4SLinus Torvalds #define TMPBUFLEN 21
8851da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
8861da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
8871da177e4SLinus Torvalds {
8882fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
88999f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
8901da177e4SLinus Torvalds 	ssize_t length;
8911da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
8921da177e4SLinus Torvalds 
89399f89551SEric W. Biederman 	if (!task)
89499f89551SEric W. Biederman 		return -ESRCH;
8951da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
8961da177e4SLinus Torvalds 				audit_get_loginuid(task->audit_context));
89799f89551SEric W. Biederman 	put_task_struct(task);
8981da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
9021da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
9031da177e4SLinus Torvalds {
9042fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
9051da177e4SLinus Torvalds 	char *page, *tmp;
9061da177e4SLinus Torvalds 	ssize_t length;
9071da177e4SLinus Torvalds 	uid_t loginuid;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
9101da177e4SLinus Torvalds 		return -EPERM;
9111da177e4SLinus Torvalds 
91213b41b09SEric W. Biederman 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
9131da177e4SLinus Torvalds 		return -EPERM;
9141da177e4SLinus Torvalds 
915e0182909SAl Viro 	if (count >= PAGE_SIZE)
916e0182909SAl Viro 		count = PAGE_SIZE - 1;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	if (*ppos != 0) {
9191da177e4SLinus Torvalds 		/* No partial writes. */
9201da177e4SLinus Torvalds 		return -EINVAL;
9211da177e4SLinus Torvalds 	}
922e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
9231da177e4SLinus Torvalds 	if (!page)
9241da177e4SLinus Torvalds 		return -ENOMEM;
9251da177e4SLinus Torvalds 	length = -EFAULT;
9261da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
9271da177e4SLinus Torvalds 		goto out_free_page;
9281da177e4SLinus Torvalds 
929e0182909SAl Viro 	page[count] = '\0';
9301da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
9311da177e4SLinus Torvalds 	if (tmp == page) {
9321da177e4SLinus Torvalds 		length = -EINVAL;
9331da177e4SLinus Torvalds 		goto out_free_page;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	}
93699f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
9371da177e4SLinus Torvalds 	if (likely(length == 0))
9381da177e4SLinus Torvalds 		length = count;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds out_free_page:
9411da177e4SLinus Torvalds 	free_page((unsigned long) page);
9421da177e4SLinus Torvalds 	return length;
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
94500977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
9461da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
9471da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
9481da177e4SLinus Torvalds };
9491da177e4SLinus Torvalds #endif
9501da177e4SLinus Torvalds 
951f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
952f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
953f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
954f4f154fdSAkinobu Mita {
955f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
956f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
957f4f154fdSAkinobu Mita 	size_t len;
958f4f154fdSAkinobu Mita 	int make_it_fail;
959f4f154fdSAkinobu Mita 
960f4f154fdSAkinobu Mita 	if (!task)
961f4f154fdSAkinobu Mita 		return -ESRCH;
962f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
963f4f154fdSAkinobu Mita 	put_task_struct(task);
964f4f154fdSAkinobu Mita 
965f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
9660c28f287SAkinobu Mita 
9670c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
968f4f154fdSAkinobu Mita }
969f4f154fdSAkinobu Mita 
970f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
971f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
972f4f154fdSAkinobu Mita {
973f4f154fdSAkinobu Mita 	struct task_struct *task;
974f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
975f4f154fdSAkinobu Mita 	int make_it_fail;
976f4f154fdSAkinobu Mita 
977f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
978f4f154fdSAkinobu Mita 		return -EPERM;
979f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
980f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
981f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
982f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
983f4f154fdSAkinobu Mita 		return -EFAULT;
984f4f154fdSAkinobu Mita 	make_it_fail = simple_strtol(buffer, &end, 0);
985f4f154fdSAkinobu Mita 	if (*end == '\n')
986f4f154fdSAkinobu Mita 		end++;
987f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
988f4f154fdSAkinobu Mita 	if (!task)
989f4f154fdSAkinobu Mita 		return -ESRCH;
990f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
991f4f154fdSAkinobu Mita 	put_task_struct(task);
992f4f154fdSAkinobu Mita 	if (end - buffer == 0)
993f4f154fdSAkinobu Mita 		return -EIO;
994f4f154fdSAkinobu Mita 	return end - buffer;
995f4f154fdSAkinobu Mita }
996f4f154fdSAkinobu Mita 
99700977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
998f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
999f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
1000f4f154fdSAkinobu Mita };
1001f4f154fdSAkinobu Mita #endif
1002f4f154fdSAkinobu Mita 
100343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
100443ae34cbSIngo Molnar /*
100543ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
100643ae34cbSIngo Molnar  */
100743ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
100843ae34cbSIngo Molnar {
100943ae34cbSIngo Molnar 	struct inode *inode = m->private;
101043ae34cbSIngo Molnar 	struct task_struct *p;
101143ae34cbSIngo Molnar 
101243ae34cbSIngo Molnar 	WARN_ON(!inode);
101343ae34cbSIngo Molnar 
101443ae34cbSIngo Molnar 	p = get_proc_task(inode);
101543ae34cbSIngo Molnar 	if (!p)
101643ae34cbSIngo Molnar 		return -ESRCH;
101743ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
101843ae34cbSIngo Molnar 
101943ae34cbSIngo Molnar 	put_task_struct(p);
102043ae34cbSIngo Molnar 
102143ae34cbSIngo Molnar 	return 0;
102243ae34cbSIngo Molnar }
102343ae34cbSIngo Molnar 
102443ae34cbSIngo Molnar static ssize_t
102543ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
102643ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
102743ae34cbSIngo Molnar {
102843ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
102943ae34cbSIngo Molnar 	struct task_struct *p;
103043ae34cbSIngo Molnar 
103143ae34cbSIngo Molnar 	WARN_ON(!inode);
103243ae34cbSIngo Molnar 
103343ae34cbSIngo Molnar 	p = get_proc_task(inode);
103443ae34cbSIngo Molnar 	if (!p)
103543ae34cbSIngo Molnar 		return -ESRCH;
103643ae34cbSIngo Molnar 	proc_sched_set_task(p);
103743ae34cbSIngo Molnar 
103843ae34cbSIngo Molnar 	put_task_struct(p);
103943ae34cbSIngo Molnar 
104043ae34cbSIngo Molnar 	return count;
104143ae34cbSIngo Molnar }
104243ae34cbSIngo Molnar 
104343ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
104443ae34cbSIngo Molnar {
104543ae34cbSIngo Molnar 	int ret;
104643ae34cbSIngo Molnar 
104743ae34cbSIngo Molnar 	ret = single_open(filp, sched_show, NULL);
104843ae34cbSIngo Molnar 	if (!ret) {
104943ae34cbSIngo Molnar 		struct seq_file *m = filp->private_data;
105043ae34cbSIngo Molnar 
105143ae34cbSIngo Molnar 		m->private = inode;
105243ae34cbSIngo Molnar 	}
105343ae34cbSIngo Molnar 	return ret;
105443ae34cbSIngo Molnar }
105543ae34cbSIngo Molnar 
105643ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
105743ae34cbSIngo Molnar 	.open		= sched_open,
105843ae34cbSIngo Molnar 	.read		= seq_read,
105943ae34cbSIngo Molnar 	.write		= sched_write,
106043ae34cbSIngo Molnar 	.llseek		= seq_lseek,
10615ea473a1SAlexey Dobriyan 	.release	= single_release,
106243ae34cbSIngo Molnar };
106343ae34cbSIngo Molnar 
106443ae34cbSIngo Molnar #endif
106543ae34cbSIngo Molnar 
1066008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
10671da177e4SLinus Torvalds {
10681da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
10691da177e4SLinus Torvalds 	int error = -EACCES;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
10721da177e4SLinus Torvalds 	path_release(nd);
10731da177e4SLinus Torvalds 
1074778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1075778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
10761da177e4SLinus Torvalds 		goto out;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
10791da177e4SLinus Torvalds 	nd->last_type = LAST_BIND;
10801da177e4SLinus Torvalds out:
1081008b150aSAl Viro 	return ERR_PTR(error);
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
10851da177e4SLinus Torvalds 			    char __user *buffer, int buflen)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	struct inode * inode;
1088e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1089e12ba74dSMel Gorman 	char *path;
10901da177e4SLinus Torvalds 	int len;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	if (!tmp)
10931da177e4SLinus Torvalds 		return -ENOMEM;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	inode = dentry->d_inode;
10961da177e4SLinus Torvalds 	path = d_path(dentry, mnt, tmp, PAGE_SIZE);
10971da177e4SLinus Torvalds 	len = PTR_ERR(path);
10981da177e4SLinus Torvalds 	if (IS_ERR(path))
10991da177e4SLinus Torvalds 		goto out;
11001da177e4SLinus Torvalds 	len = tmp + PAGE_SIZE - 1 - path;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	if (len > buflen)
11031da177e4SLinus Torvalds 		len = buflen;
11041da177e4SLinus Torvalds 	if (copy_to_user(buffer, path, len))
11051da177e4SLinus Torvalds 		len = -EFAULT;
11061da177e4SLinus Torvalds  out:
11071da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
11081da177e4SLinus Torvalds 	return len;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	int error = -EACCES;
11141da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
11151da177e4SLinus Torvalds 	struct dentry *de;
11161da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
11171da177e4SLinus Torvalds 
1118778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1119778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
11201da177e4SLinus Torvalds 		goto out;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
11231da177e4SLinus Torvalds 	if (error)
11241da177e4SLinus Torvalds 		goto out;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	error = do_proc_readlink(de, mnt, buffer, buflen);
11271da177e4SLinus Torvalds 	dput(de);
11281da177e4SLinus Torvalds 	mntput(mnt);
11291da177e4SLinus Torvalds out:
11301da177e4SLinus Torvalds 	return error;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
1133c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
11341da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
11356d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
11366d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
11371da177e4SLinus Torvalds };
11381da177e4SLinus Torvalds 
113928a6d671SEric W. Biederman 
114028a6d671SEric W. Biederman /* building an inode */
114128a6d671SEric W. Biederman 
114228a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
114328a6d671SEric W. Biederman {
114428a6d671SEric W. Biederman 	int dumpable = 0;
114528a6d671SEric W. Biederman 	struct mm_struct *mm;
114628a6d671SEric W. Biederman 
114728a6d671SEric W. Biederman 	task_lock(task);
114828a6d671SEric W. Biederman 	mm = task->mm;
114928a6d671SEric W. Biederman 	if (mm)
11506c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
115128a6d671SEric W. Biederman 	task_unlock(task);
115228a6d671SEric W. Biederman 	if(dumpable == 1)
115328a6d671SEric W. Biederman 		return 1;
115428a6d671SEric W. Biederman 	return 0;
115528a6d671SEric W. Biederman }
115628a6d671SEric W. Biederman 
115728a6d671SEric W. Biederman 
115861a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
115928a6d671SEric W. Biederman {
116028a6d671SEric W. Biederman 	struct inode * inode;
116128a6d671SEric W. Biederman 	struct proc_inode *ei;
116228a6d671SEric W. Biederman 
116328a6d671SEric W. Biederman 	/* We need a new inode */
116428a6d671SEric W. Biederman 
116528a6d671SEric W. Biederman 	inode = new_inode(sb);
116628a6d671SEric W. Biederman 	if (!inode)
116728a6d671SEric W. Biederman 		goto out;
116828a6d671SEric W. Biederman 
116928a6d671SEric W. Biederman 	/* Common stuff */
117028a6d671SEric W. Biederman 	ei = PROC_I(inode);
117128a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
117228a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
117328a6d671SEric W. Biederman 
117428a6d671SEric W. Biederman 	/*
117528a6d671SEric W. Biederman 	 * grab the reference to task.
117628a6d671SEric W. Biederman 	 */
11771a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
117828a6d671SEric W. Biederman 	if (!ei->pid)
117928a6d671SEric W. Biederman 		goto out_unlock;
118028a6d671SEric W. Biederman 
118128a6d671SEric W. Biederman 	inode->i_uid = 0;
118228a6d671SEric W. Biederman 	inode->i_gid = 0;
118328a6d671SEric W. Biederman 	if (task_dumpable(task)) {
118428a6d671SEric W. Biederman 		inode->i_uid = task->euid;
118528a6d671SEric W. Biederman 		inode->i_gid = task->egid;
118628a6d671SEric W. Biederman 	}
118728a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
118828a6d671SEric W. Biederman 
118928a6d671SEric W. Biederman out:
119028a6d671SEric W. Biederman 	return inode;
119128a6d671SEric W. Biederman 
119228a6d671SEric W. Biederman out_unlock:
119328a6d671SEric W. Biederman 	iput(inode);
119428a6d671SEric W. Biederman 	return NULL;
119528a6d671SEric W. Biederman }
119628a6d671SEric W. Biederman 
119728a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
119828a6d671SEric W. Biederman {
119928a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
120028a6d671SEric W. Biederman 	struct task_struct *task;
120128a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
120228a6d671SEric W. Biederman 
120328a6d671SEric W. Biederman 	rcu_read_lock();
120428a6d671SEric W. Biederman 	stat->uid = 0;
120528a6d671SEric W. Biederman 	stat->gid = 0;
120628a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
120728a6d671SEric W. Biederman 	if (task) {
120828a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
120928a6d671SEric W. Biederman 		    task_dumpable(task)) {
121028a6d671SEric W. Biederman 			stat->uid = task->euid;
121128a6d671SEric W. Biederman 			stat->gid = task->egid;
121228a6d671SEric W. Biederman 		}
121328a6d671SEric W. Biederman 	}
121428a6d671SEric W. Biederman 	rcu_read_unlock();
121528a6d671SEric W. Biederman 	return 0;
121628a6d671SEric W. Biederman }
121728a6d671SEric W. Biederman 
121828a6d671SEric W. Biederman /* dentry stuff */
121928a6d671SEric W. Biederman 
122028a6d671SEric W. Biederman /*
122128a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
122228a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
122328a6d671SEric W. Biederman  * due to the way we treat inodes.
122428a6d671SEric W. Biederman  *
122528a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
122628a6d671SEric W. Biederman  * performed a setuid(), etc.
122728a6d671SEric W. Biederman  *
122828a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
122928a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
123028a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
123128a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
123228a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
123328a6d671SEric W. Biederman  * directories.
123428a6d671SEric W. Biederman  */
123528a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
123628a6d671SEric W. Biederman {
123728a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
123828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
123928a6d671SEric W. Biederman 	if (task) {
124028a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
124128a6d671SEric W. Biederman 		    task_dumpable(task)) {
124228a6d671SEric W. Biederman 			inode->i_uid = task->euid;
124328a6d671SEric W. Biederman 			inode->i_gid = task->egid;
124428a6d671SEric W. Biederman 		} else {
124528a6d671SEric W. Biederman 			inode->i_uid = 0;
124628a6d671SEric W. Biederman 			inode->i_gid = 0;
124728a6d671SEric W. Biederman 		}
124828a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
124928a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
125028a6d671SEric W. Biederman 		put_task_struct(task);
125128a6d671SEric W. Biederman 		return 1;
125228a6d671SEric W. Biederman 	}
125328a6d671SEric W. Biederman 	d_drop(dentry);
125428a6d671SEric W. Biederman 	return 0;
125528a6d671SEric W. Biederman }
125628a6d671SEric W. Biederman 
125728a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry)
125828a6d671SEric W. Biederman {
125928a6d671SEric W. Biederman 	/* Is the task we represent dead?
126028a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
126128a6d671SEric W. Biederman 	 * kill it immediately.
126228a6d671SEric W. Biederman 	 */
126328a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
126428a6d671SEric W. Biederman }
126528a6d671SEric W. Biederman 
126628a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations =
126728a6d671SEric W. Biederman {
126828a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
126928a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
127028a6d671SEric W. Biederman };
127128a6d671SEric W. Biederman 
127228a6d671SEric W. Biederman /* Lookups */
127328a6d671SEric W. Biederman 
1274c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1275c5141e6dSEric Dumazet 				struct task_struct *, const void *);
127661a28784SEric W. Biederman 
12771c0d04c9SEric W. Biederman /*
12781c0d04c9SEric W. Biederman  * Fill a directory entry.
12791c0d04c9SEric W. Biederman  *
12801c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
12811c0d04c9SEric W. Biederman  * file type from dcache entry.
12821c0d04c9SEric W. Biederman  *
12831c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
12841c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
12851c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
12861c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
12871c0d04c9SEric W. Biederman  * by stat.
12881c0d04c9SEric W. Biederman  */
128961a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
129061a28784SEric W. Biederman 	char *name, int len,
1291c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
129261a28784SEric W. Biederman {
12932fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
129461a28784SEric W. Biederman 	struct inode *inode;
129561a28784SEric W. Biederman 	struct qstr qname;
129661a28784SEric W. Biederman 	ino_t ino = 0;
129761a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
129861a28784SEric W. Biederman 
129961a28784SEric W. Biederman 	qname.name = name;
130061a28784SEric W. Biederman 	qname.len  = len;
130161a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
130261a28784SEric W. Biederman 
130361a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
130461a28784SEric W. Biederman 	if (!child) {
130561a28784SEric W. Biederman 		struct dentry *new;
130661a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
130761a28784SEric W. Biederman 		if (new) {
130861a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
130961a28784SEric W. Biederman 			if (child)
131061a28784SEric W. Biederman 				dput(new);
131161a28784SEric W. Biederman 			else
131261a28784SEric W. Biederman 				child = new;
131361a28784SEric W. Biederman 		}
131461a28784SEric W. Biederman 	}
131561a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
131661a28784SEric W. Biederman 		goto end_instantiate;
131761a28784SEric W. Biederman 	inode = child->d_inode;
131861a28784SEric W. Biederman 	if (inode) {
131961a28784SEric W. Biederman 		ino = inode->i_ino;
132061a28784SEric W. Biederman 		type = inode->i_mode >> 12;
132161a28784SEric W. Biederman 	}
132261a28784SEric W. Biederman 	dput(child);
132361a28784SEric W. Biederman end_instantiate:
132461a28784SEric W. Biederman 	if (!ino)
132561a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
132661a28784SEric W. Biederman 	if (!ino)
132761a28784SEric W. Biederman 		ino = 1;
132861a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
132961a28784SEric W. Biederman }
133061a28784SEric W. Biederman 
133128a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
133228a6d671SEric W. Biederman {
133328a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
133428a6d671SEric W. Biederman 	int len = dentry->d_name.len;
133528a6d671SEric W. Biederman 	unsigned n = 0;
133628a6d671SEric W. Biederman 
133728a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
133828a6d671SEric W. Biederman 		goto out;
133928a6d671SEric W. Biederman 	while (len-- > 0) {
134028a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
134128a6d671SEric W. Biederman 		if (c > 9)
134228a6d671SEric W. Biederman 			goto out;
134328a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
134428a6d671SEric W. Biederman 			goto out;
134528a6d671SEric W. Biederman 		n *= 10;
134628a6d671SEric W. Biederman 		n += c;
134728a6d671SEric W. Biederman 	}
134828a6d671SEric W. Biederman 	return n;
134928a6d671SEric W. Biederman out:
135028a6d671SEric W. Biederman 	return ~0U;
135128a6d671SEric W. Biederman }
135228a6d671SEric W. Biederman 
135327932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
135427932742SMiklos Szeredi 
135527932742SMiklos Szeredi static int proc_fd_info(struct inode *inode, struct dentry **dentry,
135627932742SMiklos Szeredi 			struct vfsmount **mnt, char *info)
135728a6d671SEric W. Biederman {
135828a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
135928a6d671SEric W. Biederman 	struct files_struct *files = NULL;
136028a6d671SEric W. Biederman 	struct file *file;
136128a6d671SEric W. Biederman 	int fd = proc_fd(inode);
136228a6d671SEric W. Biederman 
136328a6d671SEric W. Biederman 	if (task) {
136428a6d671SEric W. Biederman 		files = get_files_struct(task);
136528a6d671SEric W. Biederman 		put_task_struct(task);
136628a6d671SEric W. Biederman 	}
136728a6d671SEric W. Biederman 	if (files) {
136828a6d671SEric W. Biederman 		/*
136928a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
137028a6d671SEric W. Biederman 		 * hold ->file_lock.
137128a6d671SEric W. Biederman 		 */
137228a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
137328a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
137428a6d671SEric W. Biederman 		if (file) {
137527932742SMiklos Szeredi 			if (mnt)
13762fddfeefSJosef "Jeff" Sipek 				*mnt = mntget(file->f_path.mnt);
137727932742SMiklos Szeredi 			if (dentry)
13782fddfeefSJosef "Jeff" Sipek 				*dentry = dget(file->f_path.dentry);
137927932742SMiklos Szeredi 			if (info)
138027932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
138127932742SMiklos Szeredi 					 "pos:\t%lli\n"
138227932742SMiklos Szeredi 					 "flags:\t0%o\n",
138327932742SMiklos Szeredi 					 (long long) file->f_pos,
138427932742SMiklos Szeredi 					 file->f_flags);
138528a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
138628a6d671SEric W. Biederman 			put_files_struct(files);
138728a6d671SEric W. Biederman 			return 0;
138828a6d671SEric W. Biederman 		}
138928a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
139028a6d671SEric W. Biederman 		put_files_struct(files);
139128a6d671SEric W. Biederman 	}
139228a6d671SEric W. Biederman 	return -ENOENT;
139328a6d671SEric W. Biederman }
139428a6d671SEric W. Biederman 
139527932742SMiklos Szeredi static int proc_fd_link(struct inode *inode, struct dentry **dentry,
139627932742SMiklos Szeredi 			struct vfsmount **mnt)
139727932742SMiklos Szeredi {
139827932742SMiklos Szeredi 	return proc_fd_info(inode, dentry, mnt, NULL);
139927932742SMiklos Szeredi }
140027932742SMiklos Szeredi 
140128a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
140228a6d671SEric W. Biederman {
140328a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
140428a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
140528a6d671SEric W. Biederman 	int fd = proc_fd(inode);
140628a6d671SEric W. Biederman 	struct files_struct *files;
140728a6d671SEric W. Biederman 
140828a6d671SEric W. Biederman 	if (task) {
140928a6d671SEric W. Biederman 		files = get_files_struct(task);
141028a6d671SEric W. Biederman 		if (files) {
141128a6d671SEric W. Biederman 			rcu_read_lock();
141228a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
141328a6d671SEric W. Biederman 				rcu_read_unlock();
141428a6d671SEric W. Biederman 				put_files_struct(files);
141528a6d671SEric W. Biederman 				if (task_dumpable(task)) {
141628a6d671SEric W. Biederman 					inode->i_uid = task->euid;
141728a6d671SEric W. Biederman 					inode->i_gid = task->egid;
141828a6d671SEric W. Biederman 				} else {
141928a6d671SEric W. Biederman 					inode->i_uid = 0;
142028a6d671SEric W. Biederman 					inode->i_gid = 0;
142128a6d671SEric W. Biederman 				}
142228a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
142328a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
142428a6d671SEric W. Biederman 				put_task_struct(task);
142528a6d671SEric W. Biederman 				return 1;
142628a6d671SEric W. Biederman 			}
142728a6d671SEric W. Biederman 			rcu_read_unlock();
142828a6d671SEric W. Biederman 			put_files_struct(files);
142928a6d671SEric W. Biederman 		}
143028a6d671SEric W. Biederman 		put_task_struct(task);
143128a6d671SEric W. Biederman 	}
143228a6d671SEric W. Biederman 	d_drop(dentry);
143328a6d671SEric W. Biederman 	return 0;
143428a6d671SEric W. Biederman }
143528a6d671SEric W. Biederman 
143628a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations =
143728a6d671SEric W. Biederman {
143828a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
143928a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
144028a6d671SEric W. Biederman };
144128a6d671SEric W. Biederman 
1442444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1443c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
144428a6d671SEric W. Biederman {
1445c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
144628a6d671SEric W. Biederman 	struct file *file;
144728a6d671SEric W. Biederman 	struct files_struct *files;
144828a6d671SEric W. Biederman  	struct inode *inode;
144928a6d671SEric W. Biederman  	struct proc_inode *ei;
1450444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
145128a6d671SEric W. Biederman 
145261a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
145328a6d671SEric W. Biederman 	if (!inode)
145428a6d671SEric W. Biederman 		goto out;
145528a6d671SEric W. Biederman 	ei = PROC_I(inode);
145628a6d671SEric W. Biederman 	ei->fd = fd;
145728a6d671SEric W. Biederman 	files = get_files_struct(task);
145828a6d671SEric W. Biederman 	if (!files)
1459444ceed8SEric W. Biederman 		goto out_iput;
146028a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
146128a6d671SEric W. Biederman 
146228a6d671SEric W. Biederman 	/*
146328a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
146428a6d671SEric W. Biederman 	 * hold ->file_lock.
146528a6d671SEric W. Biederman 	 */
146628a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
146728a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
146828a6d671SEric W. Biederman 	if (!file)
1469444ceed8SEric W. Biederman 		goto out_unlock;
147028a6d671SEric W. Biederman 	if (file->f_mode & 1)
147128a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
147228a6d671SEric W. Biederman 	if (file->f_mode & 2)
147328a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
147428a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
147528a6d671SEric W. Biederman 	put_files_struct(files);
1476444ceed8SEric W. Biederman 
147728a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
147828a6d671SEric W. Biederman 	inode->i_size = 64;
147928a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
148028a6d671SEric W. Biederman 	dentry->d_op = &tid_fd_dentry_operations;
148128a6d671SEric W. Biederman 	d_add(dentry, inode);
148228a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
148328a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1484444ceed8SEric W. Biederman 		error = NULL;
1485444ceed8SEric W. Biederman 
1486444ceed8SEric W. Biederman  out:
1487444ceed8SEric W. Biederman 	return error;
1488444ceed8SEric W. Biederman out_unlock:
1489444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1490444ceed8SEric W. Biederman 	put_files_struct(files);
1491444ceed8SEric W. Biederman out_iput:
1492444ceed8SEric W. Biederman 	iput(inode);
1493444ceed8SEric W. Biederman 	goto out;
1494444ceed8SEric W. Biederman }
1495444ceed8SEric W. Biederman 
149627932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
149727932742SMiklos Szeredi 					   struct dentry *dentry,
149827932742SMiklos Szeredi 					   instantiate_t instantiate)
1499444ceed8SEric W. Biederman {
1500444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1501444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1502444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1503444ceed8SEric W. Biederman 
1504444ceed8SEric W. Biederman 	if (!task)
1505444ceed8SEric W. Biederman 		goto out_no_task;
1506444ceed8SEric W. Biederman 	if (fd == ~0U)
1507444ceed8SEric W. Biederman 		goto out;
1508444ceed8SEric W. Biederman 
150927932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
151028a6d671SEric W. Biederman out:
151128a6d671SEric W. Biederman 	put_task_struct(task);
151228a6d671SEric W. Biederman out_no_task:
151328a6d671SEric W. Biederman 	return result;
151428a6d671SEric W. Biederman }
151528a6d671SEric W. Biederman 
151627932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
151727932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
15181da177e4SLinus Torvalds {
15192fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
15205634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
152199f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
15221da177e4SLinus Torvalds 	unsigned int fd, tid, ino;
15231da177e4SLinus Torvalds 	int retval;
15241da177e4SLinus Torvalds 	struct files_struct * files;
1525badf1662SDipankar Sarma 	struct fdtable *fdt;
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	retval = -ENOENT;
152899f89551SEric W. Biederman 	if (!p)
152999f89551SEric W. Biederman 		goto out_no_task;
15301da177e4SLinus Torvalds 	retval = 0;
15311da177e4SLinus Torvalds 	tid = p->pid;
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	fd = filp->f_pos;
15341da177e4SLinus Torvalds 	switch (fd) {
15351da177e4SLinus Torvalds 		case 0:
15361da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
15371da177e4SLinus Torvalds 				goto out;
15381da177e4SLinus Torvalds 			filp->f_pos++;
15391da177e4SLinus Torvalds 		case 1:
15405634708bSEric W. Biederman 			ino = parent_ino(dentry);
15411da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
15421da177e4SLinus Torvalds 				goto out;
15431da177e4SLinus Torvalds 			filp->f_pos++;
15441da177e4SLinus Torvalds 		default:
15451da177e4SLinus Torvalds 			files = get_files_struct(p);
15461da177e4SLinus Torvalds 			if (!files)
15471da177e4SLinus Torvalds 				goto out;
1548b835996fSDipankar Sarma 			rcu_read_lock();
1549badf1662SDipankar Sarma 			fdt = files_fdtable(files);
15501da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
1551badf1662SDipankar Sarma 			     fd < fdt->max_fds;
15521da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
155327932742SMiklos Szeredi 				char name[PROC_NUMBUF];
155427932742SMiklos Szeredi 				int len;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
15571da177e4SLinus Torvalds 					continue;
1558b835996fSDipankar Sarma 				rcu_read_unlock();
15591da177e4SLinus Torvalds 
156027932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
156127932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
156227932742SMiklos Szeredi 						    name, len, instantiate,
156327932742SMiklos Szeredi 						    p, &fd) < 0) {
1564b835996fSDipankar Sarma 					rcu_read_lock();
15651da177e4SLinus Torvalds 					break;
15661da177e4SLinus Torvalds 				}
1567b835996fSDipankar Sarma 				rcu_read_lock();
15681da177e4SLinus Torvalds 			}
1569b835996fSDipankar Sarma 			rcu_read_unlock();
15701da177e4SLinus Torvalds 			put_files_struct(files);
15711da177e4SLinus Torvalds 	}
15721da177e4SLinus Torvalds out:
157399f89551SEric W. Biederman 	put_task_struct(p);
157499f89551SEric W. Biederman out_no_task:
15751da177e4SLinus Torvalds 	return retval;
15761da177e4SLinus Torvalds }
15771da177e4SLinus Torvalds 
157827932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
157927932742SMiklos Szeredi 				    struct nameidata *nd)
158027932742SMiklos Szeredi {
158127932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
158227932742SMiklos Szeredi }
158327932742SMiklos Szeredi 
158427932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
158527932742SMiklos Szeredi {
158627932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
158727932742SMiklos Szeredi }
158827932742SMiklos Szeredi 
158927932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
159027932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
159127932742SMiklos Szeredi {
159227932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
159327932742SMiklos Szeredi 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
159427932742SMiklos Szeredi 	if (!err)
159527932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
159627932742SMiklos Szeredi 	return err;
159727932742SMiklos Szeredi }
159827932742SMiklos Szeredi 
159927932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
160027932742SMiklos Szeredi 	.open		= nonseekable_open,
160127932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
160227932742SMiklos Szeredi };
160327932742SMiklos Szeredi 
160400977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
16051da177e4SLinus Torvalds 	.read		= generic_read_dir,
16061da177e4SLinus Torvalds 	.readdir	= proc_readfd,
16071da177e4SLinus Torvalds };
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds /*
16108948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
16118948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
16128948e11fSAlexey Dobriyan  */
16138948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask,
16148948e11fSAlexey Dobriyan 				struct nameidata *nd)
16158948e11fSAlexey Dobriyan {
16168948e11fSAlexey Dobriyan 	int rv;
16178948e11fSAlexey Dobriyan 
16188948e11fSAlexey Dobriyan 	rv = generic_permission(inode, mask, NULL);
16198948e11fSAlexey Dobriyan 	if (rv == 0)
16208948e11fSAlexey Dobriyan 		return 0;
16218948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
16228948e11fSAlexey Dobriyan 		rv = 0;
16238948e11fSAlexey Dobriyan 	return rv;
16248948e11fSAlexey Dobriyan }
16258948e11fSAlexey Dobriyan 
16268948e11fSAlexey Dobriyan /*
16271da177e4SLinus Torvalds  * proc directories can do almost nothing..
16281da177e4SLinus Torvalds  */
1629c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
16301da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
16318948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
16326d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
16331da177e4SLinus Torvalds };
16341da177e4SLinus Torvalds 
163527932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
163627932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
163727932742SMiklos Szeredi {
163827932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
163927932742SMiklos Szeredi  	struct inode *inode;
164027932742SMiklos Szeredi  	struct proc_inode *ei;
164127932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
164227932742SMiklos Szeredi 
164327932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
164427932742SMiklos Szeredi 	if (!inode)
164527932742SMiklos Szeredi 		goto out;
164627932742SMiklos Szeredi 	ei = PROC_I(inode);
164727932742SMiklos Szeredi 	ei->fd = fd;
164827932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
164927932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
165027932742SMiklos Szeredi 	dentry->d_op = &tid_fd_dentry_operations;
165127932742SMiklos Szeredi 	d_add(dentry, inode);
165227932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
165327932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
165427932742SMiklos Szeredi 		error = NULL;
165527932742SMiklos Szeredi 
165627932742SMiklos Szeredi  out:
165727932742SMiklos Szeredi 	return error;
165827932742SMiklos Szeredi }
165927932742SMiklos Szeredi 
166027932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
166127932742SMiklos Szeredi 					struct dentry *dentry,
166227932742SMiklos Szeredi 					struct nameidata *nd)
166327932742SMiklos Szeredi {
166427932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
166527932742SMiklos Szeredi }
166627932742SMiklos Szeredi 
166727932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
166827932742SMiklos Szeredi {
166927932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
167027932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
167127932742SMiklos Szeredi }
167227932742SMiklos Szeredi 
167327932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
167427932742SMiklos Szeredi 	.read		= generic_read_dir,
167527932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
167627932742SMiklos Szeredi };
167727932742SMiklos Szeredi 
167827932742SMiklos Szeredi /*
167927932742SMiklos Szeredi  * proc directories can do almost nothing..
168027932742SMiklos Szeredi  */
168127932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
168227932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
168327932742SMiklos Szeredi 	.setattr	= proc_setattr,
168427932742SMiklos Szeredi };
168527932742SMiklos Szeredi 
168627932742SMiklos Szeredi 
1687444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
1688c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
1689444ceed8SEric W. Biederman {
1690c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
1691444ceed8SEric W. Biederman 	struct inode *inode;
1692444ceed8SEric W. Biederman 	struct proc_inode *ei;
1693444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-EINVAL);
1694444ceed8SEric W. Biederman 
169561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
1696444ceed8SEric W. Biederman 	if (!inode)
1697444ceed8SEric W. Biederman 		goto out;
1698444ceed8SEric W. Biederman 
1699444ceed8SEric W. Biederman 	ei = PROC_I(inode);
1700444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
1701444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
1702444ceed8SEric W. Biederman 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
1703444ceed8SEric W. Biederman 	if (p->iop)
1704444ceed8SEric W. Biederman 		inode->i_op = p->iop;
1705444ceed8SEric W. Biederman 	if (p->fop)
1706444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
1707444ceed8SEric W. Biederman 	ei->op = p->op;
1708444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
1709444ceed8SEric W. Biederman 	d_add(dentry, inode);
1710444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
1711444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
1712444ceed8SEric W. Biederman 		error = NULL;
1713444ceed8SEric W. Biederman out:
1714444ceed8SEric W. Biederman 	return error;
1715444ceed8SEric W. Biederman }
1716444ceed8SEric W. Biederman 
17171da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
17181da177e4SLinus Torvalds 					 struct dentry *dentry,
1719c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
17207bcd6b0eSEric W. Biederman 					 unsigned int nents)
17211da177e4SLinus Torvalds {
17221da177e4SLinus Torvalds 	struct inode *inode;
1723cd6a3ce9SEric W. Biederman 	struct dentry *error;
172499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1725c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
17261da177e4SLinus Torvalds 
1727cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
17281da177e4SLinus Torvalds 	inode = NULL;
17291da177e4SLinus Torvalds 
173099f89551SEric W. Biederman 	if (!task)
173199f89551SEric W. Biederman 		goto out_no_task;
17321da177e4SLinus Torvalds 
173320cdc894SEric W. Biederman 	/*
173420cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
173520cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
173620cdc894SEric W. Biederman 	 */
17377bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
17387bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
17391da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
17401da177e4SLinus Torvalds 			continue;
17411da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
17421da177e4SLinus Torvalds 			break;
17431da177e4SLinus Torvalds 	}
17447bcd6b0eSEric W. Biederman 	if (p > last)
17451da177e4SLinus Torvalds 		goto out;
17461da177e4SLinus Torvalds 
1747444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
17481da177e4SLinus Torvalds out:
174999f89551SEric W. Biederman 	put_task_struct(task);
175099f89551SEric W. Biederman out_no_task:
1751cd6a3ce9SEric W. Biederman 	return error;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
1754c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
1755c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
175661a28784SEric W. Biederman {
175761a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
175861a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
175961a28784SEric W. Biederman }
176061a28784SEric W. Biederman 
176128a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
176228a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
1763c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
176428a6d671SEric W. Biederman {
176528a6d671SEric W. Biederman 	int i;
176628a6d671SEric W. Biederman 	int pid;
17672fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
176828a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
176928a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1770c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
177128a6d671SEric W. Biederman 	ino_t ino;
177228a6d671SEric W. Biederman 	int ret;
177328a6d671SEric W. Biederman 
177428a6d671SEric W. Biederman 	ret = -ENOENT;
177528a6d671SEric W. Biederman 	if (!task)
177661a28784SEric W. Biederman 		goto out_no_task;
177728a6d671SEric W. Biederman 
177828a6d671SEric W. Biederman 	ret = 0;
177928a6d671SEric W. Biederman 	pid = task->pid;
178028a6d671SEric W. Biederman 	i = filp->f_pos;
178128a6d671SEric W. Biederman 	switch (i) {
178228a6d671SEric W. Biederman 	case 0:
178328a6d671SEric W. Biederman 		ino = inode->i_ino;
178428a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
178528a6d671SEric W. Biederman 			goto out;
178628a6d671SEric W. Biederman 		i++;
178728a6d671SEric W. Biederman 		filp->f_pos++;
178828a6d671SEric W. Biederman 		/* fall through */
178928a6d671SEric W. Biederman 	case 1:
179028a6d671SEric W. Biederman 		ino = parent_ino(dentry);
179128a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
179228a6d671SEric W. Biederman 			goto out;
179328a6d671SEric W. Biederman 		i++;
179428a6d671SEric W. Biederman 		filp->f_pos++;
179528a6d671SEric W. Biederman 		/* fall through */
179628a6d671SEric W. Biederman 	default:
179728a6d671SEric W. Biederman 		i -= 2;
179828a6d671SEric W. Biederman 		if (i >= nents) {
179928a6d671SEric W. Biederman 			ret = 1;
180028a6d671SEric W. Biederman 			goto out;
180128a6d671SEric W. Biederman 		}
180228a6d671SEric W. Biederman 		p = ents + i;
18037bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
18047bcd6b0eSEric W. Biederman 		while (p <= last) {
180561a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
180628a6d671SEric W. Biederman 				goto out;
180728a6d671SEric W. Biederman 			filp->f_pos++;
180828a6d671SEric W. Biederman 			p++;
180928a6d671SEric W. Biederman 		}
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 
181228a6d671SEric W. Biederman 	ret = 1;
181328a6d671SEric W. Biederman out:
181461a28784SEric W. Biederman 	put_task_struct(task);
181561a28784SEric W. Biederman out_no_task:
181628a6d671SEric W. Biederman 	return ret;
18171da177e4SLinus Torvalds }
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
182028a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
182128a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
182228a6d671SEric W. Biederman {
18232fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
182404ff9708SAl Viro 	char *p = NULL;
182528a6d671SEric W. Biederman 	ssize_t length;
182628a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
182728a6d671SEric W. Biederman 
182828a6d671SEric W. Biederman 	if (!task)
182904ff9708SAl Viro 		return -ESRCH;
183028a6d671SEric W. Biederman 
183128a6d671SEric W. Biederman 	length = security_getprocattr(task,
18322fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
183304ff9708SAl Viro 				      &p);
183428a6d671SEric W. Biederman 	put_task_struct(task);
183504ff9708SAl Viro 	if (length > 0)
183604ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
183704ff9708SAl Viro 	kfree(p);
183828a6d671SEric W. Biederman 	return length;
183928a6d671SEric W. Biederman }
184028a6d671SEric W. Biederman 
184128a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
184228a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
184328a6d671SEric W. Biederman {
18442fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
184528a6d671SEric W. Biederman 	char *page;
184628a6d671SEric W. Biederman 	ssize_t length;
184728a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
184828a6d671SEric W. Biederman 
184928a6d671SEric W. Biederman 	length = -ESRCH;
185028a6d671SEric W. Biederman 	if (!task)
185128a6d671SEric W. Biederman 		goto out_no_task;
185228a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
185328a6d671SEric W. Biederman 		count = PAGE_SIZE;
185428a6d671SEric W. Biederman 
185528a6d671SEric W. Biederman 	/* No partial writes. */
185628a6d671SEric W. Biederman 	length = -EINVAL;
185728a6d671SEric W. Biederman 	if (*ppos != 0)
185828a6d671SEric W. Biederman 		goto out;
185928a6d671SEric W. Biederman 
186028a6d671SEric W. Biederman 	length = -ENOMEM;
1861e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
186228a6d671SEric W. Biederman 	if (!page)
186328a6d671SEric W. Biederman 		goto out;
186428a6d671SEric W. Biederman 
186528a6d671SEric W. Biederman 	length = -EFAULT;
186628a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
186728a6d671SEric W. Biederman 		goto out_free;
186828a6d671SEric W. Biederman 
186928a6d671SEric W. Biederman 	length = security_setprocattr(task,
18702fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
187128a6d671SEric W. Biederman 				      (void*)page, count);
187228a6d671SEric W. Biederman out_free:
187328a6d671SEric W. Biederman 	free_page((unsigned long) page);
187428a6d671SEric W. Biederman out:
187528a6d671SEric W. Biederman 	put_task_struct(task);
187628a6d671SEric W. Biederman out_no_task:
187728a6d671SEric W. Biederman 	return length;
187828a6d671SEric W. Biederman }
187928a6d671SEric W. Biederman 
188000977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
188128a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
188228a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
188328a6d671SEric W. Biederman };
188428a6d671SEric W. Biederman 
1885c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
188661a28784SEric W. Biederman 	REG("current",    S_IRUGO|S_IWUGO, pid_attr),
188761a28784SEric W. Biederman 	REG("prev",       S_IRUGO,	   pid_attr),
188861a28784SEric W. Biederman 	REG("exec",       S_IRUGO|S_IWUGO, pid_attr),
188961a28784SEric W. Biederman 	REG("fscreate",   S_IRUGO|S_IWUGO, pid_attr),
189061a28784SEric W. Biederman 	REG("keycreate",  S_IRUGO|S_IWUGO, pid_attr),
189161a28784SEric W. Biederman 	REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
189228a6d671SEric W. Biederman };
189328a6d671SEric W. Biederman 
189472d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
18951da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
189872d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
18991da177e4SLinus Torvalds }
19001da177e4SLinus Torvalds 
190100977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
19021da177e4SLinus Torvalds 	.read		= generic_read_dir,
190372d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
19041da177e4SLinus Torvalds };
19051da177e4SLinus Torvalds 
190672d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
19071da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
19081da177e4SLinus Torvalds {
19097bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
19107bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds 
1913c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
191472d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
191599f89551SEric W. Biederman 	.getattr	= pid_getattr,
19166d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
19171da177e4SLinus Torvalds };
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds #endif
19201da177e4SLinus Torvalds 
19213cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
19223cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
19233cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
19243cb4a0bbSKawai, Hidehiro {
19253cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
19263cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
19273cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
19283cb4a0bbSKawai, Hidehiro 	size_t len;
19293cb4a0bbSKawai, Hidehiro 	int ret;
19303cb4a0bbSKawai, Hidehiro 
19313cb4a0bbSKawai, Hidehiro 	if (!task)
19323cb4a0bbSKawai, Hidehiro 		return -ESRCH;
19333cb4a0bbSKawai, Hidehiro 
19343cb4a0bbSKawai, Hidehiro 	ret = 0;
19353cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
19363cb4a0bbSKawai, Hidehiro 	if (mm) {
19373cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
19383cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
19393cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
19403cb4a0bbSKawai, Hidehiro 		mmput(mm);
19413cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
19423cb4a0bbSKawai, Hidehiro 	}
19433cb4a0bbSKawai, Hidehiro 
19443cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
19453cb4a0bbSKawai, Hidehiro 
19463cb4a0bbSKawai, Hidehiro 	return ret;
19473cb4a0bbSKawai, Hidehiro }
19483cb4a0bbSKawai, Hidehiro 
19493cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
19503cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
19513cb4a0bbSKawai, Hidehiro 					  size_t count,
19523cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
19533cb4a0bbSKawai, Hidehiro {
19543cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
19553cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
19563cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
19573cb4a0bbSKawai, Hidehiro 	unsigned int val;
19583cb4a0bbSKawai, Hidehiro 	int ret;
19593cb4a0bbSKawai, Hidehiro 	int i;
19603cb4a0bbSKawai, Hidehiro 	unsigned long mask;
19613cb4a0bbSKawai, Hidehiro 
19623cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
19633cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
19643cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
19653cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
19663cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
19673cb4a0bbSKawai, Hidehiro 		goto out_no_task;
19683cb4a0bbSKawai, Hidehiro 
19693cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
19703cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
19713cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
19723cb4a0bbSKawai, Hidehiro 		end++;
19733cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
19743cb4a0bbSKawai, Hidehiro 		goto out_no_task;
19753cb4a0bbSKawai, Hidehiro 
19763cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
19773cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
19783cb4a0bbSKawai, Hidehiro 	if (!task)
19793cb4a0bbSKawai, Hidehiro 		goto out_no_task;
19803cb4a0bbSKawai, Hidehiro 
19813cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
19823cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
19833cb4a0bbSKawai, Hidehiro 	if (!mm)
19843cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
19853cb4a0bbSKawai, Hidehiro 
19863cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
19873cb4a0bbSKawai, Hidehiro 		if (val & mask)
19883cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
19893cb4a0bbSKawai, Hidehiro 		else
19903cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
19913cb4a0bbSKawai, Hidehiro 	}
19923cb4a0bbSKawai, Hidehiro 
19933cb4a0bbSKawai, Hidehiro 	mmput(mm);
19943cb4a0bbSKawai, Hidehiro  out_no_mm:
19953cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
19963cb4a0bbSKawai, Hidehiro  out_no_task:
19973cb4a0bbSKawai, Hidehiro 	return ret;
19983cb4a0bbSKawai, Hidehiro }
19993cb4a0bbSKawai, Hidehiro 
20003cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
20013cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
20023cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
20033cb4a0bbSKawai, Hidehiro };
20043cb4a0bbSKawai, Hidehiro #endif
20053cb4a0bbSKawai, Hidehiro 
20061da177e4SLinus Torvalds /*
20071da177e4SLinus Torvalds  * /proc/self:
20081da177e4SLinus Torvalds  */
20091da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
20101da177e4SLinus Torvalds 			      int buflen)
20111da177e4SLinus Torvalds {
20128578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2013b488893aSPavel Emelyanov 	sprintf(tmp, "%d", task_tgid_vnr(current));
20141da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
20151da177e4SLinus Torvalds }
20161da177e4SLinus Torvalds 
2017008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
20181da177e4SLinus Torvalds {
20198578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2020b488893aSPavel Emelyanov 	sprintf(tmp, "%d", task_tgid_vnr(current));
2021008b150aSAl Viro 	return ERR_PTR(vfs_follow_link(nd,tmp));
20221da177e4SLinus Torvalds }
20231da177e4SLinus Torvalds 
2024c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
20251da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
20261da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
20271da177e4SLinus Torvalds };
20281da177e4SLinus Torvalds 
202928a6d671SEric W. Biederman /*
2030801199ceSEric W. Biederman  * proc base
2031801199ceSEric W. Biederman  *
2032801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2033801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2034801199ceSEric W. Biederman  * describe something that is process related.
2035801199ceSEric W. Biederman  */
2036c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
203761a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2038801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2039801199ceSEric W. Biederman };
2040801199ceSEric W. Biederman 
2041801199ceSEric W. Biederman /*
2042801199ceSEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
2043801199ceSEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
2044801199ceSEric W. Biederman  * due to the way we treat inodes.
2045801199ceSEric W. Biederman  */
2046801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2047801199ceSEric W. Biederman {
2048801199ceSEric W. Biederman 	struct inode *inode = dentry->d_inode;
2049801199ceSEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2050801199ceSEric W. Biederman 	if (task) {
2051801199ceSEric W. Biederman 		put_task_struct(task);
2052801199ceSEric W. Biederman 		return 1;
2053801199ceSEric W. Biederman 	}
2054801199ceSEric W. Biederman 	d_drop(dentry);
2055801199ceSEric W. Biederman 	return 0;
2056801199ceSEric W. Biederman }
2057801199ceSEric W. Biederman 
2058801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations =
2059801199ceSEric W. Biederman {
2060801199ceSEric W. Biederman 	.d_revalidate	= proc_base_revalidate,
2061801199ceSEric W. Biederman 	.d_delete	= pid_delete_dentry,
2062801199ceSEric W. Biederman };
2063801199ceSEric W. Biederman 
2064444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2065c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2066801199ceSEric W. Biederman {
2067c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2068801199ceSEric W. Biederman 	struct inode *inode;
2069801199ceSEric W. Biederman 	struct proc_inode *ei;
2070444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-EINVAL);
2071801199ceSEric W. Biederman 
2072801199ceSEric W. Biederman 	/* Allocate the inode */
2073801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2074801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2075801199ceSEric W. Biederman 	if (!inode)
2076801199ceSEric W. Biederman 		goto out;
2077801199ceSEric W. Biederman 
2078801199ceSEric W. Biederman 	/* Initialize the inode */
2079801199ceSEric W. Biederman 	ei = PROC_I(inode);
2080801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2081801199ceSEric W. Biederman 
2082801199ceSEric W. Biederman 	/*
2083801199ceSEric W. Biederman 	 * grab the reference to the task.
2084801199ceSEric W. Biederman 	 */
20851a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2086801199ceSEric W. Biederman 	if (!ei->pid)
2087801199ceSEric W. Biederman 		goto out_iput;
2088801199ceSEric W. Biederman 
2089801199ceSEric W. Biederman 	inode->i_uid = 0;
2090801199ceSEric W. Biederman 	inode->i_gid = 0;
2091801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2092801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2093801199ceSEric W. Biederman 		inode->i_nlink = 2;
2094801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2095801199ceSEric W. Biederman 		inode->i_size = 64;
2096801199ceSEric W. Biederman 	if (p->iop)
2097801199ceSEric W. Biederman 		inode->i_op = p->iop;
2098801199ceSEric W. Biederman 	if (p->fop)
2099801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2100801199ceSEric W. Biederman 	ei->op = p->op;
2101801199ceSEric W. Biederman 	dentry->d_op = &proc_base_dentry_operations;
2102801199ceSEric W. Biederman 	d_add(dentry, inode);
2103801199ceSEric W. Biederman 	error = NULL;
2104801199ceSEric W. Biederman out:
2105801199ceSEric W. Biederman 	return error;
2106801199ceSEric W. Biederman out_iput:
2107801199ceSEric W. Biederman 	iput(inode);
2108801199ceSEric W. Biederman 	goto out;
2109801199ceSEric W. Biederman }
2110801199ceSEric W. Biederman 
2111444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2112444ceed8SEric W. Biederman {
2113444ceed8SEric W. Biederman 	struct dentry *error;
2114444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2115c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2116444ceed8SEric W. Biederman 
2117444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2118444ceed8SEric W. Biederman 
2119444ceed8SEric W. Biederman 	if (!task)
2120444ceed8SEric W. Biederman 		goto out_no_task;
2121444ceed8SEric W. Biederman 
2122444ceed8SEric W. Biederman 	/* Lookup the directory entry */
21237bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
21247bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2125444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2126444ceed8SEric W. Biederman 			continue;
2127444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2128444ceed8SEric W. Biederman 			break;
2129444ceed8SEric W. Biederman 	}
21307bcd6b0eSEric W. Biederman 	if (p > last)
2131444ceed8SEric W. Biederman 		goto out;
2132444ceed8SEric W. Biederman 
2133444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2134444ceed8SEric W. Biederman 
2135444ceed8SEric W. Biederman out:
2136444ceed8SEric W. Biederman 	put_task_struct(task);
2137444ceed8SEric W. Biederman out_no_task:
2138444ceed8SEric W. Biederman 	return error;
2139444ceed8SEric W. Biederman }
2140444ceed8SEric W. Biederman 
2141c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2142c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
214361a28784SEric W. Biederman {
214461a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
214561a28784SEric W. Biederman 				proc_base_instantiate, task, p);
214661a28784SEric W. Biederman }
214761a28784SEric W. Biederman 
2148aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2149aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2150aba76fdbSAndrew Morton {
2151aba76fdbSAndrew Morton 	return sprintf(buffer,
21524b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
2153aba76fdbSAndrew Morton 			"rchar: %llu\n"
2154aba76fdbSAndrew Morton 			"wchar: %llu\n"
2155aba76fdbSAndrew Morton 			"syscr: %llu\n"
2156aba76fdbSAndrew Morton 			"syscw: %llu\n"
21574b98d11bSAlexey Dobriyan #endif
2158aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2159aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2160aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
21614b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
2162aba76fdbSAndrew Morton 			(unsigned long long)task->rchar,
2163aba76fdbSAndrew Morton 			(unsigned long long)task->wchar,
2164aba76fdbSAndrew Morton 			(unsigned long long)task->syscr,
2165aba76fdbSAndrew Morton 			(unsigned long long)task->syscw,
21664b98d11bSAlexey Dobriyan #endif
2167aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.read_bytes,
2168aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.write_bytes,
2169aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.cancelled_write_bytes);
2170aba76fdbSAndrew Morton }
2171aba76fdbSAndrew Morton #endif
2172aba76fdbSAndrew Morton 
2173801199ceSEric W. Biederman /*
217428a6d671SEric W. Biederman  * Thread groups
217528a6d671SEric W. Biederman  */
217600977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2177c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
217820cdc894SEric W. Biederman 
2179c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
218061a28784SEric W. Biederman 	DIR("task",       S_IRUGO|S_IXUGO, task),
218161a28784SEric W. Biederman 	DIR("fd",         S_IRUSR|S_IXUSR, fd),
218227932742SMiklos Szeredi 	DIR("fdinfo",     S_IRUSR|S_IXUSR, fdinfo),
2183315e28c8SJames Pearson 	REG("environ",    S_IRUSR, environ),
218461a28784SEric W. Biederman 	INF("auxv",       S_IRUSR, pid_auxv),
218561a28784SEric W. Biederman 	INF("status",     S_IRUGO, pid_status),
2186*d85f50d5SNeil Horman 	INF("limits",	  S_IRUSR, pid_limits),
218743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
218843ae34cbSIngo Molnar 	REG("sched",      S_IRUGO|S_IWUSR, pid_sched),
218943ae34cbSIngo Molnar #endif
219061a28784SEric W. Biederman 	INF("cmdline",    S_IRUGO, pid_cmdline),
219161a28784SEric W. Biederman 	INF("stat",       S_IRUGO, tgid_stat),
219261a28784SEric W. Biederman 	INF("statm",      S_IRUGO, pid_statm),
219361a28784SEric W. Biederman 	REG("maps",       S_IRUGO, maps),
219428a6d671SEric W. Biederman #ifdef CONFIG_NUMA
219561a28784SEric W. Biederman 	REG("numa_maps",  S_IRUGO, numa_maps),
219628a6d671SEric W. Biederman #endif
219761a28784SEric W. Biederman 	REG("mem",        S_IRUSR|S_IWUSR, mem),
219861a28784SEric W. Biederman 	LNK("cwd",        cwd),
219961a28784SEric W. Biederman 	LNK("root",       root),
220061a28784SEric W. Biederman 	LNK("exe",        exe),
220161a28784SEric W. Biederman 	REG("mounts",     S_IRUGO, mounts),
220261a28784SEric W. Biederman 	REG("mountstats", S_IRUSR, mountstats),
220328a6d671SEric W. Biederman #ifdef CONFIG_MMU
2204b813e931SDavid Rientjes 	REG("clear_refs", S_IWUSR, clear_refs),
220561a28784SEric W. Biederman 	REG("smaps",      S_IRUGO, smaps),
220628a6d671SEric W. Biederman #endif
220728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
220872d9dcfcSEric W. Biederman 	DIR("attr",       S_IRUGO|S_IXUGO, attr_dir),
220928a6d671SEric W. Biederman #endif
221028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
221161a28784SEric W. Biederman 	INF("wchan",      S_IRUGO, pid_wchan),
221228a6d671SEric W. Biederman #endif
221328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
221461a28784SEric W. Biederman 	INF("schedstat",  S_IRUGO, pid_schedstat),
221528a6d671SEric W. Biederman #endif
22168793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
221761a28784SEric W. Biederman 	REG("cpuset",     S_IRUGO, cpuset),
221828a6d671SEric W. Biederman #endif
2219a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2220a424316cSPaul Menage 	REG("cgroup",  S_IRUGO, cgroup),
2221a424316cSPaul Menage #endif
222261a28784SEric W. Biederman 	INF("oom_score",  S_IRUGO, oom_score),
222361a28784SEric W. Biederman 	REG("oom_adj",    S_IRUGO|S_IWUSR, oom_adjust),
222428a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
222561a28784SEric W. Biederman 	REG("loginuid",   S_IWUSR|S_IRUGO, loginuid),
222628a6d671SEric W. Biederman #endif
2227f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2228f4f154fdSAkinobu Mita 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2229f4f154fdSAkinobu Mita #endif
22303cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
22313cb4a0bbSKawai, Hidehiro 	REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
22323cb4a0bbSKawai, Hidehiro #endif
2233aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2234aba76fdbSAndrew Morton 	INF("io",	S_IRUGO, pid_io_accounting),
2235aba76fdbSAndrew Morton #endif
223628a6d671SEric W. Biederman };
223728a6d671SEric W. Biederman 
223828a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
223928a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
224028a6d671SEric W. Biederman {
224128a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
224228a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
224328a6d671SEric W. Biederman }
224428a6d671SEric W. Biederman 
224500977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
224628a6d671SEric W. Biederman 	.read		= generic_read_dir,
224728a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
224828a6d671SEric W. Biederman };
224928a6d671SEric W. Biederman 
225028a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
22517bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
22527bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
225328a6d671SEric W. Biederman }
225428a6d671SEric W. Biederman 
2255c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
225628a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
225728a6d671SEric W. Biederman 	.getattr	= pid_getattr,
225828a6d671SEric W. Biederman 	.setattr	= proc_setattr,
225928a6d671SEric W. Biederman };
226028a6d671SEric W. Biederman 
22611da177e4SLinus Torvalds /**
226248e6484dSEric W. Biederman  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
22631da177e4SLinus Torvalds  *
226448e6484dSEric W. Biederman  * @task: task that should be flushed.
22651da177e4SLinus Torvalds  *
226648e6484dSEric W. Biederman  * Looks in the dcache for
226748e6484dSEric W. Biederman  * /proc/@pid
226848e6484dSEric W. Biederman  * /proc/@tgid/task/@pid
226948e6484dSEric W. Biederman  * if either directory is present flushes it and all of it'ts children
227048e6484dSEric W. Biederman  * from the dcache.
227148e6484dSEric W. Biederman  *
227248e6484dSEric W. Biederman  * It is safe and reasonable to cache /proc entries for a task until
227348e6484dSEric W. Biederman  * that task exits.  After that they just clog up the dcache with
227448e6484dSEric W. Biederman  * useless entries, possibly causing useful dcache entries to be
227548e6484dSEric W. Biederman  * flushed instead.  This routine is proved to flush those useless
227648e6484dSEric W. Biederman  * dcache entries at process exit time.
227748e6484dSEric W. Biederman  *
227848e6484dSEric W. Biederman  * NOTE: This routine is just an optimization so it does not guarantee
227948e6484dSEric W. Biederman  *       that no dcache entries will exist at process exit time it
228048e6484dSEric W. Biederman  *       just makes it very unlikely that any will persist.
22811da177e4SLinus Torvalds  */
228260347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
22831da177e4SLinus Torvalds {
228448e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
22858578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
228648e6484dSEric W. Biederman 	struct qstr name;
22871da177e4SLinus Torvalds 
228848e6484dSEric W. Biederman 	name.name = buf;
228960347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
229060347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
229148e6484dSEric W. Biederman 	if (dentry) {
229248e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
229348e6484dSEric W. Biederman 		d_drop(dentry);
229448e6484dSEric W. Biederman 		dput(dentry);
22951da177e4SLinus Torvalds 	}
22961da177e4SLinus Torvalds 
229760347f67SPavel Emelyanov 	if (tgid == 0)
229848e6484dSEric W. Biederman 		goto out;
22991da177e4SLinus Torvalds 
230048e6484dSEric W. Biederman 	name.name = buf;
230160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
230260347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
230348e6484dSEric W. Biederman 	if (!leader)
230448e6484dSEric W. Biederman 		goto out;
230548e6484dSEric W. Biederman 
230648e6484dSEric W. Biederman 	name.name = "task";
230748e6484dSEric W. Biederman 	name.len = strlen(name.name);
230848e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
230948e6484dSEric W. Biederman 	if (!dir)
231048e6484dSEric W. Biederman 		goto out_put_leader;
231148e6484dSEric W. Biederman 
231248e6484dSEric W. Biederman 	name.name = buf;
231360347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
231448e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
231548e6484dSEric W. Biederman 	if (dentry) {
231648e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
231748e6484dSEric W. Biederman 		d_drop(dentry);
231848e6484dSEric W. Biederman 		dput(dentry);
23191da177e4SLinus Torvalds 	}
232048e6484dSEric W. Biederman 
232148e6484dSEric W. Biederman 	dput(dir);
232248e6484dSEric W. Biederman out_put_leader:
232348e6484dSEric W. Biederman 	dput(leader);
232448e6484dSEric W. Biederman out:
232548e6484dSEric W. Biederman 	return;
23261da177e4SLinus Torvalds }
23271da177e4SLinus Torvalds 
232860347f67SPavel Emelyanov /*
232960347f67SPavel Emelyanov  * when flushing dentries from proc one need to flush them from global
233060347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
233160347f67SPavel Emelyanov  * in. this call is supposed to make all this job.
233260347f67SPavel Emelyanov  */
233360347f67SPavel Emelyanov 
233460347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
233560347f67SPavel Emelyanov {
2336130f77ecSPavel Emelyanov 	int i, leader;
2337130f77ecSPavel Emelyanov 	struct pid *pid, *tgid;
2338130f77ecSPavel Emelyanov 	struct upid *upid;
2339130f77ecSPavel Emelyanov 
2340130f77ecSPavel Emelyanov 	leader = thread_group_leader(task);
2341130f77ecSPavel Emelyanov 	proc_flush_task_mnt(proc_mnt, task->pid, leader ? task->tgid : 0);
2342130f77ecSPavel Emelyanov 	pid = task_pid(task);
2343130f77ecSPavel Emelyanov 	if (pid->level == 0)
2344130f77ecSPavel Emelyanov 		return;
2345130f77ecSPavel Emelyanov 
2346130f77ecSPavel Emelyanov 	tgid = task_tgid(task);
2347130f77ecSPavel Emelyanov 	for (i = 1; i <= pid->level; i++) {
2348130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2349130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2350130f77ecSPavel Emelyanov 				leader ? 0 : tgid->numbers[i].nr);
2351130f77ecSPavel Emelyanov 	}
23526f4e6433SPavel Emelyanov 
23536f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
23546f4e6433SPavel Emelyanov 	if (upid->nr == 1)
23556f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
235660347f67SPavel Emelyanov }
235760347f67SPavel Emelyanov 
23589711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
23599711ef99SAdrian Bunk 					   struct dentry * dentry,
2360c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2361444ceed8SEric W. Biederman {
2362444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2363444ceed8SEric W. Biederman 	struct inode *inode;
2364444ceed8SEric W. Biederman 
236561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2366444ceed8SEric W. Biederman 	if (!inode)
2367444ceed8SEric W. Biederman 		goto out;
2368444ceed8SEric W. Biederman 
2369444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2370444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2371444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2372444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
237327932742SMiklos Szeredi 	inode->i_nlink = 5;
2374444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY
2375444ceed8SEric W. Biederman 	inode->i_nlink += 1;
2376444ceed8SEric W. Biederman #endif
2377444ceed8SEric W. Biederman 
2378444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2379444ceed8SEric W. Biederman 
2380444ceed8SEric W. Biederman 	d_add(dentry, inode);
2381444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2382444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2383444ceed8SEric W. Biederman 		error = NULL;
2384444ceed8SEric W. Biederman out:
2385444ceed8SEric W. Biederman 	return error;
2386444ceed8SEric W. Biederman }
2387444ceed8SEric W. Biederman 
23881da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
23891da177e4SLinus Torvalds {
2390cd6a3ce9SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
23911da177e4SLinus Torvalds 	struct task_struct *task;
23921da177e4SLinus Torvalds 	unsigned tgid;
2393b488893aSPavel Emelyanov 	struct pid_namespace *ns;
23941da177e4SLinus Torvalds 
2395801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
2396801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2397801199ceSEric W. Biederman 		goto out;
2398801199ceSEric W. Biederman 
23991da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
24001da177e4SLinus Torvalds 	if (tgid == ~0U)
24011da177e4SLinus Torvalds 		goto out;
24021da177e4SLinus Torvalds 
2403b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2404de758734SEric W. Biederman 	rcu_read_lock();
2405b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
24061da177e4SLinus Torvalds 	if (task)
24071da177e4SLinus Torvalds 		get_task_struct(task);
2408de758734SEric W. Biederman 	rcu_read_unlock();
24091da177e4SLinus Torvalds 	if (!task)
24101da177e4SLinus Torvalds 		goto out;
24111da177e4SLinus Torvalds 
2412444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
241348e6484dSEric W. Biederman 	put_task_struct(task);
24141da177e4SLinus Torvalds out:
2415cd6a3ce9SEric W. Biederman 	return result;
24161da177e4SLinus Torvalds }
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds /*
24190804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
24200bc58a91SEric W. Biederman  *
24211da177e4SLinus Torvalds  */
2422b488893aSPavel Emelyanov static struct task_struct *next_tgid(unsigned int tgid,
2423b488893aSPavel Emelyanov 		struct pid_namespace *ns)
24241da177e4SLinus Torvalds {
24250804ef4bSEric W. Biederman 	struct task_struct *task;
24260804ef4bSEric W. Biederman 	struct pid *pid;
24271da177e4SLinus Torvalds 
24280804ef4bSEric W. Biederman 	rcu_read_lock();
24290804ef4bSEric W. Biederman retry:
24300804ef4bSEric W. Biederman 	task = NULL;
2431b488893aSPavel Emelyanov 	pid = find_ge_pid(tgid, ns);
24320804ef4bSEric W. Biederman 	if (pid) {
2433b488893aSPavel Emelyanov 		tgid = pid_nr_ns(pid, ns) + 1;
24340804ef4bSEric W. Biederman 		task = pid_task(pid, PIDTYPE_PID);
24350804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
24360804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
24370804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
24380804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
24390804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
24400804ef4bSEric W. Biederman 		 *
24410804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
24420804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
24430804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
24440804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
24450804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
24460bc58a91SEric W. Biederman 		 */
24470804ef4bSEric W. Biederman 		if (!task || !has_group_leader_pid(task))
24480804ef4bSEric W. Biederman 			goto retry;
24490804ef4bSEric W. Biederman 		get_task_struct(task);
24501da177e4SLinus Torvalds 	}
2451454cc105SEric W. Biederman 	rcu_read_unlock();
24520804ef4bSEric W. Biederman 	return task;
24531da177e4SLinus Torvalds }
24541da177e4SLinus Torvalds 
24557bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
24561da177e4SLinus Torvalds 
245761a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
245861a28784SEric W. Biederman 	struct task_struct *task, int tgid)
245961a28784SEric W. Biederman {
246061a28784SEric W. Biederman 	char name[PROC_NUMBUF];
246161a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tgid);
246261a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
246361a28784SEric W. Biederman 				proc_pid_instantiate, task, NULL);
246461a28784SEric W. Biederman }
246561a28784SEric W. Biederman 
24661da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
24671da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
24681da177e4SLinus Torvalds {
24691da177e4SLinus Torvalds 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
24702fddfeefSJosef "Jeff" Sipek 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
24710bc58a91SEric W. Biederman 	struct task_struct *task;
24720bc58a91SEric W. Biederman 	int tgid;
2473b488893aSPavel Emelyanov 	struct pid_namespace *ns;
24741da177e4SLinus Torvalds 
247561a28784SEric W. Biederman 	if (!reaper)
247661a28784SEric W. Biederman 		goto out_no_task;
247761a28784SEric W. Biederman 
24787bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2479c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
248061a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2481801199ceSEric W. Biederman 			goto out;
24821da177e4SLinus Torvalds 	}
24831da177e4SLinus Torvalds 
2484b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
24850804ef4bSEric W. Biederman 	tgid = filp->f_pos - TGID_OFFSET;
2486b488893aSPavel Emelyanov 	for (task = next_tgid(tgid, ns);
24870bc58a91SEric W. Biederman 	     task;
2488b488893aSPavel Emelyanov 	     put_task_struct(task), task = next_tgid(tgid + 1, ns)) {
2489b488893aSPavel Emelyanov 		tgid = task_pid_nr_ns(task, ns);
24900804ef4bSEric W. Biederman 		filp->f_pos = tgid + TGID_OFFSET;
249161a28784SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
24920bc58a91SEric W. Biederman 			put_task_struct(task);
24930804ef4bSEric W. Biederman 			goto out;
24941da177e4SLinus Torvalds 		}
24951da177e4SLinus Torvalds 	}
24960804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
24970804ef4bSEric W. Biederman out:
249861a28784SEric W. Biederman 	put_task_struct(reaper);
249961a28784SEric W. Biederman out_no_task:
25001da177e4SLinus Torvalds 	return 0;
25011da177e4SLinus Torvalds }
25021da177e4SLinus Torvalds 
25030bc58a91SEric W. Biederman /*
250428a6d671SEric W. Biederman  * Tasks
250528a6d671SEric W. Biederman  */
2506c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
250761a28784SEric W. Biederman 	DIR("fd",        S_IRUSR|S_IXUSR, fd),
250827932742SMiklos Szeredi 	DIR("fdinfo",    S_IRUSR|S_IXUSR, fdinfo),
2509315e28c8SJames Pearson 	REG("environ",   S_IRUSR, environ),
251061a28784SEric W. Biederman 	INF("auxv",      S_IRUSR, pid_auxv),
251161a28784SEric W. Biederman 	INF("status",    S_IRUGO, pid_status),
2512*d85f50d5SNeil Horman 	INF("limits",	 S_IRUSR, pid_limits),
251343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
251443ae34cbSIngo Molnar 	REG("sched",     S_IRUGO|S_IWUSR, pid_sched),
251543ae34cbSIngo Molnar #endif
251661a28784SEric W. Biederman 	INF("cmdline",   S_IRUGO, pid_cmdline),
251761a28784SEric W. Biederman 	INF("stat",      S_IRUGO, tid_stat),
251861a28784SEric W. Biederman 	INF("statm",     S_IRUGO, pid_statm),
251961a28784SEric W. Biederman 	REG("maps",      S_IRUGO, maps),
252028a6d671SEric W. Biederman #ifdef CONFIG_NUMA
252161a28784SEric W. Biederman 	REG("numa_maps", S_IRUGO, numa_maps),
252228a6d671SEric W. Biederman #endif
252361a28784SEric W. Biederman 	REG("mem",       S_IRUSR|S_IWUSR, mem),
252461a28784SEric W. Biederman 	LNK("cwd",       cwd),
252561a28784SEric W. Biederman 	LNK("root",      root),
252661a28784SEric W. Biederman 	LNK("exe",       exe),
252761a28784SEric W. Biederman 	REG("mounts",    S_IRUGO, mounts),
252828a6d671SEric W. Biederman #ifdef CONFIG_MMU
2529b813e931SDavid Rientjes 	REG("clear_refs", S_IWUSR, clear_refs),
253061a28784SEric W. Biederman 	REG("smaps",     S_IRUGO, smaps),
253128a6d671SEric W. Biederman #endif
253228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
253372d9dcfcSEric W. Biederman 	DIR("attr",      S_IRUGO|S_IXUGO, attr_dir),
253428a6d671SEric W. Biederman #endif
253528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
253661a28784SEric W. Biederman 	INF("wchan",     S_IRUGO, pid_wchan),
253728a6d671SEric W. Biederman #endif
253828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
253961a28784SEric W. Biederman 	INF("schedstat", S_IRUGO, pid_schedstat),
254028a6d671SEric W. Biederman #endif
25418793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
254261a28784SEric W. Biederman 	REG("cpuset",    S_IRUGO, cpuset),
254328a6d671SEric W. Biederman #endif
2544a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2545a424316cSPaul Menage 	REG("cgroup",  S_IRUGO, cgroup),
2546a424316cSPaul Menage #endif
254761a28784SEric W. Biederman 	INF("oom_score", S_IRUGO, oom_score),
254861a28784SEric W. Biederman 	REG("oom_adj",   S_IRUGO|S_IWUSR, oom_adjust),
254928a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
255061a28784SEric W. Biederman 	REG("loginuid",  S_IWUSR|S_IRUGO, loginuid),
255128a6d671SEric W. Biederman #endif
2552f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2553f4f154fdSAkinobu Mita 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2554f4f154fdSAkinobu Mita #endif
255528a6d671SEric W. Biederman };
255628a6d671SEric W. Biederman 
255728a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
255828a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
255928a6d671SEric W. Biederman {
256028a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
256128a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
256228a6d671SEric W. Biederman }
256328a6d671SEric W. Biederman 
256428a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
25657bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
25667bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
256728a6d671SEric W. Biederman }
256828a6d671SEric W. Biederman 
256900977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
257028a6d671SEric W. Biederman 	.read		= generic_read_dir,
257128a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
257228a6d671SEric W. Biederman };
257328a6d671SEric W. Biederman 
2574c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
257528a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
257628a6d671SEric W. Biederman 	.getattr	= pid_getattr,
257728a6d671SEric W. Biederman 	.setattr	= proc_setattr,
257828a6d671SEric W. Biederman };
257928a6d671SEric W. Biederman 
2580444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
2581c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2582444ceed8SEric W. Biederman {
2583444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2584444ceed8SEric W. Biederman 	struct inode *inode;
258561a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2586444ceed8SEric W. Biederman 
2587444ceed8SEric W. Biederman 	if (!inode)
2588444ceed8SEric W. Biederman 		goto out;
2589444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2590444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
2591444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
2592444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
259327932742SMiklos Szeredi 	inode->i_nlink = 4;
2594444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY
2595444ceed8SEric W. Biederman 	inode->i_nlink += 1;
2596444ceed8SEric W. Biederman #endif
2597444ceed8SEric W. Biederman 
2598444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2599444ceed8SEric W. Biederman 
2600444ceed8SEric W. Biederman 	d_add(dentry, inode);
2601444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2602444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2603444ceed8SEric W. Biederman 		error = NULL;
2604444ceed8SEric W. Biederman out:
2605444ceed8SEric W. Biederman 	return error;
2606444ceed8SEric W. Biederman }
2607444ceed8SEric W. Biederman 
260828a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
260928a6d671SEric W. Biederman {
261028a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
261128a6d671SEric W. Biederman 	struct task_struct *task;
261228a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
261328a6d671SEric W. Biederman 	unsigned tid;
2614b488893aSPavel Emelyanov 	struct pid_namespace *ns;
261528a6d671SEric W. Biederman 
261628a6d671SEric W. Biederman 	if (!leader)
261728a6d671SEric W. Biederman 		goto out_no_task;
261828a6d671SEric W. Biederman 
261928a6d671SEric W. Biederman 	tid = name_to_int(dentry);
262028a6d671SEric W. Biederman 	if (tid == ~0U)
262128a6d671SEric W. Biederman 		goto out;
262228a6d671SEric W. Biederman 
2623b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
262428a6d671SEric W. Biederman 	rcu_read_lock();
2625b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
262628a6d671SEric W. Biederman 	if (task)
262728a6d671SEric W. Biederman 		get_task_struct(task);
262828a6d671SEric W. Biederman 	rcu_read_unlock();
262928a6d671SEric W. Biederman 	if (!task)
263028a6d671SEric W. Biederman 		goto out;
2631bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
263228a6d671SEric W. Biederman 		goto out_drop_task;
263328a6d671SEric W. Biederman 
2634444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
263528a6d671SEric W. Biederman out_drop_task:
263628a6d671SEric W. Biederman 	put_task_struct(task);
263728a6d671SEric W. Biederman out:
263828a6d671SEric W. Biederman 	put_task_struct(leader);
263928a6d671SEric W. Biederman out_no_task:
264028a6d671SEric W. Biederman 	return result;
264128a6d671SEric W. Biederman }
264228a6d671SEric W. Biederman 
264328a6d671SEric W. Biederman /*
26440bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
26450bc58a91SEric W. Biederman  *
26460bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
26470bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
26480bc58a91SEric W. Biederman  * directory we have more work todo.
26490bc58a91SEric W. Biederman  *
26500bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
26510bc58a91SEric W. Biederman  *
26520bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
26530bc58a91SEric W. Biederman  * threads past it.
26540bc58a91SEric W. Biederman  */
2655cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
2656b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
26570bc58a91SEric W. Biederman {
2658a872ff0cSOleg Nesterov 	struct task_struct *pos;
26590bc58a91SEric W. Biederman 
2660cc288738SEric W. Biederman 	rcu_read_lock();
26610bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
26620bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
2663b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
2664a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
2665a872ff0cSOleg Nesterov 			goto found;
26660bc58a91SEric W. Biederman 	}
26670bc58a91SEric W. Biederman 
26680bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
26690bc58a91SEric W. Biederman 	pos = NULL;
2670a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
2671a872ff0cSOleg Nesterov 		goto out;
2672a872ff0cSOleg Nesterov 
2673a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
2674a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
2675a872ff0cSOleg Nesterov 	 */
2676a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
2677a872ff0cSOleg Nesterov 		pos = next_thread(pos);
2678a872ff0cSOleg Nesterov 		if (pos == leader) {
2679a872ff0cSOleg Nesterov 			pos = NULL;
2680a872ff0cSOleg Nesterov 			goto out;
2681a872ff0cSOleg Nesterov 		}
2682a872ff0cSOleg Nesterov 	}
2683a872ff0cSOleg Nesterov found:
2684a872ff0cSOleg Nesterov 	get_task_struct(pos);
2685a872ff0cSOleg Nesterov out:
2686cc288738SEric W. Biederman 	rcu_read_unlock();
26870bc58a91SEric W. Biederman 	return pos;
26880bc58a91SEric W. Biederman }
26890bc58a91SEric W. Biederman 
26900bc58a91SEric W. Biederman /*
26910bc58a91SEric W. Biederman  * Find the next thread in the thread list.
26920bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
26930bc58a91SEric W. Biederman  *
26940bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
26950bc58a91SEric W. Biederman  */
26960bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
26970bc58a91SEric W. Biederman {
2698c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
2699cc288738SEric W. Biederman 	rcu_read_lock();
2700c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
27010bc58a91SEric W. Biederman 		pos = next_thread(start);
2702c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
27030bc58a91SEric W. Biederman 			pos = NULL;
2704c1df7fb8SOleg Nesterov 		else
2705c1df7fb8SOleg Nesterov 			get_task_struct(pos);
2706c1df7fb8SOleg Nesterov 	}
2707cc288738SEric W. Biederman 	rcu_read_unlock();
27080bc58a91SEric W. Biederman 	put_task_struct(start);
27090bc58a91SEric W. Biederman 	return pos;
27100bc58a91SEric W. Biederman }
27110bc58a91SEric W. Biederman 
271261a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
271361a28784SEric W. Biederman 	struct task_struct *task, int tid)
271461a28784SEric W. Biederman {
271561a28784SEric W. Biederman 	char name[PROC_NUMBUF];
271661a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
271761a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
271861a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
271961a28784SEric W. Biederman }
272061a28784SEric W. Biederman 
27211da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
27221da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
27231da177e4SLinus Torvalds {
27242fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
27251da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
27267d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
27270bc58a91SEric W. Biederman 	struct task_struct *task;
27281da177e4SLinus Torvalds 	int retval = -ENOENT;
27291da177e4SLinus Torvalds 	ino_t ino;
27300bc58a91SEric W. Biederman 	int tid;
27311da177e4SLinus Torvalds 	unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2732b488893aSPavel Emelyanov 	struct pid_namespace *ns;
27331da177e4SLinus Torvalds 
27347d895244SGuillaume Chazarain 	task = get_proc_task(inode);
27357d895244SGuillaume Chazarain 	if (!task)
27367d895244SGuillaume Chazarain 		goto out_no_task;
27377d895244SGuillaume Chazarain 	rcu_read_lock();
27387d895244SGuillaume Chazarain 	if (pid_alive(task)) {
27397d895244SGuillaume Chazarain 		leader = task->group_leader;
27407d895244SGuillaume Chazarain 		get_task_struct(leader);
27417d895244SGuillaume Chazarain 	}
27427d895244SGuillaume Chazarain 	rcu_read_unlock();
27437d895244SGuillaume Chazarain 	put_task_struct(task);
274499f89551SEric W. Biederman 	if (!leader)
274599f89551SEric W. Biederman 		goto out_no_task;
27461da177e4SLinus Torvalds 	retval = 0;
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	switch (pos) {
27491da177e4SLinus Torvalds 	case 0:
27501da177e4SLinus Torvalds 		ino = inode->i_ino;
27511da177e4SLinus Torvalds 		if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
27521da177e4SLinus Torvalds 			goto out;
27531da177e4SLinus Torvalds 		pos++;
27541da177e4SLinus Torvalds 		/* fall through */
27551da177e4SLinus Torvalds 	case 1:
27561da177e4SLinus Torvalds 		ino = parent_ino(dentry);
27571da177e4SLinus Torvalds 		if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
27581da177e4SLinus Torvalds 			goto out;
27591da177e4SLinus Torvalds 		pos++;
27601da177e4SLinus Torvalds 		/* fall through */
27611da177e4SLinus Torvalds 	}
27621da177e4SLinus Torvalds 
27630bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
27640bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
27650bc58a91SEric W. Biederman 	 */
2766b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
27672b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
27680bc58a91SEric W. Biederman 	filp->f_version = 0;
2769b488893aSPavel Emelyanov 	for (task = first_tid(leader, tid, pos - 2, ns);
27700bc58a91SEric W. Biederman 	     task;
27710bc58a91SEric W. Biederman 	     task = next_tid(task), pos++) {
2772b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
277361a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
27740bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
27750bc58a91SEric W. Biederman 			 * pid for the next readir call */
27762b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
27770bc58a91SEric W. Biederman 			put_task_struct(task);
27781da177e4SLinus Torvalds 			break;
27790bc58a91SEric W. Biederman 		}
27801da177e4SLinus Torvalds 	}
27811da177e4SLinus Torvalds out:
27821da177e4SLinus Torvalds 	filp->f_pos = pos;
278399f89551SEric W. Biederman 	put_task_struct(leader);
278499f89551SEric W. Biederman out_no_task:
27851da177e4SLinus Torvalds 	return retval;
27861da177e4SLinus Torvalds }
27876e66b52bSEric W. Biederman 
27886e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
27896e66b52bSEric W. Biederman {
27906e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
279199f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
27926e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
27936e66b52bSEric W. Biederman 
279499f89551SEric W. Biederman 	if (p) {
279599f89551SEric W. Biederman 		rcu_read_lock();
279699f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
279799f89551SEric W. Biederman 		rcu_read_unlock();
279899f89551SEric W. Biederman 		put_task_struct(p);
27996e66b52bSEric W. Biederman 	}
28006e66b52bSEric W. Biederman 
28016e66b52bSEric W. Biederman 	return 0;
28026e66b52bSEric W. Biederman }
280328a6d671SEric W. Biederman 
2804c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
280528a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
280628a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
280728a6d671SEric W. Biederman 	.setattr	= proc_setattr,
280828a6d671SEric W. Biederman };
280928a6d671SEric W. Biederman 
281000977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
281128a6d671SEric W. Biederman 	.read		= generic_read_dir,
281228a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
281328a6d671SEric W. Biederman };
2814