4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
16 #include <asm/uaccess.h>
18 #include <linux/config.h>
19 #include <linux/errno.h>
20 #include <linux/time.h>
21 #include <linux/proc_fs.h>
22 #include <linux/stat.h>
23 #include <linux/init.h>
24 #include <linux/file.h>
25 #include <linux/string.h>
26 #include <linux/seq_file.h>
27 #include <linux/namei.h>
28 #include <linux/namespace.h>
30 #include <linux/smp_lock.h>
31 #include <linux/kallsyms.h>
32 #include <linux/mount.h>
33 #include <linux/security.h>
34 #include <linux/ptrace.h>
35 #include <linux/vs_network.h>
36 #include <linux/vs_cvirt.h>
39 * For hysterical raisins we keep the same inumbers as in the old procfs.
40 * Feel free to change the macro below - just keep the range distinct from
41 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
42 * As soon as we'll get a separate superblock we will be able to forget
43 * about magical ranges too.
46 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
48 enum pid_directory_inos {
65 #ifdef CONFIG_SECURITY
67 PROC_TGID_ATTR_CURRENT,
70 PROC_TGID_ATTR_FSCREATE,
90 #ifdef CONFIG_SECURITY
92 PROC_TID_ATTR_CURRENT,
95 PROC_TID_ATTR_FSCREATE,
99 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
109 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
111 static struct pid_entry tgid_base_stuff[] = {
112 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
113 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
114 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
115 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
116 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
117 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
118 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
119 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
120 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
121 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
122 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
123 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
124 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
125 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
126 #ifdef CONFIG_SECURITY
127 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
129 #ifdef CONFIG_KALLSYMS
130 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
132 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
133 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
136 static struct pid_entry tid_base_stuff[] = {
137 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
138 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
139 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
140 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
141 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
142 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
143 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
144 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
145 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
146 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
147 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
148 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
149 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
150 #ifdef CONFIG_SECURITY
151 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
153 #ifdef CONFIG_KALLSYMS
154 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
156 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
157 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
161 #ifdef CONFIG_SECURITY
162 static struct pid_entry tgid_attr_stuff[] = {
163 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
164 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
165 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
166 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
169 static struct pid_entry tid_attr_stuff[] = {
170 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
171 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
172 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
173 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
180 static inline struct task_struct *proc_task(struct inode *inode)
182 return PROC_I(inode)->task;
185 static inline int proc_type(struct inode *inode)
187 return PROC_I(inode)->type;
190 int proc_pid_stat(struct task_struct*,char*);
191 int proc_pid_status(struct task_struct*,char*);
192 int proc_pid_statm(struct task_struct*,char*);
193 int proc_pid_cpu(struct task_struct*,char*);
195 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
197 struct task_struct *task = proc_task(inode);
198 struct files_struct *files;
200 int fd = proc_type(inode) - PROC_TID_FD_DIR;
202 files = get_files_struct(task);
204 spin_lock(&files->file_lock);
205 file = fcheck_files(files, fd);
207 *mnt = mntget(file->f_vfsmnt);
208 *dentry = dget(file->f_dentry);
209 spin_unlock(&files->file_lock);
210 put_files_struct(files);
213 spin_unlock(&files->file_lock);
214 put_files_struct(files);
219 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
221 struct vm_area_struct * vma;
222 int result = -ENOENT;
223 struct task_struct *task = proc_task(inode);
224 struct mm_struct * mm = get_task_mm(task);
228 down_read(&mm->mmap_sem);
231 if ((vma->vm_flags & VM_EXECUTABLE) &&
233 *mnt = mntget(vma->vm_file->f_vfsmnt);
234 *dentry = dget(vma->vm_file->f_dentry);
240 up_read(&mm->mmap_sem);
246 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
248 struct fs_struct *fs;
249 int result = -ENOENT;
250 task_lock(proc_task(inode));
251 fs = proc_task(inode)->fs;
253 atomic_inc(&fs->count);
254 task_unlock(proc_task(inode));
256 read_lock(&fs->lock);
257 *mnt = mntget(fs->pwdmnt);
258 *dentry = dget(fs->pwd);
259 read_unlock(&fs->lock);
266 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
268 struct fs_struct *fs;
269 int result = -ENOENT;
270 task_lock(proc_task(inode));
271 fs = proc_task(inode)->fs;
273 atomic_inc(&fs->count);
274 task_unlock(proc_task(inode));
276 read_lock(&fs->lock);
277 *mnt = mntget(fs->rootmnt);
278 *dentry = dget(fs->root);
279 read_unlock(&fs->lock);
286 #define MAY_PTRACE(task) \
287 (task == current || \
288 (task->parent == current && \
289 (task->ptrace & PT_PTRACED) && task->state == TASK_STOPPED && \
290 security_ptrace(current,task) == 0))
292 static int may_ptrace_attach(struct task_struct *task)
300 if (((current->uid != task->euid) ||
301 (current->uid != task->suid) ||
302 (current->uid != task->uid) ||
303 (current->gid != task->egid) ||
304 (current->gid != task->sgid) ||
305 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
308 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
310 if (security_ptrace(current, task))
319 static int proc_pid_environ(struct task_struct *task, char * buffer)
322 struct mm_struct *mm = get_task_mm(task);
324 unsigned int len = mm->env_end - mm->env_start;
327 res = access_process_vm(task, mm->env_start, buffer, len, 0);
328 if (!may_ptrace_attach(task))
335 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
339 struct mm_struct *mm = get_task_mm(task);
343 len = mm->arg_end - mm->arg_start;
348 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
350 // If the nul at the end of args has been overwritten, then
351 // assume application is using setproctitle(3).
352 if (res > 0 && buffer[res-1] != '\0') {
353 len = strnlen(buffer, res);
357 len = mm->env_end - mm->env_start;
358 if (len > PAGE_SIZE - res)
359 len = PAGE_SIZE - res;
360 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
361 res = strnlen(buffer, res);
370 static int proc_pid_auxv(struct task_struct *task, char *buffer)
373 struct mm_struct *mm = get_task_mm(task);
375 unsigned int nwords = 0;
378 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
379 res = nwords * sizeof(mm->saved_auxv[0]);
382 memcpy(buffer, mm->saved_auxv, res);
389 #ifdef CONFIG_KALLSYMS
391 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
392 * Returns the resolved symbol. If that fails, simply return the address.
394 static int proc_pid_wchan(struct task_struct *task, char *buffer)
397 const char *sym_name;
398 unsigned long wchan, size, offset;
401 wchan = get_wchan(task);
403 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
405 return sprintf(buffer, "%s", sym_name);
406 return sprintf(buffer, "%lu", wchan);
408 #endif /* CONFIG_KALLSYMS */
410 /************************************************************************/
411 /* Here the fs part begins */
412 /************************************************************************/
414 /* permission checks */
416 static int proc_check_root(struct inode *inode)
418 struct dentry *de, *base, *root;
419 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
422 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
424 read_lock(¤t->fs->lock);
425 our_vfsmnt = mntget(current->fs->rootmnt);
426 base = dget(current->fs->root);
427 read_unlock(¤t->fs->lock);
429 spin_lock(&vfsmount_lock);
433 while (vfsmnt != our_vfsmnt) {
434 if (vfsmnt == vfsmnt->mnt_parent)
436 de = vfsmnt->mnt_mountpoint;
437 vfsmnt = vfsmnt->mnt_parent;
440 if (!is_subdir(de, base))
442 spin_unlock(&vfsmount_lock);
451 spin_unlock(&vfsmount_lock);
456 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
458 if (vfs_permission(inode, mask) != 0)
460 return proc_check_root(inode);
463 extern struct seq_operations proc_pid_maps_op;
464 static int maps_open(struct inode *inode, struct file *file)
466 struct task_struct *task = proc_task(inode);
467 int ret = seq_open(file, &proc_pid_maps_op);
469 struct seq_file *m = file->private_data;
475 static struct file_operations proc_maps_operations = {
479 .release = seq_release,
482 extern struct seq_operations mounts_op;
483 static int mounts_open(struct inode *inode, struct file *file)
485 struct task_struct *task = proc_task(inode);
486 int ret = seq_open(file, &mounts_op);
489 struct seq_file *m = file->private_data;
490 struct namespace *namespace;
492 namespace = task->namespace;
494 get_namespace(namespace);
498 m->private = namespace;
500 seq_release(inode, file);
507 static int mounts_release(struct inode *inode, struct file *file)
509 struct seq_file *m = file->private_data;
510 struct namespace *namespace = m->private;
511 put_namespace(namespace);
512 return seq_release(inode, file);
515 static struct file_operations proc_mounts_operations = {
519 .release = mounts_release,
522 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
524 static ssize_t proc_info_read(struct file * file, char __user * buf,
525 size_t count, loff_t *ppos)
527 struct inode * inode = file->f_dentry->d_inode;
531 struct task_struct *task = proc_task(inode);
533 if (count > PROC_BLOCK_SIZE)
534 count = PROC_BLOCK_SIZE;
535 if (!(page = __get_free_page(GFP_KERNEL)))
538 length = PROC_I(inode)->op.proc_read(task, (char*)page);
544 /* Static 4kB (or whatever) block capacity */
545 if (*ppos >= length) {
549 if (count + *ppos > length)
550 count = length - *ppos;
552 if (copy_to_user(buf, (char *) page + *ppos, count))
560 static struct file_operations proc_info_file_operations = {
561 .read = proc_info_read,
564 static int mem_open(struct inode* inode, struct file* file)
566 file->private_data = (void*)((long)current->self_exec_id);
570 static ssize_t mem_read(struct file * file, char __user * buf,
571 size_t count, loff_t *ppos)
573 struct task_struct *task = proc_task(file->f_dentry->d_inode);
575 unsigned long src = *ppos;
577 struct mm_struct *mm;
579 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
583 page = (char *)__get_free_page(GFP_USER);
589 mm = get_task_mm(task);
595 if (file->private_data != (void*)((long)current->self_exec_id))
601 int this_len, retval;
603 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
604 retval = access_process_vm(task, src, page, this_len, 0);
605 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
611 if (copy_to_user(buf, page, retval)) {
626 free_page((unsigned long) page);
631 #define mem_write NULL
634 /* This is a security hazard */
635 static ssize_t mem_write(struct file * file, const char * buf,
636 size_t count, loff_t *ppos)
640 struct task_struct *task = proc_task(file->f_dentry->d_inode);
641 unsigned long dst = *ppos;
643 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
646 page = (char *)__get_free_page(GFP_USER);
651 int this_len, retval;
653 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
654 if (copy_from_user(page, buf, this_len)) {
658 retval = access_process_vm(task, dst, page, this_len, 1);
670 free_page((unsigned long) page);
675 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
679 file->f_pos = offset;
682 file->f_pos += offset;
687 force_successful_syscall_return();
691 static struct file_operations proc_mem_operations = {
698 static struct inode_operations proc_mem_inode_operations = {
699 .permission = proc_permission,
702 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
704 struct inode *inode = dentry->d_inode;
707 /* We don't need a base pointer in the /proc filesystem */
710 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
712 error = proc_check_root(inode);
716 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
717 nd->last_type = LAST_BIND;
722 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
723 char __user *buffer, int buflen)
725 struct inode * inode;
726 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
732 inode = dentry->d_inode;
733 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
737 len = tmp + PAGE_SIZE - 1 - path;
741 if (copy_to_user(buffer, path, len))
744 free_page((unsigned long)tmp);
748 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
751 struct inode *inode = dentry->d_inode;
753 struct vfsmount *mnt = NULL;
757 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
759 error = proc_check_root(inode);
763 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
767 error = do_proc_readlink(de, mnt, buffer, buflen);
775 static struct inode_operations proc_pid_link_inode_operations = {
776 .readlink = proc_pid_readlink,
777 .follow_link = proc_pid_follow_link
780 static int pid_alive(struct task_struct *p)
782 BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
783 return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
788 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
790 struct inode *inode = filp->f_dentry->d_inode;
791 struct task_struct *p = proc_task(inode);
792 unsigned int fd, tid, ino;
795 struct files_struct * files;
806 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
810 ino = fake_ino(tid, PROC_TID_INO);
811 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
815 files = get_files_struct(p);
818 spin_lock(&files->file_lock);
819 for (fd = filp->f_pos-2;
821 fd++, filp->f_pos++) {
824 if (!fcheck_files(files, fd))
826 spin_unlock(&files->file_lock);
832 buf[j] = '0' + (i % 10);
836 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
837 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
838 spin_lock(&files->file_lock);
841 spin_lock(&files->file_lock);
843 spin_unlock(&files->file_lock);
844 put_files_struct(files);
850 static int proc_pident_readdir(struct file *filp,
851 void *dirent, filldir_t filldir,
852 struct pid_entry *ents, unsigned int nents)
856 struct dentry *dentry = filp->f_dentry;
857 struct inode *inode = dentry->d_inode;
863 if (!pid_alive(proc_task(inode)))
867 pid = proc_task(inode)->pid;
872 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
878 ino = parent_ino(dentry);
879 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
892 if (filldir(dirent, p->name, p->len, filp->f_pos,
893 fake_ino(pid, p->type), p->mode >> 12) < 0)
905 static int proc_tgid_base_readdir(struct file * filp,
906 void * dirent, filldir_t filldir)
908 return proc_pident_readdir(filp,dirent,filldir,
909 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
912 static int proc_tid_base_readdir(struct file * filp,
913 void * dirent, filldir_t filldir)
915 return proc_pident_readdir(filp,dirent,filldir,
916 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
919 /* building an inode */
921 static int task_dumpable(struct task_struct *task)
924 struct mm_struct *mm;
929 dumpable = mm->dumpable;
935 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
937 struct inode * inode;
938 struct proc_inode *ei;
940 /* We need a new inode */
942 inode = new_inode(sb);
949 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
950 inode->i_ino = fake_ino(task->pid, ino);
952 if (!pid_alive(task))
956 * grab the reference to task.
958 get_task_struct(task);
963 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
964 inode->i_uid = task->euid;
965 inode->i_gid = task->egid;
967 inode->i_xid = vx_task_xid(task);
968 security_task_to_inode(task, inode);
982 * Exceptional case: normally we are not allowed to unhash a busy
983 * directory. In this case, however, we can do it - no aliasing problems
984 * due to the way we treat inodes.
986 * Rewrite the inode's ownerships here because the owning task may have
987 * performed a setuid(), etc.
989 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
991 struct inode *inode = dentry->d_inode;
992 struct task_struct *task = proc_task(inode);
994 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
996 /* discard wrong fakeinit */
998 if (pid_alive(task)) {
999 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1000 inode->i_uid = task->euid;
1001 inode->i_gid = task->egid;
1006 security_task_to_inode(task, inode);
1014 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1016 struct inode *inode = dentry->d_inode;
1017 struct task_struct *task = proc_task(inode);
1018 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1019 struct files_struct *files;
1021 files = get_files_struct(task);
1023 spin_lock(&files->file_lock);
1024 if (fcheck_files(files, fd)) {
1025 spin_unlock(&files->file_lock);
1026 put_files_struct(files);
1027 if (task_dumpable(task)) {
1028 inode->i_uid = task->euid;
1029 inode->i_gid = task->egid;
1034 security_task_to_inode(task, inode);
1037 spin_unlock(&files->file_lock);
1038 put_files_struct(files);
1044 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1046 struct task_struct *task = proc_task(inode);
1047 spin_lock(&task->proc_lock);
1048 if (task->proc_dentry == dentry)
1049 task->proc_dentry = NULL;
1050 spin_unlock(&task->proc_lock);
1054 static int pid_delete_dentry(struct dentry * dentry)
1056 /* Is the task we represent dead?
1057 * If so, then don't put the dentry on the lru list,
1058 * kill it immediately.
1060 return !pid_alive(proc_task(dentry->d_inode));
1063 static struct dentry_operations tid_fd_dentry_operations =
1065 .d_revalidate = tid_fd_revalidate,
1066 .d_delete = pid_delete_dentry,
1069 static struct dentry_operations pid_dentry_operations =
1071 .d_revalidate = pid_revalidate,
1072 .d_delete = pid_delete_dentry,
1075 static struct dentry_operations pid_base_dentry_operations =
1077 .d_revalidate = pid_revalidate,
1078 .d_iput = pid_base_iput,
1079 .d_delete = pid_delete_dentry,
1084 static unsigned name_to_int(struct dentry *dentry)
1086 const char *name = dentry->d_name.name;
1087 int len = dentry->d_name.len;
1090 if (len > 1 && *name == '0')
1093 unsigned c = *name++ - '0';
1096 if (n >= (~0U-9)/10)
1107 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1109 struct task_struct *task = proc_task(dir);
1110 unsigned fd = name_to_int(dentry);
1112 struct files_struct * files;
1113 struct inode *inode;
1114 struct proc_inode *ei;
1118 if (!pid_alive(task))
1121 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1125 files = get_files_struct(task);
1128 inode->i_mode = S_IFLNK;
1129 spin_lock(&files->file_lock);
1130 file = fcheck_files(files, fd);
1133 if (file->f_mode & 1)
1134 inode->i_mode |= S_IRUSR | S_IXUSR;
1135 if (file->f_mode & 2)
1136 inode->i_mode |= S_IWUSR | S_IXUSR;
1137 spin_unlock(&files->file_lock);
1138 put_files_struct(files);
1139 inode->i_op = &proc_pid_link_inode_operations;
1141 ei->op.proc_get_link = proc_fd_link;
1142 dentry->d_op = &tid_fd_dentry_operations;
1143 d_add(dentry, inode);
1147 spin_unlock(&files->file_lock);
1148 put_files_struct(files);
1152 return ERR_PTR(-ENOENT);
1155 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1156 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1158 static struct file_operations proc_fd_operations = {
1159 .read = generic_read_dir,
1160 .readdir = proc_readfd,
1163 static struct file_operations proc_task_operations = {
1164 .read = generic_read_dir,
1165 .readdir = proc_task_readdir,
1169 * proc directories can do almost nothing..
1171 static struct inode_operations proc_fd_inode_operations = {
1172 .lookup = proc_lookupfd,
1173 .permission = proc_permission,
1176 static struct inode_operations proc_task_inode_operations = {
1177 .lookup = proc_task_lookup,
1178 .permission = proc_permission,
1181 #ifdef CONFIG_SECURITY
1182 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1183 size_t count, loff_t *ppos)
1185 struct inode * inode = file->f_dentry->d_inode;
1189 struct task_struct *task = proc_task(inode);
1191 if (count > PAGE_SIZE)
1193 if (!(page = __get_free_page(GFP_KERNEL)))
1196 length = security_getprocattr(task,
1197 (char*)file->f_dentry->d_name.name,
1198 (void*)page, count);
1203 /* Static 4kB (or whatever) block capacity */
1204 if (*ppos >= length) {
1208 if (count + *ppos > length)
1209 count = length - *ppos;
1210 end = count + *ppos;
1211 if (copy_to_user(buf, (char *) page + *ppos, count))
1219 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1220 size_t count, loff_t *ppos)
1222 struct inode * inode = file->f_dentry->d_inode;
1225 struct task_struct *task = proc_task(inode);
1227 if (count > PAGE_SIZE)
1230 /* No partial writes. */
1233 page = (char*)__get_free_page(GFP_USER);
1237 if (copy_from_user(page, buf, count))
1240 length = security_setprocattr(task,
1241 (char*)file->f_dentry->d_name.name,
1242 (void*)page, count);
1244 free_page((unsigned long) page);
1248 static struct file_operations proc_pid_attr_operations = {
1249 .read = proc_pid_attr_read,
1250 .write = proc_pid_attr_write,
1253 static struct file_operations proc_tid_attr_operations;
1254 static struct inode_operations proc_tid_attr_inode_operations;
1255 static struct file_operations proc_tgid_attr_operations;
1256 static struct inode_operations proc_tgid_attr_inode_operations;
1260 static struct dentry *proc_pident_lookup(struct inode *dir,
1261 struct dentry *dentry,
1262 struct pid_entry *ents)
1264 struct inode *inode;
1266 struct task_struct *task = proc_task(dir);
1267 struct pid_entry *p;
1268 struct proc_inode *ei;
1273 if (!pid_alive(task))
1276 for (p = ents; p->name; p++) {
1277 if (p->len != dentry->d_name.len)
1279 if (!memcmp(dentry->d_name.name, p->name, p->len))
1286 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1291 inode->i_mode = p->mode;
1293 * Yes, it does not scale. And it should not. Don't add
1294 * new entries into /proc/<tgid>/ without very good reasons.
1297 case PROC_TGID_TASK:
1299 inode->i_op = &proc_task_inode_operations;
1300 inode->i_fop = &proc_task_operations;
1305 inode->i_op = &proc_fd_inode_operations;
1306 inode->i_fop = &proc_fd_operations;
1310 inode->i_op = &proc_pid_link_inode_operations;
1311 ei->op.proc_get_link = proc_exe_link;
1315 inode->i_op = &proc_pid_link_inode_operations;
1316 ei->op.proc_get_link = proc_cwd_link;
1319 case PROC_TGID_ROOT:
1320 inode->i_op = &proc_pid_link_inode_operations;
1321 ei->op.proc_get_link = proc_root_link;
1323 case PROC_TID_ENVIRON:
1324 case PROC_TGID_ENVIRON:
1325 inode->i_fop = &proc_info_file_operations;
1326 ei->op.proc_read = proc_pid_environ;
1329 case PROC_TGID_AUXV:
1330 inode->i_fop = &proc_info_file_operations;
1331 ei->op.proc_read = proc_pid_auxv;
1333 case PROC_TID_STATUS:
1334 case PROC_TGID_STATUS:
1335 inode->i_fop = &proc_info_file_operations;
1336 ei->op.proc_read = proc_pid_status;
1339 case PROC_TGID_STAT:
1340 inode->i_fop = &proc_info_file_operations;
1341 ei->op.proc_read = proc_pid_stat;
1343 case PROC_TID_CMDLINE:
1344 case PROC_TGID_CMDLINE:
1345 inode->i_fop = &proc_info_file_operations;
1346 ei->op.proc_read = proc_pid_cmdline;
1348 case PROC_TID_STATM:
1349 case PROC_TGID_STATM:
1350 inode->i_fop = &proc_info_file_operations;
1351 ei->op.proc_read = proc_pid_statm;
1354 case PROC_TGID_MAPS:
1355 inode->i_fop = &proc_maps_operations;
1359 inode->i_op = &proc_mem_inode_operations;
1360 inode->i_fop = &proc_mem_operations;
1362 case PROC_TID_MOUNTS:
1363 case PROC_TGID_MOUNTS:
1364 inode->i_fop = &proc_mounts_operations;
1366 #ifdef CONFIG_SECURITY
1369 inode->i_op = &proc_tid_attr_inode_operations;
1370 inode->i_fop = &proc_tid_attr_operations;
1372 case PROC_TGID_ATTR:
1374 inode->i_op = &proc_tgid_attr_inode_operations;
1375 inode->i_fop = &proc_tgid_attr_operations;
1377 case PROC_TID_ATTR_CURRENT:
1378 case PROC_TGID_ATTR_CURRENT:
1379 case PROC_TID_ATTR_PREV:
1380 case PROC_TGID_ATTR_PREV:
1381 case PROC_TID_ATTR_EXEC:
1382 case PROC_TGID_ATTR_EXEC:
1383 case PROC_TID_ATTR_FSCREATE:
1384 case PROC_TGID_ATTR_FSCREATE:
1385 inode->i_fop = &proc_pid_attr_operations;
1388 #ifdef CONFIG_KALLSYMS
1389 case PROC_TID_WCHAN:
1390 case PROC_TGID_WCHAN:
1391 inode->i_fop = &proc_info_file_operations;
1392 ei->op.proc_read = proc_pid_wchan;
1395 case PROC_TID_VX_INFO:
1396 case PROC_TGID_VX_INFO:
1397 inode->i_fop = &proc_info_file_operations;
1398 ei->op.proc_read = proc_pid_vx_info;
1400 case PROC_TID_IP_INFO:
1401 case PROC_TGID_IP_INFO:
1402 inode->i_fop = &proc_info_file_operations;
1403 ei->op.proc_read = proc_pid_nx_info;
1406 printk("procfs: impossible type (%d)",p->type);
1408 return ERR_PTR(-EINVAL);
1410 dentry->d_op = &pid_dentry_operations;
1411 d_add(dentry, inode);
1415 return ERR_PTR(error);
1418 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1419 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1422 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1423 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1426 static struct file_operations proc_tgid_base_operations = {
1427 .read = generic_read_dir,
1428 .readdir = proc_tgid_base_readdir,
1431 static struct file_operations proc_tid_base_operations = {
1432 .read = generic_read_dir,
1433 .readdir = proc_tid_base_readdir,
1436 static struct inode_operations proc_tgid_base_inode_operations = {
1437 .lookup = proc_tgid_base_lookup,
1440 static struct inode_operations proc_tid_base_inode_operations = {
1441 .lookup = proc_tid_base_lookup,
1444 #ifdef CONFIG_SECURITY
1445 static int proc_tgid_attr_readdir(struct file * filp,
1446 void * dirent, filldir_t filldir)
1448 return proc_pident_readdir(filp,dirent,filldir,
1449 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1452 static int proc_tid_attr_readdir(struct file * filp,
1453 void * dirent, filldir_t filldir)
1455 return proc_pident_readdir(filp,dirent,filldir,
1456 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1459 static struct file_operations proc_tgid_attr_operations = {
1460 .read = generic_read_dir,
1461 .readdir = proc_tgid_attr_readdir,
1464 static struct file_operations proc_tid_attr_operations = {
1465 .read = generic_read_dir,
1466 .readdir = proc_tid_attr_readdir,
1469 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1470 struct dentry *dentry, struct nameidata *nd)
1472 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1475 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1476 struct dentry *dentry, struct nameidata *nd)
1478 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1481 static struct inode_operations proc_tgid_attr_inode_operations = {
1482 .lookup = proc_tgid_attr_lookup,
1485 static struct inode_operations proc_tid_attr_inode_operations = {
1486 .lookup = proc_tid_attr_lookup,
1493 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1497 sprintf(tmp, "%d", current->tgid);
1498 return vfs_readlink(dentry,buffer,buflen,tmp);
1501 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1504 sprintf(tmp, "%d", current->tgid);
1505 return vfs_follow_link(nd,tmp);
1508 static struct inode_operations proc_self_inode_operations = {
1509 .readlink = proc_self_readlink,
1510 .follow_link = proc_self_follow_link,
1514 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1515 * @p: task that should be flushed.
1517 * Drops the /proc/<pid> dcache entry from the hash chains.
1519 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1520 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1521 * if the pid value is immediately reused. This is enforced by
1522 * - caller must acquire spin_lock(p->proc_lock)
1523 * - must be called before detach_pid()
1524 * - proc_pid_lookup acquires proc_lock, and checks that
1525 * the target is not dead by looking at the attach count
1529 struct dentry *proc_pid_unhash(struct task_struct *p)
1531 struct dentry *proc_dentry;
1533 proc_dentry = p->proc_dentry;
1534 if (proc_dentry != NULL) {
1536 spin_lock(&dcache_lock);
1537 if (!d_unhashed(proc_dentry)) {
1538 dget_locked(proc_dentry);
1539 __d_drop(proc_dentry);
1542 spin_unlock(&dcache_lock);
1548 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1549 * @proc_entry: directoy to prune.
1551 * Shrink the /proc directory that was used by the just killed thread.
1554 void proc_pid_flush(struct dentry *proc_dentry)
1556 if(proc_dentry != NULL) {
1557 shrink_dcache_parent(proc_dentry);
1563 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1565 struct task_struct *task;
1566 struct inode *inode;
1567 struct proc_inode *ei;
1571 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1572 inode = new_inode(dir->i_sb);
1574 return ERR_PTR(-ENOMEM);
1576 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1577 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1579 inode->i_mode = S_IFLNK|S_IRWXUGO;
1580 inode->i_uid = inode->i_gid = 0;
1582 inode->i_op = &proc_self_inode_operations;
1583 d_add(dentry, inode);
1586 tgid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1590 read_lock(&tasklist_lock);
1591 task = find_task_by_pid(tgid);
1593 get_task_struct(task);
1594 read_unlock(&tasklist_lock);
1599 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1600 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1603 put_task_struct(task);
1606 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1607 inode->i_op = &proc_tgid_base_inode_operations;
1608 inode->i_fop = &proc_tgid_base_operations;
1610 inode->i_flags|=S_IMMUTABLE;
1612 dentry->d_op = &pid_base_dentry_operations;
1615 d_add(dentry, inode);
1616 spin_lock(&task->proc_lock);
1617 task->proc_dentry = dentry;
1618 if (!pid_alive(task)) {
1619 dentry = proc_pid_unhash(task);
1622 spin_unlock(&task->proc_lock);
1624 put_task_struct(task);
1626 proc_pid_flush(dentry);
1631 return ERR_PTR(-ENOENT);
1635 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1637 struct task_struct *task;
1638 struct task_struct *leader = proc_task(dir);
1639 struct inode *inode;
1642 tid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1646 /* handle fakeinit */
1648 read_lock(&tasklist_lock);
1649 task = find_task_by_pid(tid);
1651 get_task_struct(task);
1652 read_unlock(&tasklist_lock);
1655 if (leader->tgid != task->tgid)
1659 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1660 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1664 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1665 inode->i_op = &proc_tid_base_inode_operations;
1666 inode->i_fop = &proc_tid_base_operations;
1668 inode->i_flags|=S_IMMUTABLE;
1670 dentry->d_op = &pid_base_dentry_operations;
1672 d_add(dentry, inode);
1674 put_task_struct(task);
1677 put_task_struct(task);
1679 return ERR_PTR(-ENOENT);
1682 #define PROC_NUMBUF 10
1683 #define PROC_MAXPIDS 20
1686 * Get a few tgid's to return for filldir - we need to hold the
1687 * tasklist lock while doing this, and we must release it before
1688 * we actually do the filldir itself, so we use a temp buffer..
1690 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1692 struct task_struct *p;
1696 read_lock(&tasklist_lock);
1699 p = find_task_by_pid(version);
1700 if (!thread_group_leader(p))
1707 p = next_task(&init_task);
1709 for ( ; p != &init_task; p = next_task(p)) {
1714 if (!vx_check(vx_task_xid(p), VX_WATCH|VX_IDENT))
1718 tgids[nr_tgids] = vx_map_tgid(current->vx_info, tgid);
1720 if (nr_tgids >= PROC_MAXPIDS)
1723 read_unlock(&tasklist_lock);
1728 * Get a few tid's to return for filldir - we need to hold the
1729 * tasklist lock while doing this, and we must release it before
1730 * we actually do the filldir itself, so we use a temp buffer..
1732 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1734 struct task_struct *leader_task = proc_task(dir);
1735 struct task_struct *task = leader_task;
1739 read_lock(&tasklist_lock);
1741 * The starting point task (leader_task) might be an already
1742 * unlinked task, which cannot be used to access the task-list
1743 * via next_thread().
1745 if (pid_alive(task)) do {
1746 int tid = task->pid;
1748 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1752 tids[nr_tids] = vx_map_tgid(current->vx_info, tid);
1754 if (nr_tids >= PROC_MAXPIDS)
1756 } while ((task = next_thread(task)) != leader_task);
1757 read_unlock(&tasklist_lock);
1761 /* for the /proc/ directory itself, after non-process stuff has been done */
1762 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1764 unsigned int tgid_array[PROC_MAXPIDS];
1765 char buf[PROC_NUMBUF];
1766 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1767 unsigned int nr_tgids, i;
1770 ino_t ino = fake_ino(0,PROC_TGID_INO);
1771 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1778 * f_version caches the last tgid which was returned from readdir
1780 nr_tgids = get_tgid_list(nr, filp->f_version, tgid_array);
1782 for (i = 0; i < nr_tgids; i++) {
1783 int tgid = tgid_array[i];
1784 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1785 unsigned long j = PROC_NUMBUF;
1788 buf[--j] = '0' + (tgid % 10);
1789 while ((tgid /= 10) != 0);
1791 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1792 filp->f_version = tgid;
1800 /* for the /proc/TGID/task/ directories */
1801 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1803 unsigned int tid_array[PROC_MAXPIDS];
1804 char buf[PROC_NUMBUF];
1805 unsigned int nr_tids, i;
1806 struct dentry *dentry = filp->f_dentry;
1807 struct inode *inode = dentry->d_inode;
1808 struct task_struct *task = proc_task(inode);
1809 int retval = -ENOENT;
1811 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1813 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1815 if (!pid_alive(task))
1822 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1827 ino = parent_ino(dentry);
1828 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1834 nr_tids = get_tid_list(pos, tid_array, inode);
1836 for (i = 0; i < nr_tids; i++) {
1837 unsigned long j = PROC_NUMBUF;
1838 int tid = tid_array[i];
1840 ino = fake_ino(tid,PROC_TID_INO);
1843 buf[--j] = '0' + (tid % 10);
1844 while ((tid /= 10) != 0);
1846 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)