xref: /openbmc/linux/fs/proc/base.c (revision 85863e475e59afb027b0113290e3796ee6020b7d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/proc/base.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992 Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  proc base directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
91da177e4SLinus Torvalds  *  Instead of using magical inumbers to determine the kind of object
101da177e4SLinus Torvalds  *  we allocate and fill in-core inodes upon lookup. They don't even
111da177e4SLinus Torvalds  *  go into icache. We cache the reference to task_struct upon lookup too.
121da177e4SLinus Torvalds  *  Eventually it should become a filesystem in its own. We don't use the
131da177e4SLinus Torvalds  *  rest of procfs anymore.
14e070ad49SMauricio Lin  *
15e070ad49SMauricio Lin  *
16e070ad49SMauricio Lin  *  Changelog:
17e070ad49SMauricio Lin  *  17-Jan-2005
18e070ad49SMauricio Lin  *  Allan Bezerra
19e070ad49SMauricio Lin  *  Bruna Moreira <bruna.moreira@indt.org.br>
20e070ad49SMauricio Lin  *  Edjard Mota <edjard.mota@indt.org.br>
21e070ad49SMauricio Lin  *  Ilias Biris <ilias.biris@indt.org.br>
22e070ad49SMauricio Lin  *  Mauricio Lin <mauricio.lin@indt.org.br>
23e070ad49SMauricio Lin  *
24e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25e070ad49SMauricio Lin  *
26e070ad49SMauricio Lin  *  A new process specific entry (smaps) included in /proc. It shows the
27e070ad49SMauricio Lin  *  size of rss for each memory area. The maps entry lacks information
28e070ad49SMauricio Lin  *  about physical memory size (rss) for each mapped file, i.e.,
29e070ad49SMauricio Lin  *  rss information for executables and library files.
30e070ad49SMauricio Lin  *  This additional information is useful for any tools that need to know
31e070ad49SMauricio Lin  *  about physical memory consumption for a process specific library.
32e070ad49SMauricio Lin  *
33e070ad49SMauricio Lin  *  Changelog:
34e070ad49SMauricio Lin  *  21-Feb-2005
35e070ad49SMauricio Lin  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36e070ad49SMauricio Lin  *  Pud inclusion in the page table walking.
37e070ad49SMauricio Lin  *
38e070ad49SMauricio Lin  *  ChangeLog:
39e070ad49SMauricio Lin  *  10-Mar-2005
40e070ad49SMauricio Lin  *  10LE Instituto Nokia de Tecnologia - INdT:
41e070ad49SMauricio Lin  *  A better way to walks through the page table as suggested by Hugh Dickins.
42e070ad49SMauricio Lin  *
43e070ad49SMauricio Lin  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44e070ad49SMauricio Lin  *  Smaps information related to shared, private, clean and dirty pages.
45e070ad49SMauricio Lin  *
46e070ad49SMauricio Lin  *  Paul Mundt <paul.mundt@nokia.com>:
47e070ad49SMauricio Lin  *  Overall revision about smaps.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <asm/uaccess.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <linux/errno.h>
531da177e4SLinus Torvalds #include <linux/time.h>
541da177e4SLinus Torvalds #include <linux/proc_fs.h>
551da177e4SLinus Torvalds #include <linux/stat.h>
561da177e4SLinus Torvalds #include <linux/init.h>
5716f7e0feSRandy Dunlap #include <linux/capability.h>
581da177e4SLinus Torvalds #include <linux/file.h>
591da177e4SLinus Torvalds #include <linux/string.h>
601da177e4SLinus Torvalds #include <linux/seq_file.h>
611da177e4SLinus Torvalds #include <linux/namei.h>
626b3286edSKirill Korotaev #include <linux/mnt_namespace.h>
631da177e4SLinus Torvalds #include <linux/mm.h>
64b835996fSDipankar Sarma #include <linux/rcupdate.h>
651da177e4SLinus Torvalds #include <linux/kallsyms.h>
66d85f50d5SNeil Horman #include <linux/resource.h>
675096add8SKees Cook #include <linux/module.h>
681da177e4SLinus Torvalds #include <linux/mount.h>
691da177e4SLinus Torvalds #include <linux/security.h>
701da177e4SLinus Torvalds #include <linux/ptrace.h>
71a424316cSPaul Menage #include <linux/cgroup.h>
721da177e4SLinus Torvalds #include <linux/cpuset.h>
731da177e4SLinus Torvalds #include <linux/audit.h>
745addc5ddSAl Viro #include <linux/poll.h>
751651e14eSSerge E. Hallyn #include <linux/nsproxy.h>
768ac773b4SAlexey Dobriyan #include <linux/oom.h>
773cb4a0bbSKawai, Hidehiro #include <linux/elf.h>
7860347f67SPavel Emelyanov #include <linux/pid_namespace.h>
791da177e4SLinus Torvalds #include "internal.h"
801da177e4SLinus Torvalds 
810f2fe20fSEric W. Biederman /* NOTE:
820f2fe20fSEric W. Biederman  *	Implementing inode permission operations in /proc is almost
830f2fe20fSEric W. Biederman  *	certainly an error.  Permission checks need to happen during
840f2fe20fSEric W. Biederman  *	each system call not at open time.  The reason is that most of
850f2fe20fSEric W. Biederman  *	what we wish to check for permissions in /proc varies at runtime.
860f2fe20fSEric W. Biederman  *
870f2fe20fSEric W. Biederman  *	The classic example of a problem is opening file descriptors
880f2fe20fSEric W. Biederman  *	in /proc for a task before it execs a suid executable.
890f2fe20fSEric W. Biederman  */
900f2fe20fSEric W. Biederman 
911da177e4SLinus Torvalds struct pid_entry {
921da177e4SLinus Torvalds 	char *name;
93c5141e6dSEric Dumazet 	int len;
941da177e4SLinus Torvalds 	mode_t mode;
95c5ef1c42SArjan van de Ven 	const struct inode_operations *iop;
9600977a59SArjan van de Ven 	const struct file_operations *fop;
9720cdc894SEric W. Biederman 	union proc_op op;
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
10061a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) {			\
10120cdc894SEric W. Biederman 	.name = (NAME),					\
102c5141e6dSEric Dumazet 	.len  = sizeof(NAME) - 1,			\
10320cdc894SEric W. Biederman 	.mode = MODE,					\
10420cdc894SEric W. Biederman 	.iop  = IOP,					\
10520cdc894SEric W. Biederman 	.fop  = FOP,					\
10620cdc894SEric W. Biederman 	.op   = OP,					\
10720cdc894SEric W. Biederman }
10820cdc894SEric W. Biederman 
10961a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE)							\
11061a28784SEric W. Biederman 	NOD(NAME, (S_IFDIR|(MODE)),						\
11120cdc894SEric W. Biederman 		&proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations,	\
11220cdc894SEric W. Biederman 		{} )
11361a28784SEric W. Biederman #define LNK(NAME, OTYPE)					\
11461a28784SEric W. Biederman 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
11520cdc894SEric W. Biederman 		&proc_pid_link_inode_operations, NULL,		\
11620cdc894SEric W. Biederman 		{ .proc_get_link = &proc_##OTYPE##_link } )
11761a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE)				\
11861a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), NULL,		\
11920cdc894SEric W. Biederman 		&proc_##OTYPE##_operations, {})
12061a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE)				\
12161a28784SEric W. Biederman 	NOD(NAME, (S_IFREG|(MODE)), 			\
12220cdc894SEric W. Biederman 		NULL, &proc_info_file_operations,	\
12320cdc894SEric W. Biederman 		{ .proc_read = &proc_##OTYPE } )
1241da177e4SLinus Torvalds 
1255096add8SKees Cook int maps_protect;
1265096add8SKees Cook EXPORT_SYMBOL(maps_protect);
1275096add8SKees Cook 
1280494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	struct fs_struct *fs;
1310494f6ecSMiklos Szeredi 	task_lock(task);
1320494f6ecSMiklos Szeredi 	fs = task->fs;
1331da177e4SLinus Torvalds 	if(fs)
1341da177e4SLinus Torvalds 		atomic_inc(&fs->count);
1350494f6ecSMiklos Szeredi 	task_unlock(task);
1360494f6ecSMiklos Szeredi 	return fs;
1370494f6ecSMiklos Szeredi }
1380494f6ecSMiklos Szeredi 
13999f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk)
14099f89551SEric W. Biederman {
14199f89551SEric W. Biederman 	/* Must be called with the rcu_read_lock held */
14299f89551SEric W. Biederman 	unsigned long flags;
14399f89551SEric W. Biederman 	int count = 0;
14499f89551SEric W. Biederman 
14599f89551SEric W. Biederman 	if (lock_task_sighand(tsk, &flags)) {
14699f89551SEric W. Biederman 		count = atomic_read(&tsk->signal->count);
14799f89551SEric W. Biederman 		unlock_task_sighand(tsk, &flags);
14899f89551SEric W. Biederman 	}
14999f89551SEric W. Biederman 	return count;
15099f89551SEric W. Biederman }
15199f89551SEric W. Biederman 
1520494f6ecSMiklos Szeredi static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1530494f6ecSMiklos Szeredi {
15499f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
15599f89551SEric W. Biederman 	struct fs_struct *fs = NULL;
1560494f6ecSMiklos Szeredi 	int result = -ENOENT;
15799f89551SEric W. Biederman 
15899f89551SEric W. Biederman 	if (task) {
15999f89551SEric W. Biederman 		fs = get_fs_struct(task);
16099f89551SEric W. Biederman 		put_task_struct(task);
16199f89551SEric W. Biederman 	}
1621da177e4SLinus Torvalds 	if (fs) {
1631da177e4SLinus Torvalds 		read_lock(&fs->lock);
1641da177e4SLinus Torvalds 		*mnt = mntget(fs->pwdmnt);
1651da177e4SLinus Torvalds 		*dentry = dget(fs->pwd);
1661da177e4SLinus Torvalds 		read_unlock(&fs->lock);
1671da177e4SLinus Torvalds 		result = 0;
1681da177e4SLinus Torvalds 		put_fs_struct(fs);
1691da177e4SLinus Torvalds 	}
1701da177e4SLinus Torvalds 	return result;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1741da177e4SLinus Torvalds {
17599f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
17699f89551SEric W. Biederman 	struct fs_struct *fs = NULL;
1771da177e4SLinus Torvalds 	int result = -ENOENT;
17899f89551SEric W. Biederman 
17999f89551SEric W. Biederman 	if (task) {
18099f89551SEric W. Biederman 		fs = get_fs_struct(task);
18199f89551SEric W. Biederman 		put_task_struct(task);
18299f89551SEric W. Biederman 	}
1831da177e4SLinus Torvalds 	if (fs) {
1841da177e4SLinus Torvalds 		read_lock(&fs->lock);
1851da177e4SLinus Torvalds 		*mnt = mntget(fs->rootmnt);
1861da177e4SLinus Torvalds 		*dentry = dget(fs->root);
1871da177e4SLinus Torvalds 		read_unlock(&fs->lock);
1881da177e4SLinus Torvalds 		result = 0;
1891da177e4SLinus Torvalds 		put_fs_struct(fs);
1901da177e4SLinus Torvalds 	}
1911da177e4SLinus Torvalds 	return result;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds #define MAY_PTRACE(task) \
1951da177e4SLinus Torvalds 	(task == current || \
1961da177e4SLinus Torvalds 	(task->parent == current && \
1971da177e4SLinus Torvalds 	(task->ptrace & PT_PTRACED) && \
1986d8982d9SMatthew Wilcox 	 (task_is_stopped_or_traced(task)) && \
1991da177e4SLinus Torvalds 	 security_ptrace(current,task) == 0))
2001da177e4SLinus Torvalds 
201831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task)
202831830b5SAl Viro {
203831830b5SAl Viro 	struct mm_struct *mm = get_task_mm(task);
204831830b5SAl Viro 	if (!mm)
205831830b5SAl Viro 		return NULL;
206831830b5SAl Viro 	down_read(&mm->mmap_sem);
207831830b5SAl Viro 	task_lock(task);
208831830b5SAl Viro 	if (task->mm != mm)
209831830b5SAl Viro 		goto out;
210831830b5SAl Viro 	if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
211831830b5SAl Viro 		goto out;
212831830b5SAl Viro 	task_unlock(task);
213831830b5SAl Viro 	return mm;
214831830b5SAl Viro out:
215831830b5SAl Viro 	task_unlock(task);
216831830b5SAl Viro 	up_read(&mm->mmap_sem);
217831830b5SAl Viro 	mmput(mm);
218831830b5SAl Viro 	return NULL;
219831830b5SAl Viro }
220831830b5SAl Viro 
2211da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	int res = 0;
2241da177e4SLinus Torvalds 	unsigned int len;
2251da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2261da177e4SLinus Torvalds 	if (!mm)
2271da177e4SLinus Torvalds 		goto out;
2281da177e4SLinus Torvalds 	if (!mm->arg_end)
2291da177e4SLinus Torvalds 		goto out_mm;	/* Shh! No looking before we're done */
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds  	len = mm->arg_end - mm->arg_start;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	if (len > PAGE_SIZE)
2341da177e4SLinus Torvalds 		len = PAGE_SIZE;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	// If the nul at the end of args has been overwritten, then
2391da177e4SLinus Torvalds 	// assume application is using setproctitle(3).
2401da177e4SLinus Torvalds 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
2411da177e4SLinus Torvalds 		len = strnlen(buffer, res);
2421da177e4SLinus Torvalds 		if (len < res) {
2431da177e4SLinus Torvalds 		    res = len;
2441da177e4SLinus Torvalds 		} else {
2451da177e4SLinus Torvalds 			len = mm->env_end - mm->env_start;
2461da177e4SLinus Torvalds 			if (len > PAGE_SIZE - res)
2471da177e4SLinus Torvalds 				len = PAGE_SIZE - res;
2481da177e4SLinus Torvalds 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
2491da177e4SLinus Torvalds 			res = strnlen(buffer, res);
2501da177e4SLinus Torvalds 		}
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds out_mm:
2531da177e4SLinus Torvalds 	mmput(mm);
2541da177e4SLinus Torvalds out:
2551da177e4SLinus Torvalds 	return res;
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	int res = 0;
2611da177e4SLinus Torvalds 	struct mm_struct *mm = get_task_mm(task);
2621da177e4SLinus Torvalds 	if (mm) {
2631da177e4SLinus Torvalds 		unsigned int nwords = 0;
2641da177e4SLinus Torvalds 		do
2651da177e4SLinus Torvalds 			nwords += 2;
2661da177e4SLinus Torvalds 		while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
2671da177e4SLinus Torvalds 		res = nwords * sizeof(mm->saved_auxv[0]);
2681da177e4SLinus Torvalds 		if (res > PAGE_SIZE)
2691da177e4SLinus Torvalds 			res = PAGE_SIZE;
2701da177e4SLinus Torvalds 		memcpy(buffer, mm->saved_auxv, res);
2711da177e4SLinus Torvalds 		mmput(mm);
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 	return res;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS
2781da177e4SLinus Torvalds /*
2791da177e4SLinus Torvalds  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
2801da177e4SLinus Torvalds  * Returns the resolved symbol.  If that fails, simply return the address.
2811da177e4SLinus Torvalds  */
2821da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer)
2831da177e4SLinus Torvalds {
284ffb45122SAlexey Dobriyan 	unsigned long wchan;
2859281aceaSTejun Heo 	char symname[KSYM_NAME_LEN];
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	wchan = get_wchan(task);
2881da177e4SLinus Torvalds 
2899d65cb4aSAlexey Dobriyan 	if (lookup_symbol_name(wchan, symname) < 0)
2901da177e4SLinus Torvalds 		return sprintf(buffer, "%lu", wchan);
2919d65cb4aSAlexey Dobriyan 	else
2929d65cb4aSAlexey Dobriyan 		return sprintf(buffer, "%s", symname);
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS
2971da177e4SLinus Torvalds /*
2981da177e4SLinus Torvalds  * Provides /proc/PID/schedstat
2991da177e4SLinus Torvalds  */
3001da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer)
3011da177e4SLinus Torvalds {
302172ba844SBalbir Singh 	return sprintf(buffer, "%llu %llu %lu\n",
3031da177e4SLinus Torvalds 			task->sched_info.cpu_time,
3041da177e4SLinus Torvalds 			task->sched_info.run_delay,
3052d72376bSIngo Molnar 			task->sched_info.pcount);
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds #endif
3081da177e4SLinus Torvalds 
3099745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
3109745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v)
3119745512cSArjan van de Ven {
3129745512cSArjan van de Ven 	int i;
3139745512cSArjan van de Ven 	struct task_struct *task = m->private;
3149745512cSArjan van de Ven 	seq_puts(m, "Latency Top version : v0.1\n");
3159745512cSArjan van de Ven 
3169745512cSArjan van de Ven 	for (i = 0; i < 32; i++) {
3179745512cSArjan van de Ven 		if (task->latency_record[i].backtrace[0]) {
3189745512cSArjan van de Ven 			int q;
3199745512cSArjan van de Ven 			seq_printf(m, "%i %li %li ",
3209745512cSArjan van de Ven 				task->latency_record[i].count,
3219745512cSArjan van de Ven 				task->latency_record[i].time,
3229745512cSArjan van de Ven 				task->latency_record[i].max);
3239745512cSArjan van de Ven 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
3249745512cSArjan van de Ven 				char sym[KSYM_NAME_LEN];
3259745512cSArjan van de Ven 				char *c;
3269745512cSArjan van de Ven 				if (!task->latency_record[i].backtrace[q])
3279745512cSArjan van de Ven 					break;
3289745512cSArjan van de Ven 				if (task->latency_record[i].backtrace[q] == ULONG_MAX)
3299745512cSArjan van de Ven 					break;
3309745512cSArjan van de Ven 				sprint_symbol(sym, task->latency_record[i].backtrace[q]);
3319745512cSArjan van de Ven 				c = strchr(sym, '+');
3329745512cSArjan van de Ven 				if (c)
3339745512cSArjan van de Ven 					*c = 0;
3349745512cSArjan van de Ven 				seq_printf(m, "%s ", sym);
3359745512cSArjan van de Ven 			}
3369745512cSArjan van de Ven 			seq_printf(m, "\n");
3379745512cSArjan van de Ven 		}
3389745512cSArjan van de Ven 
3399745512cSArjan van de Ven 	}
3409745512cSArjan van de Ven 	return 0;
3419745512cSArjan van de Ven }
3429745512cSArjan van de Ven 
3439745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file)
3449745512cSArjan van de Ven {
3459745512cSArjan van de Ven 	int ret;
3469745512cSArjan van de Ven 	struct seq_file *m;
3479745512cSArjan van de Ven 	struct task_struct *task = get_proc_task(inode);
3489745512cSArjan van de Ven 
3499745512cSArjan van de Ven 	ret = single_open(file, lstats_show_proc, NULL);
3509745512cSArjan van de Ven 	if (!ret) {
3519745512cSArjan van de Ven 		m = file->private_data;
3529745512cSArjan van de Ven 		m->private = task;
3539745512cSArjan van de Ven 	}
3549745512cSArjan van de Ven 	return ret;
3559745512cSArjan van de Ven }
3569745512cSArjan van de Ven 
3579745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf,
3589745512cSArjan van de Ven 			    size_t count, loff_t *offs)
3599745512cSArjan van de Ven {
3609745512cSArjan van de Ven 	struct seq_file *m;
3619745512cSArjan van de Ven 	struct task_struct *task;
3629745512cSArjan van de Ven 
3639745512cSArjan van de Ven 	m = file->private_data;
3649745512cSArjan van de Ven 	task = m->private;
3659745512cSArjan van de Ven 	clear_all_latency_tracing(task);
3669745512cSArjan van de Ven 
3679745512cSArjan van de Ven 	return count;
3689745512cSArjan van de Ven }
3699745512cSArjan van de Ven 
3709745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = {
3719745512cSArjan van de Ven 	.open		= lstats_open,
3729745512cSArjan van de Ven 	.read		= seq_read,
3739745512cSArjan van de Ven 	.write		= lstats_write,
3749745512cSArjan van de Ven 	.llseek		= seq_lseek,
3759745512cSArjan van de Ven 	.release	= single_release,
3769745512cSArjan van de Ven };
3779745512cSArjan van de Ven 
3789745512cSArjan van de Ven #endif
3799745512cSArjan van de Ven 
3801da177e4SLinus Torvalds /* The badness from the OOM killer */
3811da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime);
3821da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	unsigned long points;
3851da177e4SLinus Torvalds 	struct timespec uptime;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	do_posix_clock_monotonic_gettime(&uptime);
38819c5d45aSAlexey Dobriyan 	read_lock(&tasklist_lock);
3891da177e4SLinus Torvalds 	points = badness(task, uptime.tv_sec);
39019c5d45aSAlexey Dobriyan 	read_unlock(&tasklist_lock);
3911da177e4SLinus Torvalds 	return sprintf(buffer, "%lu\n", points);
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
394d85f50d5SNeil Horman struct limit_names {
395d85f50d5SNeil Horman 	char *name;
396d85f50d5SNeil Horman 	char *unit;
397d85f50d5SNeil Horman };
398d85f50d5SNeil Horman 
399d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = {
400d85f50d5SNeil Horman 	[RLIMIT_CPU] = {"Max cpu time", "ms"},
401d85f50d5SNeil Horman 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
402d85f50d5SNeil Horman 	[RLIMIT_DATA] = {"Max data size", "bytes"},
403d85f50d5SNeil Horman 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
404d85f50d5SNeil Horman 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
405d85f50d5SNeil Horman 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
406d85f50d5SNeil Horman 	[RLIMIT_NPROC] = {"Max processes", "processes"},
407d85f50d5SNeil Horman 	[RLIMIT_NOFILE] = {"Max open files", "files"},
408d85f50d5SNeil Horman 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
409d85f50d5SNeil Horman 	[RLIMIT_AS] = {"Max address space", "bytes"},
410d85f50d5SNeil Horman 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
411d85f50d5SNeil Horman 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
412d85f50d5SNeil Horman 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
413d85f50d5SNeil Horman 	[RLIMIT_NICE] = {"Max nice priority", NULL},
414d85f50d5SNeil Horman 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
415d85f50d5SNeil Horman };
416d85f50d5SNeil Horman 
417d85f50d5SNeil Horman /* Display limits for a process */
418d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer)
419d85f50d5SNeil Horman {
420d85f50d5SNeil Horman 	unsigned int i;
421d85f50d5SNeil Horman 	int count = 0;
422d85f50d5SNeil Horman 	unsigned long flags;
423d85f50d5SNeil Horman 	char *bufptr = buffer;
424d85f50d5SNeil Horman 
425d85f50d5SNeil Horman 	struct rlimit rlim[RLIM_NLIMITS];
426d85f50d5SNeil Horman 
427d85f50d5SNeil Horman 	rcu_read_lock();
428d85f50d5SNeil Horman 	if (!lock_task_sighand(task,&flags)) {
429d85f50d5SNeil Horman 		rcu_read_unlock();
430d85f50d5SNeil Horman 		return 0;
431d85f50d5SNeil Horman 	}
432d85f50d5SNeil Horman 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
433d85f50d5SNeil Horman 	unlock_task_sighand(task, &flags);
434d85f50d5SNeil Horman 	rcu_read_unlock();
435d85f50d5SNeil Horman 
436d85f50d5SNeil Horman 	/*
437d85f50d5SNeil Horman 	 * print the file header
438d85f50d5SNeil Horman 	 */
439d85f50d5SNeil Horman 	count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
440d85f50d5SNeil Horman 			"Limit", "Soft Limit", "Hard Limit", "Units");
441d85f50d5SNeil Horman 
442d85f50d5SNeil Horman 	for (i = 0; i < RLIM_NLIMITS; i++) {
443d85f50d5SNeil Horman 		if (rlim[i].rlim_cur == RLIM_INFINITY)
444d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20s ",
445d85f50d5SNeil Horman 					 lnames[i].name, "unlimited");
446d85f50d5SNeil Horman 		else
447d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-25s %-20lu ",
448d85f50d5SNeil Horman 					 lnames[i].name, rlim[i].rlim_cur);
449d85f50d5SNeil Horman 
450d85f50d5SNeil Horman 		if (rlim[i].rlim_max == RLIM_INFINITY)
451d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20s ", "unlimited");
452d85f50d5SNeil Horman 		else
453d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-20lu ",
454d85f50d5SNeil Horman 					 rlim[i].rlim_max);
455d85f50d5SNeil Horman 
456d85f50d5SNeil Horman 		if (lnames[i].unit)
457d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "%-10s\n",
458d85f50d5SNeil Horman 					 lnames[i].unit);
459d85f50d5SNeil Horman 		else
460d85f50d5SNeil Horman 			count += sprintf(&bufptr[count], "\n");
461d85f50d5SNeil Horman 	}
462d85f50d5SNeil Horman 
463d85f50d5SNeil Horman 	return count;
464d85f50d5SNeil Horman }
465d85f50d5SNeil Horman 
4661da177e4SLinus Torvalds /************************************************************************/
4671da177e4SLinus Torvalds /*                       Here the fs part begins                        */
4681da177e4SLinus Torvalds /************************************************************************/
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds /* permission checks */
471778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode)
4721da177e4SLinus Torvalds {
473778c1144SEric W. Biederman 	struct task_struct *task;
474778c1144SEric W. Biederman 	int allowed = 0;
475df26c40eSEric W. Biederman 	/* Allow access to a task's file descriptors if it is us or we
476df26c40eSEric W. Biederman 	 * may use ptrace attach to the process and find out that
477df26c40eSEric W. Biederman 	 * information.
478778c1144SEric W. Biederman 	 */
479778c1144SEric W. Biederman 	task = get_proc_task(inode);
480df26c40eSEric W. Biederman 	if (task) {
481778c1144SEric W. Biederman 		allowed = ptrace_may_attach(task);
482778c1144SEric W. Biederman 		put_task_struct(task);
483df26c40eSEric W. Biederman 	}
484778c1144SEric W. Biederman 	return allowed;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4876d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr)
4886d76fa58SLinus Torvalds {
4896d76fa58SLinus Torvalds 	int error;
4906d76fa58SLinus Torvalds 	struct inode *inode = dentry->d_inode;
4916d76fa58SLinus Torvalds 
4926d76fa58SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE)
4936d76fa58SLinus Torvalds 		return -EPERM;
4946d76fa58SLinus Torvalds 
4956d76fa58SLinus Torvalds 	error = inode_change_ok(inode, attr);
4966d76fa58SLinus Torvalds 	if (!error)
4976d76fa58SLinus Torvalds 		error = inode_setattr(inode, attr);
4986d76fa58SLinus Torvalds 	return error;
4996d76fa58SLinus Torvalds }
5006d76fa58SLinus Torvalds 
501c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = {
5026d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
5036d76fa58SLinus Torvalds };
5046d76fa58SLinus Torvalds 
5051da177e4SLinus Torvalds extern struct seq_operations mounts_op;
5065addc5ddSAl Viro struct proc_mounts {
5075addc5ddSAl Viro 	struct seq_file m;
5085addc5ddSAl Viro 	int event;
5095addc5ddSAl Viro };
5105addc5ddSAl Viro 
5111da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file)
5121da177e4SLinus Torvalds {
51399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
514cf7b708cSPavel Emelyanov 	struct nsproxy *nsp;
5156b3286edSKirill Korotaev 	struct mnt_namespace *ns = NULL;
5165addc5ddSAl Viro 	struct proc_mounts *p;
5175addc5ddSAl Viro 	int ret = -EINVAL;
5185addc5ddSAl Viro 
51999f89551SEric W. Biederman 	if (task) {
520cf7b708cSPavel Emelyanov 		rcu_read_lock();
521cf7b708cSPavel Emelyanov 		nsp = task_nsproxy(task);
522cf7b708cSPavel Emelyanov 		if (nsp) {
523cf7b708cSPavel Emelyanov 			ns = nsp->mnt_ns;
5246b3286edSKirill Korotaev 			if (ns)
5256b3286edSKirill Korotaev 				get_mnt_ns(ns);
526863c4702SAlexey Dobriyan 		}
527cf7b708cSPavel Emelyanov 		rcu_read_unlock();
528cf7b708cSPavel Emelyanov 
52999f89551SEric W. Biederman 		put_task_struct(task);
53099f89551SEric W. Biederman 	}
5311da177e4SLinus Torvalds 
5326b3286edSKirill Korotaev 	if (ns) {
5335addc5ddSAl Viro 		ret = -ENOMEM;
5345addc5ddSAl Viro 		p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
5355addc5ddSAl Viro 		if (p) {
5365addc5ddSAl Viro 			file->private_data = &p->m;
5375addc5ddSAl Viro 			ret = seq_open(file, &mounts_op);
5385addc5ddSAl Viro 			if (!ret) {
5396b3286edSKirill Korotaev 				p->m.private = ns;
5406b3286edSKirill Korotaev 				p->event = ns->event;
5415addc5ddSAl Viro 				return 0;
5421da177e4SLinus Torvalds 			}
5435addc5ddSAl Viro 			kfree(p);
5445addc5ddSAl Viro 		}
5456b3286edSKirill Korotaev 		put_mnt_ns(ns);
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds 	return ret;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	struct seq_file *m = file->private_data;
5536b3286edSKirill Korotaev 	struct mnt_namespace *ns = m->private;
5546b3286edSKirill Korotaev 	put_mnt_ns(ns);
5551da177e4SLinus Torvalds 	return seq_release(inode, file);
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5585addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait)
5595addc5ddSAl Viro {
5605addc5ddSAl Viro 	struct proc_mounts *p = file->private_data;
5616b3286edSKirill Korotaev 	struct mnt_namespace *ns = p->m.private;
5625addc5ddSAl Viro 	unsigned res = 0;
5635addc5ddSAl Viro 
5645addc5ddSAl Viro 	poll_wait(file, &ns->poll, wait);
5655addc5ddSAl Viro 
5665addc5ddSAl Viro 	spin_lock(&vfsmount_lock);
5675addc5ddSAl Viro 	if (p->event != ns->event) {
5685addc5ddSAl Viro 		p->event = ns->event;
5695addc5ddSAl Viro 		res = POLLERR;
5705addc5ddSAl Viro 	}
5715addc5ddSAl Viro 	spin_unlock(&vfsmount_lock);
5725addc5ddSAl Viro 
5735addc5ddSAl Viro 	return res;
5745addc5ddSAl Viro }
5755addc5ddSAl Viro 
57600977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = {
5771da177e4SLinus Torvalds 	.open		= mounts_open,
5781da177e4SLinus Torvalds 	.read		= seq_read,
5791da177e4SLinus Torvalds 	.llseek		= seq_lseek,
5801da177e4SLinus Torvalds 	.release	= mounts_release,
5815addc5ddSAl Viro 	.poll		= mounts_poll,
5821da177e4SLinus Torvalds };
5831da177e4SLinus Torvalds 
584b4629fe2SChuck Lever extern struct seq_operations mountstats_op;
585b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file)
586b4629fe2SChuck Lever {
587b4629fe2SChuck Lever 	int ret = seq_open(file, &mountstats_op);
588b4629fe2SChuck Lever 
589b4629fe2SChuck Lever 	if (!ret) {
590b4629fe2SChuck Lever 		struct seq_file *m = file->private_data;
591cf7b708cSPavel Emelyanov 		struct nsproxy *nsp;
5926b3286edSKirill Korotaev 		struct mnt_namespace *mnt_ns = NULL;
59399f89551SEric W. Biederman 		struct task_struct *task = get_proc_task(inode);
59499f89551SEric W. Biederman 
59599f89551SEric W. Biederman 		if (task) {
596cf7b708cSPavel Emelyanov 			rcu_read_lock();
597cf7b708cSPavel Emelyanov 			nsp = task_nsproxy(task);
598cf7b708cSPavel Emelyanov 			if (nsp) {
599cf7b708cSPavel Emelyanov 				mnt_ns = nsp->mnt_ns;
6006b3286edSKirill Korotaev 				if (mnt_ns)
6016b3286edSKirill Korotaev 					get_mnt_ns(mnt_ns);
602cf7b708cSPavel Emelyanov 			}
603cf7b708cSPavel Emelyanov 			rcu_read_unlock();
604cf7b708cSPavel Emelyanov 
60599f89551SEric W. Biederman 			put_task_struct(task);
60699f89551SEric W. Biederman 		}
607b4629fe2SChuck Lever 
6086b3286edSKirill Korotaev 		if (mnt_ns)
6096b3286edSKirill Korotaev 			m->private = mnt_ns;
610b4629fe2SChuck Lever 		else {
611b4629fe2SChuck Lever 			seq_release(inode, file);
612b4629fe2SChuck Lever 			ret = -EINVAL;
613b4629fe2SChuck Lever 		}
614b4629fe2SChuck Lever 	}
615b4629fe2SChuck Lever 	return ret;
616b4629fe2SChuck Lever }
617b4629fe2SChuck Lever 
61800977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = {
619b4629fe2SChuck Lever 	.open		= mountstats_open,
620b4629fe2SChuck Lever 	.read		= seq_read,
621b4629fe2SChuck Lever 	.llseek		= seq_lseek,
622b4629fe2SChuck Lever 	.release	= mounts_release,
623b4629fe2SChuck Lever };
624b4629fe2SChuck Lever 
6251da177e4SLinus Torvalds #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf,
6281da177e4SLinus Torvalds 			  size_t count, loff_t *ppos)
6291da177e4SLinus Torvalds {
6302fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
6311da177e4SLinus Torvalds 	unsigned long page;
6321da177e4SLinus Torvalds 	ssize_t length;
63399f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
63499f89551SEric W. Biederman 
63599f89551SEric W. Biederman 	length = -ESRCH;
63699f89551SEric W. Biederman 	if (!task)
63799f89551SEric W. Biederman 		goto out_no_task;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	if (count > PROC_BLOCK_SIZE)
6401da177e4SLinus Torvalds 		count = PROC_BLOCK_SIZE;
64199f89551SEric W. Biederman 
64299f89551SEric W. Biederman 	length = -ENOMEM;
643e12ba74dSMel Gorman 	if (!(page = __get_free_page(GFP_TEMPORARY)))
64499f89551SEric W. Biederman 		goto out;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	if (length >= 0)
6491da177e4SLinus Torvalds 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
6501da177e4SLinus Torvalds 	free_page(page);
65199f89551SEric W. Biederman out:
65299f89551SEric W. Biederman 	put_task_struct(task);
65399f89551SEric W. Biederman out_no_task:
6541da177e4SLinus Torvalds 	return length;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
65700977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = {
6581da177e4SLinus Torvalds 	.read		= proc_info_read,
6591da177e4SLinus Torvalds };
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file)
6621da177e4SLinus Torvalds {
6631da177e4SLinus Torvalds 	file->private_data = (void*)((long)current->self_exec_id);
6641da177e4SLinus Torvalds 	return 0;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf,
6681da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
6691da177e4SLinus Torvalds {
6702fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
6711da177e4SLinus Torvalds 	char *page;
6721da177e4SLinus Torvalds 	unsigned long src = *ppos;
6731da177e4SLinus Torvalds 	int ret = -ESRCH;
6741da177e4SLinus Torvalds 	struct mm_struct *mm;
6751da177e4SLinus Torvalds 
67699f89551SEric W. Biederman 	if (!task)
67799f89551SEric W. Biederman 		goto out_no_task;
67899f89551SEric W. Biederman 
679ab8d11beSMiklos Szeredi 	if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
6801da177e4SLinus Torvalds 		goto out;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	ret = -ENOMEM;
683e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
6841da177e4SLinus Torvalds 	if (!page)
6851da177e4SLinus Torvalds 		goto out;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	ret = 0;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	mm = get_task_mm(task);
6901da177e4SLinus Torvalds 	if (!mm)
6911da177e4SLinus Torvalds 		goto out_free;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	ret = -EIO;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	if (file->private_data != (void*)((long)current->self_exec_id))
6961da177e4SLinus Torvalds 		goto out_put;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	ret = 0;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	while (count > 0) {
7011da177e4SLinus Torvalds 		int this_len, retval;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
7041da177e4SLinus Torvalds 		retval = access_process_vm(task, src, page, this_len, 0);
705ab8d11beSMiklos Szeredi 		if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
7061da177e4SLinus Torvalds 			if (!ret)
7071da177e4SLinus Torvalds 				ret = -EIO;
7081da177e4SLinus Torvalds 			break;
7091da177e4SLinus Torvalds 		}
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 		if (copy_to_user(buf, page, retval)) {
7121da177e4SLinus Torvalds 			ret = -EFAULT;
7131da177e4SLinus Torvalds 			break;
7141da177e4SLinus Torvalds 		}
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 		ret += retval;
7171da177e4SLinus Torvalds 		src += retval;
7181da177e4SLinus Torvalds 		buf += retval;
7191da177e4SLinus Torvalds 		count -= retval;
7201da177e4SLinus Torvalds 	}
7211da177e4SLinus Torvalds 	*ppos = src;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds out_put:
7241da177e4SLinus Torvalds 	mmput(mm);
7251da177e4SLinus Torvalds out_free:
7261da177e4SLinus Torvalds 	free_page((unsigned long) page);
7271da177e4SLinus Torvalds out:
72899f89551SEric W. Biederman 	put_task_struct(task);
72999f89551SEric W. Biederman out_no_task:
7301da177e4SLinus Torvalds 	return ret;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds #define mem_write NULL
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds #ifndef mem_write
7361da177e4SLinus Torvalds /* This is a security hazard */
73763967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf,
7381da177e4SLinus Torvalds 			 size_t count, loff_t *ppos)
7391da177e4SLinus Torvalds {
740f7ca54f4SFrederik Deweerdt 	int copied;
7411da177e4SLinus Torvalds 	char *page;
7422fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
7431da177e4SLinus Torvalds 	unsigned long dst = *ppos;
7441da177e4SLinus Torvalds 
74599f89551SEric W. Biederman 	copied = -ESRCH;
74699f89551SEric W. Biederman 	if (!task)
74799f89551SEric W. Biederman 		goto out_no_task;
7481da177e4SLinus Torvalds 
74999f89551SEric W. Biederman 	if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
75099f89551SEric W. Biederman 		goto out;
75199f89551SEric W. Biederman 
75299f89551SEric W. Biederman 	copied = -ENOMEM;
753e12ba74dSMel Gorman 	page = (char *)__get_free_page(GFP_TEMPORARY);
7541da177e4SLinus Torvalds 	if (!page)
75599f89551SEric W. Biederman 		goto out;
7561da177e4SLinus Torvalds 
757f7ca54f4SFrederik Deweerdt 	copied = 0;
7581da177e4SLinus Torvalds 	while (count > 0) {
7591da177e4SLinus Torvalds 		int this_len, retval;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
7621da177e4SLinus Torvalds 		if (copy_from_user(page, buf, this_len)) {
7631da177e4SLinus Torvalds 			copied = -EFAULT;
7641da177e4SLinus Torvalds 			break;
7651da177e4SLinus Torvalds 		}
7661da177e4SLinus Torvalds 		retval = access_process_vm(task, dst, page, this_len, 1);
7671da177e4SLinus Torvalds 		if (!retval) {
7681da177e4SLinus Torvalds 			if (!copied)
7691da177e4SLinus Torvalds 				copied = -EIO;
7701da177e4SLinus Torvalds 			break;
7711da177e4SLinus Torvalds 		}
7721da177e4SLinus Torvalds 		copied += retval;
7731da177e4SLinus Torvalds 		buf += retval;
7741da177e4SLinus Torvalds 		dst += retval;
7751da177e4SLinus Torvalds 		count -= retval;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 	*ppos = dst;
7781da177e4SLinus Torvalds 	free_page((unsigned long) page);
77999f89551SEric W. Biederman out:
78099f89551SEric W. Biederman 	put_task_struct(task);
78199f89551SEric W. Biederman out_no_task:
7821da177e4SLinus Torvalds 	return copied;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds #endif
7851da177e4SLinus Torvalds 
786*85863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig)
7871da177e4SLinus Torvalds {
7881da177e4SLinus Torvalds 	switch (orig) {
7891da177e4SLinus Torvalds 	case 0:
7901da177e4SLinus Torvalds 		file->f_pos = offset;
7911da177e4SLinus Torvalds 		break;
7921da177e4SLinus Torvalds 	case 1:
7931da177e4SLinus Torvalds 		file->f_pos += offset;
7941da177e4SLinus Torvalds 		break;
7951da177e4SLinus Torvalds 	default:
7961da177e4SLinus Torvalds 		return -EINVAL;
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 	force_successful_syscall_return();
7991da177e4SLinus Torvalds 	return file->f_pos;
8001da177e4SLinus Torvalds }
8011da177e4SLinus Torvalds 
80200977a59SArjan van de Ven static const struct file_operations proc_mem_operations = {
8031da177e4SLinus Torvalds 	.llseek		= mem_lseek,
8041da177e4SLinus Torvalds 	.read		= mem_read,
8051da177e4SLinus Torvalds 	.write		= mem_write,
8061da177e4SLinus Torvalds 	.open		= mem_open,
8071da177e4SLinus Torvalds };
8081da177e4SLinus Torvalds 
809315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf,
810315e28c8SJames Pearson 			size_t count, loff_t *ppos)
811315e28c8SJames Pearson {
812315e28c8SJames Pearson 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
813315e28c8SJames Pearson 	char *page;
814315e28c8SJames Pearson 	unsigned long src = *ppos;
815315e28c8SJames Pearson 	int ret = -ESRCH;
816315e28c8SJames Pearson 	struct mm_struct *mm;
817315e28c8SJames Pearson 
818315e28c8SJames Pearson 	if (!task)
819315e28c8SJames Pearson 		goto out_no_task;
820315e28c8SJames Pearson 
821315e28c8SJames Pearson 	if (!ptrace_may_attach(task))
822315e28c8SJames Pearson 		goto out;
823315e28c8SJames Pearson 
824315e28c8SJames Pearson 	ret = -ENOMEM;
825315e28c8SJames Pearson 	page = (char *)__get_free_page(GFP_TEMPORARY);
826315e28c8SJames Pearson 	if (!page)
827315e28c8SJames Pearson 		goto out;
828315e28c8SJames Pearson 
829315e28c8SJames Pearson 	ret = 0;
830315e28c8SJames Pearson 
831315e28c8SJames Pearson 	mm = get_task_mm(task);
832315e28c8SJames Pearson 	if (!mm)
833315e28c8SJames Pearson 		goto out_free;
834315e28c8SJames Pearson 
835315e28c8SJames Pearson 	while (count > 0) {
836315e28c8SJames Pearson 		int this_len, retval, max_len;
837315e28c8SJames Pearson 
838315e28c8SJames Pearson 		this_len = mm->env_end - (mm->env_start + src);
839315e28c8SJames Pearson 
840315e28c8SJames Pearson 		if (this_len <= 0)
841315e28c8SJames Pearson 			break;
842315e28c8SJames Pearson 
843315e28c8SJames Pearson 		max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
844315e28c8SJames Pearson 		this_len = (this_len > max_len) ? max_len : this_len;
845315e28c8SJames Pearson 
846315e28c8SJames Pearson 		retval = access_process_vm(task, (mm->env_start + src),
847315e28c8SJames Pearson 			page, this_len, 0);
848315e28c8SJames Pearson 
849315e28c8SJames Pearson 		if (retval <= 0) {
850315e28c8SJames Pearson 			ret = retval;
851315e28c8SJames Pearson 			break;
852315e28c8SJames Pearson 		}
853315e28c8SJames Pearson 
854315e28c8SJames Pearson 		if (copy_to_user(buf, page, retval)) {
855315e28c8SJames Pearson 			ret = -EFAULT;
856315e28c8SJames Pearson 			break;
857315e28c8SJames Pearson 		}
858315e28c8SJames Pearson 
859315e28c8SJames Pearson 		ret += retval;
860315e28c8SJames Pearson 		src += retval;
861315e28c8SJames Pearson 		buf += retval;
862315e28c8SJames Pearson 		count -= retval;
863315e28c8SJames Pearson 	}
864315e28c8SJames Pearson 	*ppos = src;
865315e28c8SJames Pearson 
866315e28c8SJames Pearson 	mmput(mm);
867315e28c8SJames Pearson out_free:
868315e28c8SJames Pearson 	free_page((unsigned long) page);
869315e28c8SJames Pearson out:
870315e28c8SJames Pearson 	put_task_struct(task);
871315e28c8SJames Pearson out_no_task:
872315e28c8SJames Pearson 	return ret;
873315e28c8SJames Pearson }
874315e28c8SJames Pearson 
875315e28c8SJames Pearson static const struct file_operations proc_environ_operations = {
876315e28c8SJames Pearson 	.read		= environ_read,
877315e28c8SJames Pearson };
878315e28c8SJames Pearson 
8791da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf,
8801da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
8811da177e4SLinus Torvalds {
8822fddfeefSJosef "Jeff" Sipek 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
8838578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF];
8841da177e4SLinus Torvalds 	size_t len;
88599f89551SEric W. Biederman 	int oom_adjust;
8861da177e4SLinus Torvalds 
88799f89551SEric W. Biederman 	if (!task)
88899f89551SEric W. Biederman 		return -ESRCH;
88999f89551SEric W. Biederman 	oom_adjust = task->oomkilladj;
89099f89551SEric W. Biederman 	put_task_struct(task);
89199f89551SEric W. Biederman 
8928578cea7SEric W. Biederman 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
8930c28f287SAkinobu Mita 
8940c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
8981da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
8991da177e4SLinus Torvalds {
90099f89551SEric W. Biederman 	struct task_struct *task;
9018578cea7SEric W. Biederman 	char buffer[PROC_NUMBUF], *end;
9021da177e4SLinus Torvalds 	int oom_adjust;
9031da177e4SLinus Torvalds 
9048578cea7SEric W. Biederman 	memset(buffer, 0, sizeof(buffer));
9058578cea7SEric W. Biederman 	if (count > sizeof(buffer) - 1)
9068578cea7SEric W. Biederman 		count = sizeof(buffer) - 1;
9071da177e4SLinus Torvalds 	if (copy_from_user(buffer, buf, count))
9081da177e4SLinus Torvalds 		return -EFAULT;
9091da177e4SLinus Torvalds 	oom_adjust = simple_strtol(buffer, &end, 0);
9108ac773b4SAlexey Dobriyan 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
9118ac773b4SAlexey Dobriyan 	     oom_adjust != OOM_DISABLE)
9121da177e4SLinus Torvalds 		return -EINVAL;
9131da177e4SLinus Torvalds 	if (*end == '\n')
9141da177e4SLinus Torvalds 		end++;
9152fddfeefSJosef "Jeff" Sipek 	task = get_proc_task(file->f_path.dentry->d_inode);
91699f89551SEric W. Biederman 	if (!task)
91799f89551SEric W. Biederman 		return -ESRCH;
9188fb4fc68SGuillem Jover 	if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
9198fb4fc68SGuillem Jover 		put_task_struct(task);
9208fb4fc68SGuillem Jover 		return -EACCES;
9218fb4fc68SGuillem Jover 	}
9221da177e4SLinus Torvalds 	task->oomkilladj = oom_adjust;
92399f89551SEric W. Biederman 	put_task_struct(task);
9241da177e4SLinus Torvalds 	if (end - buffer == 0)
9251da177e4SLinus Torvalds 		return -EIO;
9261da177e4SLinus Torvalds 	return end - buffer;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
92900977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = {
9301da177e4SLinus Torvalds 	.read		= oom_adjust_read,
9311da177e4SLinus Torvalds 	.write		= oom_adjust_write,
9321da177e4SLinus Torvalds };
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
9351da177e4SLinus Torvalds #define TMPBUFLEN 21
9361da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
9371da177e4SLinus Torvalds 				  size_t count, loff_t *ppos)
9381da177e4SLinus Torvalds {
9392fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
94099f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
9411da177e4SLinus Torvalds 	ssize_t length;
9421da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
9431da177e4SLinus Torvalds 
94499f89551SEric W. Biederman 	if (!task)
94599f89551SEric W. Biederman 		return -ESRCH;
9461da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
9470c11b942SAl Viro 				audit_get_loginuid(task));
94899f89551SEric W. Biederman 	put_task_struct(task);
9491da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
9531da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
9541da177e4SLinus Torvalds {
9552fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
9561da177e4SLinus Torvalds 	char *page, *tmp;
9571da177e4SLinus Torvalds 	ssize_t length;
9581da177e4SLinus Torvalds 	uid_t loginuid;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	if (!capable(CAP_AUDIT_CONTROL))
9611da177e4SLinus Torvalds 		return -EPERM;
9621da177e4SLinus Torvalds 
96313b41b09SEric W. Biederman 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
9641da177e4SLinus Torvalds 		return -EPERM;
9651da177e4SLinus Torvalds 
966e0182909SAl Viro 	if (count >= PAGE_SIZE)
967e0182909SAl Viro 		count = PAGE_SIZE - 1;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	if (*ppos != 0) {
9701da177e4SLinus Torvalds 		/* No partial writes. */
9711da177e4SLinus Torvalds 		return -EINVAL;
9721da177e4SLinus Torvalds 	}
973e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
9741da177e4SLinus Torvalds 	if (!page)
9751da177e4SLinus Torvalds 		return -ENOMEM;
9761da177e4SLinus Torvalds 	length = -EFAULT;
9771da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
9781da177e4SLinus Torvalds 		goto out_free_page;
9791da177e4SLinus Torvalds 
980e0182909SAl Viro 	page[count] = '\0';
9811da177e4SLinus Torvalds 	loginuid = simple_strtoul(page, &tmp, 10);
9821da177e4SLinus Torvalds 	if (tmp == page) {
9831da177e4SLinus Torvalds 		length = -EINVAL;
9841da177e4SLinus Torvalds 		goto out_free_page;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	}
98799f89551SEric W. Biederman 	length = audit_set_loginuid(current, loginuid);
9881da177e4SLinus Torvalds 	if (likely(length == 0))
9891da177e4SLinus Torvalds 		length = count;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds out_free_page:
9921da177e4SLinus Torvalds 	free_page((unsigned long) page);
9931da177e4SLinus Torvalds 	return length;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
99600977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = {
9971da177e4SLinus Torvalds 	.read		= proc_loginuid_read,
9981da177e4SLinus Torvalds 	.write		= proc_loginuid_write,
9991da177e4SLinus Torvalds };
10001da177e4SLinus Torvalds #endif
10011da177e4SLinus Torvalds 
1002f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
1003f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1004f4f154fdSAkinobu Mita 				      size_t count, loff_t *ppos)
1005f4f154fdSAkinobu Mita {
1006f4f154fdSAkinobu Mita 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1007f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF];
1008f4f154fdSAkinobu Mita 	size_t len;
1009f4f154fdSAkinobu Mita 	int make_it_fail;
1010f4f154fdSAkinobu Mita 
1011f4f154fdSAkinobu Mita 	if (!task)
1012f4f154fdSAkinobu Mita 		return -ESRCH;
1013f4f154fdSAkinobu Mita 	make_it_fail = task->make_it_fail;
1014f4f154fdSAkinobu Mita 	put_task_struct(task);
1015f4f154fdSAkinobu Mita 
1016f4f154fdSAkinobu Mita 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
10170c28f287SAkinobu Mita 
10180c28f287SAkinobu Mita 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1019f4f154fdSAkinobu Mita }
1020f4f154fdSAkinobu Mita 
1021f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file,
1022f4f154fdSAkinobu Mita 			const char __user * buf, size_t count, loff_t *ppos)
1023f4f154fdSAkinobu Mita {
1024f4f154fdSAkinobu Mita 	struct task_struct *task;
1025f4f154fdSAkinobu Mita 	char buffer[PROC_NUMBUF], *end;
1026f4f154fdSAkinobu Mita 	int make_it_fail;
1027f4f154fdSAkinobu Mita 
1028f4f154fdSAkinobu Mita 	if (!capable(CAP_SYS_RESOURCE))
1029f4f154fdSAkinobu Mita 		return -EPERM;
1030f4f154fdSAkinobu Mita 	memset(buffer, 0, sizeof(buffer));
1031f4f154fdSAkinobu Mita 	if (count > sizeof(buffer) - 1)
1032f4f154fdSAkinobu Mita 		count = sizeof(buffer) - 1;
1033f4f154fdSAkinobu Mita 	if (copy_from_user(buffer, buf, count))
1034f4f154fdSAkinobu Mita 		return -EFAULT;
1035f4f154fdSAkinobu Mita 	make_it_fail = simple_strtol(buffer, &end, 0);
1036f4f154fdSAkinobu Mita 	if (*end == '\n')
1037f4f154fdSAkinobu Mita 		end++;
1038f4f154fdSAkinobu Mita 	task = get_proc_task(file->f_dentry->d_inode);
1039f4f154fdSAkinobu Mita 	if (!task)
1040f4f154fdSAkinobu Mita 		return -ESRCH;
1041f4f154fdSAkinobu Mita 	task->make_it_fail = make_it_fail;
1042f4f154fdSAkinobu Mita 	put_task_struct(task);
1043f4f154fdSAkinobu Mita 	if (end - buffer == 0)
1044f4f154fdSAkinobu Mita 		return -EIO;
1045f4f154fdSAkinobu Mita 	return end - buffer;
1046f4f154fdSAkinobu Mita }
1047f4f154fdSAkinobu Mita 
104800977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = {
1049f4f154fdSAkinobu Mita 	.read		= proc_fault_inject_read,
1050f4f154fdSAkinobu Mita 	.write		= proc_fault_inject_write,
1051f4f154fdSAkinobu Mita };
1052f4f154fdSAkinobu Mita #endif
1053f4f154fdSAkinobu Mita 
10549745512cSArjan van de Ven 
105543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
105643ae34cbSIngo Molnar /*
105743ae34cbSIngo Molnar  * Print out various scheduling related per-task fields:
105843ae34cbSIngo Molnar  */
105943ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v)
106043ae34cbSIngo Molnar {
106143ae34cbSIngo Molnar 	struct inode *inode = m->private;
106243ae34cbSIngo Molnar 	struct task_struct *p;
106343ae34cbSIngo Molnar 
106443ae34cbSIngo Molnar 	WARN_ON(!inode);
106543ae34cbSIngo Molnar 
106643ae34cbSIngo Molnar 	p = get_proc_task(inode);
106743ae34cbSIngo Molnar 	if (!p)
106843ae34cbSIngo Molnar 		return -ESRCH;
106943ae34cbSIngo Molnar 	proc_sched_show_task(p, m);
107043ae34cbSIngo Molnar 
107143ae34cbSIngo Molnar 	put_task_struct(p);
107243ae34cbSIngo Molnar 
107343ae34cbSIngo Molnar 	return 0;
107443ae34cbSIngo Molnar }
107543ae34cbSIngo Molnar 
107643ae34cbSIngo Molnar static ssize_t
107743ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf,
107843ae34cbSIngo Molnar 	    size_t count, loff_t *offset)
107943ae34cbSIngo Molnar {
108043ae34cbSIngo Molnar 	struct inode *inode = file->f_path.dentry->d_inode;
108143ae34cbSIngo Molnar 	struct task_struct *p;
108243ae34cbSIngo Molnar 
108343ae34cbSIngo Molnar 	WARN_ON(!inode);
108443ae34cbSIngo Molnar 
108543ae34cbSIngo Molnar 	p = get_proc_task(inode);
108643ae34cbSIngo Molnar 	if (!p)
108743ae34cbSIngo Molnar 		return -ESRCH;
108843ae34cbSIngo Molnar 	proc_sched_set_task(p);
108943ae34cbSIngo Molnar 
109043ae34cbSIngo Molnar 	put_task_struct(p);
109143ae34cbSIngo Molnar 
109243ae34cbSIngo Molnar 	return count;
109343ae34cbSIngo Molnar }
109443ae34cbSIngo Molnar 
109543ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp)
109643ae34cbSIngo Molnar {
109743ae34cbSIngo Molnar 	int ret;
109843ae34cbSIngo Molnar 
109943ae34cbSIngo Molnar 	ret = single_open(filp, sched_show, NULL);
110043ae34cbSIngo Molnar 	if (!ret) {
110143ae34cbSIngo Molnar 		struct seq_file *m = filp->private_data;
110243ae34cbSIngo Molnar 
110343ae34cbSIngo Molnar 		m->private = inode;
110443ae34cbSIngo Molnar 	}
110543ae34cbSIngo Molnar 	return ret;
110643ae34cbSIngo Molnar }
110743ae34cbSIngo Molnar 
110843ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = {
110943ae34cbSIngo Molnar 	.open		= sched_open,
111043ae34cbSIngo Molnar 	.read		= seq_read,
111143ae34cbSIngo Molnar 	.write		= sched_write,
111243ae34cbSIngo Molnar 	.llseek		= seq_lseek,
11135ea473a1SAlexey Dobriyan 	.release	= single_release,
111443ae34cbSIngo Molnar };
111543ae34cbSIngo Molnar 
111643ae34cbSIngo Molnar #endif
111743ae34cbSIngo Molnar 
1118008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
11191da177e4SLinus Torvalds {
11201da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
11211da177e4SLinus Torvalds 	int error = -EACCES;
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 	/* We don't need a base pointer in the /proc filesystem */
11241da177e4SLinus Torvalds 	path_release(nd);
11251da177e4SLinus Torvalds 
1126778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1127778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
11281da177e4SLinus Torvalds 		goto out;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
11311da177e4SLinus Torvalds 	nd->last_type = LAST_BIND;
11321da177e4SLinus Torvalds out:
1133008b150aSAl Viro 	return ERR_PTR(error);
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
11371da177e4SLinus Torvalds 			    char __user *buffer, int buflen)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	struct inode * inode;
1140e12ba74dSMel Gorman 	char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1141e12ba74dSMel Gorman 	char *path;
11421da177e4SLinus Torvalds 	int len;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	if (!tmp)
11451da177e4SLinus Torvalds 		return -ENOMEM;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	inode = dentry->d_inode;
11481da177e4SLinus Torvalds 	path = d_path(dentry, mnt, tmp, PAGE_SIZE);
11491da177e4SLinus Torvalds 	len = PTR_ERR(path);
11501da177e4SLinus Torvalds 	if (IS_ERR(path))
11511da177e4SLinus Torvalds 		goto out;
11521da177e4SLinus Torvalds 	len = tmp + PAGE_SIZE - 1 - path;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	if (len > buflen)
11551da177e4SLinus Torvalds 		len = buflen;
11561da177e4SLinus Torvalds 	if (copy_to_user(buffer, path, len))
11571da177e4SLinus Torvalds 		len = -EFAULT;
11581da177e4SLinus Torvalds  out:
11591da177e4SLinus Torvalds 	free_page((unsigned long)tmp);
11601da177e4SLinus Torvalds 	return len;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
11641da177e4SLinus Torvalds {
11651da177e4SLinus Torvalds 	int error = -EACCES;
11661da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
11671da177e4SLinus Torvalds 	struct dentry *de;
11681da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
11691da177e4SLinus Torvalds 
1170778c1144SEric W. Biederman 	/* Are we allowed to snoop on the tasks file descriptors? */
1171778c1144SEric W. Biederman 	if (!proc_fd_access_allowed(inode))
11721da177e4SLinus Torvalds 		goto out;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
11751da177e4SLinus Torvalds 	if (error)
11761da177e4SLinus Torvalds 		goto out;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	error = do_proc_readlink(de, mnt, buffer, buflen);
11791da177e4SLinus Torvalds 	dput(de);
11801da177e4SLinus Torvalds 	mntput(mnt);
11811da177e4SLinus Torvalds out:
11821da177e4SLinus Torvalds 	return error;
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds 
1185c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = {
11861da177e4SLinus Torvalds 	.readlink	= proc_pid_readlink,
11876d76fa58SLinus Torvalds 	.follow_link	= proc_pid_follow_link,
11886d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
11891da177e4SLinus Torvalds };
11901da177e4SLinus Torvalds 
119128a6d671SEric W. Biederman 
119228a6d671SEric W. Biederman /* building an inode */
119328a6d671SEric W. Biederman 
119428a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task)
119528a6d671SEric W. Biederman {
119628a6d671SEric W. Biederman 	int dumpable = 0;
119728a6d671SEric W. Biederman 	struct mm_struct *mm;
119828a6d671SEric W. Biederman 
119928a6d671SEric W. Biederman 	task_lock(task);
120028a6d671SEric W. Biederman 	mm = task->mm;
120128a6d671SEric W. Biederman 	if (mm)
12026c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(mm);
120328a6d671SEric W. Biederman 	task_unlock(task);
120428a6d671SEric W. Biederman 	if(dumpable == 1)
120528a6d671SEric W. Biederman 		return 1;
120628a6d671SEric W. Biederman 	return 0;
120728a6d671SEric W. Biederman }
120828a6d671SEric W. Biederman 
120928a6d671SEric W. Biederman 
121061a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
121128a6d671SEric W. Biederman {
121228a6d671SEric W. Biederman 	struct inode * inode;
121328a6d671SEric W. Biederman 	struct proc_inode *ei;
121428a6d671SEric W. Biederman 
121528a6d671SEric W. Biederman 	/* We need a new inode */
121628a6d671SEric W. Biederman 
121728a6d671SEric W. Biederman 	inode = new_inode(sb);
121828a6d671SEric W. Biederman 	if (!inode)
121928a6d671SEric W. Biederman 		goto out;
122028a6d671SEric W. Biederman 
122128a6d671SEric W. Biederman 	/* Common stuff */
122228a6d671SEric W. Biederman 	ei = PROC_I(inode);
122328a6d671SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
122428a6d671SEric W. Biederman 	inode->i_op = &proc_def_inode_operations;
122528a6d671SEric W. Biederman 
122628a6d671SEric W. Biederman 	/*
122728a6d671SEric W. Biederman 	 * grab the reference to task.
122828a6d671SEric W. Biederman 	 */
12291a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
123028a6d671SEric W. Biederman 	if (!ei->pid)
123128a6d671SEric W. Biederman 		goto out_unlock;
123228a6d671SEric W. Biederman 
123328a6d671SEric W. Biederman 	inode->i_uid = 0;
123428a6d671SEric W. Biederman 	inode->i_gid = 0;
123528a6d671SEric W. Biederman 	if (task_dumpable(task)) {
123628a6d671SEric W. Biederman 		inode->i_uid = task->euid;
123728a6d671SEric W. Biederman 		inode->i_gid = task->egid;
123828a6d671SEric W. Biederman 	}
123928a6d671SEric W. Biederman 	security_task_to_inode(task, inode);
124028a6d671SEric W. Biederman 
124128a6d671SEric W. Biederman out:
124228a6d671SEric W. Biederman 	return inode;
124328a6d671SEric W. Biederman 
124428a6d671SEric W. Biederman out_unlock:
124528a6d671SEric W. Biederman 	iput(inode);
124628a6d671SEric W. Biederman 	return NULL;
124728a6d671SEric W. Biederman }
124828a6d671SEric W. Biederman 
124928a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
125028a6d671SEric W. Biederman {
125128a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
125228a6d671SEric W. Biederman 	struct task_struct *task;
125328a6d671SEric W. Biederman 	generic_fillattr(inode, stat);
125428a6d671SEric W. Biederman 
125528a6d671SEric W. Biederman 	rcu_read_lock();
125628a6d671SEric W. Biederman 	stat->uid = 0;
125728a6d671SEric W. Biederman 	stat->gid = 0;
125828a6d671SEric W. Biederman 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
125928a6d671SEric W. Biederman 	if (task) {
126028a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
126128a6d671SEric W. Biederman 		    task_dumpable(task)) {
126228a6d671SEric W. Biederman 			stat->uid = task->euid;
126328a6d671SEric W. Biederman 			stat->gid = task->egid;
126428a6d671SEric W. Biederman 		}
126528a6d671SEric W. Biederman 	}
126628a6d671SEric W. Biederman 	rcu_read_unlock();
126728a6d671SEric W. Biederman 	return 0;
126828a6d671SEric W. Biederman }
126928a6d671SEric W. Biederman 
127028a6d671SEric W. Biederman /* dentry stuff */
127128a6d671SEric W. Biederman 
127228a6d671SEric W. Biederman /*
127328a6d671SEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
127428a6d671SEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
127528a6d671SEric W. Biederman  * due to the way we treat inodes.
127628a6d671SEric W. Biederman  *
127728a6d671SEric W. Biederman  * Rewrite the inode's ownerships here because the owning task may have
127828a6d671SEric W. Biederman  * performed a setuid(), etc.
127928a6d671SEric W. Biederman  *
128028a6d671SEric W. Biederman  * Before the /proc/pid/status file was created the only way to read
128128a6d671SEric W. Biederman  * the effective uid of a /process was to stat /proc/pid.  Reading
128228a6d671SEric W. Biederman  * /proc/pid/status is slow enough that procps and other packages
128328a6d671SEric W. Biederman  * kept stating /proc/pid.  To keep the rules in /proc simple I have
128428a6d671SEric W. Biederman  * made this apply to all per process world readable and executable
128528a6d671SEric W. Biederman  * directories.
128628a6d671SEric W. Biederman  */
128728a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
128828a6d671SEric W. Biederman {
128928a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
129028a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
129128a6d671SEric W. Biederman 	if (task) {
129228a6d671SEric W. Biederman 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
129328a6d671SEric W. Biederman 		    task_dumpable(task)) {
129428a6d671SEric W. Biederman 			inode->i_uid = task->euid;
129528a6d671SEric W. Biederman 			inode->i_gid = task->egid;
129628a6d671SEric W. Biederman 		} else {
129728a6d671SEric W. Biederman 			inode->i_uid = 0;
129828a6d671SEric W. Biederman 			inode->i_gid = 0;
129928a6d671SEric W. Biederman 		}
130028a6d671SEric W. Biederman 		inode->i_mode &= ~(S_ISUID | S_ISGID);
130128a6d671SEric W. Biederman 		security_task_to_inode(task, inode);
130228a6d671SEric W. Biederman 		put_task_struct(task);
130328a6d671SEric W. Biederman 		return 1;
130428a6d671SEric W. Biederman 	}
130528a6d671SEric W. Biederman 	d_drop(dentry);
130628a6d671SEric W. Biederman 	return 0;
130728a6d671SEric W. Biederman }
130828a6d671SEric W. Biederman 
130928a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry)
131028a6d671SEric W. Biederman {
131128a6d671SEric W. Biederman 	/* Is the task we represent dead?
131228a6d671SEric W. Biederman 	 * If so, then don't put the dentry on the lru list,
131328a6d671SEric W. Biederman 	 * kill it immediately.
131428a6d671SEric W. Biederman 	 */
131528a6d671SEric W. Biederman 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
131628a6d671SEric W. Biederman }
131728a6d671SEric W. Biederman 
131828a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations =
131928a6d671SEric W. Biederman {
132028a6d671SEric W. Biederman 	.d_revalidate	= pid_revalidate,
132128a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
132228a6d671SEric W. Biederman };
132328a6d671SEric W. Biederman 
132428a6d671SEric W. Biederman /* Lookups */
132528a6d671SEric W. Biederman 
1326c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1327c5141e6dSEric Dumazet 				struct task_struct *, const void *);
132861a28784SEric W. Biederman 
13291c0d04c9SEric W. Biederman /*
13301c0d04c9SEric W. Biederman  * Fill a directory entry.
13311c0d04c9SEric W. Biederman  *
13321c0d04c9SEric W. Biederman  * If possible create the dcache entry and derive our inode number and
13331c0d04c9SEric W. Biederman  * file type from dcache entry.
13341c0d04c9SEric W. Biederman  *
13351c0d04c9SEric W. Biederman  * Since all of the proc inode numbers are dynamically generated, the inode
13361c0d04c9SEric W. Biederman  * numbers do not exist until the inode is cache.  This means creating the
13371c0d04c9SEric W. Biederman  * the dcache entry in readdir is necessary to keep the inode numbers
13381c0d04c9SEric W. Biederman  * reported by readdir in sync with the inode numbers reported
13391c0d04c9SEric W. Biederman  * by stat.
13401c0d04c9SEric W. Biederman  */
134161a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
134261a28784SEric W. Biederman 	char *name, int len,
1343c5141e6dSEric Dumazet 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
134461a28784SEric W. Biederman {
13452fddfeefSJosef "Jeff" Sipek 	struct dentry *child, *dir = filp->f_path.dentry;
134661a28784SEric W. Biederman 	struct inode *inode;
134761a28784SEric W. Biederman 	struct qstr qname;
134861a28784SEric W. Biederman 	ino_t ino = 0;
134961a28784SEric W. Biederman 	unsigned type = DT_UNKNOWN;
135061a28784SEric W. Biederman 
135161a28784SEric W. Biederman 	qname.name = name;
135261a28784SEric W. Biederman 	qname.len  = len;
135361a28784SEric W. Biederman 	qname.hash = full_name_hash(name, len);
135461a28784SEric W. Biederman 
135561a28784SEric W. Biederman 	child = d_lookup(dir, &qname);
135661a28784SEric W. Biederman 	if (!child) {
135761a28784SEric W. Biederman 		struct dentry *new;
135861a28784SEric W. Biederman 		new = d_alloc(dir, &qname);
135961a28784SEric W. Biederman 		if (new) {
136061a28784SEric W. Biederman 			child = instantiate(dir->d_inode, new, task, ptr);
136161a28784SEric W. Biederman 			if (child)
136261a28784SEric W. Biederman 				dput(new);
136361a28784SEric W. Biederman 			else
136461a28784SEric W. Biederman 				child = new;
136561a28784SEric W. Biederman 		}
136661a28784SEric W. Biederman 	}
136761a28784SEric W. Biederman 	if (!child || IS_ERR(child) || !child->d_inode)
136861a28784SEric W. Biederman 		goto end_instantiate;
136961a28784SEric W. Biederman 	inode = child->d_inode;
137061a28784SEric W. Biederman 	if (inode) {
137161a28784SEric W. Biederman 		ino = inode->i_ino;
137261a28784SEric W. Biederman 		type = inode->i_mode >> 12;
137361a28784SEric W. Biederman 	}
137461a28784SEric W. Biederman 	dput(child);
137561a28784SEric W. Biederman end_instantiate:
137661a28784SEric W. Biederman 	if (!ino)
137761a28784SEric W. Biederman 		ino = find_inode_number(dir, &qname);
137861a28784SEric W. Biederman 	if (!ino)
137961a28784SEric W. Biederman 		ino = 1;
138061a28784SEric W. Biederman 	return filldir(dirent, name, len, filp->f_pos, ino, type);
138161a28784SEric W. Biederman }
138261a28784SEric W. Biederman 
138328a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry)
138428a6d671SEric W. Biederman {
138528a6d671SEric W. Biederman 	const char *name = dentry->d_name.name;
138628a6d671SEric W. Biederman 	int len = dentry->d_name.len;
138728a6d671SEric W. Biederman 	unsigned n = 0;
138828a6d671SEric W. Biederman 
138928a6d671SEric W. Biederman 	if (len > 1 && *name == '0')
139028a6d671SEric W. Biederman 		goto out;
139128a6d671SEric W. Biederman 	while (len-- > 0) {
139228a6d671SEric W. Biederman 		unsigned c = *name++ - '0';
139328a6d671SEric W. Biederman 		if (c > 9)
139428a6d671SEric W. Biederman 			goto out;
139528a6d671SEric W. Biederman 		if (n >= (~0U-9)/10)
139628a6d671SEric W. Biederman 			goto out;
139728a6d671SEric W. Biederman 		n *= 10;
139828a6d671SEric W. Biederman 		n += c;
139928a6d671SEric W. Biederman 	}
140028a6d671SEric W. Biederman 	return n;
140128a6d671SEric W. Biederman out:
140228a6d671SEric W. Biederman 	return ~0U;
140328a6d671SEric W. Biederman }
140428a6d671SEric W. Biederman 
140527932742SMiklos Szeredi #define PROC_FDINFO_MAX 64
140627932742SMiklos Szeredi 
140727932742SMiklos Szeredi static int proc_fd_info(struct inode *inode, struct dentry **dentry,
140827932742SMiklos Szeredi 			struct vfsmount **mnt, char *info)
140928a6d671SEric W. Biederman {
141028a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
141128a6d671SEric W. Biederman 	struct files_struct *files = NULL;
141228a6d671SEric W. Biederman 	struct file *file;
141328a6d671SEric W. Biederman 	int fd = proc_fd(inode);
141428a6d671SEric W. Biederman 
141528a6d671SEric W. Biederman 	if (task) {
141628a6d671SEric W. Biederman 		files = get_files_struct(task);
141728a6d671SEric W. Biederman 		put_task_struct(task);
141828a6d671SEric W. Biederman 	}
141928a6d671SEric W. Biederman 	if (files) {
142028a6d671SEric W. Biederman 		/*
142128a6d671SEric W. Biederman 		 * We are not taking a ref to the file structure, so we must
142228a6d671SEric W. Biederman 		 * hold ->file_lock.
142328a6d671SEric W. Biederman 		 */
142428a6d671SEric W. Biederman 		spin_lock(&files->file_lock);
142528a6d671SEric W. Biederman 		file = fcheck_files(files, fd);
142628a6d671SEric W. Biederman 		if (file) {
142727932742SMiklos Szeredi 			if (mnt)
14282fddfeefSJosef "Jeff" Sipek 				*mnt = mntget(file->f_path.mnt);
142927932742SMiklos Szeredi 			if (dentry)
14302fddfeefSJosef "Jeff" Sipek 				*dentry = dget(file->f_path.dentry);
143127932742SMiklos Szeredi 			if (info)
143227932742SMiklos Szeredi 				snprintf(info, PROC_FDINFO_MAX,
143327932742SMiklos Szeredi 					 "pos:\t%lli\n"
143427932742SMiklos Szeredi 					 "flags:\t0%o\n",
143527932742SMiklos Szeredi 					 (long long) file->f_pos,
143627932742SMiklos Szeredi 					 file->f_flags);
143728a6d671SEric W. Biederman 			spin_unlock(&files->file_lock);
143828a6d671SEric W. Biederman 			put_files_struct(files);
143928a6d671SEric W. Biederman 			return 0;
144028a6d671SEric W. Biederman 		}
144128a6d671SEric W. Biederman 		spin_unlock(&files->file_lock);
144228a6d671SEric W. Biederman 		put_files_struct(files);
144328a6d671SEric W. Biederman 	}
144428a6d671SEric W. Biederman 	return -ENOENT;
144528a6d671SEric W. Biederman }
144628a6d671SEric W. Biederman 
144727932742SMiklos Szeredi static int proc_fd_link(struct inode *inode, struct dentry **dentry,
144827932742SMiklos Szeredi 			struct vfsmount **mnt)
144927932742SMiklos Szeredi {
145027932742SMiklos Szeredi 	return proc_fd_info(inode, dentry, mnt, NULL);
145127932742SMiklos Szeredi }
145227932742SMiklos Szeredi 
145328a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
145428a6d671SEric W. Biederman {
145528a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
145628a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
145728a6d671SEric W. Biederman 	int fd = proc_fd(inode);
145828a6d671SEric W. Biederman 	struct files_struct *files;
145928a6d671SEric W. Biederman 
146028a6d671SEric W. Biederman 	if (task) {
146128a6d671SEric W. Biederman 		files = get_files_struct(task);
146228a6d671SEric W. Biederman 		if (files) {
146328a6d671SEric W. Biederman 			rcu_read_lock();
146428a6d671SEric W. Biederman 			if (fcheck_files(files, fd)) {
146528a6d671SEric W. Biederman 				rcu_read_unlock();
146628a6d671SEric W. Biederman 				put_files_struct(files);
146728a6d671SEric W. Biederman 				if (task_dumpable(task)) {
146828a6d671SEric W. Biederman 					inode->i_uid = task->euid;
146928a6d671SEric W. Biederman 					inode->i_gid = task->egid;
147028a6d671SEric W. Biederman 				} else {
147128a6d671SEric W. Biederman 					inode->i_uid = 0;
147228a6d671SEric W. Biederman 					inode->i_gid = 0;
147328a6d671SEric W. Biederman 				}
147428a6d671SEric W. Biederman 				inode->i_mode &= ~(S_ISUID | S_ISGID);
147528a6d671SEric W. Biederman 				security_task_to_inode(task, inode);
147628a6d671SEric W. Biederman 				put_task_struct(task);
147728a6d671SEric W. Biederman 				return 1;
147828a6d671SEric W. Biederman 			}
147928a6d671SEric W. Biederman 			rcu_read_unlock();
148028a6d671SEric W. Biederman 			put_files_struct(files);
148128a6d671SEric W. Biederman 		}
148228a6d671SEric W. Biederman 		put_task_struct(task);
148328a6d671SEric W. Biederman 	}
148428a6d671SEric W. Biederman 	d_drop(dentry);
148528a6d671SEric W. Biederman 	return 0;
148628a6d671SEric W. Biederman }
148728a6d671SEric W. Biederman 
148828a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations =
148928a6d671SEric W. Biederman {
149028a6d671SEric W. Biederman 	.d_revalidate	= tid_fd_revalidate,
149128a6d671SEric W. Biederman 	.d_delete	= pid_delete_dentry,
149228a6d671SEric W. Biederman };
149328a6d671SEric W. Biederman 
1494444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir,
1495c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
149628a6d671SEric W. Biederman {
1497c5141e6dSEric Dumazet 	unsigned fd = *(const unsigned *)ptr;
149828a6d671SEric W. Biederman 	struct file *file;
149928a6d671SEric W. Biederman 	struct files_struct *files;
150028a6d671SEric W. Biederman  	struct inode *inode;
150128a6d671SEric W. Biederman  	struct proc_inode *ei;
1502444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
150328a6d671SEric W. Biederman 
150461a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
150528a6d671SEric W. Biederman 	if (!inode)
150628a6d671SEric W. Biederman 		goto out;
150728a6d671SEric W. Biederman 	ei = PROC_I(inode);
150828a6d671SEric W. Biederman 	ei->fd = fd;
150928a6d671SEric W. Biederman 	files = get_files_struct(task);
151028a6d671SEric W. Biederman 	if (!files)
1511444ceed8SEric W. Biederman 		goto out_iput;
151228a6d671SEric W. Biederman 	inode->i_mode = S_IFLNK;
151328a6d671SEric W. Biederman 
151428a6d671SEric W. Biederman 	/*
151528a6d671SEric W. Biederman 	 * We are not taking a ref to the file structure, so we must
151628a6d671SEric W. Biederman 	 * hold ->file_lock.
151728a6d671SEric W. Biederman 	 */
151828a6d671SEric W. Biederman 	spin_lock(&files->file_lock);
151928a6d671SEric W. Biederman 	file = fcheck_files(files, fd);
152028a6d671SEric W. Biederman 	if (!file)
1521444ceed8SEric W. Biederman 		goto out_unlock;
152228a6d671SEric W. Biederman 	if (file->f_mode & 1)
152328a6d671SEric W. Biederman 		inode->i_mode |= S_IRUSR | S_IXUSR;
152428a6d671SEric W. Biederman 	if (file->f_mode & 2)
152528a6d671SEric W. Biederman 		inode->i_mode |= S_IWUSR | S_IXUSR;
152628a6d671SEric W. Biederman 	spin_unlock(&files->file_lock);
152728a6d671SEric W. Biederman 	put_files_struct(files);
1528444ceed8SEric W. Biederman 
152928a6d671SEric W. Biederman 	inode->i_op = &proc_pid_link_inode_operations;
153028a6d671SEric W. Biederman 	inode->i_size = 64;
153128a6d671SEric W. Biederman 	ei->op.proc_get_link = proc_fd_link;
153228a6d671SEric W. Biederman 	dentry->d_op = &tid_fd_dentry_operations;
153328a6d671SEric W. Biederman 	d_add(dentry, inode);
153428a6d671SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
153528a6d671SEric W. Biederman 	if (tid_fd_revalidate(dentry, NULL))
1536444ceed8SEric W. Biederman 		error = NULL;
1537444ceed8SEric W. Biederman 
1538444ceed8SEric W. Biederman  out:
1539444ceed8SEric W. Biederman 	return error;
1540444ceed8SEric W. Biederman out_unlock:
1541444ceed8SEric W. Biederman 	spin_unlock(&files->file_lock);
1542444ceed8SEric W. Biederman 	put_files_struct(files);
1543444ceed8SEric W. Biederman out_iput:
1544444ceed8SEric W. Biederman 	iput(inode);
1545444ceed8SEric W. Biederman 	goto out;
1546444ceed8SEric W. Biederman }
1547444ceed8SEric W. Biederman 
154827932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir,
154927932742SMiklos Szeredi 					   struct dentry *dentry,
155027932742SMiklos Szeredi 					   instantiate_t instantiate)
1551444ceed8SEric W. Biederman {
1552444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1553444ceed8SEric W. Biederman 	unsigned fd = name_to_int(dentry);
1554444ceed8SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
1555444ceed8SEric W. Biederman 
1556444ceed8SEric W. Biederman 	if (!task)
1557444ceed8SEric W. Biederman 		goto out_no_task;
1558444ceed8SEric W. Biederman 	if (fd == ~0U)
1559444ceed8SEric W. Biederman 		goto out;
1560444ceed8SEric W. Biederman 
156127932742SMiklos Szeredi 	result = instantiate(dir, dentry, task, &fd);
156228a6d671SEric W. Biederman out:
156328a6d671SEric W. Biederman 	put_task_struct(task);
156428a6d671SEric W. Biederman out_no_task:
156528a6d671SEric W. Biederman 	return result;
156628a6d671SEric W. Biederman }
156728a6d671SEric W. Biederman 
156827932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent,
156927932742SMiklos Szeredi 			      filldir_t filldir, instantiate_t instantiate)
15701da177e4SLinus Torvalds {
15712fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
15725634708bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
157399f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
1574457c2510SPavel Emelyanov 	unsigned int fd, ino;
15751da177e4SLinus Torvalds 	int retval;
15761da177e4SLinus Torvalds 	struct files_struct * files;
1577badf1662SDipankar Sarma 	struct fdtable *fdt;
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	retval = -ENOENT;
158099f89551SEric W. Biederman 	if (!p)
158199f89551SEric W. Biederman 		goto out_no_task;
15821da177e4SLinus Torvalds 	retval = 0;
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	fd = filp->f_pos;
15851da177e4SLinus Torvalds 	switch (fd) {
15861da177e4SLinus Torvalds 		case 0:
15871da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
15881da177e4SLinus Torvalds 				goto out;
15891da177e4SLinus Torvalds 			filp->f_pos++;
15901da177e4SLinus Torvalds 		case 1:
15915634708bSEric W. Biederman 			ino = parent_ino(dentry);
15921da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
15931da177e4SLinus Torvalds 				goto out;
15941da177e4SLinus Torvalds 			filp->f_pos++;
15951da177e4SLinus Torvalds 		default:
15961da177e4SLinus Torvalds 			files = get_files_struct(p);
15971da177e4SLinus Torvalds 			if (!files)
15981da177e4SLinus Torvalds 				goto out;
1599b835996fSDipankar Sarma 			rcu_read_lock();
1600badf1662SDipankar Sarma 			fdt = files_fdtable(files);
16011da177e4SLinus Torvalds 			for (fd = filp->f_pos-2;
1602badf1662SDipankar Sarma 			     fd < fdt->max_fds;
16031da177e4SLinus Torvalds 			     fd++, filp->f_pos++) {
160427932742SMiklos Szeredi 				char name[PROC_NUMBUF];
160527932742SMiklos Szeredi 				int len;
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 				if (!fcheck_files(files, fd))
16081da177e4SLinus Torvalds 					continue;
1609b835996fSDipankar Sarma 				rcu_read_unlock();
16101da177e4SLinus Torvalds 
161127932742SMiklos Szeredi 				len = snprintf(name, sizeof(name), "%d", fd);
161227932742SMiklos Szeredi 				if (proc_fill_cache(filp, dirent, filldir,
161327932742SMiklos Szeredi 						    name, len, instantiate,
161427932742SMiklos Szeredi 						    p, &fd) < 0) {
1615b835996fSDipankar Sarma 					rcu_read_lock();
16161da177e4SLinus Torvalds 					break;
16171da177e4SLinus Torvalds 				}
1618b835996fSDipankar Sarma 				rcu_read_lock();
16191da177e4SLinus Torvalds 			}
1620b835996fSDipankar Sarma 			rcu_read_unlock();
16211da177e4SLinus Torvalds 			put_files_struct(files);
16221da177e4SLinus Torvalds 	}
16231da177e4SLinus Torvalds out:
162499f89551SEric W. Biederman 	put_task_struct(p);
162599f89551SEric W. Biederman out_no_task:
16261da177e4SLinus Torvalds 	return retval;
16271da177e4SLinus Torvalds }
16281da177e4SLinus Torvalds 
162927932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
163027932742SMiklos Szeredi 				    struct nameidata *nd)
163127932742SMiklos Szeredi {
163227932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
163327932742SMiklos Szeredi }
163427932742SMiklos Szeredi 
163527932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
163627932742SMiklos Szeredi {
163727932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
163827932742SMiklos Szeredi }
163927932742SMiklos Szeredi 
164027932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
164127932742SMiklos Szeredi 				      size_t len, loff_t *ppos)
164227932742SMiklos Szeredi {
164327932742SMiklos Szeredi 	char tmp[PROC_FDINFO_MAX];
164427932742SMiklos Szeredi 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
164527932742SMiklos Szeredi 	if (!err)
164627932742SMiklos Szeredi 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
164727932742SMiklos Szeredi 	return err;
164827932742SMiklos Szeredi }
164927932742SMiklos Szeredi 
165027932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = {
165127932742SMiklos Szeredi 	.open		= nonseekable_open,
165227932742SMiklos Szeredi 	.read		= proc_fdinfo_read,
165327932742SMiklos Szeredi };
165427932742SMiklos Szeredi 
165500977a59SArjan van de Ven static const struct file_operations proc_fd_operations = {
16561da177e4SLinus Torvalds 	.read		= generic_read_dir,
16571da177e4SLinus Torvalds 	.readdir	= proc_readfd,
16581da177e4SLinus Torvalds };
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds /*
16618948e11fSAlexey Dobriyan  * /proc/pid/fd needs a special permission handler so that a process can still
16628948e11fSAlexey Dobriyan  * access /proc/self/fd after it has executed a setuid().
16638948e11fSAlexey Dobriyan  */
16648948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask,
16658948e11fSAlexey Dobriyan 				struct nameidata *nd)
16668948e11fSAlexey Dobriyan {
16678948e11fSAlexey Dobriyan 	int rv;
16688948e11fSAlexey Dobriyan 
16698948e11fSAlexey Dobriyan 	rv = generic_permission(inode, mask, NULL);
16708948e11fSAlexey Dobriyan 	if (rv == 0)
16718948e11fSAlexey Dobriyan 		return 0;
16728948e11fSAlexey Dobriyan 	if (task_pid(current) == proc_pid(inode))
16738948e11fSAlexey Dobriyan 		rv = 0;
16748948e11fSAlexey Dobriyan 	return rv;
16758948e11fSAlexey Dobriyan }
16768948e11fSAlexey Dobriyan 
16778948e11fSAlexey Dobriyan /*
16781da177e4SLinus Torvalds  * proc directories can do almost nothing..
16791da177e4SLinus Torvalds  */
1680c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = {
16811da177e4SLinus Torvalds 	.lookup		= proc_lookupfd,
16828948e11fSAlexey Dobriyan 	.permission	= proc_fd_permission,
16836d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
16841da177e4SLinus Torvalds };
16851da177e4SLinus Torvalds 
168627932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
168727932742SMiklos Szeredi 	struct dentry *dentry, struct task_struct *task, const void *ptr)
168827932742SMiklos Szeredi {
168927932742SMiklos Szeredi 	unsigned fd = *(unsigned *)ptr;
169027932742SMiklos Szeredi  	struct inode *inode;
169127932742SMiklos Szeredi  	struct proc_inode *ei;
169227932742SMiklos Szeredi 	struct dentry *error = ERR_PTR(-ENOENT);
169327932742SMiklos Szeredi 
169427932742SMiklos Szeredi 	inode = proc_pid_make_inode(dir->i_sb, task);
169527932742SMiklos Szeredi 	if (!inode)
169627932742SMiklos Szeredi 		goto out;
169727932742SMiklos Szeredi 	ei = PROC_I(inode);
169827932742SMiklos Szeredi 	ei->fd = fd;
169927932742SMiklos Szeredi 	inode->i_mode = S_IFREG | S_IRUSR;
170027932742SMiklos Szeredi 	inode->i_fop = &proc_fdinfo_file_operations;
170127932742SMiklos Szeredi 	dentry->d_op = &tid_fd_dentry_operations;
170227932742SMiklos Szeredi 	d_add(dentry, inode);
170327932742SMiklos Szeredi 	/* Close the race of the process dying before we return the dentry */
170427932742SMiklos Szeredi 	if (tid_fd_revalidate(dentry, NULL))
170527932742SMiklos Szeredi 		error = NULL;
170627932742SMiklos Szeredi 
170727932742SMiklos Szeredi  out:
170827932742SMiklos Szeredi 	return error;
170927932742SMiklos Szeredi }
171027932742SMiklos Szeredi 
171127932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir,
171227932742SMiklos Szeredi 					struct dentry *dentry,
171327932742SMiklos Szeredi 					struct nameidata *nd)
171427932742SMiklos Szeredi {
171527932742SMiklos Szeredi 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
171627932742SMiklos Szeredi }
171727932742SMiklos Szeredi 
171827932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
171927932742SMiklos Szeredi {
172027932742SMiklos Szeredi 	return proc_readfd_common(filp, dirent, filldir,
172127932742SMiklos Szeredi 				  proc_fdinfo_instantiate);
172227932742SMiklos Szeredi }
172327932742SMiklos Szeredi 
172427932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = {
172527932742SMiklos Szeredi 	.read		= generic_read_dir,
172627932742SMiklos Szeredi 	.readdir	= proc_readfdinfo,
172727932742SMiklos Szeredi };
172827932742SMiklos Szeredi 
172927932742SMiklos Szeredi /*
173027932742SMiklos Szeredi  * proc directories can do almost nothing..
173127932742SMiklos Szeredi  */
173227932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = {
173327932742SMiklos Szeredi 	.lookup		= proc_lookupfdinfo,
173427932742SMiklos Szeredi 	.setattr	= proc_setattr,
173527932742SMiklos Szeredi };
173627932742SMiklos Szeredi 
173727932742SMiklos Szeredi 
1738444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir,
1739c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
1740444ceed8SEric W. Biederman {
1741c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
1742444ceed8SEric W. Biederman 	struct inode *inode;
1743444ceed8SEric W. Biederman 	struct proc_inode *ei;
1744444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-EINVAL);
1745444ceed8SEric W. Biederman 
174661a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
1747444ceed8SEric W. Biederman 	if (!inode)
1748444ceed8SEric W. Biederman 		goto out;
1749444ceed8SEric W. Biederman 
1750444ceed8SEric W. Biederman 	ei = PROC_I(inode);
1751444ceed8SEric W. Biederman 	inode->i_mode = p->mode;
1752444ceed8SEric W. Biederman 	if (S_ISDIR(inode->i_mode))
1753444ceed8SEric W. Biederman 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
1754444ceed8SEric W. Biederman 	if (p->iop)
1755444ceed8SEric W. Biederman 		inode->i_op = p->iop;
1756444ceed8SEric W. Biederman 	if (p->fop)
1757444ceed8SEric W. Biederman 		inode->i_fop = p->fop;
1758444ceed8SEric W. Biederman 	ei->op = p->op;
1759444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
1760444ceed8SEric W. Biederman 	d_add(dentry, inode);
1761444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
1762444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
1763444ceed8SEric W. Biederman 		error = NULL;
1764444ceed8SEric W. Biederman out:
1765444ceed8SEric W. Biederman 	return error;
1766444ceed8SEric W. Biederman }
1767444ceed8SEric W. Biederman 
17681da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir,
17691da177e4SLinus Torvalds 					 struct dentry *dentry,
1770c5141e6dSEric Dumazet 					 const struct pid_entry *ents,
17717bcd6b0eSEric W. Biederman 					 unsigned int nents)
17721da177e4SLinus Torvalds {
17731da177e4SLinus Torvalds 	struct inode *inode;
1774cd6a3ce9SEric W. Biederman 	struct dentry *error;
177599f89551SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
1776c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
17771da177e4SLinus Torvalds 
1778cd6a3ce9SEric W. Biederman 	error = ERR_PTR(-ENOENT);
17791da177e4SLinus Torvalds 	inode = NULL;
17801da177e4SLinus Torvalds 
178199f89551SEric W. Biederman 	if (!task)
178299f89551SEric W. Biederman 		goto out_no_task;
17831da177e4SLinus Torvalds 
178420cdc894SEric W. Biederman 	/*
178520cdc894SEric W. Biederman 	 * Yes, it does not scale. And it should not. Don't add
178620cdc894SEric W. Biederman 	 * new entries into /proc/<tgid>/ without very good reasons.
178720cdc894SEric W. Biederman 	 */
17887bcd6b0eSEric W. Biederman 	last = &ents[nents - 1];
17897bcd6b0eSEric W. Biederman 	for (p = ents; p <= last; p++) {
17901da177e4SLinus Torvalds 		if (p->len != dentry->d_name.len)
17911da177e4SLinus Torvalds 			continue;
17921da177e4SLinus Torvalds 		if (!memcmp(dentry->d_name.name, p->name, p->len))
17931da177e4SLinus Torvalds 			break;
17941da177e4SLinus Torvalds 	}
17957bcd6b0eSEric W. Biederman 	if (p > last)
17961da177e4SLinus Torvalds 		goto out;
17971da177e4SLinus Torvalds 
1798444ceed8SEric W. Biederman 	error = proc_pident_instantiate(dir, dentry, task, p);
17991da177e4SLinus Torvalds out:
180099f89551SEric W. Biederman 	put_task_struct(task);
180199f89551SEric W. Biederman out_no_task:
1802cd6a3ce9SEric W. Biederman 	return error;
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds 
1805c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent,
1806c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
180761a28784SEric W. Biederman {
180861a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
180961a28784SEric W. Biederman 				proc_pident_instantiate, task, p);
181061a28784SEric W. Biederman }
181161a28784SEric W. Biederman 
181228a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp,
181328a6d671SEric W. Biederman 		void *dirent, filldir_t filldir,
1814c5141e6dSEric Dumazet 		const struct pid_entry *ents, unsigned int nents)
181528a6d671SEric W. Biederman {
181628a6d671SEric W. Biederman 	int i;
18172fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
181828a6d671SEric W. Biederman 	struct inode *inode = dentry->d_inode;
181928a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
1820c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
182128a6d671SEric W. Biederman 	ino_t ino;
182228a6d671SEric W. Biederman 	int ret;
182328a6d671SEric W. Biederman 
182428a6d671SEric W. Biederman 	ret = -ENOENT;
182528a6d671SEric W. Biederman 	if (!task)
182661a28784SEric W. Biederman 		goto out_no_task;
182728a6d671SEric W. Biederman 
182828a6d671SEric W. Biederman 	ret = 0;
182928a6d671SEric W. Biederman 	i = filp->f_pos;
183028a6d671SEric W. Biederman 	switch (i) {
183128a6d671SEric W. Biederman 	case 0:
183228a6d671SEric W. Biederman 		ino = inode->i_ino;
183328a6d671SEric W. Biederman 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
183428a6d671SEric W. Biederman 			goto out;
183528a6d671SEric W. Biederman 		i++;
183628a6d671SEric W. Biederman 		filp->f_pos++;
183728a6d671SEric W. Biederman 		/* fall through */
183828a6d671SEric W. Biederman 	case 1:
183928a6d671SEric W. Biederman 		ino = parent_ino(dentry);
184028a6d671SEric W. Biederman 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
184128a6d671SEric W. Biederman 			goto out;
184228a6d671SEric W. Biederman 		i++;
184328a6d671SEric W. Biederman 		filp->f_pos++;
184428a6d671SEric W. Biederman 		/* fall through */
184528a6d671SEric W. Biederman 	default:
184628a6d671SEric W. Biederman 		i -= 2;
184728a6d671SEric W. Biederman 		if (i >= nents) {
184828a6d671SEric W. Biederman 			ret = 1;
184928a6d671SEric W. Biederman 			goto out;
185028a6d671SEric W. Biederman 		}
185128a6d671SEric W. Biederman 		p = ents + i;
18527bcd6b0eSEric W. Biederman 		last = &ents[nents - 1];
18537bcd6b0eSEric W. Biederman 		while (p <= last) {
185461a28784SEric W. Biederman 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
185528a6d671SEric W. Biederman 				goto out;
185628a6d671SEric W. Biederman 			filp->f_pos++;
185728a6d671SEric W. Biederman 			p++;
185828a6d671SEric W. Biederman 		}
18591da177e4SLinus Torvalds 	}
18601da177e4SLinus Torvalds 
186128a6d671SEric W. Biederman 	ret = 1;
186228a6d671SEric W. Biederman out:
186361a28784SEric W. Biederman 	put_task_struct(task);
186461a28784SEric W. Biederman out_no_task:
186528a6d671SEric W. Biederman 	return ret;
18661da177e4SLinus Torvalds }
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds #ifdef CONFIG_SECURITY
186928a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
187028a6d671SEric W. Biederman 				  size_t count, loff_t *ppos)
187128a6d671SEric W. Biederman {
18722fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
187304ff9708SAl Viro 	char *p = NULL;
187428a6d671SEric W. Biederman 	ssize_t length;
187528a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
187628a6d671SEric W. Biederman 
187728a6d671SEric W. Biederman 	if (!task)
187804ff9708SAl Viro 		return -ESRCH;
187928a6d671SEric W. Biederman 
188028a6d671SEric W. Biederman 	length = security_getprocattr(task,
18812fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
188204ff9708SAl Viro 				      &p);
188328a6d671SEric W. Biederman 	put_task_struct(task);
188404ff9708SAl Viro 	if (length > 0)
188504ff9708SAl Viro 		length = simple_read_from_buffer(buf, count, ppos, p, length);
188604ff9708SAl Viro 	kfree(p);
188728a6d671SEric W. Biederman 	return length;
188828a6d671SEric W. Biederman }
188928a6d671SEric W. Biederman 
189028a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
189128a6d671SEric W. Biederman 				   size_t count, loff_t *ppos)
189228a6d671SEric W. Biederman {
18932fddfeefSJosef "Jeff" Sipek 	struct inode * inode = file->f_path.dentry->d_inode;
189428a6d671SEric W. Biederman 	char *page;
189528a6d671SEric W. Biederman 	ssize_t length;
189628a6d671SEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
189728a6d671SEric W. Biederman 
189828a6d671SEric W. Biederman 	length = -ESRCH;
189928a6d671SEric W. Biederman 	if (!task)
190028a6d671SEric W. Biederman 		goto out_no_task;
190128a6d671SEric W. Biederman 	if (count > PAGE_SIZE)
190228a6d671SEric W. Biederman 		count = PAGE_SIZE;
190328a6d671SEric W. Biederman 
190428a6d671SEric W. Biederman 	/* No partial writes. */
190528a6d671SEric W. Biederman 	length = -EINVAL;
190628a6d671SEric W. Biederman 	if (*ppos != 0)
190728a6d671SEric W. Biederman 		goto out;
190828a6d671SEric W. Biederman 
190928a6d671SEric W. Biederman 	length = -ENOMEM;
1910e12ba74dSMel Gorman 	page = (char*)__get_free_page(GFP_TEMPORARY);
191128a6d671SEric W. Biederman 	if (!page)
191228a6d671SEric W. Biederman 		goto out;
191328a6d671SEric W. Biederman 
191428a6d671SEric W. Biederman 	length = -EFAULT;
191528a6d671SEric W. Biederman 	if (copy_from_user(page, buf, count))
191628a6d671SEric W. Biederman 		goto out_free;
191728a6d671SEric W. Biederman 
191828a6d671SEric W. Biederman 	length = security_setprocattr(task,
19192fddfeefSJosef "Jeff" Sipek 				      (char*)file->f_path.dentry->d_name.name,
192028a6d671SEric W. Biederman 				      (void*)page, count);
192128a6d671SEric W. Biederman out_free:
192228a6d671SEric W. Biederman 	free_page((unsigned long) page);
192328a6d671SEric W. Biederman out:
192428a6d671SEric W. Biederman 	put_task_struct(task);
192528a6d671SEric W. Biederman out_no_task:
192628a6d671SEric W. Biederman 	return length;
192728a6d671SEric W. Biederman }
192828a6d671SEric W. Biederman 
192900977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = {
193028a6d671SEric W. Biederman 	.read		= proc_pid_attr_read,
193128a6d671SEric W. Biederman 	.write		= proc_pid_attr_write,
193228a6d671SEric W. Biederman };
193328a6d671SEric W. Biederman 
1934c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = {
193561a28784SEric W. Biederman 	REG("current",    S_IRUGO|S_IWUGO, pid_attr),
193661a28784SEric W. Biederman 	REG("prev",       S_IRUGO,	   pid_attr),
193761a28784SEric W. Biederman 	REG("exec",       S_IRUGO|S_IWUGO, pid_attr),
193861a28784SEric W. Biederman 	REG("fscreate",   S_IRUGO|S_IWUGO, pid_attr),
193961a28784SEric W. Biederman 	REG("keycreate",  S_IRUGO|S_IWUGO, pid_attr),
194061a28784SEric W. Biederman 	REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
194128a6d671SEric W. Biederman };
194228a6d671SEric W. Biederman 
194372d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp,
19441da177e4SLinus Torvalds 			     void * dirent, filldir_t filldir)
19451da177e4SLinus Torvalds {
19461da177e4SLinus Torvalds 	return proc_pident_readdir(filp,dirent,filldir,
194772d9dcfcSEric W. Biederman 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds 
195000977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = {
19511da177e4SLinus Torvalds 	.read		= generic_read_dir,
195272d9dcfcSEric W. Biederman 	.readdir	= proc_attr_dir_readdir,
19531da177e4SLinus Torvalds };
19541da177e4SLinus Torvalds 
195572d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir,
19561da177e4SLinus Torvalds 				struct dentry *dentry, struct nameidata *nd)
19571da177e4SLinus Torvalds {
19587bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
19597bcd6b0eSEric W. Biederman 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
19601da177e4SLinus Torvalds }
19611da177e4SLinus Torvalds 
1962c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = {
196372d9dcfcSEric W. Biederman 	.lookup		= proc_attr_dir_lookup,
196499f89551SEric W. Biederman 	.getattr	= pid_getattr,
19656d76fa58SLinus Torvalds 	.setattr	= proc_setattr,
19661da177e4SLinus Torvalds };
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds #endif
19691da177e4SLinus Torvalds 
19703cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
19713cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
19723cb4a0bbSKawai, Hidehiro 					 size_t count, loff_t *ppos)
19733cb4a0bbSKawai, Hidehiro {
19743cb4a0bbSKawai, Hidehiro 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
19753cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
19763cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF];
19773cb4a0bbSKawai, Hidehiro 	size_t len;
19783cb4a0bbSKawai, Hidehiro 	int ret;
19793cb4a0bbSKawai, Hidehiro 
19803cb4a0bbSKawai, Hidehiro 	if (!task)
19813cb4a0bbSKawai, Hidehiro 		return -ESRCH;
19823cb4a0bbSKawai, Hidehiro 
19833cb4a0bbSKawai, Hidehiro 	ret = 0;
19843cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
19853cb4a0bbSKawai, Hidehiro 	if (mm) {
19863cb4a0bbSKawai, Hidehiro 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
19873cb4a0bbSKawai, Hidehiro 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
19883cb4a0bbSKawai, Hidehiro 				MMF_DUMP_FILTER_SHIFT));
19893cb4a0bbSKawai, Hidehiro 		mmput(mm);
19903cb4a0bbSKawai, Hidehiro 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
19913cb4a0bbSKawai, Hidehiro 	}
19923cb4a0bbSKawai, Hidehiro 
19933cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
19943cb4a0bbSKawai, Hidehiro 
19953cb4a0bbSKawai, Hidehiro 	return ret;
19963cb4a0bbSKawai, Hidehiro }
19973cb4a0bbSKawai, Hidehiro 
19983cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file,
19993cb4a0bbSKawai, Hidehiro 					  const char __user *buf,
20003cb4a0bbSKawai, Hidehiro 					  size_t count,
20013cb4a0bbSKawai, Hidehiro 					  loff_t *ppos)
20023cb4a0bbSKawai, Hidehiro {
20033cb4a0bbSKawai, Hidehiro 	struct task_struct *task;
20043cb4a0bbSKawai, Hidehiro 	struct mm_struct *mm;
20053cb4a0bbSKawai, Hidehiro 	char buffer[PROC_NUMBUF], *end;
20063cb4a0bbSKawai, Hidehiro 	unsigned int val;
20073cb4a0bbSKawai, Hidehiro 	int ret;
20083cb4a0bbSKawai, Hidehiro 	int i;
20093cb4a0bbSKawai, Hidehiro 	unsigned long mask;
20103cb4a0bbSKawai, Hidehiro 
20113cb4a0bbSKawai, Hidehiro 	ret = -EFAULT;
20123cb4a0bbSKawai, Hidehiro 	memset(buffer, 0, sizeof(buffer));
20133cb4a0bbSKawai, Hidehiro 	if (count > sizeof(buffer) - 1)
20143cb4a0bbSKawai, Hidehiro 		count = sizeof(buffer) - 1;
20153cb4a0bbSKawai, Hidehiro 	if (copy_from_user(buffer, buf, count))
20163cb4a0bbSKawai, Hidehiro 		goto out_no_task;
20173cb4a0bbSKawai, Hidehiro 
20183cb4a0bbSKawai, Hidehiro 	ret = -EINVAL;
20193cb4a0bbSKawai, Hidehiro 	val = (unsigned int)simple_strtoul(buffer, &end, 0);
20203cb4a0bbSKawai, Hidehiro 	if (*end == '\n')
20213cb4a0bbSKawai, Hidehiro 		end++;
20223cb4a0bbSKawai, Hidehiro 	if (end - buffer == 0)
20233cb4a0bbSKawai, Hidehiro 		goto out_no_task;
20243cb4a0bbSKawai, Hidehiro 
20253cb4a0bbSKawai, Hidehiro 	ret = -ESRCH;
20263cb4a0bbSKawai, Hidehiro 	task = get_proc_task(file->f_dentry->d_inode);
20273cb4a0bbSKawai, Hidehiro 	if (!task)
20283cb4a0bbSKawai, Hidehiro 		goto out_no_task;
20293cb4a0bbSKawai, Hidehiro 
20303cb4a0bbSKawai, Hidehiro 	ret = end - buffer;
20313cb4a0bbSKawai, Hidehiro 	mm = get_task_mm(task);
20323cb4a0bbSKawai, Hidehiro 	if (!mm)
20333cb4a0bbSKawai, Hidehiro 		goto out_no_mm;
20343cb4a0bbSKawai, Hidehiro 
20353cb4a0bbSKawai, Hidehiro 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
20363cb4a0bbSKawai, Hidehiro 		if (val & mask)
20373cb4a0bbSKawai, Hidehiro 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
20383cb4a0bbSKawai, Hidehiro 		else
20393cb4a0bbSKawai, Hidehiro 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
20403cb4a0bbSKawai, Hidehiro 	}
20413cb4a0bbSKawai, Hidehiro 
20423cb4a0bbSKawai, Hidehiro 	mmput(mm);
20433cb4a0bbSKawai, Hidehiro  out_no_mm:
20443cb4a0bbSKawai, Hidehiro 	put_task_struct(task);
20453cb4a0bbSKawai, Hidehiro  out_no_task:
20463cb4a0bbSKawai, Hidehiro 	return ret;
20473cb4a0bbSKawai, Hidehiro }
20483cb4a0bbSKawai, Hidehiro 
20493cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = {
20503cb4a0bbSKawai, Hidehiro 	.read		= proc_coredump_filter_read,
20513cb4a0bbSKawai, Hidehiro 	.write		= proc_coredump_filter_write,
20523cb4a0bbSKawai, Hidehiro };
20533cb4a0bbSKawai, Hidehiro #endif
20543cb4a0bbSKawai, Hidehiro 
20551da177e4SLinus Torvalds /*
20561da177e4SLinus Torvalds  * /proc/self:
20571da177e4SLinus Torvalds  */
20581da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
20591da177e4SLinus Torvalds 			      int buflen)
20601da177e4SLinus Torvalds {
20618578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2062b488893aSPavel Emelyanov 	sprintf(tmp, "%d", task_tgid_vnr(current));
20631da177e4SLinus Torvalds 	return vfs_readlink(dentry,buffer,buflen,tmp);
20641da177e4SLinus Torvalds }
20651da177e4SLinus Torvalds 
2066008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
20671da177e4SLinus Torvalds {
20688578cea7SEric W. Biederman 	char tmp[PROC_NUMBUF];
2069b488893aSPavel Emelyanov 	sprintf(tmp, "%d", task_tgid_vnr(current));
2070008b150aSAl Viro 	return ERR_PTR(vfs_follow_link(nd,tmp));
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds 
2073c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = {
20741da177e4SLinus Torvalds 	.readlink	= proc_self_readlink,
20751da177e4SLinus Torvalds 	.follow_link	= proc_self_follow_link,
20761da177e4SLinus Torvalds };
20771da177e4SLinus Torvalds 
207828a6d671SEric W. Biederman /*
2079801199ceSEric W. Biederman  * proc base
2080801199ceSEric W. Biederman  *
2081801199ceSEric W. Biederman  * These are the directory entries in the root directory of /proc
2082801199ceSEric W. Biederman  * that properly belong to the /proc filesystem, as they describe
2083801199ceSEric W. Biederman  * describe something that is process related.
2084801199ceSEric W. Biederman  */
2085c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = {
208661a28784SEric W. Biederman 	NOD("self", S_IFLNK|S_IRWXUGO,
2087801199ceSEric W. Biederman 		&proc_self_inode_operations, NULL, {}),
2088801199ceSEric W. Biederman };
2089801199ceSEric W. Biederman 
2090801199ceSEric W. Biederman /*
2091801199ceSEric W. Biederman  *	Exceptional case: normally we are not allowed to unhash a busy
2092801199ceSEric W. Biederman  * directory. In this case, however, we can do it - no aliasing problems
2093801199ceSEric W. Biederman  * due to the way we treat inodes.
2094801199ceSEric W. Biederman  */
2095801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2096801199ceSEric W. Biederman {
2097801199ceSEric W. Biederman 	struct inode *inode = dentry->d_inode;
2098801199ceSEric W. Biederman 	struct task_struct *task = get_proc_task(inode);
2099801199ceSEric W. Biederman 	if (task) {
2100801199ceSEric W. Biederman 		put_task_struct(task);
2101801199ceSEric W. Biederman 		return 1;
2102801199ceSEric W. Biederman 	}
2103801199ceSEric W. Biederman 	d_drop(dentry);
2104801199ceSEric W. Biederman 	return 0;
2105801199ceSEric W. Biederman }
2106801199ceSEric W. Biederman 
2107801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations =
2108801199ceSEric W. Biederman {
2109801199ceSEric W. Biederman 	.d_revalidate	= proc_base_revalidate,
2110801199ceSEric W. Biederman 	.d_delete	= pid_delete_dentry,
2111801199ceSEric W. Biederman };
2112801199ceSEric W. Biederman 
2113444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir,
2114c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2115801199ceSEric W. Biederman {
2116c5141e6dSEric Dumazet 	const struct pid_entry *p = ptr;
2117801199ceSEric W. Biederman 	struct inode *inode;
2118801199ceSEric W. Biederman 	struct proc_inode *ei;
2119444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-EINVAL);
2120801199ceSEric W. Biederman 
2121801199ceSEric W. Biederman 	/* Allocate the inode */
2122801199ceSEric W. Biederman 	error = ERR_PTR(-ENOMEM);
2123801199ceSEric W. Biederman 	inode = new_inode(dir->i_sb);
2124801199ceSEric W. Biederman 	if (!inode)
2125801199ceSEric W. Biederman 		goto out;
2126801199ceSEric W. Biederman 
2127801199ceSEric W. Biederman 	/* Initialize the inode */
2128801199ceSEric W. Biederman 	ei = PROC_I(inode);
2129801199ceSEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2130801199ceSEric W. Biederman 
2131801199ceSEric W. Biederman 	/*
2132801199ceSEric W. Biederman 	 * grab the reference to the task.
2133801199ceSEric W. Biederman 	 */
21341a657f78SOleg Nesterov 	ei->pid = get_task_pid(task, PIDTYPE_PID);
2135801199ceSEric W. Biederman 	if (!ei->pid)
2136801199ceSEric W. Biederman 		goto out_iput;
2137801199ceSEric W. Biederman 
2138801199ceSEric W. Biederman 	inode->i_uid = 0;
2139801199ceSEric W. Biederman 	inode->i_gid = 0;
2140801199ceSEric W. Biederman 	inode->i_mode = p->mode;
2141801199ceSEric W. Biederman 	if (S_ISDIR(inode->i_mode))
2142801199ceSEric W. Biederman 		inode->i_nlink = 2;
2143801199ceSEric W. Biederman 	if (S_ISLNK(inode->i_mode))
2144801199ceSEric W. Biederman 		inode->i_size = 64;
2145801199ceSEric W. Biederman 	if (p->iop)
2146801199ceSEric W. Biederman 		inode->i_op = p->iop;
2147801199ceSEric W. Biederman 	if (p->fop)
2148801199ceSEric W. Biederman 		inode->i_fop = p->fop;
2149801199ceSEric W. Biederman 	ei->op = p->op;
2150801199ceSEric W. Biederman 	dentry->d_op = &proc_base_dentry_operations;
2151801199ceSEric W. Biederman 	d_add(dentry, inode);
2152801199ceSEric W. Biederman 	error = NULL;
2153801199ceSEric W. Biederman out:
2154801199ceSEric W. Biederman 	return error;
2155801199ceSEric W. Biederman out_iput:
2156801199ceSEric W. Biederman 	iput(inode);
2157801199ceSEric W. Biederman 	goto out;
2158801199ceSEric W. Biederman }
2159801199ceSEric W. Biederman 
2160444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2161444ceed8SEric W. Biederman {
2162444ceed8SEric W. Biederman 	struct dentry *error;
2163444ceed8SEric W. Biederman 	struct task_struct *task = get_proc_task(dir);
2164c5141e6dSEric Dumazet 	const struct pid_entry *p, *last;
2165444ceed8SEric W. Biederman 
2166444ceed8SEric W. Biederman 	error = ERR_PTR(-ENOENT);
2167444ceed8SEric W. Biederman 
2168444ceed8SEric W. Biederman 	if (!task)
2169444ceed8SEric W. Biederman 		goto out_no_task;
2170444ceed8SEric W. Biederman 
2171444ceed8SEric W. Biederman 	/* Lookup the directory entry */
21727bcd6b0eSEric W. Biederman 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
21737bcd6b0eSEric W. Biederman 	for (p = proc_base_stuff; p <= last; p++) {
2174444ceed8SEric W. Biederman 		if (p->len != dentry->d_name.len)
2175444ceed8SEric W. Biederman 			continue;
2176444ceed8SEric W. Biederman 		if (!memcmp(dentry->d_name.name, p->name, p->len))
2177444ceed8SEric W. Biederman 			break;
2178444ceed8SEric W. Biederman 	}
21797bcd6b0eSEric W. Biederman 	if (p > last)
2180444ceed8SEric W. Biederman 		goto out;
2181444ceed8SEric W. Biederman 
2182444ceed8SEric W. Biederman 	error = proc_base_instantiate(dir, dentry, task, p);
2183444ceed8SEric W. Biederman 
2184444ceed8SEric W. Biederman out:
2185444ceed8SEric W. Biederman 	put_task_struct(task);
2186444ceed8SEric W. Biederman out_no_task:
2187444ceed8SEric W. Biederman 	return error;
2188444ceed8SEric W. Biederman }
2189444ceed8SEric W. Biederman 
2190c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent,
2191c5141e6dSEric Dumazet 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
219261a28784SEric W. Biederman {
219361a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
219461a28784SEric W. Biederman 				proc_base_instantiate, task, p);
219561a28784SEric W. Biederman }
219661a28784SEric W. Biederman 
2197aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2198aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2199aba76fdbSAndrew Morton {
2200aba76fdbSAndrew Morton 	return sprintf(buffer,
22014b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
2202aba76fdbSAndrew Morton 			"rchar: %llu\n"
2203aba76fdbSAndrew Morton 			"wchar: %llu\n"
2204aba76fdbSAndrew Morton 			"syscr: %llu\n"
2205aba76fdbSAndrew Morton 			"syscw: %llu\n"
22064b98d11bSAlexey Dobriyan #endif
2207aba76fdbSAndrew Morton 			"read_bytes: %llu\n"
2208aba76fdbSAndrew Morton 			"write_bytes: %llu\n"
2209aba76fdbSAndrew Morton 			"cancelled_write_bytes: %llu\n",
22104b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT
2211aba76fdbSAndrew Morton 			(unsigned long long)task->rchar,
2212aba76fdbSAndrew Morton 			(unsigned long long)task->wchar,
2213aba76fdbSAndrew Morton 			(unsigned long long)task->syscr,
2214aba76fdbSAndrew Morton 			(unsigned long long)task->syscw,
22154b98d11bSAlexey Dobriyan #endif
2216aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.read_bytes,
2217aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.write_bytes,
2218aba76fdbSAndrew Morton 			(unsigned long long)task->ioac.cancelled_write_bytes);
2219aba76fdbSAndrew Morton }
2220aba76fdbSAndrew Morton #endif
2221aba76fdbSAndrew Morton 
2222801199ceSEric W. Biederman /*
222328a6d671SEric W. Biederman  * Thread groups
222428a6d671SEric W. Biederman  */
222500977a59SArjan van de Ven static const struct file_operations proc_task_operations;
2226c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations;
222720cdc894SEric W. Biederman 
2228c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = {
222961a28784SEric W. Biederman 	DIR("task",       S_IRUGO|S_IXUGO, task),
223061a28784SEric W. Biederman 	DIR("fd",         S_IRUSR|S_IXUSR, fd),
223127932742SMiklos Szeredi 	DIR("fdinfo",     S_IRUSR|S_IXUSR, fdinfo),
2232315e28c8SJames Pearson 	REG("environ",    S_IRUSR, environ),
223361a28784SEric W. Biederman 	INF("auxv",       S_IRUSR, pid_auxv),
223461a28784SEric W. Biederman 	INF("status",     S_IRUGO, pid_status),
2235d85f50d5SNeil Horman 	INF("limits",	  S_IRUSR, pid_limits),
223643ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
223743ae34cbSIngo Molnar 	REG("sched",      S_IRUGO|S_IWUSR, pid_sched),
223843ae34cbSIngo Molnar #endif
223961a28784SEric W. Biederman 	INF("cmdline",    S_IRUGO, pid_cmdline),
224061a28784SEric W. Biederman 	INF("stat",       S_IRUGO, tgid_stat),
224161a28784SEric W. Biederman 	INF("statm",      S_IRUGO, pid_statm),
224261a28784SEric W. Biederman 	REG("maps",       S_IRUGO, maps),
224328a6d671SEric W. Biederman #ifdef CONFIG_NUMA
224461a28784SEric W. Biederman 	REG("numa_maps",  S_IRUGO, numa_maps),
224528a6d671SEric W. Biederman #endif
224661a28784SEric W. Biederman 	REG("mem",        S_IRUSR|S_IWUSR, mem),
224761a28784SEric W. Biederman 	LNK("cwd",        cwd),
224861a28784SEric W. Biederman 	LNK("root",       root),
224961a28784SEric W. Biederman 	LNK("exe",        exe),
225061a28784SEric W. Biederman 	REG("mounts",     S_IRUGO, mounts),
225161a28784SEric W. Biederman 	REG("mountstats", S_IRUSR, mountstats),
225228a6d671SEric W. Biederman #ifdef CONFIG_MMU
2253b813e931SDavid Rientjes 	REG("clear_refs", S_IWUSR, clear_refs),
225461a28784SEric W. Biederman 	REG("smaps",      S_IRUGO, smaps),
2255*85863e47SMatt Mackall 	REG("pagemap",    S_IRUSR, pagemap),
225628a6d671SEric W. Biederman #endif
225728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
225872d9dcfcSEric W. Biederman 	DIR("attr",       S_IRUGO|S_IXUGO, attr_dir),
225928a6d671SEric W. Biederman #endif
226028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
226161a28784SEric W. Biederman 	INF("wchan",      S_IRUGO, pid_wchan),
226228a6d671SEric W. Biederman #endif
226328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
226461a28784SEric W. Biederman 	INF("schedstat",  S_IRUGO, pid_schedstat),
226528a6d671SEric W. Biederman #endif
22669745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
22679745512cSArjan van de Ven 	REG("latency",  S_IRUGO, lstats),
22689745512cSArjan van de Ven #endif
22698793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
227061a28784SEric W. Biederman 	REG("cpuset",     S_IRUGO, cpuset),
227128a6d671SEric W. Biederman #endif
2272a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2273a424316cSPaul Menage 	REG("cgroup",  S_IRUGO, cgroup),
2274a424316cSPaul Menage #endif
227561a28784SEric W. Biederman 	INF("oom_score",  S_IRUGO, oom_score),
227661a28784SEric W. Biederman 	REG("oom_adj",    S_IRUGO|S_IWUSR, oom_adjust),
227728a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
227861a28784SEric W. Biederman 	REG("loginuid",   S_IWUSR|S_IRUGO, loginuid),
227928a6d671SEric W. Biederman #endif
2280f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2281f4f154fdSAkinobu Mita 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2282f4f154fdSAkinobu Mita #endif
22833cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
22843cb4a0bbSKawai, Hidehiro 	REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
22853cb4a0bbSKawai, Hidehiro #endif
2286aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING
2287aba76fdbSAndrew Morton 	INF("io",	S_IRUGO, pid_io_accounting),
2288aba76fdbSAndrew Morton #endif
228928a6d671SEric W. Biederman };
229028a6d671SEric W. Biederman 
229128a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp,
229228a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
229328a6d671SEric W. Biederman {
229428a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
229528a6d671SEric W. Biederman 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
229628a6d671SEric W. Biederman }
229728a6d671SEric W. Biederman 
229800977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = {
229928a6d671SEric W. Biederman 	.read		= generic_read_dir,
230028a6d671SEric W. Biederman 	.readdir	= proc_tgid_base_readdir,
230128a6d671SEric W. Biederman };
230228a6d671SEric W. Biederman 
230328a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
23047bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
23057bcd6b0eSEric W. Biederman 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
230628a6d671SEric W. Biederman }
230728a6d671SEric W. Biederman 
2308c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = {
230928a6d671SEric W. Biederman 	.lookup		= proc_tgid_base_lookup,
231028a6d671SEric W. Biederman 	.getattr	= pid_getattr,
231128a6d671SEric W. Biederman 	.setattr	= proc_setattr,
231228a6d671SEric W. Biederman };
231328a6d671SEric W. Biederman 
231460347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
23151da177e4SLinus Torvalds {
231648e6484dSEric W. Biederman 	struct dentry *dentry, *leader, *dir;
23178578cea7SEric W. Biederman 	char buf[PROC_NUMBUF];
231848e6484dSEric W. Biederman 	struct qstr name;
23191da177e4SLinus Torvalds 
232048e6484dSEric W. Biederman 	name.name = buf;
232160347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
232260347f67SPavel Emelyanov 	dentry = d_hash_and_lookup(mnt->mnt_root, &name);
232348e6484dSEric W. Biederman 	if (dentry) {
232448e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
232548e6484dSEric W. Biederman 		d_drop(dentry);
232648e6484dSEric W. Biederman 		dput(dentry);
23271da177e4SLinus Torvalds 	}
23281da177e4SLinus Torvalds 
232960347f67SPavel Emelyanov 	if (tgid == 0)
233048e6484dSEric W. Biederman 		goto out;
23311da177e4SLinus Torvalds 
233248e6484dSEric W. Biederman 	name.name = buf;
233360347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", tgid);
233460347f67SPavel Emelyanov 	leader = d_hash_and_lookup(mnt->mnt_root, &name);
233548e6484dSEric W. Biederman 	if (!leader)
233648e6484dSEric W. Biederman 		goto out;
233748e6484dSEric W. Biederman 
233848e6484dSEric W. Biederman 	name.name = "task";
233948e6484dSEric W. Biederman 	name.len = strlen(name.name);
234048e6484dSEric W. Biederman 	dir = d_hash_and_lookup(leader, &name);
234148e6484dSEric W. Biederman 	if (!dir)
234248e6484dSEric W. Biederman 		goto out_put_leader;
234348e6484dSEric W. Biederman 
234448e6484dSEric W. Biederman 	name.name = buf;
234560347f67SPavel Emelyanov 	name.len = snprintf(buf, sizeof(buf), "%d", pid);
234648e6484dSEric W. Biederman 	dentry = d_hash_and_lookup(dir, &name);
234748e6484dSEric W. Biederman 	if (dentry) {
234848e6484dSEric W. Biederman 		shrink_dcache_parent(dentry);
234948e6484dSEric W. Biederman 		d_drop(dentry);
235048e6484dSEric W. Biederman 		dput(dentry);
23511da177e4SLinus Torvalds 	}
235248e6484dSEric W. Biederman 
235348e6484dSEric W. Biederman 	dput(dir);
235448e6484dSEric W. Biederman out_put_leader:
235548e6484dSEric W. Biederman 	dput(leader);
235648e6484dSEric W. Biederman out:
235748e6484dSEric W. Biederman 	return;
23581da177e4SLinus Torvalds }
23591da177e4SLinus Torvalds 
23600895e91dSRandy Dunlap /**
23610895e91dSRandy Dunlap  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
23620895e91dSRandy Dunlap  * @task: task that should be flushed.
23630895e91dSRandy Dunlap  *
23640895e91dSRandy Dunlap  * When flushing dentries from proc, one needs to flush them from global
236560347f67SPavel Emelyanov  * proc (proc_mnt) and from all the namespaces' procs this task was seen
23660895e91dSRandy Dunlap  * in. This call is supposed to do all of this job.
23670895e91dSRandy Dunlap  *
23680895e91dSRandy Dunlap  * Looks in the dcache for
23690895e91dSRandy Dunlap  * /proc/@pid
23700895e91dSRandy Dunlap  * /proc/@tgid/task/@pid
23710895e91dSRandy Dunlap  * if either directory is present flushes it and all of it'ts children
23720895e91dSRandy Dunlap  * from the dcache.
23730895e91dSRandy Dunlap  *
23740895e91dSRandy Dunlap  * It is safe and reasonable to cache /proc entries for a task until
23750895e91dSRandy Dunlap  * that task exits.  After that they just clog up the dcache with
23760895e91dSRandy Dunlap  * useless entries, possibly causing useful dcache entries to be
23770895e91dSRandy Dunlap  * flushed instead.  This routine is proved to flush those useless
23780895e91dSRandy Dunlap  * dcache entries at process exit time.
23790895e91dSRandy Dunlap  *
23800895e91dSRandy Dunlap  * NOTE: This routine is just an optimization so it does not guarantee
23810895e91dSRandy Dunlap  *       that no dcache entries will exist at process exit time it
23820895e91dSRandy Dunlap  *       just makes it very unlikely that any will persist.
238360347f67SPavel Emelyanov  */
238460347f67SPavel Emelyanov 
238560347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task)
238660347f67SPavel Emelyanov {
23879fcc2d15SEric W. Biederman 	int i;
23889fcc2d15SEric W. Biederman 	struct pid *pid, *tgid = NULL;
2389130f77ecSPavel Emelyanov 	struct upid *upid;
2390130f77ecSPavel Emelyanov 
2391130f77ecSPavel Emelyanov 	pid = task_pid(task);
23929fcc2d15SEric W. Biederman 	if (thread_group_leader(task))
2393130f77ecSPavel Emelyanov 		tgid = task_tgid(task);
23949fcc2d15SEric W. Biederman 
23959fcc2d15SEric W. Biederman 	for (i = 0; i <= pid->level; i++) {
2396130f77ecSPavel Emelyanov 		upid = &pid->numbers[i];
2397130f77ecSPavel Emelyanov 		proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
23989fcc2d15SEric W. Biederman 			tgid ? tgid->numbers[i].nr : 0);
2399130f77ecSPavel Emelyanov 	}
24006f4e6433SPavel Emelyanov 
24016f4e6433SPavel Emelyanov 	upid = &pid->numbers[pid->level];
24026f4e6433SPavel Emelyanov 	if (upid->nr == 1)
24036f4e6433SPavel Emelyanov 		pid_ns_release_proc(upid->ns);
240460347f67SPavel Emelyanov }
240560347f67SPavel Emelyanov 
24069711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir,
24079711ef99SAdrian Bunk 					   struct dentry * dentry,
2408c5141e6dSEric Dumazet 					   struct task_struct *task, const void *ptr)
2409444ceed8SEric W. Biederman {
2410444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2411444ceed8SEric W. Biederman 	struct inode *inode;
2412444ceed8SEric W. Biederman 
241361a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2414444ceed8SEric W. Biederman 	if (!inode)
2415444ceed8SEric W. Biederman 		goto out;
2416444ceed8SEric W. Biederman 
2417444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2418444ceed8SEric W. Biederman 	inode->i_op = &proc_tgid_base_inode_operations;
2419444ceed8SEric W. Biederman 	inode->i_fop = &proc_tgid_base_operations;
2420444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
242127932742SMiklos Szeredi 	inode->i_nlink = 5;
2422444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY
2423444ceed8SEric W. Biederman 	inode->i_nlink += 1;
2424444ceed8SEric W. Biederman #endif
2425444ceed8SEric W. Biederman 
2426444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2427444ceed8SEric W. Biederman 
2428444ceed8SEric W. Biederman 	d_add(dentry, inode);
2429444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2430444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2431444ceed8SEric W. Biederman 		error = NULL;
2432444ceed8SEric W. Biederman out:
2433444ceed8SEric W. Biederman 	return error;
2434444ceed8SEric W. Biederman }
2435444ceed8SEric W. Biederman 
24361da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
24371da177e4SLinus Torvalds {
2438cd6a3ce9SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
24391da177e4SLinus Torvalds 	struct task_struct *task;
24401da177e4SLinus Torvalds 	unsigned tgid;
2441b488893aSPavel Emelyanov 	struct pid_namespace *ns;
24421da177e4SLinus Torvalds 
2443801199ceSEric W. Biederman 	result = proc_base_lookup(dir, dentry);
2444801199ceSEric W. Biederman 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2445801199ceSEric W. Biederman 		goto out;
2446801199ceSEric W. Biederman 
24471da177e4SLinus Torvalds 	tgid = name_to_int(dentry);
24481da177e4SLinus Torvalds 	if (tgid == ~0U)
24491da177e4SLinus Torvalds 		goto out;
24501da177e4SLinus Torvalds 
2451b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
2452de758734SEric W. Biederman 	rcu_read_lock();
2453b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tgid, ns);
24541da177e4SLinus Torvalds 	if (task)
24551da177e4SLinus Torvalds 		get_task_struct(task);
2456de758734SEric W. Biederman 	rcu_read_unlock();
24571da177e4SLinus Torvalds 	if (!task)
24581da177e4SLinus Torvalds 		goto out;
24591da177e4SLinus Torvalds 
2460444ceed8SEric W. Biederman 	result = proc_pid_instantiate(dir, dentry, task, NULL);
246148e6484dSEric W. Biederman 	put_task_struct(task);
24621da177e4SLinus Torvalds out:
2463cd6a3ce9SEric W. Biederman 	return result;
24641da177e4SLinus Torvalds }
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds /*
24670804ef4bSEric W. Biederman  * Find the first task with tgid >= tgid
24680bc58a91SEric W. Biederman  *
24691da177e4SLinus Torvalds  */
247019fd4bb2SEric W. Biederman struct tgid_iter {
247119fd4bb2SEric W. Biederman 	unsigned int tgid;
24720804ef4bSEric W. Biederman 	struct task_struct *task;
247319fd4bb2SEric W. Biederman };
247419fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
247519fd4bb2SEric W. Biederman {
24760804ef4bSEric W. Biederman 	struct pid *pid;
24771da177e4SLinus Torvalds 
247819fd4bb2SEric W. Biederman 	if (iter.task)
247919fd4bb2SEric W. Biederman 		put_task_struct(iter.task);
24800804ef4bSEric W. Biederman 	rcu_read_lock();
24810804ef4bSEric W. Biederman retry:
248219fd4bb2SEric W. Biederman 	iter.task = NULL;
248319fd4bb2SEric W. Biederman 	pid = find_ge_pid(iter.tgid, ns);
24840804ef4bSEric W. Biederman 	if (pid) {
248519fd4bb2SEric W. Biederman 		iter.tgid = pid_nr_ns(pid, ns);
248619fd4bb2SEric W. Biederman 		iter.task = pid_task(pid, PIDTYPE_PID);
24870804ef4bSEric W. Biederman 		/* What we to know is if the pid we have find is the
24880804ef4bSEric W. Biederman 		 * pid of a thread_group_leader.  Testing for task
24890804ef4bSEric W. Biederman 		 * being a thread_group_leader is the obvious thing
24900804ef4bSEric W. Biederman 		 * todo but there is a window when it fails, due to
24910804ef4bSEric W. Biederman 		 * the pid transfer logic in de_thread.
24920804ef4bSEric W. Biederman 		 *
24930804ef4bSEric W. Biederman 		 * So we perform the straight forward test of seeing
24940804ef4bSEric W. Biederman 		 * if the pid we have found is the pid of a thread
24950804ef4bSEric W. Biederman 		 * group leader, and don't worry if the task we have
24960804ef4bSEric W. Biederman 		 * found doesn't happen to be a thread group leader.
24970804ef4bSEric W. Biederman 		 * As we don't care in the case of readdir.
24980bc58a91SEric W. Biederman 		 */
249919fd4bb2SEric W. Biederman 		if (!iter.task || !has_group_leader_pid(iter.task)) {
250019fd4bb2SEric W. Biederman 			iter.tgid += 1;
25010804ef4bSEric W. Biederman 			goto retry;
250219fd4bb2SEric W. Biederman 		}
250319fd4bb2SEric W. Biederman 		get_task_struct(iter.task);
25041da177e4SLinus Torvalds 	}
2505454cc105SEric W. Biederman 	rcu_read_unlock();
250619fd4bb2SEric W. Biederman 	return iter;
25071da177e4SLinus Torvalds }
25081da177e4SLinus Torvalds 
25097bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
25101da177e4SLinus Torvalds 
251161a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
251219fd4bb2SEric W. Biederman 	struct tgid_iter iter)
251361a28784SEric W. Biederman {
251461a28784SEric W. Biederman 	char name[PROC_NUMBUF];
251519fd4bb2SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", iter.tgid);
251661a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
251719fd4bb2SEric W. Biederman 				proc_pid_instantiate, iter.task, NULL);
251861a28784SEric W. Biederman }
251961a28784SEric W. Biederman 
25201da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */
25211da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
25221da177e4SLinus Torvalds {
25231da177e4SLinus Torvalds 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
25242fddfeefSJosef "Jeff" Sipek 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
252519fd4bb2SEric W. Biederman 	struct tgid_iter iter;
2526b488893aSPavel Emelyanov 	struct pid_namespace *ns;
25271da177e4SLinus Torvalds 
252861a28784SEric W. Biederman 	if (!reaper)
252961a28784SEric W. Biederman 		goto out_no_task;
253061a28784SEric W. Biederman 
25317bcd6b0eSEric W. Biederman 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2532c5141e6dSEric Dumazet 		const struct pid_entry *p = &proc_base_stuff[nr];
253361a28784SEric W. Biederman 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2534801199ceSEric W. Biederman 			goto out;
25351da177e4SLinus Torvalds 	}
25361da177e4SLinus Torvalds 
2537b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
253819fd4bb2SEric W. Biederman 	iter.task = NULL;
253919fd4bb2SEric W. Biederman 	iter.tgid = filp->f_pos - TGID_OFFSET;
254019fd4bb2SEric W. Biederman 	for (iter = next_tgid(ns, iter);
254119fd4bb2SEric W. Biederman 	     iter.task;
254219fd4bb2SEric W. Biederman 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
254319fd4bb2SEric W. Biederman 		filp->f_pos = iter.tgid + TGID_OFFSET;
254419fd4bb2SEric W. Biederman 		if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
254519fd4bb2SEric W. Biederman 			put_task_struct(iter.task);
25460804ef4bSEric W. Biederman 			goto out;
25471da177e4SLinus Torvalds 		}
25481da177e4SLinus Torvalds 	}
25490804ef4bSEric W. Biederman 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
25500804ef4bSEric W. Biederman out:
255161a28784SEric W. Biederman 	put_task_struct(reaper);
255261a28784SEric W. Biederman out_no_task:
25531da177e4SLinus Torvalds 	return 0;
25541da177e4SLinus Torvalds }
25551da177e4SLinus Torvalds 
25560bc58a91SEric W. Biederman /*
255728a6d671SEric W. Biederman  * Tasks
255828a6d671SEric W. Biederman  */
2559c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = {
256061a28784SEric W. Biederman 	DIR("fd",        S_IRUSR|S_IXUSR, fd),
256127932742SMiklos Szeredi 	DIR("fdinfo",    S_IRUSR|S_IXUSR, fdinfo),
2562315e28c8SJames Pearson 	REG("environ",   S_IRUSR, environ),
256361a28784SEric W. Biederman 	INF("auxv",      S_IRUSR, pid_auxv),
256461a28784SEric W. Biederman 	INF("status",    S_IRUGO, pid_status),
2565d85f50d5SNeil Horman 	INF("limits",	 S_IRUSR, pid_limits),
256643ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
256743ae34cbSIngo Molnar 	REG("sched",     S_IRUGO|S_IWUSR, pid_sched),
256843ae34cbSIngo Molnar #endif
256961a28784SEric W. Biederman 	INF("cmdline",   S_IRUGO, pid_cmdline),
257061a28784SEric W. Biederman 	INF("stat",      S_IRUGO, tid_stat),
257161a28784SEric W. Biederman 	INF("statm",     S_IRUGO, pid_statm),
257261a28784SEric W. Biederman 	REG("maps",      S_IRUGO, maps),
257328a6d671SEric W. Biederman #ifdef CONFIG_NUMA
257461a28784SEric W. Biederman 	REG("numa_maps", S_IRUGO, numa_maps),
257528a6d671SEric W. Biederman #endif
257661a28784SEric W. Biederman 	REG("mem",       S_IRUSR|S_IWUSR, mem),
257761a28784SEric W. Biederman 	LNK("cwd",       cwd),
257861a28784SEric W. Biederman 	LNK("root",      root),
257961a28784SEric W. Biederman 	LNK("exe",       exe),
258061a28784SEric W. Biederman 	REG("mounts",    S_IRUGO, mounts),
258128a6d671SEric W. Biederman #ifdef CONFIG_MMU
2582b813e931SDavid Rientjes 	REG("clear_refs", S_IWUSR, clear_refs),
258361a28784SEric W. Biederman 	REG("smaps",     S_IRUGO, smaps),
2584*85863e47SMatt Mackall 	REG("pagemap",    S_IRUSR, pagemap),
258528a6d671SEric W. Biederman #endif
258628a6d671SEric W. Biederman #ifdef CONFIG_SECURITY
258772d9dcfcSEric W. Biederman 	DIR("attr",      S_IRUGO|S_IXUGO, attr_dir),
258828a6d671SEric W. Biederman #endif
258928a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS
259061a28784SEric W. Biederman 	INF("wchan",     S_IRUGO, pid_wchan),
259128a6d671SEric W. Biederman #endif
259228a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS
259361a28784SEric W. Biederman 	INF("schedstat", S_IRUGO, pid_schedstat),
259428a6d671SEric W. Biederman #endif
25959745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP
25969745512cSArjan van de Ven 	REG("latency",  S_IRUGO, lstats),
25979745512cSArjan van de Ven #endif
25988793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET
259961a28784SEric W. Biederman 	REG("cpuset",    S_IRUGO, cpuset),
260028a6d671SEric W. Biederman #endif
2601a424316cSPaul Menage #ifdef CONFIG_CGROUPS
2602a424316cSPaul Menage 	REG("cgroup",  S_IRUGO, cgroup),
2603a424316cSPaul Menage #endif
260461a28784SEric W. Biederman 	INF("oom_score", S_IRUGO, oom_score),
260561a28784SEric W. Biederman 	REG("oom_adj",   S_IRUGO|S_IWUSR, oom_adjust),
260628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL
260761a28784SEric W. Biederman 	REG("loginuid",  S_IWUSR|S_IRUGO, loginuid),
260828a6d671SEric W. Biederman #endif
2609f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION
2610f4f154fdSAkinobu Mita 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2611f4f154fdSAkinobu Mita #endif
261228a6d671SEric W. Biederman };
261328a6d671SEric W. Biederman 
261428a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp,
261528a6d671SEric W. Biederman 			     void * dirent, filldir_t filldir)
261628a6d671SEric W. Biederman {
261728a6d671SEric W. Biederman 	return proc_pident_readdir(filp,dirent,filldir,
261828a6d671SEric W. Biederman 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
261928a6d671SEric W. Biederman }
262028a6d671SEric W. Biederman 
262128a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
26227bcd6b0eSEric W. Biederman 	return proc_pident_lookup(dir, dentry,
26237bcd6b0eSEric W. Biederman 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
262428a6d671SEric W. Biederman }
262528a6d671SEric W. Biederman 
262600977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = {
262728a6d671SEric W. Biederman 	.read		= generic_read_dir,
262828a6d671SEric W. Biederman 	.readdir	= proc_tid_base_readdir,
262928a6d671SEric W. Biederman };
263028a6d671SEric W. Biederman 
2631c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = {
263228a6d671SEric W. Biederman 	.lookup		= proc_tid_base_lookup,
263328a6d671SEric W. Biederman 	.getattr	= pid_getattr,
263428a6d671SEric W. Biederman 	.setattr	= proc_setattr,
263528a6d671SEric W. Biederman };
263628a6d671SEric W. Biederman 
2637444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir,
2638c5141e6dSEric Dumazet 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2639444ceed8SEric W. Biederman {
2640444ceed8SEric W. Biederman 	struct dentry *error = ERR_PTR(-ENOENT);
2641444ceed8SEric W. Biederman 	struct inode *inode;
264261a28784SEric W. Biederman 	inode = proc_pid_make_inode(dir->i_sb, task);
2643444ceed8SEric W. Biederman 
2644444ceed8SEric W. Biederman 	if (!inode)
2645444ceed8SEric W. Biederman 		goto out;
2646444ceed8SEric W. Biederman 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2647444ceed8SEric W. Biederman 	inode->i_op = &proc_tid_base_inode_operations;
2648444ceed8SEric W. Biederman 	inode->i_fop = &proc_tid_base_operations;
2649444ceed8SEric W. Biederman 	inode->i_flags|=S_IMMUTABLE;
265027932742SMiklos Szeredi 	inode->i_nlink = 4;
2651444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY
2652444ceed8SEric W. Biederman 	inode->i_nlink += 1;
2653444ceed8SEric W. Biederman #endif
2654444ceed8SEric W. Biederman 
2655444ceed8SEric W. Biederman 	dentry->d_op = &pid_dentry_operations;
2656444ceed8SEric W. Biederman 
2657444ceed8SEric W. Biederman 	d_add(dentry, inode);
2658444ceed8SEric W. Biederman 	/* Close the race of the process dying before we return the dentry */
2659444ceed8SEric W. Biederman 	if (pid_revalidate(dentry, NULL))
2660444ceed8SEric W. Biederman 		error = NULL;
2661444ceed8SEric W. Biederman out:
2662444ceed8SEric W. Biederman 	return error;
2663444ceed8SEric W. Biederman }
2664444ceed8SEric W. Biederman 
266528a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
266628a6d671SEric W. Biederman {
266728a6d671SEric W. Biederman 	struct dentry *result = ERR_PTR(-ENOENT);
266828a6d671SEric W. Biederman 	struct task_struct *task;
266928a6d671SEric W. Biederman 	struct task_struct *leader = get_proc_task(dir);
267028a6d671SEric W. Biederman 	unsigned tid;
2671b488893aSPavel Emelyanov 	struct pid_namespace *ns;
267228a6d671SEric W. Biederman 
267328a6d671SEric W. Biederman 	if (!leader)
267428a6d671SEric W. Biederman 		goto out_no_task;
267528a6d671SEric W. Biederman 
267628a6d671SEric W. Biederman 	tid = name_to_int(dentry);
267728a6d671SEric W. Biederman 	if (tid == ~0U)
267828a6d671SEric W. Biederman 		goto out;
267928a6d671SEric W. Biederman 
2680b488893aSPavel Emelyanov 	ns = dentry->d_sb->s_fs_info;
268128a6d671SEric W. Biederman 	rcu_read_lock();
2682b488893aSPavel Emelyanov 	task = find_task_by_pid_ns(tid, ns);
268328a6d671SEric W. Biederman 	if (task)
268428a6d671SEric W. Biederman 		get_task_struct(task);
268528a6d671SEric W. Biederman 	rcu_read_unlock();
268628a6d671SEric W. Biederman 	if (!task)
268728a6d671SEric W. Biederman 		goto out;
2688bac0abd6SPavel Emelyanov 	if (!same_thread_group(leader, task))
268928a6d671SEric W. Biederman 		goto out_drop_task;
269028a6d671SEric W. Biederman 
2691444ceed8SEric W. Biederman 	result = proc_task_instantiate(dir, dentry, task, NULL);
269228a6d671SEric W. Biederman out_drop_task:
269328a6d671SEric W. Biederman 	put_task_struct(task);
269428a6d671SEric W. Biederman out:
269528a6d671SEric W. Biederman 	put_task_struct(leader);
269628a6d671SEric W. Biederman out_no_task:
269728a6d671SEric W. Biederman 	return result;
269828a6d671SEric W. Biederman }
269928a6d671SEric W. Biederman 
270028a6d671SEric W. Biederman /*
27010bc58a91SEric W. Biederman  * Find the first tid of a thread group to return to user space.
27020bc58a91SEric W. Biederman  *
27030bc58a91SEric W. Biederman  * Usually this is just the thread group leader, but if the users
27040bc58a91SEric W. Biederman  * buffer was too small or there was a seek into the middle of the
27050bc58a91SEric W. Biederman  * directory we have more work todo.
27060bc58a91SEric W. Biederman  *
27070bc58a91SEric W. Biederman  * In the case of a short read we start with find_task_by_pid.
27080bc58a91SEric W. Biederman  *
27090bc58a91SEric W. Biederman  * In the case of a seek we start with the leader and walk nr
27100bc58a91SEric W. Biederman  * threads past it.
27110bc58a91SEric W. Biederman  */
2712cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader,
2713b488893aSPavel Emelyanov 		int tid, int nr, struct pid_namespace *ns)
27140bc58a91SEric W. Biederman {
2715a872ff0cSOleg Nesterov 	struct task_struct *pos;
27160bc58a91SEric W. Biederman 
2717cc288738SEric W. Biederman 	rcu_read_lock();
27180bc58a91SEric W. Biederman 	/* Attempt to start with the pid of a thread */
27190bc58a91SEric W. Biederman 	if (tid && (nr > 0)) {
2720b488893aSPavel Emelyanov 		pos = find_task_by_pid_ns(tid, ns);
2721a872ff0cSOleg Nesterov 		if (pos && (pos->group_leader == leader))
2722a872ff0cSOleg Nesterov 			goto found;
27230bc58a91SEric W. Biederman 	}
27240bc58a91SEric W. Biederman 
27250bc58a91SEric W. Biederman 	/* If nr exceeds the number of threads there is nothing todo */
27260bc58a91SEric W. Biederman 	pos = NULL;
2727a872ff0cSOleg Nesterov 	if (nr && nr >= get_nr_threads(leader))
2728a872ff0cSOleg Nesterov 		goto out;
2729a872ff0cSOleg Nesterov 
2730a872ff0cSOleg Nesterov 	/* If we haven't found our starting place yet start
2731a872ff0cSOleg Nesterov 	 * with the leader and walk nr threads forward.
2732a872ff0cSOleg Nesterov 	 */
2733a872ff0cSOleg Nesterov 	for (pos = leader; nr > 0; --nr) {
2734a872ff0cSOleg Nesterov 		pos = next_thread(pos);
2735a872ff0cSOleg Nesterov 		if (pos == leader) {
2736a872ff0cSOleg Nesterov 			pos = NULL;
2737a872ff0cSOleg Nesterov 			goto out;
2738a872ff0cSOleg Nesterov 		}
2739a872ff0cSOleg Nesterov 	}
2740a872ff0cSOleg Nesterov found:
2741a872ff0cSOleg Nesterov 	get_task_struct(pos);
2742a872ff0cSOleg Nesterov out:
2743cc288738SEric W. Biederman 	rcu_read_unlock();
27440bc58a91SEric W. Biederman 	return pos;
27450bc58a91SEric W. Biederman }
27460bc58a91SEric W. Biederman 
27470bc58a91SEric W. Biederman /*
27480bc58a91SEric W. Biederman  * Find the next thread in the thread list.
27490bc58a91SEric W. Biederman  * Return NULL if there is an error or no next thread.
27500bc58a91SEric W. Biederman  *
27510bc58a91SEric W. Biederman  * The reference to the input task_struct is released.
27520bc58a91SEric W. Biederman  */
27530bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start)
27540bc58a91SEric W. Biederman {
2755c1df7fb8SOleg Nesterov 	struct task_struct *pos = NULL;
2756cc288738SEric W. Biederman 	rcu_read_lock();
2757c1df7fb8SOleg Nesterov 	if (pid_alive(start)) {
27580bc58a91SEric W. Biederman 		pos = next_thread(start);
2759c1df7fb8SOleg Nesterov 		if (thread_group_leader(pos))
27600bc58a91SEric W. Biederman 			pos = NULL;
2761c1df7fb8SOleg Nesterov 		else
2762c1df7fb8SOleg Nesterov 			get_task_struct(pos);
2763c1df7fb8SOleg Nesterov 	}
2764cc288738SEric W. Biederman 	rcu_read_unlock();
27650bc58a91SEric W. Biederman 	put_task_struct(start);
27660bc58a91SEric W. Biederman 	return pos;
27670bc58a91SEric W. Biederman }
27680bc58a91SEric W. Biederman 
276961a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
277061a28784SEric W. Biederman 	struct task_struct *task, int tid)
277161a28784SEric W. Biederman {
277261a28784SEric W. Biederman 	char name[PROC_NUMBUF];
277361a28784SEric W. Biederman 	int len = snprintf(name, sizeof(name), "%d", tid);
277461a28784SEric W. Biederman 	return proc_fill_cache(filp, dirent, filldir, name, len,
277561a28784SEric W. Biederman 				proc_task_instantiate, task, NULL);
277661a28784SEric W. Biederman }
277761a28784SEric W. Biederman 
27781da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */
27791da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
27801da177e4SLinus Torvalds {
27812fddfeefSJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
27821da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
27837d895244SGuillaume Chazarain 	struct task_struct *leader = NULL;
27840bc58a91SEric W. Biederman 	struct task_struct *task;
27851da177e4SLinus Torvalds 	int retval = -ENOENT;
27861da177e4SLinus Torvalds 	ino_t ino;
27870bc58a91SEric W. Biederman 	int tid;
27881da177e4SLinus Torvalds 	unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2789b488893aSPavel Emelyanov 	struct pid_namespace *ns;
27901da177e4SLinus Torvalds 
27917d895244SGuillaume Chazarain 	task = get_proc_task(inode);
27927d895244SGuillaume Chazarain 	if (!task)
27937d895244SGuillaume Chazarain 		goto out_no_task;
27947d895244SGuillaume Chazarain 	rcu_read_lock();
27957d895244SGuillaume Chazarain 	if (pid_alive(task)) {
27967d895244SGuillaume Chazarain 		leader = task->group_leader;
27977d895244SGuillaume Chazarain 		get_task_struct(leader);
27987d895244SGuillaume Chazarain 	}
27997d895244SGuillaume Chazarain 	rcu_read_unlock();
28007d895244SGuillaume Chazarain 	put_task_struct(task);
280199f89551SEric W. Biederman 	if (!leader)
280299f89551SEric W. Biederman 		goto out_no_task;
28031da177e4SLinus Torvalds 	retval = 0;
28041da177e4SLinus Torvalds 
28051da177e4SLinus Torvalds 	switch (pos) {
28061da177e4SLinus Torvalds 	case 0:
28071da177e4SLinus Torvalds 		ino = inode->i_ino;
28081da177e4SLinus Torvalds 		if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
28091da177e4SLinus Torvalds 			goto out;
28101da177e4SLinus Torvalds 		pos++;
28111da177e4SLinus Torvalds 		/* fall through */
28121da177e4SLinus Torvalds 	case 1:
28131da177e4SLinus Torvalds 		ino = parent_ino(dentry);
28141da177e4SLinus Torvalds 		if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
28151da177e4SLinus Torvalds 			goto out;
28161da177e4SLinus Torvalds 		pos++;
28171da177e4SLinus Torvalds 		/* fall through */
28181da177e4SLinus Torvalds 	}
28191da177e4SLinus Torvalds 
28200bc58a91SEric W. Biederman 	/* f_version caches the tgid value that the last readdir call couldn't
28210bc58a91SEric W. Biederman 	 * return. lseek aka telldir automagically resets f_version to 0.
28220bc58a91SEric W. Biederman 	 */
2823b488893aSPavel Emelyanov 	ns = filp->f_dentry->d_sb->s_fs_info;
28242b47c361SMathieu Desnoyers 	tid = (int)filp->f_version;
28250bc58a91SEric W. Biederman 	filp->f_version = 0;
2826b488893aSPavel Emelyanov 	for (task = first_tid(leader, tid, pos - 2, ns);
28270bc58a91SEric W. Biederman 	     task;
28280bc58a91SEric W. Biederman 	     task = next_tid(task), pos++) {
2829b488893aSPavel Emelyanov 		tid = task_pid_nr_ns(task, ns);
283061a28784SEric W. Biederman 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
28310bc58a91SEric W. Biederman 			/* returning this tgid failed, save it as the first
28320bc58a91SEric W. Biederman 			 * pid for the next readir call */
28332b47c361SMathieu Desnoyers 			filp->f_version = (u64)tid;
28340bc58a91SEric W. Biederman 			put_task_struct(task);
28351da177e4SLinus Torvalds 			break;
28360bc58a91SEric W. Biederman 		}
28371da177e4SLinus Torvalds 	}
28381da177e4SLinus Torvalds out:
28391da177e4SLinus Torvalds 	filp->f_pos = pos;
284099f89551SEric W. Biederman 	put_task_struct(leader);
284199f89551SEric W. Biederman out_no_task:
28421da177e4SLinus Torvalds 	return retval;
28431da177e4SLinus Torvalds }
28446e66b52bSEric W. Biederman 
28456e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
28466e66b52bSEric W. Biederman {
28476e66b52bSEric W. Biederman 	struct inode *inode = dentry->d_inode;
284899f89551SEric W. Biederman 	struct task_struct *p = get_proc_task(inode);
28496e66b52bSEric W. Biederman 	generic_fillattr(inode, stat);
28506e66b52bSEric W. Biederman 
285199f89551SEric W. Biederman 	if (p) {
285299f89551SEric W. Biederman 		rcu_read_lock();
285399f89551SEric W. Biederman 		stat->nlink += get_nr_threads(p);
285499f89551SEric W. Biederman 		rcu_read_unlock();
285599f89551SEric W. Biederman 		put_task_struct(p);
28566e66b52bSEric W. Biederman 	}
28576e66b52bSEric W. Biederman 
28586e66b52bSEric W. Biederman 	return 0;
28596e66b52bSEric W. Biederman }
286028a6d671SEric W. Biederman 
2861c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = {
286228a6d671SEric W. Biederman 	.lookup		= proc_task_lookup,
286328a6d671SEric W. Biederman 	.getattr	= proc_task_getattr,
286428a6d671SEric W. Biederman 	.setattr	= proc_setattr,
286528a6d671SEric W. Biederman };
286628a6d671SEric W. Biederman 
286700977a59SArjan van de Ven static const struct file_operations proc_task_operations = {
286828a6d671SEric W. Biederman 	.read		= generic_read_dir,
286928a6d671SEric W. Biederman 	.readdir	= proc_task_readdir,
287028a6d671SEric W. Biederman };
2871