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>
36 #include <linux/vs_cvirt.h>
39 * For hysterical raisins we keep the same inumbers as in the old procfs.
40 * Feel free to change the macro below - just keep the range distinct from
41 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
42 * As soon as we'll get a separate superblock we will be able to forget
43 * about magical ranges too.
46 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
48 enum pid_directory_inos {
65 #ifdef CONFIG_SECURITY
67 PROC_TGID_ATTR_CURRENT,
70 PROC_TGID_ATTR_FSCREATE,
90 #ifdef CONFIG_SECURITY
92 PROC_TID_ATTR_CURRENT,
95 PROC_TID_ATTR_FSCREATE,
99 #ifdef CONFIG_DELAY_ACCT
101 PROC_TGID_DELAY_ACCT,
103 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
113 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
115 static struct pid_entry tgid_base_stuff[] = {
116 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
117 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
118 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
119 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
120 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
121 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
122 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
123 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
124 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
125 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
126 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
127 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
128 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
129 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
130 #ifdef CONFIG_SECURITY
131 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
133 #ifdef CONFIG_DELAY_ACCT
134 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
136 #ifdef CONFIG_KALLSYMS
137 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
139 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
140 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
143 static struct pid_entry tid_base_stuff[] = {
144 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
145 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
146 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
147 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
148 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
149 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
150 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
151 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
152 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
153 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
154 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
155 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
156 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
157 #ifdef CONFIG_SECURITY
158 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
160 #ifdef CONFIG_DELAY_ACCT
161 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
163 #ifdef CONFIG_KALLSYMS
164 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
166 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
167 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
171 #ifdef CONFIG_SECURITY
172 static struct pid_entry tgid_attr_stuff[] = {
173 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
174 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
175 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
176 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
179 static struct pid_entry tid_attr_stuff[] = {
180 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
181 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
182 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
183 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
190 static inline struct task_struct *proc_task(struct inode *inode)
192 return PROC_I(inode)->task;
195 static inline int proc_type(struct inode *inode)
197 return PROC_I(inode)->type;
200 int proc_pid_stat(struct task_struct*,char*);
201 int proc_pid_status(struct task_struct*,char*);
202 int proc_pid_statm(struct task_struct*,char*);
203 int proc_pid_cpu(struct task_struct*,char*);
204 int proc_pid_delay(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) && task->state == TASK_STOPPED && \
301 security_ptrace(current,task) == 0))
303 static int may_ptrace_attach(struct task_struct *task)
311 if (((current->uid != task->euid) ||
312 (current->uid != task->suid) ||
313 (current->uid != task->uid) ||
314 (current->gid != task->egid) ||
315 (current->gid != task->sgid) ||
316 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
319 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
321 if (security_ptrace(current, task))
330 static int proc_pid_environ(struct task_struct *task, char * buffer)
333 struct mm_struct *mm = get_task_mm(task);
335 unsigned int len = mm->env_end - mm->env_start;
338 res = access_process_vm(task, mm->env_start, buffer, len, 0);
339 if (!may_ptrace_attach(task))
346 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
350 struct mm_struct *mm = get_task_mm(task);
354 len = mm->arg_end - mm->arg_start;
359 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
361 // If the nul at the end of args has been overwritten, then
362 // assume application is using setproctitle(3).
363 if (res > 0 && buffer[res-1] != '\0') {
364 len = strnlen(buffer, res);
368 len = mm->env_end - mm->env_start;
369 if (len > PAGE_SIZE - res)
370 len = PAGE_SIZE - res;
371 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
372 res = strnlen(buffer, res);
381 static int proc_pid_auxv(struct task_struct *task, char *buffer)
384 struct mm_struct *mm = get_task_mm(task);
386 unsigned int nwords = 0;
389 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
390 res = nwords * sizeof(mm->saved_auxv[0]);
393 memcpy(buffer, mm->saved_auxv, res);
400 #ifdef CONFIG_KALLSYMS
402 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
403 * Returns the resolved symbol. If that fails, simply return the address.
405 static int proc_pid_wchan(struct task_struct *task, char *buffer)
408 const char *sym_name;
409 unsigned long wchan, size, offset;
412 wchan = get_wchan(task);
414 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
416 return sprintf(buffer, "%s", sym_name);
417 return sprintf(buffer, "%lu", wchan);
419 #endif /* CONFIG_KALLSYMS */
421 /************************************************************************/
422 /* Here the fs part begins */
423 /************************************************************************/
425 /* permission checks */
427 static int proc_check_root(struct inode *inode)
429 struct dentry *de, *base, *root;
430 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
433 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
435 read_lock(¤t->fs->lock);
436 our_vfsmnt = mntget(current->fs->rootmnt);
437 base = dget(current->fs->root);
438 read_unlock(¤t->fs->lock);
440 spin_lock(&vfsmount_lock);
444 while (vfsmnt != our_vfsmnt) {
445 if (vfsmnt == vfsmnt->mnt_parent)
447 de = vfsmnt->mnt_mountpoint;
448 vfsmnt = vfsmnt->mnt_parent;
451 if (!is_subdir(de, base))
453 spin_unlock(&vfsmount_lock);
462 spin_unlock(&vfsmount_lock);
467 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
469 if (vfs_permission(inode, mask) != 0)
471 return proc_check_root(inode);
474 extern struct seq_operations proc_pid_maps_op;
475 static int maps_open(struct inode *inode, struct file *file)
477 struct task_struct *task = proc_task(inode);
478 int ret = seq_open(file, &proc_pid_maps_op);
480 struct seq_file *m = file->private_data;
486 static struct file_operations proc_maps_operations = {
490 .release = seq_release,
493 extern struct seq_operations mounts_op;
494 static int mounts_open(struct inode *inode, struct file *file)
496 struct task_struct *task = proc_task(inode);
497 int ret = seq_open(file, &mounts_op);
500 struct seq_file *m = file->private_data;
501 struct namespace *namespace;
503 namespace = task->namespace;
505 get_namespace(namespace);
509 m->private = namespace;
511 seq_release(inode, file);
518 static int mounts_release(struct inode *inode, struct file *file)
520 struct seq_file *m = file->private_data;
521 struct namespace *namespace = m->private;
522 put_namespace(namespace);
523 return seq_release(inode, file);
526 static struct file_operations proc_mounts_operations = {
530 .release = mounts_release,
533 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
535 static ssize_t proc_info_read(struct file * file, char __user * buf,
536 size_t count, loff_t *ppos)
538 struct inode * inode = file->f_dentry->d_inode;
542 struct task_struct *task = proc_task(inode);
544 if (count > PROC_BLOCK_SIZE)
545 count = PROC_BLOCK_SIZE;
546 if (!(page = __get_free_page(GFP_KERNEL)))
549 length = PROC_I(inode)->op.proc_read(task, (char*)page);
555 /* Static 4kB (or whatever) block capacity */
556 if (*ppos >= length) {
560 if (count + *ppos > length)
561 count = length - *ppos;
563 if (copy_to_user(buf, (char *) page + *ppos, count))
571 static struct file_operations proc_info_file_operations = {
572 .read = proc_info_read,
575 static int mem_open(struct inode* inode, struct file* file)
577 file->private_data = (void*)((long)current->self_exec_id);
581 static ssize_t mem_read(struct file * file, char __user * buf,
582 size_t count, loff_t *ppos)
584 struct task_struct *task = proc_task(file->f_dentry->d_inode);
586 unsigned long src = *ppos;
588 struct mm_struct *mm;
590 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
594 page = (char *)__get_free_page(GFP_USER);
600 mm = get_task_mm(task);
606 if (file->private_data != (void*)((long)current->self_exec_id))
612 int this_len, retval;
614 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
615 retval = access_process_vm(task, src, page, this_len, 0);
616 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
622 if (copy_to_user(buf, page, retval)) {
637 free_page((unsigned long) page);
642 #define mem_write NULL
645 /* This is a security hazard */
646 static ssize_t mem_write(struct file * file, const char * buf,
647 size_t count, loff_t *ppos)
651 struct task_struct *task = proc_task(file->f_dentry->d_inode);
652 unsigned long dst = *ppos;
654 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
657 page = (char *)__get_free_page(GFP_USER);
662 int this_len, retval;
664 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
665 if (copy_from_user(page, buf, this_len)) {
669 retval = access_process_vm(task, dst, page, this_len, 1);
681 free_page((unsigned long) page);
686 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
690 file->f_pos = offset;
693 file->f_pos += offset;
698 force_successful_syscall_return();
702 static struct file_operations proc_mem_operations = {
709 static struct inode_operations proc_mem_inode_operations = {
710 .permission = proc_permission,
713 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
715 struct inode *inode = dentry->d_inode;
718 /* We don't need a base pointer in the /proc filesystem */
721 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
723 error = proc_check_root(inode);
727 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
728 nd->last_type = LAST_BIND;
733 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
734 char __user *buffer, int buflen)
736 struct inode * inode;
737 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
743 inode = dentry->d_inode;
744 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
748 len = tmp + PAGE_SIZE - 1 - path;
752 if (copy_to_user(buffer, path, len))
755 free_page((unsigned long)tmp);
759 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
762 struct inode *inode = dentry->d_inode;
764 struct vfsmount *mnt = NULL;
768 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
770 error = proc_check_root(inode);
774 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
778 error = do_proc_readlink(de, mnt, buffer, buflen);
786 static struct inode_operations proc_pid_link_inode_operations = {
787 .readlink = proc_pid_readlink,
788 .follow_link = proc_pid_follow_link
791 static int pid_alive(struct task_struct *p)
793 BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
794 return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
799 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
801 struct inode *inode = filp->f_dentry->d_inode;
802 struct task_struct *p = proc_task(inode);
803 unsigned int fd, tid, ino;
806 struct files_struct * files;
817 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
821 ino = fake_ino(tid, PROC_TID_INO);
822 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
826 files = get_files_struct(p);
829 spin_lock(&files->file_lock);
830 for (fd = filp->f_pos-2;
832 fd++, filp->f_pos++) {
835 if (!fcheck_files(files, fd))
837 spin_unlock(&files->file_lock);
843 buf[j] = '0' + (i % 10);
847 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
848 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
849 spin_lock(&files->file_lock);
852 spin_lock(&files->file_lock);
854 spin_unlock(&files->file_lock);
855 put_files_struct(files);
861 static int proc_pident_readdir(struct file *filp,
862 void *dirent, filldir_t filldir,
863 struct pid_entry *ents, unsigned int nents)
867 struct dentry *dentry = filp->f_dentry;
868 struct inode *inode = dentry->d_inode;
874 if (!pid_alive(proc_task(inode)))
878 pid = proc_task(inode)->pid;
883 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
889 ino = parent_ino(dentry);
890 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
903 if (filldir(dirent, p->name, p->len, filp->f_pos,
904 fake_ino(pid, p->type), p->mode >> 12) < 0)
916 static int proc_tgid_base_readdir(struct file * filp,
917 void * dirent, filldir_t filldir)
919 return proc_pident_readdir(filp,dirent,filldir,
920 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
923 static int proc_tid_base_readdir(struct file * filp,
924 void * dirent, filldir_t filldir)
926 return proc_pident_readdir(filp,dirent,filldir,
927 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
930 /* building an inode */
932 static int task_dumpable(struct task_struct *task)
935 struct mm_struct *mm;
940 dumpable = mm->dumpable;
946 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
948 struct inode * inode;
949 struct proc_inode *ei;
951 /* We need a new inode */
953 inode = new_inode(sb);
960 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
961 inode->i_ino = fake_ino(task->pid, ino);
963 if (!pid_alive(task))
967 * grab the reference to task.
969 get_task_struct(task);
974 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
975 inode->i_uid = task->euid;
976 inode->i_gid = task->egid;
978 inode->i_xid = vx_task_xid(task);
979 security_task_to_inode(task, inode);
993 * Exceptional case: normally we are not allowed to unhash a busy
994 * directory. In this case, however, we can do it - no aliasing problems
995 * due to the way we treat inodes.
997 * Rewrite the inode's ownerships here because the owning task may have
998 * performed a setuid(), etc.
1000 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1002 struct inode *inode = dentry->d_inode;
1003 struct task_struct *task = proc_task(inode);
1005 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1007 /* discard wrong fakeinit */
1009 if (pid_alive(task)) {
1010 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1011 inode->i_uid = task->euid;
1012 inode->i_gid = task->egid;
1017 security_task_to_inode(task, inode);
1025 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1027 struct inode *inode = dentry->d_inode;
1028 struct task_struct *task = proc_task(inode);
1029 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1030 struct files_struct *files;
1032 files = get_files_struct(task);
1034 spin_lock(&files->file_lock);
1035 if (fcheck_files(files, fd)) {
1036 spin_unlock(&files->file_lock);
1037 put_files_struct(files);
1038 if (task_dumpable(task)) {
1039 inode->i_uid = task->euid;
1040 inode->i_gid = task->egid;
1045 security_task_to_inode(task, inode);
1048 spin_unlock(&files->file_lock);
1049 put_files_struct(files);
1055 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1057 struct task_struct *task = proc_task(inode);
1058 spin_lock(&task->proc_lock);
1059 if (task->proc_dentry == dentry)
1060 task->proc_dentry = NULL;
1061 spin_unlock(&task->proc_lock);
1065 static int pid_delete_dentry(struct dentry * dentry)
1067 /* Is the task we represent dead?
1068 * If so, then don't put the dentry on the lru list,
1069 * kill it immediately.
1071 return !pid_alive(proc_task(dentry->d_inode));
1074 static struct dentry_operations tid_fd_dentry_operations =
1076 .d_revalidate = tid_fd_revalidate,
1077 .d_delete = pid_delete_dentry,
1080 static struct dentry_operations pid_dentry_operations =
1082 .d_revalidate = pid_revalidate,
1083 .d_delete = pid_delete_dentry,
1086 static struct dentry_operations pid_base_dentry_operations =
1088 .d_revalidate = pid_revalidate,
1089 .d_iput = pid_base_iput,
1090 .d_delete = pid_delete_dentry,
1095 static unsigned name_to_int(struct dentry *dentry)
1097 const char *name = dentry->d_name.name;
1098 int len = dentry->d_name.len;
1101 if (len > 1 && *name == '0')
1104 unsigned c = *name++ - '0';
1107 if (n >= (~0U-9)/10)
1118 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1120 struct task_struct *task = proc_task(dir);
1121 unsigned fd = name_to_int(dentry);
1123 struct files_struct * files;
1124 struct inode *inode;
1125 struct proc_inode *ei;
1129 if (!pid_alive(task))
1132 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1136 files = get_files_struct(task);
1139 inode->i_mode = S_IFLNK;
1140 spin_lock(&files->file_lock);
1141 file = fcheck_files(files, fd);
1144 if (file->f_mode & 1)
1145 inode->i_mode |= S_IRUSR | S_IXUSR;
1146 if (file->f_mode & 2)
1147 inode->i_mode |= S_IWUSR | S_IXUSR;
1148 spin_unlock(&files->file_lock);
1149 put_files_struct(files);
1150 inode->i_op = &proc_pid_link_inode_operations;
1152 ei->op.proc_get_link = proc_fd_link;
1153 dentry->d_op = &tid_fd_dentry_operations;
1154 d_add(dentry, inode);
1158 spin_unlock(&files->file_lock);
1159 put_files_struct(files);
1163 return ERR_PTR(-ENOENT);
1166 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1167 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1169 static struct file_operations proc_fd_operations = {
1170 .read = generic_read_dir,
1171 .readdir = proc_readfd,
1174 static struct file_operations proc_task_operations = {
1175 .read = generic_read_dir,
1176 .readdir = proc_task_readdir,
1180 * proc directories can do almost nothing..
1182 static struct inode_operations proc_fd_inode_operations = {
1183 .lookup = proc_lookupfd,
1184 .permission = proc_permission,
1187 static struct inode_operations proc_task_inode_operations = {
1188 .lookup = proc_task_lookup,
1189 .permission = proc_permission,
1192 #ifdef CONFIG_SECURITY
1193 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1194 size_t count, loff_t *ppos)
1196 struct inode * inode = file->f_dentry->d_inode;
1200 struct task_struct *task = proc_task(inode);
1202 if (count > PAGE_SIZE)
1204 if (!(page = __get_free_page(GFP_KERNEL)))
1207 length = security_getprocattr(task,
1208 (char*)file->f_dentry->d_name.name,
1209 (void*)page, count);
1214 /* Static 4kB (or whatever) block capacity */
1215 if (*ppos >= length) {
1219 if (count + *ppos > length)
1220 count = length - *ppos;
1221 end = count + *ppos;
1222 if (copy_to_user(buf, (char *) page + *ppos, count))
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 case PROC_TID_VX_INFO:
1407 case PROC_TGID_VX_INFO:
1408 inode->i_fop = &proc_info_file_operations;
1409 ei->op.proc_read = proc_pid_vx_info;
1411 case PROC_TID_IP_INFO:
1412 case PROC_TGID_IP_INFO:
1413 inode->i_fop = &proc_info_file_operations;
1414 ei->op.proc_read = proc_pid_nx_info;
1416 #ifdef CONFIG_DELAY_ACCT
1417 case PROC_TID_DELAY_ACCT:
1418 case PROC_TGID_DELAY_ACCT:
1419 inode->i_fop = &proc_info_file_operations;
1420 ei->op.proc_read = proc_pid_delay;
1424 printk("procfs: impossible type (%d)",p->type);
1426 return ERR_PTR(-EINVAL);
1428 dentry->d_op = &pid_dentry_operations;
1429 d_add(dentry, inode);
1433 return ERR_PTR(error);
1436 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1437 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1440 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1441 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1444 static struct file_operations proc_tgid_base_operations = {
1445 .read = generic_read_dir,
1446 .readdir = proc_tgid_base_readdir,
1449 static struct file_operations proc_tid_base_operations = {
1450 .read = generic_read_dir,
1451 .readdir = proc_tid_base_readdir,
1454 static struct inode_operations proc_tgid_base_inode_operations = {
1455 .lookup = proc_tgid_base_lookup,
1458 static struct inode_operations proc_tid_base_inode_operations = {
1459 .lookup = proc_tid_base_lookup,
1462 #ifdef CONFIG_SECURITY
1463 static int proc_tgid_attr_readdir(struct file * filp,
1464 void * dirent, filldir_t filldir)
1466 return proc_pident_readdir(filp,dirent,filldir,
1467 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1470 static int proc_tid_attr_readdir(struct file * filp,
1471 void * dirent, filldir_t filldir)
1473 return proc_pident_readdir(filp,dirent,filldir,
1474 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1477 static struct file_operations proc_tgid_attr_operations = {
1478 .read = generic_read_dir,
1479 .readdir = proc_tgid_attr_readdir,
1482 static struct file_operations proc_tid_attr_operations = {
1483 .read = generic_read_dir,
1484 .readdir = proc_tid_attr_readdir,
1487 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1488 struct dentry *dentry, struct nameidata *nd)
1490 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1493 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1494 struct dentry *dentry, struct nameidata *nd)
1496 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1499 static struct inode_operations proc_tgid_attr_inode_operations = {
1500 .lookup = proc_tgid_attr_lookup,
1503 static struct inode_operations proc_tid_attr_inode_operations = {
1504 .lookup = proc_tid_attr_lookup,
1511 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1515 sprintf(tmp, "%d", current->tgid);
1516 return vfs_readlink(dentry,buffer,buflen,tmp);
1519 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1522 sprintf(tmp, "%d", current->tgid);
1523 return vfs_follow_link(nd,tmp);
1526 static struct inode_operations proc_self_inode_operations = {
1527 .readlink = proc_self_readlink,
1528 .follow_link = proc_self_follow_link,
1532 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1533 * @p: task that should be flushed.
1535 * Drops the /proc/<pid> dcache entry from the hash chains.
1537 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1538 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1539 * if the pid value is immediately reused. This is enforced by
1540 * - caller must acquire spin_lock(p->proc_lock)
1541 * - must be called before detach_pid()
1542 * - proc_pid_lookup acquires proc_lock, and checks that
1543 * the target is not dead by looking at the attach count
1547 struct dentry *proc_pid_unhash(struct task_struct *p)
1549 struct dentry *proc_dentry;
1551 proc_dentry = p->proc_dentry;
1552 if (proc_dentry != NULL) {
1554 spin_lock(&dcache_lock);
1555 if (!d_unhashed(proc_dentry)) {
1556 dget_locked(proc_dentry);
1557 __d_drop(proc_dentry);
1560 spin_unlock(&dcache_lock);
1566 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1567 * @proc_entry: directoy to prune.
1569 * Shrink the /proc directory that was used by the just killed thread.
1572 void proc_pid_flush(struct dentry *proc_dentry)
1575 if(proc_dentry != NULL) {
1576 shrink_dcache_parent(proc_dentry);
1582 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1584 struct task_struct *task;
1585 struct inode *inode;
1586 struct proc_inode *ei;
1590 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1591 inode = new_inode(dir->i_sb);
1593 return ERR_PTR(-ENOMEM);
1595 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1596 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1598 inode->i_mode = S_IFLNK|S_IRWXUGO;
1599 inode->i_uid = inode->i_gid = 0;
1601 inode->i_op = &proc_self_inode_operations;
1602 d_add(dentry, inode);
1605 tgid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1609 read_lock(&tasklist_lock);
1610 task = find_task_by_pid(tgid);
1612 get_task_struct(task);
1613 read_unlock(&tasklist_lock);
1618 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1619 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1622 put_task_struct(task);
1625 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1626 inode->i_op = &proc_tgid_base_inode_operations;
1627 inode->i_fop = &proc_tgid_base_operations;
1629 inode->i_flags|=S_IMMUTABLE;
1631 dentry->d_op = &pid_base_dentry_operations;
1634 d_add(dentry, inode);
1635 spin_lock(&task->proc_lock);
1636 task->proc_dentry = dentry;
1637 if (!pid_alive(task)) {
1638 dentry = proc_pid_unhash(task);
1641 spin_unlock(&task->proc_lock);
1643 put_task_struct(task);
1645 proc_pid_flush(dentry);
1650 return ERR_PTR(-ENOENT);
1654 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1656 struct task_struct *task;
1657 struct task_struct *leader = proc_task(dir);
1658 struct inode *inode;
1661 tid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1665 /* handle fakeinit */
1667 read_lock(&tasklist_lock);
1668 task = find_task_by_pid(tid);
1670 get_task_struct(task);
1671 read_unlock(&tasklist_lock);
1674 if (leader->tgid != task->tgid)
1678 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1679 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1683 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1684 inode->i_op = &proc_tid_base_inode_operations;
1685 inode->i_fop = &proc_tid_base_operations;
1687 inode->i_flags|=S_IMMUTABLE;
1689 dentry->d_op = &pid_base_dentry_operations;
1691 d_add(dentry, inode);
1693 put_task_struct(task);
1696 put_task_struct(task);
1698 return ERR_PTR(-ENOENT);
1701 #define PROC_NUMBUF 10
1702 #define PROC_MAXPIDS 20
1705 * Get a few tgid's to return for filldir - we need to hold the
1706 * tasklist lock while doing this, and we must release it before
1707 * we actually do the filldir itself, so we use a temp buffer..
1709 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1711 struct task_struct *p;
1715 read_lock(&tasklist_lock);
1718 p = find_task_by_pid(version);
1719 if (!thread_group_leader(p))
1726 p = next_task(&init_task);
1728 for ( ; p != &init_task; p = next_task(p)) {
1733 if (!vx_check(vx_task_xid(p), VX_WATCH|VX_IDENT))
1737 tgids[nr_tgids] = vx_map_tgid(current->vx_info, tgid);
1739 if (nr_tgids >= PROC_MAXPIDS)
1742 read_unlock(&tasklist_lock);
1747 * Get a few tid's to return for filldir - we need to hold the
1748 * tasklist lock while doing this, and we must release it before
1749 * we actually do the filldir itself, so we use a temp buffer..
1751 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1753 struct task_struct *leader_task = proc_task(dir);
1754 struct task_struct *task = leader_task;
1758 read_lock(&tasklist_lock);
1760 * The starting point task (leader_task) might be an already
1761 * unlinked task, which cannot be used to access the task-list
1762 * via next_thread().
1764 if (pid_alive(task)) do {
1765 int tid = task->pid;
1767 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1771 tids[nr_tids] = vx_map_tgid(current->vx_info, tid);
1773 if (nr_tids >= PROC_MAXPIDS)
1775 } while ((task = next_thread(task)) != leader_task);
1776 read_unlock(&tasklist_lock);
1780 /* for the /proc/ directory itself, after non-process stuff has been done */
1781 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1783 unsigned int tgid_array[PROC_MAXPIDS];
1784 char buf[PROC_NUMBUF];
1785 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1786 unsigned int nr_tgids, i;
1789 ino_t ino = fake_ino(0,PROC_TGID_INO);
1790 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1797 * f_version caches the last tgid which was returned from readdir
1799 nr_tgids = get_tgid_list(nr, filp->f_version, tgid_array);
1801 for (i = 0; i < nr_tgids; i++) {
1802 int tgid = tgid_array[i];
1803 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1804 unsigned long j = PROC_NUMBUF;
1807 buf[--j] = '0' + (tgid % 10);
1808 while ((tgid /= 10) != 0);
1810 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1811 filp->f_version = tgid;
1819 /* for the /proc/TGID/task/ directories */
1820 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1822 unsigned int tid_array[PROC_MAXPIDS];
1823 char buf[PROC_NUMBUF];
1824 unsigned int nr_tids, i;
1825 struct dentry *dentry = filp->f_dentry;
1826 struct inode *inode = dentry->d_inode;
1827 struct task_struct *task = proc_task(inode);
1828 int retval = -ENOENT;
1830 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1832 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1834 if (!pid_alive(task))
1841 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1846 ino = parent_ino(dentry);
1847 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1853 nr_tids = get_tid_list(pos, tid_array, inode);
1855 for (i = 0; i < nr_tids; i++) {
1856 unsigned long j = PROC_NUMBUF;
1857 int tid = tid_array[i];
1859 ino = fake_ino(tid,PROC_TID_INO);
1862 buf[--j] = '0' + (tid % 10);
1863 while ((tid /= 10) != 0);
1865 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)