[PATCH] proc: optimize proc_check_dentry_visible
[linux-2.6/kmemtrace.git] / fs / proc / base.c
blobf0db7f616ac3993bf6c594e2c1b9b301635d8822
1 /*
2 * linux/fs/proc/base.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
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.
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>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
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.
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
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.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
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"
77 /* NOTE:
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
88 * For hysterical raisins we keep the same inumbers as in the old procfs.
89 * Feel free to change the macro below - just keep the range distinct from
90 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
91 * As soon as we'll get a separate superblock we will be able to forget
92 * about magical ranges too.
95 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
97 enum pid_directory_inos {
98 PROC_TGID_INO = 2,
99 PROC_TGID_TASK,
100 PROC_TGID_STATUS,
101 PROC_TGID_MEM,
102 #ifdef CONFIG_SECCOMP
103 PROC_TGID_SECCOMP,
104 #endif
105 PROC_TGID_CWD,
106 PROC_TGID_ROOT,
107 PROC_TGID_EXE,
108 PROC_TGID_FD,
109 PROC_TGID_ENVIRON,
110 PROC_TGID_AUXV,
111 PROC_TGID_CMDLINE,
112 PROC_TGID_STAT,
113 PROC_TGID_STATM,
114 PROC_TGID_MAPS,
115 PROC_TGID_NUMA_MAPS,
116 PROC_TGID_MOUNTS,
117 PROC_TGID_MOUNTSTATS,
118 PROC_TGID_WCHAN,
119 #ifdef CONFIG_MMU
120 PROC_TGID_SMAPS,
121 #endif
122 #ifdef CONFIG_SCHEDSTATS
123 PROC_TGID_SCHEDSTAT,
124 #endif
125 #ifdef CONFIG_CPUSETS
126 PROC_TGID_CPUSET,
127 #endif
128 #ifdef CONFIG_SECURITY
129 PROC_TGID_ATTR,
130 PROC_TGID_ATTR_CURRENT,
131 PROC_TGID_ATTR_PREV,
132 PROC_TGID_ATTR_EXEC,
133 PROC_TGID_ATTR_FSCREATE,
134 PROC_TGID_ATTR_KEYCREATE,
135 #endif
136 #ifdef CONFIG_AUDITSYSCALL
137 PROC_TGID_LOGINUID,
138 #endif
139 PROC_TGID_OOM_SCORE,
140 PROC_TGID_OOM_ADJUST,
141 PROC_TID_INO,
142 PROC_TID_STATUS,
143 PROC_TID_MEM,
144 #ifdef CONFIG_SECCOMP
145 PROC_TID_SECCOMP,
146 #endif
147 PROC_TID_CWD,
148 PROC_TID_ROOT,
149 PROC_TID_EXE,
150 PROC_TID_FD,
151 PROC_TID_ENVIRON,
152 PROC_TID_AUXV,
153 PROC_TID_CMDLINE,
154 PROC_TID_STAT,
155 PROC_TID_STATM,
156 PROC_TID_MAPS,
157 PROC_TID_NUMA_MAPS,
158 PROC_TID_MOUNTS,
159 PROC_TID_MOUNTSTATS,
160 PROC_TID_WCHAN,
161 #ifdef CONFIG_MMU
162 PROC_TID_SMAPS,
163 #endif
164 #ifdef CONFIG_SCHEDSTATS
165 PROC_TID_SCHEDSTAT,
166 #endif
167 #ifdef CONFIG_CPUSETS
168 PROC_TID_CPUSET,
169 #endif
170 #ifdef CONFIG_SECURITY
171 PROC_TID_ATTR,
172 PROC_TID_ATTR_CURRENT,
173 PROC_TID_ATTR_PREV,
174 PROC_TID_ATTR_EXEC,
175 PROC_TID_ATTR_FSCREATE,
176 PROC_TID_ATTR_KEYCREATE,
177 #endif
178 #ifdef CONFIG_AUDITSYSCALL
179 PROC_TID_LOGINUID,
180 #endif
181 PROC_TID_OOM_SCORE,
182 PROC_TID_OOM_ADJUST,
184 /* Add new entries before this */
185 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
188 /* Worst case buffer size needed for holding an integer. */
189 #define PROC_NUMBUF 10
191 struct pid_entry {
192 int type;
193 int len;
194 char *name;
195 mode_t mode;
198 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
200 static struct pid_entry tgid_base_stuff[] = {
201 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
202 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
203 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
204 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
205 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
206 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
207 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
208 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
209 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
210 #ifdef CONFIG_NUMA
211 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
212 #endif
213 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
214 #ifdef CONFIG_SECCOMP
215 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
216 #endif
217 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
218 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
219 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
220 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
221 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
222 #ifdef CONFIG_MMU
223 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
224 #endif
225 #ifdef CONFIG_SECURITY
226 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
227 #endif
228 #ifdef CONFIG_KALLSYMS
229 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
230 #endif
231 #ifdef CONFIG_SCHEDSTATS
232 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
233 #endif
234 #ifdef CONFIG_CPUSETS
235 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
236 #endif
237 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
238 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
239 #ifdef CONFIG_AUDITSYSCALL
240 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
241 #endif
242 {0,0,NULL,0}
244 static struct pid_entry tid_base_stuff[] = {
245 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
246 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
247 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
248 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
249 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
250 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
251 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
252 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
253 #ifdef CONFIG_NUMA
254 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
255 #endif
256 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
257 #ifdef CONFIG_SECCOMP
258 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
259 #endif
260 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
261 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
262 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
263 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
264 #ifdef CONFIG_MMU
265 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
266 #endif
267 #ifdef CONFIG_SECURITY
268 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
269 #endif
270 #ifdef CONFIG_KALLSYMS
271 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
272 #endif
273 #ifdef CONFIG_SCHEDSTATS
274 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
275 #endif
276 #ifdef CONFIG_CPUSETS
277 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
278 #endif
279 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
280 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
281 #ifdef CONFIG_AUDITSYSCALL
282 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
283 #endif
284 {0,0,NULL,0}
287 #ifdef CONFIG_SECURITY
288 static struct pid_entry tgid_attr_stuff[] = {
289 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
290 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
291 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
292 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
293 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
294 {0,0,NULL,0}
296 static struct pid_entry tid_attr_stuff[] = {
297 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
298 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
299 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
300 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
301 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
302 {0,0,NULL,0}
304 #endif
306 #undef E
308 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
310 struct task_struct *task = get_proc_task(inode);
311 struct files_struct *files = NULL;
312 struct file *file;
313 int fd = proc_fd(inode);
315 if (task) {
316 files = get_files_struct(task);
317 put_task_struct(task);
319 if (files) {
321 * We are not taking a ref to the file structure, so we must
322 * hold ->file_lock.
324 spin_lock(&files->file_lock);
325 file = fcheck_files(files, fd);
326 if (file) {
327 *mnt = mntget(file->f_vfsmnt);
328 *dentry = dget(file->f_dentry);
329 spin_unlock(&files->file_lock);
330 put_files_struct(files);
331 return 0;
333 spin_unlock(&files->file_lock);
334 put_files_struct(files);
336 return -ENOENT;
339 static struct fs_struct *get_fs_struct(struct task_struct *task)
341 struct fs_struct *fs;
342 task_lock(task);
343 fs = task->fs;
344 if(fs)
345 atomic_inc(&fs->count);
346 task_unlock(task);
347 return fs;
350 static int get_nr_threads(struct task_struct *tsk)
352 /* Must be called with the rcu_read_lock held */
353 unsigned long flags;
354 int count = 0;
356 if (lock_task_sighand(tsk, &flags)) {
357 count = atomic_read(&tsk->signal->count);
358 unlock_task_sighand(tsk, &flags);
360 return count;
363 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
365 struct task_struct *task = get_proc_task(inode);
366 struct fs_struct *fs = NULL;
367 int result = -ENOENT;
369 if (task) {
370 fs = get_fs_struct(task);
371 put_task_struct(task);
373 if (fs) {
374 read_lock(&fs->lock);
375 *mnt = mntget(fs->pwdmnt);
376 *dentry = dget(fs->pwd);
377 read_unlock(&fs->lock);
378 result = 0;
379 put_fs_struct(fs);
381 return result;
384 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
386 struct task_struct *task = get_proc_task(inode);
387 struct fs_struct *fs = NULL;
388 int result = -ENOENT;
390 if (task) {
391 fs = get_fs_struct(task);
392 put_task_struct(task);
394 if (fs) {
395 read_lock(&fs->lock);
396 *mnt = mntget(fs->rootmnt);
397 *dentry = dget(fs->root);
398 read_unlock(&fs->lock);
399 result = 0;
400 put_fs_struct(fs);
402 return result;
405 #define MAY_PTRACE(task) \
406 (task == current || \
407 (task->parent == current && \
408 (task->ptrace & PT_PTRACED) && \
409 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
410 security_ptrace(current,task) == 0))
412 static int proc_pid_environ(struct task_struct *task, char * buffer)
414 int res = 0;
415 struct mm_struct *mm = get_task_mm(task);
416 if (mm) {
417 unsigned int len = mm->env_end - mm->env_start;
418 if (len > PAGE_SIZE)
419 len = PAGE_SIZE;
420 res = access_process_vm(task, mm->env_start, buffer, len, 0);
421 if (!ptrace_may_attach(task))
422 res = -ESRCH;
423 mmput(mm);
425 return res;
428 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
430 int res = 0;
431 unsigned int len;
432 struct mm_struct *mm = get_task_mm(task);
433 if (!mm)
434 goto out;
435 if (!mm->arg_end)
436 goto out_mm; /* Shh! No looking before we're done */
438 len = mm->arg_end - mm->arg_start;
440 if (len > PAGE_SIZE)
441 len = PAGE_SIZE;
443 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
445 // If the nul at the end of args has been overwritten, then
446 // assume application is using setproctitle(3).
447 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
448 len = strnlen(buffer, res);
449 if (len < res) {
450 res = len;
451 } else {
452 len = mm->env_end - mm->env_start;
453 if (len > PAGE_SIZE - res)
454 len = PAGE_SIZE - res;
455 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
456 res = strnlen(buffer, res);
459 out_mm:
460 mmput(mm);
461 out:
462 return res;
465 static int proc_pid_auxv(struct task_struct *task, char *buffer)
467 int res = 0;
468 struct mm_struct *mm = get_task_mm(task);
469 if (mm) {
470 unsigned int nwords = 0;
472 nwords += 2;
473 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
474 res = nwords * sizeof(mm->saved_auxv[0]);
475 if (res > PAGE_SIZE)
476 res = PAGE_SIZE;
477 memcpy(buffer, mm->saved_auxv, res);
478 mmput(mm);
480 return res;
484 #ifdef CONFIG_KALLSYMS
486 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
487 * Returns the resolved symbol. If that fails, simply return the address.
489 static int proc_pid_wchan(struct task_struct *task, char *buffer)
491 char *modname;
492 const char *sym_name;
493 unsigned long wchan, size, offset;
494 char namebuf[KSYM_NAME_LEN+1];
496 wchan = get_wchan(task);
498 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
499 if (sym_name)
500 return sprintf(buffer, "%s", sym_name);
501 return sprintf(buffer, "%lu", wchan);
503 #endif /* CONFIG_KALLSYMS */
505 #ifdef CONFIG_SCHEDSTATS
507 * Provides /proc/PID/schedstat
509 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
511 return sprintf(buffer, "%lu %lu %lu\n",
512 task->sched_info.cpu_time,
513 task->sched_info.run_delay,
514 task->sched_info.pcnt);
516 #endif
518 /* The badness from the OOM killer */
519 unsigned long badness(struct task_struct *p, unsigned long uptime);
520 static int proc_oom_score(struct task_struct *task, char *buffer)
522 unsigned long points;
523 struct timespec uptime;
525 do_posix_clock_monotonic_gettime(&uptime);
526 points = badness(task, uptime.tv_sec);
527 return sprintf(buffer, "%lu\n", points);
530 /************************************************************************/
531 /* Here the fs part begins */
532 /************************************************************************/
534 /* permission checks */
536 /* If the process being read is separated by chroot from the reading process,
537 * don't let the reader access the threads.
539 static int proc_check_chroot(struct dentry *de, struct vfsmount *mnt)
541 struct dentry *base;
542 struct vfsmount *our_vfsmnt;
543 int res = 0;
545 read_lock(&current->fs->lock);
546 our_vfsmnt = mntget(current->fs->rootmnt);
547 base = dget(current->fs->root);
548 read_unlock(&current->fs->lock);
550 spin_lock(&vfsmount_lock);
552 while (mnt != our_vfsmnt) {
553 if (mnt == mnt->mnt_parent)
554 goto out;
555 de = mnt->mnt_mountpoint;
556 mnt = mnt->mnt_parent;
559 if (!is_subdir(de, base))
560 goto out;
561 spin_unlock(&vfsmount_lock);
563 exit:
564 dput(base);
565 mntput(our_vfsmnt);
566 return res;
567 out:
568 spin_unlock(&vfsmount_lock);
569 res = -EACCES;
570 goto exit;
573 extern struct seq_operations mounts_op;
574 struct proc_mounts {
575 struct seq_file m;
576 int event;
579 static int mounts_open(struct inode *inode, struct file *file)
581 struct task_struct *task = get_proc_task(inode);
582 struct namespace *namespace = NULL;
583 struct proc_mounts *p;
584 int ret = -EINVAL;
586 if (task) {
587 task_lock(task);
588 namespace = task->namespace;
589 if (namespace)
590 get_namespace(namespace);
591 task_unlock(task);
592 put_task_struct(task);
595 if (namespace) {
596 ret = -ENOMEM;
597 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
598 if (p) {
599 file->private_data = &p->m;
600 ret = seq_open(file, &mounts_op);
601 if (!ret) {
602 p->m.private = namespace;
603 p->event = namespace->event;
604 return 0;
606 kfree(p);
608 put_namespace(namespace);
610 return ret;
613 static int mounts_release(struct inode *inode, struct file *file)
615 struct seq_file *m = file->private_data;
616 struct namespace *namespace = m->private;
617 put_namespace(namespace);
618 return seq_release(inode, file);
621 static unsigned mounts_poll(struct file *file, poll_table *wait)
623 struct proc_mounts *p = file->private_data;
624 struct namespace *ns = p->m.private;
625 unsigned res = 0;
627 poll_wait(file, &ns->poll, wait);
629 spin_lock(&vfsmount_lock);
630 if (p->event != ns->event) {
631 p->event = ns->event;
632 res = POLLERR;
634 spin_unlock(&vfsmount_lock);
636 return res;
639 static struct file_operations proc_mounts_operations = {
640 .open = mounts_open,
641 .read = seq_read,
642 .llseek = seq_lseek,
643 .release = mounts_release,
644 .poll = mounts_poll,
647 extern struct seq_operations mountstats_op;
648 static int mountstats_open(struct inode *inode, struct file *file)
650 int ret = seq_open(file, &mountstats_op);
652 if (!ret) {
653 struct seq_file *m = file->private_data;
654 struct namespace *namespace = NULL;
655 struct task_struct *task = get_proc_task(inode);
657 if (task) {
658 task_lock(task);
659 namespace = task->namespace;
660 if (namespace)
661 get_namespace(namespace);
662 task_unlock(task);
663 put_task_struct(task);
666 if (namespace)
667 m->private = namespace;
668 else {
669 seq_release(inode, file);
670 ret = -EINVAL;
673 return ret;
676 static struct file_operations proc_mountstats_operations = {
677 .open = mountstats_open,
678 .read = seq_read,
679 .llseek = seq_lseek,
680 .release = mounts_release,
683 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
685 static ssize_t proc_info_read(struct file * file, char __user * buf,
686 size_t count, loff_t *ppos)
688 struct inode * inode = file->f_dentry->d_inode;
689 unsigned long page;
690 ssize_t length;
691 struct task_struct *task = get_proc_task(inode);
693 length = -ESRCH;
694 if (!task)
695 goto out_no_task;
697 if (count > PROC_BLOCK_SIZE)
698 count = PROC_BLOCK_SIZE;
700 length = -ENOMEM;
701 if (!(page = __get_free_page(GFP_KERNEL)))
702 goto out;
704 length = PROC_I(inode)->op.proc_read(task, (char*)page);
706 if (length >= 0)
707 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
708 free_page(page);
709 out:
710 put_task_struct(task);
711 out_no_task:
712 return length;
715 static struct file_operations proc_info_file_operations = {
716 .read = proc_info_read,
719 static int mem_open(struct inode* inode, struct file* file)
721 file->private_data = (void*)((long)current->self_exec_id);
722 return 0;
725 static ssize_t mem_read(struct file * file, char __user * buf,
726 size_t count, loff_t *ppos)
728 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
729 char *page;
730 unsigned long src = *ppos;
731 int ret = -ESRCH;
732 struct mm_struct *mm;
734 if (!task)
735 goto out_no_task;
737 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
738 goto out;
740 ret = -ENOMEM;
741 page = (char *)__get_free_page(GFP_USER);
742 if (!page)
743 goto out;
745 ret = 0;
747 mm = get_task_mm(task);
748 if (!mm)
749 goto out_free;
751 ret = -EIO;
753 if (file->private_data != (void*)((long)current->self_exec_id))
754 goto out_put;
756 ret = 0;
758 while (count > 0) {
759 int this_len, retval;
761 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
762 retval = access_process_vm(task, src, page, this_len, 0);
763 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
764 if (!ret)
765 ret = -EIO;
766 break;
769 if (copy_to_user(buf, page, retval)) {
770 ret = -EFAULT;
771 break;
774 ret += retval;
775 src += retval;
776 buf += retval;
777 count -= retval;
779 *ppos = src;
781 out_put:
782 mmput(mm);
783 out_free:
784 free_page((unsigned long) page);
785 out:
786 put_task_struct(task);
787 out_no_task:
788 return ret;
791 #define mem_write NULL
793 #ifndef mem_write
794 /* This is a security hazard */
795 static ssize_t mem_write(struct file * file, const char * buf,
796 size_t count, loff_t *ppos)
798 int copied = 0;
799 char *page;
800 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
801 unsigned long dst = *ppos;
803 copied = -ESRCH;
804 if (!task)
805 goto out_no_task;
807 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
808 goto out;
810 copied = -ENOMEM;
811 page = (char *)__get_free_page(GFP_USER);
812 if (!page)
813 goto out;
815 while (count > 0) {
816 int this_len, retval;
818 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
819 if (copy_from_user(page, buf, this_len)) {
820 copied = -EFAULT;
821 break;
823 retval = access_process_vm(task, dst, page, this_len, 1);
824 if (!retval) {
825 if (!copied)
826 copied = -EIO;
827 break;
829 copied += retval;
830 buf += retval;
831 dst += retval;
832 count -= retval;
834 *ppos = dst;
835 free_page((unsigned long) page);
836 out:
837 put_task_struct(task);
838 out_no_task:
839 return copied;
841 #endif
843 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
845 switch (orig) {
846 case 0:
847 file->f_pos = offset;
848 break;
849 case 1:
850 file->f_pos += offset;
851 break;
852 default:
853 return -EINVAL;
855 force_successful_syscall_return();
856 return file->f_pos;
859 static struct file_operations proc_mem_operations = {
860 .llseek = mem_lseek,
861 .read = mem_read,
862 .write = mem_write,
863 .open = mem_open,
866 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
867 size_t count, loff_t *ppos)
869 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
870 char buffer[PROC_NUMBUF];
871 size_t len;
872 int oom_adjust;
873 loff_t __ppos = *ppos;
875 if (!task)
876 return -ESRCH;
877 oom_adjust = task->oomkilladj;
878 put_task_struct(task);
880 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
881 if (__ppos >= len)
882 return 0;
883 if (count > len-__ppos)
884 count = len-__ppos;
885 if (copy_to_user(buf, buffer + __ppos, count))
886 return -EFAULT;
887 *ppos = __ppos + count;
888 return count;
891 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
892 size_t count, loff_t *ppos)
894 struct task_struct *task;
895 char buffer[PROC_NUMBUF], *end;
896 int oom_adjust;
898 if (!capable(CAP_SYS_RESOURCE))
899 return -EPERM;
900 memset(buffer, 0, sizeof(buffer));
901 if (count > sizeof(buffer) - 1)
902 count = sizeof(buffer) - 1;
903 if (copy_from_user(buffer, buf, count))
904 return -EFAULT;
905 oom_adjust = simple_strtol(buffer, &end, 0);
906 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
907 return -EINVAL;
908 if (*end == '\n')
909 end++;
910 task = get_proc_task(file->f_dentry->d_inode);
911 if (!task)
912 return -ESRCH;
913 task->oomkilladj = oom_adjust;
914 put_task_struct(task);
915 if (end - buffer == 0)
916 return -EIO;
917 return end - buffer;
920 static struct file_operations proc_oom_adjust_operations = {
921 .read = oom_adjust_read,
922 .write = oom_adjust_write,
925 #ifdef CONFIG_AUDITSYSCALL
926 #define TMPBUFLEN 21
927 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
928 size_t count, loff_t *ppos)
930 struct inode * inode = file->f_dentry->d_inode;
931 struct task_struct *task = get_proc_task(inode);
932 ssize_t length;
933 char tmpbuf[TMPBUFLEN];
935 if (!task)
936 return -ESRCH;
937 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
938 audit_get_loginuid(task->audit_context));
939 put_task_struct(task);
940 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
943 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
944 size_t count, loff_t *ppos)
946 struct inode * inode = file->f_dentry->d_inode;
947 char *page, *tmp;
948 ssize_t length;
949 uid_t loginuid;
951 if (!capable(CAP_AUDIT_CONTROL))
952 return -EPERM;
954 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
955 return -EPERM;
957 if (count >= PAGE_SIZE)
958 count = PAGE_SIZE - 1;
960 if (*ppos != 0) {
961 /* No partial writes. */
962 return -EINVAL;
964 page = (char*)__get_free_page(GFP_USER);
965 if (!page)
966 return -ENOMEM;
967 length = -EFAULT;
968 if (copy_from_user(page, buf, count))
969 goto out_free_page;
971 page[count] = '\0';
972 loginuid = simple_strtoul(page, &tmp, 10);
973 if (tmp == page) {
974 length = -EINVAL;
975 goto out_free_page;
978 length = audit_set_loginuid(current, loginuid);
979 if (likely(length == 0))
980 length = count;
982 out_free_page:
983 free_page((unsigned long) page);
984 return length;
987 static struct file_operations proc_loginuid_operations = {
988 .read = proc_loginuid_read,
989 .write = proc_loginuid_write,
991 #endif
993 #ifdef CONFIG_SECCOMP
994 static ssize_t seccomp_read(struct file *file, char __user *buf,
995 size_t count, loff_t *ppos)
997 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
998 char __buf[20];
999 loff_t __ppos = *ppos;
1000 size_t len;
1002 if (!tsk)
1003 return -ESRCH;
1004 /* no need to print the trailing zero, so use only len */
1005 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1006 put_task_struct(tsk);
1007 if (__ppos >= len)
1008 return 0;
1009 if (count > len - __ppos)
1010 count = len - __ppos;
1011 if (copy_to_user(buf, __buf + __ppos, count))
1012 return -EFAULT;
1013 *ppos = __ppos + count;
1014 return count;
1017 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1018 size_t count, loff_t *ppos)
1020 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1021 char __buf[20], *end;
1022 unsigned int seccomp_mode;
1023 ssize_t result;
1025 result = -ESRCH;
1026 if (!tsk)
1027 goto out_no_task;
1029 /* can set it only once to be even more secure */
1030 result = -EPERM;
1031 if (unlikely(tsk->seccomp.mode))
1032 goto out;
1034 result = -EFAULT;
1035 memset(__buf, 0, sizeof(__buf));
1036 count = min(count, sizeof(__buf) - 1);
1037 if (copy_from_user(__buf, buf, count))
1038 goto out;
1040 seccomp_mode = simple_strtoul(__buf, &end, 0);
1041 if (*end == '\n')
1042 end++;
1043 result = -EINVAL;
1044 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1045 tsk->seccomp.mode = seccomp_mode;
1046 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1047 } else
1048 goto out;
1049 result = -EIO;
1050 if (unlikely(!(end - __buf)))
1051 goto out;
1052 result = end - __buf;
1053 out:
1054 put_task_struct(tsk);
1055 out_no_task:
1056 return result;
1059 static struct file_operations proc_seccomp_operations = {
1060 .read = seccomp_read,
1061 .write = seccomp_write,
1063 #endif /* CONFIG_SECCOMP */
1065 static int proc_check_dentry_visible(struct inode *inode,
1066 struct dentry *dentry, struct vfsmount *mnt)
1068 /* Verify that the current process can already see the
1069 * file pointed at by the file descriptor.
1070 * This prevents /proc from being an accidental information leak.
1072 * This prevents access to files that are not visible do to
1073 * being on the otherside of a chroot, in a different
1074 * namespace, or are simply process local (like pipes).
1076 struct task_struct *task;
1077 int error = -EACCES;
1079 /* See if the the two tasks share a commone set of
1080 * file descriptors. If so everything is visible.
1082 rcu_read_lock();
1083 task = tref_task(proc_tref(inode));
1084 if (task) {
1085 struct files_struct *task_files, *files;
1086 /* This test answeres the question:
1087 * Is there a point in time since we looked up the
1088 * file descriptor where the two tasks share the
1089 * same files struct?
1091 rmb();
1092 files = current->files;
1093 task_files = task->files;
1094 if (files && (files == task_files))
1095 error = 0;
1097 rcu_read_unlock();
1098 if (!error)
1099 goto out;
1101 /* If the two tasks don't share a common set of file
1102 * descriptors see if the destination dentry is already
1103 * visible in the current tasks filesystem namespace.
1105 error = proc_check_chroot(dentry, mnt);
1106 out:
1107 return error;
1111 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1113 struct inode *inode = dentry->d_inode;
1114 int error = -EACCES;
1116 /* We don't need a base pointer in the /proc filesystem */
1117 path_release(nd);
1119 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1120 goto out;
1122 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1123 nd->last_type = LAST_BIND;
1124 if (error)
1125 goto out;
1127 /* Only return files this task can already see */
1128 error = proc_check_dentry_visible(inode, nd->dentry, nd->mnt);
1129 if (error)
1130 path_release(nd);
1131 out:
1132 return ERR_PTR(error);
1135 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1136 char __user *buffer, int buflen)
1138 struct inode * inode;
1139 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1140 int len;
1142 if (!tmp)
1143 return -ENOMEM;
1145 inode = dentry->d_inode;
1146 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1147 len = PTR_ERR(path);
1148 if (IS_ERR(path))
1149 goto out;
1150 len = tmp + PAGE_SIZE - 1 - path;
1152 if (len > buflen)
1153 len = buflen;
1154 if (copy_to_user(buffer, path, len))
1155 len = -EFAULT;
1156 out:
1157 free_page((unsigned long)tmp);
1158 return len;
1161 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1163 int error = -EACCES;
1164 struct inode *inode = dentry->d_inode;
1165 struct dentry *de;
1166 struct vfsmount *mnt = NULL;
1169 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1170 goto out;
1172 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1173 if (error)
1174 goto out;
1176 /* Only return files this task can already see */
1177 error = proc_check_dentry_visible(inode, de, mnt);
1178 if (error)
1179 goto out_put;
1181 error = do_proc_readlink(de, mnt, buffer, buflen);
1182 out_put:
1183 dput(de);
1184 mntput(mnt);
1185 out:
1186 return error;
1189 static struct inode_operations proc_pid_link_inode_operations = {
1190 .readlink = proc_pid_readlink,
1191 .follow_link = proc_pid_follow_link
1194 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1196 struct dentry *dentry = filp->f_dentry;
1197 struct inode *inode = dentry->d_inode;
1198 struct task_struct *p = get_proc_task(inode);
1199 unsigned int fd, tid, ino;
1200 int retval;
1201 char buf[PROC_NUMBUF];
1202 struct files_struct * files;
1203 struct fdtable *fdt;
1205 retval = -ENOENT;
1206 if (!p)
1207 goto out_no_task;
1208 retval = 0;
1209 tid = p->pid;
1211 fd = filp->f_pos;
1212 switch (fd) {
1213 case 0:
1214 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1215 goto out;
1216 filp->f_pos++;
1217 case 1:
1218 ino = parent_ino(dentry);
1219 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1220 goto out;
1221 filp->f_pos++;
1222 default:
1223 files = get_files_struct(p);
1224 if (!files)
1225 goto out;
1226 rcu_read_lock();
1227 fdt = files_fdtable(files);
1228 for (fd = filp->f_pos-2;
1229 fd < fdt->max_fds;
1230 fd++, filp->f_pos++) {
1231 unsigned int i,j;
1233 if (!fcheck_files(files, fd))
1234 continue;
1235 rcu_read_unlock();
1237 j = PROC_NUMBUF;
1238 i = fd;
1239 do {
1240 j--;
1241 buf[j] = '0' + (i % 10);
1242 i /= 10;
1243 } while (i);
1245 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1246 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1247 rcu_read_lock();
1248 break;
1250 rcu_read_lock();
1252 rcu_read_unlock();
1253 put_files_struct(files);
1255 out:
1256 put_task_struct(p);
1257 out_no_task:
1258 return retval;
1261 static int proc_pident_readdir(struct file *filp,
1262 void *dirent, filldir_t filldir,
1263 struct pid_entry *ents, unsigned int nents)
1265 int i;
1266 int pid;
1267 struct dentry *dentry = filp->f_dentry;
1268 struct inode *inode = dentry->d_inode;
1269 struct task_struct *task = get_proc_task(inode);
1270 struct pid_entry *p;
1271 ino_t ino;
1272 int ret;
1274 ret = -ENOENT;
1275 if (!task)
1276 goto out;
1278 ret = 0;
1279 pid = task->pid;
1280 put_task_struct(task);
1281 i = filp->f_pos;
1282 switch (i) {
1283 case 0:
1284 ino = inode->i_ino;
1285 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1286 goto out;
1287 i++;
1288 filp->f_pos++;
1289 /* fall through */
1290 case 1:
1291 ino = parent_ino(dentry);
1292 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1293 goto out;
1294 i++;
1295 filp->f_pos++;
1296 /* fall through */
1297 default:
1298 i -= 2;
1299 if (i >= nents) {
1300 ret = 1;
1301 goto out;
1303 p = ents + i;
1304 while (p->name) {
1305 if (filldir(dirent, p->name, p->len, filp->f_pos,
1306 fake_ino(pid, p->type), p->mode >> 12) < 0)
1307 goto out;
1308 filp->f_pos++;
1309 p++;
1313 ret = 1;
1314 out:
1315 return ret;
1318 static int proc_tgid_base_readdir(struct file * filp,
1319 void * dirent, filldir_t filldir)
1321 return proc_pident_readdir(filp,dirent,filldir,
1322 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1325 static int proc_tid_base_readdir(struct file * filp,
1326 void * dirent, filldir_t filldir)
1328 return proc_pident_readdir(filp,dirent,filldir,
1329 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1332 /* building an inode */
1334 static int task_dumpable(struct task_struct *task)
1336 int dumpable = 0;
1337 struct mm_struct *mm;
1339 task_lock(task);
1340 mm = task->mm;
1341 if (mm)
1342 dumpable = mm->dumpable;
1343 task_unlock(task);
1344 if(dumpable == 1)
1345 return 1;
1346 return 0;
1350 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1352 struct inode * inode;
1353 struct proc_inode *ei;
1355 /* We need a new inode */
1357 inode = new_inode(sb);
1358 if (!inode)
1359 goto out;
1361 /* Common stuff */
1362 ei = PROC_I(inode);
1363 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1364 inode->i_ino = fake_ino(task->pid, ino);
1367 * grab the reference to task.
1369 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1370 if (!ei->pid)
1371 goto out_unlock;
1373 inode->i_uid = 0;
1374 inode->i_gid = 0;
1375 if (task_dumpable(task)) {
1376 inode->i_uid = task->euid;
1377 inode->i_gid = task->egid;
1379 security_task_to_inode(task, inode);
1381 out:
1382 return inode;
1384 out_unlock:
1385 iput(inode);
1386 return NULL;
1389 /* dentry stuff */
1392 * Exceptional case: normally we are not allowed to unhash a busy
1393 * directory. In this case, however, we can do it - no aliasing problems
1394 * due to the way we treat inodes.
1396 * Rewrite the inode's ownerships here because the owning task may have
1397 * performed a setuid(), etc.
1399 * Before the /proc/pid/status file was created the only way to read
1400 * the effective uid of a /process was to stat /proc/pid. Reading
1401 * /proc/pid/status is slow enough that procps and other packages
1402 * kept stating /proc/pid. To keep the rules in /proc simple I have
1403 * made this apply to all per process world readable and executable
1404 * directories.
1406 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1408 struct inode *inode = dentry->d_inode;
1409 struct task_struct *task = get_proc_task(inode);
1410 if (task) {
1411 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1412 task_dumpable(task)) {
1413 inode->i_uid = task->euid;
1414 inode->i_gid = task->egid;
1415 } else {
1416 inode->i_uid = 0;
1417 inode->i_gid = 0;
1419 security_task_to_inode(task, inode);
1420 put_task_struct(task);
1421 return 1;
1423 d_drop(dentry);
1424 return 0;
1427 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1429 struct inode *inode = dentry->d_inode;
1430 struct task_struct *task;
1431 generic_fillattr(inode, stat);
1433 rcu_read_lock();
1434 stat->uid = 0;
1435 stat->gid = 0;
1436 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1437 if (task) {
1438 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1439 task_dumpable(task)) {
1440 stat->uid = task->euid;
1441 stat->gid = task->egid;
1444 rcu_read_unlock();
1445 return 0;
1448 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1450 struct inode *inode = dentry->d_inode;
1451 struct task_struct *task = get_proc_task(inode);
1452 int fd = proc_fd(inode);
1453 struct files_struct *files;
1455 if (task) {
1456 files = get_files_struct(task);
1457 if (files) {
1458 rcu_read_lock();
1459 if (fcheck_files(files, fd)) {
1460 rcu_read_unlock();
1461 put_files_struct(files);
1462 if (task_dumpable(task)) {
1463 inode->i_uid = task->euid;
1464 inode->i_gid = task->egid;
1465 } else {
1466 inode->i_uid = 0;
1467 inode->i_gid = 0;
1469 security_task_to_inode(task, inode);
1470 put_task_struct(task);
1471 return 1;
1473 rcu_read_unlock();
1474 put_files_struct(files);
1476 put_task_struct(task);
1478 d_drop(dentry);
1479 return 0;
1482 static int pid_delete_dentry(struct dentry * dentry)
1484 /* Is the task we represent dead?
1485 * If so, then don't put the dentry on the lru list,
1486 * kill it immediately.
1488 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1491 static struct dentry_operations tid_fd_dentry_operations =
1493 .d_revalidate = tid_fd_revalidate,
1494 .d_delete = pid_delete_dentry,
1497 static struct dentry_operations pid_dentry_operations =
1499 .d_revalidate = pid_revalidate,
1500 .d_delete = pid_delete_dentry,
1503 /* Lookups */
1505 static unsigned name_to_int(struct dentry *dentry)
1507 const char *name = dentry->d_name.name;
1508 int len = dentry->d_name.len;
1509 unsigned n = 0;
1511 if (len > 1 && *name == '0')
1512 goto out;
1513 while (len-- > 0) {
1514 unsigned c = *name++ - '0';
1515 if (c > 9)
1516 goto out;
1517 if (n >= (~0U-9)/10)
1518 goto out;
1519 n *= 10;
1520 n += c;
1522 return n;
1523 out:
1524 return ~0U;
1527 /* SMP-safe */
1528 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1530 struct task_struct *task = get_proc_task(dir);
1531 unsigned fd = name_to_int(dentry);
1532 struct dentry *result = ERR_PTR(-ENOENT);
1533 struct file * file;
1534 struct files_struct * files;
1535 struct inode *inode;
1536 struct proc_inode *ei;
1538 if (!task)
1539 goto out_no_task;
1540 if (fd == ~0U)
1541 goto out;
1543 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1544 if (!inode)
1545 goto out;
1546 ei = PROC_I(inode);
1547 ei->fd = fd;
1548 files = get_files_struct(task);
1549 if (!files)
1550 goto out_unlock;
1551 inode->i_mode = S_IFLNK;
1554 * We are not taking a ref to the file structure, so we must
1555 * hold ->file_lock.
1557 spin_lock(&files->file_lock);
1558 file = fcheck_files(files, fd);
1559 if (!file)
1560 goto out_unlock2;
1561 if (file->f_mode & 1)
1562 inode->i_mode |= S_IRUSR | S_IXUSR;
1563 if (file->f_mode & 2)
1564 inode->i_mode |= S_IWUSR | S_IXUSR;
1565 spin_unlock(&files->file_lock);
1566 put_files_struct(files);
1567 inode->i_op = &proc_pid_link_inode_operations;
1568 inode->i_size = 64;
1569 ei->op.proc_get_link = proc_fd_link;
1570 dentry->d_op = &tid_fd_dentry_operations;
1571 d_add(dentry, inode);
1572 /* Close the race of the process dying before we return the dentry */
1573 if (tid_fd_revalidate(dentry, NULL))
1574 result = NULL;
1575 out:
1576 put_task_struct(task);
1577 out_no_task:
1578 return result;
1580 out_unlock2:
1581 spin_unlock(&files->file_lock);
1582 put_files_struct(files);
1583 out_unlock:
1584 iput(inode);
1585 goto out;
1588 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1589 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1590 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
1592 static struct file_operations proc_fd_operations = {
1593 .read = generic_read_dir,
1594 .readdir = proc_readfd,
1597 static struct file_operations proc_task_operations = {
1598 .read = generic_read_dir,
1599 .readdir = proc_task_readdir,
1603 * proc directories can do almost nothing..
1605 static struct inode_operations proc_fd_inode_operations = {
1606 .lookup = proc_lookupfd,
1609 static struct inode_operations proc_task_inode_operations = {
1610 .lookup = proc_task_lookup,
1611 .getattr = proc_task_getattr,
1614 #ifdef CONFIG_SECURITY
1615 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1616 size_t count, loff_t *ppos)
1618 struct inode * inode = file->f_dentry->d_inode;
1619 unsigned long page;
1620 ssize_t length;
1621 struct task_struct *task = get_proc_task(inode);
1623 length = -ESRCH;
1624 if (!task)
1625 goto out_no_task;
1627 if (count > PAGE_SIZE)
1628 count = PAGE_SIZE;
1629 length = -ENOMEM;
1630 if (!(page = __get_free_page(GFP_KERNEL)))
1631 goto out;
1633 length = security_getprocattr(task,
1634 (char*)file->f_dentry->d_name.name,
1635 (void*)page, count);
1636 if (length >= 0)
1637 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1638 free_page(page);
1639 out:
1640 put_task_struct(task);
1641 out_no_task:
1642 return length;
1645 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1646 size_t count, loff_t *ppos)
1648 struct inode * inode = file->f_dentry->d_inode;
1649 char *page;
1650 ssize_t length;
1651 struct task_struct *task = get_proc_task(inode);
1653 length = -ESRCH;
1654 if (!task)
1655 goto out_no_task;
1656 if (count > PAGE_SIZE)
1657 count = PAGE_SIZE;
1659 /* No partial writes. */
1660 length = -EINVAL;
1661 if (*ppos != 0)
1662 goto out;
1664 length = -ENOMEM;
1665 page = (char*)__get_free_page(GFP_USER);
1666 if (!page)
1667 goto out;
1669 length = -EFAULT;
1670 if (copy_from_user(page, buf, count))
1671 goto out_free;
1673 length = security_setprocattr(task,
1674 (char*)file->f_dentry->d_name.name,
1675 (void*)page, count);
1676 out_free:
1677 free_page((unsigned long) page);
1678 out:
1679 put_task_struct(task);
1680 out_no_task:
1681 return length;
1684 static struct file_operations proc_pid_attr_operations = {
1685 .read = proc_pid_attr_read,
1686 .write = proc_pid_attr_write,
1689 static struct file_operations proc_tid_attr_operations;
1690 static struct inode_operations proc_tid_attr_inode_operations;
1691 static struct file_operations proc_tgid_attr_operations;
1692 static struct inode_operations proc_tgid_attr_inode_operations;
1693 #endif
1695 /* SMP-safe */
1696 static struct dentry *proc_pident_lookup(struct inode *dir,
1697 struct dentry *dentry,
1698 struct pid_entry *ents)
1700 struct inode *inode;
1701 struct dentry *error;
1702 struct task_struct *task = get_proc_task(dir);
1703 struct pid_entry *p;
1704 struct proc_inode *ei;
1706 error = ERR_PTR(-ENOENT);
1707 inode = NULL;
1709 if (!task)
1710 goto out_no_task;
1712 for (p = ents; p->name; p++) {
1713 if (p->len != dentry->d_name.len)
1714 continue;
1715 if (!memcmp(dentry->d_name.name, p->name, p->len))
1716 break;
1718 if (!p->name)
1719 goto out;
1721 error = ERR_PTR(-EINVAL);
1722 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1723 if (!inode)
1724 goto out;
1726 ei = PROC_I(inode);
1727 inode->i_mode = p->mode;
1729 * Yes, it does not scale. And it should not. Don't add
1730 * new entries into /proc/<tgid>/ without very good reasons.
1732 switch(p->type) {
1733 case PROC_TGID_TASK:
1734 inode->i_nlink = 2;
1735 inode->i_op = &proc_task_inode_operations;
1736 inode->i_fop = &proc_task_operations;
1737 break;
1738 case PROC_TID_FD:
1739 case PROC_TGID_FD:
1740 inode->i_nlink = 2;
1741 inode->i_op = &proc_fd_inode_operations;
1742 inode->i_fop = &proc_fd_operations;
1743 break;
1744 case PROC_TID_EXE:
1745 case PROC_TGID_EXE:
1746 inode->i_op = &proc_pid_link_inode_operations;
1747 ei->op.proc_get_link = proc_exe_link;
1748 break;
1749 case PROC_TID_CWD:
1750 case PROC_TGID_CWD:
1751 inode->i_op = &proc_pid_link_inode_operations;
1752 ei->op.proc_get_link = proc_cwd_link;
1753 break;
1754 case PROC_TID_ROOT:
1755 case PROC_TGID_ROOT:
1756 inode->i_op = &proc_pid_link_inode_operations;
1757 ei->op.proc_get_link = proc_root_link;
1758 break;
1759 case PROC_TID_ENVIRON:
1760 case PROC_TGID_ENVIRON:
1761 inode->i_fop = &proc_info_file_operations;
1762 ei->op.proc_read = proc_pid_environ;
1763 break;
1764 case PROC_TID_AUXV:
1765 case PROC_TGID_AUXV:
1766 inode->i_fop = &proc_info_file_operations;
1767 ei->op.proc_read = proc_pid_auxv;
1768 break;
1769 case PROC_TID_STATUS:
1770 case PROC_TGID_STATUS:
1771 inode->i_fop = &proc_info_file_operations;
1772 ei->op.proc_read = proc_pid_status;
1773 break;
1774 case PROC_TID_STAT:
1775 inode->i_fop = &proc_info_file_operations;
1776 ei->op.proc_read = proc_tid_stat;
1777 break;
1778 case PROC_TGID_STAT:
1779 inode->i_fop = &proc_info_file_operations;
1780 ei->op.proc_read = proc_tgid_stat;
1781 break;
1782 case PROC_TID_CMDLINE:
1783 case PROC_TGID_CMDLINE:
1784 inode->i_fop = &proc_info_file_operations;
1785 ei->op.proc_read = proc_pid_cmdline;
1786 break;
1787 case PROC_TID_STATM:
1788 case PROC_TGID_STATM:
1789 inode->i_fop = &proc_info_file_operations;
1790 ei->op.proc_read = proc_pid_statm;
1791 break;
1792 case PROC_TID_MAPS:
1793 case PROC_TGID_MAPS:
1794 inode->i_fop = &proc_maps_operations;
1795 break;
1796 #ifdef CONFIG_NUMA
1797 case PROC_TID_NUMA_MAPS:
1798 case PROC_TGID_NUMA_MAPS:
1799 inode->i_fop = &proc_numa_maps_operations;
1800 break;
1801 #endif
1802 case PROC_TID_MEM:
1803 case PROC_TGID_MEM:
1804 inode->i_fop = &proc_mem_operations;
1805 break;
1806 #ifdef CONFIG_SECCOMP
1807 case PROC_TID_SECCOMP:
1808 case PROC_TGID_SECCOMP:
1809 inode->i_fop = &proc_seccomp_operations;
1810 break;
1811 #endif /* CONFIG_SECCOMP */
1812 case PROC_TID_MOUNTS:
1813 case PROC_TGID_MOUNTS:
1814 inode->i_fop = &proc_mounts_operations;
1815 break;
1816 #ifdef CONFIG_MMU
1817 case PROC_TID_SMAPS:
1818 case PROC_TGID_SMAPS:
1819 inode->i_fop = &proc_smaps_operations;
1820 break;
1821 #endif
1822 case PROC_TID_MOUNTSTATS:
1823 case PROC_TGID_MOUNTSTATS:
1824 inode->i_fop = &proc_mountstats_operations;
1825 break;
1826 #ifdef CONFIG_SECURITY
1827 case PROC_TID_ATTR:
1828 inode->i_nlink = 2;
1829 inode->i_op = &proc_tid_attr_inode_operations;
1830 inode->i_fop = &proc_tid_attr_operations;
1831 break;
1832 case PROC_TGID_ATTR:
1833 inode->i_nlink = 2;
1834 inode->i_op = &proc_tgid_attr_inode_operations;
1835 inode->i_fop = &proc_tgid_attr_operations;
1836 break;
1837 case PROC_TID_ATTR_CURRENT:
1838 case PROC_TGID_ATTR_CURRENT:
1839 case PROC_TID_ATTR_PREV:
1840 case PROC_TGID_ATTR_PREV:
1841 case PROC_TID_ATTR_EXEC:
1842 case PROC_TGID_ATTR_EXEC:
1843 case PROC_TID_ATTR_FSCREATE:
1844 case PROC_TGID_ATTR_FSCREATE:
1845 case PROC_TID_ATTR_KEYCREATE:
1846 case PROC_TGID_ATTR_KEYCREATE:
1847 inode->i_fop = &proc_pid_attr_operations;
1848 break;
1849 #endif
1850 #ifdef CONFIG_KALLSYMS
1851 case PROC_TID_WCHAN:
1852 case PROC_TGID_WCHAN:
1853 inode->i_fop = &proc_info_file_operations;
1854 ei->op.proc_read = proc_pid_wchan;
1855 break;
1856 #endif
1857 #ifdef CONFIG_SCHEDSTATS
1858 case PROC_TID_SCHEDSTAT:
1859 case PROC_TGID_SCHEDSTAT:
1860 inode->i_fop = &proc_info_file_operations;
1861 ei->op.proc_read = proc_pid_schedstat;
1862 break;
1863 #endif
1864 #ifdef CONFIG_CPUSETS
1865 case PROC_TID_CPUSET:
1866 case PROC_TGID_CPUSET:
1867 inode->i_fop = &proc_cpuset_operations;
1868 break;
1869 #endif
1870 case PROC_TID_OOM_SCORE:
1871 case PROC_TGID_OOM_SCORE:
1872 inode->i_fop = &proc_info_file_operations;
1873 ei->op.proc_read = proc_oom_score;
1874 break;
1875 case PROC_TID_OOM_ADJUST:
1876 case PROC_TGID_OOM_ADJUST:
1877 inode->i_fop = &proc_oom_adjust_operations;
1878 break;
1879 #ifdef CONFIG_AUDITSYSCALL
1880 case PROC_TID_LOGINUID:
1881 case PROC_TGID_LOGINUID:
1882 inode->i_fop = &proc_loginuid_operations;
1883 break;
1884 #endif
1885 default:
1886 printk("procfs: impossible type (%d)",p->type);
1887 iput(inode);
1888 error = ERR_PTR(-EINVAL);
1889 goto out;
1891 dentry->d_op = &pid_dentry_operations;
1892 d_add(dentry, inode);
1893 /* Close the race of the process dying before we return the dentry */
1894 if (pid_revalidate(dentry, NULL))
1895 error = NULL;
1896 out:
1897 put_task_struct(task);
1898 out_no_task:
1899 return error;
1902 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1903 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1906 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1907 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1910 static struct file_operations proc_tgid_base_operations = {
1911 .read = generic_read_dir,
1912 .readdir = proc_tgid_base_readdir,
1915 static struct file_operations proc_tid_base_operations = {
1916 .read = generic_read_dir,
1917 .readdir = proc_tid_base_readdir,
1920 static struct inode_operations proc_tgid_base_inode_operations = {
1921 .lookup = proc_tgid_base_lookup,
1922 .getattr = pid_getattr,
1925 static struct inode_operations proc_tid_base_inode_operations = {
1926 .lookup = proc_tid_base_lookup,
1927 .getattr = pid_getattr,
1930 #ifdef CONFIG_SECURITY
1931 static int proc_tgid_attr_readdir(struct file * filp,
1932 void * dirent, filldir_t filldir)
1934 return proc_pident_readdir(filp,dirent,filldir,
1935 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1938 static int proc_tid_attr_readdir(struct file * filp,
1939 void * dirent, filldir_t filldir)
1941 return proc_pident_readdir(filp,dirent,filldir,
1942 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1945 static struct file_operations proc_tgid_attr_operations = {
1946 .read = generic_read_dir,
1947 .readdir = proc_tgid_attr_readdir,
1950 static struct file_operations proc_tid_attr_operations = {
1951 .read = generic_read_dir,
1952 .readdir = proc_tid_attr_readdir,
1955 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1956 struct dentry *dentry, struct nameidata *nd)
1958 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1961 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1962 struct dentry *dentry, struct nameidata *nd)
1964 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1967 static struct inode_operations proc_tgid_attr_inode_operations = {
1968 .lookup = proc_tgid_attr_lookup,
1969 .getattr = pid_getattr,
1972 static struct inode_operations proc_tid_attr_inode_operations = {
1973 .lookup = proc_tid_attr_lookup,
1974 .getattr = pid_getattr,
1976 #endif
1979 * /proc/self:
1981 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1982 int buflen)
1984 char tmp[PROC_NUMBUF];
1985 sprintf(tmp, "%d", current->tgid);
1986 return vfs_readlink(dentry,buffer,buflen,tmp);
1989 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1991 char tmp[PROC_NUMBUF];
1992 sprintf(tmp, "%d", current->tgid);
1993 return ERR_PTR(vfs_follow_link(nd,tmp));
1996 static struct inode_operations proc_self_inode_operations = {
1997 .readlink = proc_self_readlink,
1998 .follow_link = proc_self_follow_link,
2002 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2004 * @task: task that should be flushed.
2006 * Looks in the dcache for
2007 * /proc/@pid
2008 * /proc/@tgid/task/@pid
2009 * if either directory is present flushes it and all of it'ts children
2010 * from the dcache.
2012 * It is safe and reasonable to cache /proc entries for a task until
2013 * that task exits. After that they just clog up the dcache with
2014 * useless entries, possibly causing useful dcache entries to be
2015 * flushed instead. This routine is proved to flush those useless
2016 * dcache entries at process exit time.
2018 * NOTE: This routine is just an optimization so it does not guarantee
2019 * that no dcache entries will exist at process exit time it
2020 * just makes it very unlikely that any will persist.
2022 void proc_flush_task(struct task_struct *task)
2024 struct dentry *dentry, *leader, *dir;
2025 char buf[PROC_NUMBUF];
2026 struct qstr name;
2028 name.name = buf;
2029 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2030 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2031 if (dentry) {
2032 shrink_dcache_parent(dentry);
2033 d_drop(dentry);
2034 dput(dentry);
2037 if (thread_group_leader(task))
2038 goto out;
2040 name.name = buf;
2041 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2042 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2043 if (!leader)
2044 goto out;
2046 name.name = "task";
2047 name.len = strlen(name.name);
2048 dir = d_hash_and_lookup(leader, &name);
2049 if (!dir)
2050 goto out_put_leader;
2052 name.name = buf;
2053 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2054 dentry = d_hash_and_lookup(dir, &name);
2055 if (dentry) {
2056 shrink_dcache_parent(dentry);
2057 d_drop(dentry);
2058 dput(dentry);
2061 dput(dir);
2062 out_put_leader:
2063 dput(leader);
2064 out:
2065 return;
2068 /* SMP-safe */
2069 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2071 struct dentry *result = ERR_PTR(-ENOENT);
2072 struct task_struct *task;
2073 struct inode *inode;
2074 struct proc_inode *ei;
2075 unsigned tgid;
2077 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2078 inode = new_inode(dir->i_sb);
2079 if (!inode)
2080 return ERR_PTR(-ENOMEM);
2081 ei = PROC_I(inode);
2082 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2083 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2084 ei->pde = NULL;
2085 inode->i_mode = S_IFLNK|S_IRWXUGO;
2086 inode->i_uid = inode->i_gid = 0;
2087 inode->i_size = 64;
2088 inode->i_op = &proc_self_inode_operations;
2089 d_add(dentry, inode);
2090 return NULL;
2092 tgid = name_to_int(dentry);
2093 if (tgid == ~0U)
2094 goto out;
2096 rcu_read_lock();
2097 task = find_task_by_pid(tgid);
2098 if (task)
2099 get_task_struct(task);
2100 rcu_read_unlock();
2101 if (!task)
2102 goto out;
2104 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2105 if (!inode)
2106 goto out_put_task;
2108 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2109 inode->i_op = &proc_tgid_base_inode_operations;
2110 inode->i_fop = &proc_tgid_base_operations;
2111 inode->i_flags|=S_IMMUTABLE;
2112 #ifdef CONFIG_SECURITY
2113 inode->i_nlink = 5;
2114 #else
2115 inode->i_nlink = 4;
2116 #endif
2118 dentry->d_op = &pid_dentry_operations;
2120 d_add(dentry, inode);
2121 /* Close the race of the process dying before we return the dentry */
2122 if (pid_revalidate(dentry, NULL))
2123 result = NULL;
2125 out_put_task:
2126 put_task_struct(task);
2127 out:
2128 return result;
2131 /* SMP-safe */
2132 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2134 struct dentry *result = ERR_PTR(-ENOENT);
2135 struct task_struct *task;
2136 struct task_struct *leader = get_proc_task(dir);
2137 struct inode *inode;
2138 unsigned tid;
2140 if (!leader)
2141 goto out_no_task;
2143 tid = name_to_int(dentry);
2144 if (tid == ~0U)
2145 goto out;
2147 rcu_read_lock();
2148 task = find_task_by_pid(tid);
2149 if (task)
2150 get_task_struct(task);
2151 rcu_read_unlock();
2152 if (!task)
2153 goto out;
2154 if (leader->tgid != task->tgid)
2155 goto out_drop_task;
2157 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2160 if (!inode)
2161 goto out_drop_task;
2162 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2163 inode->i_op = &proc_tid_base_inode_operations;
2164 inode->i_fop = &proc_tid_base_operations;
2165 inode->i_flags|=S_IMMUTABLE;
2166 #ifdef CONFIG_SECURITY
2167 inode->i_nlink = 4;
2168 #else
2169 inode->i_nlink = 3;
2170 #endif
2172 dentry->d_op = &pid_dentry_operations;
2174 d_add(dentry, inode);
2175 /* Close the race of the process dying before we return the dentry */
2176 if (pid_revalidate(dentry, NULL))
2177 result = NULL;
2179 out_drop_task:
2180 put_task_struct(task);
2181 out:
2182 put_task_struct(leader);
2183 out_no_task:
2184 return result;
2188 * Find the first tgid to return to user space.
2190 * Usually this is just whatever follows &init_task, but if the users
2191 * buffer was too small to hold the full list or there was a seek into
2192 * the middle of the directory we have more work to do.
2194 * In the case of a short read we start with find_task_by_pid.
2196 * In the case of a seek we start with &init_task and walk nr
2197 * threads past it.
2199 static struct task_struct *first_tgid(int tgid, unsigned int nr)
2201 struct task_struct *pos;
2202 rcu_read_lock();
2203 if (tgid && nr) {
2204 pos = find_task_by_pid(tgid);
2205 if (pos && thread_group_leader(pos))
2206 goto found;
2208 /* If nr exceeds the number of processes get out quickly */
2209 pos = NULL;
2210 if (nr && nr >= nr_processes())
2211 goto done;
2213 /* If we haven't found our starting place yet start with
2214 * the init_task and walk nr tasks forward.
2216 for (pos = next_task(&init_task); nr > 0; --nr) {
2217 pos = next_task(pos);
2218 if (pos == &init_task) {
2219 pos = NULL;
2220 goto done;
2223 found:
2224 get_task_struct(pos);
2225 done:
2226 rcu_read_unlock();
2227 return pos;
2231 * Find the next task in the task list.
2232 * Return NULL if we loop or there is any error.
2234 * The reference to the input task_struct is released.
2236 static struct task_struct *next_tgid(struct task_struct *start)
2238 struct task_struct *pos;
2239 rcu_read_lock();
2240 pos = start;
2241 if (pid_alive(start))
2242 pos = next_task(start);
2243 if (pid_alive(pos) && (pos != &init_task)) {
2244 get_task_struct(pos);
2245 goto done;
2247 pos = NULL;
2248 done:
2249 rcu_read_unlock();
2250 put_task_struct(start);
2251 return pos;
2254 /* for the /proc/ directory itself, after non-process stuff has been done */
2255 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2257 char buf[PROC_NUMBUF];
2258 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2259 struct task_struct *task;
2260 int tgid;
2262 if (!nr) {
2263 ino_t ino = fake_ino(0,PROC_TGID_INO);
2264 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2265 return 0;
2266 filp->f_pos++;
2267 nr++;
2269 nr -= 1;
2271 /* f_version caches the tgid value that the last readdir call couldn't
2272 * return. lseek aka telldir automagically resets f_version to 0.
2274 tgid = filp->f_version;
2275 filp->f_version = 0;
2276 for (task = first_tgid(tgid, nr);
2277 task;
2278 task = next_tgid(task), filp->f_pos++) {
2279 int len;
2280 ino_t ino;
2281 tgid = task->pid;
2282 len = snprintf(buf, sizeof(buf), "%d", tgid);
2283 ino = fake_ino(tgid, PROC_TGID_INO);
2284 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2285 /* returning this tgid failed, save it as the first
2286 * pid for the next readir call */
2287 filp->f_version = tgid;
2288 put_task_struct(task);
2289 break;
2292 return 0;
2296 * Find the first tid of a thread group to return to user space.
2298 * Usually this is just the thread group leader, but if the users
2299 * buffer was too small or there was a seek into the middle of the
2300 * directory we have more work todo.
2302 * In the case of a short read we start with find_task_by_pid.
2304 * In the case of a seek we start with the leader and walk nr
2305 * threads past it.
2307 static struct task_struct *first_tid(struct task_struct *leader, int tid, int nr)
2309 struct task_struct *pos = NULL;
2310 read_lock(&tasklist_lock);
2312 /* Attempt to start with the pid of a thread */
2313 if (tid && (nr > 0)) {
2314 pos = find_task_by_pid(tid);
2315 if (pos && (pos->group_leader != leader))
2316 pos = NULL;
2317 if (pos)
2318 nr = 0;
2321 /* If nr exceeds the number of threads there is nothing todo */
2322 if (nr) {
2323 if (nr >= get_nr_threads(leader))
2324 goto done;
2327 /* If we haven't found our starting place yet start with the
2328 * leader and walk nr threads forward.
2330 if (!pos && (nr >= 0))
2331 pos = leader;
2333 for (; pos && pid_alive(pos); pos = next_thread(pos)) {
2334 if (--nr > 0)
2335 continue;
2336 get_task_struct(pos);
2337 goto done;
2339 pos = NULL;
2340 done:
2341 read_unlock(&tasklist_lock);
2342 return pos;
2346 * Find the next thread in the thread list.
2347 * Return NULL if there is an error or no next thread.
2349 * The reference to the input task_struct is released.
2351 static struct task_struct *next_tid(struct task_struct *start)
2353 struct task_struct *pos;
2354 read_lock(&tasklist_lock);
2355 pos = start;
2356 if (pid_alive(start))
2357 pos = next_thread(start);
2358 if (pid_alive(pos) && (pos != start->group_leader))
2359 get_task_struct(pos);
2360 else
2361 pos = NULL;
2362 read_unlock(&tasklist_lock);
2363 put_task_struct(start);
2364 return pos;
2367 /* for the /proc/TGID/task/ directories */
2368 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2370 char buf[PROC_NUMBUF];
2371 struct dentry *dentry = filp->f_dentry;
2372 struct inode *inode = dentry->d_inode;
2373 struct task_struct *leader = get_proc_task(inode);
2374 struct task_struct *task;
2375 int retval = -ENOENT;
2376 ino_t ino;
2377 int tid;
2378 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2380 if (!leader)
2381 goto out_no_task;
2382 retval = 0;
2384 switch (pos) {
2385 case 0:
2386 ino = inode->i_ino;
2387 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2388 goto out;
2389 pos++;
2390 /* fall through */
2391 case 1:
2392 ino = parent_ino(dentry);
2393 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2394 goto out;
2395 pos++;
2396 /* fall through */
2399 /* f_version caches the tgid value that the last readdir call couldn't
2400 * return. lseek aka telldir automagically resets f_version to 0.
2402 tid = filp->f_version;
2403 filp->f_version = 0;
2404 for (task = first_tid(leader, tid, pos - 2);
2405 task;
2406 task = next_tid(task), pos++) {
2407 int len;
2408 tid = task->pid;
2409 len = snprintf(buf, sizeof(buf), "%d", tid);
2410 ino = fake_ino(tid, PROC_TID_INO);
2411 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2412 /* returning this tgid failed, save it as the first
2413 * pid for the next readir call */
2414 filp->f_version = tid;
2415 put_task_struct(task);
2416 break;
2419 out:
2420 filp->f_pos = pos;
2421 put_task_struct(leader);
2422 out_no_task:
2423 return retval;
2426 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2428 struct inode *inode = dentry->d_inode;
2429 struct task_struct *p = get_proc_task(inode);
2430 generic_fillattr(inode, stat);
2432 if (p) {
2433 rcu_read_lock();
2434 stat->nlink += get_nr_threads(p);
2435 rcu_read_unlock();
2436 put_task_struct(p);
2439 return 0;