[PATCH] proc: Fix the .. inode number on /proc/<pid>/fd
[linux-2.6/kvm.git] / fs / proc / base.c
blob13e3ab99eb746711817f5e15fc9216b0bc35c699
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 PROC_TGID_ATTR_KEYCREATE,
125 #endif
126 #ifdef CONFIG_AUDITSYSCALL
127 PROC_TGID_LOGINUID,
128 #endif
129 PROC_TGID_OOM_SCORE,
130 PROC_TGID_OOM_ADJUST,
131 PROC_TID_INO,
132 PROC_TID_STATUS,
133 PROC_TID_MEM,
134 #ifdef CONFIG_SECCOMP
135 PROC_TID_SECCOMP,
136 #endif
137 PROC_TID_CWD,
138 PROC_TID_ROOT,
139 PROC_TID_EXE,
140 PROC_TID_FD,
141 PROC_TID_ENVIRON,
142 PROC_TID_AUXV,
143 PROC_TID_CMDLINE,
144 PROC_TID_STAT,
145 PROC_TID_STATM,
146 PROC_TID_MAPS,
147 PROC_TID_NUMA_MAPS,
148 PROC_TID_MOUNTS,
149 PROC_TID_MOUNTSTATS,
150 PROC_TID_WCHAN,
151 #ifdef CONFIG_MMU
152 PROC_TID_SMAPS,
153 #endif
154 #ifdef CONFIG_SCHEDSTATS
155 PROC_TID_SCHEDSTAT,
156 #endif
157 #ifdef CONFIG_CPUSETS
158 PROC_TID_CPUSET,
159 #endif
160 #ifdef CONFIG_SECURITY
161 PROC_TID_ATTR,
162 PROC_TID_ATTR_CURRENT,
163 PROC_TID_ATTR_PREV,
164 PROC_TID_ATTR_EXEC,
165 PROC_TID_ATTR_FSCREATE,
166 PROC_TID_ATTR_KEYCREATE,
167 #endif
168 #ifdef CONFIG_AUDITSYSCALL
169 PROC_TID_LOGINUID,
170 #endif
171 PROC_TID_OOM_SCORE,
172 PROC_TID_OOM_ADJUST,
174 /* Add new entries before this */
175 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
178 struct pid_entry {
179 int type;
180 int len;
181 char *name;
182 mode_t mode;
185 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
187 static struct pid_entry tgid_base_stuff[] = {
188 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
189 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
190 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
191 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
192 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
193 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
194 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
195 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
196 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
197 #ifdef CONFIG_NUMA
198 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
199 #endif
200 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
201 #ifdef CONFIG_SECCOMP
202 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
203 #endif
204 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
205 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
206 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
207 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
208 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
209 #ifdef CONFIG_MMU
210 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
211 #endif
212 #ifdef CONFIG_SECURITY
213 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
214 #endif
215 #ifdef CONFIG_KALLSYMS
216 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
217 #endif
218 #ifdef CONFIG_SCHEDSTATS
219 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
220 #endif
221 #ifdef CONFIG_CPUSETS
222 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
223 #endif
224 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
225 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
226 #ifdef CONFIG_AUDITSYSCALL
227 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
228 #endif
229 {0,0,NULL,0}
231 static struct pid_entry tid_base_stuff[] = {
232 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
233 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
234 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
235 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
236 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
237 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
238 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
239 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
240 #ifdef CONFIG_NUMA
241 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
242 #endif
243 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
244 #ifdef CONFIG_SECCOMP
245 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
246 #endif
247 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
248 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
249 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
250 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
251 #ifdef CONFIG_MMU
252 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
253 #endif
254 #ifdef CONFIG_SECURITY
255 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
256 #endif
257 #ifdef CONFIG_KALLSYMS
258 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
259 #endif
260 #ifdef CONFIG_SCHEDSTATS
261 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
262 #endif
263 #ifdef CONFIG_CPUSETS
264 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
265 #endif
266 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
267 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
268 #ifdef CONFIG_AUDITSYSCALL
269 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
270 #endif
271 {0,0,NULL,0}
274 #ifdef CONFIG_SECURITY
275 static struct pid_entry tgid_attr_stuff[] = {
276 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
277 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
278 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
279 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
280 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
281 {0,0,NULL,0}
283 static struct pid_entry tid_attr_stuff[] = {
284 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
285 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
286 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
287 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
288 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
289 {0,0,NULL,0}
291 #endif
293 #undef E
295 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
297 struct task_struct *task = proc_task(inode);
298 struct files_struct *files;
299 struct file *file;
300 int fd = proc_type(inode) - PROC_TID_FD_DIR;
302 files = get_files_struct(task);
303 if (files) {
305 * We are not taking a ref to the file structure, so we must
306 * hold ->file_lock.
308 spin_lock(&files->file_lock);
309 file = fcheck_files(files, fd);
310 if (file) {
311 *mnt = mntget(file->f_vfsmnt);
312 *dentry = dget(file->f_dentry);
313 spin_unlock(&files->file_lock);
314 put_files_struct(files);
315 return 0;
317 spin_unlock(&files->file_lock);
318 put_files_struct(files);
320 return -ENOENT;
323 static struct fs_struct *get_fs_struct(struct task_struct *task)
325 struct fs_struct *fs;
326 task_lock(task);
327 fs = task->fs;
328 if(fs)
329 atomic_inc(&fs->count);
330 task_unlock(task);
331 return fs;
334 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
336 struct fs_struct *fs = get_fs_struct(proc_task(inode));
337 int result = -ENOENT;
338 if (fs) {
339 read_lock(&fs->lock);
340 *mnt = mntget(fs->pwdmnt);
341 *dentry = dget(fs->pwd);
342 read_unlock(&fs->lock);
343 result = 0;
344 put_fs_struct(fs);
346 return result;
349 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
351 struct fs_struct *fs = get_fs_struct(proc_task(inode));
352 int result = -ENOENT;
353 if (fs) {
354 read_lock(&fs->lock);
355 *mnt = mntget(fs->rootmnt);
356 *dentry = dget(fs->root);
357 read_unlock(&fs->lock);
358 result = 0;
359 put_fs_struct(fs);
361 return result;
365 /* Same as proc_root_link, but this addionally tries to get fs from other
366 * threads in the group */
367 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
368 struct vfsmount **mnt)
370 struct fs_struct *fs;
371 int result = -ENOENT;
372 struct task_struct *leader = proc_task(inode);
374 task_lock(leader);
375 fs = leader->fs;
376 if (fs) {
377 atomic_inc(&fs->count);
378 task_unlock(leader);
379 } else {
380 /* Try to get fs from other threads */
381 task_unlock(leader);
382 read_lock(&tasklist_lock);
383 if (pid_alive(leader)) {
384 struct task_struct *task = leader;
386 while ((task = next_thread(task)) != leader) {
387 task_lock(task);
388 fs = task->fs;
389 if (fs) {
390 atomic_inc(&fs->count);
391 task_unlock(task);
392 break;
394 task_unlock(task);
397 read_unlock(&tasklist_lock);
400 if (fs) {
401 read_lock(&fs->lock);
402 *mnt = mntget(fs->rootmnt);
403 *dentry = dget(fs->root);
404 read_unlock(&fs->lock);
405 result = 0;
406 put_fs_struct(fs);
408 return result;
412 #define MAY_PTRACE(task) \
413 (task == current || \
414 (task->parent == current && \
415 (task->ptrace & PT_PTRACED) && \
416 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
417 security_ptrace(current,task) == 0))
419 static int proc_pid_environ(struct task_struct *task, char * buffer)
421 int res = 0;
422 struct mm_struct *mm = get_task_mm(task);
423 if (mm) {
424 unsigned int len = mm->env_end - mm->env_start;
425 if (len > PAGE_SIZE)
426 len = PAGE_SIZE;
427 res = access_process_vm(task, mm->env_start, buffer, len, 0);
428 if (!ptrace_may_attach(task))
429 res = -ESRCH;
430 mmput(mm);
432 return res;
435 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
437 int res = 0;
438 unsigned int len;
439 struct mm_struct *mm = get_task_mm(task);
440 if (!mm)
441 goto out;
442 if (!mm->arg_end)
443 goto out_mm; /* Shh! No looking before we're done */
445 len = mm->arg_end - mm->arg_start;
447 if (len > PAGE_SIZE)
448 len = PAGE_SIZE;
450 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
452 // If the nul at the end of args has been overwritten, then
453 // assume application is using setproctitle(3).
454 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
455 len = strnlen(buffer, res);
456 if (len < res) {
457 res = len;
458 } else {
459 len = mm->env_end - mm->env_start;
460 if (len > PAGE_SIZE - res)
461 len = PAGE_SIZE - res;
462 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
463 res = strnlen(buffer, res);
466 out_mm:
467 mmput(mm);
468 out:
469 return res;
472 static int proc_pid_auxv(struct task_struct *task, char *buffer)
474 int res = 0;
475 struct mm_struct *mm = get_task_mm(task);
476 if (mm) {
477 unsigned int nwords = 0;
479 nwords += 2;
480 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
481 res = nwords * sizeof(mm->saved_auxv[0]);
482 if (res > PAGE_SIZE)
483 res = PAGE_SIZE;
484 memcpy(buffer, mm->saved_auxv, res);
485 mmput(mm);
487 return res;
491 #ifdef CONFIG_KALLSYMS
493 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
494 * Returns the resolved symbol. If that fails, simply return the address.
496 static int proc_pid_wchan(struct task_struct *task, char *buffer)
498 char *modname;
499 const char *sym_name;
500 unsigned long wchan, size, offset;
501 char namebuf[KSYM_NAME_LEN+1];
503 wchan = get_wchan(task);
505 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
506 if (sym_name)
507 return sprintf(buffer, "%s", sym_name);
508 return sprintf(buffer, "%lu", wchan);
510 #endif /* CONFIG_KALLSYMS */
512 #ifdef CONFIG_SCHEDSTATS
514 * Provides /proc/PID/schedstat
516 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
518 return sprintf(buffer, "%lu %lu %lu\n",
519 task->sched_info.cpu_time,
520 task->sched_info.run_delay,
521 task->sched_info.pcnt);
523 #endif
525 /* The badness from the OOM killer */
526 unsigned long badness(struct task_struct *p, unsigned long uptime);
527 static int proc_oom_score(struct task_struct *task, char *buffer)
529 unsigned long points;
530 struct timespec uptime;
532 do_posix_clock_monotonic_gettime(&uptime);
533 points = badness(task, uptime.tv_sec);
534 return sprintf(buffer, "%lu\n", points);
537 /************************************************************************/
538 /* Here the fs part begins */
539 /************************************************************************/
541 /* permission checks */
543 /* If the process being read is separated by chroot from the reading process,
544 * don't let the reader access the threads.
546 * note: this does dput(root) and mntput(vfsmnt) on exit.
548 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
550 struct dentry *de, *base;
551 struct vfsmount *our_vfsmnt, *mnt;
552 int res = 0;
554 read_lock(&current->fs->lock);
555 our_vfsmnt = mntget(current->fs->rootmnt);
556 base = dget(current->fs->root);
557 read_unlock(&current->fs->lock);
559 spin_lock(&vfsmount_lock);
560 de = root;
561 mnt = vfsmnt;
563 while (mnt != our_vfsmnt) {
564 if (mnt == mnt->mnt_parent)
565 goto out;
566 de = mnt->mnt_mountpoint;
567 mnt = mnt->mnt_parent;
570 if (!is_subdir(de, base))
571 goto out;
572 spin_unlock(&vfsmount_lock);
574 exit:
575 dput(base);
576 mntput(our_vfsmnt);
577 dput(root);
578 mntput(vfsmnt);
579 return res;
580 out:
581 spin_unlock(&vfsmount_lock);
582 res = -EACCES;
583 goto exit;
586 static int proc_check_root(struct inode *inode)
588 struct dentry *root;
589 struct vfsmount *vfsmnt;
591 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
592 return -ENOENT;
593 return proc_check_chroot(root, vfsmnt);
596 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
598 if (generic_permission(inode, mask, NULL) != 0)
599 return -EACCES;
600 return proc_check_root(inode);
603 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
605 struct dentry *root;
606 struct vfsmount *vfsmnt;
608 if (generic_permission(inode, mask, NULL) != 0)
609 return -EACCES;
611 if (proc_task_root_link(inode, &root, &vfsmnt))
612 return -ENOENT;
614 return proc_check_chroot(root, vfsmnt);
617 extern struct seq_operations proc_pid_maps_op;
618 static int maps_open(struct inode *inode, struct file *file)
620 struct task_struct *task = proc_task(inode);
621 int ret = seq_open(file, &proc_pid_maps_op);
622 if (!ret) {
623 struct seq_file *m = file->private_data;
624 m->private = task;
626 return ret;
629 static struct file_operations proc_maps_operations = {
630 .open = maps_open,
631 .read = seq_read,
632 .llseek = seq_lseek,
633 .release = seq_release,
636 #ifdef CONFIG_NUMA
637 extern struct seq_operations proc_pid_numa_maps_op;
638 static int numa_maps_open(struct inode *inode, struct file *file)
640 struct task_struct *task = proc_task(inode);
641 int ret = seq_open(file, &proc_pid_numa_maps_op);
642 if (!ret) {
643 struct seq_file *m = file->private_data;
644 m->private = task;
646 return ret;
649 static struct file_operations proc_numa_maps_operations = {
650 .open = numa_maps_open,
651 .read = seq_read,
652 .llseek = seq_lseek,
653 .release = seq_release,
655 #endif
657 #ifdef CONFIG_MMU
658 extern struct seq_operations proc_pid_smaps_op;
659 static int smaps_open(struct inode *inode, struct file *file)
661 struct task_struct *task = proc_task(inode);
662 int ret = seq_open(file, &proc_pid_smaps_op);
663 if (!ret) {
664 struct seq_file *m = file->private_data;
665 m->private = task;
667 return ret;
670 static struct file_operations proc_smaps_operations = {
671 .open = smaps_open,
672 .read = seq_read,
673 .llseek = seq_lseek,
674 .release = seq_release,
676 #endif
678 extern struct seq_operations mounts_op;
679 struct proc_mounts {
680 struct seq_file m;
681 int event;
684 static int mounts_open(struct inode *inode, struct file *file)
686 struct task_struct *task = proc_task(inode);
687 struct namespace *namespace;
688 struct proc_mounts *p;
689 int ret = -EINVAL;
691 task_lock(task);
692 namespace = task->namespace;
693 if (namespace)
694 get_namespace(namespace);
695 task_unlock(task);
697 if (namespace) {
698 ret = -ENOMEM;
699 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
700 if (p) {
701 file->private_data = &p->m;
702 ret = seq_open(file, &mounts_op);
703 if (!ret) {
704 p->m.private = namespace;
705 p->event = namespace->event;
706 return 0;
708 kfree(p);
710 put_namespace(namespace);
712 return ret;
715 static int mounts_release(struct inode *inode, struct file *file)
717 struct seq_file *m = file->private_data;
718 struct namespace *namespace = m->private;
719 put_namespace(namespace);
720 return seq_release(inode, file);
723 static unsigned mounts_poll(struct file *file, poll_table *wait)
725 struct proc_mounts *p = file->private_data;
726 struct namespace *ns = p->m.private;
727 unsigned res = 0;
729 poll_wait(file, &ns->poll, wait);
731 spin_lock(&vfsmount_lock);
732 if (p->event != ns->event) {
733 p->event = ns->event;
734 res = POLLERR;
736 spin_unlock(&vfsmount_lock);
738 return res;
741 static struct file_operations proc_mounts_operations = {
742 .open = mounts_open,
743 .read = seq_read,
744 .llseek = seq_lseek,
745 .release = mounts_release,
746 .poll = mounts_poll,
749 extern struct seq_operations mountstats_op;
750 static int mountstats_open(struct inode *inode, struct file *file)
752 struct task_struct *task = proc_task(inode);
753 int ret = seq_open(file, &mountstats_op);
755 if (!ret) {
756 struct seq_file *m = file->private_data;
757 struct namespace *namespace;
758 task_lock(task);
759 namespace = task->namespace;
760 if (namespace)
761 get_namespace(namespace);
762 task_unlock(task);
764 if (namespace)
765 m->private = namespace;
766 else {
767 seq_release(inode, file);
768 ret = -EINVAL;
771 return ret;
774 static struct file_operations proc_mountstats_operations = {
775 .open = mountstats_open,
776 .read = seq_read,
777 .llseek = seq_lseek,
778 .release = mounts_release,
781 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
783 static ssize_t proc_info_read(struct file * file, char __user * buf,
784 size_t count, loff_t *ppos)
786 struct inode * inode = file->f_dentry->d_inode;
787 unsigned long page;
788 ssize_t length;
789 struct task_struct *task = proc_task(inode);
791 if (count > PROC_BLOCK_SIZE)
792 count = PROC_BLOCK_SIZE;
793 if (!(page = __get_free_page(GFP_KERNEL)))
794 return -ENOMEM;
796 length = PROC_I(inode)->op.proc_read(task, (char*)page);
798 if (length >= 0)
799 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
800 free_page(page);
801 return length;
804 static struct file_operations proc_info_file_operations = {
805 .read = proc_info_read,
808 static int mem_open(struct inode* inode, struct file* file)
810 file->private_data = (void*)((long)current->self_exec_id);
811 return 0;
814 static ssize_t mem_read(struct file * file, char __user * buf,
815 size_t count, loff_t *ppos)
817 struct task_struct *task = proc_task(file->f_dentry->d_inode);
818 char *page;
819 unsigned long src = *ppos;
820 int ret = -ESRCH;
821 struct mm_struct *mm;
823 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
824 goto out;
826 ret = -ENOMEM;
827 page = (char *)__get_free_page(GFP_USER);
828 if (!page)
829 goto out;
831 ret = 0;
833 mm = get_task_mm(task);
834 if (!mm)
835 goto out_free;
837 ret = -EIO;
839 if (file->private_data != (void*)((long)current->self_exec_id))
840 goto out_put;
842 ret = 0;
844 while (count > 0) {
845 int this_len, retval;
847 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
848 retval = access_process_vm(task, src, page, this_len, 0);
849 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
850 if (!ret)
851 ret = -EIO;
852 break;
855 if (copy_to_user(buf, page, retval)) {
856 ret = -EFAULT;
857 break;
860 ret += retval;
861 src += retval;
862 buf += retval;
863 count -= retval;
865 *ppos = src;
867 out_put:
868 mmput(mm);
869 out_free:
870 free_page((unsigned long) page);
871 out:
872 return ret;
875 #define mem_write NULL
877 #ifndef mem_write
878 /* This is a security hazard */
879 static ssize_t mem_write(struct file * file, const char * buf,
880 size_t count, loff_t *ppos)
882 int copied = 0;
883 char *page;
884 struct task_struct *task = proc_task(file->f_dentry->d_inode);
885 unsigned long dst = *ppos;
887 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
888 return -ESRCH;
890 page = (char *)__get_free_page(GFP_USER);
891 if (!page)
892 return -ENOMEM;
894 while (count > 0) {
895 int this_len, retval;
897 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
898 if (copy_from_user(page, buf, this_len)) {
899 copied = -EFAULT;
900 break;
902 retval = access_process_vm(task, dst, page, this_len, 1);
903 if (!retval) {
904 if (!copied)
905 copied = -EIO;
906 break;
908 copied += retval;
909 buf += retval;
910 dst += retval;
911 count -= retval;
913 *ppos = dst;
914 free_page((unsigned long) page);
915 return copied;
917 #endif
919 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
921 switch (orig) {
922 case 0:
923 file->f_pos = offset;
924 break;
925 case 1:
926 file->f_pos += offset;
927 break;
928 default:
929 return -EINVAL;
931 force_successful_syscall_return();
932 return file->f_pos;
935 static struct file_operations proc_mem_operations = {
936 .llseek = mem_lseek,
937 .read = mem_read,
938 .write = mem_write,
939 .open = mem_open,
942 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
943 size_t count, loff_t *ppos)
945 struct task_struct *task = proc_task(file->f_dentry->d_inode);
946 char buffer[8];
947 size_t len;
948 int oom_adjust = task->oomkilladj;
949 loff_t __ppos = *ppos;
951 len = sprintf(buffer, "%i\n", oom_adjust);
952 if (__ppos >= len)
953 return 0;
954 if (count > len-__ppos)
955 count = len-__ppos;
956 if (copy_to_user(buf, buffer + __ppos, count))
957 return -EFAULT;
958 *ppos = __ppos + count;
959 return count;
962 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
963 size_t count, loff_t *ppos)
965 struct task_struct *task = proc_task(file->f_dentry->d_inode);
966 char buffer[8], *end;
967 int oom_adjust;
969 if (!capable(CAP_SYS_RESOURCE))
970 return -EPERM;
971 memset(buffer, 0, 8);
972 if (count > 6)
973 count = 6;
974 if (copy_from_user(buffer, buf, count))
975 return -EFAULT;
976 oom_adjust = simple_strtol(buffer, &end, 0);
977 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
978 return -EINVAL;
979 if (*end == '\n')
980 end++;
981 task->oomkilladj = oom_adjust;
982 if (end - buffer == 0)
983 return -EIO;
984 return end - buffer;
987 static struct file_operations proc_oom_adjust_operations = {
988 .read = oom_adjust_read,
989 .write = oom_adjust_write,
992 static struct inode_operations proc_mem_inode_operations = {
993 .permission = proc_permission,
996 #ifdef CONFIG_AUDITSYSCALL
997 #define TMPBUFLEN 21
998 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
999 size_t count, loff_t *ppos)
1001 struct inode * inode = file->f_dentry->d_inode;
1002 struct task_struct *task = proc_task(inode);
1003 ssize_t length;
1004 char tmpbuf[TMPBUFLEN];
1006 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1007 audit_get_loginuid(task->audit_context));
1008 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1011 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1012 size_t count, loff_t *ppos)
1014 struct inode * inode = file->f_dentry->d_inode;
1015 char *page, *tmp;
1016 ssize_t length;
1017 struct task_struct *task = proc_task(inode);
1018 uid_t loginuid;
1020 if (!capable(CAP_AUDIT_CONTROL))
1021 return -EPERM;
1023 if (current != task)
1024 return -EPERM;
1026 if (count >= PAGE_SIZE)
1027 count = PAGE_SIZE - 1;
1029 if (*ppos != 0) {
1030 /* No partial writes. */
1031 return -EINVAL;
1033 page = (char*)__get_free_page(GFP_USER);
1034 if (!page)
1035 return -ENOMEM;
1036 length = -EFAULT;
1037 if (copy_from_user(page, buf, count))
1038 goto out_free_page;
1040 page[count] = '\0';
1041 loginuid = simple_strtoul(page, &tmp, 10);
1042 if (tmp == page) {
1043 length = -EINVAL;
1044 goto out_free_page;
1047 length = audit_set_loginuid(task, loginuid);
1048 if (likely(length == 0))
1049 length = count;
1051 out_free_page:
1052 free_page((unsigned long) page);
1053 return length;
1056 static struct file_operations proc_loginuid_operations = {
1057 .read = proc_loginuid_read,
1058 .write = proc_loginuid_write,
1060 #endif
1062 #ifdef CONFIG_SECCOMP
1063 static ssize_t seccomp_read(struct file *file, char __user *buf,
1064 size_t count, loff_t *ppos)
1066 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1067 char __buf[20];
1068 loff_t __ppos = *ppos;
1069 size_t len;
1071 /* no need to print the trailing zero, so use only len */
1072 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1073 if (__ppos >= len)
1074 return 0;
1075 if (count > len - __ppos)
1076 count = len - __ppos;
1077 if (copy_to_user(buf, __buf + __ppos, count))
1078 return -EFAULT;
1079 *ppos = __ppos + count;
1080 return count;
1083 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1084 size_t count, loff_t *ppos)
1086 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1087 char __buf[20], *end;
1088 unsigned int seccomp_mode;
1090 /* can set it only once to be even more secure */
1091 if (unlikely(tsk->seccomp.mode))
1092 return -EPERM;
1094 memset(__buf, 0, sizeof(__buf));
1095 count = min(count, sizeof(__buf) - 1);
1096 if (copy_from_user(__buf, buf, count))
1097 return -EFAULT;
1098 seccomp_mode = simple_strtoul(__buf, &end, 0);
1099 if (*end == '\n')
1100 end++;
1101 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1102 tsk->seccomp.mode = seccomp_mode;
1103 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1104 } else
1105 return -EINVAL;
1106 if (unlikely(!(end - __buf)))
1107 return -EIO;
1108 return end - __buf;
1111 static struct file_operations proc_seccomp_operations = {
1112 .read = seccomp_read,
1113 .write = seccomp_write,
1115 #endif /* CONFIG_SECCOMP */
1117 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1119 struct inode *inode = dentry->d_inode;
1120 int error = -EACCES;
1122 /* We don't need a base pointer in the /proc filesystem */
1123 path_release(nd);
1125 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1126 goto out;
1127 error = proc_check_root(inode);
1128 if (error)
1129 goto out;
1131 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1132 nd->last_type = LAST_BIND;
1133 out:
1134 return ERR_PTR(error);
1137 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1138 char __user *buffer, int buflen)
1140 struct inode * inode;
1141 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1142 int len;
1144 if (!tmp)
1145 return -ENOMEM;
1147 inode = dentry->d_inode;
1148 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1149 len = PTR_ERR(path);
1150 if (IS_ERR(path))
1151 goto out;
1152 len = tmp + PAGE_SIZE - 1 - path;
1154 if (len > buflen)
1155 len = buflen;
1156 if (copy_to_user(buffer, path, len))
1157 len = -EFAULT;
1158 out:
1159 free_page((unsigned long)tmp);
1160 return len;
1163 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1165 int error = -EACCES;
1166 struct inode *inode = dentry->d_inode;
1167 struct dentry *de;
1168 struct vfsmount *mnt = NULL;
1170 lock_kernel();
1172 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1173 goto out;
1174 error = proc_check_root(inode);
1175 if (error)
1176 goto out;
1178 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1179 if (error)
1180 goto out;
1182 error = do_proc_readlink(de, mnt, buffer, buflen);
1183 dput(de);
1184 mntput(mnt);
1185 out:
1186 unlock_kernel();
1187 return error;
1190 static struct inode_operations proc_pid_link_inode_operations = {
1191 .readlink = proc_pid_readlink,
1192 .follow_link = proc_pid_follow_link
1195 #define NUMBUF 10
1197 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1199 struct dentry *dentry = filp->f_dentry;
1200 struct inode *inode = dentry->d_inode;
1201 struct task_struct *p = proc_task(inode);
1202 unsigned int fd, tid, ino;
1203 int retval;
1204 char buf[NUMBUF];
1205 struct files_struct * files;
1206 struct fdtable *fdt;
1208 retval = -ENOENT;
1209 if (!pid_alive(p))
1210 goto out;
1211 retval = 0;
1212 tid = p->pid;
1214 fd = filp->f_pos;
1215 switch (fd) {
1216 case 0:
1217 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1218 goto out;
1219 filp->f_pos++;
1220 case 1:
1221 ino = parent_ino(dentry);
1222 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1223 goto out;
1224 filp->f_pos++;
1225 default:
1226 files = get_files_struct(p);
1227 if (!files)
1228 goto out;
1229 rcu_read_lock();
1230 fdt = files_fdtable(files);
1231 for (fd = filp->f_pos-2;
1232 fd < fdt->max_fds;
1233 fd++, filp->f_pos++) {
1234 unsigned int i,j;
1236 if (!fcheck_files(files, fd))
1237 continue;
1238 rcu_read_unlock();
1240 j = NUMBUF;
1241 i = fd;
1242 do {
1243 j--;
1244 buf[j] = '0' + (i % 10);
1245 i /= 10;
1246 } while (i);
1248 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1249 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1250 rcu_read_lock();
1251 break;
1253 rcu_read_lock();
1255 rcu_read_unlock();
1256 put_files_struct(files);
1258 out:
1259 return retval;
1262 static int proc_pident_readdir(struct file *filp,
1263 void *dirent, filldir_t filldir,
1264 struct pid_entry *ents, unsigned int nents)
1266 int i;
1267 int pid;
1268 struct dentry *dentry = filp->f_dentry;
1269 struct inode *inode = dentry->d_inode;
1270 struct pid_entry *p;
1271 ino_t ino;
1272 int ret;
1274 ret = -ENOENT;
1275 if (!pid_alive(proc_task(inode)))
1276 goto out;
1278 ret = 0;
1279 pid = proc_task(inode)->pid;
1280 i = filp->f_pos;
1281 switch (i) {
1282 case 0:
1283 ino = inode->i_ino;
1284 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1285 goto out;
1286 i++;
1287 filp->f_pos++;
1288 /* fall through */
1289 case 1:
1290 ino = parent_ino(dentry);
1291 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1292 goto out;
1293 i++;
1294 filp->f_pos++;
1295 /* fall through */
1296 default:
1297 i -= 2;
1298 if (i >= nents) {
1299 ret = 1;
1300 goto out;
1302 p = ents + i;
1303 while (p->name) {
1304 if (filldir(dirent, p->name, p->len, filp->f_pos,
1305 fake_ino(pid, p->type), p->mode >> 12) < 0)
1306 goto out;
1307 filp->f_pos++;
1308 p++;
1312 ret = 1;
1313 out:
1314 return ret;
1317 static int proc_tgid_base_readdir(struct file * filp,
1318 void * dirent, filldir_t filldir)
1320 return proc_pident_readdir(filp,dirent,filldir,
1321 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1324 static int proc_tid_base_readdir(struct file * filp,
1325 void * dirent, filldir_t filldir)
1327 return proc_pident_readdir(filp,dirent,filldir,
1328 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1331 /* building an inode */
1333 static int task_dumpable(struct task_struct *task)
1335 int dumpable = 0;
1336 struct mm_struct *mm;
1338 task_lock(task);
1339 mm = task->mm;
1340 if (mm)
1341 dumpable = mm->dumpable;
1342 task_unlock(task);
1343 if(dumpable == 1)
1344 return 1;
1345 return 0;
1349 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1351 struct inode * inode;
1352 struct proc_inode *ei;
1354 /* We need a new inode */
1356 inode = new_inode(sb);
1357 if (!inode)
1358 goto out;
1360 /* Common stuff */
1361 ei = PROC_I(inode);
1362 ei->task = NULL;
1363 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1364 inode->i_ino = fake_ino(task->pid, ino);
1366 if (!pid_alive(task))
1367 goto out_unlock;
1370 * grab the reference to task.
1372 get_task_struct(task);
1373 ei->task = task;
1374 ei->type = ino;
1375 inode->i_uid = 0;
1376 inode->i_gid = 0;
1377 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1378 inode->i_uid = task->euid;
1379 inode->i_gid = task->egid;
1381 security_task_to_inode(task, inode);
1383 out:
1384 return inode;
1386 out_unlock:
1387 ei->pde = NULL;
1388 iput(inode);
1389 return NULL;
1392 /* dentry stuff */
1395 * Exceptional case: normally we are not allowed to unhash a busy
1396 * directory. In this case, however, we can do it - no aliasing problems
1397 * due to the way we treat inodes.
1399 * Rewrite the inode's ownerships here because the owning task may have
1400 * performed a setuid(), etc.
1402 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1404 struct inode *inode = dentry->d_inode;
1405 struct task_struct *task = proc_task(inode);
1406 if (pid_alive(task)) {
1407 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1408 inode->i_uid = task->euid;
1409 inode->i_gid = task->egid;
1410 } else {
1411 inode->i_uid = 0;
1412 inode->i_gid = 0;
1414 security_task_to_inode(task, inode);
1415 return 1;
1417 d_drop(dentry);
1418 return 0;
1421 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1423 struct inode *inode = dentry->d_inode;
1424 struct task_struct *task = proc_task(inode);
1425 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1426 struct files_struct *files;
1428 files = get_files_struct(task);
1429 if (files) {
1430 rcu_read_lock();
1431 if (fcheck_files(files, fd)) {
1432 rcu_read_unlock();
1433 put_files_struct(files);
1434 if (task_dumpable(task)) {
1435 inode->i_uid = task->euid;
1436 inode->i_gid = task->egid;
1437 } else {
1438 inode->i_uid = 0;
1439 inode->i_gid = 0;
1441 security_task_to_inode(task, inode);
1442 return 1;
1444 rcu_read_unlock();
1445 put_files_struct(files);
1447 d_drop(dentry);
1448 return 0;
1451 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1453 struct task_struct *task = proc_task(inode);
1454 spin_lock(&task->proc_lock);
1455 if (task->proc_dentry == dentry)
1456 task->proc_dentry = NULL;
1457 spin_unlock(&task->proc_lock);
1458 iput(inode);
1461 static int pid_delete_dentry(struct dentry * dentry)
1463 /* Is the task we represent dead?
1464 * If so, then don't put the dentry on the lru list,
1465 * kill it immediately.
1467 return !pid_alive(proc_task(dentry->d_inode));
1470 static struct dentry_operations tid_fd_dentry_operations =
1472 .d_revalidate = tid_fd_revalidate,
1473 .d_delete = pid_delete_dentry,
1476 static struct dentry_operations pid_dentry_operations =
1478 .d_revalidate = pid_revalidate,
1479 .d_delete = pid_delete_dentry,
1482 static struct dentry_operations pid_base_dentry_operations =
1484 .d_revalidate = pid_revalidate,
1485 .d_iput = pid_base_iput,
1486 .d_delete = pid_delete_dentry,
1489 /* Lookups */
1491 static unsigned name_to_int(struct dentry *dentry)
1493 const char *name = dentry->d_name.name;
1494 int len = dentry->d_name.len;
1495 unsigned n = 0;
1497 if (len > 1 && *name == '0')
1498 goto out;
1499 while (len-- > 0) {
1500 unsigned c = *name++ - '0';
1501 if (c > 9)
1502 goto out;
1503 if (n >= (~0U-9)/10)
1504 goto out;
1505 n *= 10;
1506 n += c;
1508 return n;
1509 out:
1510 return ~0U;
1513 /* SMP-safe */
1514 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1516 struct task_struct *task = proc_task(dir);
1517 unsigned fd = name_to_int(dentry);
1518 struct file * file;
1519 struct files_struct * files;
1520 struct inode *inode;
1521 struct proc_inode *ei;
1523 if (fd == ~0U)
1524 goto out;
1525 if (!pid_alive(task))
1526 goto out;
1528 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1529 if (!inode)
1530 goto out;
1531 ei = PROC_I(inode);
1532 files = get_files_struct(task);
1533 if (!files)
1534 goto out_unlock;
1535 inode->i_mode = S_IFLNK;
1538 * We are not taking a ref to the file structure, so we must
1539 * hold ->file_lock.
1541 spin_lock(&files->file_lock);
1542 file = fcheck_files(files, fd);
1543 if (!file)
1544 goto out_unlock2;
1545 if (file->f_mode & 1)
1546 inode->i_mode |= S_IRUSR | S_IXUSR;
1547 if (file->f_mode & 2)
1548 inode->i_mode |= S_IWUSR | S_IXUSR;
1549 spin_unlock(&files->file_lock);
1550 put_files_struct(files);
1551 inode->i_op = &proc_pid_link_inode_operations;
1552 inode->i_size = 64;
1553 ei->op.proc_get_link = proc_fd_link;
1554 dentry->d_op = &tid_fd_dentry_operations;
1555 d_add(dentry, inode);
1556 return NULL;
1558 out_unlock2:
1559 spin_unlock(&files->file_lock);
1560 put_files_struct(files);
1561 out_unlock:
1562 iput(inode);
1563 out:
1564 return ERR_PTR(-ENOENT);
1567 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1568 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1570 static struct file_operations proc_fd_operations = {
1571 .read = generic_read_dir,
1572 .readdir = proc_readfd,
1575 static struct file_operations proc_task_operations = {
1576 .read = generic_read_dir,
1577 .readdir = proc_task_readdir,
1581 * proc directories can do almost nothing..
1583 static struct inode_operations proc_fd_inode_operations = {
1584 .lookup = proc_lookupfd,
1585 .permission = proc_permission,
1588 static struct inode_operations proc_task_inode_operations = {
1589 .lookup = proc_task_lookup,
1590 .permission = proc_task_permission,
1593 #ifdef CONFIG_SECURITY
1594 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1595 size_t count, loff_t *ppos)
1597 struct inode * inode = file->f_dentry->d_inode;
1598 unsigned long page;
1599 ssize_t length;
1600 struct task_struct *task = proc_task(inode);
1602 if (count > PAGE_SIZE)
1603 count = PAGE_SIZE;
1604 if (!(page = __get_free_page(GFP_KERNEL)))
1605 return -ENOMEM;
1607 length = security_getprocattr(task,
1608 (char*)file->f_dentry->d_name.name,
1609 (void*)page, count);
1610 if (length >= 0)
1611 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1612 free_page(page);
1613 return length;
1616 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1617 size_t count, loff_t *ppos)
1619 struct inode * inode = file->f_dentry->d_inode;
1620 char *page;
1621 ssize_t length;
1622 struct task_struct *task = proc_task(inode);
1624 if (count > PAGE_SIZE)
1625 count = PAGE_SIZE;
1626 if (*ppos != 0) {
1627 /* No partial writes. */
1628 return -EINVAL;
1630 page = (char*)__get_free_page(GFP_USER);
1631 if (!page)
1632 return -ENOMEM;
1633 length = -EFAULT;
1634 if (copy_from_user(page, buf, count))
1635 goto out;
1637 length = security_setprocattr(task,
1638 (char*)file->f_dentry->d_name.name,
1639 (void*)page, count);
1640 out:
1641 free_page((unsigned long) page);
1642 return length;
1645 static struct file_operations proc_pid_attr_operations = {
1646 .read = proc_pid_attr_read,
1647 .write = proc_pid_attr_write,
1650 static struct file_operations proc_tid_attr_operations;
1651 static struct inode_operations proc_tid_attr_inode_operations;
1652 static struct file_operations proc_tgid_attr_operations;
1653 static struct inode_operations proc_tgid_attr_inode_operations;
1654 #endif
1656 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1658 /* SMP-safe */
1659 static struct dentry *proc_pident_lookup(struct inode *dir,
1660 struct dentry *dentry,
1661 struct pid_entry *ents)
1663 struct inode *inode;
1664 int error;
1665 struct task_struct *task = proc_task(dir);
1666 struct pid_entry *p;
1667 struct proc_inode *ei;
1669 error = -ENOENT;
1670 inode = NULL;
1672 if (!pid_alive(task))
1673 goto out;
1675 for (p = ents; p->name; p++) {
1676 if (p->len != dentry->d_name.len)
1677 continue;
1678 if (!memcmp(dentry->d_name.name, p->name, p->len))
1679 break;
1681 if (!p->name)
1682 goto out;
1684 error = -EINVAL;
1685 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1686 if (!inode)
1687 goto out;
1689 ei = PROC_I(inode);
1690 inode->i_mode = p->mode;
1692 * Yes, it does not scale. And it should not. Don't add
1693 * new entries into /proc/<tgid>/ without very good reasons.
1695 switch(p->type) {
1696 case PROC_TGID_TASK:
1697 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1698 inode->i_op = &proc_task_inode_operations;
1699 inode->i_fop = &proc_task_operations;
1700 break;
1701 case PROC_TID_FD:
1702 case PROC_TGID_FD:
1703 inode->i_nlink = 2;
1704 inode->i_op = &proc_fd_inode_operations;
1705 inode->i_fop = &proc_fd_operations;
1706 break;
1707 case PROC_TID_EXE:
1708 case PROC_TGID_EXE:
1709 inode->i_op = &proc_pid_link_inode_operations;
1710 ei->op.proc_get_link = proc_exe_link;
1711 break;
1712 case PROC_TID_CWD:
1713 case PROC_TGID_CWD:
1714 inode->i_op = &proc_pid_link_inode_operations;
1715 ei->op.proc_get_link = proc_cwd_link;
1716 break;
1717 case PROC_TID_ROOT:
1718 case PROC_TGID_ROOT:
1719 inode->i_op = &proc_pid_link_inode_operations;
1720 ei->op.proc_get_link = proc_root_link;
1721 break;
1722 case PROC_TID_ENVIRON:
1723 case PROC_TGID_ENVIRON:
1724 inode->i_fop = &proc_info_file_operations;
1725 ei->op.proc_read = proc_pid_environ;
1726 break;
1727 case PROC_TID_AUXV:
1728 case PROC_TGID_AUXV:
1729 inode->i_fop = &proc_info_file_operations;
1730 ei->op.proc_read = proc_pid_auxv;
1731 break;
1732 case PROC_TID_STATUS:
1733 case PROC_TGID_STATUS:
1734 inode->i_fop = &proc_info_file_operations;
1735 ei->op.proc_read = proc_pid_status;
1736 break;
1737 case PROC_TID_STAT:
1738 inode->i_fop = &proc_info_file_operations;
1739 ei->op.proc_read = proc_tid_stat;
1740 break;
1741 case PROC_TGID_STAT:
1742 inode->i_fop = &proc_info_file_operations;
1743 ei->op.proc_read = proc_tgid_stat;
1744 break;
1745 case PROC_TID_CMDLINE:
1746 case PROC_TGID_CMDLINE:
1747 inode->i_fop = &proc_info_file_operations;
1748 ei->op.proc_read = proc_pid_cmdline;
1749 break;
1750 case PROC_TID_STATM:
1751 case PROC_TGID_STATM:
1752 inode->i_fop = &proc_info_file_operations;
1753 ei->op.proc_read = proc_pid_statm;
1754 break;
1755 case PROC_TID_MAPS:
1756 case PROC_TGID_MAPS:
1757 inode->i_fop = &proc_maps_operations;
1758 break;
1759 #ifdef CONFIG_NUMA
1760 case PROC_TID_NUMA_MAPS:
1761 case PROC_TGID_NUMA_MAPS:
1762 inode->i_fop = &proc_numa_maps_operations;
1763 break;
1764 #endif
1765 case PROC_TID_MEM:
1766 case PROC_TGID_MEM:
1767 inode->i_op = &proc_mem_inode_operations;
1768 inode->i_fop = &proc_mem_operations;
1769 break;
1770 #ifdef CONFIG_SECCOMP
1771 case PROC_TID_SECCOMP:
1772 case PROC_TGID_SECCOMP:
1773 inode->i_fop = &proc_seccomp_operations;
1774 break;
1775 #endif /* CONFIG_SECCOMP */
1776 case PROC_TID_MOUNTS:
1777 case PROC_TGID_MOUNTS:
1778 inode->i_fop = &proc_mounts_operations;
1779 break;
1780 #ifdef CONFIG_MMU
1781 case PROC_TID_SMAPS:
1782 case PROC_TGID_SMAPS:
1783 inode->i_fop = &proc_smaps_operations;
1784 break;
1785 #endif
1786 case PROC_TID_MOUNTSTATS:
1787 case PROC_TGID_MOUNTSTATS:
1788 inode->i_fop = &proc_mountstats_operations;
1789 break;
1790 #ifdef CONFIG_SECURITY
1791 case PROC_TID_ATTR:
1792 inode->i_nlink = 2;
1793 inode->i_op = &proc_tid_attr_inode_operations;
1794 inode->i_fop = &proc_tid_attr_operations;
1795 break;
1796 case PROC_TGID_ATTR:
1797 inode->i_nlink = 2;
1798 inode->i_op = &proc_tgid_attr_inode_operations;
1799 inode->i_fop = &proc_tgid_attr_operations;
1800 break;
1801 case PROC_TID_ATTR_CURRENT:
1802 case PROC_TGID_ATTR_CURRENT:
1803 case PROC_TID_ATTR_PREV:
1804 case PROC_TGID_ATTR_PREV:
1805 case PROC_TID_ATTR_EXEC:
1806 case PROC_TGID_ATTR_EXEC:
1807 case PROC_TID_ATTR_FSCREATE:
1808 case PROC_TGID_ATTR_FSCREATE:
1809 case PROC_TID_ATTR_KEYCREATE:
1810 case PROC_TGID_ATTR_KEYCREATE:
1811 inode->i_fop = &proc_pid_attr_operations;
1812 break;
1813 #endif
1814 #ifdef CONFIG_KALLSYMS
1815 case PROC_TID_WCHAN:
1816 case PROC_TGID_WCHAN:
1817 inode->i_fop = &proc_info_file_operations;
1818 ei->op.proc_read = proc_pid_wchan;
1819 break;
1820 #endif
1821 #ifdef CONFIG_SCHEDSTATS
1822 case PROC_TID_SCHEDSTAT:
1823 case PROC_TGID_SCHEDSTAT:
1824 inode->i_fop = &proc_info_file_operations;
1825 ei->op.proc_read = proc_pid_schedstat;
1826 break;
1827 #endif
1828 #ifdef CONFIG_CPUSETS
1829 case PROC_TID_CPUSET:
1830 case PROC_TGID_CPUSET:
1831 inode->i_fop = &proc_cpuset_operations;
1832 break;
1833 #endif
1834 case PROC_TID_OOM_SCORE:
1835 case PROC_TGID_OOM_SCORE:
1836 inode->i_fop = &proc_info_file_operations;
1837 ei->op.proc_read = proc_oom_score;
1838 break;
1839 case PROC_TID_OOM_ADJUST:
1840 case PROC_TGID_OOM_ADJUST:
1841 inode->i_fop = &proc_oom_adjust_operations;
1842 break;
1843 #ifdef CONFIG_AUDITSYSCALL
1844 case PROC_TID_LOGINUID:
1845 case PROC_TGID_LOGINUID:
1846 inode->i_fop = &proc_loginuid_operations;
1847 break;
1848 #endif
1849 default:
1850 printk("procfs: impossible type (%d)",p->type);
1851 iput(inode);
1852 return ERR_PTR(-EINVAL);
1854 dentry->d_op = &pid_dentry_operations;
1855 d_add(dentry, inode);
1856 return NULL;
1858 out:
1859 return ERR_PTR(error);
1862 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1863 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1866 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1867 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1870 static struct file_operations proc_tgid_base_operations = {
1871 .read = generic_read_dir,
1872 .readdir = proc_tgid_base_readdir,
1875 static struct file_operations proc_tid_base_operations = {
1876 .read = generic_read_dir,
1877 .readdir = proc_tid_base_readdir,
1880 static struct inode_operations proc_tgid_base_inode_operations = {
1881 .lookup = proc_tgid_base_lookup,
1884 static struct inode_operations proc_tid_base_inode_operations = {
1885 .lookup = proc_tid_base_lookup,
1888 #ifdef CONFIG_SECURITY
1889 static int proc_tgid_attr_readdir(struct file * filp,
1890 void * dirent, filldir_t filldir)
1892 return proc_pident_readdir(filp,dirent,filldir,
1893 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1896 static int proc_tid_attr_readdir(struct file * filp,
1897 void * dirent, filldir_t filldir)
1899 return proc_pident_readdir(filp,dirent,filldir,
1900 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1903 static struct file_operations proc_tgid_attr_operations = {
1904 .read = generic_read_dir,
1905 .readdir = proc_tgid_attr_readdir,
1908 static struct file_operations proc_tid_attr_operations = {
1909 .read = generic_read_dir,
1910 .readdir = proc_tid_attr_readdir,
1913 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1914 struct dentry *dentry, struct nameidata *nd)
1916 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1919 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1920 struct dentry *dentry, struct nameidata *nd)
1922 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1925 static struct inode_operations proc_tgid_attr_inode_operations = {
1926 .lookup = proc_tgid_attr_lookup,
1929 static struct inode_operations proc_tid_attr_inode_operations = {
1930 .lookup = proc_tid_attr_lookup,
1932 #endif
1935 * /proc/self:
1937 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1938 int buflen)
1940 char tmp[30];
1941 sprintf(tmp, "%d", current->tgid);
1942 return vfs_readlink(dentry,buffer,buflen,tmp);
1945 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1947 char tmp[30];
1948 sprintf(tmp, "%d", current->tgid);
1949 return ERR_PTR(vfs_follow_link(nd,tmp));
1952 static struct inode_operations proc_self_inode_operations = {
1953 .readlink = proc_self_readlink,
1954 .follow_link = proc_self_follow_link,
1958 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1959 * @p: task that should be flushed.
1961 * Drops the /proc/@pid dcache entry from the hash chains.
1963 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1964 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1965 * if the pid value is immediately reused. This is enforced by
1966 * - caller must acquire spin_lock(p->proc_lock)
1967 * - must be called before detach_pid()
1968 * - proc_pid_lookup acquires proc_lock, and checks that
1969 * the target is not dead by looking at the attach count
1970 * of PIDTYPE_PID.
1973 struct dentry *proc_pid_unhash(struct task_struct *p)
1975 struct dentry *proc_dentry;
1977 proc_dentry = p->proc_dentry;
1978 if (proc_dentry != NULL) {
1980 spin_lock(&dcache_lock);
1981 spin_lock(&proc_dentry->d_lock);
1982 if (!d_unhashed(proc_dentry)) {
1983 dget_locked(proc_dentry);
1984 __d_drop(proc_dentry);
1985 spin_unlock(&proc_dentry->d_lock);
1986 } else {
1987 spin_unlock(&proc_dentry->d_lock);
1988 proc_dentry = NULL;
1990 spin_unlock(&dcache_lock);
1992 return proc_dentry;
1996 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1997 * @proc_dentry: directoy to prune.
1999 * Shrink the /proc directory that was used by the just killed thread.
2002 void proc_pid_flush(struct dentry *proc_dentry)
2004 might_sleep();
2005 if(proc_dentry != NULL) {
2006 shrink_dcache_parent(proc_dentry);
2007 dput(proc_dentry);
2011 /* SMP-safe */
2012 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2014 struct task_struct *task;
2015 struct inode *inode;
2016 struct proc_inode *ei;
2017 unsigned tgid;
2018 int died;
2020 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2021 inode = new_inode(dir->i_sb);
2022 if (!inode)
2023 return ERR_PTR(-ENOMEM);
2024 ei = PROC_I(inode);
2025 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2026 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2027 ei->pde = NULL;
2028 inode->i_mode = S_IFLNK|S_IRWXUGO;
2029 inode->i_uid = inode->i_gid = 0;
2030 inode->i_size = 64;
2031 inode->i_op = &proc_self_inode_operations;
2032 d_add(dentry, inode);
2033 return NULL;
2035 tgid = name_to_int(dentry);
2036 if (tgid == ~0U)
2037 goto out;
2039 read_lock(&tasklist_lock);
2040 task = find_task_by_pid(tgid);
2041 if (task)
2042 get_task_struct(task);
2043 read_unlock(&tasklist_lock);
2044 if (!task)
2045 goto out;
2047 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2050 if (!inode) {
2051 put_task_struct(task);
2052 goto out;
2054 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2055 inode->i_op = &proc_tgid_base_inode_operations;
2056 inode->i_fop = &proc_tgid_base_operations;
2057 inode->i_flags|=S_IMMUTABLE;
2058 #ifdef CONFIG_SECURITY
2059 inode->i_nlink = 5;
2060 #else
2061 inode->i_nlink = 4;
2062 #endif
2064 dentry->d_op = &pid_base_dentry_operations;
2066 died = 0;
2067 d_add(dentry, inode);
2068 spin_lock(&task->proc_lock);
2069 task->proc_dentry = dentry;
2070 if (!pid_alive(task)) {
2071 dentry = proc_pid_unhash(task);
2072 died = 1;
2074 spin_unlock(&task->proc_lock);
2076 put_task_struct(task);
2077 if (died) {
2078 proc_pid_flush(dentry);
2079 goto out;
2081 return NULL;
2082 out:
2083 return ERR_PTR(-ENOENT);
2086 /* SMP-safe */
2087 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2089 struct task_struct *task;
2090 struct task_struct *leader = proc_task(dir);
2091 struct inode *inode;
2092 unsigned tid;
2094 tid = name_to_int(dentry);
2095 if (tid == ~0U)
2096 goto out;
2098 read_lock(&tasklist_lock);
2099 task = find_task_by_pid(tid);
2100 if (task)
2101 get_task_struct(task);
2102 read_unlock(&tasklist_lock);
2103 if (!task)
2104 goto out;
2105 if (leader->tgid != task->tgid)
2106 goto out_drop_task;
2108 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2111 if (!inode)
2112 goto out_drop_task;
2113 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2114 inode->i_op = &proc_tid_base_inode_operations;
2115 inode->i_fop = &proc_tid_base_operations;
2116 inode->i_flags|=S_IMMUTABLE;
2117 #ifdef CONFIG_SECURITY
2118 inode->i_nlink = 4;
2119 #else
2120 inode->i_nlink = 3;
2121 #endif
2123 dentry->d_op = &pid_base_dentry_operations;
2125 d_add(dentry, inode);
2127 put_task_struct(task);
2128 return NULL;
2129 out_drop_task:
2130 put_task_struct(task);
2131 out:
2132 return ERR_PTR(-ENOENT);
2135 #define PROC_NUMBUF 10
2136 #define PROC_MAXPIDS 20
2139 * Get a few tgid's to return for filldir - we need to hold the
2140 * tasklist lock while doing this, and we must release it before
2141 * we actually do the filldir itself, so we use a temp buffer..
2143 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2145 struct task_struct *p;
2146 int nr_tgids = 0;
2148 index--;
2149 read_lock(&tasklist_lock);
2150 p = NULL;
2151 if (version) {
2152 p = find_task_by_pid(version);
2153 if (p && !thread_group_leader(p))
2154 p = NULL;
2157 if (p)
2158 index = 0;
2159 else
2160 p = next_task(&init_task);
2162 for ( ; p != &init_task; p = next_task(p)) {
2163 int tgid = p->pid;
2164 if (!pid_alive(p))
2165 continue;
2166 if (--index >= 0)
2167 continue;
2168 tgids[nr_tgids] = tgid;
2169 nr_tgids++;
2170 if (nr_tgids >= PROC_MAXPIDS)
2171 break;
2173 read_unlock(&tasklist_lock);
2174 return nr_tgids;
2178 * Get a few tid's to return for filldir - we need to hold the
2179 * tasklist lock while doing this, and we must release it before
2180 * we actually do the filldir itself, so we use a temp buffer..
2182 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2184 struct task_struct *leader_task = proc_task(dir);
2185 struct task_struct *task = leader_task;
2186 int nr_tids = 0;
2188 index -= 2;
2189 read_lock(&tasklist_lock);
2191 * The starting point task (leader_task) might be an already
2192 * unlinked task, which cannot be used to access the task-list
2193 * via next_thread().
2195 if (pid_alive(task)) do {
2196 int tid = task->pid;
2198 if (--index >= 0)
2199 continue;
2200 if (tids != NULL)
2201 tids[nr_tids] = tid;
2202 nr_tids++;
2203 if (nr_tids >= PROC_MAXPIDS)
2204 break;
2205 } while ((task = next_thread(task)) != leader_task);
2206 read_unlock(&tasklist_lock);
2207 return nr_tids;
2210 /* for the /proc/ directory itself, after non-process stuff has been done */
2211 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2213 unsigned int tgid_array[PROC_MAXPIDS];
2214 char buf[PROC_NUMBUF];
2215 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2216 unsigned int nr_tgids, i;
2217 int next_tgid;
2219 if (!nr) {
2220 ino_t ino = fake_ino(0,PROC_TGID_INO);
2221 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2222 return 0;
2223 filp->f_pos++;
2224 nr++;
2227 /* f_version caches the tgid value that the last readdir call couldn't
2228 * return. lseek aka telldir automagically resets f_version to 0.
2230 next_tgid = filp->f_version;
2231 filp->f_version = 0;
2232 for (;;) {
2233 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2234 if (!nr_tgids) {
2235 /* no more entries ! */
2236 break;
2238 next_tgid = 0;
2240 /* do not use the last found pid, reserve it for next_tgid */
2241 if (nr_tgids == PROC_MAXPIDS) {
2242 nr_tgids--;
2243 next_tgid = tgid_array[nr_tgids];
2246 for (i=0;i<nr_tgids;i++) {
2247 int tgid = tgid_array[i];
2248 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2249 unsigned long j = PROC_NUMBUF;
2252 buf[--j] = '0' + (tgid % 10);
2253 while ((tgid /= 10) != 0);
2255 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2256 /* returning this tgid failed, save it as the first
2257 * pid for the next readir call */
2258 filp->f_version = tgid_array[i];
2259 goto out;
2261 filp->f_pos++;
2262 nr++;
2265 out:
2266 return 0;
2269 /* for the /proc/TGID/task/ directories */
2270 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2272 unsigned int tid_array[PROC_MAXPIDS];
2273 char buf[PROC_NUMBUF];
2274 unsigned int nr_tids, i;
2275 struct dentry *dentry = filp->f_dentry;
2276 struct inode *inode = dentry->d_inode;
2277 int retval = -ENOENT;
2278 ino_t ino;
2279 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2281 if (!pid_alive(proc_task(inode)))
2282 goto out;
2283 retval = 0;
2285 switch (pos) {
2286 case 0:
2287 ino = inode->i_ino;
2288 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2289 goto out;
2290 pos++;
2291 /* fall through */
2292 case 1:
2293 ino = parent_ino(dentry);
2294 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2295 goto out;
2296 pos++;
2297 /* fall through */
2300 nr_tids = get_tid_list(pos, tid_array, inode);
2301 inode->i_nlink = pos + nr_tids;
2303 for (i = 0; i < nr_tids; i++) {
2304 unsigned long j = PROC_NUMBUF;
2305 int tid = tid_array[i];
2307 ino = fake_ino(tid,PROC_TID_INO);
2310 buf[--j] = '0' + (tid % 10);
2311 while ((tid /= 10) != 0);
2313 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
2314 break;
2315 pos++;
2317 out:
2318 filp->f_pos = pos;
2319 return retval;