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