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 #ifdef CONFIG_DELAY_ACCT
106 PROC_TGID_DELAY_ACCT,
108 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
118 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
120 static struct pid_entry tgid_base_stuff[] = {
121 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
122 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
123 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
124 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
125 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
126 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
127 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
128 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
129 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
130 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
131 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
132 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
133 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
134 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
135 #ifdef CONFIG_SECURITY
136 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
138 #ifdef CONFIG_DELAY_ACCT
139 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
141 #ifdef CONFIG_KALLSYMS
142 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
144 #ifdef CONFIG_SCHEDSTATS
145 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
147 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
148 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
149 #ifdef CONFIG_SCHEDSTATS
150 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
154 static struct pid_entry tid_base_stuff[] = {
155 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
156 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
157 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
158 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
159 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
160 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
161 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
162 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
163 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
164 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
165 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
166 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
167 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
168 #ifdef CONFIG_SECURITY
169 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
171 #ifdef CONFIG_DELAY_ACCT
172 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
174 #ifdef CONFIG_KALLSYMS
175 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
177 #ifdef CONFIG_SCHEDSTATS
178 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
180 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
181 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
182 #ifdef CONFIG_SCHEDSTATS
183 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
188 #ifdef CONFIG_SECURITY
189 static struct pid_entry tgid_attr_stuff[] = {
190 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
191 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
192 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
193 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
196 static struct pid_entry tid_attr_stuff[] = {
197 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
198 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
199 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
200 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
207 static inline struct task_struct *proc_task(struct inode *inode)
209 return PROC_I(inode)->task;
212 static inline int proc_type(struct inode *inode)
214 return PROC_I(inode)->type;
217 int proc_pid_stat(struct task_struct*,char*);
218 int proc_pid_status(struct task_struct*,char*);
219 int proc_pid_statm(struct task_struct*,char*);
220 #ifdef CONFIG_DELAY_ACCT
221 int proc_pid_delay(struct task_struct*,char*);
224 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
226 struct task_struct *task = proc_task(inode);
227 struct files_struct *files;
229 int fd = proc_type(inode) - PROC_TID_FD_DIR;
231 files = get_files_struct(task);
233 spin_lock(&files->file_lock);
234 file = fcheck_files(files, fd);
236 *mnt = mntget(file->f_vfsmnt);
237 *dentry = dget(file->f_dentry);
238 spin_unlock(&files->file_lock);
239 put_files_struct(files);
242 spin_unlock(&files->file_lock);
243 put_files_struct(files);
248 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
250 struct vm_area_struct * vma;
251 int result = -ENOENT;
252 struct task_struct *task = proc_task(inode);
253 struct mm_struct * mm = get_task_mm(task);
257 down_read(&mm->mmap_sem);
260 if ((vma->vm_flags & VM_EXECUTABLE) &&
262 *mnt = mntget(vma->vm_file->f_vfsmnt);
263 *dentry = dget(vma->vm_file->f_dentry);
269 up_read(&mm->mmap_sem);
275 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
277 struct fs_struct *fs;
278 int result = -ENOENT;
279 task_lock(proc_task(inode));
280 fs = proc_task(inode)->fs;
282 atomic_inc(&fs->count);
283 task_unlock(proc_task(inode));
285 read_lock(&fs->lock);
286 *mnt = mntget(fs->pwdmnt);
287 *dentry = dget(fs->pwd);
288 read_unlock(&fs->lock);
295 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
297 struct fs_struct *fs;
298 int result = -ENOENT;
299 task_lock(proc_task(inode));
300 fs = proc_task(inode)->fs;
302 atomic_inc(&fs->count);
303 task_unlock(proc_task(inode));
305 read_lock(&fs->lock);
306 *mnt = mntget(fs->rootmnt);
307 *dentry = dget(fs->root);
308 read_unlock(&fs->lock);
315 #define MAY_PTRACE(task) \
316 (task == current || \
317 (task->parent == current && \
318 (task->ptrace & PT_PTRACED) && \
319 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
320 security_ptrace(current,task) == 0))
322 static int may_ptrace_attach(struct task_struct *task)
330 if (((current->uid != task->euid) ||
331 (current->uid != task->suid) ||
332 (current->uid != task->uid) ||
333 (current->gid != task->egid) ||
334 (current->gid != task->sgid) ||
335 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
338 if (task->mm->dumpable != 1 && !capable(CAP_SYS_PTRACE))
340 if (security_ptrace(current, task))
349 static int proc_pid_environ(struct task_struct *task, char * buffer)
352 struct mm_struct *mm = get_task_mm(task);
354 unsigned int len = mm->env_end - mm->env_start;
357 res = access_process_vm(task, mm->env_start, buffer, len, 0);
358 if (!may_ptrace_attach(task))
365 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
369 struct mm_struct *mm = get_task_mm(task);
373 goto out_mm; /* Shh! No looking before we're done */
375 len = mm->arg_end - mm->arg_start;
380 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
382 // If the nul at the end of args has been overwritten, then
383 // assume application is using setproctitle(3).
384 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
385 len = strnlen(buffer, res);
389 len = mm->env_end - mm->env_start;
390 if (len > PAGE_SIZE - res)
391 len = PAGE_SIZE - res;
392 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
393 res = strnlen(buffer, res);
402 static int proc_pid_auxv(struct task_struct *task, char *buffer)
405 struct mm_struct *mm = get_task_mm(task);
407 unsigned int nwords = 0;
410 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
411 res = nwords * sizeof(mm->saved_auxv[0]);
414 memcpy(buffer, mm->saved_auxv, res);
421 #ifdef CONFIG_KALLSYMS
423 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
424 * Returns the resolved symbol. If that fails, simply return the address.
426 static int proc_pid_wchan(struct task_struct *task, char *buffer)
429 const char *sym_name;
430 unsigned long wchan, size, offset;
433 wchan = get_wchan(task);
435 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
437 return sprintf(buffer, "%s", sym_name);
438 return sprintf(buffer, "%lu", wchan);
440 #endif /* CONFIG_KALLSYMS */
442 #ifdef CONFIG_SCHEDSTATS
444 * Provides /proc/PID/schedstat
446 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
448 return sprintf(buffer, "%lu %lu %lu\n",
449 task->sched_info.cpu_time,
450 task->sched_info.run_delay,
451 task->sched_info.pcnt);
455 /************************************************************************/
456 /* Here the fs part begins */
457 /************************************************************************/
459 /* permission checks */
461 static int proc_check_root(struct inode *inode)
463 struct dentry *de, *base, *root;
464 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
467 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
469 read_lock(¤t->fs->lock);
470 our_vfsmnt = mntget(current->fs->rootmnt);
471 base = dget(current->fs->root);
472 read_unlock(¤t->fs->lock);
474 spin_lock(&vfsmount_lock);
478 while (vfsmnt != our_vfsmnt) {
479 if (vfsmnt == vfsmnt->mnt_parent)
481 de = vfsmnt->mnt_mountpoint;
482 vfsmnt = vfsmnt->mnt_parent;
485 if (!is_subdir(de, base))
487 spin_unlock(&vfsmount_lock);
496 spin_unlock(&vfsmount_lock);
501 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
503 if (vfs_permission(inode, mask) != 0)
505 return proc_check_root(inode);
508 extern struct seq_operations proc_pid_maps_op;
509 static int maps_open(struct inode *inode, struct file *file)
511 struct task_struct *task = proc_task(inode);
512 int ret = seq_open(file, &proc_pid_maps_op);
514 struct seq_file *m = file->private_data;
520 static struct file_operations proc_maps_operations = {
524 .release = seq_release,
527 extern struct seq_operations mounts_op;
528 static int mounts_open(struct inode *inode, struct file *file)
530 struct task_struct *task = proc_task(inode);
531 int ret = seq_open(file, &mounts_op);
534 struct seq_file *m = file->private_data;
535 struct namespace *namespace;
537 namespace = task->namespace;
539 get_namespace(namespace);
543 m->private = namespace;
545 seq_release(inode, file);
552 static int mounts_release(struct inode *inode, struct file *file)
554 struct seq_file *m = file->private_data;
555 struct namespace *namespace = m->private;
556 put_namespace(namespace);
557 return seq_release(inode, file);
560 static struct file_operations proc_mounts_operations = {
564 .release = mounts_release,
567 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
569 static ssize_t proc_info_read(struct file * file, char __user * buf,
570 size_t count, loff_t *ppos)
572 struct inode * inode = file->f_dentry->d_inode;
575 struct task_struct *task = proc_task(inode);
577 if (count > PROC_BLOCK_SIZE)
578 count = PROC_BLOCK_SIZE;
579 if (!(page = __get_free_page(GFP_KERNEL)))
582 length = PROC_I(inode)->op.proc_read(task, (char*)page);
585 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
590 static struct file_operations proc_info_file_operations = {
591 .read = proc_info_read,
594 static int mem_open(struct inode* inode, struct file* file)
596 file->private_data = (void*)((long)current->self_exec_id);
600 static ssize_t mem_read(struct file * file, char __user * buf,
601 size_t count, loff_t *ppos)
603 struct task_struct *task = proc_task(file->f_dentry->d_inode);
605 unsigned long src = *ppos;
607 struct mm_struct *mm;
609 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
613 page = (char *)__get_free_page(GFP_USER);
619 mm = get_task_mm(task);
625 if (file->private_data != (void*)((long)current->self_exec_id))
631 int this_len, retval;
633 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
634 retval = access_process_vm(task, src, page, this_len, 0);
635 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
641 if (copy_to_user(buf, page, retval)) {
656 free_page((unsigned long) page);
661 #define mem_write NULL
664 /* This is a security hazard */
665 static ssize_t mem_write(struct file * file, const char * buf,
666 size_t count, loff_t *ppos)
670 struct task_struct *task = proc_task(file->f_dentry->d_inode);
671 unsigned long dst = *ppos;
673 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
676 page = (char *)__get_free_page(GFP_USER);
681 int this_len, retval;
683 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
684 if (copy_from_user(page, buf, this_len)) {
688 retval = access_process_vm(task, dst, page, this_len, 1);
700 free_page((unsigned long) page);
705 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
709 file->f_pos = offset;
712 file->f_pos += offset;
717 force_successful_syscall_return();
721 static struct file_operations proc_mem_operations = {
728 static struct inode_operations proc_mem_inode_operations = {
729 .permission = proc_permission,
732 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
734 struct inode *inode = dentry->d_inode;
737 /* We don't need a base pointer in the /proc filesystem */
740 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
742 error = proc_check_root(inode);
746 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
747 nd->last_type = LAST_BIND;
752 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
753 char __user *buffer, int buflen)
755 struct inode * inode;
756 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
762 inode = dentry->d_inode;
763 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
767 len = tmp + PAGE_SIZE - 1 - path;
771 if (copy_to_user(buffer, path, len))
774 free_page((unsigned long)tmp);
778 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
781 struct inode *inode = dentry->d_inode;
783 struct vfsmount *mnt = NULL;
787 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
789 error = proc_check_root(inode);
793 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
797 error = do_proc_readlink(de, mnt, buffer, buflen);
805 static struct inode_operations proc_pid_link_inode_operations = {
806 .readlink = proc_pid_readlink,
807 .follow_link = proc_pid_follow_link
812 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
814 struct inode *inode = filp->f_dentry->d_inode;
815 struct task_struct *p = proc_task(inode);
816 unsigned int fd, tid, ino;
819 struct files_struct * files;
830 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
834 ino = fake_ino(tid, PROC_TID_INO);
835 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
839 files = get_files_struct(p);
842 spin_lock(&files->file_lock);
843 for (fd = filp->f_pos-2;
845 fd++, filp->f_pos++) {
848 if (!fcheck_files(files, fd))
850 spin_unlock(&files->file_lock);
856 buf[j] = '0' + (i % 10);
860 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
861 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
862 spin_lock(&files->file_lock);
865 spin_lock(&files->file_lock);
867 spin_unlock(&files->file_lock);
868 put_files_struct(files);
874 static int proc_pident_readdir(struct file *filp,
875 void *dirent, filldir_t filldir,
876 struct pid_entry *ents, unsigned int nents)
880 struct dentry *dentry = filp->f_dentry;
881 struct inode *inode = dentry->d_inode;
887 if (!pid_alive(proc_task(inode)))
891 pid = proc_task(inode)->pid;
896 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
902 ino = parent_ino(dentry);
903 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
916 if (filldir(dirent, p->name, p->len, filp->f_pos,
917 fake_ino(pid, p->type), p->mode >> 12) < 0)
929 static int proc_tgid_base_readdir(struct file * filp,
930 void * dirent, filldir_t filldir)
932 return proc_pident_readdir(filp,dirent,filldir,
933 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
936 static int proc_tid_base_readdir(struct file * filp,
937 void * dirent, filldir_t filldir)
939 return proc_pident_readdir(filp,dirent,filldir,
940 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
943 /* building an inode */
945 static int task_dumpable(struct task_struct *task)
948 struct mm_struct *mm;
953 dumpable = mm->dumpable;
961 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
963 struct inode * inode;
964 struct proc_inode *ei;
966 /* We need a new inode */
968 inode = new_inode(sb);
975 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
976 inode->i_ino = fake_ino(task->pid, ino);
978 if (!pid_alive(task))
982 * grab the reference to task.
984 get_task_struct(task);
989 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
990 inode->i_uid = task->euid;
991 inode->i_gid = task->egid;
993 inode->i_xid = vx_task_xid(task);
994 security_task_to_inode(task, inode);
1008 * Exceptional case: normally we are not allowed to unhash a busy
1009 * directory. In this case, however, we can do it - no aliasing problems
1010 * due to the way we treat inodes.
1012 * Rewrite the inode's ownerships here because the owning task may have
1013 * performed a setuid(), etc.
1015 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1017 struct inode *inode = dentry->d_inode;
1018 struct task_struct *task = proc_task(inode);
1020 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1022 /* discard wrong fakeinit */
1024 if (pid_alive(task)) {
1025 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1026 inode->i_uid = task->euid;
1027 inode->i_gid = task->egid;
1032 security_task_to_inode(task, inode);
1040 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1042 struct inode *inode = dentry->d_inode;
1043 struct task_struct *task = proc_task(inode);
1044 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1045 struct files_struct *files;
1047 files = get_files_struct(task);
1049 spin_lock(&files->file_lock);
1050 if (fcheck_files(files, fd)) {
1051 spin_unlock(&files->file_lock);
1052 put_files_struct(files);
1053 if (task_dumpable(task)) {
1054 inode->i_uid = task->euid;
1055 inode->i_gid = task->egid;
1060 security_task_to_inode(task, inode);
1063 spin_unlock(&files->file_lock);
1064 put_files_struct(files);
1070 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1072 struct task_struct *task = proc_task(inode);
1073 spin_lock(&task->proc_lock);
1074 if (task->proc_dentry == dentry)
1075 task->proc_dentry = NULL;
1076 spin_unlock(&task->proc_lock);
1080 static int pid_delete_dentry(struct dentry * dentry)
1082 /* Is the task we represent dead?
1083 * If so, then don't put the dentry on the lru list,
1084 * kill it immediately.
1086 return !pid_alive(proc_task(dentry->d_inode));
1089 static struct dentry_operations tid_fd_dentry_operations =
1091 .d_revalidate = tid_fd_revalidate,
1092 .d_delete = pid_delete_dentry,
1095 static struct dentry_operations pid_dentry_operations =
1097 .d_revalidate = pid_revalidate,
1098 .d_delete = pid_delete_dentry,
1101 static struct dentry_operations pid_base_dentry_operations =
1103 .d_revalidate = pid_revalidate,
1104 .d_iput = pid_base_iput,
1105 .d_delete = pid_delete_dentry,
1110 static unsigned name_to_int(struct dentry *dentry)
1112 const char *name = dentry->d_name.name;
1113 int len = dentry->d_name.len;
1116 if (len > 1 && *name == '0')
1119 unsigned c = *name++ - '0';
1122 if (n >= (~0U-9)/10)
1133 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1135 struct task_struct *task = proc_task(dir);
1136 unsigned fd = name_to_int(dentry);
1138 struct files_struct * files;
1139 struct inode *inode;
1140 struct proc_inode *ei;
1144 if (!pid_alive(task))
1147 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1151 files = get_files_struct(task);
1154 inode->i_mode = S_IFLNK;
1155 spin_lock(&files->file_lock);
1156 file = fcheck_files(files, fd);
1159 if (file->f_mode & 1)
1160 inode->i_mode |= S_IRUSR | S_IXUSR;
1161 if (file->f_mode & 2)
1162 inode->i_mode |= S_IWUSR | S_IXUSR;
1163 spin_unlock(&files->file_lock);
1164 put_files_struct(files);
1165 inode->i_op = &proc_pid_link_inode_operations;
1167 ei->op.proc_get_link = proc_fd_link;
1168 dentry->d_op = &tid_fd_dentry_operations;
1169 d_add(dentry, inode);
1173 spin_unlock(&files->file_lock);
1174 put_files_struct(files);
1178 return ERR_PTR(-ENOENT);
1181 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1182 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1184 static struct file_operations proc_fd_operations = {
1185 .read = generic_read_dir,
1186 .readdir = proc_readfd,
1189 static struct file_operations proc_task_operations = {
1190 .read = generic_read_dir,
1191 .readdir = proc_task_readdir,
1195 * proc directories can do almost nothing..
1197 static struct inode_operations proc_fd_inode_operations = {
1198 .lookup = proc_lookupfd,
1199 .permission = proc_permission,
1202 static struct inode_operations proc_task_inode_operations = {
1203 .lookup = proc_task_lookup,
1204 .permission = proc_permission,
1207 #ifdef CONFIG_SECURITY
1208 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1209 size_t count, loff_t *ppos)
1211 struct inode * inode = file->f_dentry->d_inode;
1214 struct task_struct *task = proc_task(inode);
1216 if (count > PAGE_SIZE)
1218 if (!(page = __get_free_page(GFP_KERNEL)))
1221 length = security_getprocattr(task,
1222 (char*)file->f_dentry->d_name.name,
1223 (void*)page, count);
1225 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1230 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1231 size_t count, loff_t *ppos)
1233 struct inode * inode = file->f_dentry->d_inode;
1236 struct task_struct *task = proc_task(inode);
1238 if (count > PAGE_SIZE)
1241 /* No partial writes. */
1244 page = (char*)__get_free_page(GFP_USER);
1248 if (copy_from_user(page, buf, count))
1251 length = security_setprocattr(task,
1252 (char*)file->f_dentry->d_name.name,
1253 (void*)page, count);
1255 free_page((unsigned long) page);
1259 static struct file_operations proc_pid_attr_operations = {
1260 .read = proc_pid_attr_read,
1261 .write = proc_pid_attr_write,
1264 static struct file_operations proc_tid_attr_operations;
1265 static struct inode_operations proc_tid_attr_inode_operations;
1266 static struct file_operations proc_tgid_attr_operations;
1267 static struct inode_operations proc_tgid_attr_inode_operations;
1271 static struct dentry *proc_pident_lookup(struct inode *dir,
1272 struct dentry *dentry,
1273 struct pid_entry *ents)
1275 struct inode *inode;
1277 struct task_struct *task = proc_task(dir);
1278 struct pid_entry *p;
1279 struct proc_inode *ei;
1284 if (!pid_alive(task))
1287 for (p = ents; p->name; p++) {
1288 if (p->len != dentry->d_name.len)
1290 if (!memcmp(dentry->d_name.name, p->name, p->len))
1297 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1302 inode->i_mode = p->mode;
1304 * Yes, it does not scale. And it should not. Don't add
1305 * new entries into /proc/<tgid>/ without very good reasons.
1308 case PROC_TGID_TASK:
1310 inode->i_op = &proc_task_inode_operations;
1311 inode->i_fop = &proc_task_operations;
1316 inode->i_op = &proc_fd_inode_operations;
1317 inode->i_fop = &proc_fd_operations;
1321 inode->i_op = &proc_pid_link_inode_operations;
1322 ei->op.proc_get_link = proc_exe_link;
1326 inode->i_op = &proc_pid_link_inode_operations;
1327 ei->op.proc_get_link = proc_cwd_link;
1330 case PROC_TGID_ROOT:
1331 inode->i_op = &proc_pid_link_inode_operations;
1332 ei->op.proc_get_link = proc_root_link;
1334 case PROC_TID_ENVIRON:
1335 case PROC_TGID_ENVIRON:
1336 inode->i_fop = &proc_info_file_operations;
1337 ei->op.proc_read = proc_pid_environ;
1340 case PROC_TGID_AUXV:
1341 inode->i_fop = &proc_info_file_operations;
1342 ei->op.proc_read = proc_pid_auxv;
1344 case PROC_TID_STATUS:
1345 case PROC_TGID_STATUS:
1346 inode->i_fop = &proc_info_file_operations;
1347 ei->op.proc_read = proc_pid_status;
1350 case PROC_TGID_STAT:
1351 inode->i_fop = &proc_info_file_operations;
1352 ei->op.proc_read = proc_pid_stat;
1354 case PROC_TID_CMDLINE:
1355 case PROC_TGID_CMDLINE:
1356 inode->i_fop = &proc_info_file_operations;
1357 ei->op.proc_read = proc_pid_cmdline;
1359 case PROC_TID_STATM:
1360 case PROC_TGID_STATM:
1361 inode->i_fop = &proc_info_file_operations;
1362 ei->op.proc_read = proc_pid_statm;
1365 case PROC_TGID_MAPS:
1366 inode->i_fop = &proc_maps_operations;
1370 inode->i_op = &proc_mem_inode_operations;
1371 inode->i_fop = &proc_mem_operations;
1373 case PROC_TID_MOUNTS:
1374 case PROC_TGID_MOUNTS:
1375 inode->i_fop = &proc_mounts_operations;
1377 #ifdef CONFIG_SECURITY
1380 inode->i_op = &proc_tid_attr_inode_operations;
1381 inode->i_fop = &proc_tid_attr_operations;
1383 case PROC_TGID_ATTR:
1385 inode->i_op = &proc_tgid_attr_inode_operations;
1386 inode->i_fop = &proc_tgid_attr_operations;
1388 case PROC_TID_ATTR_CURRENT:
1389 case PROC_TGID_ATTR_CURRENT:
1390 case PROC_TID_ATTR_PREV:
1391 case PROC_TGID_ATTR_PREV:
1392 case PROC_TID_ATTR_EXEC:
1393 case PROC_TGID_ATTR_EXEC:
1394 case PROC_TID_ATTR_FSCREATE:
1395 case PROC_TGID_ATTR_FSCREATE:
1396 inode->i_fop = &proc_pid_attr_operations;
1399 #ifdef CONFIG_KALLSYMS
1400 case PROC_TID_WCHAN:
1401 case PROC_TGID_WCHAN:
1402 inode->i_fop = &proc_info_file_operations;
1403 ei->op.proc_read = proc_pid_wchan;
1406 #ifdef CONFIG_SCHEDSTATS
1407 case PROC_TID_SCHEDSTAT:
1408 case PROC_TGID_SCHEDSTAT:
1409 inode->i_fop = &proc_info_file_operations;
1410 ei->op.proc_read = proc_pid_schedstat;
1413 case PROC_TID_VX_INFO:
1414 case PROC_TGID_VX_INFO:
1415 inode->i_fop = &proc_info_file_operations;
1416 ei->op.proc_read = proc_pid_vx_info;
1418 case PROC_TID_IP_INFO:
1419 case PROC_TGID_IP_INFO:
1420 inode->i_fop = &proc_info_file_operations;
1421 ei->op.proc_read = proc_pid_nx_info;
1423 #ifdef CONFIG_DELAY_ACCT
1424 case PROC_TID_DELAY_ACCT:
1425 case PROC_TGID_DELAY_ACCT:
1426 inode->i_fop = &proc_info_file_operations;
1427 ei->op.proc_read = proc_pid_delay;
1430 #ifdef CONFIG_SCHEDSTATS
1431 case PROC_TID_SCHEDSTAT:
1432 case PROC_TGID_SCHEDSTAT:
1433 inode->i_fop = &proc_info_file_operations;
1434 ei->op.proc_read = proc_pid_schedstat;
1438 printk("procfs: impossible type (%d)",p->type);
1440 return ERR_PTR(-EINVAL);
1442 dentry->d_op = &pid_dentry_operations;
1443 d_add(dentry, inode);
1447 return ERR_PTR(error);
1450 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1451 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1454 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1455 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1458 static struct file_operations proc_tgid_base_operations = {
1459 .read = generic_read_dir,
1460 .readdir = proc_tgid_base_readdir,
1463 static struct file_operations proc_tid_base_operations = {
1464 .read = generic_read_dir,
1465 .readdir = proc_tid_base_readdir,
1468 static struct inode_operations proc_tgid_base_inode_operations = {
1469 .lookup = proc_tgid_base_lookup,
1472 static struct inode_operations proc_tid_base_inode_operations = {
1473 .lookup = proc_tid_base_lookup,
1476 #ifdef CONFIG_SECURITY
1477 static int proc_tgid_attr_readdir(struct file * filp,
1478 void * dirent, filldir_t filldir)
1480 return proc_pident_readdir(filp,dirent,filldir,
1481 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1484 static int proc_tid_attr_readdir(struct file * filp,
1485 void * dirent, filldir_t filldir)
1487 return proc_pident_readdir(filp,dirent,filldir,
1488 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1491 static struct file_operations proc_tgid_attr_operations = {
1492 .read = generic_read_dir,
1493 .readdir = proc_tgid_attr_readdir,
1496 static struct file_operations proc_tid_attr_operations = {
1497 .read = generic_read_dir,
1498 .readdir = proc_tid_attr_readdir,
1501 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1502 struct dentry *dentry, struct nameidata *nd)
1504 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1507 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1508 struct dentry *dentry, struct nameidata *nd)
1510 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1513 static struct inode_operations proc_tgid_attr_inode_operations = {
1514 .lookup = proc_tgid_attr_lookup,
1517 static struct inode_operations proc_tid_attr_inode_operations = {
1518 .lookup = proc_tid_attr_lookup,
1525 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1529 sprintf(tmp, "%d", vx_map_pid(current->tgid));
1530 return vfs_readlink(dentry,buffer,buflen,tmp);
1533 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1536 sprintf(tmp, "%d", vx_map_pid(current->tgid));
1537 return vfs_follow_link(nd,tmp);
1540 static struct inode_operations proc_self_inode_operations = {
1541 .readlink = proc_self_readlink,
1542 .follow_link = proc_self_follow_link,
1546 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1547 * @p: task that should be flushed.
1549 * Drops the /proc/<pid> dcache entry from the hash chains.
1551 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1552 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1553 * if the pid value is immediately reused. This is enforced by
1554 * - caller must acquire spin_lock(p->proc_lock)
1555 * - must be called before detach_pid()
1556 * - proc_pid_lookup acquires proc_lock, and checks that
1557 * the target is not dead by looking at the attach count
1561 struct dentry *proc_pid_unhash(struct task_struct *p)
1563 struct dentry *proc_dentry;
1565 proc_dentry = p->proc_dentry;
1566 if (proc_dentry != NULL) {
1568 spin_lock(&dcache_lock);
1569 if (!d_unhashed(proc_dentry)) {
1570 dget_locked(proc_dentry);
1571 __d_drop(proc_dentry);
1574 spin_unlock(&dcache_lock);
1580 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1581 * @proc_entry: directoy to prune.
1583 * Shrink the /proc directory that was used by the just killed thread.
1586 void proc_pid_flush(struct dentry *proc_dentry)
1589 if(proc_dentry != NULL) {
1590 shrink_dcache_parent(proc_dentry);
1596 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1598 struct task_struct *task;
1599 struct inode *inode;
1600 struct proc_inode *ei;
1604 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1605 inode = new_inode(dir->i_sb);
1607 return ERR_PTR(-ENOMEM);
1609 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1610 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1612 inode->i_mode = S_IFLNK|S_IRWXUGO;
1613 inode->i_uid = inode->i_gid = 0;
1615 inode->i_op = &proc_self_inode_operations;
1616 d_add(dentry, inode);
1619 tgid = name_to_int(dentry);
1623 read_lock(&tasklist_lock);
1624 task = find_task_by_pid(tgid);
1626 get_task_struct(task);
1627 read_unlock(&tasklist_lock);
1631 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1634 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1638 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1639 inode->i_op = &proc_tgid_base_inode_operations;
1640 inode->i_fop = &proc_tgid_base_operations;
1642 inode->i_flags|=S_IMMUTABLE;
1644 dentry->d_op = &pid_base_dentry_operations;
1647 d_add(dentry, inode);
1648 spin_lock(&task->proc_lock);
1649 task->proc_dentry = dentry;
1650 if (!pid_alive(task)) {
1651 dentry = proc_pid_unhash(task);
1654 spin_unlock(&task->proc_lock);
1656 put_task_struct(task);
1658 proc_pid_flush(dentry);
1663 put_task_struct(task);
1665 return ERR_PTR(-ENOENT);
1669 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1671 struct task_struct *task;
1672 struct task_struct *leader = proc_task(dir);
1673 struct inode *inode;
1676 tid = name_to_int(dentry);
1679 if (vx_current_initpid(tid))
1682 read_lock(&tasklist_lock);
1683 task = find_task_by_pid(tid);
1685 get_task_struct(task);
1686 read_unlock(&tasklist_lock);
1689 if (leader->tgid != task->tgid)
1692 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1695 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1699 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1700 inode->i_op = &proc_tid_base_inode_operations;
1701 inode->i_fop = &proc_tid_base_operations;
1703 inode->i_flags|=S_IMMUTABLE;
1705 dentry->d_op = &pid_base_dentry_operations;
1707 d_add(dentry, inode);
1709 put_task_struct(task);
1712 put_task_struct(task);
1714 return ERR_PTR(-ENOENT);
1717 #define PROC_NUMBUF 10
1718 #define PROC_MAXPIDS 20
1721 * Get a few tgid's to return for filldir - we need to hold the
1722 * tasklist lock while doing this, and we must release it before
1723 * we actually do the filldir itself, so we use a temp buffer..
1725 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1727 struct task_struct *p;
1731 read_lock(&tasklist_lock);
1734 p = find_task_by_real_pid(version);
1735 if (p && !thread_group_leader(p))
1742 p = next_task(&init_task);
1744 for ( ; p != &init_task; p = next_task(p)) {
1749 if (!vx_check(vx_task_xid(p), VX_WATCH|VX_IDENT))
1753 tgids[nr_tgids] = vx_map_tgid(tgid);
1755 if (nr_tgids >= PROC_MAXPIDS)
1758 read_unlock(&tasklist_lock);
1763 * Get a few tid's to return for filldir - we need to hold the
1764 * tasklist lock while doing this, and we must release it before
1765 * we actually do the filldir itself, so we use a temp buffer..
1767 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1769 struct task_struct *leader_task = proc_task(dir);
1770 struct task_struct *task = leader_task;
1774 read_lock(&tasklist_lock);
1776 * The starting point task (leader_task) might be an already
1777 * unlinked task, which cannot be used to access the task-list
1778 * via next_thread().
1780 if (pid_alive(task)) do {
1781 int tid = task->pid;
1783 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1787 tids[nr_tids] = vx_map_pid(tid);
1789 if (nr_tids >= PROC_MAXPIDS)
1791 } while ((task = next_thread(task)) != leader_task);
1792 read_unlock(&tasklist_lock);
1796 /* for the /proc/ directory itself, after non-process stuff has been done */
1797 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1799 unsigned int tgid_array[PROC_MAXPIDS];
1800 char buf[PROC_NUMBUF];
1801 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1802 unsigned int nr_tgids, i;
1806 ino_t ino = fake_ino(0,PROC_TGID_INO);
1807 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1813 /* f_version caches the tgid value that the last readdir call couldn't
1814 * return. lseek aka telldir automagically resets f_version to 0.
1816 next_tgid = filp->f_version;
1817 filp->f_version = 0;
1819 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
1821 /* no more entries ! */
1826 /* do not use the last found pid, reserve it for next_tgid */
1827 if (nr_tgids == PROC_MAXPIDS) {
1829 next_tgid = tgid_array[nr_tgids];
1832 for (i=0;i<nr_tgids;i++) {
1833 int tgid = tgid_array[i];
1834 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1835 unsigned long j = PROC_NUMBUF;
1838 buf[--j] = '0' + (tgid % 10);
1839 while ((tgid /= 10) != 0);
1841 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1842 /* returning this tgid failed, save it as the first
1843 * pid for the next readir call */
1844 filp->f_version = tgid_array[i];
1855 /* for the /proc/TGID/task/ directories */
1856 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1858 unsigned int tid_array[PROC_MAXPIDS];
1859 char buf[PROC_NUMBUF];
1860 unsigned int nr_tids, i;
1861 struct dentry *dentry = filp->f_dentry;
1862 struct inode *inode = dentry->d_inode;
1863 struct task_struct *task = proc_task(inode);
1864 int retval = -ENOENT;
1866 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1868 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1870 if (!pid_alive(task))
1877 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1882 ino = parent_ino(dentry);
1883 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1889 nr_tids = get_tid_list(pos, tid_array, inode);
1891 for (i = 0; i < nr_tids; i++) {
1892 unsigned long j = PROC_NUMBUF;
1893 int tid = tid_array[i];
1895 ino = fake_ino(tid,PROC_TID_INO);
1898 buf[--j] = '0' + (tid % 10);
1899 while ((tid /= 10) != 0);
1901 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)