xref: /linux/fs/proc/base.c (revision 757dea93e136b219af09d3cd56a81063fdbdef1a)
1 /*
2  *  linux/fs/proc/base.c
3  *
4  *  Copyright (C) 1991, 1992 Linus Torvalds
5  *
6  *  proc base directory handling functions
7  *
8  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9  *  Instead of using magical inumbers to determine the kind of object
10  *  we allocate and fill in-core inodes upon lookup. They don't even
11  *  go into icache. We cache the reference to task_struct upon lookup too.
12  *  Eventually it should become a filesystem in its own. We don't use the
13  *  rest of procfs anymore.
14  *
15  *
16  *  Changelog:
17  *  17-Jan-2005
18  *  Allan Bezerra
19  *  Bruna Moreira <bruna.moreira@indt.org.br>
20  *  Edjard Mota <edjard.mota@indt.org.br>
21  *  Ilias Biris <ilias.biris@indt.org.br>
22  *  Mauricio Lin <mauricio.lin@indt.org.br>
23  *
24  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25  *
26  *  A new process specific entry (smaps) included in /proc. It shows the
27  *  size of rss for each memory area. The maps entry lacks information
28  *  about physical memory size (rss) for each mapped file, i.e.,
29  *  rss information for executables and library files.
30  *  This additional information is useful for any tools that need to know
31  *  about physical memory consumption for a process specific library.
32  *
33  *  Changelog:
34  *  21-Feb-2005
35  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36  *  Pud inclusion in the page table walking.
37  *
38  *  ChangeLog:
39  *  10-Mar-2005
40  *  10LE Instituto Nokia de Tecnologia - INdT:
41  *  A better way to walks through the page table as suggested by Hugh Dickins.
42  *
43  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44  *  Smaps information related to shared, private, clean and dirty pages.
45  *
46  *  Paul Mundt <paul.mundt@nokia.com>:
47  *  Overall revision about smaps.
48  */
49 
50 #include <asm/uaccess.h>
51 
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/mnt_namespace.h>
63 #include <linux/mm.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/module.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
77 #include "internal.h"
78 
79 /* NOTE:
80  *	Implementing inode permission operations in /proc is almost
81  *	certainly an error.  Permission checks need to happen during
82  *	each system call not at open time.  The reason is that most of
83  *	what we wish to check for permissions in /proc varies at runtime.
84  *
85  *	The classic example of a problem is opening file descriptors
86  *	in /proc for a task before it execs a suid executable.
87  */
88 
89 
90 /* Worst case buffer size needed for holding an integer. */
91 #define PROC_NUMBUF 13
92 
93 struct pid_entry {
94 	char *name;
95 	int len;
96 	mode_t mode;
97 	const struct inode_operations *iop;
98 	const struct file_operations *fop;
99 	union proc_op op;
100 };
101 
102 #define NOD(NAME, MODE, IOP, FOP, OP) {			\
103 	.name = (NAME),					\
104 	.len  = sizeof(NAME) - 1,			\
105 	.mode = MODE,					\
106 	.iop  = IOP,					\
107 	.fop  = FOP,					\
108 	.op   = OP,					\
109 }
110 
111 #define DIR(NAME, MODE, OTYPE)							\
112 	NOD(NAME, (S_IFDIR|(MODE)),						\
113 		&proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations,	\
114 		{} )
115 #define LNK(NAME, OTYPE)					\
116 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
117 		&proc_pid_link_inode_operations, NULL,		\
118 		{ .proc_get_link = &proc_##OTYPE##_link } )
119 #define REG(NAME, MODE, OTYPE)				\
120 	NOD(NAME, (S_IFREG|(MODE)), NULL,		\
121 		&proc_##OTYPE##_operations, {})
122 #define INF(NAME, MODE, OTYPE)				\
123 	NOD(NAME, (S_IFREG|(MODE)), 			\
124 		NULL, &proc_info_file_operations,	\
125 		{ .proc_read = &proc_##OTYPE } )
126 
127 int maps_protect;
128 EXPORT_SYMBOL(maps_protect);
129 
130 static struct fs_struct *get_fs_struct(struct task_struct *task)
131 {
132 	struct fs_struct *fs;
133 	task_lock(task);
134 	fs = task->fs;
135 	if(fs)
136 		atomic_inc(&fs->count);
137 	task_unlock(task);
138 	return fs;
139 }
140 
141 static int get_nr_threads(struct task_struct *tsk)
142 {
143 	/* Must be called with the rcu_read_lock held */
144 	unsigned long flags;
145 	int count = 0;
146 
147 	if (lock_task_sighand(tsk, &flags)) {
148 		count = atomic_read(&tsk->signal->count);
149 		unlock_task_sighand(tsk, &flags);
150 	}
151 	return count;
152 }
153 
154 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
155 {
156 	struct task_struct *task = get_proc_task(inode);
157 	struct fs_struct *fs = NULL;
158 	int result = -ENOENT;
159 
160 	if (task) {
161 		fs = get_fs_struct(task);
162 		put_task_struct(task);
163 	}
164 	if (fs) {
165 		read_lock(&fs->lock);
166 		*mnt = mntget(fs->pwdmnt);
167 		*dentry = dget(fs->pwd);
168 		read_unlock(&fs->lock);
169 		result = 0;
170 		put_fs_struct(fs);
171 	}
172 	return result;
173 }
174 
175 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
176 {
177 	struct task_struct *task = get_proc_task(inode);
178 	struct fs_struct *fs = NULL;
179 	int result = -ENOENT;
180 
181 	if (task) {
182 		fs = get_fs_struct(task);
183 		put_task_struct(task);
184 	}
185 	if (fs) {
186 		read_lock(&fs->lock);
187 		*mnt = mntget(fs->rootmnt);
188 		*dentry = dget(fs->root);
189 		read_unlock(&fs->lock);
190 		result = 0;
191 		put_fs_struct(fs);
192 	}
193 	return result;
194 }
195 
196 #define MAY_PTRACE(task) \
197 	(task == current || \
198 	(task->parent == current && \
199 	(task->ptrace & PT_PTRACED) && \
200 	 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
201 	 security_ptrace(current,task) == 0))
202 
203 static int proc_pid_environ(struct task_struct *task, char * buffer)
204 {
205 	int res = 0;
206 	struct mm_struct *mm = get_task_mm(task);
207 	if (mm) {
208 		unsigned int len = mm->env_end - mm->env_start;
209 		if (len > PAGE_SIZE)
210 			len = PAGE_SIZE;
211 		res = access_process_vm(task, mm->env_start, buffer, len, 0);
212 		if (!ptrace_may_attach(task))
213 			res = -ESRCH;
214 		mmput(mm);
215 	}
216 	return res;
217 }
218 
219 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
220 {
221 	int res = 0;
222 	unsigned int len;
223 	struct mm_struct *mm = get_task_mm(task);
224 	if (!mm)
225 		goto out;
226 	if (!mm->arg_end)
227 		goto out_mm;	/* Shh! No looking before we're done */
228 
229  	len = mm->arg_end - mm->arg_start;
230 
231 	if (len > PAGE_SIZE)
232 		len = PAGE_SIZE;
233 
234 	res = access_process_vm(task, mm->arg_start, buffer, len, 0);
235 
236 	// If the nul at the end of args has been overwritten, then
237 	// assume application is using setproctitle(3).
238 	if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
239 		len = strnlen(buffer, res);
240 		if (len < res) {
241 		    res = len;
242 		} else {
243 			len = mm->env_end - mm->env_start;
244 			if (len > PAGE_SIZE - res)
245 				len = PAGE_SIZE - res;
246 			res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
247 			res = strnlen(buffer, res);
248 		}
249 	}
250 out_mm:
251 	mmput(mm);
252 out:
253 	return res;
254 }
255 
256 static int proc_pid_auxv(struct task_struct *task, char *buffer)
257 {
258 	int res = 0;
259 	struct mm_struct *mm = get_task_mm(task);
260 	if (mm) {
261 		unsigned int nwords = 0;
262 		do
263 			nwords += 2;
264 		while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
265 		res = nwords * sizeof(mm->saved_auxv[0]);
266 		if (res > PAGE_SIZE)
267 			res = PAGE_SIZE;
268 		memcpy(buffer, mm->saved_auxv, res);
269 		mmput(mm);
270 	}
271 	return res;
272 }
273 
274 
275 #ifdef CONFIG_KALLSYMS
276 /*
277  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
278  * Returns the resolved symbol.  If that fails, simply return the address.
279  */
280 static int proc_pid_wchan(struct task_struct *task, char *buffer)
281 {
282 	char *modname;
283 	const char *sym_name;
284 	unsigned long wchan, size, offset;
285 	char namebuf[KSYM_NAME_LEN+1];
286 
287 	wchan = get_wchan(task);
288 
289 	sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
290 	if (sym_name)
291 		return sprintf(buffer, "%s", sym_name);
292 	return sprintf(buffer, "%lu", wchan);
293 }
294 #endif /* CONFIG_KALLSYMS */
295 
296 #ifdef CONFIG_SCHEDSTATS
297 /*
298  * Provides /proc/PID/schedstat
299  */
300 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
301 {
302 	return sprintf(buffer, "%lu %lu %lu\n",
303 			task->sched_info.cpu_time,
304 			task->sched_info.run_delay,
305 			task->sched_info.pcnt);
306 }
307 #endif
308 
309 /* The badness from the OOM killer */
310 unsigned long badness(struct task_struct *p, unsigned long uptime);
311 static int proc_oom_score(struct task_struct *task, char *buffer)
312 {
313 	unsigned long points;
314 	struct timespec uptime;
315 
316 	do_posix_clock_monotonic_gettime(&uptime);
317 	read_lock(&tasklist_lock);
318 	points = badness(task, uptime.tv_sec);
319 	read_unlock(&tasklist_lock);
320 	return sprintf(buffer, "%lu\n", points);
321 }
322 
323 /************************************************************************/
324 /*                       Here the fs part begins                        */
325 /************************************************************************/
326 
327 /* permission checks */
328 static int proc_fd_access_allowed(struct inode *inode)
329 {
330 	struct task_struct *task;
331 	int allowed = 0;
332 	/* Allow access to a task's file descriptors if it is us or we
333 	 * may use ptrace attach to the process and find out that
334 	 * information.
335 	 */
336 	task = get_proc_task(inode);
337 	if (task) {
338 		allowed = ptrace_may_attach(task);
339 		put_task_struct(task);
340 	}
341 	return allowed;
342 }
343 
344 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
345 {
346 	int error;
347 	struct inode *inode = dentry->d_inode;
348 
349 	if (attr->ia_valid & ATTR_MODE)
350 		return -EPERM;
351 
352 	error = inode_change_ok(inode, attr);
353 	if (!error) {
354 		error = security_inode_setattr(dentry, attr);
355 		if (!error)
356 			error = inode_setattr(inode, attr);
357 	}
358 	return error;
359 }
360 
361 static const struct inode_operations proc_def_inode_operations = {
362 	.setattr	= proc_setattr,
363 };
364 
365 extern struct seq_operations mounts_op;
366 struct proc_mounts {
367 	struct seq_file m;
368 	int event;
369 };
370 
371 static int mounts_open(struct inode *inode, struct file *file)
372 {
373 	struct task_struct *task = get_proc_task(inode);
374 	struct mnt_namespace *ns = NULL;
375 	struct proc_mounts *p;
376 	int ret = -EINVAL;
377 
378 	if (task) {
379 		task_lock(task);
380 		if (task->nsproxy) {
381 			ns = task->nsproxy->mnt_ns;
382 			if (ns)
383 				get_mnt_ns(ns);
384 		}
385 		task_unlock(task);
386 		put_task_struct(task);
387 	}
388 
389 	if (ns) {
390 		ret = -ENOMEM;
391 		p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
392 		if (p) {
393 			file->private_data = &p->m;
394 			ret = seq_open(file, &mounts_op);
395 			if (!ret) {
396 				p->m.private = ns;
397 				p->event = ns->event;
398 				return 0;
399 			}
400 			kfree(p);
401 		}
402 		put_mnt_ns(ns);
403 	}
404 	return ret;
405 }
406 
407 static int mounts_release(struct inode *inode, struct file *file)
408 {
409 	struct seq_file *m = file->private_data;
410 	struct mnt_namespace *ns = m->private;
411 	put_mnt_ns(ns);
412 	return seq_release(inode, file);
413 }
414 
415 static unsigned mounts_poll(struct file *file, poll_table *wait)
416 {
417 	struct proc_mounts *p = file->private_data;
418 	struct mnt_namespace *ns = p->m.private;
419 	unsigned res = 0;
420 
421 	poll_wait(file, &ns->poll, wait);
422 
423 	spin_lock(&vfsmount_lock);
424 	if (p->event != ns->event) {
425 		p->event = ns->event;
426 		res = POLLERR;
427 	}
428 	spin_unlock(&vfsmount_lock);
429 
430 	return res;
431 }
432 
433 static const struct file_operations proc_mounts_operations = {
434 	.open		= mounts_open,
435 	.read		= seq_read,
436 	.llseek		= seq_lseek,
437 	.release	= mounts_release,
438 	.poll		= mounts_poll,
439 };
440 
441 extern struct seq_operations mountstats_op;
442 static int mountstats_open(struct inode *inode, struct file *file)
443 {
444 	int ret = seq_open(file, &mountstats_op);
445 
446 	if (!ret) {
447 		struct seq_file *m = file->private_data;
448 		struct mnt_namespace *mnt_ns = NULL;
449 		struct task_struct *task = get_proc_task(inode);
450 
451 		if (task) {
452 			task_lock(task);
453 			if (task->nsproxy)
454 				mnt_ns = task->nsproxy->mnt_ns;
455 			if (mnt_ns)
456 				get_mnt_ns(mnt_ns);
457 			task_unlock(task);
458 			put_task_struct(task);
459 		}
460 
461 		if (mnt_ns)
462 			m->private = mnt_ns;
463 		else {
464 			seq_release(inode, file);
465 			ret = -EINVAL;
466 		}
467 	}
468 	return ret;
469 }
470 
471 static const struct file_operations proc_mountstats_operations = {
472 	.open		= mountstats_open,
473 	.read		= seq_read,
474 	.llseek		= seq_lseek,
475 	.release	= mounts_release,
476 };
477 
478 #define PROC_BLOCK_SIZE	(3*1024)		/* 4K page size but our output routines use some slack for overruns */
479 
480 static ssize_t proc_info_read(struct file * file, char __user * buf,
481 			  size_t count, loff_t *ppos)
482 {
483 	struct inode * inode = file->f_path.dentry->d_inode;
484 	unsigned long page;
485 	ssize_t length;
486 	struct task_struct *task = get_proc_task(inode);
487 
488 	length = -ESRCH;
489 	if (!task)
490 		goto out_no_task;
491 
492 	if (count > PROC_BLOCK_SIZE)
493 		count = PROC_BLOCK_SIZE;
494 
495 	length = -ENOMEM;
496 	if (!(page = __get_free_page(GFP_KERNEL)))
497 		goto out;
498 
499 	length = PROC_I(inode)->op.proc_read(task, (char*)page);
500 
501 	if (length >= 0)
502 		length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
503 	free_page(page);
504 out:
505 	put_task_struct(task);
506 out_no_task:
507 	return length;
508 }
509 
510 static const struct file_operations proc_info_file_operations = {
511 	.read		= proc_info_read,
512 };
513 
514 static int mem_open(struct inode* inode, struct file* file)
515 {
516 	file->private_data = (void*)((long)current->self_exec_id);
517 	return 0;
518 }
519 
520 static ssize_t mem_read(struct file * file, char __user * buf,
521 			size_t count, loff_t *ppos)
522 {
523 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
524 	char *page;
525 	unsigned long src = *ppos;
526 	int ret = -ESRCH;
527 	struct mm_struct *mm;
528 
529 	if (!task)
530 		goto out_no_task;
531 
532 	if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
533 		goto out;
534 
535 	ret = -ENOMEM;
536 	page = (char *)__get_free_page(GFP_USER);
537 	if (!page)
538 		goto out;
539 
540 	ret = 0;
541 
542 	mm = get_task_mm(task);
543 	if (!mm)
544 		goto out_free;
545 
546 	ret = -EIO;
547 
548 	if (file->private_data != (void*)((long)current->self_exec_id))
549 		goto out_put;
550 
551 	ret = 0;
552 
553 	while (count > 0) {
554 		int this_len, retval;
555 
556 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
557 		retval = access_process_vm(task, src, page, this_len, 0);
558 		if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
559 			if (!ret)
560 				ret = -EIO;
561 			break;
562 		}
563 
564 		if (copy_to_user(buf, page, retval)) {
565 			ret = -EFAULT;
566 			break;
567 		}
568 
569 		ret += retval;
570 		src += retval;
571 		buf += retval;
572 		count -= retval;
573 	}
574 	*ppos = src;
575 
576 out_put:
577 	mmput(mm);
578 out_free:
579 	free_page((unsigned long) page);
580 out:
581 	put_task_struct(task);
582 out_no_task:
583 	return ret;
584 }
585 
586 #define mem_write NULL
587 
588 #ifndef mem_write
589 /* This is a security hazard */
590 static ssize_t mem_write(struct file * file, const char __user *buf,
591 			 size_t count, loff_t *ppos)
592 {
593 	int copied;
594 	char *page;
595 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
596 	unsigned long dst = *ppos;
597 
598 	copied = -ESRCH;
599 	if (!task)
600 		goto out_no_task;
601 
602 	if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
603 		goto out;
604 
605 	copied = -ENOMEM;
606 	page = (char *)__get_free_page(GFP_USER);
607 	if (!page)
608 		goto out;
609 
610 	copied = 0;
611 	while (count > 0) {
612 		int this_len, retval;
613 
614 		this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
615 		if (copy_from_user(page, buf, this_len)) {
616 			copied = -EFAULT;
617 			break;
618 		}
619 		retval = access_process_vm(task, dst, page, this_len, 1);
620 		if (!retval) {
621 			if (!copied)
622 				copied = -EIO;
623 			break;
624 		}
625 		copied += retval;
626 		buf += retval;
627 		dst += retval;
628 		count -= retval;
629 	}
630 	*ppos = dst;
631 	free_page((unsigned long) page);
632 out:
633 	put_task_struct(task);
634 out_no_task:
635 	return copied;
636 }
637 #endif
638 
639 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
640 {
641 	switch (orig) {
642 	case 0:
643 		file->f_pos = offset;
644 		break;
645 	case 1:
646 		file->f_pos += offset;
647 		break;
648 	default:
649 		return -EINVAL;
650 	}
651 	force_successful_syscall_return();
652 	return file->f_pos;
653 }
654 
655 static const struct file_operations proc_mem_operations = {
656 	.llseek		= mem_lseek,
657 	.read		= mem_read,
658 	.write		= mem_write,
659 	.open		= mem_open,
660 };
661 
662 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
663 				size_t count, loff_t *ppos)
664 {
665 	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
666 	char buffer[PROC_NUMBUF];
667 	size_t len;
668 	int oom_adjust;
669 	loff_t __ppos = *ppos;
670 
671 	if (!task)
672 		return -ESRCH;
673 	oom_adjust = task->oomkilladj;
674 	put_task_struct(task);
675 
676 	len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
677 	if (__ppos >= len)
678 		return 0;
679 	if (count > len-__ppos)
680 		count = len-__ppos;
681 	if (copy_to_user(buf, buffer + __ppos, count))
682 		return -EFAULT;
683 	*ppos = __ppos + count;
684 	return count;
685 }
686 
687 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
688 				size_t count, loff_t *ppos)
689 {
690 	struct task_struct *task;
691 	char buffer[PROC_NUMBUF], *end;
692 	int oom_adjust;
693 
694 	memset(buffer, 0, sizeof(buffer));
695 	if (count > sizeof(buffer) - 1)
696 		count = sizeof(buffer) - 1;
697 	if (copy_from_user(buffer, buf, count))
698 		return -EFAULT;
699 	oom_adjust = simple_strtol(buffer, &end, 0);
700 	if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
701 	     oom_adjust != OOM_DISABLE)
702 		return -EINVAL;
703 	if (*end == '\n')
704 		end++;
705 	task = get_proc_task(file->f_path.dentry->d_inode);
706 	if (!task)
707 		return -ESRCH;
708 	if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
709 		put_task_struct(task);
710 		return -EACCES;
711 	}
712 	task->oomkilladj = oom_adjust;
713 	put_task_struct(task);
714 	if (end - buffer == 0)
715 		return -EIO;
716 	return end - buffer;
717 }
718 
719 static const struct file_operations proc_oom_adjust_operations = {
720 	.read		= oom_adjust_read,
721 	.write		= oom_adjust_write,
722 };
723 
724 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
725 				size_t count, loff_t *ppos)
726 {
727 	struct task_struct *task;
728 	char buffer[PROC_NUMBUF], *end;
729 	struct mm_struct *mm;
730 
731 	memset(buffer, 0, sizeof(buffer));
732 	if (count > sizeof(buffer) - 1)
733 		count = sizeof(buffer) - 1;
734 	if (copy_from_user(buffer, buf, count))
735 		return -EFAULT;
736 	if (!simple_strtol(buffer, &end, 0))
737 		return -EINVAL;
738 	if (*end == '\n')
739 		end++;
740 	task = get_proc_task(file->f_path.dentry->d_inode);
741 	if (!task)
742 		return -ESRCH;
743 	mm = get_task_mm(task);
744 	if (mm) {
745 		clear_refs_smap(mm);
746 		mmput(mm);
747 	}
748 	put_task_struct(task);
749 	if (end - buffer == 0)
750 		return -EIO;
751 	return end - buffer;
752 }
753 
754 static struct file_operations proc_clear_refs_operations = {
755 	.write		= clear_refs_write,
756 };
757 
758 #ifdef CONFIG_AUDITSYSCALL
759 #define TMPBUFLEN 21
760 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
761 				  size_t count, loff_t *ppos)
762 {
763 	struct inode * inode = file->f_path.dentry->d_inode;
764 	struct task_struct *task = get_proc_task(inode);
765 	ssize_t length;
766 	char tmpbuf[TMPBUFLEN];
767 
768 	if (!task)
769 		return -ESRCH;
770 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
771 				audit_get_loginuid(task->audit_context));
772 	put_task_struct(task);
773 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
774 }
775 
776 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
777 				   size_t count, loff_t *ppos)
778 {
779 	struct inode * inode = file->f_path.dentry->d_inode;
780 	char *page, *tmp;
781 	ssize_t length;
782 	uid_t loginuid;
783 
784 	if (!capable(CAP_AUDIT_CONTROL))
785 		return -EPERM;
786 
787 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
788 		return -EPERM;
789 
790 	if (count >= PAGE_SIZE)
791 		count = PAGE_SIZE - 1;
792 
793 	if (*ppos != 0) {
794 		/* No partial writes. */
795 		return -EINVAL;
796 	}
797 	page = (char*)__get_free_page(GFP_USER);
798 	if (!page)
799 		return -ENOMEM;
800 	length = -EFAULT;
801 	if (copy_from_user(page, buf, count))
802 		goto out_free_page;
803 
804 	page[count] = '\0';
805 	loginuid = simple_strtoul(page, &tmp, 10);
806 	if (tmp == page) {
807 		length = -EINVAL;
808 		goto out_free_page;
809 
810 	}
811 	length = audit_set_loginuid(current, loginuid);
812 	if (likely(length == 0))
813 		length = count;
814 
815 out_free_page:
816 	free_page((unsigned long) page);
817 	return length;
818 }
819 
820 static const struct file_operations proc_loginuid_operations = {
821 	.read		= proc_loginuid_read,
822 	.write		= proc_loginuid_write,
823 };
824 #endif
825 
826 #ifdef CONFIG_SECCOMP
827 static ssize_t seccomp_read(struct file *file, char __user *buf,
828 			    size_t count, loff_t *ppos)
829 {
830 	struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
831 	char __buf[20];
832 	loff_t __ppos = *ppos;
833 	size_t len;
834 
835 	if (!tsk)
836 		return -ESRCH;
837 	/* no need to print the trailing zero, so use only len */
838 	len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
839 	put_task_struct(tsk);
840 	if (__ppos >= len)
841 		return 0;
842 	if (count > len - __ppos)
843 		count = len - __ppos;
844 	if (copy_to_user(buf, __buf + __ppos, count))
845 		return -EFAULT;
846 	*ppos = __ppos + count;
847 	return count;
848 }
849 
850 static ssize_t seccomp_write(struct file *file, const char __user *buf,
851 			     size_t count, loff_t *ppos)
852 {
853 	struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
854 	char __buf[20], *end;
855 	unsigned int seccomp_mode;
856 	ssize_t result;
857 
858 	result = -ESRCH;
859 	if (!tsk)
860 		goto out_no_task;
861 
862 	/* can set it only once to be even more secure */
863 	result = -EPERM;
864 	if (unlikely(tsk->seccomp.mode))
865 		goto out;
866 
867 	result = -EFAULT;
868 	memset(__buf, 0, sizeof(__buf));
869 	count = min(count, sizeof(__buf) - 1);
870 	if (copy_from_user(__buf, buf, count))
871 		goto out;
872 
873 	seccomp_mode = simple_strtoul(__buf, &end, 0);
874 	if (*end == '\n')
875 		end++;
876 	result = -EINVAL;
877 	if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
878 		tsk->seccomp.mode = seccomp_mode;
879 		set_tsk_thread_flag(tsk, TIF_SECCOMP);
880 	} else
881 		goto out;
882 	result = -EIO;
883 	if (unlikely(!(end - __buf)))
884 		goto out;
885 	result = end - __buf;
886 out:
887 	put_task_struct(tsk);
888 out_no_task:
889 	return result;
890 }
891 
892 static const struct file_operations proc_seccomp_operations = {
893 	.read		= seccomp_read,
894 	.write		= seccomp_write,
895 };
896 #endif /* CONFIG_SECCOMP */
897 
898 #ifdef CONFIG_FAULT_INJECTION
899 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
900 				      size_t count, loff_t *ppos)
901 {
902 	struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
903 	char buffer[PROC_NUMBUF];
904 	size_t len;
905 	int make_it_fail;
906 	loff_t __ppos = *ppos;
907 
908 	if (!task)
909 		return -ESRCH;
910 	make_it_fail = task->make_it_fail;
911 	put_task_struct(task);
912 
913 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
914 	if (__ppos >= len)
915 		return 0;
916 	if (count > len-__ppos)
917 		count = len-__ppos;
918 	if (copy_to_user(buf, buffer + __ppos, count))
919 		return -EFAULT;
920 	*ppos = __ppos + count;
921 	return count;
922 }
923 
924 static ssize_t proc_fault_inject_write(struct file * file,
925 			const char __user * buf, size_t count, loff_t *ppos)
926 {
927 	struct task_struct *task;
928 	char buffer[PROC_NUMBUF], *end;
929 	int make_it_fail;
930 
931 	if (!capable(CAP_SYS_RESOURCE))
932 		return -EPERM;
933 	memset(buffer, 0, sizeof(buffer));
934 	if (count > sizeof(buffer) - 1)
935 		count = sizeof(buffer) - 1;
936 	if (copy_from_user(buffer, buf, count))
937 		return -EFAULT;
938 	make_it_fail = simple_strtol(buffer, &end, 0);
939 	if (*end == '\n')
940 		end++;
941 	task = get_proc_task(file->f_dentry->d_inode);
942 	if (!task)
943 		return -ESRCH;
944 	task->make_it_fail = make_it_fail;
945 	put_task_struct(task);
946 	if (end - buffer == 0)
947 		return -EIO;
948 	return end - buffer;
949 }
950 
951 static const struct file_operations proc_fault_inject_operations = {
952 	.read		= proc_fault_inject_read,
953 	.write		= proc_fault_inject_write,
954 };
955 #endif
956 
957 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
958 {
959 	struct inode *inode = dentry->d_inode;
960 	int error = -EACCES;
961 
962 	/* We don't need a base pointer in the /proc filesystem */
963 	path_release(nd);
964 
965 	/* Are we allowed to snoop on the tasks file descriptors? */
966 	if (!proc_fd_access_allowed(inode))
967 		goto out;
968 
969 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
970 	nd->last_type = LAST_BIND;
971 out:
972 	return ERR_PTR(error);
973 }
974 
975 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
976 			    char __user *buffer, int buflen)
977 {
978 	struct inode * inode;
979 	char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
980 	int len;
981 
982 	if (!tmp)
983 		return -ENOMEM;
984 
985 	inode = dentry->d_inode;
986 	path = d_path(dentry, mnt, tmp, PAGE_SIZE);
987 	len = PTR_ERR(path);
988 	if (IS_ERR(path))
989 		goto out;
990 	len = tmp + PAGE_SIZE - 1 - path;
991 
992 	if (len > buflen)
993 		len = buflen;
994 	if (copy_to_user(buffer, path, len))
995 		len = -EFAULT;
996  out:
997 	free_page((unsigned long)tmp);
998 	return len;
999 }
1000 
1001 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1002 {
1003 	int error = -EACCES;
1004 	struct inode *inode = dentry->d_inode;
1005 	struct dentry *de;
1006 	struct vfsmount *mnt = NULL;
1007 
1008 	/* Are we allowed to snoop on the tasks file descriptors? */
1009 	if (!proc_fd_access_allowed(inode))
1010 		goto out;
1011 
1012 	error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1013 	if (error)
1014 		goto out;
1015 
1016 	error = do_proc_readlink(de, mnt, buffer, buflen);
1017 	dput(de);
1018 	mntput(mnt);
1019 out:
1020 	return error;
1021 }
1022 
1023 static const struct inode_operations proc_pid_link_inode_operations = {
1024 	.readlink	= proc_pid_readlink,
1025 	.follow_link	= proc_pid_follow_link,
1026 	.setattr	= proc_setattr,
1027 };
1028 
1029 
1030 /* building an inode */
1031 
1032 static int task_dumpable(struct task_struct *task)
1033 {
1034 	int dumpable = 0;
1035 	struct mm_struct *mm;
1036 
1037 	task_lock(task);
1038 	mm = task->mm;
1039 	if (mm)
1040 		dumpable = mm->dumpable;
1041 	task_unlock(task);
1042 	if(dumpable == 1)
1043 		return 1;
1044 	return 0;
1045 }
1046 
1047 
1048 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1049 {
1050 	struct inode * inode;
1051 	struct proc_inode *ei;
1052 
1053 	/* We need a new inode */
1054 
1055 	inode = new_inode(sb);
1056 	if (!inode)
1057 		goto out;
1058 
1059 	/* Common stuff */
1060 	ei = PROC_I(inode);
1061 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1062 	inode->i_op = &proc_def_inode_operations;
1063 
1064 	/*
1065 	 * grab the reference to task.
1066 	 */
1067 	ei->pid = get_task_pid(task, PIDTYPE_PID);
1068 	if (!ei->pid)
1069 		goto out_unlock;
1070 
1071 	inode->i_uid = 0;
1072 	inode->i_gid = 0;
1073 	if (task_dumpable(task)) {
1074 		inode->i_uid = task->euid;
1075 		inode->i_gid = task->egid;
1076 	}
1077 	security_task_to_inode(task, inode);
1078 
1079 out:
1080 	return inode;
1081 
1082 out_unlock:
1083 	iput(inode);
1084 	return NULL;
1085 }
1086 
1087 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1088 {
1089 	struct inode *inode = dentry->d_inode;
1090 	struct task_struct *task;
1091 	generic_fillattr(inode, stat);
1092 
1093 	rcu_read_lock();
1094 	stat->uid = 0;
1095 	stat->gid = 0;
1096 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
1097 	if (task) {
1098 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1099 		    task_dumpable(task)) {
1100 			stat->uid = task->euid;
1101 			stat->gid = task->egid;
1102 		}
1103 	}
1104 	rcu_read_unlock();
1105 	return 0;
1106 }
1107 
1108 /* dentry stuff */
1109 
1110 /*
1111  *	Exceptional case: normally we are not allowed to unhash a busy
1112  * directory. In this case, however, we can do it - no aliasing problems
1113  * due to the way we treat inodes.
1114  *
1115  * Rewrite the inode's ownerships here because the owning task may have
1116  * performed a setuid(), etc.
1117  *
1118  * Before the /proc/pid/status file was created the only way to read
1119  * the effective uid of a /process was to stat /proc/pid.  Reading
1120  * /proc/pid/status is slow enough that procps and other packages
1121  * kept stating /proc/pid.  To keep the rules in /proc simple I have
1122  * made this apply to all per process world readable and executable
1123  * directories.
1124  */
1125 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1126 {
1127 	struct inode *inode = dentry->d_inode;
1128 	struct task_struct *task = get_proc_task(inode);
1129 	if (task) {
1130 		if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1131 		    task_dumpable(task)) {
1132 			inode->i_uid = task->euid;
1133 			inode->i_gid = task->egid;
1134 		} else {
1135 			inode->i_uid = 0;
1136 			inode->i_gid = 0;
1137 		}
1138 		inode->i_mode &= ~(S_ISUID | S_ISGID);
1139 		security_task_to_inode(task, inode);
1140 		put_task_struct(task);
1141 		return 1;
1142 	}
1143 	d_drop(dentry);
1144 	return 0;
1145 }
1146 
1147 static int pid_delete_dentry(struct dentry * dentry)
1148 {
1149 	/* Is the task we represent dead?
1150 	 * If so, then don't put the dentry on the lru list,
1151 	 * kill it immediately.
1152 	 */
1153 	return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1154 }
1155 
1156 static struct dentry_operations pid_dentry_operations =
1157 {
1158 	.d_revalidate	= pid_revalidate,
1159 	.d_delete	= pid_delete_dentry,
1160 };
1161 
1162 /* Lookups */
1163 
1164 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1165 				struct task_struct *, const void *);
1166 
1167 /*
1168  * Fill a directory entry.
1169  *
1170  * If possible create the dcache entry and derive our inode number and
1171  * file type from dcache entry.
1172  *
1173  * Since all of the proc inode numbers are dynamically generated, the inode
1174  * numbers do not exist until the inode is cache.  This means creating the
1175  * the dcache entry in readdir is necessary to keep the inode numbers
1176  * reported by readdir in sync with the inode numbers reported
1177  * by stat.
1178  */
1179 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1180 	char *name, int len,
1181 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
1182 {
1183 	struct dentry *child, *dir = filp->f_path.dentry;
1184 	struct inode *inode;
1185 	struct qstr qname;
1186 	ino_t ino = 0;
1187 	unsigned type = DT_UNKNOWN;
1188 
1189 	qname.name = name;
1190 	qname.len  = len;
1191 	qname.hash = full_name_hash(name, len);
1192 
1193 	child = d_lookup(dir, &qname);
1194 	if (!child) {
1195 		struct dentry *new;
1196 		new = d_alloc(dir, &qname);
1197 		if (new) {
1198 			child = instantiate(dir->d_inode, new, task, ptr);
1199 			if (child)
1200 				dput(new);
1201 			else
1202 				child = new;
1203 		}
1204 	}
1205 	if (!child || IS_ERR(child) || !child->d_inode)
1206 		goto end_instantiate;
1207 	inode = child->d_inode;
1208 	if (inode) {
1209 		ino = inode->i_ino;
1210 		type = inode->i_mode >> 12;
1211 	}
1212 	dput(child);
1213 end_instantiate:
1214 	if (!ino)
1215 		ino = find_inode_number(dir, &qname);
1216 	if (!ino)
1217 		ino = 1;
1218 	return filldir(dirent, name, len, filp->f_pos, ino, type);
1219 }
1220 
1221 static unsigned name_to_int(struct dentry *dentry)
1222 {
1223 	const char *name = dentry->d_name.name;
1224 	int len = dentry->d_name.len;
1225 	unsigned n = 0;
1226 
1227 	if (len > 1 && *name == '0')
1228 		goto out;
1229 	while (len-- > 0) {
1230 		unsigned c = *name++ - '0';
1231 		if (c > 9)
1232 			goto out;
1233 		if (n >= (~0U-9)/10)
1234 			goto out;
1235 		n *= 10;
1236 		n += c;
1237 	}
1238 	return n;
1239 out:
1240 	return ~0U;
1241 }
1242 
1243 #define PROC_FDINFO_MAX 64
1244 
1245 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1246 			struct vfsmount **mnt, char *info)
1247 {
1248 	struct task_struct *task = get_proc_task(inode);
1249 	struct files_struct *files = NULL;
1250 	struct file *file;
1251 	int fd = proc_fd(inode);
1252 
1253 	if (task) {
1254 		files = get_files_struct(task);
1255 		put_task_struct(task);
1256 	}
1257 	if (files) {
1258 		/*
1259 		 * We are not taking a ref to the file structure, so we must
1260 		 * hold ->file_lock.
1261 		 */
1262 		spin_lock(&files->file_lock);
1263 		file = fcheck_files(files, fd);
1264 		if (file) {
1265 			if (mnt)
1266 				*mnt = mntget(file->f_path.mnt);
1267 			if (dentry)
1268 				*dentry = dget(file->f_path.dentry);
1269 			if (info)
1270 				snprintf(info, PROC_FDINFO_MAX,
1271 					 "pos:\t%lli\n"
1272 					 "flags:\t0%o\n",
1273 					 (long long) file->f_pos,
1274 					 file->f_flags);
1275 			spin_unlock(&files->file_lock);
1276 			put_files_struct(files);
1277 			return 0;
1278 		}
1279 		spin_unlock(&files->file_lock);
1280 		put_files_struct(files);
1281 	}
1282 	return -ENOENT;
1283 }
1284 
1285 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1286 			struct vfsmount **mnt)
1287 {
1288 	return proc_fd_info(inode, dentry, mnt, NULL);
1289 }
1290 
1291 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1292 {
1293 	struct inode *inode = dentry->d_inode;
1294 	struct task_struct *task = get_proc_task(inode);
1295 	int fd = proc_fd(inode);
1296 	struct files_struct *files;
1297 
1298 	if (task) {
1299 		files = get_files_struct(task);
1300 		if (files) {
1301 			rcu_read_lock();
1302 			if (fcheck_files(files, fd)) {
1303 				rcu_read_unlock();
1304 				put_files_struct(files);
1305 				if (task_dumpable(task)) {
1306 					inode->i_uid = task->euid;
1307 					inode->i_gid = task->egid;
1308 				} else {
1309 					inode->i_uid = 0;
1310 					inode->i_gid = 0;
1311 				}
1312 				inode->i_mode &= ~(S_ISUID | S_ISGID);
1313 				security_task_to_inode(task, inode);
1314 				put_task_struct(task);
1315 				return 1;
1316 			}
1317 			rcu_read_unlock();
1318 			put_files_struct(files);
1319 		}
1320 		put_task_struct(task);
1321 	}
1322 	d_drop(dentry);
1323 	return 0;
1324 }
1325 
1326 static struct dentry_operations tid_fd_dentry_operations =
1327 {
1328 	.d_revalidate	= tid_fd_revalidate,
1329 	.d_delete	= pid_delete_dentry,
1330 };
1331 
1332 static struct dentry *proc_fd_instantiate(struct inode *dir,
1333 	struct dentry *dentry, struct task_struct *task, const void *ptr)
1334 {
1335 	unsigned fd = *(const unsigned *)ptr;
1336 	struct file *file;
1337 	struct files_struct *files;
1338  	struct inode *inode;
1339  	struct proc_inode *ei;
1340 	struct dentry *error = ERR_PTR(-ENOENT);
1341 
1342 	inode = proc_pid_make_inode(dir->i_sb, task);
1343 	if (!inode)
1344 		goto out;
1345 	ei = PROC_I(inode);
1346 	ei->fd = fd;
1347 	files = get_files_struct(task);
1348 	if (!files)
1349 		goto out_iput;
1350 	inode->i_mode = S_IFLNK;
1351 
1352 	/*
1353 	 * We are not taking a ref to the file structure, so we must
1354 	 * hold ->file_lock.
1355 	 */
1356 	spin_lock(&files->file_lock);
1357 	file = fcheck_files(files, fd);
1358 	if (!file)
1359 		goto out_unlock;
1360 	if (file->f_mode & 1)
1361 		inode->i_mode |= S_IRUSR | S_IXUSR;
1362 	if (file->f_mode & 2)
1363 		inode->i_mode |= S_IWUSR | S_IXUSR;
1364 	spin_unlock(&files->file_lock);
1365 	put_files_struct(files);
1366 
1367 	inode->i_op = &proc_pid_link_inode_operations;
1368 	inode->i_size = 64;
1369 	ei->op.proc_get_link = proc_fd_link;
1370 	dentry->d_op = &tid_fd_dentry_operations;
1371 	d_add(dentry, inode);
1372 	/* Close the race of the process dying before we return the dentry */
1373 	if (tid_fd_revalidate(dentry, NULL))
1374 		error = NULL;
1375 
1376  out:
1377 	return error;
1378 out_unlock:
1379 	spin_unlock(&files->file_lock);
1380 	put_files_struct(files);
1381 out_iput:
1382 	iput(inode);
1383 	goto out;
1384 }
1385 
1386 static struct dentry *proc_lookupfd_common(struct inode *dir,
1387 					   struct dentry *dentry,
1388 					   instantiate_t instantiate)
1389 {
1390 	struct task_struct *task = get_proc_task(dir);
1391 	unsigned fd = name_to_int(dentry);
1392 	struct dentry *result = ERR_PTR(-ENOENT);
1393 
1394 	if (!task)
1395 		goto out_no_task;
1396 	if (fd == ~0U)
1397 		goto out;
1398 
1399 	result = instantiate(dir, dentry, task, &fd);
1400 out:
1401 	put_task_struct(task);
1402 out_no_task:
1403 	return result;
1404 }
1405 
1406 static int proc_readfd_common(struct file * filp, void * dirent,
1407 			      filldir_t filldir, instantiate_t instantiate)
1408 {
1409 	struct dentry *dentry = filp->f_path.dentry;
1410 	struct inode *inode = dentry->d_inode;
1411 	struct task_struct *p = get_proc_task(inode);
1412 	unsigned int fd, tid, ino;
1413 	int retval;
1414 	struct files_struct * files;
1415 	struct fdtable *fdt;
1416 
1417 	retval = -ENOENT;
1418 	if (!p)
1419 		goto out_no_task;
1420 	retval = 0;
1421 	tid = p->pid;
1422 
1423 	fd = filp->f_pos;
1424 	switch (fd) {
1425 		case 0:
1426 			if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1427 				goto out;
1428 			filp->f_pos++;
1429 		case 1:
1430 			ino = parent_ino(dentry);
1431 			if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1432 				goto out;
1433 			filp->f_pos++;
1434 		default:
1435 			files = get_files_struct(p);
1436 			if (!files)
1437 				goto out;
1438 			rcu_read_lock();
1439 			fdt = files_fdtable(files);
1440 			for (fd = filp->f_pos-2;
1441 			     fd < fdt->max_fds;
1442 			     fd++, filp->f_pos++) {
1443 				char name[PROC_NUMBUF];
1444 				int len;
1445 
1446 				if (!fcheck_files(files, fd))
1447 					continue;
1448 				rcu_read_unlock();
1449 
1450 				len = snprintf(name, sizeof(name), "%d", fd);
1451 				if (proc_fill_cache(filp, dirent, filldir,
1452 						    name, len, instantiate,
1453 						    p, &fd) < 0) {
1454 					rcu_read_lock();
1455 					break;
1456 				}
1457 				rcu_read_lock();
1458 			}
1459 			rcu_read_unlock();
1460 			put_files_struct(files);
1461 	}
1462 out:
1463 	put_task_struct(p);
1464 out_no_task:
1465 	return retval;
1466 }
1467 
1468 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1469 				    struct nameidata *nd)
1470 {
1471 	return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1472 }
1473 
1474 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1475 {
1476 	return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1477 }
1478 
1479 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1480 				      size_t len, loff_t *ppos)
1481 {
1482 	char tmp[PROC_FDINFO_MAX];
1483 	int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1484 	if (!err)
1485 		err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1486 	return err;
1487 }
1488 
1489 static const struct file_operations proc_fdinfo_file_operations = {
1490 	.open		= nonseekable_open,
1491 	.read		= proc_fdinfo_read,
1492 };
1493 
1494 static const struct file_operations proc_fd_operations = {
1495 	.read		= generic_read_dir,
1496 	.readdir	= proc_readfd,
1497 };
1498 
1499 /*
1500  * /proc/pid/fd needs a special permission handler so that a process can still
1501  * access /proc/self/fd after it has executed a setuid().
1502  */
1503 static int proc_fd_permission(struct inode *inode, int mask,
1504 				struct nameidata *nd)
1505 {
1506 	int rv;
1507 
1508 	rv = generic_permission(inode, mask, NULL);
1509 	if (rv == 0)
1510 		return 0;
1511 	if (task_pid(current) == proc_pid(inode))
1512 		rv = 0;
1513 	return rv;
1514 }
1515 
1516 /*
1517  * proc directories can do almost nothing..
1518  */
1519 static const struct inode_operations proc_fd_inode_operations = {
1520 	.lookup		= proc_lookupfd,
1521 	.permission	= proc_fd_permission,
1522 	.setattr	= proc_setattr,
1523 };
1524 
1525 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1526 	struct dentry *dentry, struct task_struct *task, const void *ptr)
1527 {
1528 	unsigned fd = *(unsigned *)ptr;
1529  	struct inode *inode;
1530  	struct proc_inode *ei;
1531 	struct dentry *error = ERR_PTR(-ENOENT);
1532 
1533 	inode = proc_pid_make_inode(dir->i_sb, task);
1534 	if (!inode)
1535 		goto out;
1536 	ei = PROC_I(inode);
1537 	ei->fd = fd;
1538 	inode->i_mode = S_IFREG | S_IRUSR;
1539 	inode->i_fop = &proc_fdinfo_file_operations;
1540 	dentry->d_op = &tid_fd_dentry_operations;
1541 	d_add(dentry, inode);
1542 	/* Close the race of the process dying before we return the dentry */
1543 	if (tid_fd_revalidate(dentry, NULL))
1544 		error = NULL;
1545 
1546  out:
1547 	return error;
1548 }
1549 
1550 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1551 					struct dentry *dentry,
1552 					struct nameidata *nd)
1553 {
1554 	return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1555 }
1556 
1557 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1558 {
1559 	return proc_readfd_common(filp, dirent, filldir,
1560 				  proc_fdinfo_instantiate);
1561 }
1562 
1563 static const struct file_operations proc_fdinfo_operations = {
1564 	.read		= generic_read_dir,
1565 	.readdir	= proc_readfdinfo,
1566 };
1567 
1568 /*
1569  * proc directories can do almost nothing..
1570  */
1571 static const struct inode_operations proc_fdinfo_inode_operations = {
1572 	.lookup		= proc_lookupfdinfo,
1573 	.setattr	= proc_setattr,
1574 };
1575 
1576 
1577 static struct dentry *proc_pident_instantiate(struct inode *dir,
1578 	struct dentry *dentry, struct task_struct *task, const void *ptr)
1579 {
1580 	const struct pid_entry *p = ptr;
1581 	struct inode *inode;
1582 	struct proc_inode *ei;
1583 	struct dentry *error = ERR_PTR(-EINVAL);
1584 
1585 	inode = proc_pid_make_inode(dir->i_sb, task);
1586 	if (!inode)
1587 		goto out;
1588 
1589 	ei = PROC_I(inode);
1590 	inode->i_mode = p->mode;
1591 	if (S_ISDIR(inode->i_mode))
1592 		inode->i_nlink = 2;	/* Use getattr to fix if necessary */
1593 	if (p->iop)
1594 		inode->i_op = p->iop;
1595 	if (p->fop)
1596 		inode->i_fop = p->fop;
1597 	ei->op = p->op;
1598 	dentry->d_op = &pid_dentry_operations;
1599 	d_add(dentry, inode);
1600 	/* Close the race of the process dying before we return the dentry */
1601 	if (pid_revalidate(dentry, NULL))
1602 		error = NULL;
1603 out:
1604 	return error;
1605 }
1606 
1607 static struct dentry *proc_pident_lookup(struct inode *dir,
1608 					 struct dentry *dentry,
1609 					 const struct pid_entry *ents,
1610 					 unsigned int nents)
1611 {
1612 	struct inode *inode;
1613 	struct dentry *error;
1614 	struct task_struct *task = get_proc_task(dir);
1615 	const struct pid_entry *p, *last;
1616 
1617 	error = ERR_PTR(-ENOENT);
1618 	inode = NULL;
1619 
1620 	if (!task)
1621 		goto out_no_task;
1622 
1623 	/*
1624 	 * Yes, it does not scale. And it should not. Don't add
1625 	 * new entries into /proc/<tgid>/ without very good reasons.
1626 	 */
1627 	last = &ents[nents - 1];
1628 	for (p = ents; p <= last; p++) {
1629 		if (p->len != dentry->d_name.len)
1630 			continue;
1631 		if (!memcmp(dentry->d_name.name, p->name, p->len))
1632 			break;
1633 	}
1634 	if (p > last)
1635 		goto out;
1636 
1637 	error = proc_pident_instantiate(dir, dentry, task, p);
1638 out:
1639 	put_task_struct(task);
1640 out_no_task:
1641 	return error;
1642 }
1643 
1644 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1645 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1646 {
1647 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1648 				proc_pident_instantiate, task, p);
1649 }
1650 
1651 static int proc_pident_readdir(struct file *filp,
1652 		void *dirent, filldir_t filldir,
1653 		const struct pid_entry *ents, unsigned int nents)
1654 {
1655 	int i;
1656 	int pid;
1657 	struct dentry *dentry = filp->f_path.dentry;
1658 	struct inode *inode = dentry->d_inode;
1659 	struct task_struct *task = get_proc_task(inode);
1660 	const struct pid_entry *p, *last;
1661 	ino_t ino;
1662 	int ret;
1663 
1664 	ret = -ENOENT;
1665 	if (!task)
1666 		goto out_no_task;
1667 
1668 	ret = 0;
1669 	pid = task->pid;
1670 	i = filp->f_pos;
1671 	switch (i) {
1672 	case 0:
1673 		ino = inode->i_ino;
1674 		if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1675 			goto out;
1676 		i++;
1677 		filp->f_pos++;
1678 		/* fall through */
1679 	case 1:
1680 		ino = parent_ino(dentry);
1681 		if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1682 			goto out;
1683 		i++;
1684 		filp->f_pos++;
1685 		/* fall through */
1686 	default:
1687 		i -= 2;
1688 		if (i >= nents) {
1689 			ret = 1;
1690 			goto out;
1691 		}
1692 		p = ents + i;
1693 		last = &ents[nents - 1];
1694 		while (p <= last) {
1695 			if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1696 				goto out;
1697 			filp->f_pos++;
1698 			p++;
1699 		}
1700 	}
1701 
1702 	ret = 1;
1703 out:
1704 	put_task_struct(task);
1705 out_no_task:
1706 	return ret;
1707 }
1708 
1709 #ifdef CONFIG_SECURITY
1710 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1711 				  size_t count, loff_t *ppos)
1712 {
1713 	struct inode * inode = file->f_path.dentry->d_inode;
1714 	char *p = NULL;
1715 	ssize_t length;
1716 	struct task_struct *task = get_proc_task(inode);
1717 
1718 	if (!task)
1719 		return -ESRCH;
1720 
1721 	length = security_getprocattr(task,
1722 				      (char*)file->f_path.dentry->d_name.name,
1723 				      &p);
1724 	put_task_struct(task);
1725 	if (length > 0)
1726 		length = simple_read_from_buffer(buf, count, ppos, p, length);
1727 	kfree(p);
1728 	return length;
1729 }
1730 
1731 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1732 				   size_t count, loff_t *ppos)
1733 {
1734 	struct inode * inode = file->f_path.dentry->d_inode;
1735 	char *page;
1736 	ssize_t length;
1737 	struct task_struct *task = get_proc_task(inode);
1738 
1739 	length = -ESRCH;
1740 	if (!task)
1741 		goto out_no_task;
1742 	if (count > PAGE_SIZE)
1743 		count = PAGE_SIZE;
1744 
1745 	/* No partial writes. */
1746 	length = -EINVAL;
1747 	if (*ppos != 0)
1748 		goto out;
1749 
1750 	length = -ENOMEM;
1751 	page = (char*)__get_free_page(GFP_USER);
1752 	if (!page)
1753 		goto out;
1754 
1755 	length = -EFAULT;
1756 	if (copy_from_user(page, buf, count))
1757 		goto out_free;
1758 
1759 	length = security_setprocattr(task,
1760 				      (char*)file->f_path.dentry->d_name.name,
1761 				      (void*)page, count);
1762 out_free:
1763 	free_page((unsigned long) page);
1764 out:
1765 	put_task_struct(task);
1766 out_no_task:
1767 	return length;
1768 }
1769 
1770 static const struct file_operations proc_pid_attr_operations = {
1771 	.read		= proc_pid_attr_read,
1772 	.write		= proc_pid_attr_write,
1773 };
1774 
1775 static const struct pid_entry attr_dir_stuff[] = {
1776 	REG("current",    S_IRUGO|S_IWUGO, pid_attr),
1777 	REG("prev",       S_IRUGO,	   pid_attr),
1778 	REG("exec",       S_IRUGO|S_IWUGO, pid_attr),
1779 	REG("fscreate",   S_IRUGO|S_IWUGO, pid_attr),
1780 	REG("keycreate",  S_IRUGO|S_IWUGO, pid_attr),
1781 	REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1782 };
1783 
1784 static int proc_attr_dir_readdir(struct file * filp,
1785 			     void * dirent, filldir_t filldir)
1786 {
1787 	return proc_pident_readdir(filp,dirent,filldir,
1788 				   attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1789 }
1790 
1791 static const struct file_operations proc_attr_dir_operations = {
1792 	.read		= generic_read_dir,
1793 	.readdir	= proc_attr_dir_readdir,
1794 };
1795 
1796 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1797 				struct dentry *dentry, struct nameidata *nd)
1798 {
1799 	return proc_pident_lookup(dir, dentry,
1800 				  attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1801 }
1802 
1803 static const struct inode_operations proc_attr_dir_inode_operations = {
1804 	.lookup		= proc_attr_dir_lookup,
1805 	.getattr	= pid_getattr,
1806 	.setattr	= proc_setattr,
1807 };
1808 
1809 #endif
1810 
1811 /*
1812  * /proc/self:
1813  */
1814 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1815 			      int buflen)
1816 {
1817 	char tmp[PROC_NUMBUF];
1818 	sprintf(tmp, "%d", current->tgid);
1819 	return vfs_readlink(dentry,buffer,buflen,tmp);
1820 }
1821 
1822 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1823 {
1824 	char tmp[PROC_NUMBUF];
1825 	sprintf(tmp, "%d", current->tgid);
1826 	return ERR_PTR(vfs_follow_link(nd,tmp));
1827 }
1828 
1829 static const struct inode_operations proc_self_inode_operations = {
1830 	.readlink	= proc_self_readlink,
1831 	.follow_link	= proc_self_follow_link,
1832 };
1833 
1834 /*
1835  * proc base
1836  *
1837  * These are the directory entries in the root directory of /proc
1838  * that properly belong to the /proc filesystem, as they describe
1839  * describe something that is process related.
1840  */
1841 static const struct pid_entry proc_base_stuff[] = {
1842 	NOD("self", S_IFLNK|S_IRWXUGO,
1843 		&proc_self_inode_operations, NULL, {}),
1844 };
1845 
1846 /*
1847  *	Exceptional case: normally we are not allowed to unhash a busy
1848  * directory. In this case, however, we can do it - no aliasing problems
1849  * due to the way we treat inodes.
1850  */
1851 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1852 {
1853 	struct inode *inode = dentry->d_inode;
1854 	struct task_struct *task = get_proc_task(inode);
1855 	if (task) {
1856 		put_task_struct(task);
1857 		return 1;
1858 	}
1859 	d_drop(dentry);
1860 	return 0;
1861 }
1862 
1863 static struct dentry_operations proc_base_dentry_operations =
1864 {
1865 	.d_revalidate	= proc_base_revalidate,
1866 	.d_delete	= pid_delete_dentry,
1867 };
1868 
1869 static struct dentry *proc_base_instantiate(struct inode *dir,
1870 	struct dentry *dentry, struct task_struct *task, const void *ptr)
1871 {
1872 	const struct pid_entry *p = ptr;
1873 	struct inode *inode;
1874 	struct proc_inode *ei;
1875 	struct dentry *error = ERR_PTR(-EINVAL);
1876 
1877 	/* Allocate the inode */
1878 	error = ERR_PTR(-ENOMEM);
1879 	inode = new_inode(dir->i_sb);
1880 	if (!inode)
1881 		goto out;
1882 
1883 	/* Initialize the inode */
1884 	ei = PROC_I(inode);
1885 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1886 
1887 	/*
1888 	 * grab the reference to the task.
1889 	 */
1890 	ei->pid = get_task_pid(task, PIDTYPE_PID);
1891 	if (!ei->pid)
1892 		goto out_iput;
1893 
1894 	inode->i_uid = 0;
1895 	inode->i_gid = 0;
1896 	inode->i_mode = p->mode;
1897 	if (S_ISDIR(inode->i_mode))
1898 		inode->i_nlink = 2;
1899 	if (S_ISLNK(inode->i_mode))
1900 		inode->i_size = 64;
1901 	if (p->iop)
1902 		inode->i_op = p->iop;
1903 	if (p->fop)
1904 		inode->i_fop = p->fop;
1905 	ei->op = p->op;
1906 	dentry->d_op = &proc_base_dentry_operations;
1907 	d_add(dentry, inode);
1908 	error = NULL;
1909 out:
1910 	return error;
1911 out_iput:
1912 	iput(inode);
1913 	goto out;
1914 }
1915 
1916 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1917 {
1918 	struct dentry *error;
1919 	struct task_struct *task = get_proc_task(dir);
1920 	const struct pid_entry *p, *last;
1921 
1922 	error = ERR_PTR(-ENOENT);
1923 
1924 	if (!task)
1925 		goto out_no_task;
1926 
1927 	/* Lookup the directory entry */
1928 	last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1929 	for (p = proc_base_stuff; p <= last; p++) {
1930 		if (p->len != dentry->d_name.len)
1931 			continue;
1932 		if (!memcmp(dentry->d_name.name, p->name, p->len))
1933 			break;
1934 	}
1935 	if (p > last)
1936 		goto out;
1937 
1938 	error = proc_base_instantiate(dir, dentry, task, p);
1939 
1940 out:
1941 	put_task_struct(task);
1942 out_no_task:
1943 	return error;
1944 }
1945 
1946 static int proc_base_fill_cache(struct file *filp, void *dirent,
1947 	filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1948 {
1949 	return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1950 				proc_base_instantiate, task, p);
1951 }
1952 
1953 #ifdef CONFIG_TASK_IO_ACCOUNTING
1954 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1955 {
1956 	return sprintf(buffer,
1957 #ifdef CONFIG_TASK_XACCT
1958 			"rchar: %llu\n"
1959 			"wchar: %llu\n"
1960 			"syscr: %llu\n"
1961 			"syscw: %llu\n"
1962 #endif
1963 			"read_bytes: %llu\n"
1964 			"write_bytes: %llu\n"
1965 			"cancelled_write_bytes: %llu\n",
1966 #ifdef CONFIG_TASK_XACCT
1967 			(unsigned long long)task->rchar,
1968 			(unsigned long long)task->wchar,
1969 			(unsigned long long)task->syscr,
1970 			(unsigned long long)task->syscw,
1971 #endif
1972 			(unsigned long long)task->ioac.read_bytes,
1973 			(unsigned long long)task->ioac.write_bytes,
1974 			(unsigned long long)task->ioac.cancelled_write_bytes);
1975 }
1976 #endif
1977 
1978 /*
1979  * Thread groups
1980  */
1981 static const struct file_operations proc_task_operations;
1982 static const struct inode_operations proc_task_inode_operations;
1983 
1984 static const struct pid_entry tgid_base_stuff[] = {
1985 	DIR("task",       S_IRUGO|S_IXUGO, task),
1986 	DIR("fd",         S_IRUSR|S_IXUSR, fd),
1987 	DIR("fdinfo",     S_IRUSR|S_IXUSR, fdinfo),
1988 	INF("environ",    S_IRUSR, pid_environ),
1989 	INF("auxv",       S_IRUSR, pid_auxv),
1990 	INF("status",     S_IRUGO, pid_status),
1991 	INF("cmdline",    S_IRUGO, pid_cmdline),
1992 	INF("stat",       S_IRUGO, tgid_stat),
1993 	INF("statm",      S_IRUGO, pid_statm),
1994 	REG("maps",       S_IRUGO, maps),
1995 #ifdef CONFIG_NUMA
1996 	REG("numa_maps",  S_IRUGO, numa_maps),
1997 #endif
1998 	REG("mem",        S_IRUSR|S_IWUSR, mem),
1999 #ifdef CONFIG_SECCOMP
2000 	REG("seccomp",    S_IRUSR|S_IWUSR, seccomp),
2001 #endif
2002 	LNK("cwd",        cwd),
2003 	LNK("root",       root),
2004 	LNK("exe",        exe),
2005 	REG("mounts",     S_IRUGO, mounts),
2006 	REG("mountstats", S_IRUSR, mountstats),
2007 #ifdef CONFIG_MMU
2008 	REG("clear_refs", S_IWUSR, clear_refs),
2009 	REG("smaps",      S_IRUGO, smaps),
2010 #endif
2011 #ifdef CONFIG_SECURITY
2012 	DIR("attr",       S_IRUGO|S_IXUGO, attr_dir),
2013 #endif
2014 #ifdef CONFIG_KALLSYMS
2015 	INF("wchan",      S_IRUGO, pid_wchan),
2016 #endif
2017 #ifdef CONFIG_SCHEDSTATS
2018 	INF("schedstat",  S_IRUGO, pid_schedstat),
2019 #endif
2020 #ifdef CONFIG_CPUSETS
2021 	REG("cpuset",     S_IRUGO, cpuset),
2022 #endif
2023 	INF("oom_score",  S_IRUGO, oom_score),
2024 	REG("oom_adj",    S_IRUGO|S_IWUSR, oom_adjust),
2025 #ifdef CONFIG_AUDITSYSCALL
2026 	REG("loginuid",   S_IWUSR|S_IRUGO, loginuid),
2027 #endif
2028 #ifdef CONFIG_FAULT_INJECTION
2029 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2030 #endif
2031 #ifdef CONFIG_TASK_IO_ACCOUNTING
2032 	INF("io",	S_IRUGO, pid_io_accounting),
2033 #endif
2034 };
2035 
2036 static int proc_tgid_base_readdir(struct file * filp,
2037 			     void * dirent, filldir_t filldir)
2038 {
2039 	return proc_pident_readdir(filp,dirent,filldir,
2040 				   tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2041 }
2042 
2043 static const struct file_operations proc_tgid_base_operations = {
2044 	.read		= generic_read_dir,
2045 	.readdir	= proc_tgid_base_readdir,
2046 };
2047 
2048 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2049 	return proc_pident_lookup(dir, dentry,
2050 				  tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2051 }
2052 
2053 static const struct inode_operations proc_tgid_base_inode_operations = {
2054 	.lookup		= proc_tgid_base_lookup,
2055 	.getattr	= pid_getattr,
2056 	.setattr	= proc_setattr,
2057 };
2058 
2059 /**
2060  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
2061  *
2062  * @task: task that should be flushed.
2063  *
2064  * Looks in the dcache for
2065  * /proc/@pid
2066  * /proc/@tgid/task/@pid
2067  * if either directory is present flushes it and all of it'ts children
2068  * from the dcache.
2069  *
2070  * It is safe and reasonable to cache /proc entries for a task until
2071  * that task exits.  After that they just clog up the dcache with
2072  * useless entries, possibly causing useful dcache entries to be
2073  * flushed instead.  This routine is proved to flush those useless
2074  * dcache entries at process exit time.
2075  *
2076  * NOTE: This routine is just an optimization so it does not guarantee
2077  *       that no dcache entries will exist at process exit time it
2078  *       just makes it very unlikely that any will persist.
2079  */
2080 void proc_flush_task(struct task_struct *task)
2081 {
2082 	struct dentry *dentry, *leader, *dir;
2083 	char buf[PROC_NUMBUF];
2084 	struct qstr name;
2085 
2086 	name.name = buf;
2087 	name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2088 	dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2089 	if (dentry) {
2090 		shrink_dcache_parent(dentry);
2091 		d_drop(dentry);
2092 		dput(dentry);
2093 	}
2094 
2095 	if (thread_group_leader(task))
2096 		goto out;
2097 
2098 	name.name = buf;
2099 	name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2100 	leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2101 	if (!leader)
2102 		goto out;
2103 
2104 	name.name = "task";
2105 	name.len = strlen(name.name);
2106 	dir = d_hash_and_lookup(leader, &name);
2107 	if (!dir)
2108 		goto out_put_leader;
2109 
2110 	name.name = buf;
2111 	name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2112 	dentry = d_hash_and_lookup(dir, &name);
2113 	if (dentry) {
2114 		shrink_dcache_parent(dentry);
2115 		d_drop(dentry);
2116 		dput(dentry);
2117 	}
2118 
2119 	dput(dir);
2120 out_put_leader:
2121 	dput(leader);
2122 out:
2123 	return;
2124 }
2125 
2126 static struct dentry *proc_pid_instantiate(struct inode *dir,
2127 					   struct dentry * dentry,
2128 					   struct task_struct *task, const void *ptr)
2129 {
2130 	struct dentry *error = ERR_PTR(-ENOENT);
2131 	struct inode *inode;
2132 
2133 	inode = proc_pid_make_inode(dir->i_sb, task);
2134 	if (!inode)
2135 		goto out;
2136 
2137 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2138 	inode->i_op = &proc_tgid_base_inode_operations;
2139 	inode->i_fop = &proc_tgid_base_operations;
2140 	inode->i_flags|=S_IMMUTABLE;
2141 	inode->i_nlink = 5;
2142 #ifdef CONFIG_SECURITY
2143 	inode->i_nlink += 1;
2144 #endif
2145 
2146 	dentry->d_op = &pid_dentry_operations;
2147 
2148 	d_add(dentry, inode);
2149 	/* Close the race of the process dying before we return the dentry */
2150 	if (pid_revalidate(dentry, NULL))
2151 		error = NULL;
2152 out:
2153 	return error;
2154 }
2155 
2156 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2157 {
2158 	struct dentry *result = ERR_PTR(-ENOENT);
2159 	struct task_struct *task;
2160 	unsigned tgid;
2161 
2162 	result = proc_base_lookup(dir, dentry);
2163 	if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2164 		goto out;
2165 
2166 	tgid = name_to_int(dentry);
2167 	if (tgid == ~0U)
2168 		goto out;
2169 
2170 	rcu_read_lock();
2171 	task = find_task_by_pid(tgid);
2172 	if (task)
2173 		get_task_struct(task);
2174 	rcu_read_unlock();
2175 	if (!task)
2176 		goto out;
2177 
2178 	result = proc_pid_instantiate(dir, dentry, task, NULL);
2179 	put_task_struct(task);
2180 out:
2181 	return result;
2182 }
2183 
2184 /*
2185  * Find the first task with tgid >= tgid
2186  *
2187  */
2188 static struct task_struct *next_tgid(unsigned int tgid)
2189 {
2190 	struct task_struct *task;
2191 	struct pid *pid;
2192 
2193 	rcu_read_lock();
2194 retry:
2195 	task = NULL;
2196 	pid = find_ge_pid(tgid);
2197 	if (pid) {
2198 		tgid = pid->nr + 1;
2199 		task = pid_task(pid, PIDTYPE_PID);
2200 		/* What we to know is if the pid we have find is the
2201 		 * pid of a thread_group_leader.  Testing for task
2202 		 * being a thread_group_leader is the obvious thing
2203 		 * todo but there is a window when it fails, due to
2204 		 * the pid transfer logic in de_thread.
2205 		 *
2206 		 * So we perform the straight forward test of seeing
2207 		 * if the pid we have found is the pid of a thread
2208 		 * group leader, and don't worry if the task we have
2209 		 * found doesn't happen to be a thread group leader.
2210 		 * As we don't care in the case of readdir.
2211 		 */
2212 		if (!task || !has_group_leader_pid(task))
2213 			goto retry;
2214 		get_task_struct(task);
2215 	}
2216 	rcu_read_unlock();
2217 	return task;
2218 }
2219 
2220 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2221 
2222 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2223 	struct task_struct *task, int tgid)
2224 {
2225 	char name[PROC_NUMBUF];
2226 	int len = snprintf(name, sizeof(name), "%d", tgid);
2227 	return proc_fill_cache(filp, dirent, filldir, name, len,
2228 				proc_pid_instantiate, task, NULL);
2229 }
2230 
2231 /* for the /proc/ directory itself, after non-process stuff has been done */
2232 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2233 {
2234 	unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2235 	struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2236 	struct task_struct *task;
2237 	int tgid;
2238 
2239 	if (!reaper)
2240 		goto out_no_task;
2241 
2242 	for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2243 		const struct pid_entry *p = &proc_base_stuff[nr];
2244 		if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2245 			goto out;
2246 	}
2247 
2248 	tgid = filp->f_pos - TGID_OFFSET;
2249 	for (task = next_tgid(tgid);
2250 	     task;
2251 	     put_task_struct(task), task = next_tgid(tgid + 1)) {
2252 		tgid = task->pid;
2253 		filp->f_pos = tgid + TGID_OFFSET;
2254 		if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2255 			put_task_struct(task);
2256 			goto out;
2257 		}
2258 	}
2259 	filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2260 out:
2261 	put_task_struct(reaper);
2262 out_no_task:
2263 	return 0;
2264 }
2265 
2266 /*
2267  * Tasks
2268  */
2269 static const struct pid_entry tid_base_stuff[] = {
2270 	DIR("fd",        S_IRUSR|S_IXUSR, fd),
2271 	DIR("fdinfo",    S_IRUSR|S_IXUSR, fdinfo),
2272 	INF("environ",   S_IRUSR, pid_environ),
2273 	INF("auxv",      S_IRUSR, pid_auxv),
2274 	INF("status",    S_IRUGO, pid_status),
2275 	INF("cmdline",   S_IRUGO, pid_cmdline),
2276 	INF("stat",      S_IRUGO, tid_stat),
2277 	INF("statm",     S_IRUGO, pid_statm),
2278 	REG("maps",      S_IRUGO, maps),
2279 #ifdef CONFIG_NUMA
2280 	REG("numa_maps", S_IRUGO, numa_maps),
2281 #endif
2282 	REG("mem",       S_IRUSR|S_IWUSR, mem),
2283 #ifdef CONFIG_SECCOMP
2284 	REG("seccomp",   S_IRUSR|S_IWUSR, seccomp),
2285 #endif
2286 	LNK("cwd",       cwd),
2287 	LNK("root",      root),
2288 	LNK("exe",       exe),
2289 	REG("mounts",    S_IRUGO, mounts),
2290 #ifdef CONFIG_MMU
2291 	REG("clear_refs", S_IWUSR, clear_refs),
2292 	REG("smaps",     S_IRUGO, smaps),
2293 #endif
2294 #ifdef CONFIG_SECURITY
2295 	DIR("attr",      S_IRUGO|S_IXUGO, attr_dir),
2296 #endif
2297 #ifdef CONFIG_KALLSYMS
2298 	INF("wchan",     S_IRUGO, pid_wchan),
2299 #endif
2300 #ifdef CONFIG_SCHEDSTATS
2301 	INF("schedstat", S_IRUGO, pid_schedstat),
2302 #endif
2303 #ifdef CONFIG_CPUSETS
2304 	REG("cpuset",    S_IRUGO, cpuset),
2305 #endif
2306 	INF("oom_score", S_IRUGO, oom_score),
2307 	REG("oom_adj",   S_IRUGO|S_IWUSR, oom_adjust),
2308 #ifdef CONFIG_AUDITSYSCALL
2309 	REG("loginuid",  S_IWUSR|S_IRUGO, loginuid),
2310 #endif
2311 #ifdef CONFIG_FAULT_INJECTION
2312 	REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2313 #endif
2314 };
2315 
2316 static int proc_tid_base_readdir(struct file * filp,
2317 			     void * dirent, filldir_t filldir)
2318 {
2319 	return proc_pident_readdir(filp,dirent,filldir,
2320 				   tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2321 }
2322 
2323 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2324 	return proc_pident_lookup(dir, dentry,
2325 				  tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2326 }
2327 
2328 static const struct file_operations proc_tid_base_operations = {
2329 	.read		= generic_read_dir,
2330 	.readdir	= proc_tid_base_readdir,
2331 };
2332 
2333 static const struct inode_operations proc_tid_base_inode_operations = {
2334 	.lookup		= proc_tid_base_lookup,
2335 	.getattr	= pid_getattr,
2336 	.setattr	= proc_setattr,
2337 };
2338 
2339 static struct dentry *proc_task_instantiate(struct inode *dir,
2340 	struct dentry *dentry, struct task_struct *task, const void *ptr)
2341 {
2342 	struct dentry *error = ERR_PTR(-ENOENT);
2343 	struct inode *inode;
2344 	inode = proc_pid_make_inode(dir->i_sb, task);
2345 
2346 	if (!inode)
2347 		goto out;
2348 	inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2349 	inode->i_op = &proc_tid_base_inode_operations;
2350 	inode->i_fop = &proc_tid_base_operations;
2351 	inode->i_flags|=S_IMMUTABLE;
2352 	inode->i_nlink = 4;
2353 #ifdef CONFIG_SECURITY
2354 	inode->i_nlink += 1;
2355 #endif
2356 
2357 	dentry->d_op = &pid_dentry_operations;
2358 
2359 	d_add(dentry, inode);
2360 	/* Close the race of the process dying before we return the dentry */
2361 	if (pid_revalidate(dentry, NULL))
2362 		error = NULL;
2363 out:
2364 	return error;
2365 }
2366 
2367 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2368 {
2369 	struct dentry *result = ERR_PTR(-ENOENT);
2370 	struct task_struct *task;
2371 	struct task_struct *leader = get_proc_task(dir);
2372 	unsigned tid;
2373 
2374 	if (!leader)
2375 		goto out_no_task;
2376 
2377 	tid = name_to_int(dentry);
2378 	if (tid == ~0U)
2379 		goto out;
2380 
2381 	rcu_read_lock();
2382 	task = find_task_by_pid(tid);
2383 	if (task)
2384 		get_task_struct(task);
2385 	rcu_read_unlock();
2386 	if (!task)
2387 		goto out;
2388 	if (leader->tgid != task->tgid)
2389 		goto out_drop_task;
2390 
2391 	result = proc_task_instantiate(dir, dentry, task, NULL);
2392 out_drop_task:
2393 	put_task_struct(task);
2394 out:
2395 	put_task_struct(leader);
2396 out_no_task:
2397 	return result;
2398 }
2399 
2400 /*
2401  * Find the first tid of a thread group to return to user space.
2402  *
2403  * Usually this is just the thread group leader, but if the users
2404  * buffer was too small or there was a seek into the middle of the
2405  * directory we have more work todo.
2406  *
2407  * In the case of a short read we start with find_task_by_pid.
2408  *
2409  * In the case of a seek we start with the leader and walk nr
2410  * threads past it.
2411  */
2412 static struct task_struct *first_tid(struct task_struct *leader,
2413 					int tid, int nr)
2414 {
2415 	struct task_struct *pos;
2416 
2417 	rcu_read_lock();
2418 	/* Attempt to start with the pid of a thread */
2419 	if (tid && (nr > 0)) {
2420 		pos = find_task_by_pid(tid);
2421 		if (pos && (pos->group_leader == leader))
2422 			goto found;
2423 	}
2424 
2425 	/* If nr exceeds the number of threads there is nothing todo */
2426 	pos = NULL;
2427 	if (nr && nr >= get_nr_threads(leader))
2428 		goto out;
2429 
2430 	/* If we haven't found our starting place yet start
2431 	 * with the leader and walk nr threads forward.
2432 	 */
2433 	for (pos = leader; nr > 0; --nr) {
2434 		pos = next_thread(pos);
2435 		if (pos == leader) {
2436 			pos = NULL;
2437 			goto out;
2438 		}
2439 	}
2440 found:
2441 	get_task_struct(pos);
2442 out:
2443 	rcu_read_unlock();
2444 	return pos;
2445 }
2446 
2447 /*
2448  * Find the next thread in the thread list.
2449  * Return NULL if there is an error or no next thread.
2450  *
2451  * The reference to the input task_struct is released.
2452  */
2453 static struct task_struct *next_tid(struct task_struct *start)
2454 {
2455 	struct task_struct *pos = NULL;
2456 	rcu_read_lock();
2457 	if (pid_alive(start)) {
2458 		pos = next_thread(start);
2459 		if (thread_group_leader(pos))
2460 			pos = NULL;
2461 		else
2462 			get_task_struct(pos);
2463 	}
2464 	rcu_read_unlock();
2465 	put_task_struct(start);
2466 	return pos;
2467 }
2468 
2469 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2470 	struct task_struct *task, int tid)
2471 {
2472 	char name[PROC_NUMBUF];
2473 	int len = snprintf(name, sizeof(name), "%d", tid);
2474 	return proc_fill_cache(filp, dirent, filldir, name, len,
2475 				proc_task_instantiate, task, NULL);
2476 }
2477 
2478 /* for the /proc/TGID/task/ directories */
2479 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2480 {
2481 	struct dentry *dentry = filp->f_path.dentry;
2482 	struct inode *inode = dentry->d_inode;
2483 	struct task_struct *leader = NULL;
2484 	struct task_struct *task;
2485 	int retval = -ENOENT;
2486 	ino_t ino;
2487 	int tid;
2488 	unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2489 
2490 	task = get_proc_task(inode);
2491 	if (!task)
2492 		goto out_no_task;
2493 	rcu_read_lock();
2494 	if (pid_alive(task)) {
2495 		leader = task->group_leader;
2496 		get_task_struct(leader);
2497 	}
2498 	rcu_read_unlock();
2499 	put_task_struct(task);
2500 	if (!leader)
2501 		goto out_no_task;
2502 	retval = 0;
2503 
2504 	switch (pos) {
2505 	case 0:
2506 		ino = inode->i_ino;
2507 		if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2508 			goto out;
2509 		pos++;
2510 		/* fall through */
2511 	case 1:
2512 		ino = parent_ino(dentry);
2513 		if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2514 			goto out;
2515 		pos++;
2516 		/* fall through */
2517 	}
2518 
2519 	/* f_version caches the tgid value that the last readdir call couldn't
2520 	 * return. lseek aka telldir automagically resets f_version to 0.
2521 	 */
2522 	tid = filp->f_version;
2523 	filp->f_version = 0;
2524 	for (task = first_tid(leader, tid, pos - 2);
2525 	     task;
2526 	     task = next_tid(task), pos++) {
2527 		tid = task->pid;
2528 		if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2529 			/* returning this tgid failed, save it as the first
2530 			 * pid for the next readir call */
2531 			filp->f_version = tid;
2532 			put_task_struct(task);
2533 			break;
2534 		}
2535 	}
2536 out:
2537 	filp->f_pos = pos;
2538 	put_task_struct(leader);
2539 out_no_task:
2540 	return retval;
2541 }
2542 
2543 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2544 {
2545 	struct inode *inode = dentry->d_inode;
2546 	struct task_struct *p = get_proc_task(inode);
2547 	generic_fillattr(inode, stat);
2548 
2549 	if (p) {
2550 		rcu_read_lock();
2551 		stat->nlink += get_nr_threads(p);
2552 		rcu_read_unlock();
2553 		put_task_struct(p);
2554 	}
2555 
2556 	return 0;
2557 }
2558 
2559 static const struct inode_operations proc_task_inode_operations = {
2560 	.lookup		= proc_task_lookup,
2561 	.getattr	= proc_task_getattr,
2562 	.setattr	= proc_setattr,
2563 };
2564 
2565 static const struct file_operations proc_task_operations = {
2566 	.read		= generic_read_dir,
2567 	.readdir	= proc_task_readdir,
2568 };
2569