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 #include <asm/uaccess.h>
18 #include <linux/config.h>
19 #include <linux/errno.h>
20 #include <linux/time.h>
21 #include <linux/proc_fs.h>
22 #include <linux/stat.h>
23 #include <linux/init.h>
24 #include <linux/file.h>
25 #include <linux/string.h>
26 #include <linux/seq_file.h>
27 #include <linux/namei.h>
28 #include <linux/namespace.h>
30 #include <linux/smp_lock.h>
31 #include <linux/kallsyms.h>
32 #include <linux/mount.h>
33 #include <linux/security.h>
34 #include <linux/ptrace.h>
35 #include <linux/vs_network.h>
38 * For hysterical raisins we keep the same inumbers as in the old procfs.
39 * Feel free to change the macro below - just keep the range distinct from
40 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
41 * As soon as we'll get a separate superblock we will be able to forget
42 * about magical ranges too.
45 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
47 enum pid_directory_inos {
64 #ifdef CONFIG_SCHEDSTATS
67 #ifdef CONFIG_SECURITY
69 PROC_TGID_ATTR_CURRENT,
72 PROC_TGID_ATTR_FSCREATE,
92 #ifdef CONFIG_SCHEDSTATS
95 #ifdef CONFIG_SECURITY
97 PROC_TID_ATTR_CURRENT,
100 PROC_TID_ATTR_FSCREATE,
104 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
114 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
116 static struct pid_entry tgid_base_stuff[] = {
117 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
118 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
119 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
120 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
121 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
122 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
123 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
124 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
125 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
126 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
127 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
128 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
129 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
130 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
131 #ifdef CONFIG_SECURITY
132 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
134 #ifdef CONFIG_KALLSYMS
135 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
137 #ifdef CONFIG_SCHEDSTATS
138 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
140 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
141 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
144 static struct pid_entry tid_base_stuff[] = {
145 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
146 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
147 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
148 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
149 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
150 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
151 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
152 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
153 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
154 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
155 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
156 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
157 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
158 #ifdef CONFIG_SECURITY
159 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
161 #ifdef CONFIG_KALLSYMS
162 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
164 #ifdef CONFIG_SCHEDSTATS
165 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
167 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
168 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
172 #ifdef CONFIG_SECURITY
173 static struct pid_entry tgid_attr_stuff[] = {
174 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
175 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
176 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
177 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
180 static struct pid_entry tid_attr_stuff[] = {
181 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
182 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
183 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
184 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
191 static inline struct task_struct *proc_task(struct inode *inode)
193 return PROC_I(inode)->task;
196 static inline int proc_type(struct inode *inode)
198 return PROC_I(inode)->type;
201 int proc_tid_stat(struct task_struct*,char*);
202 int proc_tgid_stat(struct task_struct*,char*);
203 int proc_pid_status(struct task_struct*,char*);
204 int proc_pid_statm(struct task_struct*,char*);
206 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
208 struct task_struct *task = proc_task(inode);
209 struct files_struct *files;
211 int fd = proc_type(inode) - PROC_TID_FD_DIR;
213 files = get_files_struct(task);
215 spin_lock(&files->file_lock);
216 file = fcheck_files(files, fd);
218 *mnt = mntget(file->f_vfsmnt);
219 *dentry = dget(file->f_dentry);
220 spin_unlock(&files->file_lock);
221 put_files_struct(files);
224 spin_unlock(&files->file_lock);
225 put_files_struct(files);
230 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
232 struct vm_area_struct * vma;
233 int result = -ENOENT;
234 struct task_struct *task = proc_task(inode);
235 struct mm_struct * mm = get_task_mm(task);
239 down_read(&mm->mmap_sem);
242 if ((vma->vm_flags & VM_EXECUTABLE) &&
244 *mnt = mntget(vma->vm_file->f_vfsmnt);
245 *dentry = dget(vma->vm_file->f_dentry);
251 up_read(&mm->mmap_sem);
257 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
259 struct fs_struct *fs;
260 int result = -ENOENT;
261 task_lock(proc_task(inode));
262 fs = proc_task(inode)->fs;
264 atomic_inc(&fs->count);
265 task_unlock(proc_task(inode));
267 read_lock(&fs->lock);
268 *mnt = mntget(fs->pwdmnt);
269 *dentry = dget(fs->pwd);
270 read_unlock(&fs->lock);
277 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
279 struct fs_struct *fs;
280 int result = -ENOENT;
281 task_lock(proc_task(inode));
282 fs = proc_task(inode)->fs;
284 atomic_inc(&fs->count);
285 task_unlock(proc_task(inode));
287 read_lock(&fs->lock);
288 *mnt = mntget(fs->rootmnt);
289 *dentry = dget(fs->root);
290 read_unlock(&fs->lock);
297 #define MAY_PTRACE(task) \
298 (task == current || \
299 (task->parent == current && \
300 (task->ptrace & PT_PTRACED) && \
301 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
302 security_ptrace(current,task) == 0))
304 static int may_ptrace_attach(struct task_struct *task)
312 if (((current->uid != task->euid) ||
313 (current->uid != task->suid) ||
314 (current->uid != task->uid) ||
315 (current->gid != task->egid) ||
316 (current->gid != task->sgid) ||
317 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
320 if (task->mm->dumpable != 1 && !capable(CAP_SYS_PTRACE))
322 if (security_ptrace(current, task))
331 static int proc_pid_environ(struct task_struct *task, char * buffer)
334 struct mm_struct *mm = get_task_mm(task);
336 unsigned int len = mm->env_end - mm->env_start;
339 res = access_process_vm(task, mm->env_start, buffer, len, 0);
340 if (!may_ptrace_attach(task))
347 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
351 struct mm_struct *mm = get_task_mm(task);
355 goto out_mm; /* Shh! No looking before we're done */
357 len = mm->arg_end - mm->arg_start;
362 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
364 // If the nul at the end of args has been overwritten, then
365 // assume application is using setproctitle(3).
366 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
367 len = strnlen(buffer, res);
371 len = mm->env_end - mm->env_start;
372 if (len > PAGE_SIZE - res)
373 len = PAGE_SIZE - res;
374 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
375 res = strnlen(buffer, res);
384 static int proc_pid_auxv(struct task_struct *task, char *buffer)
387 struct mm_struct *mm = get_task_mm(task);
389 unsigned int nwords = 0;
392 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
393 res = nwords * sizeof(mm->saved_auxv[0]);
396 memcpy(buffer, mm->saved_auxv, res);
403 #ifdef CONFIG_KALLSYMS
405 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
406 * Returns the resolved symbol. If that fails, simply return the address.
408 static int proc_pid_wchan(struct task_struct *task, char *buffer)
411 const char *sym_name;
412 unsigned long wchan, size, offset;
415 wchan = get_wchan(task);
417 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
419 return sprintf(buffer, "%s", sym_name);
420 return sprintf(buffer, "%lu", wchan);
422 #endif /* CONFIG_KALLSYMS */
424 #ifdef CONFIG_SCHEDSTATS
426 * Provides /proc/PID/schedstat
428 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
430 return sprintf(buffer, "%lu %lu %lu\n",
431 task->sched_info.cpu_time,
432 task->sched_info.run_delay,
433 task->sched_info.pcnt);
437 /************************************************************************/
438 /* Here the fs part begins */
439 /************************************************************************/
441 /* permission checks */
443 static int proc_check_root(struct inode *inode)
445 struct dentry *de, *base, *root;
446 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
449 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
451 read_lock(¤t->fs->lock);
452 our_vfsmnt = mntget(current->fs->rootmnt);
453 base = dget(current->fs->root);
454 read_unlock(¤t->fs->lock);
456 spin_lock(&vfsmount_lock);
460 while (vfsmnt != our_vfsmnt) {
461 if (vfsmnt == vfsmnt->mnt_parent)
463 de = vfsmnt->mnt_mountpoint;
464 vfsmnt = vfsmnt->mnt_parent;
467 if (!is_subdir(de, base))
469 spin_unlock(&vfsmount_lock);
478 spin_unlock(&vfsmount_lock);
483 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
485 if (generic_permission(inode, mask, NULL) != 0)
487 return proc_check_root(inode);
490 extern struct seq_operations proc_pid_maps_op;
491 static int maps_open(struct inode *inode, struct file *file)
493 struct task_struct *task = proc_task(inode);
494 int ret = seq_open(file, &proc_pid_maps_op);
496 struct seq_file *m = file->private_data;
502 static struct file_operations proc_maps_operations = {
506 .release = seq_release,
509 extern struct seq_operations mounts_op;
510 static int mounts_open(struct inode *inode, struct file *file)
512 struct task_struct *task = proc_task(inode);
513 int ret = seq_open(file, &mounts_op);
516 struct seq_file *m = file->private_data;
517 struct namespace *namespace;
519 namespace = task->namespace;
521 get_namespace(namespace);
525 m->private = namespace;
527 seq_release(inode, file);
534 static int mounts_release(struct inode *inode, struct file *file)
536 struct seq_file *m = file->private_data;
537 struct namespace *namespace = m->private;
538 put_namespace(namespace);
539 return seq_release(inode, file);
542 static struct file_operations proc_mounts_operations = {
546 .release = mounts_release,
549 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
551 static ssize_t proc_info_read(struct file * file, char __user * buf,
552 size_t count, loff_t *ppos)
554 struct inode * inode = file->f_dentry->d_inode;
557 struct task_struct *task = proc_task(inode);
559 if (count > PROC_BLOCK_SIZE)
560 count = PROC_BLOCK_SIZE;
561 if (!(page = __get_free_page(GFP_KERNEL)))
564 length = PROC_I(inode)->op.proc_read(task, (char*)page);
567 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
572 static struct file_operations proc_info_file_operations = {
573 .read = proc_info_read,
576 static int mem_open(struct inode* inode, struct file* file)
578 file->private_data = (void*)((long)current->self_exec_id);
582 static ssize_t mem_read(struct file * file, char __user * buf,
583 size_t count, loff_t *ppos)
585 struct task_struct *task = proc_task(file->f_dentry->d_inode);
587 unsigned long src = *ppos;
589 struct mm_struct *mm;
591 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
595 page = (char *)__get_free_page(GFP_USER);
601 mm = get_task_mm(task);
607 if (file->private_data != (void*)((long)current->self_exec_id))
613 int this_len, retval;
615 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
616 retval = access_process_vm(task, src, page, this_len, 0);
617 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
623 if (copy_to_user(buf, page, retval)) {
638 free_page((unsigned long) page);
643 #define mem_write NULL
646 /* This is a security hazard */
647 static ssize_t mem_write(struct file * file, const char * buf,
648 size_t count, loff_t *ppos)
652 struct task_struct *task = proc_task(file->f_dentry->d_inode);
653 unsigned long dst = *ppos;
655 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
658 page = (char *)__get_free_page(GFP_USER);
663 int this_len, retval;
665 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
666 if (copy_from_user(page, buf, this_len)) {
670 retval = access_process_vm(task, dst, page, this_len, 1);
682 free_page((unsigned long) page);
687 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
691 file->f_pos = offset;
694 file->f_pos += offset;
699 force_successful_syscall_return();
703 static struct file_operations proc_mem_operations = {
710 static struct inode_operations proc_mem_inode_operations = {
711 .permission = proc_permission,
714 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
716 struct inode *inode = dentry->d_inode;
719 /* We don't need a base pointer in the /proc filesystem */
722 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
724 error = proc_check_root(inode);
728 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
729 nd->last_type = LAST_BIND;
734 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
735 char __user *buffer, int buflen)
737 struct inode * inode;
738 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
744 inode = dentry->d_inode;
745 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
749 len = tmp + PAGE_SIZE - 1 - path;
753 if (copy_to_user(buffer, path, len))
756 free_page((unsigned long)tmp);
760 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
763 struct inode *inode = dentry->d_inode;
765 struct vfsmount *mnt = NULL;
769 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
771 error = proc_check_root(inode);
775 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
779 error = do_proc_readlink(de, mnt, buffer, buflen);
787 static struct inode_operations proc_pid_link_inode_operations = {
788 .readlink = proc_pid_readlink,
789 .follow_link = proc_pid_follow_link
794 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
796 struct inode *inode = filp->f_dentry->d_inode;
797 struct task_struct *p = proc_task(inode);
798 unsigned int fd, tid, ino;
801 struct files_struct * files;
812 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
816 ino = fake_ino(tid, PROC_TID_INO);
817 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
821 files = get_files_struct(p);
824 spin_lock(&files->file_lock);
825 for (fd = filp->f_pos-2;
827 fd++, filp->f_pos++) {
830 if (!fcheck_files(files, fd))
832 spin_unlock(&files->file_lock);
838 buf[j] = '0' + (i % 10);
842 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
843 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
844 spin_lock(&files->file_lock);
847 spin_lock(&files->file_lock);
849 spin_unlock(&files->file_lock);
850 put_files_struct(files);
856 static int proc_pident_readdir(struct file *filp,
857 void *dirent, filldir_t filldir,
858 struct pid_entry *ents, unsigned int nents)
862 struct dentry *dentry = filp->f_dentry;
863 struct inode *inode = dentry->d_inode;
869 if (!pid_alive(proc_task(inode)))
873 pid = proc_task(inode)->pid;
878 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
884 ino = parent_ino(dentry);
885 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
898 if (filldir(dirent, p->name, p->len, filp->f_pos,
899 fake_ino(pid, p->type), p->mode >> 12) < 0)
911 static int proc_tgid_base_readdir(struct file * filp,
912 void * dirent, filldir_t filldir)
914 return proc_pident_readdir(filp,dirent,filldir,
915 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
918 static int proc_tid_base_readdir(struct file * filp,
919 void * dirent, filldir_t filldir)
921 return proc_pident_readdir(filp,dirent,filldir,
922 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
925 /* building an inode */
927 static int task_dumpable(struct task_struct *task)
930 struct mm_struct *mm;
935 dumpable = mm->dumpable;
943 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
945 struct inode * inode;
946 struct proc_inode *ei;
948 /* We need a new inode */
950 inode = new_inode(sb);
957 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
958 inode->i_ino = fake_ino(task->pid, ino);
960 if (!pid_alive(task))
964 * grab the reference to task.
966 get_task_struct(task);
971 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
972 inode->i_uid = task->euid;
973 inode->i_gid = task->egid;
975 inode->i_xid = vx_task_xid(task);
976 security_task_to_inode(task, inode);
990 * Exceptional case: normally we are not allowed to unhash a busy
991 * directory. In this case, however, we can do it - no aliasing problems
992 * due to the way we treat inodes.
994 * Rewrite the inode's ownerships here because the owning task may have
995 * performed a setuid(), etc.
997 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
999 struct inode *inode = dentry->d_inode;
1000 struct task_struct *task = proc_task(inode);
1002 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1004 /* discard wrong fakeinit */
1006 if (pid_alive(task)) {
1007 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1008 inode->i_uid = task->euid;
1009 inode->i_gid = task->egid;
1014 security_task_to_inode(task, inode);
1022 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1024 struct inode *inode = dentry->d_inode;
1025 struct task_struct *task = proc_task(inode);
1026 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1027 struct files_struct *files;
1029 files = get_files_struct(task);
1031 spin_lock(&files->file_lock);
1032 if (fcheck_files(files, fd)) {
1033 spin_unlock(&files->file_lock);
1034 put_files_struct(files);
1035 if (task_dumpable(task)) {
1036 inode->i_uid = task->euid;
1037 inode->i_gid = task->egid;
1042 security_task_to_inode(task, inode);
1045 spin_unlock(&files->file_lock);
1046 put_files_struct(files);
1052 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1054 struct task_struct *task = proc_task(inode);
1055 spin_lock(&task->proc_lock);
1056 if (task->proc_dentry == dentry)
1057 task->proc_dentry = NULL;
1058 spin_unlock(&task->proc_lock);
1062 static int pid_delete_dentry(struct dentry * dentry)
1064 /* Is the task we represent dead?
1065 * If so, then don't put the dentry on the lru list,
1066 * kill it immediately.
1068 return !pid_alive(proc_task(dentry->d_inode));
1071 static struct dentry_operations tid_fd_dentry_operations =
1073 .d_revalidate = tid_fd_revalidate,
1074 .d_delete = pid_delete_dentry,
1077 static struct dentry_operations pid_dentry_operations =
1079 .d_revalidate = pid_revalidate,
1080 .d_delete = pid_delete_dentry,
1083 static struct dentry_operations pid_base_dentry_operations =
1085 .d_revalidate = pid_revalidate,
1086 .d_iput = pid_base_iput,
1087 .d_delete = pid_delete_dentry,
1092 static unsigned name_to_int(struct dentry *dentry)
1094 const char *name = dentry->d_name.name;
1095 int len = dentry->d_name.len;
1098 if (len > 1 && *name == '0')
1101 unsigned c = *name++ - '0';
1104 if (n >= (~0U-9)/10)
1115 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1117 struct task_struct *task = proc_task(dir);
1118 unsigned fd = name_to_int(dentry);
1120 struct files_struct * files;
1121 struct inode *inode;
1122 struct proc_inode *ei;
1126 if (!pid_alive(task))
1129 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1133 files = get_files_struct(task);
1136 inode->i_mode = S_IFLNK;
1137 spin_lock(&files->file_lock);
1138 file = fcheck_files(files, fd);
1141 if (file->f_mode & 1)
1142 inode->i_mode |= S_IRUSR | S_IXUSR;
1143 if (file->f_mode & 2)
1144 inode->i_mode |= S_IWUSR | S_IXUSR;
1145 spin_unlock(&files->file_lock);
1146 put_files_struct(files);
1147 inode->i_op = &proc_pid_link_inode_operations;
1149 ei->op.proc_get_link = proc_fd_link;
1150 dentry->d_op = &tid_fd_dentry_operations;
1151 d_add(dentry, inode);
1155 spin_unlock(&files->file_lock);
1156 put_files_struct(files);
1160 return ERR_PTR(-ENOENT);
1163 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1164 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1166 static struct file_operations proc_fd_operations = {
1167 .read = generic_read_dir,
1168 .readdir = proc_readfd,
1171 static struct file_operations proc_task_operations = {
1172 .read = generic_read_dir,
1173 .readdir = proc_task_readdir,
1177 * proc directories can do almost nothing..
1179 static struct inode_operations proc_fd_inode_operations = {
1180 .lookup = proc_lookupfd,
1181 .permission = proc_permission,
1184 static struct inode_operations proc_task_inode_operations = {
1185 .lookup = proc_task_lookup,
1186 .permission = proc_permission,
1189 #ifdef CONFIG_SECURITY
1190 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1191 size_t count, loff_t *ppos)
1193 struct inode * inode = file->f_dentry->d_inode;
1196 struct task_struct *task = proc_task(inode);
1198 if (count > PAGE_SIZE)
1200 if (!(page = __get_free_page(GFP_KERNEL)))
1203 length = security_getprocattr(task,
1204 (char*)file->f_dentry->d_name.name,
1205 (void*)page, count);
1207 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1212 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1213 size_t count, loff_t *ppos)
1215 struct inode * inode = file->f_dentry->d_inode;
1218 struct task_struct *task = proc_task(inode);
1220 if (count > PAGE_SIZE)
1223 /* No partial writes. */
1226 page = (char*)__get_free_page(GFP_USER);
1230 if (copy_from_user(page, buf, count))
1233 length = security_setprocattr(task,
1234 (char*)file->f_dentry->d_name.name,
1235 (void*)page, count);
1237 free_page((unsigned long) page);
1241 static struct file_operations proc_pid_attr_operations = {
1242 .read = proc_pid_attr_read,
1243 .write = proc_pid_attr_write,
1246 static struct file_operations proc_tid_attr_operations;
1247 static struct inode_operations proc_tid_attr_inode_operations;
1248 static struct file_operations proc_tgid_attr_operations;
1249 static struct inode_operations proc_tgid_attr_inode_operations;
1252 extern int proc_pid_vx_info(struct task_struct *, char *);
1253 extern int proc_pid_nx_info(struct task_struct *, char *);
1256 static struct dentry *proc_pident_lookup(struct inode *dir,
1257 struct dentry *dentry,
1258 struct pid_entry *ents)
1260 struct inode *inode;
1262 struct task_struct *task = proc_task(dir);
1263 struct pid_entry *p;
1264 struct proc_inode *ei;
1269 if (!pid_alive(task))
1272 for (p = ents; p->name; p++) {
1273 if (p->len != dentry->d_name.len)
1275 if (!memcmp(dentry->d_name.name, p->name, p->len))
1282 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1287 inode->i_mode = p->mode;
1289 * Yes, it does not scale. And it should not. Don't add
1290 * new entries into /proc/<tgid>/ without very good reasons.
1293 case PROC_TGID_TASK:
1295 inode->i_op = &proc_task_inode_operations;
1296 inode->i_fop = &proc_task_operations;
1301 inode->i_op = &proc_fd_inode_operations;
1302 inode->i_fop = &proc_fd_operations;
1306 inode->i_op = &proc_pid_link_inode_operations;
1307 ei->op.proc_get_link = proc_exe_link;
1311 inode->i_op = &proc_pid_link_inode_operations;
1312 ei->op.proc_get_link = proc_cwd_link;
1315 case PROC_TGID_ROOT:
1316 inode->i_op = &proc_pid_link_inode_operations;
1317 ei->op.proc_get_link = proc_root_link;
1319 case PROC_TID_ENVIRON:
1320 case PROC_TGID_ENVIRON:
1321 inode->i_fop = &proc_info_file_operations;
1322 ei->op.proc_read = proc_pid_environ;
1325 case PROC_TGID_AUXV:
1326 inode->i_fop = &proc_info_file_operations;
1327 ei->op.proc_read = proc_pid_auxv;
1329 case PROC_TID_STATUS:
1330 case PROC_TGID_STATUS:
1331 inode->i_fop = &proc_info_file_operations;
1332 ei->op.proc_read = proc_pid_status;
1335 inode->i_fop = &proc_info_file_operations;
1336 ei->op.proc_read = proc_tid_stat;
1338 case PROC_TGID_STAT:
1339 inode->i_fop = &proc_info_file_operations;
1340 ei->op.proc_read = proc_tgid_stat;
1342 case PROC_TID_CMDLINE:
1343 case PROC_TGID_CMDLINE:
1344 inode->i_fop = &proc_info_file_operations;
1345 ei->op.proc_read = proc_pid_cmdline;
1347 case PROC_TID_STATM:
1348 case PROC_TGID_STATM:
1349 inode->i_fop = &proc_info_file_operations;
1350 ei->op.proc_read = proc_pid_statm;
1353 case PROC_TGID_MAPS:
1354 inode->i_fop = &proc_maps_operations;
1358 inode->i_op = &proc_mem_inode_operations;
1359 inode->i_fop = &proc_mem_operations;
1361 case PROC_TID_MOUNTS:
1362 case PROC_TGID_MOUNTS:
1363 inode->i_fop = &proc_mounts_operations;
1365 #ifdef CONFIG_SECURITY
1368 inode->i_op = &proc_tid_attr_inode_operations;
1369 inode->i_fop = &proc_tid_attr_operations;
1371 case PROC_TGID_ATTR:
1373 inode->i_op = &proc_tgid_attr_inode_operations;
1374 inode->i_fop = &proc_tgid_attr_operations;
1376 case PROC_TID_ATTR_CURRENT:
1377 case PROC_TGID_ATTR_CURRENT:
1378 case PROC_TID_ATTR_PREV:
1379 case PROC_TGID_ATTR_PREV:
1380 case PROC_TID_ATTR_EXEC:
1381 case PROC_TGID_ATTR_EXEC:
1382 case PROC_TID_ATTR_FSCREATE:
1383 case PROC_TGID_ATTR_FSCREATE:
1384 inode->i_fop = &proc_pid_attr_operations;
1387 #ifdef CONFIG_KALLSYMS
1388 case PROC_TID_WCHAN:
1389 case PROC_TGID_WCHAN:
1390 inode->i_fop = &proc_info_file_operations;
1391 ei->op.proc_read = proc_pid_wchan;
1394 #ifdef CONFIG_SCHEDSTATS
1395 case PROC_TID_SCHEDSTAT:
1396 case PROC_TGID_SCHEDSTAT:
1397 inode->i_fop = &proc_info_file_operations;
1398 ei->op.proc_read = proc_pid_schedstat;
1401 case PROC_TID_VX_INFO:
1402 case PROC_TGID_VX_INFO:
1403 inode->i_fop = &proc_info_file_operations;
1404 ei->op.proc_read = proc_pid_vx_info;
1406 case PROC_TID_IP_INFO:
1407 case PROC_TGID_IP_INFO:
1408 inode->i_fop = &proc_info_file_operations;
1409 ei->op.proc_read = proc_pid_nx_info;
1411 #ifdef CONFIG_SCHEDSTATS
1412 case PROC_TID_SCHEDSTAT:
1413 case PROC_TGID_SCHEDSTAT:
1414 inode->i_fop = &proc_info_file_operations;
1415 ei->op.proc_read = proc_pid_schedstat;
1419 printk("procfs: impossible type (%d)",p->type);
1421 return ERR_PTR(-EINVAL);
1423 dentry->d_op = &pid_dentry_operations;
1424 d_add(dentry, inode);
1428 return ERR_PTR(error);
1431 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1432 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1435 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1436 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1439 static struct file_operations proc_tgid_base_operations = {
1440 .read = generic_read_dir,
1441 .readdir = proc_tgid_base_readdir,
1444 static struct file_operations proc_tid_base_operations = {
1445 .read = generic_read_dir,
1446 .readdir = proc_tid_base_readdir,
1449 static struct inode_operations proc_tgid_base_inode_operations = {
1450 .lookup = proc_tgid_base_lookup,
1453 static struct inode_operations proc_tid_base_inode_operations = {
1454 .lookup = proc_tid_base_lookup,
1457 #ifdef CONFIG_SECURITY
1458 static int proc_tgid_attr_readdir(struct file * filp,
1459 void * dirent, filldir_t filldir)
1461 return proc_pident_readdir(filp,dirent,filldir,
1462 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1465 static int proc_tid_attr_readdir(struct file * filp,
1466 void * dirent, filldir_t filldir)
1468 return proc_pident_readdir(filp,dirent,filldir,
1469 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1472 static struct file_operations proc_tgid_attr_operations = {
1473 .read = generic_read_dir,
1474 .readdir = proc_tgid_attr_readdir,
1477 static struct file_operations proc_tid_attr_operations = {
1478 .read = generic_read_dir,
1479 .readdir = proc_tid_attr_readdir,
1482 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1483 struct dentry *dentry, struct nameidata *nd)
1485 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1488 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1489 struct dentry *dentry, struct nameidata *nd)
1491 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1494 static struct inode_operations proc_tgid_attr_inode_operations = {
1495 .lookup = proc_tgid_attr_lookup,
1498 static struct inode_operations proc_tid_attr_inode_operations = {
1499 .lookup = proc_tid_attr_lookup,
1506 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1510 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1511 return vfs_readlink(dentry,buffer,buflen,tmp);
1514 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1517 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1518 return vfs_follow_link(nd,tmp);
1521 static struct inode_operations proc_self_inode_operations = {
1522 .readlink = proc_self_readlink,
1523 .follow_link = proc_self_follow_link,
1527 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1528 * @p: task that should be flushed.
1530 * Drops the /proc/<pid> dcache entry from the hash chains.
1532 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1533 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1534 * if the pid value is immediately reused. This is enforced by
1535 * - caller must acquire spin_lock(p->proc_lock)
1536 * - must be called before detach_pid()
1537 * - proc_pid_lookup acquires proc_lock, and checks that
1538 * the target is not dead by looking at the attach count
1542 struct dentry *proc_pid_unhash(struct task_struct *p)
1544 struct dentry *proc_dentry;
1546 proc_dentry = p->proc_dentry;
1547 if (proc_dentry != NULL) {
1549 spin_lock(&dcache_lock);
1550 if (!d_unhashed(proc_dentry)) {
1551 dget_locked(proc_dentry);
1552 __d_drop(proc_dentry);
1555 spin_unlock(&dcache_lock);
1561 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1562 * @proc_entry: directoy to prune.
1564 * Shrink the /proc directory that was used by the just killed thread.
1567 void proc_pid_flush(struct dentry *proc_dentry)
1570 if(proc_dentry != NULL) {
1571 shrink_dcache_parent(proc_dentry);
1577 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1579 struct task_struct *task;
1580 struct inode *inode;
1581 struct proc_inode *ei;
1585 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1586 inode = new_inode(dir->i_sb);
1588 return ERR_PTR(-ENOMEM);
1590 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1591 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1593 inode->i_mode = S_IFLNK|S_IRWXUGO;
1594 inode->i_uid = inode->i_gid = 0;
1596 inode->i_op = &proc_self_inode_operations;
1597 d_add(dentry, inode);
1600 tgid = name_to_int(dentry);
1604 read_lock(&tasklist_lock);
1605 task = find_task_by_pid(tgid);
1607 get_task_struct(task);
1608 read_unlock(&tasklist_lock);
1612 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1615 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1619 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1620 inode->i_op = &proc_tgid_base_inode_operations;
1621 inode->i_fop = &proc_tgid_base_operations;
1623 inode->i_flags|=S_IMMUTABLE;
1625 dentry->d_op = &pid_base_dentry_operations;
1628 d_add(dentry, inode);
1629 spin_lock(&task->proc_lock);
1630 task->proc_dentry = dentry;
1631 if (!pid_alive(task)) {
1632 dentry = proc_pid_unhash(task);
1635 spin_unlock(&task->proc_lock);
1637 put_task_struct(task);
1639 proc_pid_flush(dentry);
1644 put_task_struct(task);
1646 return ERR_PTR(-ENOENT);
1650 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1652 struct task_struct *task;
1653 struct task_struct *leader = proc_task(dir);
1654 struct inode *inode;
1657 tid = name_to_int(dentry);
1660 if (vx_current_initpid(tid))
1663 read_lock(&tasklist_lock);
1664 task = find_task_by_pid(tid);
1666 get_task_struct(task);
1667 read_unlock(&tasklist_lock);
1670 if (leader->tgid != task->tgid)
1673 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1676 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1680 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1681 inode->i_op = &proc_tid_base_inode_operations;
1682 inode->i_fop = &proc_tid_base_operations;
1684 inode->i_flags|=S_IMMUTABLE;
1686 dentry->d_op = &pid_base_dentry_operations;
1688 d_add(dentry, inode);
1690 put_task_struct(task);
1693 put_task_struct(task);
1695 return ERR_PTR(-ENOENT);
1698 #define PROC_NUMBUF 10
1699 #define PROC_MAXPIDS 20
1702 * Get a few tgid's to return for filldir - we need to hold the
1703 * tasklist lock while doing this, and we must release it before
1704 * we actually do the filldir itself, so we use a temp buffer..
1706 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1708 struct task_struct *p;
1712 read_lock(&tasklist_lock);
1715 p = find_task_by_real_pid(version);
1716 if (p && !thread_group_leader(p))
1723 p = next_task(&init_task);
1725 for ( ; p != &init_task; p = next_task(p)) {
1730 if (!vx_check(vx_task_xid(p), VX_WATCH|VX_IDENT))
1734 tgids[nr_tgids] = vx_map_tgid(tgid);
1736 if (nr_tgids >= PROC_MAXPIDS)
1739 read_unlock(&tasklist_lock);
1744 * Get a few tid's to return for filldir - we need to hold the
1745 * tasklist lock while doing this, and we must release it before
1746 * we actually do the filldir itself, so we use a temp buffer..
1748 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1750 struct task_struct *leader_task = proc_task(dir);
1751 struct task_struct *task = leader_task;
1755 read_lock(&tasklist_lock);
1757 * The starting point task (leader_task) might be an already
1758 * unlinked task, which cannot be used to access the task-list
1759 * via next_thread().
1761 if (pid_alive(task)) do {
1762 int tid = task->pid;
1764 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1768 tids[nr_tids] = vx_map_pid(tid);
1770 if (nr_tids >= PROC_MAXPIDS)
1772 } while ((task = next_thread(task)) != leader_task);
1773 read_unlock(&tasklist_lock);
1777 /* for the /proc/ directory itself, after non-process stuff has been done */
1778 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1780 unsigned int tgid_array[PROC_MAXPIDS];
1781 char buf[PROC_NUMBUF];
1782 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1783 unsigned int nr_tgids, i;
1787 ino_t ino = fake_ino(0,PROC_TGID_INO);
1788 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1794 /* f_version caches the tgid value that the last readdir call couldn't
1795 * return. lseek aka telldir automagically resets f_version to 0.
1797 next_tgid = filp->f_version;
1798 filp->f_version = 0;
1800 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
1802 /* no more entries ! */
1807 /* do not use the last found pid, reserve it for next_tgid */
1808 if (nr_tgids == PROC_MAXPIDS) {
1810 next_tgid = tgid_array[nr_tgids];
1813 for (i=0;i<nr_tgids;i++) {
1814 int tgid = tgid_array[i];
1815 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1816 unsigned long j = PROC_NUMBUF;
1819 buf[--j] = '0' + (tgid % 10);
1820 while ((tgid /= 10) != 0);
1822 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1823 /* returning this tgid failed, save it as the first
1824 * pid for the next readir call */
1825 filp->f_version = tgid_array[i];
1836 /* for the /proc/TGID/task/ directories */
1837 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1839 unsigned int tid_array[PROC_MAXPIDS];
1840 char buf[PROC_NUMBUF];
1841 unsigned int nr_tids, i;
1842 struct dentry *dentry = filp->f_dentry;
1843 struct inode *inode = dentry->d_inode;
1844 struct task_struct *task = proc_task(inode);
1845 int retval = -ENOENT;
1847 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1849 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1851 if (!pid_alive(task))
1858 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1863 ino = parent_ino(dentry);
1864 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1870 nr_tids = get_tid_list(pos, tid_array, inode);
1872 for (i = 0; i < nr_tids; i++) {
1873 unsigned long j = PROC_NUMBUF;
1874 int tid = tid_array[i];
1876 ino = fake_ino(tid,PROC_TID_INO);
1879 buf[--j] = '0' + (tid % 10);
1880 while ((tid /= 10) != 0);
1882 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)