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 {
63 #ifdef CONFIG_SECURITY
65 PROC_TGID_ATTR_CURRENT,
68 PROC_TGID_ATTR_FSCREATE,
86 #ifdef CONFIG_SECURITY
88 PROC_TID_ATTR_CURRENT,
91 PROC_TID_ATTR_FSCREATE,
93 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
103 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
105 static struct pid_entry tgid_base_stuff[] = {
106 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
107 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
108 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
109 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
110 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
111 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
112 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
113 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
114 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
115 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
116 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
117 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
118 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
119 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
120 #ifdef CONFIG_SECURITY
121 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
123 #ifdef CONFIG_KALLSYMS
124 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
128 static struct pid_entry tid_base_stuff[] = {
129 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
130 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
131 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
132 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
133 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
134 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
135 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
136 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
137 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
138 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
139 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
140 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
141 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
142 #ifdef CONFIG_SECURITY
143 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
145 #ifdef CONFIG_KALLSYMS
146 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
151 #ifdef CONFIG_SECURITY
152 static struct pid_entry tgid_attr_stuff[] = {
153 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
154 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
155 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
156 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
159 static struct pid_entry tid_attr_stuff[] = {
160 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
161 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
162 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
163 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
170 static inline struct task_struct *proc_task(struct inode *inode)
172 return PROC_I(inode)->task;
175 static inline int proc_type(struct inode *inode)
177 return PROC_I(inode)->type;
180 int proc_pid_stat(struct task_struct*,char*);
181 int proc_pid_status(struct task_struct*,char*);
182 int proc_pid_statm(struct task_struct*,char*);
183 int proc_pid_cpu(struct task_struct*,char*);
185 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
187 struct task_struct *task = proc_task(inode);
188 struct files_struct *files;
190 int fd = proc_type(inode) - PROC_TID_FD_DIR;
192 files = get_files_struct(task);
194 spin_lock(&files->file_lock);
195 file = fcheck_files(files, fd);
197 *mnt = mntget(file->f_vfsmnt);
198 *dentry = dget(file->f_dentry);
199 spin_unlock(&files->file_lock);
200 put_files_struct(files);
203 spin_unlock(&files->file_lock);
204 put_files_struct(files);
209 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
211 struct vm_area_struct * vma;
212 int result = -ENOENT;
213 struct task_struct *task = proc_task(inode);
214 struct mm_struct * mm = get_task_mm(task);
218 down_read(&mm->mmap_sem);
221 if ((vma->vm_flags & VM_EXECUTABLE) &&
223 *mnt = mntget(vma->vm_file->f_vfsmnt);
224 *dentry = dget(vma->vm_file->f_dentry);
230 up_read(&mm->mmap_sem);
236 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
238 struct fs_struct *fs;
239 int result = -ENOENT;
240 task_lock(proc_task(inode));
241 fs = proc_task(inode)->fs;
243 atomic_inc(&fs->count);
244 task_unlock(proc_task(inode));
246 read_lock(&fs->lock);
247 *mnt = mntget(fs->pwdmnt);
248 *dentry = dget(fs->pwd);
249 read_unlock(&fs->lock);
256 static int proc_root_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->rootmnt);
268 *dentry = dget(fs->root);
269 read_unlock(&fs->lock);
276 #define MAY_PTRACE(task) \
277 (task == current || \
278 (task->parent == current && \
279 (task->ptrace & PT_PTRACED) && task->state == TASK_STOPPED && \
280 security_ptrace(current,task) == 0))
282 static int may_ptrace_attach(struct task_struct *task)
290 if (((current->uid != task->euid) ||
291 (current->uid != task->suid) ||
292 (current->uid != task->uid) ||
293 (current->gid != task->egid) ||
294 (current->gid != task->sgid) ||
295 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
298 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
300 if (security_ptrace(current, task))
309 static int proc_pid_environ(struct task_struct *task, char * buffer)
312 struct mm_struct *mm = get_task_mm(task);
314 unsigned int len = mm->env_end - mm->env_start;
317 res = access_process_vm(task, mm->env_start, buffer, len, 0);
318 if (!may_ptrace_attach(task))
325 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
329 struct mm_struct *mm = get_task_mm(task);
333 len = mm->arg_end - mm->arg_start;
338 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
340 // If the nul at the end of args has been overwritten, then
341 // assume application is using setproctitle(3).
342 if (res > 0 && buffer[res-1] != '\0') {
343 len = strnlen(buffer, res);
347 len = mm->env_end - mm->env_start;
348 if (len > PAGE_SIZE - res)
349 len = PAGE_SIZE - res;
350 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
351 res = strnlen(buffer, res);
360 static int proc_pid_auxv(struct task_struct *task, char *buffer)
363 struct mm_struct *mm = get_task_mm(task);
365 unsigned int nwords = 0;
368 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
369 res = nwords * sizeof(mm->saved_auxv[0]);
372 memcpy(buffer, mm->saved_auxv, res);
379 #ifdef CONFIG_KALLSYMS
381 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
382 * Returns the resolved symbol. If that fails, simply return the address.
384 static int proc_pid_wchan(struct task_struct *task, char *buffer)
387 const char *sym_name;
388 unsigned long wchan, size, offset;
391 wchan = get_wchan(task);
393 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
395 return sprintf(buffer, "%s", sym_name);
396 return sprintf(buffer, "%lu", wchan);
398 #endif /* CONFIG_KALLSYMS */
400 /************************************************************************/
401 /* Here the fs part begins */
402 /************************************************************************/
404 /* permission checks */
406 static int proc_check_root(struct inode *inode)
408 struct dentry *de, *base, *root;
409 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
412 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
414 read_lock(¤t->fs->lock);
415 our_vfsmnt = mntget(current->fs->rootmnt);
416 base = dget(current->fs->root);
417 read_unlock(¤t->fs->lock);
419 spin_lock(&vfsmount_lock);
423 while (vfsmnt != our_vfsmnt) {
424 if (vfsmnt == vfsmnt->mnt_parent)
426 de = vfsmnt->mnt_mountpoint;
427 vfsmnt = vfsmnt->mnt_parent;
430 if (!is_subdir(de, base))
432 spin_unlock(&vfsmount_lock);
441 spin_unlock(&vfsmount_lock);
446 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
448 if (vfs_permission(inode, mask) != 0)
450 return proc_check_root(inode);
453 extern struct seq_operations proc_pid_maps_op;
454 static int maps_open(struct inode *inode, struct file *file)
456 struct task_struct *task = proc_task(inode);
457 int ret = seq_open(file, &proc_pid_maps_op);
459 struct seq_file *m = file->private_data;
465 static struct file_operations proc_maps_operations = {
469 .release = seq_release,
472 extern struct seq_operations mounts_op;
473 static int mounts_open(struct inode *inode, struct file *file)
475 struct task_struct *task = proc_task(inode);
476 int ret = seq_open(file, &mounts_op);
479 struct seq_file *m = file->private_data;
480 struct namespace *namespace;
482 namespace = task->namespace;
484 get_namespace(namespace);
488 m->private = namespace;
490 seq_release(inode, file);
497 static int mounts_release(struct inode *inode, struct file *file)
499 struct seq_file *m = file->private_data;
500 struct namespace *namespace = m->private;
501 put_namespace(namespace);
502 return seq_release(inode, file);
505 static struct file_operations proc_mounts_operations = {
509 .release = mounts_release,
512 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
514 static ssize_t proc_info_read(struct file * file, char __user * buf,
515 size_t count, loff_t *ppos)
517 struct inode * inode = file->f_dentry->d_inode;
521 struct task_struct *task = proc_task(inode);
523 if (count > PROC_BLOCK_SIZE)
524 count = PROC_BLOCK_SIZE;
525 if (!(page = __get_free_page(GFP_KERNEL)))
528 length = PROC_I(inode)->op.proc_read(task, (char*)page);
534 /* Static 4kB (or whatever) block capacity */
535 if (*ppos >= length) {
539 if (count + *ppos > length)
540 count = length - *ppos;
542 if (copy_to_user(buf, (char *) page + *ppos, count))
550 static struct file_operations proc_info_file_operations = {
551 .read = proc_info_read,
554 static int mem_open(struct inode* inode, struct file* file)
556 file->private_data = (void*)((long)current->self_exec_id);
560 static ssize_t mem_read(struct file * file, char __user * buf,
561 size_t count, loff_t *ppos)
563 struct task_struct *task = proc_task(file->f_dentry->d_inode);
565 unsigned long src = *ppos;
567 struct mm_struct *mm;
569 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
573 page = (char *)__get_free_page(GFP_USER);
579 mm = get_task_mm(task);
585 if (file->private_data != (void*)((long)current->self_exec_id))
591 int this_len, retval;
593 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
594 retval = access_process_vm(task, src, page, this_len, 0);
595 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
601 if (copy_to_user(buf, page, retval)) {
616 free_page((unsigned long) page);
621 #define mem_write NULL
624 /* This is a security hazard */
625 static ssize_t mem_write(struct file * file, const char * buf,
626 size_t count, loff_t *ppos)
630 struct task_struct *task = proc_task(file->f_dentry->d_inode);
631 unsigned long dst = *ppos;
633 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
636 page = (char *)__get_free_page(GFP_USER);
641 int this_len, retval;
643 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
644 if (copy_from_user(page, buf, this_len)) {
648 retval = access_process_vm(task, dst, page, this_len, 1);
660 free_page((unsigned long) page);
665 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
669 file->f_pos = offset;
672 file->f_pos += offset;
677 force_successful_syscall_return();
681 static struct file_operations proc_mem_operations = {
688 static struct inode_operations proc_mem_inode_operations = {
689 .permission = proc_permission,
692 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
694 struct inode *inode = dentry->d_inode;
697 /* We don't need a base pointer in the /proc filesystem */
700 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
702 error = proc_check_root(inode);
706 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
707 nd->last_type = LAST_BIND;
712 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
713 char __user *buffer, int buflen)
715 struct inode * inode;
716 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
722 inode = dentry->d_inode;
723 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
727 len = tmp + PAGE_SIZE - 1 - path;
731 if (copy_to_user(buffer, path, len))
734 free_page((unsigned long)tmp);
738 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
741 struct inode *inode = dentry->d_inode;
743 struct vfsmount *mnt = NULL;
747 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
749 error = proc_check_root(inode);
753 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
757 error = do_proc_readlink(de, mnt, buffer, buflen);
765 static struct inode_operations proc_pid_link_inode_operations = {
766 .readlink = proc_pid_readlink,
767 .follow_link = proc_pid_follow_link
770 static int pid_alive(struct task_struct *p)
772 BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
773 return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
778 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
780 struct inode *inode = filp->f_dentry->d_inode;
781 struct task_struct *p = proc_task(inode);
782 unsigned int fd, tid, ino;
785 struct files_struct * files;
796 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
800 ino = fake_ino(tid, PROC_TID_INO);
801 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
805 files = get_files_struct(p);
808 spin_lock(&files->file_lock);
809 for (fd = filp->f_pos-2;
811 fd++, filp->f_pos++) {
814 if (!fcheck_files(files, fd))
816 spin_unlock(&files->file_lock);
822 buf[j] = '0' + (i % 10);
826 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
827 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
828 spin_lock(&files->file_lock);
831 spin_lock(&files->file_lock);
833 spin_unlock(&files->file_lock);
834 put_files_struct(files);
840 static int proc_pident_readdir(struct file *filp,
841 void *dirent, filldir_t filldir,
842 struct pid_entry *ents, unsigned int nents)
846 struct dentry *dentry = filp->f_dentry;
847 struct inode *inode = dentry->d_inode;
853 if (!pid_alive(proc_task(inode)))
857 pid = proc_task(inode)->pid;
862 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
868 ino = parent_ino(dentry);
869 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
882 if (filldir(dirent, p->name, p->len, filp->f_pos,
883 fake_ino(pid, p->type), p->mode >> 12) < 0)
895 static int proc_tgid_base_readdir(struct file * filp,
896 void * dirent, filldir_t filldir)
898 return proc_pident_readdir(filp,dirent,filldir,
899 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
902 static int proc_tid_base_readdir(struct file * filp,
903 void * dirent, filldir_t filldir)
905 return proc_pident_readdir(filp,dirent,filldir,
906 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
909 /* building an inode */
911 static int task_dumpable(struct task_struct *task)
914 struct mm_struct *mm;
919 dumpable = mm->dumpable;
925 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
927 struct inode * inode;
928 struct proc_inode *ei;
930 /* We need a new inode */
932 inode = new_inode(sb);
939 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
940 inode->i_ino = fake_ino(task->pid, ino);
942 if (!pid_alive(task))
946 * grab the reference to task.
948 get_task_struct(task);
953 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
954 inode->i_uid = task->euid;
955 inode->i_gid = task->egid;
957 security_task_to_inode(task, inode);
971 * Exceptional case: normally we are not allowed to unhash a busy
972 * directory. In this case, however, we can do it - no aliasing problems
973 * due to the way we treat inodes.
975 * Rewrite the inode's ownerships here because the owning task may have
976 * performed a setuid(), etc.
978 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
980 struct inode *inode = dentry->d_inode;
981 struct task_struct *task = proc_task(inode);
982 if (pid_alive(task)) {
983 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
984 inode->i_uid = task->euid;
985 inode->i_gid = task->egid;
990 security_task_to_inode(task, inode);
997 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
999 struct inode *inode = dentry->d_inode;
1000 struct task_struct *task = proc_task(inode);
1001 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1002 struct files_struct *files;
1004 files = get_files_struct(task);
1006 spin_lock(&files->file_lock);
1007 if (fcheck_files(files, fd)) {
1008 spin_unlock(&files->file_lock);
1009 put_files_struct(files);
1010 if (task_dumpable(task)) {
1011 inode->i_uid = task->euid;
1012 inode->i_gid = task->egid;
1017 security_task_to_inode(task, inode);
1020 spin_unlock(&files->file_lock);
1021 put_files_struct(files);
1027 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1029 struct task_struct *task = proc_task(inode);
1030 spin_lock(&task->proc_lock);
1031 if (task->proc_dentry == dentry)
1032 task->proc_dentry = NULL;
1033 spin_unlock(&task->proc_lock);
1037 static int pid_delete_dentry(struct dentry * dentry)
1039 /* Is the task we represent dead?
1040 * If so, then don't put the dentry on the lru list,
1041 * kill it immediately.
1043 return !pid_alive(proc_task(dentry->d_inode));
1046 static struct dentry_operations tid_fd_dentry_operations =
1048 .d_revalidate = tid_fd_revalidate,
1049 .d_delete = pid_delete_dentry,
1052 static struct dentry_operations pid_dentry_operations =
1054 .d_revalidate = pid_revalidate,
1055 .d_delete = pid_delete_dentry,
1058 static struct dentry_operations pid_base_dentry_operations =
1060 .d_revalidate = pid_revalidate,
1061 .d_iput = pid_base_iput,
1062 .d_delete = pid_delete_dentry,
1067 static unsigned name_to_int(struct dentry *dentry)
1069 const char *name = dentry->d_name.name;
1070 int len = dentry->d_name.len;
1073 if (len > 1 && *name == '0')
1076 unsigned c = *name++ - '0';
1079 if (n >= (~0U-9)/10)
1090 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1092 struct task_struct *task = proc_task(dir);
1093 unsigned fd = name_to_int(dentry);
1095 struct files_struct * files;
1096 struct inode *inode;
1097 struct proc_inode *ei;
1101 if (!pid_alive(task))
1104 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1108 files = get_files_struct(task);
1111 inode->i_mode = S_IFLNK;
1112 spin_lock(&files->file_lock);
1113 file = fcheck_files(files, fd);
1116 if (file->f_mode & 1)
1117 inode->i_mode |= S_IRUSR | S_IXUSR;
1118 if (file->f_mode & 2)
1119 inode->i_mode |= S_IWUSR | S_IXUSR;
1120 spin_unlock(&files->file_lock);
1121 put_files_struct(files);
1122 inode->i_op = &proc_pid_link_inode_operations;
1124 ei->op.proc_get_link = proc_fd_link;
1125 dentry->d_op = &tid_fd_dentry_operations;
1126 d_add(dentry, inode);
1130 spin_unlock(&files->file_lock);
1131 put_files_struct(files);
1135 return ERR_PTR(-ENOENT);
1138 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1139 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1141 static struct file_operations proc_fd_operations = {
1142 .read = generic_read_dir,
1143 .readdir = proc_readfd,
1146 static struct file_operations proc_task_operations = {
1147 .read = generic_read_dir,
1148 .readdir = proc_task_readdir,
1152 * proc directories can do almost nothing..
1154 static struct inode_operations proc_fd_inode_operations = {
1155 .lookup = proc_lookupfd,
1156 .permission = proc_permission,
1159 static struct inode_operations proc_task_inode_operations = {
1160 .lookup = proc_task_lookup,
1161 .permission = proc_permission,
1164 #ifdef CONFIG_SECURITY
1165 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1166 size_t count, loff_t *ppos)
1168 struct inode * inode = file->f_dentry->d_inode;
1172 struct task_struct *task = proc_task(inode);
1174 if (count > PAGE_SIZE)
1176 if (!(page = __get_free_page(GFP_KERNEL)))
1179 length = security_getprocattr(task,
1180 (char*)file->f_dentry->d_name.name,
1181 (void*)page, count);
1186 /* Static 4kB (or whatever) block capacity */
1187 if (*ppos >= length) {
1191 if (count + *ppos > length)
1192 count = length - *ppos;
1193 end = count + *ppos;
1194 if (copy_to_user(buf, (char *) page + *ppos, count))
1202 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1203 size_t count, loff_t *ppos)
1205 struct inode * inode = file->f_dentry->d_inode;
1208 struct task_struct *task = proc_task(inode);
1210 if (count > PAGE_SIZE)
1213 /* No partial writes. */
1216 page = (char*)__get_free_page(GFP_USER);
1220 if (copy_from_user(page, buf, count))
1223 length = security_setprocattr(task,
1224 (char*)file->f_dentry->d_name.name,
1225 (void*)page, count);
1227 free_page((unsigned long) page);
1231 static struct file_operations proc_pid_attr_operations = {
1232 .read = proc_pid_attr_read,
1233 .write = proc_pid_attr_write,
1236 static struct file_operations proc_tid_attr_operations;
1237 static struct inode_operations proc_tid_attr_inode_operations;
1238 static struct file_operations proc_tgid_attr_operations;
1239 static struct inode_operations proc_tgid_attr_inode_operations;
1243 static struct dentry *proc_pident_lookup(struct inode *dir,
1244 struct dentry *dentry,
1245 struct pid_entry *ents)
1247 struct inode *inode;
1249 struct task_struct *task = proc_task(dir);
1250 struct pid_entry *p;
1251 struct proc_inode *ei;
1256 if (!pid_alive(task))
1259 for (p = ents; p->name; p++) {
1260 if (p->len != dentry->d_name.len)
1262 if (!memcmp(dentry->d_name.name, p->name, p->len))
1269 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1274 inode->i_mode = p->mode;
1276 * Yes, it does not scale. And it should not. Don't add
1277 * new entries into /proc/<tgid>/ without very good reasons.
1280 case PROC_TGID_TASK:
1282 inode->i_op = &proc_task_inode_operations;
1283 inode->i_fop = &proc_task_operations;
1288 inode->i_op = &proc_fd_inode_operations;
1289 inode->i_fop = &proc_fd_operations;
1293 inode->i_op = &proc_pid_link_inode_operations;
1294 ei->op.proc_get_link = proc_exe_link;
1298 inode->i_op = &proc_pid_link_inode_operations;
1299 ei->op.proc_get_link = proc_cwd_link;
1302 case PROC_TGID_ROOT:
1303 inode->i_op = &proc_pid_link_inode_operations;
1304 ei->op.proc_get_link = proc_root_link;
1306 case PROC_TID_ENVIRON:
1307 case PROC_TGID_ENVIRON:
1308 inode->i_fop = &proc_info_file_operations;
1309 ei->op.proc_read = proc_pid_environ;
1312 case PROC_TGID_AUXV:
1313 inode->i_fop = &proc_info_file_operations;
1314 ei->op.proc_read = proc_pid_auxv;
1316 case PROC_TID_STATUS:
1317 case PROC_TGID_STATUS:
1318 inode->i_fop = &proc_info_file_operations;
1319 ei->op.proc_read = proc_pid_status;
1322 case PROC_TGID_STAT:
1323 inode->i_fop = &proc_info_file_operations;
1324 ei->op.proc_read = proc_pid_stat;
1326 case PROC_TID_CMDLINE:
1327 case PROC_TGID_CMDLINE:
1328 inode->i_fop = &proc_info_file_operations;
1329 ei->op.proc_read = proc_pid_cmdline;
1331 case PROC_TID_STATM:
1332 case PROC_TGID_STATM:
1333 inode->i_fop = &proc_info_file_operations;
1334 ei->op.proc_read = proc_pid_statm;
1337 case PROC_TGID_MAPS:
1338 inode->i_fop = &proc_maps_operations;
1342 inode->i_op = &proc_mem_inode_operations;
1343 inode->i_fop = &proc_mem_operations;
1345 case PROC_TID_MOUNTS:
1346 case PROC_TGID_MOUNTS:
1347 inode->i_fop = &proc_mounts_operations;
1349 #ifdef CONFIG_SECURITY
1352 inode->i_op = &proc_tid_attr_inode_operations;
1353 inode->i_fop = &proc_tid_attr_operations;
1355 case PROC_TGID_ATTR:
1357 inode->i_op = &proc_tgid_attr_inode_operations;
1358 inode->i_fop = &proc_tgid_attr_operations;
1360 case PROC_TID_ATTR_CURRENT:
1361 case PROC_TGID_ATTR_CURRENT:
1362 case PROC_TID_ATTR_PREV:
1363 case PROC_TGID_ATTR_PREV:
1364 case PROC_TID_ATTR_EXEC:
1365 case PROC_TGID_ATTR_EXEC:
1366 case PROC_TID_ATTR_FSCREATE:
1367 case PROC_TGID_ATTR_FSCREATE:
1368 inode->i_fop = &proc_pid_attr_operations;
1371 #ifdef CONFIG_KALLSYMS
1372 case PROC_TID_WCHAN:
1373 case PROC_TGID_WCHAN:
1374 inode->i_fop = &proc_info_file_operations;
1375 ei->op.proc_read = proc_pid_wchan;
1379 printk("procfs: impossible type (%d)",p->type);
1381 return ERR_PTR(-EINVAL);
1383 dentry->d_op = &pid_dentry_operations;
1384 d_add(dentry, inode);
1388 return ERR_PTR(error);
1391 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1392 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1395 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1396 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1399 static struct file_operations proc_tgid_base_operations = {
1400 .read = generic_read_dir,
1401 .readdir = proc_tgid_base_readdir,
1404 static struct file_operations proc_tid_base_operations = {
1405 .read = generic_read_dir,
1406 .readdir = proc_tid_base_readdir,
1409 static struct inode_operations proc_tgid_base_inode_operations = {
1410 .lookup = proc_tgid_base_lookup,
1413 static struct inode_operations proc_tid_base_inode_operations = {
1414 .lookup = proc_tid_base_lookup,
1417 #ifdef CONFIG_SECURITY
1418 static int proc_tgid_attr_readdir(struct file * filp,
1419 void * dirent, filldir_t filldir)
1421 return proc_pident_readdir(filp,dirent,filldir,
1422 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1425 static int proc_tid_attr_readdir(struct file * filp,
1426 void * dirent, filldir_t filldir)
1428 return proc_pident_readdir(filp,dirent,filldir,
1429 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1432 static struct file_operations proc_tgid_attr_operations = {
1433 .read = generic_read_dir,
1434 .readdir = proc_tgid_attr_readdir,
1437 static struct file_operations proc_tid_attr_operations = {
1438 .read = generic_read_dir,
1439 .readdir = proc_tid_attr_readdir,
1442 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1443 struct dentry *dentry, struct nameidata *nd)
1445 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1448 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1449 struct dentry *dentry, struct nameidata *nd)
1451 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1454 static struct inode_operations proc_tgid_attr_inode_operations = {
1455 .lookup = proc_tgid_attr_lookup,
1458 static struct inode_operations proc_tid_attr_inode_operations = {
1459 .lookup = proc_tid_attr_lookup,
1466 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1470 sprintf(tmp, "%d", current->tgid);
1471 return vfs_readlink(dentry,buffer,buflen,tmp);
1474 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1477 sprintf(tmp, "%d", current->tgid);
1478 return vfs_follow_link(nd,tmp);
1481 static struct inode_operations proc_self_inode_operations = {
1482 .readlink = proc_self_readlink,
1483 .follow_link = proc_self_follow_link,
1487 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1488 * @p: task that should be flushed.
1490 * Drops the /proc/<pid> dcache entry from the hash chains.
1492 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1493 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1494 * if the pid value is immediately reused. This is enforced by
1495 * - caller must acquire spin_lock(p->proc_lock)
1496 * - must be called before detach_pid()
1497 * - proc_pid_lookup acquires proc_lock, and checks that
1498 * the target is not dead by looking at the attach count
1502 struct dentry *proc_pid_unhash(struct task_struct *p)
1504 struct dentry *proc_dentry;
1506 proc_dentry = p->proc_dentry;
1507 if (proc_dentry != NULL) {
1509 spin_lock(&dcache_lock);
1510 if (!d_unhashed(proc_dentry)) {
1511 dget_locked(proc_dentry);
1512 __d_drop(proc_dentry);
1515 spin_unlock(&dcache_lock);
1521 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1522 * @proc_entry: directoy to prune.
1524 * Shrink the /proc directory that was used by the just killed thread.
1527 void proc_pid_flush(struct dentry *proc_dentry)
1529 if(proc_dentry != NULL) {
1530 shrink_dcache_parent(proc_dentry);
1536 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1538 struct task_struct *task;
1539 struct inode *inode;
1540 struct proc_inode *ei;
1544 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1545 inode = new_inode(dir->i_sb);
1547 return ERR_PTR(-ENOMEM);
1549 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1550 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1552 inode->i_mode = S_IFLNK|S_IRWXUGO;
1553 inode->i_uid = inode->i_gid = 0;
1555 inode->i_op = &proc_self_inode_operations;
1556 d_add(dentry, inode);
1559 tgid = name_to_int(dentry);
1563 read_lock(&tasklist_lock);
1564 task = find_task_by_pid(tgid);
1566 get_task_struct(task);
1567 read_unlock(&tasklist_lock);
1571 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1575 put_task_struct(task);
1578 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1579 inode->i_op = &proc_tgid_base_inode_operations;
1580 inode->i_fop = &proc_tgid_base_operations;
1582 inode->i_flags|=S_IMMUTABLE;
1584 dentry->d_op = &pid_base_dentry_operations;
1587 d_add(dentry, inode);
1588 spin_lock(&task->proc_lock);
1589 task->proc_dentry = dentry;
1590 if (!pid_alive(task)) {
1591 dentry = proc_pid_unhash(task);
1594 spin_unlock(&task->proc_lock);
1596 put_task_struct(task);
1598 proc_pid_flush(dentry);
1603 return ERR_PTR(-ENOENT);
1607 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1609 struct task_struct *task;
1610 struct task_struct *leader = proc_task(dir);
1611 struct inode *inode;
1614 tid = name_to_int(dentry);
1618 read_lock(&tasklist_lock);
1619 task = find_task_by_pid(tid);
1621 get_task_struct(task);
1622 read_unlock(&tasklist_lock);
1625 if (leader->tgid != task->tgid)
1628 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1633 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1634 inode->i_op = &proc_tid_base_inode_operations;
1635 inode->i_fop = &proc_tid_base_operations;
1637 inode->i_flags|=S_IMMUTABLE;
1639 dentry->d_op = &pid_base_dentry_operations;
1641 d_add(dentry, inode);
1643 put_task_struct(task);
1646 put_task_struct(task);
1648 return ERR_PTR(-ENOENT);
1651 #define PROC_NUMBUF 10
1652 #define PROC_MAXPIDS 20
1655 * Get a few tgid's to return for filldir - we need to hold the
1656 * tasklist lock while doing this, and we must release it before
1657 * we actually do the filldir itself, so we use a temp buffer..
1659 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1661 struct task_struct *p;
1665 read_lock(&tasklist_lock);
1668 p = find_task_by_pid(version);
1669 if (!thread_group_leader(p))
1676 p = next_task(&init_task);
1678 for ( ; p != &init_task; p = next_task(p)) {
1684 tgids[nr_tgids] = tgid;
1686 if (nr_tgids >= PROC_MAXPIDS)
1689 read_unlock(&tasklist_lock);
1694 * Get a few tid's to return for filldir - we need to hold the
1695 * tasklist lock while doing this, and we must release it before
1696 * we actually do the filldir itself, so we use a temp buffer..
1698 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1700 struct task_struct *leader_task = proc_task(dir);
1701 struct task_struct *task = leader_task;
1705 read_lock(&tasklist_lock);
1707 * The starting point task (leader_task) might be an already
1708 * unlinked task, which cannot be used to access the task-list
1709 * via next_thread().
1711 if (pid_alive(task)) do {
1712 int tid = task->pid;
1716 tids[nr_tids] = tid;
1718 if (nr_tids >= PROC_MAXPIDS)
1720 } while ((task = next_thread(task)) != leader_task);
1721 read_unlock(&tasklist_lock);
1725 /* for the /proc/ directory itself, after non-process stuff has been done */
1726 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1728 unsigned int tgid_array[PROC_MAXPIDS];
1729 char buf[PROC_NUMBUF];
1730 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1731 unsigned int nr_tgids, i;
1734 ino_t ino = fake_ino(0,PROC_TGID_INO);
1735 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1742 * f_version caches the last tgid which was returned from readdir
1744 nr_tgids = get_tgid_list(nr, filp->f_version, tgid_array);
1746 for (i = 0; i < nr_tgids; i++) {
1747 int tgid = tgid_array[i];
1748 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1749 unsigned long j = PROC_NUMBUF;
1752 buf[--j] = '0' + (tgid % 10);
1753 while ((tgid /= 10) != 0);
1755 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1756 filp->f_version = tgid;
1764 /* for the /proc/TGID/task/ directories */
1765 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1767 unsigned int tid_array[PROC_MAXPIDS];
1768 char buf[PROC_NUMBUF];
1769 unsigned int nr_tids, i;
1770 struct dentry *dentry = filp->f_dentry;
1771 struct inode *inode = dentry->d_inode;
1772 int retval = -ENOENT;
1774 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1776 if (!pid_alive(proc_task(inode)))
1783 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1788 ino = parent_ino(dentry);
1789 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1795 nr_tids = get_tid_list(pos, tid_array, inode);
1797 for (i = 0; i < nr_tids; i++) {
1798 unsigned long j = PROC_NUMBUF;
1799 int tid = tid_array[i];
1801 ino = fake_ino(tid,PROC_TID_INO);
1804 buf[--j] = '0' + (tid % 10);
1805 while ((tid /= 10) != 0);
1807 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)