[PATCH] use get_fs_struct() in proc
[linux-2.6/openmoko-kernel/knife-kernel.git] / fs / proc / base.c
blob73b1e94171b7bc6cc01de6335f31f523c62bb3f4
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/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/namespace.h>
63 #include <linux/mm.h>
64 #include <linux/smp_lock.h>
65 #include <linux/kallsyms.h>
66 #include <linux/mount.h>
67 #include <linux/security.h>
68 #include <linux/ptrace.h>
69 #include <linux/seccomp.h>
70 #include <linux/cpuset.h>
71 #include <linux/audit.h>
72 #include "internal.h"
75 * For hysterical raisins we keep the same inumbers as in the old procfs.
76 * Feel free to change the macro below - just keep the range distinct from
77 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
78 * As soon as we'll get a separate superblock we will be able to forget
79 * about magical ranges too.
82 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
84 enum pid_directory_inos {
85 PROC_TGID_INO = 2,
86 PROC_TGID_TASK,
87 PROC_TGID_STATUS,
88 PROC_TGID_MEM,
89 #ifdef CONFIG_SECCOMP
90 PROC_TGID_SECCOMP,
91 #endif
92 PROC_TGID_CWD,
93 PROC_TGID_ROOT,
94 PROC_TGID_EXE,
95 PROC_TGID_FD,
96 PROC_TGID_ENVIRON,
97 PROC_TGID_AUXV,
98 PROC_TGID_CMDLINE,
99 PROC_TGID_STAT,
100 PROC_TGID_STATM,
101 PROC_TGID_MAPS,
102 PROC_TGID_NUMA_MAPS,
103 PROC_TGID_MOUNTS,
104 PROC_TGID_WCHAN,
105 PROC_TGID_SMAPS,
106 #ifdef CONFIG_SCHEDSTATS
107 PROC_TGID_SCHEDSTAT,
108 #endif
109 #ifdef CONFIG_CPUSETS
110 PROC_TGID_CPUSET,
111 #endif
112 #ifdef CONFIG_SECURITY
113 PROC_TGID_ATTR,
114 PROC_TGID_ATTR_CURRENT,
115 PROC_TGID_ATTR_PREV,
116 PROC_TGID_ATTR_EXEC,
117 PROC_TGID_ATTR_FSCREATE,
118 #endif
119 #ifdef CONFIG_AUDITSYSCALL
120 PROC_TGID_LOGINUID,
121 #endif
122 PROC_TGID_FD_DIR,
123 PROC_TGID_OOM_SCORE,
124 PROC_TGID_OOM_ADJUST,
125 PROC_TID_INO,
126 PROC_TID_STATUS,
127 PROC_TID_MEM,
128 #ifdef CONFIG_SECCOMP
129 PROC_TID_SECCOMP,
130 #endif
131 PROC_TID_CWD,
132 PROC_TID_ROOT,
133 PROC_TID_EXE,
134 PROC_TID_FD,
135 PROC_TID_ENVIRON,
136 PROC_TID_AUXV,
137 PROC_TID_CMDLINE,
138 PROC_TID_STAT,
139 PROC_TID_STATM,
140 PROC_TID_MAPS,
141 PROC_TID_NUMA_MAPS,
142 PROC_TID_MOUNTS,
143 PROC_TID_WCHAN,
144 PROC_TID_SMAPS,
145 #ifdef CONFIG_SCHEDSTATS
146 PROC_TID_SCHEDSTAT,
147 #endif
148 #ifdef CONFIG_CPUSETS
149 PROC_TID_CPUSET,
150 #endif
151 #ifdef CONFIG_SECURITY
152 PROC_TID_ATTR,
153 PROC_TID_ATTR_CURRENT,
154 PROC_TID_ATTR_PREV,
155 PROC_TID_ATTR_EXEC,
156 PROC_TID_ATTR_FSCREATE,
157 #endif
158 #ifdef CONFIG_AUDITSYSCALL
159 PROC_TID_LOGINUID,
160 #endif
161 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
162 PROC_TID_OOM_SCORE,
163 PROC_TID_OOM_ADJUST,
166 struct pid_entry {
167 int type;
168 int len;
169 char *name;
170 mode_t mode;
173 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
175 static struct pid_entry tgid_base_stuff[] = {
176 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
177 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
178 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
179 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
180 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
181 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
182 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
183 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
184 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
185 #ifdef CONFIG_NUMA
186 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
187 #endif
188 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
189 #ifdef CONFIG_SECCOMP
190 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
191 #endif
192 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
193 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
194 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
195 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
196 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
197 #ifdef CONFIG_SECURITY
198 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
199 #endif
200 #ifdef CONFIG_KALLSYMS
201 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
202 #endif
203 #ifdef CONFIG_SCHEDSTATS
204 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
205 #endif
206 #ifdef CONFIG_CPUSETS
207 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
208 #endif
209 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
210 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
211 #ifdef CONFIG_AUDITSYSCALL
212 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
213 #endif
214 {0,0,NULL,0}
216 static struct pid_entry tid_base_stuff[] = {
217 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
218 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
219 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
220 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
221 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
222 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
223 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
224 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
225 #ifdef CONFIG_NUMA
226 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
227 #endif
228 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
229 #ifdef CONFIG_SECCOMP
230 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
231 #endif
232 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
233 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
234 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
235 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
236 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
237 #ifdef CONFIG_SECURITY
238 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
239 #endif
240 #ifdef CONFIG_KALLSYMS
241 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
242 #endif
243 #ifdef CONFIG_SCHEDSTATS
244 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
245 #endif
246 #ifdef CONFIG_CPUSETS
247 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
248 #endif
249 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
250 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
251 #ifdef CONFIG_AUDITSYSCALL
252 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
253 #endif
254 {0,0,NULL,0}
257 #ifdef CONFIG_SECURITY
258 static struct pid_entry tgid_attr_stuff[] = {
259 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
260 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
261 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
262 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
263 {0,0,NULL,0}
265 static struct pid_entry tid_attr_stuff[] = {
266 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
267 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
268 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
269 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
270 {0,0,NULL,0}
272 #endif
274 #undef E
276 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
278 struct task_struct *task = proc_task(inode);
279 struct files_struct *files;
280 struct file *file;
281 int fd = proc_type(inode) - PROC_TID_FD_DIR;
283 files = get_files_struct(task);
284 if (files) {
285 spin_lock(&files->file_lock);
286 file = fcheck_files(files, fd);
287 if (file) {
288 *mnt = mntget(file->f_vfsmnt);
289 *dentry = dget(file->f_dentry);
290 spin_unlock(&files->file_lock);
291 put_files_struct(files);
292 return 0;
294 spin_unlock(&files->file_lock);
295 put_files_struct(files);
297 return -ENOENT;
300 static struct fs_struct *get_fs_struct(struct task_struct *task)
302 struct fs_struct *fs;
303 task_lock(task);
304 fs = task->fs;
305 if(fs)
306 atomic_inc(&fs->count);
307 task_unlock(task);
308 return fs;
311 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
313 struct fs_struct *fs = get_fs_struct(proc_task(inode));
314 int result = -ENOENT;
315 if (fs) {
316 read_lock(&fs->lock);
317 *mnt = mntget(fs->pwdmnt);
318 *dentry = dget(fs->pwd);
319 read_unlock(&fs->lock);
320 result = 0;
321 put_fs_struct(fs);
323 return result;
326 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
328 struct fs_struct *fs = get_fs_struct(proc_task(inode));
329 int result = -ENOENT;
330 if (fs) {
331 read_lock(&fs->lock);
332 *mnt = mntget(fs->rootmnt);
333 *dentry = dget(fs->root);
334 read_unlock(&fs->lock);
335 result = 0;
336 put_fs_struct(fs);
338 return result;
341 #define MAY_PTRACE(task) \
342 (task == current || \
343 (task->parent == current && \
344 (task->ptrace & PT_PTRACED) && \
345 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
346 security_ptrace(current,task) == 0))
348 static int may_ptrace_attach(struct task_struct *task)
350 int retval = 0;
352 task_lock(task);
354 if (!task->mm)
355 goto out;
356 if (((current->uid != task->euid) ||
357 (current->uid != task->suid) ||
358 (current->uid != task->uid) ||
359 (current->gid != task->egid) ||
360 (current->gid != task->sgid) ||
361 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
362 goto out;
363 rmb();
364 if (task->mm->dumpable != 1 && !capable(CAP_SYS_PTRACE))
365 goto out;
366 if (security_ptrace(current, task))
367 goto out;
369 retval = 1;
370 out:
371 task_unlock(task);
372 return retval;
375 static int proc_pid_environ(struct task_struct *task, char * buffer)
377 int res = 0;
378 struct mm_struct *mm = get_task_mm(task);
379 if (mm) {
380 unsigned int len = mm->env_end - mm->env_start;
381 if (len > PAGE_SIZE)
382 len = PAGE_SIZE;
383 res = access_process_vm(task, mm->env_start, buffer, len, 0);
384 if (!may_ptrace_attach(task))
385 res = -ESRCH;
386 mmput(mm);
388 return res;
391 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
393 int res = 0;
394 unsigned int len;
395 struct mm_struct *mm = get_task_mm(task);
396 if (!mm)
397 goto out;
398 if (!mm->arg_end)
399 goto out_mm; /* Shh! No looking before we're done */
401 len = mm->arg_end - mm->arg_start;
403 if (len > PAGE_SIZE)
404 len = PAGE_SIZE;
406 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
408 // If the nul at the end of args has been overwritten, then
409 // assume application is using setproctitle(3).
410 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
411 len = strnlen(buffer, res);
412 if (len < res) {
413 res = len;
414 } else {
415 len = mm->env_end - mm->env_start;
416 if (len > PAGE_SIZE - res)
417 len = PAGE_SIZE - res;
418 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
419 res = strnlen(buffer, res);
422 out_mm:
423 mmput(mm);
424 out:
425 return res;
428 static int proc_pid_auxv(struct task_struct *task, char *buffer)
430 int res = 0;
431 struct mm_struct *mm = get_task_mm(task);
432 if (mm) {
433 unsigned int nwords = 0;
435 nwords += 2;
436 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
437 res = nwords * sizeof(mm->saved_auxv[0]);
438 if (res > PAGE_SIZE)
439 res = PAGE_SIZE;
440 memcpy(buffer, mm->saved_auxv, res);
441 mmput(mm);
443 return res;
447 #ifdef CONFIG_KALLSYMS
449 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
450 * Returns the resolved symbol. If that fails, simply return the address.
452 static int proc_pid_wchan(struct task_struct *task, char *buffer)
454 char *modname;
455 const char *sym_name;
456 unsigned long wchan, size, offset;
457 char namebuf[KSYM_NAME_LEN+1];
459 wchan = get_wchan(task);
461 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
462 if (sym_name)
463 return sprintf(buffer, "%s", sym_name);
464 return sprintf(buffer, "%lu", wchan);
466 #endif /* CONFIG_KALLSYMS */
468 #ifdef CONFIG_SCHEDSTATS
470 * Provides /proc/PID/schedstat
472 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
474 return sprintf(buffer, "%lu %lu %lu\n",
475 task->sched_info.cpu_time,
476 task->sched_info.run_delay,
477 task->sched_info.pcnt);
479 #endif
481 /* The badness from the OOM killer */
482 unsigned long badness(struct task_struct *p, unsigned long uptime);
483 static int proc_oom_score(struct task_struct *task, char *buffer)
485 unsigned long points;
486 struct timespec uptime;
488 do_posix_clock_monotonic_gettime(&uptime);
489 points = badness(task, uptime.tv_sec);
490 return sprintf(buffer, "%lu\n", points);
493 /************************************************************************/
494 /* Here the fs part begins */
495 /************************************************************************/
497 /* permission checks */
499 static int proc_check_root(struct inode *inode)
501 struct dentry *de, *base, *root;
502 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
503 int res = 0;
505 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
506 return -ENOENT;
507 read_lock(&current->fs->lock);
508 our_vfsmnt = mntget(current->fs->rootmnt);
509 base = dget(current->fs->root);
510 read_unlock(&current->fs->lock);
512 spin_lock(&vfsmount_lock);
513 de = root;
514 mnt = vfsmnt;
516 while (vfsmnt != our_vfsmnt) {
517 if (vfsmnt == vfsmnt->mnt_parent)
518 goto out;
519 de = vfsmnt->mnt_mountpoint;
520 vfsmnt = vfsmnt->mnt_parent;
523 if (!is_subdir(de, base))
524 goto out;
525 spin_unlock(&vfsmount_lock);
527 exit:
528 dput(base);
529 mntput(our_vfsmnt);
530 dput(root);
531 mntput(mnt);
532 return res;
533 out:
534 spin_unlock(&vfsmount_lock);
535 res = -EACCES;
536 goto exit;
539 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
541 if (generic_permission(inode, mask, NULL) != 0)
542 return -EACCES;
543 return proc_check_root(inode);
546 extern struct seq_operations proc_pid_maps_op;
547 static int maps_open(struct inode *inode, struct file *file)
549 struct task_struct *task = proc_task(inode);
550 int ret = seq_open(file, &proc_pid_maps_op);
551 if (!ret) {
552 struct seq_file *m = file->private_data;
553 m->private = task;
555 return ret;
558 static struct file_operations proc_maps_operations = {
559 .open = maps_open,
560 .read = seq_read,
561 .llseek = seq_lseek,
562 .release = seq_release,
565 #ifdef CONFIG_NUMA
566 extern struct seq_operations proc_pid_numa_maps_op;
567 static int numa_maps_open(struct inode *inode, struct file *file)
569 struct task_struct *task = proc_task(inode);
570 int ret = seq_open(file, &proc_pid_numa_maps_op);
571 if (!ret) {
572 struct seq_file *m = file->private_data;
573 m->private = task;
575 return ret;
578 static struct file_operations proc_numa_maps_operations = {
579 .open = numa_maps_open,
580 .read = seq_read,
581 .llseek = seq_lseek,
582 .release = seq_release,
584 #endif
586 extern struct seq_operations proc_pid_smaps_op;
587 static int smaps_open(struct inode *inode, struct file *file)
589 struct task_struct *task = proc_task(inode);
590 int ret = seq_open(file, &proc_pid_smaps_op);
591 if (!ret) {
592 struct seq_file *m = file->private_data;
593 m->private = task;
595 return ret;
598 static struct file_operations proc_smaps_operations = {
599 .open = smaps_open,
600 .read = seq_read,
601 .llseek = seq_lseek,
602 .release = seq_release,
605 extern struct seq_operations mounts_op;
606 static int mounts_open(struct inode *inode, struct file *file)
608 struct task_struct *task = proc_task(inode);
609 int ret = seq_open(file, &mounts_op);
611 if (!ret) {
612 struct seq_file *m = file->private_data;
613 struct namespace *namespace;
614 task_lock(task);
615 namespace = task->namespace;
616 if (namespace)
617 get_namespace(namespace);
618 task_unlock(task);
620 if (namespace)
621 m->private = namespace;
622 else {
623 seq_release(inode, file);
624 ret = -EINVAL;
627 return ret;
630 static int mounts_release(struct inode *inode, struct file *file)
632 struct seq_file *m = file->private_data;
633 struct namespace *namespace = m->private;
634 put_namespace(namespace);
635 return seq_release(inode, file);
638 static struct file_operations proc_mounts_operations = {
639 .open = mounts_open,
640 .read = seq_read,
641 .llseek = seq_lseek,
642 .release = mounts_release,
645 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
647 static ssize_t proc_info_read(struct file * file, char __user * buf,
648 size_t count, loff_t *ppos)
650 struct inode * inode = file->f_dentry->d_inode;
651 unsigned long page;
652 ssize_t length;
653 struct task_struct *task = proc_task(inode);
655 if (count > PROC_BLOCK_SIZE)
656 count = PROC_BLOCK_SIZE;
657 if (!(page = __get_free_page(GFP_KERNEL)))
658 return -ENOMEM;
660 length = PROC_I(inode)->op.proc_read(task, (char*)page);
662 if (length >= 0)
663 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
664 free_page(page);
665 return length;
668 static struct file_operations proc_info_file_operations = {
669 .read = proc_info_read,
672 static int mem_open(struct inode* inode, struct file* file)
674 file->private_data = (void*)((long)current->self_exec_id);
675 return 0;
678 static ssize_t mem_read(struct file * file, char __user * buf,
679 size_t count, loff_t *ppos)
681 struct task_struct *task = proc_task(file->f_dentry->d_inode);
682 char *page;
683 unsigned long src = *ppos;
684 int ret = -ESRCH;
685 struct mm_struct *mm;
687 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
688 goto out;
690 ret = -ENOMEM;
691 page = (char *)__get_free_page(GFP_USER);
692 if (!page)
693 goto out;
695 ret = 0;
697 mm = get_task_mm(task);
698 if (!mm)
699 goto out_free;
701 ret = -EIO;
703 if (file->private_data != (void*)((long)current->self_exec_id))
704 goto out_put;
706 ret = 0;
708 while (count > 0) {
709 int this_len, retval;
711 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
712 retval = access_process_vm(task, src, page, this_len, 0);
713 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
714 if (!ret)
715 ret = -EIO;
716 break;
719 if (copy_to_user(buf, page, retval)) {
720 ret = -EFAULT;
721 break;
724 ret += retval;
725 src += retval;
726 buf += retval;
727 count -= retval;
729 *ppos = src;
731 out_put:
732 mmput(mm);
733 out_free:
734 free_page((unsigned long) page);
735 out:
736 return ret;
739 #define mem_write NULL
741 #ifndef mem_write
742 /* This is a security hazard */
743 static ssize_t mem_write(struct file * file, const char * buf,
744 size_t count, loff_t *ppos)
746 int copied = 0;
747 char *page;
748 struct task_struct *task = proc_task(file->f_dentry->d_inode);
749 unsigned long dst = *ppos;
751 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
752 return -ESRCH;
754 page = (char *)__get_free_page(GFP_USER);
755 if (!page)
756 return -ENOMEM;
758 while (count > 0) {
759 int this_len, retval;
761 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
762 if (copy_from_user(page, buf, this_len)) {
763 copied = -EFAULT;
764 break;
766 retval = access_process_vm(task, dst, page, this_len, 1);
767 if (!retval) {
768 if (!copied)
769 copied = -EIO;
770 break;
772 copied += retval;
773 buf += retval;
774 dst += retval;
775 count -= retval;
777 *ppos = dst;
778 free_page((unsigned long) page);
779 return copied;
781 #endif
783 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
785 switch (orig) {
786 case 0:
787 file->f_pos = offset;
788 break;
789 case 1:
790 file->f_pos += offset;
791 break;
792 default:
793 return -EINVAL;
795 force_successful_syscall_return();
796 return file->f_pos;
799 static struct file_operations proc_mem_operations = {
800 .llseek = mem_lseek,
801 .read = mem_read,
802 .write = mem_write,
803 .open = mem_open,
806 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
807 size_t count, loff_t *ppos)
809 struct task_struct *task = proc_task(file->f_dentry->d_inode);
810 char buffer[8];
811 size_t len;
812 int oom_adjust = task->oomkilladj;
813 loff_t __ppos = *ppos;
815 len = sprintf(buffer, "%i\n", oom_adjust);
816 if (__ppos >= len)
817 return 0;
818 if (count > len-__ppos)
819 count = len-__ppos;
820 if (copy_to_user(buf, buffer + __ppos, count))
821 return -EFAULT;
822 *ppos = __ppos + count;
823 return count;
826 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
827 size_t count, loff_t *ppos)
829 struct task_struct *task = proc_task(file->f_dentry->d_inode);
830 char buffer[8], *end;
831 int oom_adjust;
833 if (!capable(CAP_SYS_RESOURCE))
834 return -EPERM;
835 memset(buffer, 0, 8);
836 if (count > 6)
837 count = 6;
838 if (copy_from_user(buffer, buf, count))
839 return -EFAULT;
840 oom_adjust = simple_strtol(buffer, &end, 0);
841 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
842 return -EINVAL;
843 if (*end == '\n')
844 end++;
845 task->oomkilladj = oom_adjust;
846 if (end - buffer == 0)
847 return -EIO;
848 return end - buffer;
851 static struct file_operations proc_oom_adjust_operations = {
852 .read = oom_adjust_read,
853 .write = oom_adjust_write,
856 static struct inode_operations proc_mem_inode_operations = {
857 .permission = proc_permission,
860 #ifdef CONFIG_AUDITSYSCALL
861 #define TMPBUFLEN 21
862 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
863 size_t count, loff_t *ppos)
865 struct inode * inode = file->f_dentry->d_inode;
866 struct task_struct *task = proc_task(inode);
867 ssize_t length;
868 char tmpbuf[TMPBUFLEN];
870 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
871 audit_get_loginuid(task->audit_context));
872 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
875 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
876 size_t count, loff_t *ppos)
878 struct inode * inode = file->f_dentry->d_inode;
879 char *page, *tmp;
880 ssize_t length;
881 struct task_struct *task = proc_task(inode);
882 uid_t loginuid;
884 if (!capable(CAP_AUDIT_CONTROL))
885 return -EPERM;
887 if (current != task)
888 return -EPERM;
890 if (count > PAGE_SIZE)
891 count = PAGE_SIZE;
893 if (*ppos != 0) {
894 /* No partial writes. */
895 return -EINVAL;
897 page = (char*)__get_free_page(GFP_USER);
898 if (!page)
899 return -ENOMEM;
900 length = -EFAULT;
901 if (copy_from_user(page, buf, count))
902 goto out_free_page;
904 loginuid = simple_strtoul(page, &tmp, 10);
905 if (tmp == page) {
906 length = -EINVAL;
907 goto out_free_page;
910 length = audit_set_loginuid(task, loginuid);
911 if (likely(length == 0))
912 length = count;
914 out_free_page:
915 free_page((unsigned long) page);
916 return length;
919 static struct file_operations proc_loginuid_operations = {
920 .read = proc_loginuid_read,
921 .write = proc_loginuid_write,
923 #endif
925 #ifdef CONFIG_SECCOMP
926 static ssize_t seccomp_read(struct file *file, char __user *buf,
927 size_t count, loff_t *ppos)
929 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
930 char __buf[20];
931 loff_t __ppos = *ppos;
932 size_t len;
934 /* no need to print the trailing zero, so use only len */
935 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
936 if (__ppos >= len)
937 return 0;
938 if (count > len - __ppos)
939 count = len - __ppos;
940 if (copy_to_user(buf, __buf + __ppos, count))
941 return -EFAULT;
942 *ppos = __ppos + count;
943 return count;
946 static ssize_t seccomp_write(struct file *file, const char __user *buf,
947 size_t count, loff_t *ppos)
949 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
950 char __buf[20], *end;
951 unsigned int seccomp_mode;
953 /* can set it only once to be even more secure */
954 if (unlikely(tsk->seccomp.mode))
955 return -EPERM;
957 memset(__buf, 0, sizeof(__buf));
958 count = min(count, sizeof(__buf) - 1);
959 if (copy_from_user(__buf, buf, count))
960 return -EFAULT;
961 seccomp_mode = simple_strtoul(__buf, &end, 0);
962 if (*end == '\n')
963 end++;
964 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
965 tsk->seccomp.mode = seccomp_mode;
966 set_tsk_thread_flag(tsk, TIF_SECCOMP);
967 } else
968 return -EINVAL;
969 if (unlikely(!(end - __buf)))
970 return -EIO;
971 return end - __buf;
974 static struct file_operations proc_seccomp_operations = {
975 .read = seccomp_read,
976 .write = seccomp_write,
978 #endif /* CONFIG_SECCOMP */
980 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
982 struct inode *inode = dentry->d_inode;
983 int error = -EACCES;
985 /* We don't need a base pointer in the /proc filesystem */
986 path_release(nd);
988 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
989 goto out;
990 error = proc_check_root(inode);
991 if (error)
992 goto out;
994 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
995 nd->last_type = LAST_BIND;
996 out:
997 return ERR_PTR(error);
1000 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1001 char __user *buffer, int buflen)
1003 struct inode * inode;
1004 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1005 int len;
1007 if (!tmp)
1008 return -ENOMEM;
1010 inode = dentry->d_inode;
1011 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1012 len = PTR_ERR(path);
1013 if (IS_ERR(path))
1014 goto out;
1015 len = tmp + PAGE_SIZE - 1 - path;
1017 if (len > buflen)
1018 len = buflen;
1019 if (copy_to_user(buffer, path, len))
1020 len = -EFAULT;
1021 out:
1022 free_page((unsigned long)tmp);
1023 return len;
1026 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1028 int error = -EACCES;
1029 struct inode *inode = dentry->d_inode;
1030 struct dentry *de;
1031 struct vfsmount *mnt = NULL;
1033 lock_kernel();
1035 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1036 goto out;
1037 error = proc_check_root(inode);
1038 if (error)
1039 goto out;
1041 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1042 if (error)
1043 goto out;
1045 error = do_proc_readlink(de, mnt, buffer, buflen);
1046 dput(de);
1047 mntput(mnt);
1048 out:
1049 unlock_kernel();
1050 return error;
1053 static struct inode_operations proc_pid_link_inode_operations = {
1054 .readlink = proc_pid_readlink,
1055 .follow_link = proc_pid_follow_link
1058 #define NUMBUF 10
1060 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1062 struct inode *inode = filp->f_dentry->d_inode;
1063 struct task_struct *p = proc_task(inode);
1064 unsigned int fd, tid, ino;
1065 int retval;
1066 char buf[NUMBUF];
1067 struct files_struct * files;
1069 retval = -ENOENT;
1070 if (!pid_alive(p))
1071 goto out;
1072 retval = 0;
1073 tid = p->pid;
1075 fd = filp->f_pos;
1076 switch (fd) {
1077 case 0:
1078 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1079 goto out;
1080 filp->f_pos++;
1081 case 1:
1082 ino = fake_ino(tid, PROC_TID_INO);
1083 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1084 goto out;
1085 filp->f_pos++;
1086 default:
1087 files = get_files_struct(p);
1088 if (!files)
1089 goto out;
1090 spin_lock(&files->file_lock);
1091 for (fd = filp->f_pos-2;
1092 fd < files->max_fds;
1093 fd++, filp->f_pos++) {
1094 unsigned int i,j;
1096 if (!fcheck_files(files, fd))
1097 continue;
1098 spin_unlock(&files->file_lock);
1100 j = NUMBUF;
1101 i = fd;
1102 do {
1103 j--;
1104 buf[j] = '0' + (i % 10);
1105 i /= 10;
1106 } while (i);
1108 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1109 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1110 spin_lock(&files->file_lock);
1111 break;
1113 spin_lock(&files->file_lock);
1115 spin_unlock(&files->file_lock);
1116 put_files_struct(files);
1118 out:
1119 return retval;
1122 static int proc_pident_readdir(struct file *filp,
1123 void *dirent, filldir_t filldir,
1124 struct pid_entry *ents, unsigned int nents)
1126 int i;
1127 int pid;
1128 struct dentry *dentry = filp->f_dentry;
1129 struct inode *inode = dentry->d_inode;
1130 struct pid_entry *p;
1131 ino_t ino;
1132 int ret;
1134 ret = -ENOENT;
1135 if (!pid_alive(proc_task(inode)))
1136 goto out;
1138 ret = 0;
1139 pid = proc_task(inode)->pid;
1140 i = filp->f_pos;
1141 switch (i) {
1142 case 0:
1143 ino = inode->i_ino;
1144 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1145 goto out;
1146 i++;
1147 filp->f_pos++;
1148 /* fall through */
1149 case 1:
1150 ino = parent_ino(dentry);
1151 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1152 goto out;
1153 i++;
1154 filp->f_pos++;
1155 /* fall through */
1156 default:
1157 i -= 2;
1158 if (i >= nents) {
1159 ret = 1;
1160 goto out;
1162 p = ents + i;
1163 while (p->name) {
1164 if (filldir(dirent, p->name, p->len, filp->f_pos,
1165 fake_ino(pid, p->type), p->mode >> 12) < 0)
1166 goto out;
1167 filp->f_pos++;
1168 p++;
1172 ret = 1;
1173 out:
1174 return ret;
1177 static int proc_tgid_base_readdir(struct file * filp,
1178 void * dirent, filldir_t filldir)
1180 return proc_pident_readdir(filp,dirent,filldir,
1181 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1184 static int proc_tid_base_readdir(struct file * filp,
1185 void * dirent, filldir_t filldir)
1187 return proc_pident_readdir(filp,dirent,filldir,
1188 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1191 /* building an inode */
1193 static int task_dumpable(struct task_struct *task)
1195 int dumpable = 0;
1196 struct mm_struct *mm;
1198 task_lock(task);
1199 mm = task->mm;
1200 if (mm)
1201 dumpable = mm->dumpable;
1202 task_unlock(task);
1203 if(dumpable == 1)
1204 return 1;
1205 return 0;
1209 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1211 struct inode * inode;
1212 struct proc_inode *ei;
1214 /* We need a new inode */
1216 inode = new_inode(sb);
1217 if (!inode)
1218 goto out;
1220 /* Common stuff */
1221 ei = PROC_I(inode);
1222 ei->task = NULL;
1223 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1224 inode->i_ino = fake_ino(task->pid, ino);
1226 if (!pid_alive(task))
1227 goto out_unlock;
1230 * grab the reference to task.
1232 get_task_struct(task);
1233 ei->task = task;
1234 ei->type = ino;
1235 inode->i_uid = 0;
1236 inode->i_gid = 0;
1237 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1238 inode->i_uid = task->euid;
1239 inode->i_gid = task->egid;
1241 security_task_to_inode(task, inode);
1243 out:
1244 return inode;
1246 out_unlock:
1247 ei->pde = NULL;
1248 iput(inode);
1249 return NULL;
1252 /* dentry stuff */
1255 * Exceptional case: normally we are not allowed to unhash a busy
1256 * directory. In this case, however, we can do it - no aliasing problems
1257 * due to the way we treat inodes.
1259 * Rewrite the inode's ownerships here because the owning task may have
1260 * performed a setuid(), etc.
1262 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1264 struct inode *inode = dentry->d_inode;
1265 struct task_struct *task = proc_task(inode);
1266 if (pid_alive(task)) {
1267 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1268 inode->i_uid = task->euid;
1269 inode->i_gid = task->egid;
1270 } else {
1271 inode->i_uid = 0;
1272 inode->i_gid = 0;
1274 security_task_to_inode(task, inode);
1275 return 1;
1277 d_drop(dentry);
1278 return 0;
1281 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1283 struct inode *inode = dentry->d_inode;
1284 struct task_struct *task = proc_task(inode);
1285 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1286 struct files_struct *files;
1288 files = get_files_struct(task);
1289 if (files) {
1290 spin_lock(&files->file_lock);
1291 if (fcheck_files(files, fd)) {
1292 spin_unlock(&files->file_lock);
1293 put_files_struct(files);
1294 if (task_dumpable(task)) {
1295 inode->i_uid = task->euid;
1296 inode->i_gid = task->egid;
1297 } else {
1298 inode->i_uid = 0;
1299 inode->i_gid = 0;
1301 security_task_to_inode(task, inode);
1302 return 1;
1304 spin_unlock(&files->file_lock);
1305 put_files_struct(files);
1307 d_drop(dentry);
1308 return 0;
1311 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1313 struct task_struct *task = proc_task(inode);
1314 spin_lock(&task->proc_lock);
1315 if (task->proc_dentry == dentry)
1316 task->proc_dentry = NULL;
1317 spin_unlock(&task->proc_lock);
1318 iput(inode);
1321 static int pid_delete_dentry(struct dentry * dentry)
1323 /* Is the task we represent dead?
1324 * If so, then don't put the dentry on the lru list,
1325 * kill it immediately.
1327 return !pid_alive(proc_task(dentry->d_inode));
1330 static struct dentry_operations tid_fd_dentry_operations =
1332 .d_revalidate = tid_fd_revalidate,
1333 .d_delete = pid_delete_dentry,
1336 static struct dentry_operations pid_dentry_operations =
1338 .d_revalidate = pid_revalidate,
1339 .d_delete = pid_delete_dentry,
1342 static struct dentry_operations pid_base_dentry_operations =
1344 .d_revalidate = pid_revalidate,
1345 .d_iput = pid_base_iput,
1346 .d_delete = pid_delete_dentry,
1349 /* Lookups */
1351 static unsigned name_to_int(struct dentry *dentry)
1353 const char *name = dentry->d_name.name;
1354 int len = dentry->d_name.len;
1355 unsigned n = 0;
1357 if (len > 1 && *name == '0')
1358 goto out;
1359 while (len-- > 0) {
1360 unsigned c = *name++ - '0';
1361 if (c > 9)
1362 goto out;
1363 if (n >= (~0U-9)/10)
1364 goto out;
1365 n *= 10;
1366 n += c;
1368 return n;
1369 out:
1370 return ~0U;
1373 /* SMP-safe */
1374 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1376 struct task_struct *task = proc_task(dir);
1377 unsigned fd = name_to_int(dentry);
1378 struct file * file;
1379 struct files_struct * files;
1380 struct inode *inode;
1381 struct proc_inode *ei;
1383 if (fd == ~0U)
1384 goto out;
1385 if (!pid_alive(task))
1386 goto out;
1388 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1389 if (!inode)
1390 goto out;
1391 ei = PROC_I(inode);
1392 files = get_files_struct(task);
1393 if (!files)
1394 goto out_unlock;
1395 inode->i_mode = S_IFLNK;
1396 spin_lock(&files->file_lock);
1397 file = fcheck_files(files, fd);
1398 if (!file)
1399 goto out_unlock2;
1400 if (file->f_mode & 1)
1401 inode->i_mode |= S_IRUSR | S_IXUSR;
1402 if (file->f_mode & 2)
1403 inode->i_mode |= S_IWUSR | S_IXUSR;
1404 spin_unlock(&files->file_lock);
1405 put_files_struct(files);
1406 inode->i_op = &proc_pid_link_inode_operations;
1407 inode->i_size = 64;
1408 ei->op.proc_get_link = proc_fd_link;
1409 dentry->d_op = &tid_fd_dentry_operations;
1410 d_add(dentry, inode);
1411 return NULL;
1413 out_unlock2:
1414 spin_unlock(&files->file_lock);
1415 put_files_struct(files);
1416 out_unlock:
1417 iput(inode);
1418 out:
1419 return ERR_PTR(-ENOENT);
1422 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1423 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1425 static struct file_operations proc_fd_operations = {
1426 .read = generic_read_dir,
1427 .readdir = proc_readfd,
1430 static struct file_operations proc_task_operations = {
1431 .read = generic_read_dir,
1432 .readdir = proc_task_readdir,
1436 * proc directories can do almost nothing..
1438 static struct inode_operations proc_fd_inode_operations = {
1439 .lookup = proc_lookupfd,
1440 .permission = proc_permission,
1443 static struct inode_operations proc_task_inode_operations = {
1444 .lookup = proc_task_lookup,
1445 .permission = proc_permission,
1448 #ifdef CONFIG_SECURITY
1449 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1450 size_t count, loff_t *ppos)
1452 struct inode * inode = file->f_dentry->d_inode;
1453 unsigned long page;
1454 ssize_t length;
1455 struct task_struct *task = proc_task(inode);
1457 if (count > PAGE_SIZE)
1458 count = PAGE_SIZE;
1459 if (!(page = __get_free_page(GFP_KERNEL)))
1460 return -ENOMEM;
1462 length = security_getprocattr(task,
1463 (char*)file->f_dentry->d_name.name,
1464 (void*)page, count);
1465 if (length >= 0)
1466 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1467 free_page(page);
1468 return length;
1471 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1472 size_t count, loff_t *ppos)
1474 struct inode * inode = file->f_dentry->d_inode;
1475 char *page;
1476 ssize_t length;
1477 struct task_struct *task = proc_task(inode);
1479 if (count > PAGE_SIZE)
1480 count = PAGE_SIZE;
1481 if (*ppos != 0) {
1482 /* No partial writes. */
1483 return -EINVAL;
1485 page = (char*)__get_free_page(GFP_USER);
1486 if (!page)
1487 return -ENOMEM;
1488 length = -EFAULT;
1489 if (copy_from_user(page, buf, count))
1490 goto out;
1492 length = security_setprocattr(task,
1493 (char*)file->f_dentry->d_name.name,
1494 (void*)page, count);
1495 out:
1496 free_page((unsigned long) page);
1497 return length;
1500 static struct file_operations proc_pid_attr_operations = {
1501 .read = proc_pid_attr_read,
1502 .write = proc_pid_attr_write,
1505 static struct file_operations proc_tid_attr_operations;
1506 static struct inode_operations proc_tid_attr_inode_operations;
1507 static struct file_operations proc_tgid_attr_operations;
1508 static struct inode_operations proc_tgid_attr_inode_operations;
1509 #endif
1511 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1513 /* SMP-safe */
1514 static struct dentry *proc_pident_lookup(struct inode *dir,
1515 struct dentry *dentry,
1516 struct pid_entry *ents)
1518 struct inode *inode;
1519 int error;
1520 struct task_struct *task = proc_task(dir);
1521 struct pid_entry *p;
1522 struct proc_inode *ei;
1524 error = -ENOENT;
1525 inode = NULL;
1527 if (!pid_alive(task))
1528 goto out;
1530 for (p = ents; p->name; p++) {
1531 if (p->len != dentry->d_name.len)
1532 continue;
1533 if (!memcmp(dentry->d_name.name, p->name, p->len))
1534 break;
1536 if (!p->name)
1537 goto out;
1539 error = -EINVAL;
1540 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1541 if (!inode)
1542 goto out;
1544 ei = PROC_I(inode);
1545 inode->i_mode = p->mode;
1547 * Yes, it does not scale. And it should not. Don't add
1548 * new entries into /proc/<tgid>/ without very good reasons.
1550 switch(p->type) {
1551 case PROC_TGID_TASK:
1552 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1553 inode->i_op = &proc_task_inode_operations;
1554 inode->i_fop = &proc_task_operations;
1555 break;
1556 case PROC_TID_FD:
1557 case PROC_TGID_FD:
1558 inode->i_nlink = 2;
1559 inode->i_op = &proc_fd_inode_operations;
1560 inode->i_fop = &proc_fd_operations;
1561 break;
1562 case PROC_TID_EXE:
1563 case PROC_TGID_EXE:
1564 inode->i_op = &proc_pid_link_inode_operations;
1565 ei->op.proc_get_link = proc_exe_link;
1566 break;
1567 case PROC_TID_CWD:
1568 case PROC_TGID_CWD:
1569 inode->i_op = &proc_pid_link_inode_operations;
1570 ei->op.proc_get_link = proc_cwd_link;
1571 break;
1572 case PROC_TID_ROOT:
1573 case PROC_TGID_ROOT:
1574 inode->i_op = &proc_pid_link_inode_operations;
1575 ei->op.proc_get_link = proc_root_link;
1576 break;
1577 case PROC_TID_ENVIRON:
1578 case PROC_TGID_ENVIRON:
1579 inode->i_fop = &proc_info_file_operations;
1580 ei->op.proc_read = proc_pid_environ;
1581 break;
1582 case PROC_TID_AUXV:
1583 case PROC_TGID_AUXV:
1584 inode->i_fop = &proc_info_file_operations;
1585 ei->op.proc_read = proc_pid_auxv;
1586 break;
1587 case PROC_TID_STATUS:
1588 case PROC_TGID_STATUS:
1589 inode->i_fop = &proc_info_file_operations;
1590 ei->op.proc_read = proc_pid_status;
1591 break;
1592 case PROC_TID_STAT:
1593 inode->i_fop = &proc_info_file_operations;
1594 ei->op.proc_read = proc_tid_stat;
1595 break;
1596 case PROC_TGID_STAT:
1597 inode->i_fop = &proc_info_file_operations;
1598 ei->op.proc_read = proc_tgid_stat;
1599 break;
1600 case PROC_TID_CMDLINE:
1601 case PROC_TGID_CMDLINE:
1602 inode->i_fop = &proc_info_file_operations;
1603 ei->op.proc_read = proc_pid_cmdline;
1604 break;
1605 case PROC_TID_STATM:
1606 case PROC_TGID_STATM:
1607 inode->i_fop = &proc_info_file_operations;
1608 ei->op.proc_read = proc_pid_statm;
1609 break;
1610 case PROC_TID_MAPS:
1611 case PROC_TGID_MAPS:
1612 inode->i_fop = &proc_maps_operations;
1613 break;
1614 #ifdef CONFIG_NUMA
1615 case PROC_TID_NUMA_MAPS:
1616 case PROC_TGID_NUMA_MAPS:
1617 inode->i_fop = &proc_numa_maps_operations;
1618 break;
1619 #endif
1620 case PROC_TID_MEM:
1621 case PROC_TGID_MEM:
1622 inode->i_op = &proc_mem_inode_operations;
1623 inode->i_fop = &proc_mem_operations;
1624 break;
1625 #ifdef CONFIG_SECCOMP
1626 case PROC_TID_SECCOMP:
1627 case PROC_TGID_SECCOMP:
1628 inode->i_fop = &proc_seccomp_operations;
1629 break;
1630 #endif /* CONFIG_SECCOMP */
1631 case PROC_TID_MOUNTS:
1632 case PROC_TGID_MOUNTS:
1633 inode->i_fop = &proc_mounts_operations;
1634 break;
1635 case PROC_TID_SMAPS:
1636 case PROC_TGID_SMAPS:
1637 inode->i_fop = &proc_smaps_operations;
1638 break;
1639 #ifdef CONFIG_SECURITY
1640 case PROC_TID_ATTR:
1641 inode->i_nlink = 2;
1642 inode->i_op = &proc_tid_attr_inode_operations;
1643 inode->i_fop = &proc_tid_attr_operations;
1644 break;
1645 case PROC_TGID_ATTR:
1646 inode->i_nlink = 2;
1647 inode->i_op = &proc_tgid_attr_inode_operations;
1648 inode->i_fop = &proc_tgid_attr_operations;
1649 break;
1650 case PROC_TID_ATTR_CURRENT:
1651 case PROC_TGID_ATTR_CURRENT:
1652 case PROC_TID_ATTR_PREV:
1653 case PROC_TGID_ATTR_PREV:
1654 case PROC_TID_ATTR_EXEC:
1655 case PROC_TGID_ATTR_EXEC:
1656 case PROC_TID_ATTR_FSCREATE:
1657 case PROC_TGID_ATTR_FSCREATE:
1658 inode->i_fop = &proc_pid_attr_operations;
1659 break;
1660 #endif
1661 #ifdef CONFIG_KALLSYMS
1662 case PROC_TID_WCHAN:
1663 case PROC_TGID_WCHAN:
1664 inode->i_fop = &proc_info_file_operations;
1665 ei->op.proc_read = proc_pid_wchan;
1666 break;
1667 #endif
1668 #ifdef CONFIG_SCHEDSTATS
1669 case PROC_TID_SCHEDSTAT:
1670 case PROC_TGID_SCHEDSTAT:
1671 inode->i_fop = &proc_info_file_operations;
1672 ei->op.proc_read = proc_pid_schedstat;
1673 break;
1674 #endif
1675 #ifdef CONFIG_CPUSETS
1676 case PROC_TID_CPUSET:
1677 case PROC_TGID_CPUSET:
1678 inode->i_fop = &proc_cpuset_operations;
1679 break;
1680 #endif
1681 case PROC_TID_OOM_SCORE:
1682 case PROC_TGID_OOM_SCORE:
1683 inode->i_fop = &proc_info_file_operations;
1684 ei->op.proc_read = proc_oom_score;
1685 break;
1686 case PROC_TID_OOM_ADJUST:
1687 case PROC_TGID_OOM_ADJUST:
1688 inode->i_fop = &proc_oom_adjust_operations;
1689 break;
1690 #ifdef CONFIG_AUDITSYSCALL
1691 case PROC_TID_LOGINUID:
1692 case PROC_TGID_LOGINUID:
1693 inode->i_fop = &proc_loginuid_operations;
1694 break;
1695 #endif
1696 default:
1697 printk("procfs: impossible type (%d)",p->type);
1698 iput(inode);
1699 return ERR_PTR(-EINVAL);
1701 dentry->d_op = &pid_dentry_operations;
1702 d_add(dentry, inode);
1703 return NULL;
1705 out:
1706 return ERR_PTR(error);
1709 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1710 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1713 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1714 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1717 static struct file_operations proc_tgid_base_operations = {
1718 .read = generic_read_dir,
1719 .readdir = proc_tgid_base_readdir,
1722 static struct file_operations proc_tid_base_operations = {
1723 .read = generic_read_dir,
1724 .readdir = proc_tid_base_readdir,
1727 static struct inode_operations proc_tgid_base_inode_operations = {
1728 .lookup = proc_tgid_base_lookup,
1731 static struct inode_operations proc_tid_base_inode_operations = {
1732 .lookup = proc_tid_base_lookup,
1735 #ifdef CONFIG_SECURITY
1736 static int proc_tgid_attr_readdir(struct file * filp,
1737 void * dirent, filldir_t filldir)
1739 return proc_pident_readdir(filp,dirent,filldir,
1740 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1743 static int proc_tid_attr_readdir(struct file * filp,
1744 void * dirent, filldir_t filldir)
1746 return proc_pident_readdir(filp,dirent,filldir,
1747 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1750 static struct file_operations proc_tgid_attr_operations = {
1751 .read = generic_read_dir,
1752 .readdir = proc_tgid_attr_readdir,
1755 static struct file_operations proc_tid_attr_operations = {
1756 .read = generic_read_dir,
1757 .readdir = proc_tid_attr_readdir,
1760 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1761 struct dentry *dentry, struct nameidata *nd)
1763 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1766 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1767 struct dentry *dentry, struct nameidata *nd)
1769 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1772 static struct inode_operations proc_tgid_attr_inode_operations = {
1773 .lookup = proc_tgid_attr_lookup,
1776 static struct inode_operations proc_tid_attr_inode_operations = {
1777 .lookup = proc_tid_attr_lookup,
1779 #endif
1782 * /proc/self:
1784 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1785 int buflen)
1787 char tmp[30];
1788 sprintf(tmp, "%d", current->tgid);
1789 return vfs_readlink(dentry,buffer,buflen,tmp);
1792 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1794 char tmp[30];
1795 sprintf(tmp, "%d", current->tgid);
1796 return ERR_PTR(vfs_follow_link(nd,tmp));
1799 static struct inode_operations proc_self_inode_operations = {
1800 .readlink = proc_self_readlink,
1801 .follow_link = proc_self_follow_link,
1805 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1806 * @p: task that should be flushed.
1808 * Drops the /proc/@pid dcache entry from the hash chains.
1810 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1811 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1812 * if the pid value is immediately reused. This is enforced by
1813 * - caller must acquire spin_lock(p->proc_lock)
1814 * - must be called before detach_pid()
1815 * - proc_pid_lookup acquires proc_lock, and checks that
1816 * the target is not dead by looking at the attach count
1817 * of PIDTYPE_PID.
1820 struct dentry *proc_pid_unhash(struct task_struct *p)
1822 struct dentry *proc_dentry;
1824 proc_dentry = p->proc_dentry;
1825 if (proc_dentry != NULL) {
1827 spin_lock(&dcache_lock);
1828 spin_lock(&proc_dentry->d_lock);
1829 if (!d_unhashed(proc_dentry)) {
1830 dget_locked(proc_dentry);
1831 __d_drop(proc_dentry);
1832 spin_unlock(&proc_dentry->d_lock);
1833 } else {
1834 spin_unlock(&proc_dentry->d_lock);
1835 proc_dentry = NULL;
1837 spin_unlock(&dcache_lock);
1839 return proc_dentry;
1843 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1844 * @proc_dentry: directoy to prune.
1846 * Shrink the /proc directory that was used by the just killed thread.
1849 void proc_pid_flush(struct dentry *proc_dentry)
1851 might_sleep();
1852 if(proc_dentry != NULL) {
1853 shrink_dcache_parent(proc_dentry);
1854 dput(proc_dentry);
1858 /* SMP-safe */
1859 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1861 struct task_struct *task;
1862 struct inode *inode;
1863 struct proc_inode *ei;
1864 unsigned tgid;
1865 int died;
1867 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1868 inode = new_inode(dir->i_sb);
1869 if (!inode)
1870 return ERR_PTR(-ENOMEM);
1871 ei = PROC_I(inode);
1872 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1873 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1874 ei->pde = NULL;
1875 inode->i_mode = S_IFLNK|S_IRWXUGO;
1876 inode->i_uid = inode->i_gid = 0;
1877 inode->i_size = 64;
1878 inode->i_op = &proc_self_inode_operations;
1879 d_add(dentry, inode);
1880 return NULL;
1882 tgid = name_to_int(dentry);
1883 if (tgid == ~0U)
1884 goto out;
1886 read_lock(&tasklist_lock);
1887 task = find_task_by_pid(tgid);
1888 if (task)
1889 get_task_struct(task);
1890 read_unlock(&tasklist_lock);
1891 if (!task)
1892 goto out;
1894 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1897 if (!inode) {
1898 put_task_struct(task);
1899 goto out;
1901 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1902 inode->i_op = &proc_tgid_base_inode_operations;
1903 inode->i_fop = &proc_tgid_base_operations;
1904 inode->i_flags|=S_IMMUTABLE;
1905 #ifdef CONFIG_SECURITY
1906 inode->i_nlink = 5;
1907 #else
1908 inode->i_nlink = 4;
1909 #endif
1911 dentry->d_op = &pid_base_dentry_operations;
1913 died = 0;
1914 d_add(dentry, inode);
1915 spin_lock(&task->proc_lock);
1916 task->proc_dentry = dentry;
1917 if (!pid_alive(task)) {
1918 dentry = proc_pid_unhash(task);
1919 died = 1;
1921 spin_unlock(&task->proc_lock);
1923 put_task_struct(task);
1924 if (died) {
1925 proc_pid_flush(dentry);
1926 goto out;
1928 return NULL;
1929 out:
1930 return ERR_PTR(-ENOENT);
1933 /* SMP-safe */
1934 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1936 struct task_struct *task;
1937 struct task_struct *leader = proc_task(dir);
1938 struct inode *inode;
1939 unsigned tid;
1941 tid = name_to_int(dentry);
1942 if (tid == ~0U)
1943 goto out;
1945 read_lock(&tasklist_lock);
1946 task = find_task_by_pid(tid);
1947 if (task)
1948 get_task_struct(task);
1949 read_unlock(&tasklist_lock);
1950 if (!task)
1951 goto out;
1952 if (leader->tgid != task->tgid)
1953 goto out_drop_task;
1955 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1958 if (!inode)
1959 goto out_drop_task;
1960 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1961 inode->i_op = &proc_tid_base_inode_operations;
1962 inode->i_fop = &proc_tid_base_operations;
1963 inode->i_flags|=S_IMMUTABLE;
1964 #ifdef CONFIG_SECURITY
1965 inode->i_nlink = 4;
1966 #else
1967 inode->i_nlink = 3;
1968 #endif
1970 dentry->d_op = &pid_base_dentry_operations;
1972 d_add(dentry, inode);
1974 put_task_struct(task);
1975 return NULL;
1976 out_drop_task:
1977 put_task_struct(task);
1978 out:
1979 return ERR_PTR(-ENOENT);
1982 #define PROC_NUMBUF 10
1983 #define PROC_MAXPIDS 20
1986 * Get a few tgid's to return for filldir - we need to hold the
1987 * tasklist lock while doing this, and we must release it before
1988 * we actually do the filldir itself, so we use a temp buffer..
1990 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1992 struct task_struct *p;
1993 int nr_tgids = 0;
1995 index--;
1996 read_lock(&tasklist_lock);
1997 p = NULL;
1998 if (version) {
1999 p = find_task_by_pid(version);
2000 if (p && !thread_group_leader(p))
2001 p = NULL;
2004 if (p)
2005 index = 0;
2006 else
2007 p = next_task(&init_task);
2009 for ( ; p != &init_task; p = next_task(p)) {
2010 int tgid = p->pid;
2011 if (!pid_alive(p))
2012 continue;
2013 if (--index >= 0)
2014 continue;
2015 tgids[nr_tgids] = tgid;
2016 nr_tgids++;
2017 if (nr_tgids >= PROC_MAXPIDS)
2018 break;
2020 read_unlock(&tasklist_lock);
2021 return nr_tgids;
2025 * Get a few tid's to return for filldir - we need to hold the
2026 * tasklist lock while doing this, and we must release it before
2027 * we actually do the filldir itself, so we use a temp buffer..
2029 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2031 struct task_struct *leader_task = proc_task(dir);
2032 struct task_struct *task = leader_task;
2033 int nr_tids = 0;
2035 index -= 2;
2036 read_lock(&tasklist_lock);
2038 * The starting point task (leader_task) might be an already
2039 * unlinked task, which cannot be used to access the task-list
2040 * via next_thread().
2042 if (pid_alive(task)) do {
2043 int tid = task->pid;
2045 if (--index >= 0)
2046 continue;
2047 if (tids != NULL)
2048 tids[nr_tids] = tid;
2049 nr_tids++;
2050 if (nr_tids >= PROC_MAXPIDS)
2051 break;
2052 } while ((task = next_thread(task)) != leader_task);
2053 read_unlock(&tasklist_lock);
2054 return nr_tids;
2057 /* for the /proc/ directory itself, after non-process stuff has been done */
2058 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2060 unsigned int tgid_array[PROC_MAXPIDS];
2061 char buf[PROC_NUMBUF];
2062 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2063 unsigned int nr_tgids, i;
2064 int next_tgid;
2066 if (!nr) {
2067 ino_t ino = fake_ino(0,PROC_TGID_INO);
2068 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2069 return 0;
2070 filp->f_pos++;
2071 nr++;
2074 /* f_version caches the tgid value that the last readdir call couldn't
2075 * return. lseek aka telldir automagically resets f_version to 0.
2077 next_tgid = filp->f_version;
2078 filp->f_version = 0;
2079 for (;;) {
2080 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2081 if (!nr_tgids) {
2082 /* no more entries ! */
2083 break;
2085 next_tgid = 0;
2087 /* do not use the last found pid, reserve it for next_tgid */
2088 if (nr_tgids == PROC_MAXPIDS) {
2089 nr_tgids--;
2090 next_tgid = tgid_array[nr_tgids];
2093 for (i=0;i<nr_tgids;i++) {
2094 int tgid = tgid_array[i];
2095 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2096 unsigned long j = PROC_NUMBUF;
2099 buf[--j] = '0' + (tgid % 10);
2100 while ((tgid /= 10) != 0);
2102 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2103 /* returning this tgid failed, save it as the first
2104 * pid for the next readir call */
2105 filp->f_version = tgid_array[i];
2106 goto out;
2108 filp->f_pos++;
2109 nr++;
2112 out:
2113 return 0;
2116 /* for the /proc/TGID/task/ directories */
2117 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2119 unsigned int tid_array[PROC_MAXPIDS];
2120 char buf[PROC_NUMBUF];
2121 unsigned int nr_tids, i;
2122 struct dentry *dentry = filp->f_dentry;
2123 struct inode *inode = dentry->d_inode;
2124 int retval = -ENOENT;
2125 ino_t ino;
2126 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2128 if (!pid_alive(proc_task(inode)))
2129 goto out;
2130 retval = 0;
2132 switch (pos) {
2133 case 0:
2134 ino = inode->i_ino;
2135 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2136 goto out;
2137 pos++;
2138 /* fall through */
2139 case 1:
2140 ino = parent_ino(dentry);
2141 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2142 goto out;
2143 pos++;
2144 /* fall through */
2147 nr_tids = get_tid_list(pos, tid_array, inode);
2148 inode->i_nlink = pos + nr_tids;
2150 for (i = 0; i < nr_tids; i++) {
2151 unsigned long j = PROC_NUMBUF;
2152 int tid = tid_array[i];
2154 ino = fake_ino(tid,PROC_TID_INO);
2157 buf[--j] = '0' + (tid % 10);
2158 while ((tid /= 10) != 0);
2160 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
2161 break;
2162 pos++;
2164 out:
2165 filp->f_pos = pos;
2166 return retval;