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_context.h>
36 #include <linux/vs_network.h>
37 #include <linux/vs_cvirt.h>
41 * For hysterical raisins we keep the same inumbers as in the old procfs.
42 * Feel free to change the macro below - just keep the range distinct from
43 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
44 * As soon as we'll get a separate superblock we will be able to forget
45 * about magical ranges too.
48 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
50 enum pid_directory_inos {
67 #ifdef CONFIG_SECURITY
69 PROC_TGID_ATTR_CURRENT,
72 PROC_TGID_ATTR_FSCREATE,
92 #ifdef CONFIG_SECURITY
94 PROC_TID_ATTR_CURRENT,
97 PROC_TID_ATTR_FSCREATE,
101 #ifdef CONFIG_DELAY_ACCT
103 PROC_TGID_DELAY_ACCT,
105 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
115 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
117 static struct pid_entry tgid_base_stuff[] = {
118 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
119 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
120 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
121 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
122 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
123 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
124 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
125 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
126 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
127 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
128 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
129 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
130 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
131 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
132 #ifdef CONFIG_SECURITY
133 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
135 #ifdef CONFIG_DELAY_ACCT
136 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
138 #ifdef CONFIG_KALLSYMS
139 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
141 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
142 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
145 static struct pid_entry tid_base_stuff[] = {
146 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
147 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
148 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
149 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
150 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
151 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
152 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
153 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
154 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
155 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
156 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
157 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
158 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
159 #ifdef CONFIG_SECURITY
160 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
162 #ifdef CONFIG_DELAY_ACCT
163 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
165 #ifdef CONFIG_KALLSYMS
166 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
168 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
169 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
173 #ifdef CONFIG_SECURITY
174 static struct pid_entry tgid_attr_stuff[] = {
175 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
176 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
177 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
178 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
181 static struct pid_entry tid_attr_stuff[] = {
182 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
183 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
184 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
185 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
192 static inline struct task_struct *proc_task(struct inode *inode)
194 return PROC_I(inode)->task;
197 static inline int proc_type(struct inode *inode)
199 return PROC_I(inode)->type;
202 int proc_pid_stat(struct task_struct*,char*);
203 int proc_pid_status(struct task_struct*,char*);
204 int proc_pid_statm(struct task_struct*,char*);
205 int proc_pid_cpu(struct task_struct*,char*);
206 int proc_pid_delay(struct task_struct*,char*);
208 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
210 struct task_struct *task = proc_task(inode);
211 struct files_struct *files;
213 int fd = proc_type(inode) - PROC_TID_FD_DIR;
215 files = get_files_struct(task);
217 spin_lock(&files->file_lock);
218 file = fcheck_files(files, fd);
220 *mnt = mntget(file->f_vfsmnt);
221 *dentry = dget(file->f_dentry);
222 spin_unlock(&files->file_lock);
223 put_files_struct(files);
226 spin_unlock(&files->file_lock);
227 put_files_struct(files);
232 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
234 struct vm_area_struct * vma;
235 int result = -ENOENT;
236 struct task_struct *task = proc_task(inode);
237 struct mm_struct * mm = get_task_mm(task);
241 down_read(&mm->mmap_sem);
244 if ((vma->vm_flags & VM_EXECUTABLE) &&
246 *mnt = mntget(vma->vm_file->f_vfsmnt);
247 *dentry = dget(vma->vm_file->f_dentry);
253 up_read(&mm->mmap_sem);
259 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
261 struct fs_struct *fs;
262 int result = -ENOENT;
263 task_lock(proc_task(inode));
264 fs = proc_task(inode)->fs;
266 atomic_inc(&fs->count);
267 task_unlock(proc_task(inode));
269 read_lock(&fs->lock);
270 *mnt = mntget(fs->pwdmnt);
271 *dentry = dget(fs->pwd);
272 read_unlock(&fs->lock);
279 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
281 struct fs_struct *fs;
282 int result = -ENOENT;
283 task_lock(proc_task(inode));
284 fs = proc_task(inode)->fs;
286 atomic_inc(&fs->count);
287 task_unlock(proc_task(inode));
289 read_lock(&fs->lock);
290 *mnt = mntget(fs->rootmnt);
291 *dentry = dget(fs->root);
292 read_unlock(&fs->lock);
299 #define MAY_PTRACE(task) \
300 (task == current || \
301 (task->parent == current && \
302 (task->ptrace & PT_PTRACED) && task->state == TASK_STOPPED && \
303 security_ptrace(current,task) == 0))
305 static int may_ptrace_attach(struct task_struct *task)
313 if (((current->uid != task->euid) ||
314 (current->uid != task->suid) ||
315 (current->uid != task->uid) ||
316 (current->gid != task->egid) ||
317 (current->gid != task->sgid) ||
318 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
321 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
323 if (security_ptrace(current, task))
332 static int proc_pid_environ(struct task_struct *task, char * buffer)
335 struct mm_struct *mm = get_task_mm(task);
337 unsigned int len = mm->env_end - mm->env_start;
340 res = access_process_vm(task, mm->env_start, buffer, len, 0);
341 if (!may_ptrace_attach(task))
348 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
352 struct mm_struct *mm = get_task_mm(task);
356 len = mm->arg_end - mm->arg_start;
361 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
363 // If the nul at the end of args has been overwritten, then
364 // assume application is using setproctitle(3).
365 if (res > 0 && buffer[res-1] != '\0') {
366 len = strnlen(buffer, res);
370 len = mm->env_end - mm->env_start;
371 if (len > PAGE_SIZE - res)
372 len = PAGE_SIZE - res;
373 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
374 res = strnlen(buffer, res);
383 static int proc_pid_auxv(struct task_struct *task, char *buffer)
386 struct mm_struct *mm = get_task_mm(task);
388 unsigned int nwords = 0;
391 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
392 res = nwords * sizeof(mm->saved_auxv[0]);
395 memcpy(buffer, mm->saved_auxv, res);
402 #ifdef CONFIG_KALLSYMS
404 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
405 * Returns the resolved symbol. If that fails, simply return the address.
407 static int proc_pid_wchan(struct task_struct *task, char *buffer)
410 const char *sym_name;
411 unsigned long wchan, size, offset;
414 wchan = get_wchan(task);
416 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
418 return sprintf(buffer, "%s", sym_name);
419 return sprintf(buffer, "%lu", wchan);
421 #endif /* CONFIG_KALLSYMS */
423 /************************************************************************/
424 /* Here the fs part begins */
425 /************************************************************************/
427 /* permission checks */
429 static int proc_check_root(struct inode *inode)
431 struct dentry *de, *base, *root;
432 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
435 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
437 read_lock(¤t->fs->lock);
438 our_vfsmnt = mntget(current->fs->rootmnt);
439 base = dget(current->fs->root);
440 read_unlock(¤t->fs->lock);
442 spin_lock(&vfsmount_lock);
446 while (vfsmnt != our_vfsmnt) {
447 if (vfsmnt == vfsmnt->mnt_parent)
449 de = vfsmnt->mnt_mountpoint;
450 vfsmnt = vfsmnt->mnt_parent;
453 if (!is_subdir(de, base))
455 spin_unlock(&vfsmount_lock);
464 spin_unlock(&vfsmount_lock);
469 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
471 if (vfs_permission(inode, mask) != 0)
473 return proc_check_root(inode);
476 extern struct seq_operations proc_pid_maps_op;
477 static int maps_open(struct inode *inode, struct file *file)
479 struct task_struct *task = proc_task(inode);
480 int ret = seq_open(file, &proc_pid_maps_op);
482 struct seq_file *m = file->private_data;
488 static struct file_operations proc_maps_operations = {
492 .release = seq_release,
495 extern struct seq_operations mounts_op;
496 static int mounts_open(struct inode *inode, struct file *file)
498 struct task_struct *task = proc_task(inode);
499 int ret = seq_open(file, &mounts_op);
502 struct seq_file *m = file->private_data;
503 struct namespace *namespace;
505 namespace = task->namespace;
507 get_namespace(namespace);
511 m->private = namespace;
513 seq_release(inode, file);
520 static int mounts_release(struct inode *inode, struct file *file)
522 struct seq_file *m = file->private_data;
523 struct namespace *namespace = m->private;
524 put_namespace(namespace);
525 return seq_release(inode, file);
528 static struct file_operations proc_mounts_operations = {
532 .release = mounts_release,
535 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
537 static ssize_t proc_info_read(struct file * file, char __user * buf,
538 size_t count, loff_t *ppos)
540 struct inode * inode = file->f_dentry->d_inode;
544 struct task_struct *task = proc_task(inode);
546 if (count > PROC_BLOCK_SIZE)
547 count = PROC_BLOCK_SIZE;
548 if (!(page = __get_free_page(GFP_KERNEL)))
551 length = PROC_I(inode)->op.proc_read(task, (char*)page);
557 /* Static 4kB (or whatever) block capacity */
558 if (*ppos >= length) {
562 if (count + *ppos > length)
563 count = length - *ppos;
565 if (copy_to_user(buf, (char *) page + *ppos, count))
573 static struct file_operations proc_info_file_operations = {
574 .read = proc_info_read,
577 static int mem_open(struct inode* inode, struct file* file)
579 file->private_data = (void*)((long)current->self_exec_id);
583 static ssize_t mem_read(struct file * file, char __user * buf,
584 size_t count, loff_t *ppos)
586 struct task_struct *task = proc_task(file->f_dentry->d_inode);
588 unsigned long src = *ppos;
590 struct mm_struct *mm;
592 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
596 page = (char *)__get_free_page(GFP_USER);
602 mm = get_task_mm(task);
608 if (file->private_data != (void*)((long)current->self_exec_id))
614 int this_len, retval;
616 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
617 retval = access_process_vm(task, src, page, this_len, 0);
618 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
624 if (copy_to_user(buf, page, retval)) {
639 free_page((unsigned long) page);
644 #define mem_write NULL
647 /* This is a security hazard */
648 static ssize_t mem_write(struct file * file, const char * buf,
649 size_t count, loff_t *ppos)
653 struct task_struct *task = proc_task(file->f_dentry->d_inode);
654 unsigned long dst = *ppos;
656 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
659 page = (char *)__get_free_page(GFP_USER);
664 int this_len, retval;
666 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
667 if (copy_from_user(page, buf, this_len)) {
671 retval = access_process_vm(task, dst, page, this_len, 1);
683 free_page((unsigned long) page);
688 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
692 file->f_pos = offset;
695 file->f_pos += offset;
700 force_successful_syscall_return();
704 static struct file_operations proc_mem_operations = {
711 static struct inode_operations proc_mem_inode_operations = {
712 .permission = proc_permission,
715 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
717 struct inode *inode = dentry->d_inode;
720 /* We don't need a base pointer in the /proc filesystem */
723 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
725 error = proc_check_root(inode);
729 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
730 nd->last_type = LAST_BIND;
735 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
736 char __user *buffer, int buflen)
738 struct inode * inode;
739 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
745 inode = dentry->d_inode;
746 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
750 len = tmp + PAGE_SIZE - 1 - path;
754 if (copy_to_user(buffer, path, len))
757 free_page((unsigned long)tmp);
761 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
764 struct inode *inode = dentry->d_inode;
766 struct vfsmount *mnt = NULL;
770 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
772 error = proc_check_root(inode);
776 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
780 error = do_proc_readlink(de, mnt, buffer, buflen);
788 static struct inode_operations proc_pid_link_inode_operations = {
789 .readlink = proc_pid_readlink,
790 .follow_link = proc_pid_follow_link
793 static int pid_alive(struct task_struct *p)
795 BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
796 return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
801 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
803 struct inode *inode = filp->f_dentry->d_inode;
804 struct task_struct *p = proc_task(inode);
805 unsigned int fd, tid, ino;
808 struct files_struct * files;
819 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
823 ino = fake_ino(tid, PROC_TID_INO);
824 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
828 files = get_files_struct(p);
831 spin_lock(&files->file_lock);
832 for (fd = filp->f_pos-2;
834 fd++, filp->f_pos++) {
837 if (!fcheck_files(files, fd))
839 spin_unlock(&files->file_lock);
845 buf[j] = '0' + (i % 10);
849 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
850 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
851 spin_lock(&files->file_lock);
854 spin_lock(&files->file_lock);
856 spin_unlock(&files->file_lock);
857 put_files_struct(files);
863 static int proc_pident_readdir(struct file *filp,
864 void *dirent, filldir_t filldir,
865 struct pid_entry *ents, unsigned int nents)
869 struct dentry *dentry = filp->f_dentry;
870 struct inode *inode = dentry->d_inode;
876 if (!pid_alive(proc_task(inode)))
880 pid = proc_task(inode)->pid;
885 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
891 ino = parent_ino(dentry);
892 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
905 if (filldir(dirent, p->name, p->len, filp->f_pos,
906 fake_ino(pid, p->type), p->mode >> 12) < 0)
918 static int proc_tgid_base_readdir(struct file * filp,
919 void * dirent, filldir_t filldir)
921 return proc_pident_readdir(filp,dirent,filldir,
922 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
925 static int proc_tid_base_readdir(struct file * filp,
926 void * dirent, filldir_t filldir)
928 return proc_pident_readdir(filp,dirent,filldir,
929 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
932 /* building an inode */
934 static int task_dumpable(struct task_struct *task)
937 struct mm_struct *mm;
942 dumpable = mm->dumpable;
948 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
950 struct inode * inode;
951 struct proc_inode *ei;
953 /* We need a new inode */
955 inode = new_inode(sb);
962 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
963 inode->i_ino = fake_ino(task->pid, ino);
965 if (!pid_alive(task))
969 * grab the reference to task.
971 get_task_struct(task);
976 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
977 inode->i_uid = task->euid;
978 inode->i_gid = task->egid;
980 inode->i_xid = vx_task_xid(task);
981 security_task_to_inode(task, inode);
995 * Exceptional case: normally we are not allowed to unhash a busy
996 * directory. In this case, however, we can do it - no aliasing problems
997 * due to the way we treat inodes.
999 * Rewrite the inode's ownerships here because the owning task may have
1000 * performed a setuid(), etc.
1002 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1004 struct inode *inode = dentry->d_inode;
1005 struct task_struct *task = proc_task(inode);
1007 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1009 /* discard wrong fakeinit */
1011 if (pid_alive(task)) {
1012 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1013 inode->i_uid = task->euid;
1014 inode->i_gid = task->egid;
1019 security_task_to_inode(task, inode);
1027 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1029 struct inode *inode = dentry->d_inode;
1030 struct task_struct *task = proc_task(inode);
1031 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1032 struct files_struct *files;
1034 files = get_files_struct(task);
1036 spin_lock(&files->file_lock);
1037 if (fcheck_files(files, fd)) {
1038 spin_unlock(&files->file_lock);
1039 put_files_struct(files);
1040 if (task_dumpable(task)) {
1041 inode->i_uid = task->euid;
1042 inode->i_gid = task->egid;
1047 security_task_to_inode(task, inode);
1050 spin_unlock(&files->file_lock);
1051 put_files_struct(files);
1057 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1059 struct task_struct *task = proc_task(inode);
1060 spin_lock(&task->proc_lock);
1061 if (task->proc_dentry == dentry)
1062 task->proc_dentry = NULL;
1063 spin_unlock(&task->proc_lock);
1067 static int pid_delete_dentry(struct dentry * dentry)
1069 /* Is the task we represent dead?
1070 * If so, then don't put the dentry on the lru list,
1071 * kill it immediately.
1073 return !pid_alive(proc_task(dentry->d_inode));
1076 static struct dentry_operations tid_fd_dentry_operations =
1078 .d_revalidate = tid_fd_revalidate,
1079 .d_delete = pid_delete_dentry,
1082 static struct dentry_operations pid_dentry_operations =
1084 .d_revalidate = pid_revalidate,
1085 .d_delete = pid_delete_dentry,
1088 static struct dentry_operations pid_base_dentry_operations =
1090 .d_revalidate = pid_revalidate,
1091 .d_iput = pid_base_iput,
1092 .d_delete = pid_delete_dentry,
1097 static unsigned name_to_int(struct dentry *dentry)
1099 const char *name = dentry->d_name.name;
1100 int len = dentry->d_name.len;
1103 if (len > 1 && *name == '0')
1106 unsigned c = *name++ - '0';
1109 if (n >= (~0U-9)/10)
1120 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1122 struct task_struct *task = proc_task(dir);
1123 unsigned fd = name_to_int(dentry);
1125 struct files_struct * files;
1126 struct inode *inode;
1127 struct proc_inode *ei;
1131 if (!pid_alive(task))
1134 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1138 files = get_files_struct(task);
1141 inode->i_mode = S_IFLNK;
1142 spin_lock(&files->file_lock);
1143 file = fcheck_files(files, fd);
1146 if (file->f_mode & 1)
1147 inode->i_mode |= S_IRUSR | S_IXUSR;
1148 if (file->f_mode & 2)
1149 inode->i_mode |= S_IWUSR | S_IXUSR;
1150 spin_unlock(&files->file_lock);
1151 put_files_struct(files);
1152 inode->i_op = &proc_pid_link_inode_operations;
1154 ei->op.proc_get_link = proc_fd_link;
1155 dentry->d_op = &tid_fd_dentry_operations;
1156 d_add(dentry, inode);
1160 spin_unlock(&files->file_lock);
1161 put_files_struct(files);
1165 return ERR_PTR(-ENOENT);
1168 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1169 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1171 static struct file_operations proc_fd_operations = {
1172 .read = generic_read_dir,
1173 .readdir = proc_readfd,
1176 static struct file_operations proc_task_operations = {
1177 .read = generic_read_dir,
1178 .readdir = proc_task_readdir,
1182 * proc directories can do almost nothing..
1184 static struct inode_operations proc_fd_inode_operations = {
1185 .lookup = proc_lookupfd,
1186 .permission = proc_permission,
1189 static struct inode_operations proc_task_inode_operations = {
1190 .lookup = proc_task_lookup,
1191 .permission = proc_permission,
1194 #ifdef CONFIG_SECURITY
1195 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1196 size_t count, loff_t *ppos)
1198 struct inode * inode = file->f_dentry->d_inode;
1202 struct task_struct *task = proc_task(inode);
1204 if (count > PAGE_SIZE)
1206 if (!(page = __get_free_page(GFP_KERNEL)))
1209 length = security_getprocattr(task,
1210 (char*)file->f_dentry->d_name.name,
1211 (void*)page, count);
1216 /* Static 4kB (or whatever) block capacity */
1217 if (*ppos >= length) {
1221 if (count + *ppos > length)
1222 count = length - *ppos;
1223 end = count + *ppos;
1224 if (copy_to_user(buf, (char *) page + *ppos, count))
1232 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1233 size_t count, loff_t *ppos)
1235 struct inode * inode = file->f_dentry->d_inode;
1238 struct task_struct *task = proc_task(inode);
1240 if (count > PAGE_SIZE)
1243 /* No partial writes. */
1246 page = (char*)__get_free_page(GFP_USER);
1250 if (copy_from_user(page, buf, count))
1253 length = security_setprocattr(task,
1254 (char*)file->f_dentry->d_name.name,
1255 (void*)page, count);
1257 free_page((unsigned long) page);
1261 static struct file_operations proc_pid_attr_operations = {
1262 .read = proc_pid_attr_read,
1263 .write = proc_pid_attr_write,
1266 static struct file_operations proc_tid_attr_operations;
1267 static struct inode_operations proc_tid_attr_inode_operations;
1268 static struct file_operations proc_tgid_attr_operations;
1269 static struct inode_operations proc_tgid_attr_inode_operations;
1273 static struct dentry *proc_pident_lookup(struct inode *dir,
1274 struct dentry *dentry,
1275 struct pid_entry *ents)
1277 struct inode *inode;
1279 struct task_struct *task = proc_task(dir);
1280 struct pid_entry *p;
1281 struct proc_inode *ei;
1286 if (!pid_alive(task))
1289 for (p = ents; p->name; p++) {
1290 if (p->len != dentry->d_name.len)
1292 if (!memcmp(dentry->d_name.name, p->name, p->len))
1299 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1304 inode->i_mode = p->mode;
1306 * Yes, it does not scale. And it should not. Don't add
1307 * new entries into /proc/<tgid>/ without very good reasons.
1310 case PROC_TGID_TASK:
1312 inode->i_op = &proc_task_inode_operations;
1313 inode->i_fop = &proc_task_operations;
1318 inode->i_op = &proc_fd_inode_operations;
1319 inode->i_fop = &proc_fd_operations;
1323 inode->i_op = &proc_pid_link_inode_operations;
1324 ei->op.proc_get_link = proc_exe_link;
1328 inode->i_op = &proc_pid_link_inode_operations;
1329 ei->op.proc_get_link = proc_cwd_link;
1332 case PROC_TGID_ROOT:
1333 inode->i_op = &proc_pid_link_inode_operations;
1334 ei->op.proc_get_link = proc_root_link;
1336 case PROC_TID_ENVIRON:
1337 case PROC_TGID_ENVIRON:
1338 inode->i_fop = &proc_info_file_operations;
1339 ei->op.proc_read = proc_pid_environ;
1342 case PROC_TGID_AUXV:
1343 inode->i_fop = &proc_info_file_operations;
1344 ei->op.proc_read = proc_pid_auxv;
1346 case PROC_TID_STATUS:
1347 case PROC_TGID_STATUS:
1348 inode->i_fop = &proc_info_file_operations;
1349 ei->op.proc_read = proc_pid_status;
1352 case PROC_TGID_STAT:
1353 inode->i_fop = &proc_info_file_operations;
1354 ei->op.proc_read = proc_pid_stat;
1356 case PROC_TID_CMDLINE:
1357 case PROC_TGID_CMDLINE:
1358 inode->i_fop = &proc_info_file_operations;
1359 ei->op.proc_read = proc_pid_cmdline;
1361 case PROC_TID_STATM:
1362 case PROC_TGID_STATM:
1363 inode->i_fop = &proc_info_file_operations;
1364 ei->op.proc_read = proc_pid_statm;
1367 case PROC_TGID_MAPS:
1368 inode->i_fop = &proc_maps_operations;
1372 inode->i_op = &proc_mem_inode_operations;
1373 inode->i_fop = &proc_mem_operations;
1375 case PROC_TID_MOUNTS:
1376 case PROC_TGID_MOUNTS:
1377 inode->i_fop = &proc_mounts_operations;
1379 #ifdef CONFIG_SECURITY
1382 inode->i_op = &proc_tid_attr_inode_operations;
1383 inode->i_fop = &proc_tid_attr_operations;
1385 case PROC_TGID_ATTR:
1387 inode->i_op = &proc_tgid_attr_inode_operations;
1388 inode->i_fop = &proc_tgid_attr_operations;
1390 case PROC_TID_ATTR_CURRENT:
1391 case PROC_TGID_ATTR_CURRENT:
1392 case PROC_TID_ATTR_PREV:
1393 case PROC_TGID_ATTR_PREV:
1394 case PROC_TID_ATTR_EXEC:
1395 case PROC_TGID_ATTR_EXEC:
1396 case PROC_TID_ATTR_FSCREATE:
1397 case PROC_TGID_ATTR_FSCREATE:
1398 inode->i_fop = &proc_pid_attr_operations;
1401 #ifdef CONFIG_KALLSYMS
1402 case PROC_TID_WCHAN:
1403 case PROC_TGID_WCHAN:
1404 inode->i_fop = &proc_info_file_operations;
1405 ei->op.proc_read = proc_pid_wchan;
1408 case PROC_TID_VX_INFO:
1409 case PROC_TGID_VX_INFO:
1410 inode->i_fop = &proc_info_file_operations;
1411 ei->op.proc_read = proc_pid_vx_info;
1413 case PROC_TID_IP_INFO:
1414 case PROC_TGID_IP_INFO:
1415 inode->i_fop = &proc_info_file_operations;
1416 ei->op.proc_read = proc_pid_nx_info;
1418 #ifdef CONFIG_DELAY_ACCT
1419 case PROC_TID_DELAY_ACCT:
1420 case PROC_TGID_DELAY_ACCT:
1421 inode->i_fop = &proc_info_file_operations;
1422 ei->op.proc_read = proc_pid_delay;
1426 printk("procfs: impossible type (%d)",p->type);
1428 return ERR_PTR(-EINVAL);
1430 dentry->d_op = &pid_dentry_operations;
1431 d_add(dentry, inode);
1435 return ERR_PTR(error);
1438 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1439 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1442 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1443 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1446 static struct file_operations proc_tgid_base_operations = {
1447 .read = generic_read_dir,
1448 .readdir = proc_tgid_base_readdir,
1451 static struct file_operations proc_tid_base_operations = {
1452 .read = generic_read_dir,
1453 .readdir = proc_tid_base_readdir,
1456 static struct inode_operations proc_tgid_base_inode_operations = {
1457 .lookup = proc_tgid_base_lookup,
1460 static struct inode_operations proc_tid_base_inode_operations = {
1461 .lookup = proc_tid_base_lookup,
1464 #ifdef CONFIG_SECURITY
1465 static int proc_tgid_attr_readdir(struct file * filp,
1466 void * dirent, filldir_t filldir)
1468 return proc_pident_readdir(filp,dirent,filldir,
1469 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1472 static int proc_tid_attr_readdir(struct file * filp,
1473 void * dirent, filldir_t filldir)
1475 return proc_pident_readdir(filp,dirent,filldir,
1476 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1479 static struct file_operations proc_tgid_attr_operations = {
1480 .read = generic_read_dir,
1481 .readdir = proc_tgid_attr_readdir,
1484 static struct file_operations proc_tid_attr_operations = {
1485 .read = generic_read_dir,
1486 .readdir = proc_tid_attr_readdir,
1489 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1490 struct dentry *dentry, struct nameidata *nd)
1492 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1495 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1496 struct dentry *dentry, struct nameidata *nd)
1498 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1501 static struct inode_operations proc_tgid_attr_inode_operations = {
1502 .lookup = proc_tgid_attr_lookup,
1505 static struct inode_operations proc_tid_attr_inode_operations = {
1506 .lookup = proc_tid_attr_lookup,
1513 static int proc_self_readlink(struct dentry *dentry, char *buffer, int buflen)
1516 sprintf(tmp, "%d", current->tgid);
1517 return vfs_readlink(dentry,buffer,buflen,tmp);
1520 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1523 sprintf(tmp, "%d", current->tgid);
1524 return vfs_follow_link(nd,tmp);
1527 static struct inode_operations proc_self_inode_operations = {
1528 .readlink = proc_self_readlink,
1529 .follow_link = proc_self_follow_link,
1533 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1534 * @p: task that should be flushed.
1536 * Drops the /proc/<pid> dcache entry from the hash chains.
1538 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1539 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1540 * if the pid value is immediately reused. This is enforced by
1541 * - caller must acquire spin_lock(p->proc_lock)
1542 * - must be called before detach_pid()
1543 * - proc_pid_lookup acquires proc_lock, and checks that
1544 * the target is not dead by looking at the attach count
1548 struct dentry *proc_pid_unhash(struct task_struct *p)
1550 struct dentry *proc_dentry;
1552 proc_dentry = p->proc_dentry;
1553 if (proc_dentry != NULL) {
1555 spin_lock(&dcache_lock);
1556 if (!d_unhashed(proc_dentry)) {
1557 dget_locked(proc_dentry);
1558 __d_drop(proc_dentry);
1561 spin_unlock(&dcache_lock);
1567 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1568 * @proc_entry: directoy to prune.
1570 * Shrink the /proc directory that was used by the just killed thread.
1573 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;
1806 do buf[--j] = '0' + (tgid % 10); while (tgid/=10);
1808 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1809 filp->f_version = tgid;
1817 /* for the /proc/TGID/task/ directories */
1818 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1820 unsigned int tid_array[PROC_MAXPIDS];
1821 char buf[PROC_NUMBUF];
1822 unsigned int nr_tids, i;
1823 struct dentry *dentry = filp->f_dentry;
1824 struct inode *inode = dentry->d_inode;
1825 struct task_struct *task = proc_task(inode);
1826 int retval = -ENOENT;
1828 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1830 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1832 if (!pid_alive(task))
1839 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1844 ino = parent_ino(dentry);
1845 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1851 nr_tids = get_tid_list(pos, tid_array, inode);
1853 for (i = 0; i < nr_tids; i++) {
1854 unsigned long j = PROC_NUMBUF;
1855 int tid = tid_array[i];
1857 ino = fake_ino(tid,PROC_TID_INO);
1860 buf[--j] = '0' + (tid % 10);
1863 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)