Merge branch 'master' of /home/trondmy/kernel/linux-2.6/
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / proc / base.c
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 - 1;
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 page[count] = '\0';
1037 loginuid = simple_strtoul(page, &tmp, 10);
1038 if (tmp == page) {
1039 length = -EINVAL;
1040 goto out_free_page;
1041
1042 }
1043 length = audit_set_loginuid(task, loginuid);
1044 if (likely(length == 0))
1045 length = count;
1046
1047 out_free_page:
1048 free_page((unsigned long) page);
1049 return length;
1050 }
1051
1052 static struct file_operations proc_loginuid_operations = {
1053 .read = proc_loginuid_read,
1054 .write = proc_loginuid_write,
1055 };
1056 #endif
1057
1058 #ifdef CONFIG_SECCOMP
1059 static ssize_t seccomp_read(struct file *file, char __user *buf,
1060 size_t count, loff_t *ppos)
1061 {
1062 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1063 char __buf[20];
1064 loff_t __ppos = *ppos;
1065 size_t len;
1066
1067 /* no need to print the trailing zero, so use only len */
1068 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1069 if (__ppos >= len)
1070 return 0;
1071 if (count > len - __ppos)
1072 count = len - __ppos;
1073 if (copy_to_user(buf, __buf + __ppos, count))
1074 return -EFAULT;
1075 *ppos = __ppos + count;
1076 return count;
1077 }
1078
1079 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1080 size_t count, loff_t *ppos)
1081 {
1082 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1083 char __buf[20], *end;
1084 unsigned int seccomp_mode;
1085
1086 /* can set it only once to be even more secure */
1087 if (unlikely(tsk->seccomp.mode))
1088 return -EPERM;
1089
1090 memset(__buf, 0, sizeof(__buf));
1091 count = min(count, sizeof(__buf) - 1);
1092 if (copy_from_user(__buf, buf, count))
1093 return -EFAULT;
1094 seccomp_mode = simple_strtoul(__buf, &end, 0);
1095 if (*end == '\n')
1096 end++;
1097 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1098 tsk->seccomp.mode = seccomp_mode;
1099 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1100 } else
1101 return -EINVAL;
1102 if (unlikely(!(end - __buf)))
1103 return -EIO;
1104 return end - __buf;
1105 }
1106
1107 static struct file_operations proc_seccomp_operations = {
1108 .read = seccomp_read,
1109 .write = seccomp_write,
1110 };
1111 #endif /* CONFIG_SECCOMP */
1112
1113 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1114 {
1115 struct inode *inode = dentry->d_inode;
1116 int error = -EACCES;
1117
1118 /* We don't need a base pointer in the /proc filesystem */
1119 path_release(nd);
1120
1121 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1122 goto out;
1123 error = proc_check_root(inode);
1124 if (error)
1125 goto out;
1126
1127 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1128 nd->last_type = LAST_BIND;
1129 out:
1130 return ERR_PTR(error);
1131 }
1132
1133 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1134 char __user *buffer, int buflen)
1135 {
1136 struct inode * inode;
1137 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1138 int len;
1139
1140 if (!tmp)
1141 return -ENOMEM;
1142
1143 inode = dentry->d_inode;
1144 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1145 len = PTR_ERR(path);
1146 if (IS_ERR(path))
1147 goto out;
1148 len = tmp + PAGE_SIZE - 1 - path;
1149
1150 if (len > buflen)
1151 len = buflen;
1152 if (copy_to_user(buffer, path, len))
1153 len = -EFAULT;
1154 out:
1155 free_page((unsigned long)tmp);
1156 return len;
1157 }
1158
1159 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1160 {
1161 int error = -EACCES;
1162 struct inode *inode = dentry->d_inode;
1163 struct dentry *de;
1164 struct vfsmount *mnt = NULL;
1165
1166 lock_kernel();
1167
1168 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1169 goto out;
1170 error = proc_check_root(inode);
1171 if (error)
1172 goto out;
1173
1174 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1175 if (error)
1176 goto out;
1177
1178 error = do_proc_readlink(de, mnt, buffer, buflen);
1179 dput(de);
1180 mntput(mnt);
1181 out:
1182 unlock_kernel();
1183 return error;
1184 }
1185
1186 static struct inode_operations proc_pid_link_inode_operations = {
1187 .readlink = proc_pid_readlink,
1188 .follow_link = proc_pid_follow_link
1189 };
1190
1191 #define NUMBUF 10
1192
1193 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1194 {
1195 struct inode *inode = filp->f_dentry->d_inode;
1196 struct task_struct *p = proc_task(inode);
1197 unsigned int fd, tid, ino;
1198 int retval;
1199 char buf[NUMBUF];
1200 struct files_struct * files;
1201 struct fdtable *fdt;
1202
1203 retval = -ENOENT;
1204 if (!pid_alive(p))
1205 goto out;
1206 retval = 0;
1207 tid = p->pid;
1208
1209 fd = filp->f_pos;
1210 switch (fd) {
1211 case 0:
1212 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1213 goto out;
1214 filp->f_pos++;
1215 case 1:
1216 ino = fake_ino(tid, PROC_TID_INO);
1217 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1218 goto out;
1219 filp->f_pos++;
1220 default:
1221 files = get_files_struct(p);
1222 if (!files)
1223 goto out;
1224 rcu_read_lock();
1225 fdt = files_fdtable(files);
1226 for (fd = filp->f_pos-2;
1227 fd < fdt->max_fds;
1228 fd++, filp->f_pos++) {
1229 unsigned int i,j;
1230
1231 if (!fcheck_files(files, fd))
1232 continue;
1233 rcu_read_unlock();
1234
1235 j = NUMBUF;
1236 i = fd;
1237 do {
1238 j--;
1239 buf[j] = '0' + (i % 10);
1240 i /= 10;
1241 } while (i);
1242
1243 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1244 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1245 rcu_read_lock();
1246 break;
1247 }
1248 rcu_read_lock();
1249 }
1250 rcu_read_unlock();
1251 put_files_struct(files);
1252 }
1253 out:
1254 return retval;
1255 }
1256
1257 static int proc_pident_readdir(struct file *filp,
1258 void *dirent, filldir_t filldir,
1259 struct pid_entry *ents, unsigned int nents)
1260 {
1261 int i;
1262 int pid;
1263 struct dentry *dentry = filp->f_dentry;
1264 struct inode *inode = dentry->d_inode;
1265 struct pid_entry *p;
1266 ino_t ino;
1267 int ret;
1268
1269 ret = -ENOENT;
1270 if (!pid_alive(proc_task(inode)))
1271 goto out;
1272
1273 ret = 0;
1274 pid = proc_task(inode)->pid;
1275 i = filp->f_pos;
1276 switch (i) {
1277 case 0:
1278 ino = inode->i_ino;
1279 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1280 goto out;
1281 i++;
1282 filp->f_pos++;
1283 /* fall through */
1284 case 1:
1285 ino = parent_ino(dentry);
1286 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1287 goto out;
1288 i++;
1289 filp->f_pos++;
1290 /* fall through */
1291 default:
1292 i -= 2;
1293 if (i >= nents) {
1294 ret = 1;
1295 goto out;
1296 }
1297 p = ents + i;
1298 while (p->name) {
1299 if (filldir(dirent, p->name, p->len, filp->f_pos,
1300 fake_ino(pid, p->type), p->mode >> 12) < 0)
1301 goto out;
1302 filp->f_pos++;
1303 p++;
1304 }
1305 }
1306
1307 ret = 1;
1308 out:
1309 return ret;
1310 }
1311
1312 static int proc_tgid_base_readdir(struct file * filp,
1313 void * dirent, filldir_t filldir)
1314 {
1315 return proc_pident_readdir(filp,dirent,filldir,
1316 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1317 }
1318
1319 static int proc_tid_base_readdir(struct file * filp,
1320 void * dirent, filldir_t filldir)
1321 {
1322 return proc_pident_readdir(filp,dirent,filldir,
1323 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1324 }
1325
1326 /* building an inode */
1327
1328 static int task_dumpable(struct task_struct *task)
1329 {
1330 int dumpable = 0;
1331 struct mm_struct *mm;
1332
1333 task_lock(task);
1334 mm = task->mm;
1335 if (mm)
1336 dumpable = mm->dumpable;
1337 task_unlock(task);
1338 if(dumpable == 1)
1339 return 1;
1340 return 0;
1341 }
1342
1343
1344 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1345 {
1346 struct inode * inode;
1347 struct proc_inode *ei;
1348
1349 /* We need a new inode */
1350
1351 inode = new_inode(sb);
1352 if (!inode)
1353 goto out;
1354
1355 /* Common stuff */
1356 ei = PROC_I(inode);
1357 ei->task = NULL;
1358 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1359 inode->i_ino = fake_ino(task->pid, ino);
1360
1361 if (!pid_alive(task))
1362 goto out_unlock;
1363
1364 /*
1365 * grab the reference to task.
1366 */
1367 get_task_struct(task);
1368 ei->task = task;
1369 ei->type = ino;
1370 inode->i_uid = 0;
1371 inode->i_gid = 0;
1372 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1373 inode->i_uid = task->euid;
1374 inode->i_gid = task->egid;
1375 }
1376 security_task_to_inode(task, inode);
1377
1378 out:
1379 return inode;
1380
1381 out_unlock:
1382 ei->pde = NULL;
1383 iput(inode);
1384 return NULL;
1385 }
1386
1387 /* dentry stuff */
1388
1389 /*
1390 * Exceptional case: normally we are not allowed to unhash a busy
1391 * directory. In this case, however, we can do it - no aliasing problems
1392 * due to the way we treat inodes.
1393 *
1394 * Rewrite the inode's ownerships here because the owning task may have
1395 * performed a setuid(), etc.
1396 */
1397 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1398 {
1399 struct inode *inode = dentry->d_inode;
1400 struct task_struct *task = proc_task(inode);
1401 if (pid_alive(task)) {
1402 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1403 inode->i_uid = task->euid;
1404 inode->i_gid = task->egid;
1405 } else {
1406 inode->i_uid = 0;
1407 inode->i_gid = 0;
1408 }
1409 security_task_to_inode(task, inode);
1410 return 1;
1411 }
1412 d_drop(dentry);
1413 return 0;
1414 }
1415
1416 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1417 {
1418 struct inode *inode = dentry->d_inode;
1419 struct task_struct *task = proc_task(inode);
1420 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1421 struct files_struct *files;
1422
1423 files = get_files_struct(task);
1424 if (files) {
1425 rcu_read_lock();
1426 if (fcheck_files(files, fd)) {
1427 rcu_read_unlock();
1428 put_files_struct(files);
1429 if (task_dumpable(task)) {
1430 inode->i_uid = task->euid;
1431 inode->i_gid = task->egid;
1432 } else {
1433 inode->i_uid = 0;
1434 inode->i_gid = 0;
1435 }
1436 security_task_to_inode(task, inode);
1437 return 1;
1438 }
1439 rcu_read_unlock();
1440 put_files_struct(files);
1441 }
1442 d_drop(dentry);
1443 return 0;
1444 }
1445
1446 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1447 {
1448 struct task_struct *task = proc_task(inode);
1449 spin_lock(&task->proc_lock);
1450 if (task->proc_dentry == dentry)
1451 task->proc_dentry = NULL;
1452 spin_unlock(&task->proc_lock);
1453 iput(inode);
1454 }
1455
1456 static int pid_delete_dentry(struct dentry * dentry)
1457 {
1458 /* Is the task we represent dead?
1459 * If so, then don't put the dentry on the lru list,
1460 * kill it immediately.
1461 */
1462 return !pid_alive(proc_task(dentry->d_inode));
1463 }
1464
1465 static struct dentry_operations tid_fd_dentry_operations =
1466 {
1467 .d_revalidate = tid_fd_revalidate,
1468 .d_delete = pid_delete_dentry,
1469 };
1470
1471 static struct dentry_operations pid_dentry_operations =
1472 {
1473 .d_revalidate = pid_revalidate,
1474 .d_delete = pid_delete_dentry,
1475 };
1476
1477 static struct dentry_operations pid_base_dentry_operations =
1478 {
1479 .d_revalidate = pid_revalidate,
1480 .d_iput = pid_base_iput,
1481 .d_delete = pid_delete_dentry,
1482 };
1483
1484 /* Lookups */
1485
1486 static unsigned name_to_int(struct dentry *dentry)
1487 {
1488 const char *name = dentry->d_name.name;
1489 int len = dentry->d_name.len;
1490 unsigned n = 0;
1491
1492 if (len > 1 && *name == '0')
1493 goto out;
1494 while (len-- > 0) {
1495 unsigned c = *name++ - '0';
1496 if (c > 9)
1497 goto out;
1498 if (n >= (~0U-9)/10)
1499 goto out;
1500 n *= 10;
1501 n += c;
1502 }
1503 return n;
1504 out:
1505 return ~0U;
1506 }
1507
1508 /* SMP-safe */
1509 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1510 {
1511 struct task_struct *task = proc_task(dir);
1512 unsigned fd = name_to_int(dentry);
1513 struct file * file;
1514 struct files_struct * files;
1515 struct inode *inode;
1516 struct proc_inode *ei;
1517
1518 if (fd == ~0U)
1519 goto out;
1520 if (!pid_alive(task))
1521 goto out;
1522
1523 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1524 if (!inode)
1525 goto out;
1526 ei = PROC_I(inode);
1527 files = get_files_struct(task);
1528 if (!files)
1529 goto out_unlock;
1530 inode->i_mode = S_IFLNK;
1531
1532 /*
1533 * We are not taking a ref to the file structure, so we must
1534 * hold ->file_lock.
1535 */
1536 spin_lock(&files->file_lock);
1537 file = fcheck_files(files, fd);
1538 if (!file)
1539 goto out_unlock2;
1540 if (file->f_mode & 1)
1541 inode->i_mode |= S_IRUSR | S_IXUSR;
1542 if (file->f_mode & 2)
1543 inode->i_mode |= S_IWUSR | S_IXUSR;
1544 spin_unlock(&files->file_lock);
1545 put_files_struct(files);
1546 inode->i_op = &proc_pid_link_inode_operations;
1547 inode->i_size = 64;
1548 ei->op.proc_get_link = proc_fd_link;
1549 dentry->d_op = &tid_fd_dentry_operations;
1550 d_add(dentry, inode);
1551 return NULL;
1552
1553 out_unlock2:
1554 spin_unlock(&files->file_lock);
1555 put_files_struct(files);
1556 out_unlock:
1557 iput(inode);
1558 out:
1559 return ERR_PTR(-ENOENT);
1560 }
1561
1562 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1563 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1564
1565 static struct file_operations proc_fd_operations = {
1566 .read = generic_read_dir,
1567 .readdir = proc_readfd,
1568 };
1569
1570 static struct file_operations proc_task_operations = {
1571 .read = generic_read_dir,
1572 .readdir = proc_task_readdir,
1573 };
1574
1575 /*
1576 * proc directories can do almost nothing..
1577 */
1578 static struct inode_operations proc_fd_inode_operations = {
1579 .lookup = proc_lookupfd,
1580 .permission = proc_permission,
1581 };
1582
1583 static struct inode_operations proc_task_inode_operations = {
1584 .lookup = proc_task_lookup,
1585 .permission = proc_task_permission,
1586 };
1587
1588 #ifdef CONFIG_SECURITY
1589 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1590 size_t count, loff_t *ppos)
1591 {
1592 struct inode * inode = file->f_dentry->d_inode;
1593 unsigned long page;
1594 ssize_t length;
1595 struct task_struct *task = proc_task(inode);
1596
1597 if (count > PAGE_SIZE)
1598 count = PAGE_SIZE;
1599 if (!(page = __get_free_page(GFP_KERNEL)))
1600 return -ENOMEM;
1601
1602 length = security_getprocattr(task,
1603 (char*)file->f_dentry->d_name.name,
1604 (void*)page, count);
1605 if (length >= 0)
1606 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1607 free_page(page);
1608 return length;
1609 }
1610
1611 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1612 size_t count, loff_t *ppos)
1613 {
1614 struct inode * inode = file->f_dentry->d_inode;
1615 char *page;
1616 ssize_t length;
1617 struct task_struct *task = proc_task(inode);
1618
1619 if (count > PAGE_SIZE)
1620 count = PAGE_SIZE;
1621 if (*ppos != 0) {
1622 /* No partial writes. */
1623 return -EINVAL;
1624 }
1625 page = (char*)__get_free_page(GFP_USER);
1626 if (!page)
1627 return -ENOMEM;
1628 length = -EFAULT;
1629 if (copy_from_user(page, buf, count))
1630 goto out;
1631
1632 length = security_setprocattr(task,
1633 (char*)file->f_dentry->d_name.name,
1634 (void*)page, count);
1635 out:
1636 free_page((unsigned long) page);
1637 return length;
1638 }
1639
1640 static struct file_operations proc_pid_attr_operations = {
1641 .read = proc_pid_attr_read,
1642 .write = proc_pid_attr_write,
1643 };
1644
1645 static struct file_operations proc_tid_attr_operations;
1646 static struct inode_operations proc_tid_attr_inode_operations;
1647 static struct file_operations proc_tgid_attr_operations;
1648 static struct inode_operations proc_tgid_attr_inode_operations;
1649 #endif
1650
1651 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1652
1653 /* SMP-safe */
1654 static struct dentry *proc_pident_lookup(struct inode *dir,
1655 struct dentry *dentry,
1656 struct pid_entry *ents)
1657 {
1658 struct inode *inode;
1659 int error;
1660 struct task_struct *task = proc_task(dir);
1661 struct pid_entry *p;
1662 struct proc_inode *ei;
1663
1664 error = -ENOENT;
1665 inode = NULL;
1666
1667 if (!pid_alive(task))
1668 goto out;
1669
1670 for (p = ents; p->name; p++) {
1671 if (p->len != dentry->d_name.len)
1672 continue;
1673 if (!memcmp(dentry->d_name.name, p->name, p->len))
1674 break;
1675 }
1676 if (!p->name)
1677 goto out;
1678
1679 error = -EINVAL;
1680 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1681 if (!inode)
1682 goto out;
1683
1684 ei = PROC_I(inode);
1685 inode->i_mode = p->mode;
1686 /*
1687 * Yes, it does not scale. And it should not. Don't add
1688 * new entries into /proc/<tgid>/ without very good reasons.
1689 */
1690 switch(p->type) {
1691 case PROC_TGID_TASK:
1692 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1693 inode->i_op = &proc_task_inode_operations;
1694 inode->i_fop = &proc_task_operations;
1695 break;
1696 case PROC_TID_FD:
1697 case PROC_TGID_FD:
1698 inode->i_nlink = 2;
1699 inode->i_op = &proc_fd_inode_operations;
1700 inode->i_fop = &proc_fd_operations;
1701 break;
1702 case PROC_TID_EXE:
1703 case PROC_TGID_EXE:
1704 inode->i_op = &proc_pid_link_inode_operations;
1705 ei->op.proc_get_link = proc_exe_link;
1706 break;
1707 case PROC_TID_CWD:
1708 case PROC_TGID_CWD:
1709 inode->i_op = &proc_pid_link_inode_operations;
1710 ei->op.proc_get_link = proc_cwd_link;
1711 break;
1712 case PROC_TID_ROOT:
1713 case PROC_TGID_ROOT:
1714 inode->i_op = &proc_pid_link_inode_operations;
1715 ei->op.proc_get_link = proc_root_link;
1716 break;
1717 case PROC_TID_ENVIRON:
1718 case PROC_TGID_ENVIRON:
1719 inode->i_fop = &proc_info_file_operations;
1720 ei->op.proc_read = proc_pid_environ;
1721 break;
1722 case PROC_TID_AUXV:
1723 case PROC_TGID_AUXV:
1724 inode->i_fop = &proc_info_file_operations;
1725 ei->op.proc_read = proc_pid_auxv;
1726 break;
1727 case PROC_TID_STATUS:
1728 case PROC_TGID_STATUS:
1729 inode->i_fop = &proc_info_file_operations;
1730 ei->op.proc_read = proc_pid_status;
1731 break;
1732 case PROC_TID_STAT:
1733 inode->i_fop = &proc_info_file_operations;
1734 ei->op.proc_read = proc_tid_stat;
1735 break;
1736 case PROC_TGID_STAT:
1737 inode->i_fop = &proc_info_file_operations;
1738 ei->op.proc_read = proc_tgid_stat;
1739 break;
1740 case PROC_TID_CMDLINE:
1741 case PROC_TGID_CMDLINE:
1742 inode->i_fop = &proc_info_file_operations;
1743 ei->op.proc_read = proc_pid_cmdline;
1744 break;
1745 case PROC_TID_STATM:
1746 case PROC_TGID_STATM:
1747 inode->i_fop = &proc_info_file_operations;
1748 ei->op.proc_read = proc_pid_statm;
1749 break;
1750 case PROC_TID_MAPS:
1751 case PROC_TGID_MAPS:
1752 inode->i_fop = &proc_maps_operations;
1753 break;
1754 #ifdef CONFIG_NUMA
1755 case PROC_TID_NUMA_MAPS:
1756 case PROC_TGID_NUMA_MAPS:
1757 inode->i_fop = &proc_numa_maps_operations;
1758 break;
1759 #endif
1760 case PROC_TID_MEM:
1761 case PROC_TGID_MEM:
1762 inode->i_op = &proc_mem_inode_operations;
1763 inode->i_fop = &proc_mem_operations;
1764 break;
1765 #ifdef CONFIG_SECCOMP
1766 case PROC_TID_SECCOMP:
1767 case PROC_TGID_SECCOMP:
1768 inode->i_fop = &proc_seccomp_operations;
1769 break;
1770 #endif /* CONFIG_SECCOMP */
1771 case PROC_TID_MOUNTS:
1772 case PROC_TGID_MOUNTS:
1773 inode->i_fop = &proc_mounts_operations;
1774 break;
1775 #ifdef CONFIG_MMU
1776 case PROC_TID_SMAPS:
1777 case PROC_TGID_SMAPS:
1778 inode->i_fop = &proc_smaps_operations;
1779 break;
1780 #endif
1781 case PROC_TID_MOUNTSTATS:
1782 case PROC_TGID_MOUNTSTATS:
1783 inode->i_fop = &proc_mountstats_operations;
1784 break;
1785 #ifdef CONFIG_SECURITY
1786 case PROC_TID_ATTR:
1787 inode->i_nlink = 2;
1788 inode->i_op = &proc_tid_attr_inode_operations;
1789 inode->i_fop = &proc_tid_attr_operations;
1790 break;
1791 case PROC_TGID_ATTR:
1792 inode->i_nlink = 2;
1793 inode->i_op = &proc_tgid_attr_inode_operations;
1794 inode->i_fop = &proc_tgid_attr_operations;
1795 break;
1796 case PROC_TID_ATTR_CURRENT:
1797 case PROC_TGID_ATTR_CURRENT:
1798 case PROC_TID_ATTR_PREV:
1799 case PROC_TGID_ATTR_PREV:
1800 case PROC_TID_ATTR_EXEC:
1801 case PROC_TGID_ATTR_EXEC:
1802 case PROC_TID_ATTR_FSCREATE:
1803 case PROC_TGID_ATTR_FSCREATE:
1804 inode->i_fop = &proc_pid_attr_operations;
1805 break;
1806 #endif
1807 #ifdef CONFIG_KALLSYMS
1808 case PROC_TID_WCHAN:
1809 case PROC_TGID_WCHAN:
1810 inode->i_fop = &proc_info_file_operations;
1811 ei->op.proc_read = proc_pid_wchan;
1812 break;
1813 #endif
1814 #ifdef CONFIG_SCHEDSTATS
1815 case PROC_TID_SCHEDSTAT:
1816 case PROC_TGID_SCHEDSTAT:
1817 inode->i_fop = &proc_info_file_operations;
1818 ei->op.proc_read = proc_pid_schedstat;
1819 break;
1820 #endif
1821 #ifdef CONFIG_CPUSETS
1822 case PROC_TID_CPUSET:
1823 case PROC_TGID_CPUSET:
1824 inode->i_fop = &proc_cpuset_operations;
1825 break;
1826 #endif
1827 case PROC_TID_OOM_SCORE:
1828 case PROC_TGID_OOM_SCORE:
1829 inode->i_fop = &proc_info_file_operations;
1830 ei->op.proc_read = proc_oom_score;
1831 break;
1832 case PROC_TID_OOM_ADJUST:
1833 case PROC_TGID_OOM_ADJUST:
1834 inode->i_fop = &proc_oom_adjust_operations;
1835 break;
1836 #ifdef CONFIG_AUDITSYSCALL
1837 case PROC_TID_LOGINUID:
1838 case PROC_TGID_LOGINUID:
1839 inode->i_fop = &proc_loginuid_operations;
1840 break;
1841 #endif
1842 default:
1843 printk("procfs: impossible type (%d)",p->type);
1844 iput(inode);
1845 return ERR_PTR(-EINVAL);
1846 }
1847 dentry->d_op = &pid_dentry_operations;
1848 d_add(dentry, inode);
1849 return NULL;
1850
1851 out:
1852 return ERR_PTR(error);
1853 }
1854
1855 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1856 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1857 }
1858
1859 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1860 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1861 }
1862
1863 static struct file_operations proc_tgid_base_operations = {
1864 .read = generic_read_dir,
1865 .readdir = proc_tgid_base_readdir,
1866 };
1867
1868 static struct file_operations proc_tid_base_operations = {
1869 .read = generic_read_dir,
1870 .readdir = proc_tid_base_readdir,
1871 };
1872
1873 static struct inode_operations proc_tgid_base_inode_operations = {
1874 .lookup = proc_tgid_base_lookup,
1875 };
1876
1877 static struct inode_operations proc_tid_base_inode_operations = {
1878 .lookup = proc_tid_base_lookup,
1879 };
1880
1881 #ifdef CONFIG_SECURITY
1882 static int proc_tgid_attr_readdir(struct file * filp,
1883 void * dirent, filldir_t filldir)
1884 {
1885 return proc_pident_readdir(filp,dirent,filldir,
1886 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1887 }
1888
1889 static int proc_tid_attr_readdir(struct file * filp,
1890 void * dirent, filldir_t filldir)
1891 {
1892 return proc_pident_readdir(filp,dirent,filldir,
1893 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1894 }
1895
1896 static struct file_operations proc_tgid_attr_operations = {
1897 .read = generic_read_dir,
1898 .readdir = proc_tgid_attr_readdir,
1899 };
1900
1901 static struct file_operations proc_tid_attr_operations = {
1902 .read = generic_read_dir,
1903 .readdir = proc_tid_attr_readdir,
1904 };
1905
1906 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1907 struct dentry *dentry, struct nameidata *nd)
1908 {
1909 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1910 }
1911
1912 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1913 struct dentry *dentry, struct nameidata *nd)
1914 {
1915 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1916 }
1917
1918 static struct inode_operations proc_tgid_attr_inode_operations = {
1919 .lookup = proc_tgid_attr_lookup,
1920 };
1921
1922 static struct inode_operations proc_tid_attr_inode_operations = {
1923 .lookup = proc_tid_attr_lookup,
1924 };
1925 #endif
1926
1927 /*
1928 * /proc/self:
1929 */
1930 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1931 int buflen)
1932 {
1933 char tmp[30];
1934 sprintf(tmp, "%d", current->tgid);
1935 return vfs_readlink(dentry,buffer,buflen,tmp);
1936 }
1937
1938 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1939 {
1940 char tmp[30];
1941 sprintf(tmp, "%d", current->tgid);
1942 return ERR_PTR(vfs_follow_link(nd,tmp));
1943 }
1944
1945 static struct inode_operations proc_self_inode_operations = {
1946 .readlink = proc_self_readlink,
1947 .follow_link = proc_self_follow_link,
1948 };
1949
1950 /**
1951 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1952 * @p: task that should be flushed.
1953 *
1954 * Drops the /proc/@pid dcache entry from the hash chains.
1955 *
1956 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1957 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1958 * if the pid value is immediately reused. This is enforced by
1959 * - caller must acquire spin_lock(p->proc_lock)
1960 * - must be called before detach_pid()
1961 * - proc_pid_lookup acquires proc_lock, and checks that
1962 * the target is not dead by looking at the attach count
1963 * of PIDTYPE_PID.
1964 */
1965
1966 struct dentry *proc_pid_unhash(struct task_struct *p)
1967 {
1968 struct dentry *proc_dentry;
1969
1970 proc_dentry = p->proc_dentry;
1971 if (proc_dentry != NULL) {
1972
1973 spin_lock(&dcache_lock);
1974 spin_lock(&proc_dentry->d_lock);
1975 if (!d_unhashed(proc_dentry)) {
1976 dget_locked(proc_dentry);
1977 __d_drop(proc_dentry);
1978 spin_unlock(&proc_dentry->d_lock);
1979 } else {
1980 spin_unlock(&proc_dentry->d_lock);
1981 proc_dentry = NULL;
1982 }
1983 spin_unlock(&dcache_lock);
1984 }
1985 return proc_dentry;
1986 }
1987
1988 /**
1989 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1990 * @proc_dentry: directoy to prune.
1991 *
1992 * Shrink the /proc directory that was used by the just killed thread.
1993 */
1994
1995 void proc_pid_flush(struct dentry *proc_dentry)
1996 {
1997 might_sleep();
1998 if(proc_dentry != NULL) {
1999 shrink_dcache_parent(proc_dentry);
2000 dput(proc_dentry);
2001 }
2002 }
2003
2004 /* SMP-safe */
2005 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2006 {
2007 struct task_struct *task;
2008 struct inode *inode;
2009 struct proc_inode *ei;
2010 unsigned tgid;
2011 int died;
2012
2013 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2014 inode = new_inode(dir->i_sb);
2015 if (!inode)
2016 return ERR_PTR(-ENOMEM);
2017 ei = PROC_I(inode);
2018 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2019 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2020 ei->pde = NULL;
2021 inode->i_mode = S_IFLNK|S_IRWXUGO;
2022 inode->i_uid = inode->i_gid = 0;
2023 inode->i_size = 64;
2024 inode->i_op = &proc_self_inode_operations;
2025 d_add(dentry, inode);
2026 return NULL;
2027 }
2028 tgid = name_to_int(dentry);
2029 if (tgid == ~0U)
2030 goto out;
2031
2032 read_lock(&tasklist_lock);
2033 task = find_task_by_pid(tgid);
2034 if (task)
2035 get_task_struct(task);
2036 read_unlock(&tasklist_lock);
2037 if (!task)
2038 goto out;
2039
2040 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2041
2042
2043 if (!inode) {
2044 put_task_struct(task);
2045 goto out;
2046 }
2047 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2048 inode->i_op = &proc_tgid_base_inode_operations;
2049 inode->i_fop = &proc_tgid_base_operations;
2050 inode->i_flags|=S_IMMUTABLE;
2051 #ifdef CONFIG_SECURITY
2052 inode->i_nlink = 5;
2053 #else
2054 inode->i_nlink = 4;
2055 #endif
2056
2057 dentry->d_op = &pid_base_dentry_operations;
2058
2059 died = 0;
2060 d_add(dentry, inode);
2061 spin_lock(&task->proc_lock);
2062 task->proc_dentry = dentry;
2063 if (!pid_alive(task)) {
2064 dentry = proc_pid_unhash(task);
2065 died = 1;
2066 }
2067 spin_unlock(&task->proc_lock);
2068
2069 put_task_struct(task);
2070 if (died) {
2071 proc_pid_flush(dentry);
2072 goto out;
2073 }
2074 return NULL;
2075 out:
2076 return ERR_PTR(-ENOENT);
2077 }
2078
2079 /* SMP-safe */
2080 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2081 {
2082 struct task_struct *task;
2083 struct task_struct *leader = proc_task(dir);
2084 struct inode *inode;
2085 unsigned tid;
2086
2087 tid = name_to_int(dentry);
2088 if (tid == ~0U)
2089 goto out;
2090
2091 read_lock(&tasklist_lock);
2092 task = find_task_by_pid(tid);
2093 if (task)
2094 get_task_struct(task);
2095 read_unlock(&tasklist_lock);
2096 if (!task)
2097 goto out;
2098 if (leader->tgid != task->tgid)
2099 goto out_drop_task;
2100
2101 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2102
2103
2104 if (!inode)
2105 goto out_drop_task;
2106 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2107 inode->i_op = &proc_tid_base_inode_operations;
2108 inode->i_fop = &proc_tid_base_operations;
2109 inode->i_flags|=S_IMMUTABLE;
2110 #ifdef CONFIG_SECURITY
2111 inode->i_nlink = 4;
2112 #else
2113 inode->i_nlink = 3;
2114 #endif
2115
2116 dentry->d_op = &pid_base_dentry_operations;
2117
2118 d_add(dentry, inode);
2119
2120 put_task_struct(task);
2121 return NULL;
2122 out_drop_task:
2123 put_task_struct(task);
2124 out:
2125 return ERR_PTR(-ENOENT);
2126 }
2127
2128 #define PROC_NUMBUF 10
2129 #define PROC_MAXPIDS 20
2130
2131 /*
2132 * Get a few tgid's to return for filldir - we need to hold the
2133 * tasklist lock while doing this, and we must release it before
2134 * we actually do the filldir itself, so we use a temp buffer..
2135 */
2136 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2137 {
2138 struct task_struct *p;
2139 int nr_tgids = 0;
2140
2141 index--;
2142 read_lock(&tasklist_lock);
2143 p = NULL;
2144 if (version) {
2145 p = find_task_by_pid(version);
2146 if (p && !thread_group_leader(p))
2147 p = NULL;
2148 }
2149
2150 if (p)
2151 index = 0;
2152 else
2153 p = next_task(&init_task);
2154
2155 for ( ; p != &init_task; p = next_task(p)) {
2156 int tgid = p->pid;
2157 if (!pid_alive(p))
2158 continue;
2159 if (--index >= 0)
2160 continue;
2161 tgids[nr_tgids] = tgid;
2162 nr_tgids++;
2163 if (nr_tgids >= PROC_MAXPIDS)
2164 break;
2165 }
2166 read_unlock(&tasklist_lock);
2167 return nr_tgids;
2168 }
2169
2170 /*
2171 * Get a few tid's to return for filldir - we need to hold the
2172 * tasklist lock while doing this, and we must release it before
2173 * we actually do the filldir itself, so we use a temp buffer..
2174 */
2175 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2176 {
2177 struct task_struct *leader_task = proc_task(dir);
2178 struct task_struct *task = leader_task;
2179 int nr_tids = 0;
2180
2181 index -= 2;
2182 read_lock(&tasklist_lock);
2183 /*
2184 * The starting point task (leader_task) might be an already
2185 * unlinked task, which cannot be used to access the task-list
2186 * via next_thread().
2187 */
2188 if (pid_alive(task)) do {
2189 int tid = task->pid;
2190
2191 if (--index >= 0)
2192 continue;
2193 if (tids != NULL)
2194 tids[nr_tids] = tid;
2195 nr_tids++;
2196 if (nr_tids >= PROC_MAXPIDS)
2197 break;
2198 } while ((task = next_thread(task)) != leader_task);
2199 read_unlock(&tasklist_lock);
2200 return nr_tids;
2201 }
2202
2203 /* for the /proc/ directory itself, after non-process stuff has been done */
2204 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2205 {
2206 unsigned int tgid_array[PROC_MAXPIDS];
2207 char buf[PROC_NUMBUF];
2208 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2209 unsigned int nr_tgids, i;
2210 int next_tgid;
2211
2212 if (!nr) {
2213 ino_t ino = fake_ino(0,PROC_TGID_INO);
2214 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2215 return 0;
2216 filp->f_pos++;
2217 nr++;
2218 }
2219
2220 /* f_version caches the tgid value that the last readdir call couldn't
2221 * return. lseek aka telldir automagically resets f_version to 0.
2222 */
2223 next_tgid = filp->f_version;
2224 filp->f_version = 0;
2225 for (;;) {
2226 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2227 if (!nr_tgids) {
2228 /* no more entries ! */
2229 break;
2230 }
2231 next_tgid = 0;
2232
2233 /* do not use the last found pid, reserve it for next_tgid */
2234 if (nr_tgids == PROC_MAXPIDS) {
2235 nr_tgids--;
2236 next_tgid = tgid_array[nr_tgids];
2237 }
2238
2239 for (i=0;i<nr_tgids;i++) {
2240 int tgid = tgid_array[i];
2241 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2242 unsigned long j = PROC_NUMBUF;
2243
2244 do
2245 buf[--j] = '0' + (tgid % 10);
2246 while ((tgid /= 10) != 0);
2247
2248 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2249 /* returning this tgid failed, save it as the first
2250 * pid for the next readir call */
2251 filp->f_version = tgid_array[i];
2252 goto out;
2253 }
2254 filp->f_pos++;
2255 nr++;
2256 }
2257 }
2258 out:
2259 return 0;
2260 }
2261
2262 /* for the /proc/TGID/task/ directories */
2263 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2264 {
2265 unsigned int tid_array[PROC_MAXPIDS];
2266 char buf[PROC_NUMBUF];
2267 unsigned int nr_tids, i;
2268 struct dentry *dentry = filp->f_dentry;
2269 struct inode *inode = dentry->d_inode;
2270 int retval = -ENOENT;
2271 ino_t ino;
2272 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2273
2274 if (!pid_alive(proc_task(inode)))
2275 goto out;
2276 retval = 0;
2277
2278 switch (pos) {
2279 case 0:
2280 ino = inode->i_ino;
2281 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2282 goto out;
2283 pos++;
2284 /* fall through */
2285 case 1:
2286 ino = parent_ino(dentry);
2287 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2288 goto out;
2289 pos++;
2290 /* fall through */
2291 }
2292
2293 nr_tids = get_tid_list(pos, tid_array, inode);
2294 inode->i_nlink = pos + nr_tids;
2295
2296 for (i = 0; i < nr_tids; i++) {
2297 unsigned long j = PROC_NUMBUF;
2298 int tid = tid_array[i];
2299
2300 ino = fake_ino(tid,PROC_TID_INO);
2301
2302 do
2303 buf[--j] = '0' + (tid % 10);
2304 while ((tid /= 10) != 0);
2305
2306 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
2307 break;
2308 pos++;
2309 }
2310 out:
2311 filp->f_pos = pos;
2312 return retval;
2313 }