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>
37 * For hysterical raisins we keep the same inumbers as in the old procfs.
38 * Feel free to change the macro below - just keep the range distinct from
39 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
40 * As soon as we'll get a separate superblock we will be able to forget
41 * about magical ranges too.
44 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
46 enum pid_directory_inos {
64 #ifdef CONFIG_SECURITY
66 PROC_TGID_ATTR_CURRENT,
69 PROC_TGID_ATTR_FSCREATE,
87 #ifdef CONFIG_SECURITY
89 PROC_TID_ATTR_CURRENT,
92 PROC_TID_ATTR_FSCREATE,
94 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
104 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
106 static struct pid_entry tgid_base_stuff[] = {
107 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
108 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
109 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
110 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
111 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
112 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
113 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
114 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
115 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
116 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
117 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
118 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
119 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
120 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
121 #ifdef __HAS_ARCH_PROC_MAPPED_BASE
122 E(PROC_TGID_MAPBASE, "mapped_base", S_IFREG|S_IRUSR|S_IWUSR),
124 #ifdef CONFIG_SECURITY
125 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
127 #ifdef CONFIG_KALLSYMS
128 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
132 static struct pid_entry tid_base_stuff[] = {
133 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
134 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
135 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
136 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
137 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
138 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
139 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
140 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
141 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
142 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
143 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
144 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
145 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
146 #ifdef CONFIG_SECURITY
147 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
149 #ifdef CONFIG_KALLSYMS
150 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
155 #ifdef CONFIG_SECURITY
156 static struct pid_entry tgid_attr_stuff[] = {
157 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
158 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
159 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
160 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
163 static struct pid_entry tid_attr_stuff[] = {
164 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
165 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
166 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
167 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
174 static inline struct task_struct *proc_task(struct inode *inode)
176 return PROC_I(inode)->task;
179 static inline int proc_type(struct inode *inode)
181 return PROC_I(inode)->type;
184 int proc_pid_stat(struct task_struct*,char*);
185 int proc_pid_status(struct task_struct*,char*);
186 int proc_pid_statm(struct task_struct*,char*);
188 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
190 struct task_struct *task = proc_task(inode);
191 struct files_struct *files;
193 int fd = proc_type(inode) - PROC_TID_FD_DIR;
195 files = get_files_struct(task);
197 spin_lock(&files->file_lock);
198 file = fcheck_files(files, fd);
200 *mnt = mntget(file->f_vfsmnt);
201 *dentry = dget(file->f_dentry);
202 spin_unlock(&files->file_lock);
203 put_files_struct(files);
206 spin_unlock(&files->file_lock);
207 put_files_struct(files);
212 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
214 struct vm_area_struct * vma;
215 int result = -ENOENT;
216 struct task_struct *task = proc_task(inode);
217 struct mm_struct * mm = get_task_mm(task);
221 down_read(&mm->mmap_sem);
224 if ((vma->vm_flags & VM_EXECUTABLE) &&
226 *mnt = mntget(vma->vm_file->f_vfsmnt);
227 *dentry = dget(vma->vm_file->f_dentry);
233 up_read(&mm->mmap_sem);
239 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
241 struct fs_struct *fs;
242 int result = -ENOENT;
243 task_lock(proc_task(inode));
244 fs = proc_task(inode)->fs;
246 atomic_inc(&fs->count);
247 task_unlock(proc_task(inode));
249 read_lock(&fs->lock);
250 *mnt = mntget(fs->pwdmnt);
251 *dentry = dget(fs->pwd);
252 read_unlock(&fs->lock);
259 static int proc_root_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->rootmnt);
271 *dentry = dget(fs->root);
272 read_unlock(&fs->lock);
279 #define MAY_PTRACE(task) \
280 (task == current || \
281 (task->parent == current && \
282 (task->ptrace & PT_PTRACED) && task->state == TASK_STOPPED && \
283 security_ptrace(current,task) == 0))
285 static int may_ptrace_attach(struct task_struct *task)
293 if (((current->uid != task->euid) ||
294 (current->uid != task->suid) ||
295 (current->uid != task->uid) ||
296 (current->gid != task->egid) ||
297 (current->gid != task->sgid) ||
298 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
301 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
303 if (security_ptrace(current, task))
312 static int proc_pid_environ(struct task_struct *task, char * buffer)
315 struct mm_struct *mm = get_task_mm(task);
317 unsigned int len = mm->env_end - mm->env_start;
320 res = access_process_vm(task, mm->env_start, buffer, len, 0);
321 if (!may_ptrace_attach(task))
328 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
332 struct mm_struct *mm = get_task_mm(task);
336 goto out; /* Shh! No looking before we're done */
338 len = mm->arg_end - mm->arg_start;
343 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
345 // If the nul at the end of args has been overwritten, then
346 // assume application is using setproctitle(3).
347 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
348 len = strnlen(buffer, res);
352 len = mm->env_end - mm->env_start;
353 if (len > PAGE_SIZE - res)
354 len = PAGE_SIZE - res;
355 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
356 res = strnlen(buffer, res);
365 static int proc_pid_auxv(struct task_struct *task, char *buffer)
368 struct mm_struct *mm = get_task_mm(task);
370 unsigned int nwords = 0;
373 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
374 res = nwords * sizeof(mm->saved_auxv[0]);
377 memcpy(buffer, mm->saved_auxv, res);
384 #ifdef CONFIG_KALLSYMS
386 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
387 * Returns the resolved symbol. If that fails, simply return the address.
389 static int proc_pid_wchan(struct task_struct *task, char *buffer)
392 const char *sym_name;
393 unsigned long wchan, size, offset;
396 wchan = get_wchan(task);
398 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
400 return sprintf(buffer, "%s", sym_name);
401 return sprintf(buffer, "%lu", wchan);
403 #endif /* CONFIG_KALLSYMS */
405 /************************************************************************/
406 /* Here the fs part begins */
407 /************************************************************************/
409 /* permission checks */
411 static int proc_check_root(struct inode *inode)
413 struct dentry *de, *base, *root;
414 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
417 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
419 read_lock(¤t->fs->lock);
420 our_vfsmnt = mntget(current->fs->rootmnt);
421 base = dget(current->fs->root);
422 read_unlock(¤t->fs->lock);
424 spin_lock(&vfsmount_lock);
428 while (vfsmnt != our_vfsmnt) {
429 if (vfsmnt == vfsmnt->mnt_parent)
431 de = vfsmnt->mnt_mountpoint;
432 vfsmnt = vfsmnt->mnt_parent;
435 if (!is_subdir(de, base))
437 spin_unlock(&vfsmount_lock);
446 spin_unlock(&vfsmount_lock);
451 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
453 if (vfs_permission(inode, mask) != 0)
455 return proc_check_root(inode);
458 extern struct seq_operations proc_pid_maps_op;
459 static int maps_open(struct inode *inode, struct file *file)
461 struct task_struct *task = proc_task(inode);
462 int ret = seq_open(file, &proc_pid_maps_op);
464 struct seq_file *m = file->private_data;
470 static struct file_operations proc_maps_operations = {
474 .release = seq_release,
477 extern struct seq_operations mounts_op;
478 static int mounts_open(struct inode *inode, struct file *file)
480 struct task_struct *task = proc_task(inode);
481 int ret = seq_open(file, &mounts_op);
484 struct seq_file *m = file->private_data;
485 struct namespace *namespace;
487 namespace = task->namespace;
489 get_namespace(namespace);
493 m->private = namespace;
495 seq_release(inode, file);
502 static int mounts_release(struct inode *inode, struct file *file)
504 struct seq_file *m = file->private_data;
505 struct namespace *namespace = m->private;
506 put_namespace(namespace);
507 return seq_release(inode, file);
510 static struct file_operations proc_mounts_operations = {
514 .release = mounts_release,
517 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
519 static ssize_t proc_info_read(struct file * file, char __user * buf,
520 size_t count, loff_t *ppos)
522 struct inode * inode = file->f_dentry->d_inode;
526 struct task_struct *task = proc_task(inode);
528 if (count > PROC_BLOCK_SIZE)
529 count = PROC_BLOCK_SIZE;
530 if (!(page = __get_free_page(GFP_KERNEL)))
533 length = PROC_I(inode)->op.proc_read(task, (char*)page);
539 /* Static 4kB (or whatever) block capacity */
540 if (*ppos >= length) {
544 if (count + *ppos > length)
545 count = length - *ppos;
547 if (copy_to_user(buf, (char *) page + *ppos, count))
555 static struct file_operations proc_info_file_operations = {
556 .read = proc_info_read,
559 static int mem_open(struct inode* inode, struct file* file)
561 file->private_data = (void*)((long)current->self_exec_id);
565 static ssize_t mem_read(struct file * file, char __user * buf,
566 size_t count, loff_t *ppos)
568 struct task_struct *task = proc_task(file->f_dentry->d_inode);
570 unsigned long src = *ppos;
572 struct mm_struct *mm;
574 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
578 page = (char *)__get_free_page(GFP_USER);
584 mm = get_task_mm(task);
590 if (file->private_data != (void*)((long)current->self_exec_id))
596 int this_len, retval;
598 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
599 retval = access_process_vm(task, src, page, this_len, 0);
600 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
606 if (copy_to_user(buf, page, retval)) {
621 free_page((unsigned long) page);
626 #define mem_write NULL
629 /* This is a security hazard */
630 static ssize_t mem_write(struct file * file, const char * buf,
631 size_t count, loff_t *ppos)
635 struct task_struct *task = proc_task(file->f_dentry->d_inode);
636 unsigned long dst = *ppos;
638 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
641 page = (char *)__get_free_page(GFP_USER);
646 int this_len, retval;
648 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
649 if (copy_from_user(page, buf, this_len)) {
653 retval = access_process_vm(task, dst, page, this_len, 1);
665 free_page((unsigned long) page);
670 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
674 file->f_pos = offset;
677 file->f_pos += offset;
682 force_successful_syscall_return();
686 static struct file_operations proc_mem_operations = {
693 #ifdef __HAS_ARCH_PROC_MAPPED_BASE
694 static ssize_t mapbase_read(struct file * file, char * buf,
695 size_t count, loff_t *ppos)
697 struct task_struct *task = proc_task(file->f_dentry->d_inode);
701 len = sprintf(buffer, "%li\n", task->map_base) + 1;
704 if (count > len-*ppos)
706 if (copy_to_user(buf, buffer + *ppos, count))
712 static ssize_t mapbase_write(struct file * file, const char * buf,
713 size_t count, loff_t *ppos)
715 struct task_struct *task = proc_task(file->f_dentry->d_inode);
716 char buffer[64], *end;
717 unsigned long newbase;
719 if (!capable(CAP_SYS_ADMIN))
721 memset(buffer, 0, 64);
724 if (copy_from_user(buffer, buf, count))
726 newbase = simple_strtoul(buffer, &end, 0);
730 task->map_base = newbase;
731 if (end - buffer == 0)
736 static struct file_operations proc_mapbase_operations = {
738 write: mapbase_write,
740 #endif /* __HAS_ARCH_PROC_MAPPED_BASE */
742 static struct inode_operations proc_mem_inode_operations = {
743 .permission = proc_permission,
746 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
748 struct inode *inode = dentry->d_inode;
751 /* We don't need a base pointer in the /proc filesystem */
754 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
756 error = proc_check_root(inode);
760 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
761 nd->last_type = LAST_BIND;
766 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
767 char __user *buffer, int buflen)
769 struct inode * inode;
770 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
776 inode = dentry->d_inode;
777 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
781 len = tmp + PAGE_SIZE - 1 - path;
785 if (copy_to_user(buffer, path, len))
788 free_page((unsigned long)tmp);
792 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
795 struct inode *inode = dentry->d_inode;
797 struct vfsmount *mnt = NULL;
801 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
803 error = proc_check_root(inode);
807 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
811 error = do_proc_readlink(de, mnt, buffer, buflen);
819 static struct inode_operations proc_pid_link_inode_operations = {
820 .readlink = proc_pid_readlink,
821 .follow_link = proc_pid_follow_link
824 static int pid_alive(struct task_struct *p)
826 BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
827 return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
832 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
834 struct inode *inode = filp->f_dentry->d_inode;
835 struct task_struct *p = proc_task(inode);
836 unsigned int fd, tid, ino;
839 struct files_struct * files;
850 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
854 ino = fake_ino(tid, PROC_TID_INO);
855 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
859 files = get_files_struct(p);
862 spin_lock(&files->file_lock);
863 for (fd = filp->f_pos-2;
865 fd++, filp->f_pos++) {
868 if (!fcheck_files(files, fd))
870 spin_unlock(&files->file_lock);
876 buf[j] = '0' + (i % 10);
880 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
881 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
882 spin_lock(&files->file_lock);
885 spin_lock(&files->file_lock);
887 spin_unlock(&files->file_lock);
888 put_files_struct(files);
894 static int proc_pident_readdir(struct file *filp,
895 void *dirent, filldir_t filldir,
896 struct pid_entry *ents, unsigned int nents)
900 struct dentry *dentry = filp->f_dentry;
901 struct inode *inode = dentry->d_inode;
907 if (!pid_alive(proc_task(inode)))
911 pid = proc_task(inode)->pid;
916 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
922 ino = parent_ino(dentry);
923 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
936 if (filldir(dirent, p->name, p->len, filp->f_pos,
937 fake_ino(pid, p->type), p->mode >> 12) < 0)
949 static int proc_tgid_base_readdir(struct file * filp,
950 void * dirent, filldir_t filldir)
952 return proc_pident_readdir(filp,dirent,filldir,
953 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
956 static int proc_tid_base_readdir(struct file * filp,
957 void * dirent, filldir_t filldir)
959 return proc_pident_readdir(filp,dirent,filldir,
960 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
963 /* building an inode */
965 static int task_dumpable(struct task_struct *task)
968 struct mm_struct *mm;
973 dumpable = mm->dumpable;
979 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
981 struct inode * inode;
982 struct proc_inode *ei;
984 /* We need a new inode */
986 inode = new_inode(sb);
993 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
994 inode->i_ino = fake_ino(task->pid, ino);
996 if (!pid_alive(task))
1000 * grab the reference to task.
1002 get_task_struct(task);
1007 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1008 inode->i_uid = task->euid;
1009 inode->i_gid = task->egid;
1011 security_task_to_inode(task, inode);
1025 * Exceptional case: normally we are not allowed to unhash a busy
1026 * directory. In this case, however, we can do it - no aliasing problems
1027 * due to the way we treat inodes.
1029 * Rewrite the inode's ownerships here because the owning task may have
1030 * performed a setuid(), etc.
1032 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1034 struct inode *inode = dentry->d_inode;
1035 struct task_struct *task = proc_task(inode);
1036 if (pid_alive(task)) {
1037 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1038 inode->i_uid = task->euid;
1039 inode->i_gid = task->egid;
1044 security_task_to_inode(task, inode);
1051 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1053 struct inode *inode = dentry->d_inode;
1054 struct task_struct *task = proc_task(inode);
1055 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1056 struct files_struct *files;
1058 files = get_files_struct(task);
1060 spin_lock(&files->file_lock);
1061 if (fcheck_files(files, fd)) {
1062 spin_unlock(&files->file_lock);
1063 put_files_struct(files);
1064 if (task_dumpable(task)) {
1065 inode->i_uid = task->euid;
1066 inode->i_gid = task->egid;
1071 security_task_to_inode(task, inode);
1074 spin_unlock(&files->file_lock);
1075 put_files_struct(files);
1081 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1083 struct task_struct *task = proc_task(inode);
1084 spin_lock(&task->proc_lock);
1085 if (task->proc_dentry == dentry)
1086 task->proc_dentry = NULL;
1087 spin_unlock(&task->proc_lock);
1091 static int pid_delete_dentry(struct dentry * dentry)
1093 /* Is the task we represent dead?
1094 * If so, then don't put the dentry on the lru list,
1095 * kill it immediately.
1097 return !pid_alive(proc_task(dentry->d_inode));
1100 static struct dentry_operations tid_fd_dentry_operations =
1102 .d_revalidate = tid_fd_revalidate,
1103 .d_delete = pid_delete_dentry,
1106 static struct dentry_operations pid_dentry_operations =
1108 .d_revalidate = pid_revalidate,
1109 .d_delete = pid_delete_dentry,
1112 static struct dentry_operations pid_base_dentry_operations =
1114 .d_revalidate = pid_revalidate,
1115 .d_iput = pid_base_iput,
1116 .d_delete = pid_delete_dentry,
1121 static unsigned name_to_int(struct dentry *dentry)
1123 const char *name = dentry->d_name.name;
1124 int len = dentry->d_name.len;
1127 if (len > 1 && *name == '0')
1130 unsigned c = *name++ - '0';
1133 if (n >= (~0U-9)/10)
1144 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1146 struct task_struct *task = proc_task(dir);
1147 unsigned fd = name_to_int(dentry);
1149 struct files_struct * files;
1150 struct inode *inode;
1151 struct proc_inode *ei;
1155 if (!pid_alive(task))
1158 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1162 files = get_files_struct(task);
1165 inode->i_mode = S_IFLNK;
1166 spin_lock(&files->file_lock);
1167 file = fcheck_files(files, fd);
1170 if (file->f_mode & 1)
1171 inode->i_mode |= S_IRUSR | S_IXUSR;
1172 if (file->f_mode & 2)
1173 inode->i_mode |= S_IWUSR | S_IXUSR;
1174 spin_unlock(&files->file_lock);
1175 put_files_struct(files);
1176 inode->i_op = &proc_pid_link_inode_operations;
1178 ei->op.proc_get_link = proc_fd_link;
1179 dentry->d_op = &tid_fd_dentry_operations;
1180 d_add(dentry, inode);
1184 spin_unlock(&files->file_lock);
1185 put_files_struct(files);
1189 return ERR_PTR(-ENOENT);
1192 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1193 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1195 static struct file_operations proc_fd_operations = {
1196 .read = generic_read_dir,
1197 .readdir = proc_readfd,
1200 static struct file_operations proc_task_operations = {
1201 .read = generic_read_dir,
1202 .readdir = proc_task_readdir,
1206 * proc directories can do almost nothing..
1208 static struct inode_operations proc_fd_inode_operations = {
1209 .lookup = proc_lookupfd,
1210 .permission = proc_permission,
1213 static struct inode_operations proc_task_inode_operations = {
1214 .lookup = proc_task_lookup,
1215 .permission = proc_permission,
1218 #ifdef CONFIG_SECURITY
1219 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1220 size_t count, loff_t *ppos)
1222 struct inode * inode = file->f_dentry->d_inode;
1226 struct task_struct *task = proc_task(inode);
1228 if (count > PAGE_SIZE)
1230 if (!(page = __get_free_page(GFP_KERNEL)))
1233 length = security_getprocattr(task,
1234 (char*)file->f_dentry->d_name.name,
1235 (void*)page, count);
1240 /* Static 4kB (or whatever) block capacity */
1241 if (*ppos >= length) {
1245 if (count + *ppos > length)
1246 count = length - *ppos;
1247 end = count + *ppos;
1248 if (copy_to_user(buf, (char *) page + *ppos, count))
1256 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1257 size_t count, loff_t *ppos)
1259 struct inode * inode = file->f_dentry->d_inode;
1262 struct task_struct *task = proc_task(inode);
1264 if (count > PAGE_SIZE)
1267 /* No partial writes. */
1270 page = (char*)__get_free_page(GFP_USER);
1274 if (copy_from_user(page, buf, count))
1277 length = security_setprocattr(task,
1278 (char*)file->f_dentry->d_name.name,
1279 (void*)page, count);
1281 free_page((unsigned long) page);
1285 static struct file_operations proc_pid_attr_operations = {
1286 .read = proc_pid_attr_read,
1287 .write = proc_pid_attr_write,
1290 static struct file_operations proc_tid_attr_operations;
1291 static struct inode_operations proc_tid_attr_inode_operations;
1292 static struct file_operations proc_tgid_attr_operations;
1293 static struct inode_operations proc_tgid_attr_inode_operations;
1297 static struct dentry *proc_pident_lookup(struct inode *dir,
1298 struct dentry *dentry,
1299 struct pid_entry *ents)
1301 struct inode *inode;
1303 struct task_struct *task = proc_task(dir);
1304 struct pid_entry *p;
1305 struct proc_inode *ei;
1310 if (!pid_alive(task))
1313 for (p = ents; p->name; p++) {
1314 if (p->len != dentry->d_name.len)
1316 if (!memcmp(dentry->d_name.name, p->name, p->len))
1323 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1328 inode->i_mode = p->mode;
1330 * Yes, it does not scale. And it should not. Don't add
1331 * new entries into /proc/<tgid>/ without very good reasons.
1334 case PROC_TGID_TASK:
1336 inode->i_op = &proc_task_inode_operations;
1337 inode->i_fop = &proc_task_operations;
1342 inode->i_op = &proc_fd_inode_operations;
1343 inode->i_fop = &proc_fd_operations;
1347 inode->i_op = &proc_pid_link_inode_operations;
1348 ei->op.proc_get_link = proc_exe_link;
1352 inode->i_op = &proc_pid_link_inode_operations;
1353 ei->op.proc_get_link = proc_cwd_link;
1356 case PROC_TGID_ROOT:
1357 inode->i_op = &proc_pid_link_inode_operations;
1358 ei->op.proc_get_link = proc_root_link;
1360 case PROC_TID_ENVIRON:
1361 case PROC_TGID_ENVIRON:
1362 inode->i_fop = &proc_info_file_operations;
1363 ei->op.proc_read = proc_pid_environ;
1366 case PROC_TGID_AUXV:
1367 inode->i_fop = &proc_info_file_operations;
1368 ei->op.proc_read = proc_pid_auxv;
1370 case PROC_TID_STATUS:
1371 case PROC_TGID_STATUS:
1372 inode->i_fop = &proc_info_file_operations;
1373 ei->op.proc_read = proc_pid_status;
1376 case PROC_TGID_STAT:
1377 inode->i_fop = &proc_info_file_operations;
1378 ei->op.proc_read = proc_pid_stat;
1380 case PROC_TID_CMDLINE:
1381 case PROC_TGID_CMDLINE:
1382 inode->i_fop = &proc_info_file_operations;
1383 ei->op.proc_read = proc_pid_cmdline;
1385 case PROC_TID_STATM:
1386 case PROC_TGID_STATM:
1387 inode->i_fop = &proc_info_file_operations;
1388 ei->op.proc_read = proc_pid_statm;
1391 case PROC_TGID_MAPS:
1392 inode->i_fop = &proc_maps_operations;
1394 #ifdef __HAS_ARCH_PROC_MAPPED_BASE
1395 case PROC_TGID_MAPBASE:
1396 inode->i_fop = &proc_mapbase_operations;
1401 inode->i_op = &proc_mem_inode_operations;
1402 inode->i_fop = &proc_mem_operations;
1404 case PROC_TID_MOUNTS:
1405 case PROC_TGID_MOUNTS:
1406 inode->i_fop = &proc_mounts_operations;
1408 #ifdef CONFIG_SECURITY
1411 inode->i_op = &proc_tid_attr_inode_operations;
1412 inode->i_fop = &proc_tid_attr_operations;
1414 case PROC_TGID_ATTR:
1416 inode->i_op = &proc_tgid_attr_inode_operations;
1417 inode->i_fop = &proc_tgid_attr_operations;
1419 case PROC_TID_ATTR_CURRENT:
1420 case PROC_TGID_ATTR_CURRENT:
1421 case PROC_TID_ATTR_PREV:
1422 case PROC_TGID_ATTR_PREV:
1423 case PROC_TID_ATTR_EXEC:
1424 case PROC_TGID_ATTR_EXEC:
1425 case PROC_TID_ATTR_FSCREATE:
1426 case PROC_TGID_ATTR_FSCREATE:
1427 inode->i_fop = &proc_pid_attr_operations;
1430 #ifdef CONFIG_KALLSYMS
1431 case PROC_TID_WCHAN:
1432 case PROC_TGID_WCHAN:
1433 inode->i_fop = &proc_info_file_operations;
1434 ei->op.proc_read = proc_pid_wchan;
1438 printk("procfs: impossible type (%d)",p->type);
1440 return ERR_PTR(-EINVAL);
1442 dentry->d_op = &pid_dentry_operations;
1443 d_add(dentry, inode);
1447 return ERR_PTR(error);
1450 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1451 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1454 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1455 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1458 static struct file_operations proc_tgid_base_operations = {
1459 .read = generic_read_dir,
1460 .readdir = proc_tgid_base_readdir,
1463 static struct file_operations proc_tid_base_operations = {
1464 .read = generic_read_dir,
1465 .readdir = proc_tid_base_readdir,
1468 static struct inode_operations proc_tgid_base_inode_operations = {
1469 .lookup = proc_tgid_base_lookup,
1472 static struct inode_operations proc_tid_base_inode_operations = {
1473 .lookup = proc_tid_base_lookup,
1476 #ifdef CONFIG_SECURITY
1477 static int proc_tgid_attr_readdir(struct file * filp,
1478 void * dirent, filldir_t filldir)
1480 return proc_pident_readdir(filp,dirent,filldir,
1481 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1484 static int proc_tid_attr_readdir(struct file * filp,
1485 void * dirent, filldir_t filldir)
1487 return proc_pident_readdir(filp,dirent,filldir,
1488 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1491 static struct file_operations proc_tgid_attr_operations = {
1492 .read = generic_read_dir,
1493 .readdir = proc_tgid_attr_readdir,
1496 static struct file_operations proc_tid_attr_operations = {
1497 .read = generic_read_dir,
1498 .readdir = proc_tid_attr_readdir,
1501 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1502 struct dentry *dentry, struct nameidata *nd)
1504 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1507 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1508 struct dentry *dentry, struct nameidata *nd)
1510 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1513 static struct inode_operations proc_tgid_attr_inode_operations = {
1514 .lookup = proc_tgid_attr_lookup,
1517 static struct inode_operations proc_tid_attr_inode_operations = {
1518 .lookup = proc_tid_attr_lookup,
1525 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1529 sprintf(tmp, "%d", current->tgid);
1530 return vfs_readlink(dentry,buffer,buflen,tmp);
1533 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1536 sprintf(tmp, "%d", current->tgid);
1537 return vfs_follow_link(nd,tmp);
1540 static struct inode_operations proc_self_inode_operations = {
1541 .readlink = proc_self_readlink,
1542 .follow_link = proc_self_follow_link,
1546 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1547 * @p: task that should be flushed.
1549 * Drops the /proc/<pid> dcache entry from the hash chains.
1551 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1552 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1553 * if the pid value is immediately reused. This is enforced by
1554 * - caller must acquire spin_lock(p->proc_lock)
1555 * - must be called before detach_pid()
1556 * - proc_pid_lookup acquires proc_lock, and checks that
1557 * the target is not dead by looking at the attach count
1561 struct dentry *proc_pid_unhash(struct task_struct *p)
1563 struct dentry *proc_dentry;
1565 proc_dentry = p->proc_dentry;
1566 if (proc_dentry != NULL) {
1568 spin_lock(&dcache_lock);
1569 if (!d_unhashed(proc_dentry)) {
1570 dget_locked(proc_dentry);
1571 __d_drop(proc_dentry);
1574 spin_unlock(&dcache_lock);
1580 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1581 * @proc_entry: directoy to prune.
1583 * Shrink the /proc directory that was used by the just killed thread.
1586 void proc_pid_flush(struct dentry *proc_dentry)
1588 if(proc_dentry != NULL) {
1589 shrink_dcache_parent(proc_dentry);
1595 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1597 struct task_struct *task;
1598 struct inode *inode;
1599 struct proc_inode *ei;
1603 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1604 inode = new_inode(dir->i_sb);
1606 return ERR_PTR(-ENOMEM);
1608 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1609 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1611 inode->i_mode = S_IFLNK|S_IRWXUGO;
1612 inode->i_uid = inode->i_gid = 0;
1614 inode->i_op = &proc_self_inode_operations;
1615 d_add(dentry, inode);
1618 tgid = name_to_int(dentry);
1622 read_lock(&tasklist_lock);
1623 task = find_task_by_pid(tgid);
1625 get_task_struct(task);
1626 read_unlock(&tasklist_lock);
1630 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1634 put_task_struct(task);
1637 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1638 inode->i_op = &proc_tgid_base_inode_operations;
1639 inode->i_fop = &proc_tgid_base_operations;
1641 inode->i_flags|=S_IMMUTABLE;
1643 dentry->d_op = &pid_base_dentry_operations;
1646 d_add(dentry, inode);
1647 spin_lock(&task->proc_lock);
1648 task->proc_dentry = dentry;
1649 if (!pid_alive(task)) {
1650 dentry = proc_pid_unhash(task);
1653 spin_unlock(&task->proc_lock);
1655 put_task_struct(task);
1657 proc_pid_flush(dentry);
1662 return ERR_PTR(-ENOENT);
1666 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1668 struct task_struct *task;
1669 struct task_struct *leader = proc_task(dir);
1670 struct inode *inode;
1673 tid = name_to_int(dentry);
1677 read_lock(&tasklist_lock);
1678 task = find_task_by_pid(tid);
1680 get_task_struct(task);
1681 read_unlock(&tasklist_lock);
1684 if (leader->tgid != task->tgid)
1687 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1692 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1693 inode->i_op = &proc_tid_base_inode_operations;
1694 inode->i_fop = &proc_tid_base_operations;
1696 inode->i_flags|=S_IMMUTABLE;
1698 dentry->d_op = &pid_base_dentry_operations;
1700 d_add(dentry, inode);
1702 put_task_struct(task);
1705 put_task_struct(task);
1707 return ERR_PTR(-ENOENT);
1710 #define PROC_NUMBUF 10
1711 #define PROC_MAXPIDS 20
1714 * Get a few tgid's to return for filldir - we need to hold the
1715 * tasklist lock while doing this, and we must release it before
1716 * we actually do the filldir itself, so we use a temp buffer..
1718 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1720 struct task_struct *p;
1724 read_lock(&tasklist_lock);
1727 p = find_task_by_pid(version);
1728 if (!thread_group_leader(p))
1735 p = next_task(&init_task);
1737 for ( ; p != &init_task; p = next_task(p)) {
1743 tgids[nr_tgids] = tgid;
1745 if (nr_tgids >= PROC_MAXPIDS)
1748 read_unlock(&tasklist_lock);
1753 * Get a few tid's to return for filldir - we need to hold the
1754 * tasklist lock while doing this, and we must release it before
1755 * we actually do the filldir itself, so we use a temp buffer..
1757 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1759 struct task_struct *leader_task = proc_task(dir);
1760 struct task_struct *task = leader_task;
1764 read_lock(&tasklist_lock);
1766 * The starting point task (leader_task) might be an already
1767 * unlinked task, which cannot be used to access the task-list
1768 * via next_thread().
1770 if (pid_alive(task)) do {
1771 int tid = task->pid;
1775 tids[nr_tids] = tid;
1777 if (nr_tids >= PROC_MAXPIDS)
1779 } while ((task = next_thread(task)) != leader_task);
1780 read_unlock(&tasklist_lock);
1784 /* for the /proc/ directory itself, after non-process stuff has been done */
1785 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1787 unsigned int tgid_array[PROC_MAXPIDS];
1788 char buf[PROC_NUMBUF];
1789 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1790 unsigned int nr_tgids, i;
1793 ino_t ino = fake_ino(0,PROC_TGID_INO);
1794 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1801 * f_version caches the last tgid which was returned from readdir
1803 nr_tgids = get_tgid_list(nr, filp->f_version, tgid_array);
1805 for (i = 0; i < nr_tgids; i++) {
1806 int tgid = tgid_array[i];
1807 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1808 unsigned long j = PROC_NUMBUF;
1811 buf[--j] = '0' + (tgid % 10);
1812 while ((tgid /= 10) != 0);
1814 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1815 filp->f_version = tgid;
1823 /* for the /proc/TGID/task/ directories */
1824 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1826 unsigned int tid_array[PROC_MAXPIDS];
1827 char buf[PROC_NUMBUF];
1828 unsigned int nr_tids, i;
1829 struct dentry *dentry = filp->f_dentry;
1830 struct inode *inode = dentry->d_inode;
1831 int retval = -ENOENT;
1833 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1835 if (!pid_alive(proc_task(inode)))
1842 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1847 ino = parent_ino(dentry);
1848 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1854 nr_tids = get_tid_list(pos, tid_array, inode);
1856 for (i = 0; i < nr_tids; i++) {
1857 unsigned long j = PROC_NUMBUF;
1858 int tid = tid_array[i];
1860 ino = fake_ino(tid,PROC_TID_INO);
1863 buf[--j] = '0' + (tid % 10);
1864 while ((tid /= 10) != 0);
1866 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)