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/ninline.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_SECURITY
66 PROC_TGID_ATTR_CURRENT,
69 PROC_TGID_ATTR_FSCREATE,
89 #ifdef CONFIG_SECURITY
91 PROC_TID_ATTR_CURRENT,
94 PROC_TID_ATTR_FSCREATE,
98 #ifdef CONFIG_DELAY_ACCT
100 PROC_TGID_DELAY_ACCT,
102 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
112 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
114 static struct pid_entry tgid_base_stuff[] = {
115 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
116 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
117 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
118 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
119 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
120 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
121 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
122 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
123 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
124 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
125 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
126 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
127 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
128 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
129 #ifdef CONFIG_SECURITY
130 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
132 #ifdef CONFIG_DELAY_ACCT
133 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
135 #ifdef CONFIG_KALLSYMS
136 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
138 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
139 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
142 static struct pid_entry tid_base_stuff[] = {
143 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
144 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
145 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
146 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
147 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
148 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
149 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
150 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
151 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
152 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
153 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
154 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
155 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
156 #ifdef CONFIG_SECURITY
157 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
159 #ifdef CONFIG_DELAY_ACCT
160 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
162 #ifdef CONFIG_KALLSYMS
163 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
165 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
166 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
170 #ifdef CONFIG_SECURITY
171 static struct pid_entry tgid_attr_stuff[] = {
172 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
173 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
174 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
175 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
178 static struct pid_entry tid_attr_stuff[] = {
179 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
180 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
181 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
182 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
189 static inline struct task_struct *proc_task(struct inode *inode)
191 return PROC_I(inode)->task;
194 static inline int proc_type(struct inode *inode)
196 return PROC_I(inode)->type;
199 int proc_pid_stat(struct task_struct*,char*);
200 int proc_pid_status(struct task_struct*,char*);
201 int proc_pid_statm(struct task_struct*,char*);
202 int proc_pid_cpu(struct task_struct*,char*);
203 int proc_pid_delay(struct task_struct*,char*);
205 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
207 struct task_struct *task = proc_task(inode);
208 struct files_struct *files;
210 int fd = proc_type(inode) - PROC_TID_FD_DIR;
212 files = get_files_struct(task);
214 spin_lock(&files->file_lock);
215 file = fcheck_files(files, fd);
217 *mnt = mntget(file->f_vfsmnt);
218 *dentry = dget(file->f_dentry);
219 spin_unlock(&files->file_lock);
220 put_files_struct(files);
223 spin_unlock(&files->file_lock);
224 put_files_struct(files);
229 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
231 struct vm_area_struct * vma;
232 int result = -ENOENT;
233 struct task_struct *task = proc_task(inode);
234 struct mm_struct * mm = get_task_mm(task);
238 down_read(&mm->mmap_sem);
241 if ((vma->vm_flags & VM_EXECUTABLE) &&
243 *mnt = mntget(vma->vm_file->f_vfsmnt);
244 *dentry = dget(vma->vm_file->f_dentry);
250 up_read(&mm->mmap_sem);
256 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
258 struct fs_struct *fs;
259 int result = -ENOENT;
260 task_lock(proc_task(inode));
261 fs = proc_task(inode)->fs;
263 atomic_inc(&fs->count);
264 task_unlock(proc_task(inode));
266 read_lock(&fs->lock);
267 *mnt = mntget(fs->pwdmnt);
268 *dentry = dget(fs->pwd);
269 read_unlock(&fs->lock);
276 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
278 struct fs_struct *fs;
279 int result = -ENOENT;
280 task_lock(proc_task(inode));
281 fs = proc_task(inode)->fs;
283 atomic_inc(&fs->count);
284 task_unlock(proc_task(inode));
286 read_lock(&fs->lock);
287 *mnt = mntget(fs->rootmnt);
288 *dentry = dget(fs->root);
289 read_unlock(&fs->lock);
296 #define MAY_PTRACE(task) \
297 (task == current || \
298 (task->parent == current && \
299 (task->ptrace & PT_PTRACED) && task->state == TASK_STOPPED && \
300 security_ptrace(current,task) == 0))
302 static int may_ptrace_attach(struct task_struct *task)
310 if (((current->uid != task->euid) ||
311 (current->uid != task->suid) ||
312 (current->uid != task->uid) ||
313 (current->gid != task->egid) ||
314 (current->gid != task->sgid) ||
315 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
318 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
320 if (security_ptrace(current, task))
329 static int proc_pid_environ(struct task_struct *task, char * buffer)
332 struct mm_struct *mm = get_task_mm(task);
334 unsigned int len = mm->env_end - mm->env_start;
337 res = access_process_vm(task, mm->env_start, buffer, len, 0);
338 if (!may_ptrace_attach(task))
345 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
349 struct mm_struct *mm = get_task_mm(task);
353 len = mm->arg_end - mm->arg_start;
358 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
360 // If the nul at the end of args has been overwritten, then
361 // assume application is using setproctitle(3).
362 if (res > 0 && buffer[res-1] != '\0') {
363 len = strnlen(buffer, res);
367 len = mm->env_end - mm->env_start;
368 if (len > PAGE_SIZE - res)
369 len = PAGE_SIZE - res;
370 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
371 res = strnlen(buffer, res);
380 static int proc_pid_auxv(struct task_struct *task, char *buffer)
383 struct mm_struct *mm = get_task_mm(task);
385 unsigned int nwords = 0;
388 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
389 res = nwords * sizeof(mm->saved_auxv[0]);
392 memcpy(buffer, mm->saved_auxv, res);
399 #ifdef CONFIG_KALLSYMS
401 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
402 * Returns the resolved symbol. If that fails, simply return the address.
404 static int proc_pid_wchan(struct task_struct *task, char *buffer)
407 const char *sym_name;
408 unsigned long wchan, size, offset;
411 wchan = get_wchan(task);
413 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
415 return sprintf(buffer, "%s", sym_name);
416 return sprintf(buffer, "%lu", wchan);
418 #endif /* CONFIG_KALLSYMS */
420 /************************************************************************/
421 /* Here the fs part begins */
422 /************************************************************************/
424 /* permission checks */
426 static int proc_check_root(struct inode *inode)
428 struct dentry *de, *base, *root;
429 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
432 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
434 read_lock(¤t->fs->lock);
435 our_vfsmnt = mntget(current->fs->rootmnt);
436 base = dget(current->fs->root);
437 read_unlock(¤t->fs->lock);
439 spin_lock(&vfsmount_lock);
443 while (vfsmnt != our_vfsmnt) {
444 if (vfsmnt == vfsmnt->mnt_parent)
446 de = vfsmnt->mnt_mountpoint;
447 vfsmnt = vfsmnt->mnt_parent;
450 if (!is_subdir(de, base))
452 spin_unlock(&vfsmount_lock);
461 spin_unlock(&vfsmount_lock);
466 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
468 if (vfs_permission(inode, mask) != 0)
470 return proc_check_root(inode);
473 extern struct seq_operations proc_pid_maps_op;
474 static int maps_open(struct inode *inode, struct file *file)
476 struct task_struct *task = proc_task(inode);
477 int ret = seq_open(file, &proc_pid_maps_op);
479 struct seq_file *m = file->private_data;
485 static struct file_operations proc_maps_operations = {
489 .release = seq_release,
492 extern struct seq_operations mounts_op;
493 static int mounts_open(struct inode *inode, struct file *file)
495 struct task_struct *task = proc_task(inode);
496 int ret = seq_open(file, &mounts_op);
499 struct seq_file *m = file->private_data;
500 struct namespace *namespace;
502 namespace = task->namespace;
504 get_namespace(namespace);
508 m->private = namespace;
510 seq_release(inode, file);
517 static int mounts_release(struct inode *inode, struct file *file)
519 struct seq_file *m = file->private_data;
520 struct namespace *namespace = m->private;
521 put_namespace(namespace);
522 return seq_release(inode, file);
525 static struct file_operations proc_mounts_operations = {
529 .release = mounts_release,
532 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
534 static ssize_t proc_info_read(struct file * file, char __user * buf,
535 size_t count, loff_t *ppos)
537 struct inode * inode = file->f_dentry->d_inode;
541 struct task_struct *task = proc_task(inode);
543 if (count > PROC_BLOCK_SIZE)
544 count = PROC_BLOCK_SIZE;
545 if (!(page = __get_free_page(GFP_KERNEL)))
548 length = PROC_I(inode)->op.proc_read(task, (char*)page);
554 /* Static 4kB (or whatever) block capacity */
555 if (*ppos >= length) {
559 if (count + *ppos > length)
560 count = length - *ppos;
562 if (copy_to_user(buf, (char *) page + *ppos, count))
570 static struct file_operations proc_info_file_operations = {
571 .read = proc_info_read,
574 static int mem_open(struct inode* inode, struct file* file)
576 file->private_data = (void*)((long)current->self_exec_id);
580 static ssize_t mem_read(struct file * file, char __user * buf,
581 size_t count, loff_t *ppos)
583 struct task_struct *task = proc_task(file->f_dentry->d_inode);
585 unsigned long src = *ppos;
587 struct mm_struct *mm;
589 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
593 page = (char *)__get_free_page(GFP_USER);
599 mm = get_task_mm(task);
605 if (file->private_data != (void*)((long)current->self_exec_id))
611 int this_len, retval;
613 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
614 retval = access_process_vm(task, src, page, this_len, 0);
615 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
621 if (copy_to_user(buf, page, retval)) {
636 free_page((unsigned long) page);
641 #define mem_write NULL
644 /* This is a security hazard */
645 static ssize_t mem_write(struct file * file, const char * buf,
646 size_t count, loff_t *ppos)
650 struct task_struct *task = proc_task(file->f_dentry->d_inode);
651 unsigned long dst = *ppos;
653 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
656 page = (char *)__get_free_page(GFP_USER);
661 int this_len, retval;
663 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
664 if (copy_from_user(page, buf, this_len)) {
668 retval = access_process_vm(task, dst, page, this_len, 1);
680 free_page((unsigned long) page);
685 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
689 file->f_pos = offset;
692 file->f_pos += offset;
697 force_successful_syscall_return();
701 static struct file_operations proc_mem_operations = {
708 static struct inode_operations proc_mem_inode_operations = {
709 .permission = proc_permission,
712 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
714 struct inode *inode = dentry->d_inode;
717 /* We don't need a base pointer in the /proc filesystem */
720 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
722 error = proc_check_root(inode);
726 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
727 nd->last_type = LAST_BIND;
732 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
733 char __user *buffer, int buflen)
735 struct inode * inode;
736 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
742 inode = dentry->d_inode;
743 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
747 len = tmp + PAGE_SIZE - 1 - path;
751 if (copy_to_user(buffer, path, len))
754 free_page((unsigned long)tmp);
758 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
761 struct inode *inode = dentry->d_inode;
763 struct vfsmount *mnt = NULL;
767 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
769 error = proc_check_root(inode);
773 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
777 error = do_proc_readlink(de, mnt, buffer, buflen);
785 static struct inode_operations proc_pid_link_inode_operations = {
786 .readlink = proc_pid_readlink,
787 .follow_link = proc_pid_follow_link
790 static int pid_alive(struct task_struct *p)
792 BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
793 return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
798 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
800 struct inode *inode = filp->f_dentry->d_inode;
801 struct task_struct *p = proc_task(inode);
802 unsigned int fd, tid, ino;
805 struct files_struct * files;
816 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
820 ino = fake_ino(tid, PROC_TID_INO);
821 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
825 files = get_files_struct(p);
828 spin_lock(&files->file_lock);
829 for (fd = filp->f_pos-2;
831 fd++, filp->f_pos++) {
834 if (!fcheck_files(files, fd))
836 spin_unlock(&files->file_lock);
842 buf[j] = '0' + (i % 10);
846 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
847 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
848 spin_lock(&files->file_lock);
851 spin_lock(&files->file_lock);
853 spin_unlock(&files->file_lock);
854 put_files_struct(files);
860 static int proc_pident_readdir(struct file *filp,
861 void *dirent, filldir_t filldir,
862 struct pid_entry *ents, unsigned int nents)
866 struct dentry *dentry = filp->f_dentry;
867 struct inode *inode = dentry->d_inode;
873 if (!pid_alive(proc_task(inode)))
877 pid = proc_task(inode)->pid;
882 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
888 ino = parent_ino(dentry);
889 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
902 if (filldir(dirent, p->name, p->len, filp->f_pos,
903 fake_ino(pid, p->type), p->mode >> 12) < 0)
915 static int proc_tgid_base_readdir(struct file * filp,
916 void * dirent, filldir_t filldir)
918 return proc_pident_readdir(filp,dirent,filldir,
919 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
922 static int proc_tid_base_readdir(struct file * filp,
923 void * dirent, filldir_t filldir)
925 return proc_pident_readdir(filp,dirent,filldir,
926 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
929 /* building an inode */
931 static int task_dumpable(struct task_struct *task)
934 struct mm_struct *mm;
939 dumpable = mm->dumpable;
945 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
947 struct inode * inode;
948 struct proc_inode *ei;
950 /* We need a new inode */
952 inode = new_inode(sb);
959 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
960 inode->i_ino = fake_ino(task->pid, ino);
962 if (!pid_alive(task))
966 * grab the reference to task.
968 get_task_struct(task);
973 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
974 inode->i_uid = task->euid;
975 inode->i_gid = task->egid;
977 inode->i_xid = vx_task_xid(task);
978 security_task_to_inode(task, inode);
992 * Exceptional case: normally we are not allowed to unhash a busy
993 * directory. In this case, however, we can do it - no aliasing problems
994 * due to the way we treat inodes.
996 * Rewrite the inode's ownerships here because the owning task may have
997 * performed a setuid(), etc.
999 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1001 struct inode *inode = dentry->d_inode;
1002 struct task_struct *task = proc_task(inode);
1004 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1006 /* discard wrong fakeinit */
1008 if (pid_alive(task)) {
1009 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1010 inode->i_uid = task->euid;
1011 inode->i_gid = task->egid;
1016 security_task_to_inode(task, inode);
1024 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1026 struct inode *inode = dentry->d_inode;
1027 struct task_struct *task = proc_task(inode);
1028 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1029 struct files_struct *files;
1031 files = get_files_struct(task);
1033 spin_lock(&files->file_lock);
1034 if (fcheck_files(files, fd)) {
1035 spin_unlock(&files->file_lock);
1036 put_files_struct(files);
1037 if (task_dumpable(task)) {
1038 inode->i_uid = task->euid;
1039 inode->i_gid = task->egid;
1044 security_task_to_inode(task, inode);
1047 spin_unlock(&files->file_lock);
1048 put_files_struct(files);
1054 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1056 struct task_struct *task = proc_task(inode);
1057 spin_lock(&task->proc_lock);
1058 if (task->proc_dentry == dentry)
1059 task->proc_dentry = NULL;
1060 spin_unlock(&task->proc_lock);
1064 static int pid_delete_dentry(struct dentry * dentry)
1066 /* Is the task we represent dead?
1067 * If so, then don't put the dentry on the lru list,
1068 * kill it immediately.
1070 return !pid_alive(proc_task(dentry->d_inode));
1073 static struct dentry_operations tid_fd_dentry_operations =
1075 .d_revalidate = tid_fd_revalidate,
1076 .d_delete = pid_delete_dentry,
1079 static struct dentry_operations pid_dentry_operations =
1081 .d_revalidate = pid_revalidate,
1082 .d_delete = pid_delete_dentry,
1085 static struct dentry_operations pid_base_dentry_operations =
1087 .d_revalidate = pid_revalidate,
1088 .d_iput = pid_base_iput,
1089 .d_delete = pid_delete_dentry,
1094 static unsigned name_to_int(struct dentry *dentry)
1096 const char *name = dentry->d_name.name;
1097 int len = dentry->d_name.len;
1100 if (len > 1 && *name == '0')
1103 unsigned c = *name++ - '0';
1106 if (n >= (~0U-9)/10)
1117 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1119 struct task_struct *task = proc_task(dir);
1120 unsigned fd = name_to_int(dentry);
1122 struct files_struct * files;
1123 struct inode *inode;
1124 struct proc_inode *ei;
1128 if (!pid_alive(task))
1131 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1135 files = get_files_struct(task);
1138 inode->i_mode = S_IFLNK;
1139 spin_lock(&files->file_lock);
1140 file = fcheck_files(files, fd);
1143 if (file->f_mode & 1)
1144 inode->i_mode |= S_IRUSR | S_IXUSR;
1145 if (file->f_mode & 2)
1146 inode->i_mode |= S_IWUSR | S_IXUSR;
1147 spin_unlock(&files->file_lock);
1148 put_files_struct(files);
1149 inode->i_op = &proc_pid_link_inode_operations;
1151 ei->op.proc_get_link = proc_fd_link;
1152 dentry->d_op = &tid_fd_dentry_operations;
1153 d_add(dentry, inode);
1157 spin_unlock(&files->file_lock);
1158 put_files_struct(files);
1162 return ERR_PTR(-ENOENT);
1165 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1166 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1168 static struct file_operations proc_fd_operations = {
1169 .read = generic_read_dir,
1170 .readdir = proc_readfd,
1173 static struct file_operations proc_task_operations = {
1174 .read = generic_read_dir,
1175 .readdir = proc_task_readdir,
1179 * proc directories can do almost nothing..
1181 static struct inode_operations proc_fd_inode_operations = {
1182 .lookup = proc_lookupfd,
1183 .permission = proc_permission,
1186 static struct inode_operations proc_task_inode_operations = {
1187 .lookup = proc_task_lookup,
1188 .permission = proc_permission,
1191 #ifdef CONFIG_SECURITY
1192 static ssize_t proc_pid_attr_read(struct file * file, char * buf,
1193 size_t count, loff_t *ppos)
1195 struct inode * inode = file->f_dentry->d_inode;
1199 struct task_struct *task = proc_task(inode);
1201 if (count > PAGE_SIZE)
1203 if (!(page = __get_free_page(GFP_KERNEL)))
1206 length = security_getprocattr(task,
1207 (char*)file->f_dentry->d_name.name,
1208 (void*)page, count);
1213 /* Static 4kB (or whatever) block capacity */
1214 if (*ppos >= length) {
1218 if (count + *ppos > length)
1219 count = length - *ppos;
1220 end = count + *ppos;
1221 if (copy_to_user(buf, (char *) page + *ppos, count))
1229 static ssize_t proc_pid_attr_write(struct file * file, const char * buf,
1230 size_t count, loff_t *ppos)
1232 struct inode * inode = file->f_dentry->d_inode;
1235 struct task_struct *task = proc_task(inode);
1237 if (count > PAGE_SIZE)
1240 /* No partial writes. */
1243 page = (char*)__get_free_page(GFP_USER);
1247 if (copy_from_user(page, buf, count))
1250 length = security_setprocattr(task,
1251 (char*)file->f_dentry->d_name.name,
1252 (void*)page, count);
1254 free_page((unsigned long) page);
1258 static struct file_operations proc_pid_attr_operations = {
1259 .read = proc_pid_attr_read,
1260 .write = proc_pid_attr_write,
1263 static struct file_operations proc_tid_attr_operations;
1264 static struct inode_operations proc_tid_attr_inode_operations;
1265 static struct file_operations proc_tgid_attr_operations;
1266 static struct inode_operations proc_tgid_attr_inode_operations;
1270 static struct dentry *proc_pident_lookup(struct inode *dir,
1271 struct dentry *dentry,
1272 struct pid_entry *ents)
1274 struct inode *inode;
1276 struct task_struct *task = proc_task(dir);
1277 struct pid_entry *p;
1278 struct proc_inode *ei;
1283 if (!pid_alive(task))
1286 for (p = ents; p->name; p++) {
1287 if (p->len != dentry->d_name.len)
1289 if (!memcmp(dentry->d_name.name, p->name, p->len))
1296 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1301 inode->i_mode = p->mode;
1303 * Yes, it does not scale. And it should not. Don't add
1304 * new entries into /proc/<tgid>/ without very good reasons.
1307 case PROC_TGID_TASK:
1309 inode->i_op = &proc_task_inode_operations;
1310 inode->i_fop = &proc_task_operations;
1315 inode->i_op = &proc_fd_inode_operations;
1316 inode->i_fop = &proc_fd_operations;
1320 inode->i_op = &proc_pid_link_inode_operations;
1321 ei->op.proc_get_link = proc_exe_link;
1325 inode->i_op = &proc_pid_link_inode_operations;
1326 ei->op.proc_get_link = proc_cwd_link;
1329 case PROC_TGID_ROOT:
1330 inode->i_op = &proc_pid_link_inode_operations;
1331 ei->op.proc_get_link = proc_root_link;
1333 case PROC_TID_ENVIRON:
1334 case PROC_TGID_ENVIRON:
1335 inode->i_fop = &proc_info_file_operations;
1336 ei->op.proc_read = proc_pid_environ;
1339 case PROC_TGID_AUXV:
1340 inode->i_fop = &proc_info_file_operations;
1341 ei->op.proc_read = proc_pid_auxv;
1343 case PROC_TID_STATUS:
1344 case PROC_TGID_STATUS:
1345 inode->i_fop = &proc_info_file_operations;
1346 ei->op.proc_read = proc_pid_status;
1349 case PROC_TGID_STAT:
1350 inode->i_fop = &proc_info_file_operations;
1351 ei->op.proc_read = proc_pid_stat;
1353 case PROC_TID_CMDLINE:
1354 case PROC_TGID_CMDLINE:
1355 inode->i_fop = &proc_info_file_operations;
1356 ei->op.proc_read = proc_pid_cmdline;
1358 case PROC_TID_STATM:
1359 case PROC_TGID_STATM:
1360 inode->i_fop = &proc_info_file_operations;
1361 ei->op.proc_read = proc_pid_statm;
1364 case PROC_TGID_MAPS:
1365 inode->i_fop = &proc_maps_operations;
1369 inode->i_op = &proc_mem_inode_operations;
1370 inode->i_fop = &proc_mem_operations;
1372 case PROC_TID_MOUNTS:
1373 case PROC_TGID_MOUNTS:
1374 inode->i_fop = &proc_mounts_operations;
1376 #ifdef CONFIG_SECURITY
1379 inode->i_op = &proc_tid_attr_inode_operations;
1380 inode->i_fop = &proc_tid_attr_operations;
1382 case PROC_TGID_ATTR:
1384 inode->i_op = &proc_tgid_attr_inode_operations;
1385 inode->i_fop = &proc_tgid_attr_operations;
1387 case PROC_TID_ATTR_CURRENT:
1388 case PROC_TGID_ATTR_CURRENT:
1389 case PROC_TID_ATTR_PREV:
1390 case PROC_TGID_ATTR_PREV:
1391 case PROC_TID_ATTR_EXEC:
1392 case PROC_TGID_ATTR_EXEC:
1393 case PROC_TID_ATTR_FSCREATE:
1394 case PROC_TGID_ATTR_FSCREATE:
1395 inode->i_fop = &proc_pid_attr_operations;
1398 #ifdef CONFIG_KALLSYMS
1399 case PROC_TID_WCHAN:
1400 case PROC_TGID_WCHAN:
1401 inode->i_fop = &proc_info_file_operations;
1402 ei->op.proc_read = proc_pid_wchan;
1405 case PROC_TID_VX_INFO:
1406 case PROC_TGID_VX_INFO:
1407 inode->i_fop = &proc_info_file_operations;
1408 ei->op.proc_read = proc_pid_vx_info;
1410 case PROC_TID_IP_INFO:
1411 case PROC_TGID_IP_INFO:
1412 inode->i_fop = &proc_info_file_operations;
1413 ei->op.proc_read = proc_pid_nx_info;
1415 #ifdef CONFIG_DELAY_ACCT
1416 case PROC_TID_DELAY_ACCT:
1417 case PROC_TGID_DELAY_ACCT:
1418 inode->i_fop = &proc_info_file_operations;
1419 ei->op.proc_read = proc_pid_delay;
1423 printk("procfs: impossible type (%d)",p->type);
1425 return ERR_PTR(-EINVAL);
1427 dentry->d_op = &pid_dentry_operations;
1428 d_add(dentry, inode);
1432 return ERR_PTR(error);
1435 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1436 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1439 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1440 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1443 static struct file_operations proc_tgid_base_operations = {
1444 .read = generic_read_dir,
1445 .readdir = proc_tgid_base_readdir,
1448 static struct file_operations proc_tid_base_operations = {
1449 .read = generic_read_dir,
1450 .readdir = proc_tid_base_readdir,
1453 static struct inode_operations proc_tgid_base_inode_operations = {
1454 .lookup = proc_tgid_base_lookup,
1457 static struct inode_operations proc_tid_base_inode_operations = {
1458 .lookup = proc_tid_base_lookup,
1461 #ifdef CONFIG_SECURITY
1462 static int proc_tgid_attr_readdir(struct file * filp,
1463 void * dirent, filldir_t filldir)
1465 return proc_pident_readdir(filp,dirent,filldir,
1466 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1469 static int proc_tid_attr_readdir(struct file * filp,
1470 void * dirent, filldir_t filldir)
1472 return proc_pident_readdir(filp,dirent,filldir,
1473 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1476 static struct file_operations proc_tgid_attr_operations = {
1477 .read = generic_read_dir,
1478 .readdir = proc_tgid_attr_readdir,
1481 static struct file_operations proc_tid_attr_operations = {
1482 .read = generic_read_dir,
1483 .readdir = proc_tid_attr_readdir,
1486 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1487 struct dentry *dentry, struct nameidata *nd)
1489 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1492 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1493 struct dentry *dentry, struct nameidata *nd)
1495 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1498 static struct inode_operations proc_tgid_attr_inode_operations = {
1499 .lookup = proc_tgid_attr_lookup,
1502 static struct inode_operations proc_tid_attr_inode_operations = {
1503 .lookup = proc_tid_attr_lookup,
1510 static int proc_self_readlink(struct dentry *dentry, char *buffer, int buflen)
1513 sprintf(tmp, "%d", current->tgid);
1514 return vfs_readlink(dentry,buffer,buflen,tmp);
1517 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1520 sprintf(tmp, "%d", current->tgid);
1521 return vfs_follow_link(nd,tmp);
1524 static struct inode_operations proc_self_inode_operations = {
1525 .readlink = proc_self_readlink,
1526 .follow_link = proc_self_follow_link,
1530 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1531 * @p: task that should be flushed.
1533 * Drops the /proc/<pid> dcache entry from the hash chains.
1535 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1536 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1537 * if the pid value is immediately reused. This is enforced by
1538 * - caller must acquire spin_lock(p->proc_lock)
1539 * - must be called before detach_pid()
1540 * - proc_pid_lookup acquires proc_lock, and checks that
1541 * the target is not dead by looking at the attach count
1545 struct dentry *proc_pid_unhash(struct task_struct *p)
1547 struct dentry *proc_dentry;
1549 proc_dentry = p->proc_dentry;
1550 if (proc_dentry != NULL) {
1552 spin_lock(&dcache_lock);
1553 if (!d_unhashed(proc_dentry)) {
1554 dget_locked(proc_dentry);
1555 __d_drop(proc_dentry);
1558 spin_unlock(&dcache_lock);
1564 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1565 * @proc_entry: directoy to prune.
1567 * Shrink the /proc directory that was used by the just killed thread.
1570 void proc_pid_flush(struct dentry *proc_dentry)
1572 if(proc_dentry != NULL) {
1573 shrink_dcache_parent(proc_dentry);
1579 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1581 struct task_struct *task;
1582 struct inode *inode;
1583 struct proc_inode *ei;
1587 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1588 inode = new_inode(dir->i_sb);
1590 return ERR_PTR(-ENOMEM);
1592 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1593 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1595 inode->i_mode = S_IFLNK|S_IRWXUGO;
1596 inode->i_uid = inode->i_gid = 0;
1598 inode->i_op = &proc_self_inode_operations;
1599 d_add(dentry, inode);
1602 tgid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1606 read_lock(&tasklist_lock);
1607 task = find_task_by_pid(tgid);
1609 get_task_struct(task);
1610 read_unlock(&tasklist_lock);
1615 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1616 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1619 put_task_struct(task);
1622 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1623 inode->i_op = &proc_tgid_base_inode_operations;
1624 inode->i_fop = &proc_tgid_base_operations;
1626 inode->i_flags|=S_IMMUTABLE;
1628 dentry->d_op = &pid_base_dentry_operations;
1631 d_add(dentry, inode);
1632 spin_lock(&task->proc_lock);
1633 task->proc_dentry = dentry;
1634 if (!pid_alive(task)) {
1635 dentry = proc_pid_unhash(task);
1638 spin_unlock(&task->proc_lock);
1640 put_task_struct(task);
1642 proc_pid_flush(dentry);
1647 return ERR_PTR(-ENOENT);
1651 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1653 struct task_struct *task;
1654 struct task_struct *leader = proc_task(dir);
1655 struct inode *inode;
1658 tid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1662 /* handle fakeinit */
1664 read_lock(&tasklist_lock);
1665 task = find_task_by_pid(tid);
1667 get_task_struct(task);
1668 read_unlock(&tasklist_lock);
1671 if (leader->tgid != task->tgid)
1675 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1676 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1680 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1681 inode->i_op = &proc_tid_base_inode_operations;
1682 inode->i_fop = &proc_tid_base_operations;
1684 inode->i_flags|=S_IMMUTABLE;
1686 dentry->d_op = &pid_base_dentry_operations;
1688 d_add(dentry, inode);
1690 put_task_struct(task);
1693 put_task_struct(task);
1695 return ERR_PTR(-ENOENT);
1698 #define PROC_NUMBUF 10
1699 #define PROC_MAXPIDS 20
1702 * Get a few tgid's to return for filldir - we need to hold the
1703 * tasklist lock while doing this, and we must release it before
1704 * we actually do the filldir itself, so we use a temp buffer..
1706 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1708 struct task_struct *p;
1712 read_lock(&tasklist_lock);
1715 p = find_task_by_pid(version);
1716 if (!thread_group_leader(p))
1723 p = next_task(&init_task);
1725 for ( ; p != &init_task; p = next_task(p)) {
1730 if (!vx_check(vx_task_xid(p), VX_WATCH|VX_IDENT))
1734 tgids[nr_tgids] = vx_map_tgid(current->vx_info, tgid);
1736 if (nr_tgids >= PROC_MAXPIDS)
1739 read_unlock(&tasklist_lock);
1744 * Get a few tid's to return for filldir - we need to hold the
1745 * tasklist lock while doing this, and we must release it before
1746 * we actually do the filldir itself, so we use a temp buffer..
1748 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1750 struct task_struct *leader_task = proc_task(dir);
1751 struct task_struct *task = leader_task;
1755 read_lock(&tasklist_lock);
1757 * The starting point task (leader_task) might be an already
1758 * unlinked task, which cannot be used to access the task-list
1759 * via next_thread().
1761 if (pid_alive(task)) do {
1762 int tid = task->pid;
1764 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1768 tids[nr_tids] = vx_map_tgid(current->vx_info, tid);
1770 if (nr_tids >= PROC_MAXPIDS)
1772 } while ((task = next_thread(task)) != leader_task);
1773 read_unlock(&tasklist_lock);
1777 /* for the /proc/ directory itself, after non-process stuff has been done */
1778 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1780 unsigned int tgid_array[PROC_MAXPIDS];
1781 char buf[PROC_NUMBUF];
1782 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1783 unsigned int nr_tgids, i;
1786 ino_t ino = fake_ino(0,PROC_TGID_INO);
1787 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1794 * f_version caches the last tgid which was returned from readdir
1796 nr_tgids = get_tgid_list(nr, filp->f_version, tgid_array);
1798 for (i = 0; i < nr_tgids; i++) {
1799 int tgid = tgid_array[i];
1800 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1801 unsigned long j = PROC_NUMBUF;
1803 do buf[--j] = '0' + (tgid % 10); while (tgid/=10);
1805 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1806 filp->f_version = tgid;
1814 /* for the /proc/TGID/task/ directories */
1815 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1817 unsigned int tid_array[PROC_MAXPIDS];
1818 char buf[PROC_NUMBUF];
1819 unsigned int nr_tids, i;
1820 struct dentry *dentry = filp->f_dentry;
1821 struct inode *inode = dentry->d_inode;
1822 struct task_struct *task = proc_task(inode);
1823 int retval = -ENOENT;
1825 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1827 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1829 if (!pid_alive(task))
1836 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1841 ino = parent_ino(dentry);
1842 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1848 nr_tids = get_tid_list(pos, tid_array, inode);
1850 for (i = 0; i < nr_tids; i++) {
1851 unsigned long j = PROC_NUMBUF;
1852 int tid = tid_array[i];
1854 ino = fake_ino(tid,PROC_TID_INO);
1857 buf[--j] = '0' + (tid % 10);
1860 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)