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