[PATCH] vfs: propagate mnt_flags into do_loopback/vfsmount
[linux-2.6/suspend2-2.6.18.git] / fs / proc / base.c
bloba3a3eecef68946110f8f2641959af78eff7be428
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"
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.
85 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
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,
172 /* Add new entries before this */
173 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
176 struct pid_entry {
177 int type;
178 int len;
179 char *name;
180 mode_t mode;
183 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
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}
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}
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}
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}
287 #endif
289 #undef E
291 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
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;
298 files = get_files_struct(task);
299 if (files) {
300 rcu_read_lock();
301 file = fcheck_files(files, fd);
302 if (file) {
303 *mnt = mntget(file->f_vfsmnt);
304 *dentry = dget(file->f_dentry);
305 rcu_read_unlock();
306 put_files_struct(files);
307 return 0;
309 rcu_read_unlock();
310 put_files_struct(files);
312 return -ENOENT;
315 static struct fs_struct *get_fs_struct(struct task_struct *task)
317 struct fs_struct *fs;
318 task_lock(task);
319 fs = task->fs;
320 if(fs)
321 atomic_inc(&fs->count);
322 task_unlock(task);
323 return fs;
326 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
328 struct fs_struct *fs = get_fs_struct(proc_task(inode));
329 int result = -ENOENT;
330 if (fs) {
331 read_lock(&fs->lock);
332 *mnt = mntget(fs->pwdmnt);
333 *dentry = dget(fs->pwd);
334 read_unlock(&fs->lock);
335 result = 0;
336 put_fs_struct(fs);
338 return result;
341 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
343 struct fs_struct *fs = get_fs_struct(proc_task(inode));
344 int result = -ENOENT;
345 if (fs) {
346 read_lock(&fs->lock);
347 *mnt = mntget(fs->rootmnt);
348 *dentry = dget(fs->root);
349 read_unlock(&fs->lock);
350 result = 0;
351 put_fs_struct(fs);
353 return result;
357 /* Same as proc_root_link, but this addionally tries to get fs from other
358 * threads in the group */
359 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
360 struct vfsmount **mnt)
362 struct fs_struct *fs;
363 int result = -ENOENT;
364 struct task_struct *leader = proc_task(inode);
366 task_lock(leader);
367 fs = leader->fs;
368 if (fs) {
369 atomic_inc(&fs->count);
370 task_unlock(leader);
371 } else {
372 /* Try to get fs from other threads */
373 task_unlock(leader);
374 read_lock(&tasklist_lock);
375 if (pid_alive(leader)) {
376 struct task_struct *task = leader;
378 while ((task = next_thread(task)) != leader) {
379 task_lock(task);
380 fs = task->fs;
381 if (fs) {
382 atomic_inc(&fs->count);
383 task_unlock(task);
384 break;
386 task_unlock(task);
389 read_unlock(&tasklist_lock);
392 if (fs) {
393 read_lock(&fs->lock);
394 *mnt = mntget(fs->rootmnt);
395 *dentry = dget(fs->root);
396 read_unlock(&fs->lock);
397 result = 0;
398 put_fs_struct(fs);
400 return result;
404 #define MAY_PTRACE(task) \
405 (task == current || \
406 (task->parent == current && \
407 (task->ptrace & PT_PTRACED) && \
408 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
409 security_ptrace(current,task) == 0))
411 static int proc_pid_environ(struct task_struct *task, char * buffer)
413 int res = 0;
414 struct mm_struct *mm = get_task_mm(task);
415 if (mm) {
416 unsigned int len = mm->env_end - mm->env_start;
417 if (len > PAGE_SIZE)
418 len = PAGE_SIZE;
419 res = access_process_vm(task, mm->env_start, buffer, len, 0);
420 if (!ptrace_may_attach(task))
421 res = -ESRCH;
422 mmput(mm);
424 return res;
427 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
429 int res = 0;
430 unsigned int len;
431 struct mm_struct *mm = get_task_mm(task);
432 if (!mm)
433 goto out;
434 if (!mm->arg_end)
435 goto out_mm; /* Shh! No looking before we're done */
437 len = mm->arg_end - mm->arg_start;
439 if (len > PAGE_SIZE)
440 len = PAGE_SIZE;
442 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
444 // If the nul at the end of args has been overwritten, then
445 // assume application is using setproctitle(3).
446 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
447 len = strnlen(buffer, res);
448 if (len < res) {
449 res = len;
450 } else {
451 len = mm->env_end - mm->env_start;
452 if (len > PAGE_SIZE - res)
453 len = PAGE_SIZE - res;
454 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
455 res = strnlen(buffer, res);
458 out_mm:
459 mmput(mm);
460 out:
461 return res;
464 static int proc_pid_auxv(struct task_struct *task, char *buffer)
466 int res = 0;
467 struct mm_struct *mm = get_task_mm(task);
468 if (mm) {
469 unsigned int nwords = 0;
471 nwords += 2;
472 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
473 res = nwords * sizeof(mm->saved_auxv[0]);
474 if (res > PAGE_SIZE)
475 res = PAGE_SIZE;
476 memcpy(buffer, mm->saved_auxv, res);
477 mmput(mm);
479 return res;
483 #ifdef CONFIG_KALLSYMS
485 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
486 * Returns the resolved symbol. If that fails, simply return the address.
488 static int proc_pid_wchan(struct task_struct *task, char *buffer)
490 char *modname;
491 const char *sym_name;
492 unsigned long wchan, size, offset;
493 char namebuf[KSYM_NAME_LEN+1];
495 wchan = get_wchan(task);
497 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
498 if (sym_name)
499 return sprintf(buffer, "%s", sym_name);
500 return sprintf(buffer, "%lu", wchan);
502 #endif /* CONFIG_KALLSYMS */
504 #ifdef CONFIG_SCHEDSTATS
506 * Provides /proc/PID/schedstat
508 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
510 return sprintf(buffer, "%lu %lu %lu\n",
511 task->sched_info.cpu_time,
512 task->sched_info.run_delay,
513 task->sched_info.pcnt);
515 #endif
517 /* The badness from the OOM killer */
518 unsigned long badness(struct task_struct *p, unsigned long uptime);
519 static int proc_oom_score(struct task_struct *task, char *buffer)
521 unsigned long points;
522 struct timespec uptime;
524 do_posix_clock_monotonic_gettime(&uptime);
525 points = badness(task, uptime.tv_sec);
526 return sprintf(buffer, "%lu\n", points);
529 /************************************************************************/
530 /* Here the fs part begins */
531 /************************************************************************/
533 /* permission checks */
535 /* If the process being read is separated by chroot from the reading process,
536 * don't let the reader access the threads.
538 * note: this does dput(root) and mntput(vfsmnt) on exit.
540 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
542 struct dentry *de, *base;
543 struct vfsmount *our_vfsmnt, *mnt;
544 int res = 0;
546 read_lock(&current->fs->lock);
547 our_vfsmnt = mntget(current->fs->rootmnt);
548 base = dget(current->fs->root);
549 read_unlock(&current->fs->lock);
551 spin_lock(&vfsmount_lock);
552 de = root;
553 mnt = vfsmnt;
555 while (mnt != our_vfsmnt) {
556 if (mnt == mnt->mnt_parent)
557 goto out;
558 de = mnt->mnt_mountpoint;
559 mnt = mnt->mnt_parent;
562 if (!is_subdir(de, base))
563 goto out;
564 spin_unlock(&vfsmount_lock);
566 exit:
567 dput(base);
568 mntput(our_vfsmnt);
569 dput(root);
570 mntput(vfsmnt);
571 return res;
572 out:
573 spin_unlock(&vfsmount_lock);
574 res = -EACCES;
575 goto exit;
578 static int proc_check_root(struct inode *inode)
580 struct dentry *root;
581 struct vfsmount *vfsmnt;
583 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
584 return -ENOENT;
585 return proc_check_chroot(root, vfsmnt);
588 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
590 if (generic_permission(inode, mask, NULL) != 0)
591 return -EACCES;
592 return proc_check_root(inode);
595 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
597 struct dentry *root;
598 struct vfsmount *vfsmnt;
600 if (generic_permission(inode, mask, NULL) != 0)
601 return -EACCES;
603 if (proc_task_root_link(inode, &root, &vfsmnt))
604 return -ENOENT;
606 return proc_check_chroot(root, vfsmnt);
609 extern struct seq_operations proc_pid_maps_op;
610 static int maps_open(struct inode *inode, struct file *file)
612 struct task_struct *task = proc_task(inode);
613 int ret = seq_open(file, &proc_pid_maps_op);
614 if (!ret) {
615 struct seq_file *m = file->private_data;
616 m->private = task;
618 return ret;
621 static struct file_operations proc_maps_operations = {
622 .open = maps_open,
623 .read = seq_read,
624 .llseek = seq_lseek,
625 .release = seq_release,
628 #ifdef CONFIG_NUMA
629 extern struct seq_operations proc_pid_numa_maps_op;
630 static int numa_maps_open(struct inode *inode, struct file *file)
632 struct task_struct *task = proc_task(inode);
633 int ret = seq_open(file, &proc_pid_numa_maps_op);
634 if (!ret) {
635 struct seq_file *m = file->private_data;
636 m->private = task;
638 return ret;
641 static struct file_operations proc_numa_maps_operations = {
642 .open = numa_maps_open,
643 .read = seq_read,
644 .llseek = seq_lseek,
645 .release = seq_release,
647 #endif
649 #ifdef CONFIG_MMU
650 extern struct seq_operations proc_pid_smaps_op;
651 static int smaps_open(struct inode *inode, struct file *file)
653 struct task_struct *task = proc_task(inode);
654 int ret = seq_open(file, &proc_pid_smaps_op);
655 if (!ret) {
656 struct seq_file *m = file->private_data;
657 m->private = task;
659 return ret;
662 static struct file_operations proc_smaps_operations = {
663 .open = smaps_open,
664 .read = seq_read,
665 .llseek = seq_lseek,
666 .release = seq_release,
668 #endif
670 extern struct seq_operations mounts_op;
671 struct proc_mounts {
672 struct seq_file m;
673 int event;
676 static int mounts_open(struct inode *inode, struct file *file)
678 struct task_struct *task = proc_task(inode);
679 struct namespace *namespace;
680 struct proc_mounts *p;
681 int ret = -EINVAL;
683 task_lock(task);
684 namespace = task->namespace;
685 if (namespace)
686 get_namespace(namespace);
687 task_unlock(task);
689 if (namespace) {
690 ret = -ENOMEM;
691 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
692 if (p) {
693 file->private_data = &p->m;
694 ret = seq_open(file, &mounts_op);
695 if (!ret) {
696 p->m.private = namespace;
697 p->event = namespace->event;
698 return 0;
700 kfree(p);
702 put_namespace(namespace);
704 return ret;
707 static int mounts_release(struct inode *inode, struct file *file)
709 struct seq_file *m = file->private_data;
710 struct namespace *namespace = m->private;
711 put_namespace(namespace);
712 return seq_release(inode, file);
715 static unsigned mounts_poll(struct file *file, poll_table *wait)
717 struct proc_mounts *p = file->private_data;
718 struct namespace *ns = p->m.private;
719 unsigned res = 0;
721 poll_wait(file, &ns->poll, wait);
723 spin_lock(&vfsmount_lock);
724 if (p->event != ns->event) {
725 p->event = ns->event;
726 res = POLLERR;
728 spin_unlock(&vfsmount_lock);
730 return res;
733 static struct file_operations proc_mounts_operations = {
734 .open = mounts_open,
735 .read = seq_read,
736 .llseek = seq_lseek,
737 .release = mounts_release,
738 .poll = mounts_poll,
741 extern struct seq_operations mountstats_op;
742 static int mountstats_open(struct inode *inode, struct file *file)
744 struct task_struct *task = proc_task(inode);
745 int ret = seq_open(file, &mountstats_op);
747 if (!ret) {
748 struct seq_file *m = file->private_data;
749 struct namespace *namespace;
750 task_lock(task);
751 namespace = task->namespace;
752 if (namespace)
753 get_namespace(namespace);
754 task_unlock(task);
756 if (namespace)
757 m->private = namespace;
758 else {
759 seq_release(inode, file);
760 ret = -EINVAL;
763 return ret;
766 static struct file_operations proc_mountstats_operations = {
767 .open = mountstats_open,
768 .read = seq_read,
769 .llseek = seq_lseek,
770 .release = mounts_release,
773 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
775 static ssize_t proc_info_read(struct file * file, char __user * buf,
776 size_t count, loff_t *ppos)
778 struct inode * inode = file->f_dentry->d_inode;
779 unsigned long page;
780 ssize_t length;
781 struct task_struct *task = proc_task(inode);
783 if (count > PROC_BLOCK_SIZE)
784 count = PROC_BLOCK_SIZE;
785 if (!(page = __get_free_page(GFP_KERNEL)))
786 return -ENOMEM;
788 length = PROC_I(inode)->op.proc_read(task, (char*)page);
790 if (length >= 0)
791 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
792 free_page(page);
793 return length;
796 static struct file_operations proc_info_file_operations = {
797 .read = proc_info_read,
800 static int mem_open(struct inode* inode, struct file* file)
802 file->private_data = (void*)((long)current->self_exec_id);
803 return 0;
806 static ssize_t mem_read(struct file * file, char __user * buf,
807 size_t count, loff_t *ppos)
809 struct task_struct *task = proc_task(file->f_dentry->d_inode);
810 char *page;
811 unsigned long src = *ppos;
812 int ret = -ESRCH;
813 struct mm_struct *mm;
815 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
816 goto out;
818 ret = -ENOMEM;
819 page = (char *)__get_free_page(GFP_USER);
820 if (!page)
821 goto out;
823 ret = 0;
825 mm = get_task_mm(task);
826 if (!mm)
827 goto out_free;
829 ret = -EIO;
831 if (file->private_data != (void*)((long)current->self_exec_id))
832 goto out_put;
834 ret = 0;
836 while (count > 0) {
837 int this_len, retval;
839 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
840 retval = access_process_vm(task, src, page, this_len, 0);
841 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
842 if (!ret)
843 ret = -EIO;
844 break;
847 if (copy_to_user(buf, page, retval)) {
848 ret = -EFAULT;
849 break;
852 ret += retval;
853 src += retval;
854 buf += retval;
855 count -= retval;
857 *ppos = src;
859 out_put:
860 mmput(mm);
861 out_free:
862 free_page((unsigned long) page);
863 out:
864 return ret;
867 #define mem_write NULL
869 #ifndef mem_write
870 /* This is a security hazard */
871 static ssize_t mem_write(struct file * file, const char * buf,
872 size_t count, loff_t *ppos)
874 int copied = 0;
875 char *page;
876 struct task_struct *task = proc_task(file->f_dentry->d_inode);
877 unsigned long dst = *ppos;
879 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
880 return -ESRCH;
882 page = (char *)__get_free_page(GFP_USER);
883 if (!page)
884 return -ENOMEM;
886 while (count > 0) {
887 int this_len, retval;
889 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
890 if (copy_from_user(page, buf, this_len)) {
891 copied = -EFAULT;
892 break;
894 retval = access_process_vm(task, dst, page, this_len, 1);
895 if (!retval) {
896 if (!copied)
897 copied = -EIO;
898 break;
900 copied += retval;
901 buf += retval;
902 dst += retval;
903 count -= retval;
905 *ppos = dst;
906 free_page((unsigned long) page);
907 return copied;
909 #endif
911 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
913 switch (orig) {
914 case 0:
915 file->f_pos = offset;
916 break;
917 case 1:
918 file->f_pos += offset;
919 break;
920 default:
921 return -EINVAL;
923 force_successful_syscall_return();
924 return file->f_pos;
927 static struct file_operations proc_mem_operations = {
928 .llseek = mem_lseek,
929 .read = mem_read,
930 .write = mem_write,
931 .open = mem_open,
934 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
935 size_t count, loff_t *ppos)
937 struct task_struct *task = proc_task(file->f_dentry->d_inode);
938 char buffer[8];
939 size_t len;
940 int oom_adjust = task->oomkilladj;
941 loff_t __ppos = *ppos;
943 len = sprintf(buffer, "%i\n", oom_adjust);
944 if (__ppos >= len)
945 return 0;
946 if (count > len-__ppos)
947 count = len-__ppos;
948 if (copy_to_user(buf, buffer + __ppos, count))
949 return -EFAULT;
950 *ppos = __ppos + count;
951 return count;
954 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
955 size_t count, loff_t *ppos)
957 struct task_struct *task = proc_task(file->f_dentry->d_inode);
958 char buffer[8], *end;
959 int oom_adjust;
961 if (!capable(CAP_SYS_RESOURCE))
962 return -EPERM;
963 memset(buffer, 0, 8);
964 if (count > 6)
965 count = 6;
966 if (copy_from_user(buffer, buf, count))
967 return -EFAULT;
968 oom_adjust = simple_strtol(buffer, &end, 0);
969 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
970 return -EINVAL;
971 if (*end == '\n')
972 end++;
973 task->oomkilladj = oom_adjust;
974 if (end - buffer == 0)
975 return -EIO;
976 return end - buffer;
979 static struct file_operations proc_oom_adjust_operations = {
980 .read = oom_adjust_read,
981 .write = oom_adjust_write,
984 static struct inode_operations proc_mem_inode_operations = {
985 .permission = proc_permission,
988 #ifdef CONFIG_AUDITSYSCALL
989 #define TMPBUFLEN 21
990 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
991 size_t count, loff_t *ppos)
993 struct inode * inode = file->f_dentry->d_inode;
994 struct task_struct *task = proc_task(inode);
995 ssize_t length;
996 char tmpbuf[TMPBUFLEN];
998 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
999 audit_get_loginuid(task->audit_context));
1000 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1003 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1004 size_t count, loff_t *ppos)
1006 struct inode * inode = file->f_dentry->d_inode;
1007 char *page, *tmp;
1008 ssize_t length;
1009 struct task_struct *task = proc_task(inode);
1010 uid_t loginuid;
1012 if (!capable(CAP_AUDIT_CONTROL))
1013 return -EPERM;
1015 if (current != task)
1016 return -EPERM;
1018 if (count > PAGE_SIZE)
1019 count = PAGE_SIZE;
1021 if (*ppos != 0) {
1022 /* No partial writes. */
1023 return -EINVAL;
1025 page = (char*)__get_free_page(GFP_USER);
1026 if (!page)
1027 return -ENOMEM;
1028 length = -EFAULT;
1029 if (copy_from_user(page, buf, count))
1030 goto out_free_page;
1032 loginuid = simple_strtoul(page, &tmp, 10);
1033 if (tmp == page) {
1034 length = -EINVAL;
1035 goto out_free_page;
1038 length = audit_set_loginuid(task, loginuid);
1039 if (likely(length == 0))
1040 length = count;
1042 out_free_page:
1043 free_page((unsigned long) page);
1044 return length;
1047 static struct file_operations proc_loginuid_operations = {
1048 .read = proc_loginuid_read,
1049 .write = proc_loginuid_write,
1051 #endif
1053 #ifdef CONFIG_SECCOMP
1054 static ssize_t seccomp_read(struct file *file, char __user *buf,
1055 size_t count, loff_t *ppos)
1057 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1058 char __buf[20];
1059 loff_t __ppos = *ppos;
1060 size_t len;
1062 /* no need to print the trailing zero, so use only len */
1063 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1064 if (__ppos >= len)
1065 return 0;
1066 if (count > len - __ppos)
1067 count = len - __ppos;
1068 if (copy_to_user(buf, __buf + __ppos, count))
1069 return -EFAULT;
1070 *ppos = __ppos + count;
1071 return count;
1074 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1075 size_t count, loff_t *ppos)
1077 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1078 char __buf[20], *end;
1079 unsigned int seccomp_mode;
1081 /* can set it only once to be even more secure */
1082 if (unlikely(tsk->seccomp.mode))
1083 return -EPERM;
1085 memset(__buf, 0, sizeof(__buf));
1086 count = min(count, sizeof(__buf) - 1);
1087 if (copy_from_user(__buf, buf, count))
1088 return -EFAULT;
1089 seccomp_mode = simple_strtoul(__buf, &end, 0);
1090 if (*end == '\n')
1091 end++;
1092 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1093 tsk->seccomp.mode = seccomp_mode;
1094 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1095 } else
1096 return -EINVAL;
1097 if (unlikely(!(end - __buf)))
1098 return -EIO;
1099 return end - __buf;
1102 static struct file_operations proc_seccomp_operations = {
1103 .read = seccomp_read,
1104 .write = seccomp_write,
1106 #endif /* CONFIG_SECCOMP */
1108 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1110 struct inode *inode = dentry->d_inode;
1111 int error = -EACCES;
1113 /* We don't need a base pointer in the /proc filesystem */
1114 path_release(nd);
1116 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1117 goto out;
1118 error = proc_check_root(inode);
1119 if (error)
1120 goto out;
1122 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1123 nd->last_type = LAST_BIND;
1124 out:
1125 return ERR_PTR(error);
1128 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1129 char __user *buffer, int buflen)
1131 struct inode * inode;
1132 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1133 int len;
1135 if (!tmp)
1136 return -ENOMEM;
1138 inode = dentry->d_inode;
1139 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1140 len = PTR_ERR(path);
1141 if (IS_ERR(path))
1142 goto out;
1143 len = tmp + PAGE_SIZE - 1 - path;
1145 if (len > buflen)
1146 len = buflen;
1147 if (copy_to_user(buffer, path, len))
1148 len = -EFAULT;
1149 out:
1150 free_page((unsigned long)tmp);
1151 return len;
1154 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1156 int error = -EACCES;
1157 struct inode *inode = dentry->d_inode;
1158 struct dentry *de;
1159 struct vfsmount *mnt = NULL;
1161 lock_kernel();
1163 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1164 goto out;
1165 error = proc_check_root(inode);
1166 if (error)
1167 goto out;
1169 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1170 if (error)
1171 goto out;
1173 error = do_proc_readlink(de, mnt, buffer, buflen);
1174 dput(de);
1175 mntput(mnt);
1176 out:
1177 unlock_kernel();
1178 return error;
1181 static struct inode_operations proc_pid_link_inode_operations = {
1182 .readlink = proc_pid_readlink,
1183 .follow_link = proc_pid_follow_link
1186 #define NUMBUF 10
1188 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1190 struct inode *inode = filp->f_dentry->d_inode;
1191 struct task_struct *p = proc_task(inode);
1192 unsigned int fd, tid, ino;
1193 int retval;
1194 char buf[NUMBUF];
1195 struct files_struct * files;
1196 struct fdtable *fdt;
1198 retval = -ENOENT;
1199 if (!pid_alive(p))
1200 goto out;
1201 retval = 0;
1202 tid = p->pid;
1204 fd = filp->f_pos;
1205 switch (fd) {
1206 case 0:
1207 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1208 goto out;
1209 filp->f_pos++;
1210 case 1:
1211 ino = fake_ino(tid, PROC_TID_INO);
1212 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1213 goto out;
1214 filp->f_pos++;
1215 default:
1216 files = get_files_struct(p);
1217 if (!files)
1218 goto out;
1219 rcu_read_lock();
1220 fdt = files_fdtable(files);
1221 for (fd = filp->f_pos-2;
1222 fd < fdt->max_fds;
1223 fd++, filp->f_pos++) {
1224 unsigned int i,j;
1226 if (!fcheck_files(files, fd))
1227 continue;
1228 rcu_read_unlock();
1230 j = NUMBUF;
1231 i = fd;
1232 do {
1233 j--;
1234 buf[j] = '0' + (i % 10);
1235 i /= 10;
1236 } while (i);
1238 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1239 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1240 rcu_read_lock();
1241 break;
1243 rcu_read_lock();
1245 rcu_read_unlock();
1246 put_files_struct(files);
1248 out:
1249 return retval;
1252 static int proc_pident_readdir(struct file *filp,
1253 void *dirent, filldir_t filldir,
1254 struct pid_entry *ents, unsigned int nents)
1256 int i;
1257 int pid;
1258 struct dentry *dentry = filp->f_dentry;
1259 struct inode *inode = dentry->d_inode;
1260 struct pid_entry *p;
1261 ino_t ino;
1262 int ret;
1264 ret = -ENOENT;
1265 if (!pid_alive(proc_task(inode)))
1266 goto out;
1268 ret = 0;
1269 pid = proc_task(inode)->pid;
1270 i = filp->f_pos;
1271 switch (i) {
1272 case 0:
1273 ino = inode->i_ino;
1274 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1275 goto out;
1276 i++;
1277 filp->f_pos++;
1278 /* fall through */
1279 case 1:
1280 ino = parent_ino(dentry);
1281 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1282 goto out;
1283 i++;
1284 filp->f_pos++;
1285 /* fall through */
1286 default:
1287 i -= 2;
1288 if (i >= nents) {
1289 ret = 1;
1290 goto out;
1292 p = ents + i;
1293 while (p->name) {
1294 if (filldir(dirent, p->name, p->len, filp->f_pos,
1295 fake_ino(pid, p->type), p->mode >> 12) < 0)
1296 goto out;
1297 filp->f_pos++;
1298 p++;
1302 ret = 1;
1303 out:
1304 return ret;
1307 static int proc_tgid_base_readdir(struct file * filp,
1308 void * dirent, filldir_t filldir)
1310 return proc_pident_readdir(filp,dirent,filldir,
1311 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1314 static int proc_tid_base_readdir(struct file * filp,
1315 void * dirent, filldir_t filldir)
1317 return proc_pident_readdir(filp,dirent,filldir,
1318 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1321 /* building an inode */
1323 static int task_dumpable(struct task_struct *task)
1325 int dumpable = 0;
1326 struct mm_struct *mm;
1328 task_lock(task);
1329 mm = task->mm;
1330 if (mm)
1331 dumpable = mm->dumpable;
1332 task_unlock(task);
1333 if(dumpable == 1)
1334 return 1;
1335 return 0;
1339 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1341 struct inode * inode;
1342 struct proc_inode *ei;
1344 /* We need a new inode */
1346 inode = new_inode(sb);
1347 if (!inode)
1348 goto out;
1350 /* Common stuff */
1351 ei = PROC_I(inode);
1352 ei->task = NULL;
1353 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1354 inode->i_ino = fake_ino(task->pid, ino);
1356 if (!pid_alive(task))
1357 goto out_unlock;
1360 * grab the reference to task.
1362 get_task_struct(task);
1363 ei->task = task;
1364 ei->type = ino;
1365 inode->i_uid = 0;
1366 inode->i_gid = 0;
1367 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1368 inode->i_uid = task->euid;
1369 inode->i_gid = task->egid;
1371 security_task_to_inode(task, inode);
1373 out:
1374 return inode;
1376 out_unlock:
1377 ei->pde = NULL;
1378 iput(inode);
1379 return NULL;
1382 /* dentry stuff */
1385 * Exceptional case: normally we are not allowed to unhash a busy
1386 * directory. In this case, however, we can do it - no aliasing problems
1387 * due to the way we treat inodes.
1389 * Rewrite the inode's ownerships here because the owning task may have
1390 * performed a setuid(), etc.
1392 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1394 struct inode *inode = dentry->d_inode;
1395 struct task_struct *task = proc_task(inode);
1396 if (pid_alive(task)) {
1397 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1398 inode->i_uid = task->euid;
1399 inode->i_gid = task->egid;
1400 } else {
1401 inode->i_uid = 0;
1402 inode->i_gid = 0;
1404 security_task_to_inode(task, inode);
1405 return 1;
1407 d_drop(dentry);
1408 return 0;
1411 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1413 struct inode *inode = dentry->d_inode;
1414 struct task_struct *task = proc_task(inode);
1415 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1416 struct files_struct *files;
1418 files = get_files_struct(task);
1419 if (files) {
1420 rcu_read_lock();
1421 if (fcheck_files(files, fd)) {
1422 rcu_read_unlock();
1423 put_files_struct(files);
1424 if (task_dumpable(task)) {
1425 inode->i_uid = task->euid;
1426 inode->i_gid = task->egid;
1427 } else {
1428 inode->i_uid = 0;
1429 inode->i_gid = 0;
1431 security_task_to_inode(task, inode);
1432 return 1;
1434 rcu_read_unlock();
1435 put_files_struct(files);
1437 d_drop(dentry);
1438 return 0;
1441 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1443 struct task_struct *task = proc_task(inode);
1444 spin_lock(&task->proc_lock);
1445 if (task->proc_dentry == dentry)
1446 task->proc_dentry = NULL;
1447 spin_unlock(&task->proc_lock);
1448 iput(inode);
1451 static int pid_delete_dentry(struct dentry * dentry)
1453 /* Is the task we represent dead?
1454 * If so, then don't put the dentry on the lru list,
1455 * kill it immediately.
1457 return !pid_alive(proc_task(dentry->d_inode));
1460 static struct dentry_operations tid_fd_dentry_operations =
1462 .d_revalidate = tid_fd_revalidate,
1463 .d_delete = pid_delete_dentry,
1466 static struct dentry_operations pid_dentry_operations =
1468 .d_revalidate = pid_revalidate,
1469 .d_delete = pid_delete_dentry,
1472 static struct dentry_operations pid_base_dentry_operations =
1474 .d_revalidate = pid_revalidate,
1475 .d_iput = pid_base_iput,
1476 .d_delete = pid_delete_dentry,
1479 /* Lookups */
1481 static unsigned name_to_int(struct dentry *dentry)
1483 const char *name = dentry->d_name.name;
1484 int len = dentry->d_name.len;
1485 unsigned n = 0;
1487 if (len > 1 && *name == '0')
1488 goto out;
1489 while (len-- > 0) {
1490 unsigned c = *name++ - '0';
1491 if (c > 9)
1492 goto out;
1493 if (n >= (~0U-9)/10)
1494 goto out;
1495 n *= 10;
1496 n += c;
1498 return n;
1499 out:
1500 return ~0U;
1503 /* SMP-safe */
1504 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1506 struct task_struct *task = proc_task(dir);
1507 unsigned fd = name_to_int(dentry);
1508 struct file * file;
1509 struct files_struct * files;
1510 struct inode *inode;
1511 struct proc_inode *ei;
1513 if (fd == ~0U)
1514 goto out;
1515 if (!pid_alive(task))
1516 goto out;
1518 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1519 if (!inode)
1520 goto out;
1521 ei = PROC_I(inode);
1522 files = get_files_struct(task);
1523 if (!files)
1524 goto out_unlock;
1525 inode->i_mode = S_IFLNK;
1526 rcu_read_lock();
1527 file = fcheck_files(files, fd);
1528 if (!file)
1529 goto out_unlock2;
1530 if (file->f_mode & 1)
1531 inode->i_mode |= S_IRUSR | S_IXUSR;
1532 if (file->f_mode & 2)
1533 inode->i_mode |= S_IWUSR | S_IXUSR;
1534 rcu_read_unlock();
1535 put_files_struct(files);
1536 inode->i_op = &proc_pid_link_inode_operations;
1537 inode->i_size = 64;
1538 ei->op.proc_get_link = proc_fd_link;
1539 dentry->d_op = &tid_fd_dentry_operations;
1540 d_add(dentry, inode);
1541 return NULL;
1543 out_unlock2:
1544 rcu_read_unlock();
1545 put_files_struct(files);
1546 out_unlock:
1547 iput(inode);
1548 out:
1549 return ERR_PTR(-ENOENT);
1552 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1553 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1555 static struct file_operations proc_fd_operations = {
1556 .read = generic_read_dir,
1557 .readdir = proc_readfd,
1560 static struct file_operations proc_task_operations = {
1561 .read = generic_read_dir,
1562 .readdir = proc_task_readdir,
1566 * proc directories can do almost nothing..
1568 static struct inode_operations proc_fd_inode_operations = {
1569 .lookup = proc_lookupfd,
1570 .permission = proc_permission,
1573 static struct inode_operations proc_task_inode_operations = {
1574 .lookup = proc_task_lookup,
1575 .permission = proc_task_permission,
1578 #ifdef CONFIG_SECURITY
1579 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1580 size_t count, loff_t *ppos)
1582 struct inode * inode = file->f_dentry->d_inode;
1583 unsigned long page;
1584 ssize_t length;
1585 struct task_struct *task = proc_task(inode);
1587 if (count > PAGE_SIZE)
1588 count = PAGE_SIZE;
1589 if (!(page = __get_free_page(GFP_KERNEL)))
1590 return -ENOMEM;
1592 length = security_getprocattr(task,
1593 (char*)file->f_dentry->d_name.name,
1594 (void*)page, count);
1595 if (length >= 0)
1596 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1597 free_page(page);
1598 return length;
1601 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1602 size_t count, loff_t *ppos)
1604 struct inode * inode = file->f_dentry->d_inode;
1605 char *page;
1606 ssize_t length;
1607 struct task_struct *task = proc_task(inode);
1609 if (count > PAGE_SIZE)
1610 count = PAGE_SIZE;
1611 if (*ppos != 0) {
1612 /* No partial writes. */
1613 return -EINVAL;
1615 page = (char*)__get_free_page(GFP_USER);
1616 if (!page)
1617 return -ENOMEM;
1618 length = -EFAULT;
1619 if (copy_from_user(page, buf, count))
1620 goto out;
1622 length = security_setprocattr(task,
1623 (char*)file->f_dentry->d_name.name,
1624 (void*)page, count);
1625 out:
1626 free_page((unsigned long) page);
1627 return length;
1630 static struct file_operations proc_pid_attr_operations = {
1631 .read = proc_pid_attr_read,
1632 .write = proc_pid_attr_write,
1635 static struct file_operations proc_tid_attr_operations;
1636 static struct inode_operations proc_tid_attr_inode_operations;
1637 static struct file_operations proc_tgid_attr_operations;
1638 static struct inode_operations proc_tgid_attr_inode_operations;
1639 #endif
1641 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1643 /* SMP-safe */
1644 static struct dentry *proc_pident_lookup(struct inode *dir,
1645 struct dentry *dentry,
1646 struct pid_entry *ents)
1648 struct inode *inode;
1649 int error;
1650 struct task_struct *task = proc_task(dir);
1651 struct pid_entry *p;
1652 struct proc_inode *ei;
1654 error = -ENOENT;
1655 inode = NULL;
1657 if (!pid_alive(task))
1658 goto out;
1660 for (p = ents; p->name; p++) {
1661 if (p->len != dentry->d_name.len)
1662 continue;
1663 if (!memcmp(dentry->d_name.name, p->name, p->len))
1664 break;
1666 if (!p->name)
1667 goto out;
1669 error = -EINVAL;
1670 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1671 if (!inode)
1672 goto out;
1674 ei = PROC_I(inode);
1675 inode->i_mode = p->mode;
1677 * Yes, it does not scale. And it should not. Don't add
1678 * new entries into /proc/<tgid>/ without very good reasons.
1680 switch(p->type) {
1681 case PROC_TGID_TASK:
1682 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1683 inode->i_op = &proc_task_inode_operations;
1684 inode->i_fop = &proc_task_operations;
1685 break;
1686 case PROC_TID_FD:
1687 case PROC_TGID_FD:
1688 inode->i_nlink = 2;
1689 inode->i_op = &proc_fd_inode_operations;
1690 inode->i_fop = &proc_fd_operations;
1691 break;
1692 case PROC_TID_EXE:
1693 case PROC_TGID_EXE:
1694 inode->i_op = &proc_pid_link_inode_operations;
1695 ei->op.proc_get_link = proc_exe_link;
1696 break;
1697 case PROC_TID_CWD:
1698 case PROC_TGID_CWD:
1699 inode->i_op = &proc_pid_link_inode_operations;
1700 ei->op.proc_get_link = proc_cwd_link;
1701 break;
1702 case PROC_TID_ROOT:
1703 case PROC_TGID_ROOT:
1704 inode->i_op = &proc_pid_link_inode_operations;
1705 ei->op.proc_get_link = proc_root_link;
1706 break;
1707 case PROC_TID_ENVIRON:
1708 case PROC_TGID_ENVIRON:
1709 inode->i_fop = &proc_info_file_operations;
1710 ei->op.proc_read = proc_pid_environ;
1711 break;
1712 case PROC_TID_AUXV:
1713 case PROC_TGID_AUXV:
1714 inode->i_fop = &proc_info_file_operations;
1715 ei->op.proc_read = proc_pid_auxv;
1716 break;
1717 case PROC_TID_STATUS:
1718 case PROC_TGID_STATUS:
1719 inode->i_fop = &proc_info_file_operations;
1720 ei->op.proc_read = proc_pid_status;
1721 break;
1722 case PROC_TID_STAT:
1723 inode->i_fop = &proc_info_file_operations;
1724 ei->op.proc_read = proc_tid_stat;
1725 break;
1726 case PROC_TGID_STAT:
1727 inode->i_fop = &proc_info_file_operations;
1728 ei->op.proc_read = proc_tgid_stat;
1729 break;
1730 case PROC_TID_CMDLINE:
1731 case PROC_TGID_CMDLINE:
1732 inode->i_fop = &proc_info_file_operations;
1733 ei->op.proc_read = proc_pid_cmdline;
1734 break;
1735 case PROC_TID_STATM:
1736 case PROC_TGID_STATM:
1737 inode->i_fop = &proc_info_file_operations;
1738 ei->op.proc_read = proc_pid_statm;
1739 break;
1740 case PROC_TID_MAPS:
1741 case PROC_TGID_MAPS:
1742 inode->i_fop = &proc_maps_operations;
1743 break;
1744 #ifdef CONFIG_NUMA
1745 case PROC_TID_NUMA_MAPS:
1746 case PROC_TGID_NUMA_MAPS:
1747 inode->i_fop = &proc_numa_maps_operations;
1748 break;
1749 #endif
1750 case PROC_TID_MEM:
1751 case PROC_TGID_MEM:
1752 inode->i_op = &proc_mem_inode_operations;
1753 inode->i_fop = &proc_mem_operations;
1754 break;
1755 #ifdef CONFIG_SECCOMP
1756 case PROC_TID_SECCOMP:
1757 case PROC_TGID_SECCOMP:
1758 inode->i_fop = &proc_seccomp_operations;
1759 break;
1760 #endif /* CONFIG_SECCOMP */
1761 case PROC_TID_MOUNTS:
1762 case PROC_TGID_MOUNTS:
1763 inode->i_fop = &proc_mounts_operations;
1764 break;
1765 #ifdef CONFIG_MMU
1766 case PROC_TID_SMAPS:
1767 case PROC_TGID_SMAPS:
1768 inode->i_fop = &proc_smaps_operations;
1769 break;
1770 #endif
1771 case PROC_TID_MOUNTSTATS:
1772 case PROC_TGID_MOUNTSTATS:
1773 inode->i_fop = &proc_mountstats_operations;
1774 break;
1775 #ifdef CONFIG_SECURITY
1776 case PROC_TID_ATTR:
1777 inode->i_nlink = 2;
1778 inode->i_op = &proc_tid_attr_inode_operations;
1779 inode->i_fop = &proc_tid_attr_operations;
1780 break;
1781 case PROC_TGID_ATTR:
1782 inode->i_nlink = 2;
1783 inode->i_op = &proc_tgid_attr_inode_operations;
1784 inode->i_fop = &proc_tgid_attr_operations;
1785 break;
1786 case PROC_TID_ATTR_CURRENT:
1787 case PROC_TGID_ATTR_CURRENT:
1788 case PROC_TID_ATTR_PREV:
1789 case PROC_TGID_ATTR_PREV:
1790 case PROC_TID_ATTR_EXEC:
1791 case PROC_TGID_ATTR_EXEC:
1792 case PROC_TID_ATTR_FSCREATE:
1793 case PROC_TGID_ATTR_FSCREATE:
1794 inode->i_fop = &proc_pid_attr_operations;
1795 break;
1796 #endif
1797 #ifdef CONFIG_KALLSYMS
1798 case PROC_TID_WCHAN:
1799 case PROC_TGID_WCHAN:
1800 inode->i_fop = &proc_info_file_operations;
1801 ei->op.proc_read = proc_pid_wchan;
1802 break;
1803 #endif
1804 #ifdef CONFIG_SCHEDSTATS
1805 case PROC_TID_SCHEDSTAT:
1806 case PROC_TGID_SCHEDSTAT:
1807 inode->i_fop = &proc_info_file_operations;
1808 ei->op.proc_read = proc_pid_schedstat;
1809 break;
1810 #endif
1811 #ifdef CONFIG_CPUSETS
1812 case PROC_TID_CPUSET:
1813 case PROC_TGID_CPUSET:
1814 inode->i_fop = &proc_cpuset_operations;
1815 break;
1816 #endif
1817 case PROC_TID_OOM_SCORE:
1818 case PROC_TGID_OOM_SCORE:
1819 inode->i_fop = &proc_info_file_operations;
1820 ei->op.proc_read = proc_oom_score;
1821 break;
1822 case PROC_TID_OOM_ADJUST:
1823 case PROC_TGID_OOM_ADJUST:
1824 inode->i_fop = &proc_oom_adjust_operations;
1825 break;
1826 #ifdef CONFIG_AUDITSYSCALL
1827 case PROC_TID_LOGINUID:
1828 case PROC_TGID_LOGINUID:
1829 inode->i_fop = &proc_loginuid_operations;
1830 break;
1831 #endif
1832 default:
1833 printk("procfs: impossible type (%d)",p->type);
1834 iput(inode);
1835 return ERR_PTR(-EINVAL);
1837 dentry->d_op = &pid_dentry_operations;
1838 d_add(dentry, inode);
1839 return NULL;
1841 out:
1842 return ERR_PTR(error);
1845 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1846 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1849 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1850 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1853 static struct file_operations proc_tgid_base_operations = {
1854 .read = generic_read_dir,
1855 .readdir = proc_tgid_base_readdir,
1858 static struct file_operations proc_tid_base_operations = {
1859 .read = generic_read_dir,
1860 .readdir = proc_tid_base_readdir,
1863 static struct inode_operations proc_tgid_base_inode_operations = {
1864 .lookup = proc_tgid_base_lookup,
1867 static struct inode_operations proc_tid_base_inode_operations = {
1868 .lookup = proc_tid_base_lookup,
1871 #ifdef CONFIG_SECURITY
1872 static int proc_tgid_attr_readdir(struct file * filp,
1873 void * dirent, filldir_t filldir)
1875 return proc_pident_readdir(filp,dirent,filldir,
1876 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1879 static int proc_tid_attr_readdir(struct file * filp,
1880 void * dirent, filldir_t filldir)
1882 return proc_pident_readdir(filp,dirent,filldir,
1883 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1886 static struct file_operations proc_tgid_attr_operations = {
1887 .read = generic_read_dir,
1888 .readdir = proc_tgid_attr_readdir,
1891 static struct file_operations proc_tid_attr_operations = {
1892 .read = generic_read_dir,
1893 .readdir = proc_tid_attr_readdir,
1896 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1897 struct dentry *dentry, struct nameidata *nd)
1899 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1902 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1903 struct dentry *dentry, struct nameidata *nd)
1905 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1908 static struct inode_operations proc_tgid_attr_inode_operations = {
1909 .lookup = proc_tgid_attr_lookup,
1912 static struct inode_operations proc_tid_attr_inode_operations = {
1913 .lookup = proc_tid_attr_lookup,
1915 #endif
1918 * /proc/self:
1920 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1921 int buflen)
1923 char tmp[30];
1924 sprintf(tmp, "%d", current->tgid);
1925 return vfs_readlink(dentry,buffer,buflen,tmp);
1928 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1930 char tmp[30];
1931 sprintf(tmp, "%d", current->tgid);
1932 return ERR_PTR(vfs_follow_link(nd,tmp));
1935 static struct inode_operations proc_self_inode_operations = {
1936 .readlink = proc_self_readlink,
1937 .follow_link = proc_self_follow_link,
1941 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1942 * @p: task that should be flushed.
1944 * Drops the /proc/@pid dcache entry from the hash chains.
1946 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1947 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1948 * if the pid value is immediately reused. This is enforced by
1949 * - caller must acquire spin_lock(p->proc_lock)
1950 * - must be called before detach_pid()
1951 * - proc_pid_lookup acquires proc_lock, and checks that
1952 * the target is not dead by looking at the attach count
1953 * of PIDTYPE_PID.
1956 struct dentry *proc_pid_unhash(struct task_struct *p)
1958 struct dentry *proc_dentry;
1960 proc_dentry = p->proc_dentry;
1961 if (proc_dentry != NULL) {
1963 spin_lock(&dcache_lock);
1964 spin_lock(&proc_dentry->d_lock);
1965 if (!d_unhashed(proc_dentry)) {
1966 dget_locked(proc_dentry);
1967 __d_drop(proc_dentry);
1968 spin_unlock(&proc_dentry->d_lock);
1969 } else {
1970 spin_unlock(&proc_dentry->d_lock);
1971 proc_dentry = NULL;
1973 spin_unlock(&dcache_lock);
1975 return proc_dentry;
1979 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1980 * @proc_dentry: directoy to prune.
1982 * Shrink the /proc directory that was used by the just killed thread.
1985 void proc_pid_flush(struct dentry *proc_dentry)
1987 might_sleep();
1988 if(proc_dentry != NULL) {
1989 shrink_dcache_parent(proc_dentry);
1990 dput(proc_dentry);
1994 /* SMP-safe */
1995 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1997 struct task_struct *task;
1998 struct inode *inode;
1999 struct proc_inode *ei;
2000 unsigned tgid;
2001 int died;
2003 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2004 inode = new_inode(dir->i_sb);
2005 if (!inode)
2006 return ERR_PTR(-ENOMEM);
2007 ei = PROC_I(inode);
2008 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2009 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2010 ei->pde = NULL;
2011 inode->i_mode = S_IFLNK|S_IRWXUGO;
2012 inode->i_uid = inode->i_gid = 0;
2013 inode->i_size = 64;
2014 inode->i_op = &proc_self_inode_operations;
2015 d_add(dentry, inode);
2016 return NULL;
2018 tgid = name_to_int(dentry);
2019 if (tgid == ~0U)
2020 goto out;
2022 read_lock(&tasklist_lock);
2023 task = find_task_by_pid(tgid);
2024 if (task)
2025 get_task_struct(task);
2026 read_unlock(&tasklist_lock);
2027 if (!task)
2028 goto out;
2030 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2033 if (!inode) {
2034 put_task_struct(task);
2035 goto out;
2037 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2038 inode->i_op = &proc_tgid_base_inode_operations;
2039 inode->i_fop = &proc_tgid_base_operations;
2040 inode->i_flags|=S_IMMUTABLE;
2041 #ifdef CONFIG_SECURITY
2042 inode->i_nlink = 5;
2043 #else
2044 inode->i_nlink = 4;
2045 #endif
2047 dentry->d_op = &pid_base_dentry_operations;
2049 died = 0;
2050 d_add(dentry, inode);
2051 spin_lock(&task->proc_lock);
2052 task->proc_dentry = dentry;
2053 if (!pid_alive(task)) {
2054 dentry = proc_pid_unhash(task);
2055 died = 1;
2057 spin_unlock(&task->proc_lock);
2059 put_task_struct(task);
2060 if (died) {
2061 proc_pid_flush(dentry);
2062 goto out;
2064 return NULL;
2065 out:
2066 return ERR_PTR(-ENOENT);
2069 /* SMP-safe */
2070 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2072 struct task_struct *task;
2073 struct task_struct *leader = proc_task(dir);
2074 struct inode *inode;
2075 unsigned tid;
2077 tid = name_to_int(dentry);
2078 if (tid == ~0U)
2079 goto out;
2081 read_lock(&tasklist_lock);
2082 task = find_task_by_pid(tid);
2083 if (task)
2084 get_task_struct(task);
2085 read_unlock(&tasklist_lock);
2086 if (!task)
2087 goto out;
2088 if (leader->tgid != task->tgid)
2089 goto out_drop_task;
2091 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2094 if (!inode)
2095 goto out_drop_task;
2096 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2097 inode->i_op = &proc_tid_base_inode_operations;
2098 inode->i_fop = &proc_tid_base_operations;
2099 inode->i_flags|=S_IMMUTABLE;
2100 #ifdef CONFIG_SECURITY
2101 inode->i_nlink = 4;
2102 #else
2103 inode->i_nlink = 3;
2104 #endif
2106 dentry->d_op = &pid_base_dentry_operations;
2108 d_add(dentry, inode);
2110 put_task_struct(task);
2111 return NULL;
2112 out_drop_task:
2113 put_task_struct(task);
2114 out:
2115 return ERR_PTR(-ENOENT);
2118 #define PROC_NUMBUF 10
2119 #define PROC_MAXPIDS 20
2122 * Get a few tgid's to return for filldir - we need to hold the
2123 * tasklist lock while doing this, and we must release it before
2124 * we actually do the filldir itself, so we use a temp buffer..
2126 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2128 struct task_struct *p;
2129 int nr_tgids = 0;
2131 index--;
2132 read_lock(&tasklist_lock);
2133 p = NULL;
2134 if (version) {
2135 p = find_task_by_pid(version);
2136 if (p && !thread_group_leader(p))
2137 p = NULL;
2140 if (p)
2141 index = 0;
2142 else
2143 p = next_task(&init_task);
2145 for ( ; p != &init_task; p = next_task(p)) {
2146 int tgid = p->pid;
2147 if (!pid_alive(p))
2148 continue;
2149 if (--index >= 0)
2150 continue;
2151 tgids[nr_tgids] = tgid;
2152 nr_tgids++;
2153 if (nr_tgids >= PROC_MAXPIDS)
2154 break;
2156 read_unlock(&tasklist_lock);
2157 return nr_tgids;
2161 * Get a few tid's to return for filldir - we need to hold the
2162 * tasklist lock while doing this, and we must release it before
2163 * we actually do the filldir itself, so we use a temp buffer..
2165 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2167 struct task_struct *leader_task = proc_task(dir);
2168 struct task_struct *task = leader_task;
2169 int nr_tids = 0;
2171 index -= 2;
2172 read_lock(&tasklist_lock);
2174 * The starting point task (leader_task) might be an already
2175 * unlinked task, which cannot be used to access the task-list
2176 * via next_thread().
2178 if (pid_alive(task)) do {
2179 int tid = task->pid;
2181 if (--index >= 0)
2182 continue;
2183 if (tids != NULL)
2184 tids[nr_tids] = tid;
2185 nr_tids++;
2186 if (nr_tids >= PROC_MAXPIDS)
2187 break;
2188 } while ((task = next_thread(task)) != leader_task);
2189 read_unlock(&tasklist_lock);
2190 return nr_tids;
2193 /* for the /proc/ directory itself, after non-process stuff has been done */
2194 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2196 unsigned int tgid_array[PROC_MAXPIDS];
2197 char buf[PROC_NUMBUF];
2198 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2199 unsigned int nr_tgids, i;
2200 int next_tgid;
2202 if (!nr) {
2203 ino_t ino = fake_ino(0,PROC_TGID_INO);
2204 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2205 return 0;
2206 filp->f_pos++;
2207 nr++;
2210 /* f_version caches the tgid value that the last readdir call couldn't
2211 * return. lseek aka telldir automagically resets f_version to 0.
2213 next_tgid = filp->f_version;
2214 filp->f_version = 0;
2215 for (;;) {
2216 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2217 if (!nr_tgids) {
2218 /* no more entries ! */
2219 break;
2221 next_tgid = 0;
2223 /* do not use the last found pid, reserve it for next_tgid */
2224 if (nr_tgids == PROC_MAXPIDS) {
2225 nr_tgids--;
2226 next_tgid = tgid_array[nr_tgids];
2229 for (i=0;i<nr_tgids;i++) {
2230 int tgid = tgid_array[i];
2231 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2232 unsigned long j = PROC_NUMBUF;
2235 buf[--j] = '0' + (tgid % 10);
2236 while ((tgid /= 10) != 0);
2238 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2239 /* returning this tgid failed, save it as the first
2240 * pid for the next readir call */
2241 filp->f_version = tgid_array[i];
2242 goto out;
2244 filp->f_pos++;
2245 nr++;
2248 out:
2249 return 0;
2252 /* for the /proc/TGID/task/ directories */
2253 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2255 unsigned int tid_array[PROC_MAXPIDS];
2256 char buf[PROC_NUMBUF];
2257 unsigned int nr_tids, i;
2258 struct dentry *dentry = filp->f_dentry;
2259 struct inode *inode = dentry->d_inode;
2260 int retval = -ENOENT;
2261 ino_t ino;
2262 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2264 if (!pid_alive(proc_task(inode)))
2265 goto out;
2266 retval = 0;
2268 switch (pos) {
2269 case 0:
2270 ino = inode->i_ino;
2271 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2272 goto out;
2273 pos++;
2274 /* fall through */
2275 case 1:
2276 ino = parent_ino(dentry);
2277 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2278 goto out;
2279 pos++;
2280 /* fall through */
2283 nr_tids = get_tid_list(pos, tid_array, inode);
2284 inode->i_nlink = pos + nr_tids;
2286 for (i = 0; i < nr_tids; i++) {
2287 unsigned long j = PROC_NUMBUF;
2288 int tid = tid_array[i];
2290 ino = fake_ino(tid,PROC_TID_INO);
2293 buf[--j] = '0' + (tid % 10);
2294 while ((tid /= 10) != 0);
2296 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
2297 break;
2298 pos++;
2300 out:
2301 filp->f_pos = pos;
2302 return retval;