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