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 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
108 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
110 static struct pid_entry tgid_base_stuff[] = {
111 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
112 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
113 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
114 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
115 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
116 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
117 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
118 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
119 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
120 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
121 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
122 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
123 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
124 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
125 #ifdef CONFIG_SECURITY
126 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
128 #ifdef CONFIG_KALLSYMS
129 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
131 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
132 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
135 static struct pid_entry tid_base_stuff[] = {
136 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
137 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
138 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
139 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
140 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
141 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
142 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
143 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
144 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
145 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
146 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
147 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
148 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
149 #ifdef CONFIG_SECURITY
150 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
152 #ifdef CONFIG_KALLSYMS
153 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
155 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
156 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
160 #ifdef CONFIG_SECURITY
161 static struct pid_entry tgid_attr_stuff[] = {
162 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
163 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
164 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
165 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
168 static struct pid_entry tid_attr_stuff[] = {
169 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
170 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
171 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
172 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
179 static inline struct task_struct *proc_task(struct inode *inode)
181 return PROC_I(inode)->task;
184 static inline int proc_type(struct inode *inode)
186 return PROC_I(inode)->type;
189 int proc_pid_stat(struct task_struct*,char*);
190 int proc_pid_status(struct task_struct*,char*);
191 int proc_pid_statm(struct task_struct*,char*);
192 int proc_pid_cpu(struct task_struct*,char*);
194 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
196 struct task_struct *task = proc_task(inode);
197 struct files_struct *files;
199 int fd = proc_type(inode) - PROC_TID_FD_DIR;
201 files = get_files_struct(task);
203 spin_lock(&files->file_lock);
204 file = fcheck_files(files, fd);
206 *mnt = mntget(file->f_vfsmnt);
207 *dentry = dget(file->f_dentry);
208 spin_unlock(&files->file_lock);
209 put_files_struct(files);
212 spin_unlock(&files->file_lock);
213 put_files_struct(files);
218 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
220 struct vm_area_struct * vma;
221 int result = -ENOENT;
222 struct task_struct *task = proc_task(inode);
223 struct mm_struct * mm = get_task_mm(task);
227 down_read(&mm->mmap_sem);
230 if ((vma->vm_flags & VM_EXECUTABLE) &&
232 *mnt = mntget(vma->vm_file->f_vfsmnt);
233 *dentry = dget(vma->vm_file->f_dentry);
239 up_read(&mm->mmap_sem);
245 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
247 struct fs_struct *fs;
248 int result = -ENOENT;
249 task_lock(proc_task(inode));
250 fs = proc_task(inode)->fs;
252 atomic_inc(&fs->count);
253 task_unlock(proc_task(inode));
255 read_lock(&fs->lock);
256 *mnt = mntget(fs->pwdmnt);
257 *dentry = dget(fs->pwd);
258 read_unlock(&fs->lock);
265 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
267 struct fs_struct *fs;
268 int result = -ENOENT;
269 task_lock(proc_task(inode));
270 fs = proc_task(inode)->fs;
272 atomic_inc(&fs->count);
273 task_unlock(proc_task(inode));
275 read_lock(&fs->lock);
276 *mnt = mntget(fs->rootmnt);
277 *dentry = dget(fs->root);
278 read_unlock(&fs->lock);
285 #define MAY_PTRACE(task) \
286 (task == current || \
287 (task->parent == current && \
288 (task->ptrace & PT_PTRACED) && task->state == TASK_STOPPED && \
289 security_ptrace(current,task) == 0))
291 static int may_ptrace_attach(struct task_struct *task)
299 if (((current->uid != task->euid) ||
300 (current->uid != task->suid) ||
301 (current->uid != task->uid) ||
302 (current->gid != task->egid) ||
303 (current->gid != task->sgid) ||
304 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
307 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
309 if (security_ptrace(current, task))
318 static int proc_pid_environ(struct task_struct *task, char * buffer)
321 struct mm_struct *mm = get_task_mm(task);
323 unsigned int len = mm->env_end - mm->env_start;
326 res = access_process_vm(task, mm->env_start, buffer, len, 0);
327 if (!may_ptrace_attach(task))
334 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
338 struct mm_struct *mm = get_task_mm(task);
342 len = mm->arg_end - mm->arg_start;
347 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
349 // If the nul at the end of args has been overwritten, then
350 // assume application is using setproctitle(3).
351 if (res > 0 && buffer[res-1] != '\0') {
352 len = strnlen(buffer, res);
356 len = mm->env_end - mm->env_start;
357 if (len > PAGE_SIZE - res)
358 len = PAGE_SIZE - res;
359 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
360 res = strnlen(buffer, res);
369 static int proc_pid_auxv(struct task_struct *task, char *buffer)
372 struct mm_struct *mm = get_task_mm(task);
374 unsigned int nwords = 0;
377 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
378 res = nwords * sizeof(mm->saved_auxv[0]);
381 memcpy(buffer, mm->saved_auxv, res);
388 #ifdef CONFIG_KALLSYMS
390 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
391 * Returns the resolved symbol. If that fails, simply return the address.
393 static int proc_pid_wchan(struct task_struct *task, char *buffer)
396 const char *sym_name;
397 unsigned long wchan, size, offset;
400 wchan = get_wchan(task);
402 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
404 return sprintf(buffer, "%s", sym_name);
405 return sprintf(buffer, "%lu", wchan);
407 #endif /* CONFIG_KALLSYMS */
409 /************************************************************************/
410 /* Here the fs part begins */
411 /************************************************************************/
413 /* permission checks */
415 static int proc_check_root(struct inode *inode)
417 struct dentry *de, *base, *root;
418 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
421 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
423 read_lock(¤t->fs->lock);
424 our_vfsmnt = mntget(current->fs->rootmnt);
425 base = dget(current->fs->root);
426 read_unlock(¤t->fs->lock);
428 spin_lock(&vfsmount_lock);
432 while (vfsmnt != our_vfsmnt) {
433 if (vfsmnt == vfsmnt->mnt_parent)
435 de = vfsmnt->mnt_mountpoint;
436 vfsmnt = vfsmnt->mnt_parent;
439 if (!is_subdir(de, base))
441 spin_unlock(&vfsmount_lock);
450 spin_unlock(&vfsmount_lock);
455 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
457 if (vfs_permission(inode, mask) != 0)
459 return proc_check_root(inode);
462 extern struct seq_operations proc_pid_maps_op;
463 static int maps_open(struct inode *inode, struct file *file)
465 struct task_struct *task = proc_task(inode);
466 int ret = seq_open(file, &proc_pid_maps_op);
468 struct seq_file *m = file->private_data;
474 static struct file_operations proc_maps_operations = {
478 .release = seq_release,
481 extern struct seq_operations mounts_op;
482 static int mounts_open(struct inode *inode, struct file *file)
484 struct task_struct *task = proc_task(inode);
485 int ret = seq_open(file, &mounts_op);
488 struct seq_file *m = file->private_data;
489 struct namespace *namespace;
491 namespace = task->namespace;
493 get_namespace(namespace);
497 m->private = namespace;
499 seq_release(inode, file);
506 static int mounts_release(struct inode *inode, struct file *file)
508 struct seq_file *m = file->private_data;
509 struct namespace *namespace = m->private;
510 put_namespace(namespace);
511 return seq_release(inode, file);
514 static struct file_operations proc_mounts_operations = {
518 .release = mounts_release,
521 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
523 static ssize_t proc_info_read(struct file * file, char * buf,
524 size_t count, loff_t *ppos)
526 struct inode * inode = file->f_dentry->d_inode;
530 struct task_struct *task = proc_task(inode);
532 if (count > PROC_BLOCK_SIZE)
533 count = PROC_BLOCK_SIZE;
534 if (!(page = __get_free_page(GFP_KERNEL)))
537 length = PROC_I(inode)->op.proc_read(task, (char*)page);
543 /* Static 4kB (or whatever) block capacity */
544 if (*ppos >= length) {
548 if (count + *ppos > length)
549 count = length - *ppos;
551 if (copy_to_user(buf, (char *) page + *ppos, count))
559 static struct file_operations proc_info_file_operations = {
560 .read = proc_info_read,
563 static int mem_open(struct inode* inode, struct file* file)
565 file->private_data = (void*)((long)current->self_exec_id);
569 static ssize_t mem_read(struct file * file, char * buf,
570 size_t count, loff_t *ppos)
572 struct task_struct *task = proc_task(file->f_dentry->d_inode);
574 unsigned long src = *ppos;
576 struct mm_struct *mm;
578 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
582 page = (char *)__get_free_page(GFP_USER);
588 mm = get_task_mm(task);
594 if (file->private_data != (void*)((long)current->self_exec_id))
600 int this_len, retval;
602 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
603 retval = access_process_vm(task, src, page, this_len, 0);
604 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
610 if (copy_to_user(buf, page, retval)) {
625 free_page((unsigned long) page);
630 #define mem_write NULL
633 /* This is a security hazard */
634 static ssize_t mem_write(struct file * file, const char * buf,
635 size_t count, loff_t *ppos)
639 struct task_struct *task = proc_task(file->f_dentry->d_inode);
640 unsigned long dst = *ppos;
642 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
645 page = (char *)__get_free_page(GFP_USER);
650 int this_len, retval;
652 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
653 if (copy_from_user(page, buf, this_len)) {
657 retval = access_process_vm(task, dst, page, this_len, 1);
669 free_page((unsigned long) page);
674 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
678 file->f_pos = offset;
681 file->f_pos += offset;
686 force_successful_syscall_return();
690 static struct file_operations proc_mem_operations = {
697 static struct inode_operations proc_mem_inode_operations = {
698 .permission = proc_permission,
701 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
703 struct inode *inode = dentry->d_inode;
706 /* We don't need a base pointer in the /proc filesystem */
709 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
711 error = proc_check_root(inode);
715 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
716 nd->last_type = LAST_BIND;
721 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
722 char *buffer, int buflen)
724 struct inode * inode;
725 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
731 inode = dentry->d_inode;
732 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
736 len = tmp + PAGE_SIZE - 1 - path;
740 if (copy_to_user(buffer, path, len))
743 free_page((unsigned long)tmp);
747 static int proc_pid_readlink(struct dentry * dentry, char * buffer, int buflen)
750 struct inode *inode = dentry->d_inode;
752 struct vfsmount *mnt = NULL;
756 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
758 error = proc_check_root(inode);
762 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
766 error = do_proc_readlink(de, mnt, buffer, buflen);
774 static struct inode_operations proc_pid_link_inode_operations = {
775 .readlink = proc_pid_readlink,
776 .follow_link = proc_pid_follow_link
779 static int pid_alive(struct task_struct *p)
781 BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
782 return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
787 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
789 struct inode *inode = filp->f_dentry->d_inode;
790 struct task_struct *p = proc_task(inode);
791 unsigned int fd, tid, ino;
794 struct files_struct * files;
805 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
809 ino = fake_ino(tid, PROC_TID_INO);
810 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
814 files = get_files_struct(p);
817 spin_lock(&files->file_lock);
818 for (fd = filp->f_pos-2;
820 fd++, filp->f_pos++) {
823 if (!fcheck_files(files, fd))
825 spin_unlock(&files->file_lock);
831 buf[j] = '0' + (i % 10);
835 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
836 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
837 spin_lock(&files->file_lock);
840 spin_lock(&files->file_lock);
842 spin_unlock(&files->file_lock);
843 put_files_struct(files);
849 static int proc_pident_readdir(struct file *filp,
850 void *dirent, filldir_t filldir,
851 struct pid_entry *ents, unsigned int nents)
855 struct dentry *dentry = filp->f_dentry;
856 struct inode *inode = dentry->d_inode;
862 if (!pid_alive(proc_task(inode)))
866 pid = proc_task(inode)->pid;
871 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
877 ino = parent_ino(dentry);
878 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
891 if (filldir(dirent, p->name, p->len, filp->f_pos,
892 fake_ino(pid, p->type), p->mode >> 12) < 0)
904 static int proc_tgid_base_readdir(struct file * filp,
905 void * dirent, filldir_t filldir)
907 return proc_pident_readdir(filp,dirent,filldir,
908 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
911 static int proc_tid_base_readdir(struct file * filp,
912 void * dirent, filldir_t filldir)
914 return proc_pident_readdir(filp,dirent,filldir,
915 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
918 /* building an inode */
920 static int task_dumpable(struct task_struct *task)
923 struct mm_struct *mm;
928 dumpable = mm->dumpable;
934 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
936 struct inode * inode;
937 struct proc_inode *ei;
939 /* We need a new inode */
941 inode = new_inode(sb);
948 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
949 inode->i_ino = fake_ino(task->pid, ino);
951 if (!pid_alive(task))
955 * grab the reference to task.
957 get_task_struct(task);
962 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
963 inode->i_uid = task->euid;
964 inode->i_gid = task->egid;
966 inode->i_xid = vx_task_xid(task);
967 security_task_to_inode(task, inode);
981 * Exceptional case: normally we are not allowed to unhash a busy
982 * directory. In this case, however, we can do it - no aliasing problems
983 * due to the way we treat inodes.
985 * Rewrite the inode's ownerships here because the owning task may have
986 * performed a setuid(), etc.
988 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
990 struct inode *inode = dentry->d_inode;
991 struct task_struct *task = proc_task(inode);
993 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
995 /* discard wrong fakeinit */
997 if (pid_alive(task)) {
998 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
999 inode->i_uid = task->euid;
1000 inode->i_gid = task->egid;
1005 security_task_to_inode(task, inode);
1013 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1015 struct inode *inode = dentry->d_inode;
1016 struct task_struct *task = proc_task(inode);
1017 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1018 struct files_struct *files;
1020 files = get_files_struct(task);
1022 spin_lock(&files->file_lock);
1023 if (fcheck_files(files, fd)) {
1024 spin_unlock(&files->file_lock);
1025 put_files_struct(files);
1026 if (task_dumpable(task)) {
1027 inode->i_uid = task->euid;
1028 inode->i_gid = task->egid;
1033 security_task_to_inode(task, inode);
1036 spin_unlock(&files->file_lock);
1037 put_files_struct(files);
1043 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1045 struct task_struct *task = proc_task(inode);
1046 spin_lock(&task->proc_lock);
1047 if (task->proc_dentry == dentry)
1048 task->proc_dentry = NULL;
1049 spin_unlock(&task->proc_lock);
1053 static int pid_delete_dentry(struct dentry * dentry)
1055 /* Is the task we represent dead?
1056 * If so, then don't put the dentry on the lru list,
1057 * kill it immediately.
1059 return !pid_alive(proc_task(dentry->d_inode));
1062 static struct dentry_operations tid_fd_dentry_operations =
1064 .d_revalidate = tid_fd_revalidate,
1065 .d_delete = pid_delete_dentry,
1068 static struct dentry_operations pid_dentry_operations =
1070 .d_revalidate = pid_revalidate,
1071 .d_delete = pid_delete_dentry,
1074 static struct dentry_operations pid_base_dentry_operations =
1076 .d_revalidate = pid_revalidate,
1077 .d_iput = pid_base_iput,
1078 .d_delete = pid_delete_dentry,
1083 static unsigned name_to_int(struct dentry *dentry)
1085 const char *name = dentry->d_name.name;
1086 int len = dentry->d_name.len;
1089 if (len > 1 && *name == '0')
1092 unsigned c = *name++ - '0';
1095 if (n >= (~0U-9)/10)
1106 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1108 struct task_struct *task = proc_task(dir);
1109 unsigned fd = name_to_int(dentry);
1111 struct files_struct * files;
1112 struct inode *inode;
1113 struct proc_inode *ei;
1117 if (!pid_alive(task))
1120 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1124 files = get_files_struct(task);
1127 inode->i_mode = S_IFLNK;
1128 spin_lock(&files->file_lock);
1129 file = fcheck_files(files, fd);
1132 if (file->f_mode & 1)
1133 inode->i_mode |= S_IRUSR | S_IXUSR;
1134 if (file->f_mode & 2)
1135 inode->i_mode |= S_IWUSR | S_IXUSR;
1136 spin_unlock(&files->file_lock);
1137 put_files_struct(files);
1138 inode->i_op = &proc_pid_link_inode_operations;
1140 ei->op.proc_get_link = proc_fd_link;
1141 dentry->d_op = &tid_fd_dentry_operations;
1142 d_add(dentry, inode);
1146 spin_unlock(&files->file_lock);
1147 put_files_struct(files);
1151 return ERR_PTR(-ENOENT);
1154 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1155 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1157 static struct file_operations proc_fd_operations = {
1158 .read = generic_read_dir,
1159 .readdir = proc_readfd,
1162 static struct file_operations proc_task_operations = {
1163 .read = generic_read_dir,
1164 .readdir = proc_task_readdir,
1168 * proc directories can do almost nothing..
1170 static struct inode_operations proc_fd_inode_operations = {
1171 .lookup = proc_lookupfd,
1172 .permission = proc_permission,
1175 static struct inode_operations proc_task_inode_operations = {
1176 .lookup = proc_task_lookup,
1177 .permission = proc_permission,
1180 #ifdef CONFIG_SECURITY
1181 static ssize_t proc_pid_attr_read(struct file * file, char * buf,
1182 size_t count, loff_t *ppos)
1184 struct inode * inode = file->f_dentry->d_inode;
1188 struct task_struct *task = proc_task(inode);
1190 if (count > PAGE_SIZE)
1192 if (!(page = __get_free_page(GFP_KERNEL)))
1195 length = security_getprocattr(task,
1196 (char*)file->f_dentry->d_name.name,
1197 (void*)page, count);
1202 /* Static 4kB (or whatever) block capacity */
1203 if (*ppos >= length) {
1207 if (count + *ppos > length)
1208 count = length - *ppos;
1209 end = count + *ppos;
1210 if (copy_to_user(buf, (char *) page + *ppos, count))
1218 static ssize_t proc_pid_attr_write(struct file * file, const char * buf,
1219 size_t count, loff_t *ppos)
1221 struct inode * inode = file->f_dentry->d_inode;
1224 struct task_struct *task = proc_task(inode);
1226 if (count > PAGE_SIZE)
1229 /* No partial writes. */
1232 page = (char*)__get_free_page(GFP_USER);
1236 if (copy_from_user(page, buf, count))
1239 length = security_setprocattr(task,
1240 (char*)file->f_dentry->d_name.name,
1241 (void*)page, count);
1243 free_page((unsigned long) page);
1247 static struct file_operations proc_pid_attr_operations = {
1248 .read = proc_pid_attr_read,
1249 .write = proc_pid_attr_write,
1252 static struct file_operations proc_tid_attr_operations;
1253 static struct inode_operations proc_tid_attr_inode_operations;
1254 static struct file_operations proc_tgid_attr_operations;
1255 static struct inode_operations proc_tgid_attr_inode_operations;
1259 static struct dentry *proc_pident_lookup(struct inode *dir,
1260 struct dentry *dentry,
1261 struct pid_entry *ents)
1263 struct inode *inode;
1265 struct task_struct *task = proc_task(dir);
1266 struct pid_entry *p;
1267 struct proc_inode *ei;
1272 if (!pid_alive(task))
1275 for (p = ents; p->name; p++) {
1276 if (p->len != dentry->d_name.len)
1278 if (!memcmp(dentry->d_name.name, p->name, p->len))
1285 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1290 inode->i_mode = p->mode;
1292 * Yes, it does not scale. And it should not. Don't add
1293 * new entries into /proc/<tgid>/ without very good reasons.
1296 case PROC_TGID_TASK:
1298 inode->i_op = &proc_task_inode_operations;
1299 inode->i_fop = &proc_task_operations;
1304 inode->i_op = &proc_fd_inode_operations;
1305 inode->i_fop = &proc_fd_operations;
1309 inode->i_op = &proc_pid_link_inode_operations;
1310 ei->op.proc_get_link = proc_exe_link;
1314 inode->i_op = &proc_pid_link_inode_operations;
1315 ei->op.proc_get_link = proc_cwd_link;
1318 case PROC_TGID_ROOT:
1319 inode->i_op = &proc_pid_link_inode_operations;
1320 ei->op.proc_get_link = proc_root_link;
1322 case PROC_TID_ENVIRON:
1323 case PROC_TGID_ENVIRON:
1324 inode->i_fop = &proc_info_file_operations;
1325 ei->op.proc_read = proc_pid_environ;
1328 case PROC_TGID_AUXV:
1329 inode->i_fop = &proc_info_file_operations;
1330 ei->op.proc_read = proc_pid_auxv;
1332 case PROC_TID_STATUS:
1333 case PROC_TGID_STATUS:
1334 inode->i_fop = &proc_info_file_operations;
1335 ei->op.proc_read = proc_pid_status;
1338 case PROC_TGID_STAT:
1339 inode->i_fop = &proc_info_file_operations;
1340 ei->op.proc_read = proc_pid_stat;
1342 case PROC_TID_CMDLINE:
1343 case PROC_TGID_CMDLINE:
1344 inode->i_fop = &proc_info_file_operations;
1345 ei->op.proc_read = proc_pid_cmdline;
1347 case PROC_TID_STATM:
1348 case PROC_TGID_STATM:
1349 inode->i_fop = &proc_info_file_operations;
1350 ei->op.proc_read = proc_pid_statm;
1353 case PROC_TGID_MAPS:
1354 inode->i_fop = &proc_maps_operations;
1358 inode->i_op = &proc_mem_inode_operations;
1359 inode->i_fop = &proc_mem_operations;
1361 case PROC_TID_MOUNTS:
1362 case PROC_TGID_MOUNTS:
1363 inode->i_fop = &proc_mounts_operations;
1365 #ifdef CONFIG_SECURITY
1368 inode->i_op = &proc_tid_attr_inode_operations;
1369 inode->i_fop = &proc_tid_attr_operations;
1371 case PROC_TGID_ATTR:
1373 inode->i_op = &proc_tgid_attr_inode_operations;
1374 inode->i_fop = &proc_tgid_attr_operations;
1376 case PROC_TID_ATTR_CURRENT:
1377 case PROC_TGID_ATTR_CURRENT:
1378 case PROC_TID_ATTR_PREV:
1379 case PROC_TGID_ATTR_PREV:
1380 case PROC_TID_ATTR_EXEC:
1381 case PROC_TGID_ATTR_EXEC:
1382 case PROC_TID_ATTR_FSCREATE:
1383 case PROC_TGID_ATTR_FSCREATE:
1384 inode->i_fop = &proc_pid_attr_operations;
1387 #ifdef CONFIG_KALLSYMS
1388 case PROC_TID_WCHAN:
1389 case PROC_TGID_WCHAN:
1390 inode->i_fop = &proc_info_file_operations;
1391 ei->op.proc_read = proc_pid_wchan;
1394 case PROC_TID_VX_INFO:
1395 case PROC_TGID_VX_INFO:
1396 inode->i_fop = &proc_info_file_operations;
1397 ei->op.proc_read = proc_pid_vx_info;
1399 case PROC_TID_IP_INFO:
1400 case PROC_TGID_IP_INFO:
1401 inode->i_fop = &proc_info_file_operations;
1402 ei->op.proc_read = proc_pid_nx_info;
1405 printk("procfs: impossible type (%d)",p->type);
1407 return ERR_PTR(-EINVAL);
1409 dentry->d_op = &pid_dentry_operations;
1410 d_add(dentry, inode);
1414 return ERR_PTR(error);
1417 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1418 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1421 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1422 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1425 static struct file_operations proc_tgid_base_operations = {
1426 .read = generic_read_dir,
1427 .readdir = proc_tgid_base_readdir,
1430 static struct file_operations proc_tid_base_operations = {
1431 .read = generic_read_dir,
1432 .readdir = proc_tid_base_readdir,
1435 static struct inode_operations proc_tgid_base_inode_operations = {
1436 .lookup = proc_tgid_base_lookup,
1439 static struct inode_operations proc_tid_base_inode_operations = {
1440 .lookup = proc_tid_base_lookup,
1443 #ifdef CONFIG_SECURITY
1444 static int proc_tgid_attr_readdir(struct file * filp,
1445 void * dirent, filldir_t filldir)
1447 return proc_pident_readdir(filp,dirent,filldir,
1448 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1451 static int proc_tid_attr_readdir(struct file * filp,
1452 void * dirent, filldir_t filldir)
1454 return proc_pident_readdir(filp,dirent,filldir,
1455 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1458 static struct file_operations proc_tgid_attr_operations = {
1459 .read = generic_read_dir,
1460 .readdir = proc_tgid_attr_readdir,
1463 static struct file_operations proc_tid_attr_operations = {
1464 .read = generic_read_dir,
1465 .readdir = proc_tid_attr_readdir,
1468 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1469 struct dentry *dentry, struct nameidata *nd)
1471 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1474 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1475 struct dentry *dentry, struct nameidata *nd)
1477 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1480 static struct inode_operations proc_tgid_attr_inode_operations = {
1481 .lookup = proc_tgid_attr_lookup,
1484 static struct inode_operations proc_tid_attr_inode_operations = {
1485 .lookup = proc_tid_attr_lookup,
1492 static int proc_self_readlink(struct dentry *dentry, char *buffer, int buflen)
1495 sprintf(tmp, "%d", current->tgid);
1496 return vfs_readlink(dentry,buffer,buflen,tmp);
1499 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1502 sprintf(tmp, "%d", current->tgid);
1503 return vfs_follow_link(nd,tmp);
1506 static struct inode_operations proc_self_inode_operations = {
1507 .readlink = proc_self_readlink,
1508 .follow_link = proc_self_follow_link,
1512 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1513 * @p: task that should be flushed.
1515 * Drops the /proc/<pid> dcache entry from the hash chains.
1517 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1518 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1519 * if the pid value is immediately reused. This is enforced by
1520 * - caller must acquire spin_lock(p->proc_lock)
1521 * - must be called before detach_pid()
1522 * - proc_pid_lookup acquires proc_lock, and checks that
1523 * the target is not dead by looking at the attach count
1527 struct dentry *proc_pid_unhash(struct task_struct *p)
1529 struct dentry *proc_dentry;
1531 proc_dentry = p->proc_dentry;
1532 if (proc_dentry != NULL) {
1534 spin_lock(&dcache_lock);
1535 if (!d_unhashed(proc_dentry)) {
1536 dget_locked(proc_dentry);
1537 __d_drop(proc_dentry);
1540 spin_unlock(&dcache_lock);
1546 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1547 * @proc_entry: directoy to prune.
1549 * Shrink the /proc directory that was used by the just killed thread.
1552 void proc_pid_flush(struct dentry *proc_dentry)
1554 if(proc_dentry != NULL) {
1555 shrink_dcache_parent(proc_dentry);
1561 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1563 struct task_struct *task;
1564 struct inode *inode;
1565 struct proc_inode *ei;
1569 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1570 inode = new_inode(dir->i_sb);
1572 return ERR_PTR(-ENOMEM);
1574 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1575 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1577 inode->i_mode = S_IFLNK|S_IRWXUGO;
1578 inode->i_uid = inode->i_gid = 0;
1580 inode->i_op = &proc_self_inode_operations;
1581 d_add(dentry, inode);
1584 tgid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1588 read_lock(&tasklist_lock);
1589 task = find_task_by_pid(tgid);
1591 get_task_struct(task);
1592 read_unlock(&tasklist_lock);
1597 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1598 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1601 put_task_struct(task);
1604 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1605 inode->i_op = &proc_tgid_base_inode_operations;
1606 inode->i_fop = &proc_tgid_base_operations;
1608 inode->i_flags|=S_IMMUTABLE;
1610 dentry->d_op = &pid_base_dentry_operations;
1613 d_add(dentry, inode);
1614 spin_lock(&task->proc_lock);
1615 task->proc_dentry = dentry;
1616 if (!pid_alive(task)) {
1617 dentry = proc_pid_unhash(task);
1620 spin_unlock(&task->proc_lock);
1622 put_task_struct(task);
1624 proc_pid_flush(dentry);
1629 return ERR_PTR(-ENOENT);
1633 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1635 struct task_struct *task;
1636 struct task_struct *leader = proc_task(dir);
1637 struct inode *inode;
1640 tid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1644 /* handle fakeinit */
1646 read_lock(&tasklist_lock);
1647 task = find_task_by_pid(tid);
1649 get_task_struct(task);
1650 read_unlock(&tasklist_lock);
1653 if (leader->tgid != task->tgid)
1657 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1658 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1662 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1663 inode->i_op = &proc_tid_base_inode_operations;
1664 inode->i_fop = &proc_tid_base_operations;
1666 inode->i_flags|=S_IMMUTABLE;
1668 dentry->d_op = &pid_base_dentry_operations;
1670 d_add(dentry, inode);
1672 put_task_struct(task);
1675 put_task_struct(task);
1677 return ERR_PTR(-ENOENT);
1680 #define PROC_NUMBUF 10
1681 #define PROC_MAXPIDS 20
1684 * Get a few tgid's to return for filldir - we need to hold the
1685 * tasklist lock while doing this, and we must release it before
1686 * we actually do the filldir itself, so we use a temp buffer..
1688 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1690 struct task_struct *p;
1694 read_lock(&tasklist_lock);
1697 p = find_task_by_pid(version);
1698 if (!thread_group_leader(p))
1705 p = next_task(&init_task);
1707 for ( ; p != &init_task; p = next_task(p)) {
1712 if (!vx_check(vx_task_xid(p), VX_WATCH|VX_IDENT))
1716 tgids[nr_tgids] = vx_map_tgid(current->vx_info, tgid);
1718 if (nr_tgids >= PROC_MAXPIDS)
1721 read_unlock(&tasklist_lock);
1726 * Get a few tid's to return for filldir - we need to hold the
1727 * tasklist lock while doing this, and we must release it before
1728 * we actually do the filldir itself, so we use a temp buffer..
1730 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1732 struct task_struct *leader_task = proc_task(dir);
1733 struct task_struct *task = leader_task;
1737 read_lock(&tasklist_lock);
1739 * The starting point task (leader_task) might be an already
1740 * unlinked task, which cannot be used to access the task-list
1741 * via next_thread().
1743 if (pid_alive(task)) do {
1744 int tid = task->pid;
1746 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1750 tids[nr_tids] = vx_map_tgid(current->vx_info, tid);
1752 if (nr_tids >= PROC_MAXPIDS)
1754 } while ((task = next_thread(task)) != leader_task);
1755 read_unlock(&tasklist_lock);
1759 /* for the /proc/ directory itself, after non-process stuff has been done */
1760 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1762 unsigned int tgid_array[PROC_MAXPIDS];
1763 char buf[PROC_NUMBUF];
1764 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1765 unsigned int nr_tgids, i;
1768 ino_t ino = fake_ino(0,PROC_TGID_INO);
1769 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1776 * f_version caches the last tgid which was returned from readdir
1778 nr_tgids = get_tgid_list(nr, filp->f_version, tgid_array);
1780 for (i = 0; i < nr_tgids; i++) {
1781 int tgid = tgid_array[i];
1782 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1783 unsigned long j = PROC_NUMBUF;
1785 do buf[--j] = '0' + (tgid % 10); while (tgid/=10);
1787 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1788 filp->f_version = tgid;
1796 /* for the /proc/TGID/task/ directories */
1797 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1799 unsigned int tid_array[PROC_MAXPIDS];
1800 char buf[PROC_NUMBUF];
1801 unsigned int nr_tids, i;
1802 struct dentry *dentry = filp->f_dentry;
1803 struct inode *inode = dentry->d_inode;
1804 struct task_struct *task = proc_task(inode);
1805 int retval = -ENOENT;
1807 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1809 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1811 if (!pid_alive(task))
1818 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1823 ino = parent_ino(dentry);
1824 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1830 nr_tids = get_tid_list(pos, tid_array, inode);
1832 for (i = 0; i < nr_tids; i++) {
1833 unsigned long j = PROC_NUMBUF;
1834 int tid = tid_array[i];
1836 ino = fake_ino(tid,PROC_TID_INO);
1839 buf[--j] = '0' + (tid % 10);
1842 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)