drivers/scsi/psi240i.c: fix an array overrun
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / proc / base.c
blob155602fa431a85d823c6dc5c04204f18c102acc1
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_WCHAN,
108 #ifdef CONFIG_MMU
109 PROC_TGID_SMAPS,
110 #endif
111 #ifdef CONFIG_SCHEDSTATS
112 PROC_TGID_SCHEDSTAT,
113 #endif
114 #ifdef CONFIG_CPUSETS
115 PROC_TGID_CPUSET,
116 #endif
117 #ifdef CONFIG_SECURITY
118 PROC_TGID_ATTR,
119 PROC_TGID_ATTR_CURRENT,
120 PROC_TGID_ATTR_PREV,
121 PROC_TGID_ATTR_EXEC,
122 PROC_TGID_ATTR_FSCREATE,
123 #endif
124 #ifdef CONFIG_AUDITSYSCALL
125 PROC_TGID_LOGINUID,
126 #endif
127 PROC_TGID_OOM_SCORE,
128 PROC_TGID_OOM_ADJUST,
129 PROC_TID_INO,
130 PROC_TID_STATUS,
131 PROC_TID_MEM,
132 #ifdef CONFIG_SECCOMP
133 PROC_TID_SECCOMP,
134 #endif
135 PROC_TID_CWD,
136 PROC_TID_ROOT,
137 PROC_TID_EXE,
138 PROC_TID_FD,
139 PROC_TID_ENVIRON,
140 PROC_TID_AUXV,
141 PROC_TID_CMDLINE,
142 PROC_TID_STAT,
143 PROC_TID_STATM,
144 PROC_TID_MAPS,
145 PROC_TID_NUMA_MAPS,
146 PROC_TID_MOUNTS,
147 PROC_TID_WCHAN,
148 #ifdef CONFIG_MMU
149 PROC_TID_SMAPS,
150 #endif
151 #ifdef CONFIG_SCHEDSTATS
152 PROC_TID_SCHEDSTAT,
153 #endif
154 #ifdef CONFIG_CPUSETS
155 PROC_TID_CPUSET,
156 #endif
157 #ifdef CONFIG_SECURITY
158 PROC_TID_ATTR,
159 PROC_TID_ATTR_CURRENT,
160 PROC_TID_ATTR_PREV,
161 PROC_TID_ATTR_EXEC,
162 PROC_TID_ATTR_FSCREATE,
163 #endif
164 #ifdef CONFIG_AUDITSYSCALL
165 PROC_TID_LOGINUID,
166 #endif
167 PROC_TID_OOM_SCORE,
168 PROC_TID_OOM_ADJUST,
170 /* Add new entries before this */
171 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
174 struct pid_entry {
175 int type;
176 int len;
177 char *name;
178 mode_t mode;
181 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
183 static struct pid_entry tgid_base_stuff[] = {
184 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
185 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
186 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
187 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
188 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
189 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
190 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
191 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
192 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
193 #ifdef CONFIG_NUMA
194 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
195 #endif
196 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
197 #ifdef CONFIG_SECCOMP
198 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
199 #endif
200 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
201 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
202 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
203 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
204 #ifdef CONFIG_MMU
205 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
206 #endif
207 #ifdef CONFIG_SECURITY
208 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
209 #endif
210 #ifdef CONFIG_KALLSYMS
211 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
212 #endif
213 #ifdef CONFIG_SCHEDSTATS
214 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
215 #endif
216 #ifdef CONFIG_CPUSETS
217 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
218 #endif
219 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
220 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
221 #ifdef CONFIG_AUDITSYSCALL
222 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
223 #endif
224 {0,0,NULL,0}
226 static struct pid_entry tid_base_stuff[] = {
227 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
228 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
229 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
230 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
231 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
232 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
233 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
234 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
235 #ifdef CONFIG_NUMA
236 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
237 #endif
238 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
239 #ifdef CONFIG_SECCOMP
240 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
241 #endif
242 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
243 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
244 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
245 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
246 #ifdef CONFIG_MMU
247 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
248 #endif
249 #ifdef CONFIG_SECURITY
250 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
251 #endif
252 #ifdef CONFIG_KALLSYMS
253 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
254 #endif
255 #ifdef CONFIG_SCHEDSTATS
256 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
257 #endif
258 #ifdef CONFIG_CPUSETS
259 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
260 #endif
261 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
262 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
263 #ifdef CONFIG_AUDITSYSCALL
264 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
265 #endif
266 {0,0,NULL,0}
269 #ifdef CONFIG_SECURITY
270 static struct pid_entry tgid_attr_stuff[] = {
271 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
272 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
273 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
274 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
275 {0,0,NULL,0}
277 static struct pid_entry tid_attr_stuff[] = {
278 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
279 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
280 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
281 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
282 {0,0,NULL,0}
284 #endif
286 #undef E
288 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
290 struct task_struct *task = proc_task(inode);
291 struct files_struct *files;
292 struct file *file;
293 int fd = proc_type(inode) - PROC_TID_FD_DIR;
295 files = get_files_struct(task);
296 if (files) {
298 * We are not taking a ref to the file structure, so we must
299 * hold ->file_lock.
301 spin_lock(&files->file_lock);
302 file = fcheck_files(files, fd);
303 if (file) {
304 *mnt = mntget(file->f_vfsmnt);
305 *dentry = dget(file->f_dentry);
306 spin_unlock(&files->file_lock);
307 put_files_struct(files);
308 return 0;
310 spin_unlock(&files->file_lock);
311 put_files_struct(files);
313 return -ENOENT;
316 static struct fs_struct *get_fs_struct(struct task_struct *task)
318 struct fs_struct *fs;
319 task_lock(task);
320 fs = task->fs;
321 if(fs)
322 atomic_inc(&fs->count);
323 task_unlock(task);
324 return fs;
327 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
329 struct fs_struct *fs = get_fs_struct(proc_task(inode));
330 int result = -ENOENT;
331 if (fs) {
332 read_lock(&fs->lock);
333 *mnt = mntget(fs->pwdmnt);
334 *dentry = dget(fs->pwd);
335 read_unlock(&fs->lock);
336 result = 0;
337 put_fs_struct(fs);
339 return result;
342 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
344 struct fs_struct *fs = get_fs_struct(proc_task(inode));
345 int result = -ENOENT;
346 if (fs) {
347 read_lock(&fs->lock);
348 *mnt = mntget(fs->rootmnt);
349 *dentry = dget(fs->root);
350 read_unlock(&fs->lock);
351 result = 0;
352 put_fs_struct(fs);
354 return result;
358 /* Same as proc_root_link, but this addionally tries to get fs from other
359 * threads in the group */
360 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
361 struct vfsmount **mnt)
363 struct fs_struct *fs;
364 int result = -ENOENT;
365 struct task_struct *leader = proc_task(inode);
367 task_lock(leader);
368 fs = leader->fs;
369 if (fs) {
370 atomic_inc(&fs->count);
371 task_unlock(leader);
372 } else {
373 /* Try to get fs from other threads */
374 task_unlock(leader);
375 read_lock(&tasklist_lock);
376 if (pid_alive(leader)) {
377 struct task_struct *task = leader;
379 while ((task = next_thread(task)) != leader) {
380 task_lock(task);
381 fs = task->fs;
382 if (fs) {
383 atomic_inc(&fs->count);
384 task_unlock(task);
385 break;
387 task_unlock(task);
390 read_unlock(&tasklist_lock);
393 if (fs) {
394 read_lock(&fs->lock);
395 *mnt = mntget(fs->rootmnt);
396 *dentry = dget(fs->root);
397 read_unlock(&fs->lock);
398 result = 0;
399 put_fs_struct(fs);
401 return result;
405 #define MAY_PTRACE(task) \
406 (task == current || \
407 (task->parent == current && \
408 (task->ptrace & PT_PTRACED) && \
409 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
410 security_ptrace(current,task) == 0))
412 static int proc_pid_environ(struct task_struct *task, char * buffer)
414 int res = 0;
415 struct mm_struct *mm = get_task_mm(task);
416 if (mm) {
417 unsigned int len = mm->env_end - mm->env_start;
418 if (len > PAGE_SIZE)
419 len = PAGE_SIZE;
420 res = access_process_vm(task, mm->env_start, buffer, len, 0);
421 if (!ptrace_may_attach(task))
422 res = -ESRCH;
423 mmput(mm);
425 return res;
428 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
430 int res = 0;
431 unsigned int len;
432 struct mm_struct *mm = get_task_mm(task);
433 if (!mm)
434 goto out;
435 if (!mm->arg_end)
436 goto out_mm; /* Shh! No looking before we're done */
438 len = mm->arg_end - mm->arg_start;
440 if (len > PAGE_SIZE)
441 len = PAGE_SIZE;
443 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
445 // If the nul at the end of args has been overwritten, then
446 // assume application is using setproctitle(3).
447 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
448 len = strnlen(buffer, res);
449 if (len < res) {
450 res = len;
451 } else {
452 len = mm->env_end - mm->env_start;
453 if (len > PAGE_SIZE - res)
454 len = PAGE_SIZE - res;
455 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
456 res = strnlen(buffer, res);
459 out_mm:
460 mmput(mm);
461 out:
462 return res;
465 static int proc_pid_auxv(struct task_struct *task, char *buffer)
467 int res = 0;
468 struct mm_struct *mm = get_task_mm(task);
469 if (mm) {
470 unsigned int nwords = 0;
472 nwords += 2;
473 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
474 res = nwords * sizeof(mm->saved_auxv[0]);
475 if (res > PAGE_SIZE)
476 res = PAGE_SIZE;
477 memcpy(buffer, mm->saved_auxv, res);
478 mmput(mm);
480 return res;
484 #ifdef CONFIG_KALLSYMS
486 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
487 * Returns the resolved symbol. If that fails, simply return the address.
489 static int proc_pid_wchan(struct task_struct *task, char *buffer)
491 char *modname;
492 const char *sym_name;
493 unsigned long wchan, size, offset;
494 char namebuf[KSYM_NAME_LEN+1];
496 wchan = get_wchan(task);
498 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
499 if (sym_name)
500 return sprintf(buffer, "%s", sym_name);
501 return sprintf(buffer, "%lu", wchan);
503 #endif /* CONFIG_KALLSYMS */
505 #ifdef CONFIG_SCHEDSTATS
507 * Provides /proc/PID/schedstat
509 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
511 return sprintf(buffer, "%lu %lu %lu\n",
512 task->sched_info.cpu_time,
513 task->sched_info.run_delay,
514 task->sched_info.pcnt);
516 #endif
518 /* The badness from the OOM killer */
519 unsigned long badness(struct task_struct *p, unsigned long uptime);
520 static int proc_oom_score(struct task_struct *task, char *buffer)
522 unsigned long points;
523 struct timespec uptime;
525 do_posix_clock_monotonic_gettime(&uptime);
526 points = badness(task, uptime.tv_sec);
527 return sprintf(buffer, "%lu\n", points);
530 /************************************************************************/
531 /* Here the fs part begins */
532 /************************************************************************/
534 /* permission checks */
536 /* If the process being read is separated by chroot from the reading process,
537 * don't let the reader access the threads.
539 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
541 struct dentry *de, *base;
542 struct vfsmount *our_vfsmnt, *mnt;
543 int res = 0;
544 read_lock(&current->fs->lock);
545 our_vfsmnt = mntget(current->fs->rootmnt);
546 base = dget(current->fs->root);
547 read_unlock(&current->fs->lock);
549 spin_lock(&vfsmount_lock);
550 de = root;
551 mnt = vfsmnt;
553 while (vfsmnt != our_vfsmnt) {
554 if (vfsmnt == vfsmnt->mnt_parent)
555 goto out;
556 de = vfsmnt->mnt_mountpoint;
557 vfsmnt = vfsmnt->mnt_parent;
560 if (!is_subdir(de, base))
561 goto out;
562 spin_unlock(&vfsmount_lock);
564 exit:
565 dput(base);
566 mntput(our_vfsmnt);
567 dput(root);
568 mntput(mnt);
569 return res;
570 out:
571 spin_unlock(&vfsmount_lock);
572 res = -EACCES;
573 goto exit;
576 static int proc_check_root(struct inode *inode)
578 struct dentry *root;
579 struct vfsmount *vfsmnt;
581 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
582 return -ENOENT;
583 return proc_check_chroot(root, vfsmnt);
586 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
588 if (generic_permission(inode, mask, NULL) != 0)
589 return -EACCES;
590 return proc_check_root(inode);
593 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
595 int error;
596 struct inode *inode = dentry->d_inode;
598 if (attr->ia_valid & ATTR_MODE)
599 return -EPERM;
601 error = inode_change_ok(inode, attr);
602 if (!error) {
603 error = security_inode_setattr(dentry, attr);
604 if (!error)
605 error = inode_setattr(inode, attr);
607 return error;
610 static struct inode_operations proc_def_inode_operations = {
611 .setattr = proc_setattr,
614 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
616 struct dentry *root;
617 struct vfsmount *vfsmnt;
619 if (generic_permission(inode, mask, NULL) != 0)
620 return -EACCES;
622 if (proc_task_root_link(inode, &root, &vfsmnt))
623 return -ENOENT;
625 return proc_check_chroot(root, vfsmnt);
628 extern struct seq_operations proc_pid_maps_op;
629 static int maps_open(struct inode *inode, struct file *file)
631 struct task_struct *task = proc_task(inode);
632 int ret = seq_open(file, &proc_pid_maps_op);
633 if (!ret) {
634 struct seq_file *m = file->private_data;
635 m->private = task;
637 return ret;
640 static struct file_operations proc_maps_operations = {
641 .open = maps_open,
642 .read = seq_read,
643 .llseek = seq_lseek,
644 .release = seq_release,
647 #ifdef CONFIG_NUMA
648 extern struct seq_operations proc_pid_numa_maps_op;
649 static int numa_maps_open(struct inode *inode, struct file *file)
651 struct task_struct *task = proc_task(inode);
652 int ret = seq_open(file, &proc_pid_numa_maps_op);
653 if (!ret) {
654 struct seq_file *m = file->private_data;
655 m->private = task;
657 return ret;
660 static struct file_operations proc_numa_maps_operations = {
661 .open = numa_maps_open,
662 .read = seq_read,
663 .llseek = seq_lseek,
664 .release = seq_release,
666 #endif
668 #ifdef CONFIG_MMU
669 extern struct seq_operations proc_pid_smaps_op;
670 static int smaps_open(struct inode *inode, struct file *file)
672 struct task_struct *task = proc_task(inode);
673 int ret = seq_open(file, &proc_pid_smaps_op);
674 if (!ret) {
675 struct seq_file *m = file->private_data;
676 m->private = task;
678 return ret;
681 static struct file_operations proc_smaps_operations = {
682 .open = smaps_open,
683 .read = seq_read,
684 .llseek = seq_lseek,
685 .release = seq_release,
687 #endif
689 extern struct seq_operations mounts_op;
690 struct proc_mounts {
691 struct seq_file m;
692 int event;
695 static int mounts_open(struct inode *inode, struct file *file)
697 struct task_struct *task = proc_task(inode);
698 struct namespace *namespace;
699 struct proc_mounts *p;
700 int ret = -EINVAL;
702 task_lock(task);
703 namespace = task->namespace;
704 if (namespace)
705 get_namespace(namespace);
706 task_unlock(task);
708 if (namespace) {
709 ret = -ENOMEM;
710 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
711 if (p) {
712 file->private_data = &p->m;
713 ret = seq_open(file, &mounts_op);
714 if (!ret) {
715 p->m.private = namespace;
716 p->event = namespace->event;
717 return 0;
719 kfree(p);
721 put_namespace(namespace);
723 return ret;
726 static int mounts_release(struct inode *inode, struct file *file)
728 struct seq_file *m = file->private_data;
729 struct namespace *namespace = m->private;
730 put_namespace(namespace);
731 return seq_release(inode, file);
734 static unsigned mounts_poll(struct file *file, poll_table *wait)
736 struct proc_mounts *p = file->private_data;
737 struct namespace *ns = p->m.private;
738 unsigned res = 0;
740 poll_wait(file, &ns->poll, wait);
742 spin_lock(&vfsmount_lock);
743 if (p->event != ns->event) {
744 p->event = ns->event;
745 res = POLLERR;
747 spin_unlock(&vfsmount_lock);
749 return res;
752 static struct file_operations proc_mounts_operations = {
753 .open = mounts_open,
754 .read = seq_read,
755 .llseek = seq_lseek,
756 .release = mounts_release,
757 .poll = mounts_poll,
760 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
762 static ssize_t proc_info_read(struct file * file, char __user * buf,
763 size_t count, loff_t *ppos)
765 struct inode * inode = file->f_dentry->d_inode;
766 unsigned long page;
767 ssize_t length;
768 struct task_struct *task = proc_task(inode);
770 if (count > PROC_BLOCK_SIZE)
771 count = PROC_BLOCK_SIZE;
772 if (!(page = __get_free_page(GFP_KERNEL)))
773 return -ENOMEM;
775 length = PROC_I(inode)->op.proc_read(task, (char*)page);
777 if (length >= 0)
778 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
779 free_page(page);
780 return length;
783 static struct file_operations proc_info_file_operations = {
784 .read = proc_info_read,
787 static int mem_open(struct inode* inode, struct file* file)
789 file->private_data = (void*)((long)current->self_exec_id);
790 return 0;
793 static ssize_t mem_read(struct file * file, char __user * buf,
794 size_t count, loff_t *ppos)
796 struct task_struct *task = proc_task(file->f_dentry->d_inode);
797 char *page;
798 unsigned long src = *ppos;
799 int ret = -ESRCH;
800 struct mm_struct *mm;
802 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
803 goto out;
805 ret = -ENOMEM;
806 page = (char *)__get_free_page(GFP_USER);
807 if (!page)
808 goto out;
810 ret = 0;
812 mm = get_task_mm(task);
813 if (!mm)
814 goto out_free;
816 ret = -EIO;
818 if (file->private_data != (void*)((long)current->self_exec_id))
819 goto out_put;
821 ret = 0;
823 while (count > 0) {
824 int this_len, retval;
826 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
827 retval = access_process_vm(task, src, page, this_len, 0);
828 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
829 if (!ret)
830 ret = -EIO;
831 break;
834 if (copy_to_user(buf, page, retval)) {
835 ret = -EFAULT;
836 break;
839 ret += retval;
840 src += retval;
841 buf += retval;
842 count -= retval;
844 *ppos = src;
846 out_put:
847 mmput(mm);
848 out_free:
849 free_page((unsigned long) page);
850 out:
851 return ret;
854 #define mem_write NULL
856 #ifndef mem_write
857 /* This is a security hazard */
858 static ssize_t mem_write(struct file * file, const char * buf,
859 size_t count, loff_t *ppos)
861 int copied = 0;
862 char *page;
863 struct task_struct *task = proc_task(file->f_dentry->d_inode);
864 unsigned long dst = *ppos;
866 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
867 return -ESRCH;
869 page = (char *)__get_free_page(GFP_USER);
870 if (!page)
871 return -ENOMEM;
873 while (count > 0) {
874 int this_len, retval;
876 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
877 if (copy_from_user(page, buf, this_len)) {
878 copied = -EFAULT;
879 break;
881 retval = access_process_vm(task, dst, page, this_len, 1);
882 if (!retval) {
883 if (!copied)
884 copied = -EIO;
885 break;
887 copied += retval;
888 buf += retval;
889 dst += retval;
890 count -= retval;
892 *ppos = dst;
893 free_page((unsigned long) page);
894 return copied;
896 #endif
898 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
900 switch (orig) {
901 case 0:
902 file->f_pos = offset;
903 break;
904 case 1:
905 file->f_pos += offset;
906 break;
907 default:
908 return -EINVAL;
910 force_successful_syscall_return();
911 return file->f_pos;
914 static struct file_operations proc_mem_operations = {
915 .llseek = mem_lseek,
916 .read = mem_read,
917 .write = mem_write,
918 .open = mem_open,
921 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
922 size_t count, loff_t *ppos)
924 struct task_struct *task = proc_task(file->f_dentry->d_inode);
925 char buffer[8];
926 size_t len;
927 int oom_adjust = task->oomkilladj;
928 loff_t __ppos = *ppos;
930 len = sprintf(buffer, "%i\n", oom_adjust);
931 if (__ppos >= len)
932 return 0;
933 if (count > len-__ppos)
934 count = len-__ppos;
935 if (copy_to_user(buf, buffer + __ppos, count))
936 return -EFAULT;
937 *ppos = __ppos + count;
938 return count;
941 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
942 size_t count, loff_t *ppos)
944 struct task_struct *task = proc_task(file->f_dentry->d_inode);
945 char buffer[8], *end;
946 int oom_adjust;
948 if (!capable(CAP_SYS_RESOURCE))
949 return -EPERM;
950 memset(buffer, 0, 8);
951 if (count > 6)
952 count = 6;
953 if (copy_from_user(buffer, buf, count))
954 return -EFAULT;
955 oom_adjust = simple_strtol(buffer, &end, 0);
956 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
957 return -EINVAL;
958 if (*end == '\n')
959 end++;
960 task->oomkilladj = oom_adjust;
961 if (end - buffer == 0)
962 return -EIO;
963 return end - buffer;
966 static struct file_operations proc_oom_adjust_operations = {
967 .read = oom_adjust_read,
968 .write = oom_adjust_write,
971 static struct inode_operations proc_mem_inode_operations = {
972 .permission = proc_permission,
973 .setattr = proc_setattr,
976 #ifdef CONFIG_AUDITSYSCALL
977 #define TMPBUFLEN 21
978 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
979 size_t count, loff_t *ppos)
981 struct inode * inode = file->f_dentry->d_inode;
982 struct task_struct *task = proc_task(inode);
983 ssize_t length;
984 char tmpbuf[TMPBUFLEN];
986 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
987 audit_get_loginuid(task->audit_context));
988 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
991 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
992 size_t count, loff_t *ppos)
994 struct inode * inode = file->f_dentry->d_inode;
995 char *page, *tmp;
996 ssize_t length;
997 struct task_struct *task = proc_task(inode);
998 uid_t loginuid;
1000 if (!capable(CAP_AUDIT_CONTROL))
1001 return -EPERM;
1003 if (current != task)
1004 return -EPERM;
1006 if (count > PAGE_SIZE)
1007 count = PAGE_SIZE;
1009 if (*ppos != 0) {
1010 /* No partial writes. */
1011 return -EINVAL;
1013 page = (char*)__get_free_page(GFP_USER);
1014 if (!page)
1015 return -ENOMEM;
1016 length = -EFAULT;
1017 if (copy_from_user(page, buf, count))
1018 goto out_free_page;
1020 loginuid = simple_strtoul(page, &tmp, 10);
1021 if (tmp == page) {
1022 length = -EINVAL;
1023 goto out_free_page;
1026 length = audit_set_loginuid(task, loginuid);
1027 if (likely(length == 0))
1028 length = count;
1030 out_free_page:
1031 free_page((unsigned long) page);
1032 return length;
1035 static struct file_operations proc_loginuid_operations = {
1036 .read = proc_loginuid_read,
1037 .write = proc_loginuid_write,
1039 #endif
1041 #ifdef CONFIG_SECCOMP
1042 static ssize_t seccomp_read(struct file *file, char __user *buf,
1043 size_t count, loff_t *ppos)
1045 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1046 char __buf[20];
1047 loff_t __ppos = *ppos;
1048 size_t len;
1050 /* no need to print the trailing zero, so use only len */
1051 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1052 if (__ppos >= len)
1053 return 0;
1054 if (count > len - __ppos)
1055 count = len - __ppos;
1056 if (copy_to_user(buf, __buf + __ppos, count))
1057 return -EFAULT;
1058 *ppos = __ppos + count;
1059 return count;
1062 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1063 size_t count, loff_t *ppos)
1065 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1066 char __buf[20], *end;
1067 unsigned int seccomp_mode;
1069 /* can set it only once to be even more secure */
1070 if (unlikely(tsk->seccomp.mode))
1071 return -EPERM;
1073 memset(__buf, 0, sizeof(__buf));
1074 count = min(count, sizeof(__buf) - 1);
1075 if (copy_from_user(__buf, buf, count))
1076 return -EFAULT;
1077 seccomp_mode = simple_strtoul(__buf, &end, 0);
1078 if (*end == '\n')
1079 end++;
1080 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1081 tsk->seccomp.mode = seccomp_mode;
1082 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1083 } else
1084 return -EINVAL;
1085 if (unlikely(!(end - __buf)))
1086 return -EIO;
1087 return end - __buf;
1090 static struct file_operations proc_seccomp_operations = {
1091 .read = seccomp_read,
1092 .write = seccomp_write,
1094 #endif /* CONFIG_SECCOMP */
1096 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1098 struct inode *inode = dentry->d_inode;
1099 int error = -EACCES;
1101 /* We don't need a base pointer in the /proc filesystem */
1102 path_release(nd);
1104 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1105 goto out;
1106 error = proc_check_root(inode);
1107 if (error)
1108 goto out;
1110 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1111 nd->last_type = LAST_BIND;
1112 out:
1113 return ERR_PTR(error);
1116 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1117 char __user *buffer, int buflen)
1119 struct inode * inode;
1120 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1121 int len;
1123 if (!tmp)
1124 return -ENOMEM;
1126 inode = dentry->d_inode;
1127 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1128 len = PTR_ERR(path);
1129 if (IS_ERR(path))
1130 goto out;
1131 len = tmp + PAGE_SIZE - 1 - path;
1133 if (len > buflen)
1134 len = buflen;
1135 if (copy_to_user(buffer, path, len))
1136 len = -EFAULT;
1137 out:
1138 free_page((unsigned long)tmp);
1139 return len;
1142 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1144 int error = -EACCES;
1145 struct inode *inode = dentry->d_inode;
1146 struct dentry *de;
1147 struct vfsmount *mnt = NULL;
1149 lock_kernel();
1151 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1152 goto out;
1153 error = proc_check_root(inode);
1154 if (error)
1155 goto out;
1157 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1158 if (error)
1159 goto out;
1161 error = do_proc_readlink(de, mnt, buffer, buflen);
1162 dput(de);
1163 mntput(mnt);
1164 out:
1165 unlock_kernel();
1166 return error;
1169 static struct inode_operations proc_pid_link_inode_operations = {
1170 .readlink = proc_pid_readlink,
1171 .follow_link = proc_pid_follow_link,
1172 .setattr = proc_setattr,
1175 #define NUMBUF 10
1177 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1179 struct inode *inode = filp->f_dentry->d_inode;
1180 struct task_struct *p = proc_task(inode);
1181 unsigned int fd, tid, ino;
1182 int retval;
1183 char buf[NUMBUF];
1184 struct files_struct * files;
1185 struct fdtable *fdt;
1187 retval = -ENOENT;
1188 if (!pid_alive(p))
1189 goto out;
1190 retval = 0;
1191 tid = p->pid;
1193 fd = filp->f_pos;
1194 switch (fd) {
1195 case 0:
1196 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1197 goto out;
1198 filp->f_pos++;
1199 case 1:
1200 ino = fake_ino(tid, PROC_TID_INO);
1201 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1202 goto out;
1203 filp->f_pos++;
1204 default:
1205 files = get_files_struct(p);
1206 if (!files)
1207 goto out;
1208 rcu_read_lock();
1209 fdt = files_fdtable(files);
1210 for (fd = filp->f_pos-2;
1211 fd < fdt->max_fds;
1212 fd++, filp->f_pos++) {
1213 unsigned int i,j;
1215 if (!fcheck_files(files, fd))
1216 continue;
1217 rcu_read_unlock();
1219 j = NUMBUF;
1220 i = fd;
1221 do {
1222 j--;
1223 buf[j] = '0' + (i % 10);
1224 i /= 10;
1225 } while (i);
1227 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1228 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1229 rcu_read_lock();
1230 break;
1232 rcu_read_lock();
1234 rcu_read_unlock();
1235 put_files_struct(files);
1237 out:
1238 return retval;
1241 static int proc_pident_readdir(struct file *filp,
1242 void *dirent, filldir_t filldir,
1243 struct pid_entry *ents, unsigned int nents)
1245 int i;
1246 int pid;
1247 struct dentry *dentry = filp->f_dentry;
1248 struct inode *inode = dentry->d_inode;
1249 struct pid_entry *p;
1250 ino_t ino;
1251 int ret;
1253 ret = -ENOENT;
1254 if (!pid_alive(proc_task(inode)))
1255 goto out;
1257 ret = 0;
1258 pid = proc_task(inode)->pid;
1259 i = filp->f_pos;
1260 switch (i) {
1261 case 0:
1262 ino = inode->i_ino;
1263 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1264 goto out;
1265 i++;
1266 filp->f_pos++;
1267 /* fall through */
1268 case 1:
1269 ino = parent_ino(dentry);
1270 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1271 goto out;
1272 i++;
1273 filp->f_pos++;
1274 /* fall through */
1275 default:
1276 i -= 2;
1277 if (i >= nents) {
1278 ret = 1;
1279 goto out;
1281 p = ents + i;
1282 while (p->name) {
1283 if (filldir(dirent, p->name, p->len, filp->f_pos,
1284 fake_ino(pid, p->type), p->mode >> 12) < 0)
1285 goto out;
1286 filp->f_pos++;
1287 p++;
1291 ret = 1;
1292 out:
1293 return ret;
1296 static int proc_tgid_base_readdir(struct file * filp,
1297 void * dirent, filldir_t filldir)
1299 return proc_pident_readdir(filp,dirent,filldir,
1300 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1303 static int proc_tid_base_readdir(struct file * filp,
1304 void * dirent, filldir_t filldir)
1306 return proc_pident_readdir(filp,dirent,filldir,
1307 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1310 /* building an inode */
1312 static int task_dumpable(struct task_struct *task)
1314 int dumpable = 0;
1315 struct mm_struct *mm;
1317 task_lock(task);
1318 mm = task->mm;
1319 if (mm)
1320 dumpable = mm->dumpable;
1321 task_unlock(task);
1322 if(dumpable == 1)
1323 return 1;
1324 return 0;
1328 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1330 struct inode * inode;
1331 struct proc_inode *ei;
1333 /* We need a new inode */
1335 inode = new_inode(sb);
1336 if (!inode)
1337 goto out;
1339 /* Common stuff */
1340 ei = PROC_I(inode);
1341 ei->task = NULL;
1342 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1343 inode->i_ino = fake_ino(task->pid, ino);
1344 inode->i_op = &proc_def_inode_operations;
1346 if (!pid_alive(task))
1347 goto out_unlock;
1350 * grab the reference to task.
1352 get_task_struct(task);
1353 ei->task = task;
1354 ei->type = ino;
1355 inode->i_uid = 0;
1356 inode->i_gid = 0;
1357 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1358 inode->i_uid = task->euid;
1359 inode->i_gid = task->egid;
1361 security_task_to_inode(task, inode);
1363 out:
1364 return inode;
1366 out_unlock:
1367 ei->pde = NULL;
1368 iput(inode);
1369 return NULL;
1372 /* dentry stuff */
1375 * Exceptional case: normally we are not allowed to unhash a busy
1376 * directory. In this case, however, we can do it - no aliasing problems
1377 * due to the way we treat inodes.
1379 * Rewrite the inode's ownerships here because the owning task may have
1380 * performed a setuid(), etc.
1382 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1384 struct inode *inode = dentry->d_inode;
1385 struct task_struct *task = proc_task(inode);
1386 if (pid_alive(task)) {
1387 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1388 inode->i_uid = task->euid;
1389 inode->i_gid = task->egid;
1390 } else {
1391 inode->i_uid = 0;
1392 inode->i_gid = 0;
1394 inode->i_mode &= ~(S_ISUID | S_ISGID);
1395 security_task_to_inode(task, inode);
1396 return 1;
1398 d_drop(dentry);
1399 return 0;
1402 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1404 struct inode *inode = dentry->d_inode;
1405 struct task_struct *task = proc_task(inode);
1406 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1407 struct files_struct *files;
1409 files = get_files_struct(task);
1410 if (files) {
1411 rcu_read_lock();
1412 if (fcheck_files(files, fd)) {
1413 rcu_read_unlock();
1414 put_files_struct(files);
1415 if (task_dumpable(task)) {
1416 inode->i_uid = task->euid;
1417 inode->i_gid = task->egid;
1418 } else {
1419 inode->i_uid = 0;
1420 inode->i_gid = 0;
1422 inode->i_mode &= ~(S_ISUID | S_ISGID);
1423 security_task_to_inode(task, inode);
1424 return 1;
1426 rcu_read_unlock();
1427 put_files_struct(files);
1429 d_drop(dentry);
1430 return 0;
1433 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1435 struct task_struct *task = proc_task(inode);
1436 spin_lock(&task->proc_lock);
1437 if (task->proc_dentry == dentry)
1438 task->proc_dentry = NULL;
1439 spin_unlock(&task->proc_lock);
1440 iput(inode);
1443 static int pid_delete_dentry(struct dentry * dentry)
1445 /* Is the task we represent dead?
1446 * If so, then don't put the dentry on the lru list,
1447 * kill it immediately.
1449 return !pid_alive(proc_task(dentry->d_inode));
1452 static struct dentry_operations tid_fd_dentry_operations =
1454 .d_revalidate = tid_fd_revalidate,
1455 .d_delete = pid_delete_dentry,
1458 static struct dentry_operations pid_dentry_operations =
1460 .d_revalidate = pid_revalidate,
1461 .d_delete = pid_delete_dentry,
1464 static struct dentry_operations pid_base_dentry_operations =
1466 .d_revalidate = pid_revalidate,
1467 .d_iput = pid_base_iput,
1468 .d_delete = pid_delete_dentry,
1471 /* Lookups */
1473 static unsigned name_to_int(struct dentry *dentry)
1475 const char *name = dentry->d_name.name;
1476 int len = dentry->d_name.len;
1477 unsigned n = 0;
1479 if (len > 1 && *name == '0')
1480 goto out;
1481 while (len-- > 0) {
1482 unsigned c = *name++ - '0';
1483 if (c > 9)
1484 goto out;
1485 if (n >= (~0U-9)/10)
1486 goto out;
1487 n *= 10;
1488 n += c;
1490 return n;
1491 out:
1492 return ~0U;
1495 /* SMP-safe */
1496 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1498 struct task_struct *task = proc_task(dir);
1499 unsigned fd = name_to_int(dentry);
1500 struct file * file;
1501 struct files_struct * files;
1502 struct inode *inode;
1503 struct proc_inode *ei;
1505 if (fd == ~0U)
1506 goto out;
1507 if (!pid_alive(task))
1508 goto out;
1510 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1511 if (!inode)
1512 goto out;
1513 ei = PROC_I(inode);
1514 files = get_files_struct(task);
1515 if (!files)
1516 goto out_unlock;
1517 inode->i_mode = S_IFLNK;
1520 * We are not taking a ref to the file structure, so we must
1521 * hold ->file_lock.
1523 spin_lock(&files->file_lock);
1524 file = fcheck_files(files, fd);
1525 if (!file)
1526 goto out_unlock2;
1527 if (file->f_mode & 1)
1528 inode->i_mode |= S_IRUSR | S_IXUSR;
1529 if (file->f_mode & 2)
1530 inode->i_mode |= S_IWUSR | S_IXUSR;
1531 spin_unlock(&files->file_lock);
1532 put_files_struct(files);
1533 inode->i_op = &proc_pid_link_inode_operations;
1534 inode->i_size = 64;
1535 ei->op.proc_get_link = proc_fd_link;
1536 dentry->d_op = &tid_fd_dentry_operations;
1537 d_add(dentry, inode);
1538 return NULL;
1540 out_unlock2:
1541 spin_unlock(&files->file_lock);
1542 put_files_struct(files);
1543 out_unlock:
1544 iput(inode);
1545 out:
1546 return ERR_PTR(-ENOENT);
1549 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1550 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1552 static struct file_operations proc_fd_operations = {
1553 .read = generic_read_dir,
1554 .readdir = proc_readfd,
1557 static struct file_operations proc_task_operations = {
1558 .read = generic_read_dir,
1559 .readdir = proc_task_readdir,
1563 * proc directories can do almost nothing..
1565 static struct inode_operations proc_fd_inode_operations = {
1566 .lookup = proc_lookupfd,
1567 .permission = proc_permission,
1568 .setattr = proc_setattr,
1571 static struct inode_operations proc_task_inode_operations = {
1572 .lookup = proc_task_lookup,
1573 .permission = proc_task_permission,
1574 .setattr = proc_setattr,
1577 #ifdef CONFIG_SECURITY
1578 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1579 size_t count, loff_t *ppos)
1581 struct inode * inode = file->f_dentry->d_inode;
1582 unsigned long page;
1583 ssize_t length;
1584 struct task_struct *task = proc_task(inode);
1586 if (count > PAGE_SIZE)
1587 count = PAGE_SIZE;
1588 if (!(page = __get_free_page(GFP_KERNEL)))
1589 return -ENOMEM;
1591 length = security_getprocattr(task,
1592 (char*)file->f_dentry->d_name.name,
1593 (void*)page, count);
1594 if (length >= 0)
1595 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1596 free_page(page);
1597 return length;
1600 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1601 size_t count, loff_t *ppos)
1603 struct inode * inode = file->f_dentry->d_inode;
1604 char *page;
1605 ssize_t length;
1606 struct task_struct *task = proc_task(inode);
1608 if (count > PAGE_SIZE)
1609 count = PAGE_SIZE;
1610 if (*ppos != 0) {
1611 /* No partial writes. */
1612 return -EINVAL;
1614 page = (char*)__get_free_page(GFP_USER);
1615 if (!page)
1616 return -ENOMEM;
1617 length = -EFAULT;
1618 if (copy_from_user(page, buf, count))
1619 goto out;
1621 length = security_setprocattr(task,
1622 (char*)file->f_dentry->d_name.name,
1623 (void*)page, count);
1624 out:
1625 free_page((unsigned long) page);
1626 return length;
1629 static struct file_operations proc_pid_attr_operations = {
1630 .read = proc_pid_attr_read,
1631 .write = proc_pid_attr_write,
1634 static struct file_operations proc_tid_attr_operations;
1635 static struct inode_operations proc_tid_attr_inode_operations;
1636 static struct file_operations proc_tgid_attr_operations;
1637 static struct inode_operations proc_tgid_attr_inode_operations;
1638 #endif
1640 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1642 /* SMP-safe */
1643 static struct dentry *proc_pident_lookup(struct inode *dir,
1644 struct dentry *dentry,
1645 struct pid_entry *ents)
1647 struct inode *inode;
1648 int error;
1649 struct task_struct *task = proc_task(dir);
1650 struct pid_entry *p;
1651 struct proc_inode *ei;
1653 error = -ENOENT;
1654 inode = NULL;
1656 if (!pid_alive(task))
1657 goto out;
1659 for (p = ents; p->name; p++) {
1660 if (p->len != dentry->d_name.len)
1661 continue;
1662 if (!memcmp(dentry->d_name.name, p->name, p->len))
1663 break;
1665 if (!p->name)
1666 goto out;
1668 error = -EINVAL;
1669 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1670 if (!inode)
1671 goto out;
1673 ei = PROC_I(inode);
1674 inode->i_mode = p->mode;
1676 * Yes, it does not scale. And it should not. Don't add
1677 * new entries into /proc/<tgid>/ without very good reasons.
1679 switch(p->type) {
1680 case PROC_TGID_TASK:
1681 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1682 inode->i_op = &proc_task_inode_operations;
1683 inode->i_fop = &proc_task_operations;
1684 break;
1685 case PROC_TID_FD:
1686 case PROC_TGID_FD:
1687 inode->i_nlink = 2;
1688 inode->i_op = &proc_fd_inode_operations;
1689 inode->i_fop = &proc_fd_operations;
1690 break;
1691 case PROC_TID_EXE:
1692 case PROC_TGID_EXE:
1693 inode->i_op = &proc_pid_link_inode_operations;
1694 ei->op.proc_get_link = proc_exe_link;
1695 break;
1696 case PROC_TID_CWD:
1697 case PROC_TGID_CWD:
1698 inode->i_op = &proc_pid_link_inode_operations;
1699 ei->op.proc_get_link = proc_cwd_link;
1700 break;
1701 case PROC_TID_ROOT:
1702 case PROC_TGID_ROOT:
1703 inode->i_op = &proc_pid_link_inode_operations;
1704 ei->op.proc_get_link = proc_root_link;
1705 break;
1706 case PROC_TID_ENVIRON:
1707 case PROC_TGID_ENVIRON:
1708 inode->i_fop = &proc_info_file_operations;
1709 ei->op.proc_read = proc_pid_environ;
1710 break;
1711 case PROC_TID_AUXV:
1712 case PROC_TGID_AUXV:
1713 inode->i_fop = &proc_info_file_operations;
1714 ei->op.proc_read = proc_pid_auxv;
1715 break;
1716 case PROC_TID_STATUS:
1717 case PROC_TGID_STATUS:
1718 inode->i_fop = &proc_info_file_operations;
1719 ei->op.proc_read = proc_pid_status;
1720 break;
1721 case PROC_TID_STAT:
1722 inode->i_fop = &proc_info_file_operations;
1723 ei->op.proc_read = proc_tid_stat;
1724 break;
1725 case PROC_TGID_STAT:
1726 inode->i_fop = &proc_info_file_operations;
1727 ei->op.proc_read = proc_tgid_stat;
1728 break;
1729 case PROC_TID_CMDLINE:
1730 case PROC_TGID_CMDLINE:
1731 inode->i_fop = &proc_info_file_operations;
1732 ei->op.proc_read = proc_pid_cmdline;
1733 break;
1734 case PROC_TID_STATM:
1735 case PROC_TGID_STATM:
1736 inode->i_fop = &proc_info_file_operations;
1737 ei->op.proc_read = proc_pid_statm;
1738 break;
1739 case PROC_TID_MAPS:
1740 case PROC_TGID_MAPS:
1741 inode->i_fop = &proc_maps_operations;
1742 break;
1743 #ifdef CONFIG_NUMA
1744 case PROC_TID_NUMA_MAPS:
1745 case PROC_TGID_NUMA_MAPS:
1746 inode->i_fop = &proc_numa_maps_operations;
1747 break;
1748 #endif
1749 case PROC_TID_MEM:
1750 case PROC_TGID_MEM:
1751 inode->i_op = &proc_mem_inode_operations;
1752 inode->i_fop = &proc_mem_operations;
1753 break;
1754 #ifdef CONFIG_SECCOMP
1755 case PROC_TID_SECCOMP:
1756 case PROC_TGID_SECCOMP:
1757 inode->i_fop = &proc_seccomp_operations;
1758 break;
1759 #endif /* CONFIG_SECCOMP */
1760 case PROC_TID_MOUNTS:
1761 case PROC_TGID_MOUNTS:
1762 inode->i_fop = &proc_mounts_operations;
1763 break;
1764 #ifdef CONFIG_MMU
1765 case PROC_TID_SMAPS:
1766 case PROC_TGID_SMAPS:
1767 inode->i_fop = &proc_smaps_operations;
1768 break;
1769 #endif
1770 #ifdef CONFIG_SECURITY
1771 case PROC_TID_ATTR:
1772 inode->i_nlink = 2;
1773 inode->i_op = &proc_tid_attr_inode_operations;
1774 inode->i_fop = &proc_tid_attr_operations;
1775 break;
1776 case PROC_TGID_ATTR:
1777 inode->i_nlink = 2;
1778 inode->i_op = &proc_tgid_attr_inode_operations;
1779 inode->i_fop = &proc_tgid_attr_operations;
1780 break;
1781 case PROC_TID_ATTR_CURRENT:
1782 case PROC_TGID_ATTR_CURRENT:
1783 case PROC_TID_ATTR_PREV:
1784 case PROC_TGID_ATTR_PREV:
1785 case PROC_TID_ATTR_EXEC:
1786 case PROC_TGID_ATTR_EXEC:
1787 case PROC_TID_ATTR_FSCREATE:
1788 case PROC_TGID_ATTR_FSCREATE:
1789 inode->i_fop = &proc_pid_attr_operations;
1790 break;
1791 #endif
1792 #ifdef CONFIG_KALLSYMS
1793 case PROC_TID_WCHAN:
1794 case PROC_TGID_WCHAN:
1795 inode->i_fop = &proc_info_file_operations;
1796 ei->op.proc_read = proc_pid_wchan;
1797 break;
1798 #endif
1799 #ifdef CONFIG_SCHEDSTATS
1800 case PROC_TID_SCHEDSTAT:
1801 case PROC_TGID_SCHEDSTAT:
1802 inode->i_fop = &proc_info_file_operations;
1803 ei->op.proc_read = proc_pid_schedstat;
1804 break;
1805 #endif
1806 #ifdef CONFIG_CPUSETS
1807 case PROC_TID_CPUSET:
1808 case PROC_TGID_CPUSET:
1809 inode->i_fop = &proc_cpuset_operations;
1810 break;
1811 #endif
1812 case PROC_TID_OOM_SCORE:
1813 case PROC_TGID_OOM_SCORE:
1814 inode->i_fop = &proc_info_file_operations;
1815 ei->op.proc_read = proc_oom_score;
1816 break;
1817 case PROC_TID_OOM_ADJUST:
1818 case PROC_TGID_OOM_ADJUST:
1819 inode->i_fop = &proc_oom_adjust_operations;
1820 break;
1821 #ifdef CONFIG_AUDITSYSCALL
1822 case PROC_TID_LOGINUID:
1823 case PROC_TGID_LOGINUID:
1824 inode->i_fop = &proc_loginuid_operations;
1825 break;
1826 #endif
1827 default:
1828 printk("procfs: impossible type (%d)",p->type);
1829 iput(inode);
1830 return ERR_PTR(-EINVAL);
1832 dentry->d_op = &pid_dentry_operations;
1833 d_add(dentry, inode);
1834 return NULL;
1836 out:
1837 return ERR_PTR(error);
1840 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1841 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1844 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1845 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1848 static struct file_operations proc_tgid_base_operations = {
1849 .read = generic_read_dir,
1850 .readdir = proc_tgid_base_readdir,
1853 static struct file_operations proc_tid_base_operations = {
1854 .read = generic_read_dir,
1855 .readdir = proc_tid_base_readdir,
1858 static struct inode_operations proc_tgid_base_inode_operations = {
1859 .lookup = proc_tgid_base_lookup,
1860 .setattr = proc_setattr,
1863 static struct inode_operations proc_tid_base_inode_operations = {
1864 .lookup = proc_tid_base_lookup,
1865 .setattr = proc_setattr,
1868 #ifdef CONFIG_SECURITY
1869 static int proc_tgid_attr_readdir(struct file * filp,
1870 void * dirent, filldir_t filldir)
1872 return proc_pident_readdir(filp,dirent,filldir,
1873 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1876 static int proc_tid_attr_readdir(struct file * filp,
1877 void * dirent, filldir_t filldir)
1879 return proc_pident_readdir(filp,dirent,filldir,
1880 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1883 static struct file_operations proc_tgid_attr_operations = {
1884 .read = generic_read_dir,
1885 .readdir = proc_tgid_attr_readdir,
1888 static struct file_operations proc_tid_attr_operations = {
1889 .read = generic_read_dir,
1890 .readdir = proc_tid_attr_readdir,
1893 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1894 struct dentry *dentry, struct nameidata *nd)
1896 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1899 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1900 struct dentry *dentry, struct nameidata *nd)
1902 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1905 static struct inode_operations proc_tgid_attr_inode_operations = {
1906 .lookup = proc_tgid_attr_lookup,
1907 .setattr = proc_setattr,
1910 static struct inode_operations proc_tid_attr_inode_operations = {
1911 .lookup = proc_tid_attr_lookup,
1912 .setattr = proc_setattr,
1914 #endif
1917 * /proc/self:
1919 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1920 int buflen)
1922 char tmp[30];
1923 sprintf(tmp, "%d", current->tgid);
1924 return vfs_readlink(dentry,buffer,buflen,tmp);
1927 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1929 char tmp[30];
1930 sprintf(tmp, "%d", current->tgid);
1931 return ERR_PTR(vfs_follow_link(nd,tmp));
1934 static struct inode_operations proc_self_inode_operations = {
1935 .readlink = proc_self_readlink,
1936 .follow_link = proc_self_follow_link,
1937 .setattr = proc_setattr,
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;