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>
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_SCHEDSTATS
67 #ifdef CONFIG_SECURITY
69 PROC_TGID_ATTR_CURRENT,
72 PROC_TGID_ATTR_FSCREATE,
92 #ifdef CONFIG_SCHEDSTATS
95 #ifdef CONFIG_SECURITY
97 PROC_TID_ATTR_CURRENT,
100 PROC_TID_ATTR_FSCREATE,
104 #ifdef CONFIG_DELAY_ACCT
106 PROC_TGID_DELAY_ACCT,
108 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
118 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
120 static struct pid_entry tgid_base_stuff[] = {
121 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
122 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
123 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
124 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
125 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
126 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
127 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
128 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
129 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
130 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
131 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
132 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
133 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
134 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
135 #ifdef CONFIG_SECURITY
136 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
138 #ifdef CONFIG_DELAY_ACCT
139 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
141 #ifdef CONFIG_KALLSYMS
142 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
144 #ifdef CONFIG_SCHEDSTATS
145 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
147 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
148 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
149 #ifdef CONFIG_SCHEDSTATS
150 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
154 static struct pid_entry tid_base_stuff[] = {
155 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
156 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
157 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
158 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
159 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
160 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
161 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
162 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
163 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
164 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
165 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
166 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
167 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
168 #ifdef CONFIG_SECURITY
169 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
171 #ifdef CONFIG_DELAY_ACCT
172 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
174 #ifdef CONFIG_KALLSYMS
175 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
177 #ifdef CONFIG_SCHEDSTATS
178 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
180 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
181 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
182 #ifdef CONFIG_SCHEDSTATS
183 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
188 #ifdef CONFIG_SECURITY
189 static struct pid_entry tgid_attr_stuff[] = {
190 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
191 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
192 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
193 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
196 static struct pid_entry tid_attr_stuff[] = {
197 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
198 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
199 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
200 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
207 static inline struct task_struct *proc_task(struct inode *inode)
209 return PROC_I(inode)->task;
212 static inline int proc_type(struct inode *inode)
214 return PROC_I(inode)->type;
217 int proc_tid_stat(struct task_struct*,char*);
218 int proc_tgid_stat(struct task_struct*,char*);
219 int proc_pid_status(struct task_struct*,char*);
220 int proc_pid_statm(struct task_struct*,char*);
221 #ifdef CONFIG_DELAY_ACCT
222 int proc_pid_delay(struct task_struct*,char*);
225 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
227 struct task_struct *task = proc_task(inode);
228 struct files_struct *files;
230 int fd = proc_type(inode) - PROC_TID_FD_DIR;
232 files = get_files_struct(task);
234 spin_lock(&files->file_lock);
235 file = fcheck_files(files, fd);
237 *mnt = mntget(file->f_vfsmnt);
238 *dentry = dget(file->f_dentry);
239 spin_unlock(&files->file_lock);
240 put_files_struct(files);
243 spin_unlock(&files->file_lock);
244 put_files_struct(files);
249 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
251 struct vm_area_struct * vma;
252 int result = -ENOENT;
253 struct task_struct *task = proc_task(inode);
254 struct mm_struct * mm = get_task_mm(task);
258 down_read(&mm->mmap_sem);
261 if ((vma->vm_flags & VM_EXECUTABLE) &&
263 *mnt = mntget(vma->vm_file->f_vfsmnt);
264 *dentry = dget(vma->vm_file->f_dentry);
270 up_read(&mm->mmap_sem);
276 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
278 struct fs_struct *fs;
279 int result = -ENOENT;
280 task_lock(proc_task(inode));
281 fs = proc_task(inode)->fs;
283 atomic_inc(&fs->count);
284 task_unlock(proc_task(inode));
286 read_lock(&fs->lock);
287 *mnt = mntget(fs->pwdmnt);
288 *dentry = dget(fs->pwd);
289 read_unlock(&fs->lock);
296 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
298 struct fs_struct *fs;
299 int result = -ENOENT;
300 task_lock(proc_task(inode));
301 fs = proc_task(inode)->fs;
303 atomic_inc(&fs->count);
304 task_unlock(proc_task(inode));
306 read_lock(&fs->lock);
307 *mnt = mntget(fs->rootmnt);
308 *dentry = dget(fs->root);
309 read_unlock(&fs->lock);
316 #define MAY_PTRACE(task) \
317 (task == current || \
318 (task->parent == current && \
319 (task->ptrace & PT_PTRACED) && \
320 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
321 security_ptrace(current,task) == 0))
323 static int may_ptrace_attach(struct task_struct *task)
331 if (((current->uid != task->euid) ||
332 (current->uid != task->suid) ||
333 (current->uid != task->uid) ||
334 (current->gid != task->egid) ||
335 (current->gid != task->sgid) ||
336 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
339 if (task->mm->dumpable != 1 && !capable(CAP_SYS_PTRACE))
341 if (security_ptrace(current, task))
350 static int proc_pid_environ(struct task_struct *task, char * buffer)
353 struct mm_struct *mm = get_task_mm(task);
355 unsigned int len = mm->env_end - mm->env_start;
358 res = access_process_vm(task, mm->env_start, buffer, len, 0);
359 if (!may_ptrace_attach(task))
366 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
370 struct mm_struct *mm = get_task_mm(task);
374 goto out_mm; /* Shh! No looking before we're done */
376 len = mm->arg_end - mm->arg_start;
381 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
383 // If the nul at the end of args has been overwritten, then
384 // assume application is using setproctitle(3).
385 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
386 len = strnlen(buffer, res);
390 len = mm->env_end - mm->env_start;
391 if (len > PAGE_SIZE - res)
392 len = PAGE_SIZE - res;
393 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
394 res = strnlen(buffer, res);
403 static int proc_pid_auxv(struct task_struct *task, char *buffer)
406 struct mm_struct *mm = get_task_mm(task);
408 unsigned int nwords = 0;
411 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
412 res = nwords * sizeof(mm->saved_auxv[0]);
415 memcpy(buffer, mm->saved_auxv, res);
422 #ifdef CONFIG_KALLSYMS
424 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
425 * Returns the resolved symbol. If that fails, simply return the address.
427 static int proc_pid_wchan(struct task_struct *task, char *buffer)
430 const char *sym_name;
431 unsigned long wchan, size, offset;
434 wchan = get_wchan(task);
436 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
438 return sprintf(buffer, "%s", sym_name);
439 return sprintf(buffer, "%lu", wchan);
441 #endif /* CONFIG_KALLSYMS */
443 #ifdef CONFIG_SCHEDSTATS
445 * Provides /proc/PID/schedstat
447 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
449 return sprintf(buffer, "%lu %lu %lu\n",
450 task->sched_info.cpu_time,
451 task->sched_info.run_delay,
452 task->sched_info.pcnt);
456 /************************************************************************/
457 /* Here the fs part begins */
458 /************************************************************************/
460 /* permission checks */
462 static int proc_check_root(struct inode *inode)
464 struct dentry *de, *base, *root;
465 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
468 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
470 read_lock(¤t->fs->lock);
471 our_vfsmnt = mntget(current->fs->rootmnt);
472 base = dget(current->fs->root);
473 read_unlock(¤t->fs->lock);
475 spin_lock(&vfsmount_lock);
479 while (vfsmnt != our_vfsmnt) {
480 if (vfsmnt == vfsmnt->mnt_parent)
482 de = vfsmnt->mnt_mountpoint;
483 vfsmnt = vfsmnt->mnt_parent;
486 if (!is_subdir(de, base))
488 spin_unlock(&vfsmount_lock);
497 spin_unlock(&vfsmount_lock);
502 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
504 if (generic_permission(inode, mask, NULL) != 0)
506 return proc_check_root(inode);
509 extern struct seq_operations proc_pid_maps_op;
510 static int maps_open(struct inode *inode, struct file *file)
512 struct task_struct *task = proc_task(inode);
513 int ret = seq_open(file, &proc_pid_maps_op);
515 struct seq_file *m = file->private_data;
521 static struct file_operations proc_maps_operations = {
525 .release = seq_release,
528 extern struct seq_operations mounts_op;
529 static int mounts_open(struct inode *inode, struct file *file)
531 struct task_struct *task = proc_task(inode);
532 int ret = seq_open(file, &mounts_op);
535 struct seq_file *m = file->private_data;
536 struct namespace *namespace;
538 namespace = task->namespace;
540 get_namespace(namespace);
544 m->private = namespace;
546 seq_release(inode, file);
553 static int mounts_release(struct inode *inode, struct file *file)
555 struct seq_file *m = file->private_data;
556 struct namespace *namespace = m->private;
557 put_namespace(namespace);
558 return seq_release(inode, file);
561 static struct file_operations proc_mounts_operations = {
565 .release = mounts_release,
568 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
570 static ssize_t proc_info_read(struct file * file, char __user * buf,
571 size_t count, loff_t *ppos)
573 struct inode * inode = file->f_dentry->d_inode;
576 struct task_struct *task = proc_task(inode);
578 if (count > PROC_BLOCK_SIZE)
579 count = PROC_BLOCK_SIZE;
580 if (!(page = __get_free_page(GFP_KERNEL)))
583 length = PROC_I(inode)->op.proc_read(task, (char*)page);
586 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
591 static struct file_operations proc_info_file_operations = {
592 .read = proc_info_read,
595 static int mem_open(struct inode* inode, struct file* file)
597 file->private_data = (void*)((long)current->self_exec_id);
601 static ssize_t mem_read(struct file * file, char __user * buf,
602 size_t count, loff_t *ppos)
604 struct task_struct *task = proc_task(file->f_dentry->d_inode);
606 unsigned long src = *ppos;
608 struct mm_struct *mm;
610 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
614 page = (char *)__get_free_page(GFP_USER);
620 mm = get_task_mm(task);
626 if (file->private_data != (void*)((long)current->self_exec_id))
632 int this_len, retval;
634 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
635 retval = access_process_vm(task, src, page, this_len, 0);
636 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
642 if (copy_to_user(buf, page, retval)) {
657 free_page((unsigned long) page);
662 #define mem_write NULL
665 /* This is a security hazard */
666 static ssize_t mem_write(struct file * file, const char * buf,
667 size_t count, loff_t *ppos)
671 struct task_struct *task = proc_task(file->f_dentry->d_inode);
672 unsigned long dst = *ppos;
674 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
677 page = (char *)__get_free_page(GFP_USER);
682 int this_len, retval;
684 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
685 if (copy_from_user(page, buf, this_len)) {
689 retval = access_process_vm(task, dst, page, this_len, 1);
701 free_page((unsigned long) page);
706 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
710 file->f_pos = offset;
713 file->f_pos += offset;
718 force_successful_syscall_return();
722 static struct file_operations proc_mem_operations = {
729 static struct inode_operations proc_mem_inode_operations = {
730 .permission = proc_permission,
733 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
735 struct inode *inode = dentry->d_inode;
738 /* We don't need a base pointer in the /proc filesystem */
741 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
743 error = proc_check_root(inode);
747 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
748 nd->last_type = LAST_BIND;
753 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
754 char __user *buffer, int buflen)
756 struct inode * inode;
757 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
763 inode = dentry->d_inode;
764 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
768 len = tmp + PAGE_SIZE - 1 - path;
772 if (copy_to_user(buffer, path, len))
775 free_page((unsigned long)tmp);
779 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
782 struct inode *inode = dentry->d_inode;
784 struct vfsmount *mnt = NULL;
788 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
790 error = proc_check_root(inode);
794 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
798 error = do_proc_readlink(de, mnt, buffer, buflen);
806 static struct inode_operations proc_pid_link_inode_operations = {
807 .readlink = proc_pid_readlink,
808 .follow_link = proc_pid_follow_link
813 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
815 struct inode *inode = filp->f_dentry->d_inode;
816 struct task_struct *p = proc_task(inode);
817 unsigned int fd, tid, ino;
820 struct files_struct * files;
831 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
835 ino = fake_ino(tid, PROC_TID_INO);
836 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
840 files = get_files_struct(p);
843 spin_lock(&files->file_lock);
844 for (fd = filp->f_pos-2;
846 fd++, filp->f_pos++) {
849 if (!fcheck_files(files, fd))
851 spin_unlock(&files->file_lock);
857 buf[j] = '0' + (i % 10);
861 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
862 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
863 spin_lock(&files->file_lock);
866 spin_lock(&files->file_lock);
868 spin_unlock(&files->file_lock);
869 put_files_struct(files);
875 static int proc_pident_readdir(struct file *filp,
876 void *dirent, filldir_t filldir,
877 struct pid_entry *ents, unsigned int nents)
881 struct dentry *dentry = filp->f_dentry;
882 struct inode *inode = dentry->d_inode;
888 if (!pid_alive(proc_task(inode)))
892 pid = proc_task(inode)->pid;
897 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
903 ino = parent_ino(dentry);
904 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
917 if (filldir(dirent, p->name, p->len, filp->f_pos,
918 fake_ino(pid, p->type), p->mode >> 12) < 0)
930 static int proc_tgid_base_readdir(struct file * filp,
931 void * dirent, filldir_t filldir)
933 return proc_pident_readdir(filp,dirent,filldir,
934 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
937 static int proc_tid_base_readdir(struct file * filp,
938 void * dirent, filldir_t filldir)
940 return proc_pident_readdir(filp,dirent,filldir,
941 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
944 /* building an inode */
946 static int task_dumpable(struct task_struct *task)
949 struct mm_struct *mm;
954 dumpable = mm->dumpable;
962 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
964 struct inode * inode;
965 struct proc_inode *ei;
967 /* We need a new inode */
969 inode = new_inode(sb);
976 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
977 inode->i_ino = fake_ino(task->pid, ino);
979 if (!pid_alive(task))
983 * grab the reference to task.
985 get_task_struct(task);
990 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
991 inode->i_uid = task->euid;
992 inode->i_gid = task->egid;
994 inode->i_xid = vx_task_xid(task);
995 security_task_to_inode(task, inode);
1009 * Exceptional case: normally we are not allowed to unhash a busy
1010 * directory. In this case, however, we can do it - no aliasing problems
1011 * due to the way we treat inodes.
1013 * Rewrite the inode's ownerships here because the owning task may have
1014 * performed a setuid(), etc.
1016 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1018 struct inode *inode = dentry->d_inode;
1019 struct task_struct *task = proc_task(inode);
1021 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1023 /* discard wrong fakeinit */
1025 if (pid_alive(task)) {
1026 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1027 inode->i_uid = task->euid;
1028 inode->i_gid = task->egid;
1033 security_task_to_inode(task, inode);
1041 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1043 struct inode *inode = dentry->d_inode;
1044 struct task_struct *task = proc_task(inode);
1045 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1046 struct files_struct *files;
1048 files = get_files_struct(task);
1050 spin_lock(&files->file_lock);
1051 if (fcheck_files(files, fd)) {
1052 spin_unlock(&files->file_lock);
1053 put_files_struct(files);
1054 if (task_dumpable(task)) {
1055 inode->i_uid = task->euid;
1056 inode->i_gid = task->egid;
1061 security_task_to_inode(task, inode);
1064 spin_unlock(&files->file_lock);
1065 put_files_struct(files);
1071 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1073 struct task_struct *task = proc_task(inode);
1074 spin_lock(&task->proc_lock);
1075 if (task->proc_dentry == dentry)
1076 task->proc_dentry = NULL;
1077 spin_unlock(&task->proc_lock);
1081 static int pid_delete_dentry(struct dentry * dentry)
1083 /* Is the task we represent dead?
1084 * If so, then don't put the dentry on the lru list,
1085 * kill it immediately.
1087 return !pid_alive(proc_task(dentry->d_inode));
1090 static struct dentry_operations tid_fd_dentry_operations =
1092 .d_revalidate = tid_fd_revalidate,
1093 .d_delete = pid_delete_dentry,
1096 static struct dentry_operations pid_dentry_operations =
1098 .d_revalidate = pid_revalidate,
1099 .d_delete = pid_delete_dentry,
1102 static struct dentry_operations pid_base_dentry_operations =
1104 .d_revalidate = pid_revalidate,
1105 .d_iput = pid_base_iput,
1106 .d_delete = pid_delete_dentry,
1111 static unsigned name_to_int(struct dentry *dentry)
1113 const char *name = dentry->d_name.name;
1114 int len = dentry->d_name.len;
1117 if (len > 1 && *name == '0')
1120 unsigned c = *name++ - '0';
1123 if (n >= (~0U-9)/10)
1134 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1136 struct task_struct *task = proc_task(dir);
1137 unsigned fd = name_to_int(dentry);
1139 struct files_struct * files;
1140 struct inode *inode;
1141 struct proc_inode *ei;
1145 if (!pid_alive(task))
1148 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1152 files = get_files_struct(task);
1155 inode->i_mode = S_IFLNK;
1156 spin_lock(&files->file_lock);
1157 file = fcheck_files(files, fd);
1160 if (file->f_mode & 1)
1161 inode->i_mode |= S_IRUSR | S_IXUSR;
1162 if (file->f_mode & 2)
1163 inode->i_mode |= S_IWUSR | S_IXUSR;
1164 spin_unlock(&files->file_lock);
1165 put_files_struct(files);
1166 inode->i_op = &proc_pid_link_inode_operations;
1168 ei->op.proc_get_link = proc_fd_link;
1169 dentry->d_op = &tid_fd_dentry_operations;
1170 d_add(dentry, inode);
1174 spin_unlock(&files->file_lock);
1175 put_files_struct(files);
1179 return ERR_PTR(-ENOENT);
1182 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1183 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1185 static struct file_operations proc_fd_operations = {
1186 .read = generic_read_dir,
1187 .readdir = proc_readfd,
1190 static struct file_operations proc_task_operations = {
1191 .read = generic_read_dir,
1192 .readdir = proc_task_readdir,
1196 * proc directories can do almost nothing..
1198 static struct inode_operations proc_fd_inode_operations = {
1199 .lookup = proc_lookupfd,
1200 .permission = proc_permission,
1203 static struct inode_operations proc_task_inode_operations = {
1204 .lookup = proc_task_lookup,
1205 .permission = proc_permission,
1208 #ifdef CONFIG_SECURITY
1209 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1210 size_t count, loff_t *ppos)
1212 struct inode * inode = file->f_dentry->d_inode;
1215 struct task_struct *task = proc_task(inode);
1217 if (count > PAGE_SIZE)
1219 if (!(page = __get_free_page(GFP_KERNEL)))
1222 length = security_getprocattr(task,
1223 (char*)file->f_dentry->d_name.name,
1224 (void*)page, count);
1226 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1231 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1232 size_t count, loff_t *ppos)
1234 struct inode * inode = file->f_dentry->d_inode;
1237 struct task_struct *task = proc_task(inode);
1239 if (count > PAGE_SIZE)
1242 /* No partial writes. */
1245 page = (char*)__get_free_page(GFP_USER);
1249 if (copy_from_user(page, buf, count))
1252 length = security_setprocattr(task,
1253 (char*)file->f_dentry->d_name.name,
1254 (void*)page, count);
1256 free_page((unsigned long) page);
1260 static struct file_operations proc_pid_attr_operations = {
1261 .read = proc_pid_attr_read,
1262 .write = proc_pid_attr_write,
1265 static struct file_operations proc_tid_attr_operations;
1266 static struct inode_operations proc_tid_attr_inode_operations;
1267 static struct file_operations proc_tgid_attr_operations;
1268 static struct inode_operations proc_tgid_attr_inode_operations;
1271 extern int proc_pid_vx_info(struct task_struct *, char *);
1272 extern int proc_pid_nx_info(struct task_struct *, char *);
1275 static struct dentry *proc_pident_lookup(struct inode *dir,
1276 struct dentry *dentry,
1277 struct pid_entry *ents)
1279 struct inode *inode;
1281 struct task_struct *task = proc_task(dir);
1282 struct pid_entry *p;
1283 struct proc_inode *ei;
1288 if (!pid_alive(task))
1291 for (p = ents; p->name; p++) {
1292 if (p->len != dentry->d_name.len)
1294 if (!memcmp(dentry->d_name.name, p->name, p->len))
1301 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1306 inode->i_mode = p->mode;
1308 * Yes, it does not scale. And it should not. Don't add
1309 * new entries into /proc/<tgid>/ without very good reasons.
1312 case PROC_TGID_TASK:
1314 inode->i_op = &proc_task_inode_operations;
1315 inode->i_fop = &proc_task_operations;
1320 inode->i_op = &proc_fd_inode_operations;
1321 inode->i_fop = &proc_fd_operations;
1325 inode->i_op = &proc_pid_link_inode_operations;
1326 ei->op.proc_get_link = proc_exe_link;
1330 inode->i_op = &proc_pid_link_inode_operations;
1331 ei->op.proc_get_link = proc_cwd_link;
1334 case PROC_TGID_ROOT:
1335 inode->i_op = &proc_pid_link_inode_operations;
1336 ei->op.proc_get_link = proc_root_link;
1338 case PROC_TID_ENVIRON:
1339 case PROC_TGID_ENVIRON:
1340 inode->i_fop = &proc_info_file_operations;
1341 ei->op.proc_read = proc_pid_environ;
1344 case PROC_TGID_AUXV:
1345 inode->i_fop = &proc_info_file_operations;
1346 ei->op.proc_read = proc_pid_auxv;
1348 case PROC_TID_STATUS:
1349 case PROC_TGID_STATUS:
1350 inode->i_fop = &proc_info_file_operations;
1351 ei->op.proc_read = proc_pid_status;
1354 inode->i_fop = &proc_info_file_operations;
1355 ei->op.proc_read = proc_tid_stat;
1357 case PROC_TGID_STAT:
1358 inode->i_fop = &proc_info_file_operations;
1359 ei->op.proc_read = proc_tgid_stat;
1361 case PROC_TID_CMDLINE:
1362 case PROC_TGID_CMDLINE:
1363 inode->i_fop = &proc_info_file_operations;
1364 ei->op.proc_read = proc_pid_cmdline;
1366 case PROC_TID_STATM:
1367 case PROC_TGID_STATM:
1368 inode->i_fop = &proc_info_file_operations;
1369 ei->op.proc_read = proc_pid_statm;
1372 case PROC_TGID_MAPS:
1373 inode->i_fop = &proc_maps_operations;
1377 inode->i_op = &proc_mem_inode_operations;
1378 inode->i_fop = &proc_mem_operations;
1380 case PROC_TID_MOUNTS:
1381 case PROC_TGID_MOUNTS:
1382 inode->i_fop = &proc_mounts_operations;
1384 #ifdef CONFIG_SECURITY
1387 inode->i_op = &proc_tid_attr_inode_operations;
1388 inode->i_fop = &proc_tid_attr_operations;
1390 case PROC_TGID_ATTR:
1392 inode->i_op = &proc_tgid_attr_inode_operations;
1393 inode->i_fop = &proc_tgid_attr_operations;
1395 case PROC_TID_ATTR_CURRENT:
1396 case PROC_TGID_ATTR_CURRENT:
1397 case PROC_TID_ATTR_PREV:
1398 case PROC_TGID_ATTR_PREV:
1399 case PROC_TID_ATTR_EXEC:
1400 case PROC_TGID_ATTR_EXEC:
1401 case PROC_TID_ATTR_FSCREATE:
1402 case PROC_TGID_ATTR_FSCREATE:
1403 inode->i_fop = &proc_pid_attr_operations;
1406 #ifdef CONFIG_KALLSYMS
1407 case PROC_TID_WCHAN:
1408 case PROC_TGID_WCHAN:
1409 inode->i_fop = &proc_info_file_operations;
1410 ei->op.proc_read = proc_pid_wchan;
1413 #ifdef CONFIG_SCHEDSTATS
1414 case PROC_TID_SCHEDSTAT:
1415 case PROC_TGID_SCHEDSTAT:
1416 inode->i_fop = &proc_info_file_operations;
1417 ei->op.proc_read = proc_pid_schedstat;
1420 case PROC_TID_VX_INFO:
1421 case PROC_TGID_VX_INFO:
1422 inode->i_fop = &proc_info_file_operations;
1423 ei->op.proc_read = proc_pid_vx_info;
1425 case PROC_TID_IP_INFO:
1426 case PROC_TGID_IP_INFO:
1427 inode->i_fop = &proc_info_file_operations;
1428 ei->op.proc_read = proc_pid_nx_info;
1430 #ifdef CONFIG_DELAY_ACCT
1431 case PROC_TID_DELAY_ACCT:
1432 case PROC_TGID_DELAY_ACCT:
1433 inode->i_fop = &proc_info_file_operations;
1434 ei->op.proc_read = proc_pid_delay;
1437 #ifdef CONFIG_SCHEDSTATS
1438 case PROC_TID_SCHEDSTAT:
1439 case PROC_TGID_SCHEDSTAT:
1440 inode->i_fop = &proc_info_file_operations;
1441 ei->op.proc_read = proc_pid_schedstat;
1445 printk("procfs: impossible type (%d)",p->type);
1447 return ERR_PTR(-EINVAL);
1449 dentry->d_op = &pid_dentry_operations;
1450 d_add(dentry, inode);
1454 return ERR_PTR(error);
1457 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1458 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1461 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1462 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1465 static struct file_operations proc_tgid_base_operations = {
1466 .read = generic_read_dir,
1467 .readdir = proc_tgid_base_readdir,
1470 static struct file_operations proc_tid_base_operations = {
1471 .read = generic_read_dir,
1472 .readdir = proc_tid_base_readdir,
1475 static struct inode_operations proc_tgid_base_inode_operations = {
1476 .lookup = proc_tgid_base_lookup,
1479 static struct inode_operations proc_tid_base_inode_operations = {
1480 .lookup = proc_tid_base_lookup,
1483 #ifdef CONFIG_SECURITY
1484 static int proc_tgid_attr_readdir(struct file * filp,
1485 void * dirent, filldir_t filldir)
1487 return proc_pident_readdir(filp,dirent,filldir,
1488 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1491 static int proc_tid_attr_readdir(struct file * filp,
1492 void * dirent, filldir_t filldir)
1494 return proc_pident_readdir(filp,dirent,filldir,
1495 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1498 static struct file_operations proc_tgid_attr_operations = {
1499 .read = generic_read_dir,
1500 .readdir = proc_tgid_attr_readdir,
1503 static struct file_operations proc_tid_attr_operations = {
1504 .read = generic_read_dir,
1505 .readdir = proc_tid_attr_readdir,
1508 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1509 struct dentry *dentry, struct nameidata *nd)
1511 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1514 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1515 struct dentry *dentry, struct nameidata *nd)
1517 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1520 static struct inode_operations proc_tgid_attr_inode_operations = {
1521 .lookup = proc_tgid_attr_lookup,
1524 static struct inode_operations proc_tid_attr_inode_operations = {
1525 .lookup = proc_tid_attr_lookup,
1532 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1536 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1537 return vfs_readlink(dentry,buffer,buflen,tmp);
1540 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1543 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1544 return vfs_follow_link(nd,tmp);
1547 static struct inode_operations proc_self_inode_operations = {
1548 .readlink = proc_self_readlink,
1549 .follow_link = proc_self_follow_link,
1553 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1554 * @p: task that should be flushed.
1556 * Drops the /proc/<pid> dcache entry from the hash chains.
1558 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1559 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1560 * if the pid value is immediately reused. This is enforced by
1561 * - caller must acquire spin_lock(p->proc_lock)
1562 * - must be called before detach_pid()
1563 * - proc_pid_lookup acquires proc_lock, and checks that
1564 * the target is not dead by looking at the attach count
1568 struct dentry *proc_pid_unhash(struct task_struct *p)
1570 struct dentry *proc_dentry;
1572 proc_dentry = p->proc_dentry;
1573 if (proc_dentry != NULL) {
1575 spin_lock(&dcache_lock);
1576 if (!d_unhashed(proc_dentry)) {
1577 dget_locked(proc_dentry);
1578 __d_drop(proc_dentry);
1581 spin_unlock(&dcache_lock);
1587 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1588 * @proc_entry: directoy to prune.
1590 * Shrink the /proc directory that was used by the just killed thread.
1593 void proc_pid_flush(struct dentry *proc_dentry)
1596 if(proc_dentry != NULL) {
1597 shrink_dcache_parent(proc_dentry);
1603 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1605 struct task_struct *task;
1606 struct inode *inode;
1607 struct proc_inode *ei;
1611 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1612 inode = new_inode(dir->i_sb);
1614 return ERR_PTR(-ENOMEM);
1616 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1617 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1619 inode->i_mode = S_IFLNK|S_IRWXUGO;
1620 inode->i_uid = inode->i_gid = 0;
1622 inode->i_op = &proc_self_inode_operations;
1623 d_add(dentry, inode);
1626 tgid = name_to_int(dentry);
1630 read_lock(&tasklist_lock);
1631 task = find_task_by_pid(tgid);
1633 get_task_struct(task);
1634 read_unlock(&tasklist_lock);
1638 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1641 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1645 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1646 inode->i_op = &proc_tgid_base_inode_operations;
1647 inode->i_fop = &proc_tgid_base_operations;
1649 inode->i_flags|=S_IMMUTABLE;
1651 dentry->d_op = &pid_base_dentry_operations;
1654 d_add(dentry, inode);
1655 spin_lock(&task->proc_lock);
1656 task->proc_dentry = dentry;
1657 if (!pid_alive(task)) {
1658 dentry = proc_pid_unhash(task);
1661 spin_unlock(&task->proc_lock);
1663 put_task_struct(task);
1665 proc_pid_flush(dentry);
1670 put_task_struct(task);
1672 return ERR_PTR(-ENOENT);
1676 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1678 struct task_struct *task;
1679 struct task_struct *leader = proc_task(dir);
1680 struct inode *inode;
1683 tid = name_to_int(dentry);
1686 if (vx_current_initpid(tid))
1689 read_lock(&tasklist_lock);
1690 task = find_task_by_pid(tid);
1692 get_task_struct(task);
1693 read_unlock(&tasklist_lock);
1696 if (leader->tgid != task->tgid)
1699 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1702 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1706 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1707 inode->i_op = &proc_tid_base_inode_operations;
1708 inode->i_fop = &proc_tid_base_operations;
1710 inode->i_flags|=S_IMMUTABLE;
1712 dentry->d_op = &pid_base_dentry_operations;
1714 d_add(dentry, inode);
1716 put_task_struct(task);
1719 put_task_struct(task);
1721 return ERR_PTR(-ENOENT);
1724 #define PROC_NUMBUF 10
1725 #define PROC_MAXPIDS 20
1728 * Get a few tgid'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_tgid_list(int index, unsigned long version, unsigned int *tgids)
1734 struct task_struct *p;
1738 read_lock(&tasklist_lock);
1741 p = find_task_by_real_pid(version);
1742 if (p && !thread_group_leader(p))
1749 p = next_task(&init_task);
1751 for ( ; p != &init_task; p = next_task(p)) {
1756 if (!vx_check(vx_task_xid(p), VX_WATCH|VX_IDENT))
1760 tgids[nr_tgids] = vx_map_tgid(tgid);
1762 if (nr_tgids >= PROC_MAXPIDS)
1765 read_unlock(&tasklist_lock);
1770 * Get a few tid's to return for filldir - we need to hold the
1771 * tasklist lock while doing this, and we must release it before
1772 * we actually do the filldir itself, so we use a temp buffer..
1774 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1776 struct task_struct *leader_task = proc_task(dir);
1777 struct task_struct *task = leader_task;
1781 read_lock(&tasklist_lock);
1783 * The starting point task (leader_task) might be an already
1784 * unlinked task, which cannot be used to access the task-list
1785 * via next_thread().
1787 if (pid_alive(task)) do {
1788 int tid = task->pid;
1790 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1794 tids[nr_tids] = vx_map_pid(tid);
1796 if (nr_tids >= PROC_MAXPIDS)
1798 } while ((task = next_thread(task)) != leader_task);
1799 read_unlock(&tasklist_lock);
1803 /* for the /proc/ directory itself, after non-process stuff has been done */
1804 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1806 unsigned int tgid_array[PROC_MAXPIDS];
1807 char buf[PROC_NUMBUF];
1808 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1809 unsigned int nr_tgids, i;
1813 ino_t ino = fake_ino(0,PROC_TGID_INO);
1814 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1820 /* f_version caches the tgid value that the last readdir call couldn't
1821 * return. lseek aka telldir automagically resets f_version to 0.
1823 next_tgid = filp->f_version;
1824 filp->f_version = 0;
1826 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
1828 /* no more entries ! */
1833 /* do not use the last found pid, reserve it for next_tgid */
1834 if (nr_tgids == PROC_MAXPIDS) {
1836 next_tgid = tgid_array[nr_tgids];
1839 for (i=0;i<nr_tgids;i++) {
1840 int tgid = tgid_array[i];
1841 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1842 unsigned long j = PROC_NUMBUF;
1845 buf[--j] = '0' + (tgid % 10);
1846 while ((tgid /= 10) != 0);
1848 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1849 /* returning this tgid failed, save it as the first
1850 * pid for the next readir call */
1851 filp->f_version = tgid_array[i];
1862 /* for the /proc/TGID/task/ directories */
1863 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1865 unsigned int tid_array[PROC_MAXPIDS];
1866 char buf[PROC_NUMBUF];
1867 unsigned int nr_tids, i;
1868 struct dentry *dentry = filp->f_dentry;
1869 struct inode *inode = dentry->d_inode;
1870 struct task_struct *task = proc_task(inode);
1871 int retval = -ENOENT;
1873 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1875 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1877 if (!pid_alive(task))
1884 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1889 ino = parent_ino(dentry);
1890 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1896 nr_tids = get_tid_list(pos, tid_array, inode);
1898 for (i = 0; i < nr_tids; i++) {
1899 unsigned long j = PROC_NUMBUF;
1900 int tid = tid_array[i];
1902 ino = fake_ino(tid,PROC_TID_INO);
1905 buf[--j] = '0' + (tid % 10);
1906 while ((tid /= 10) != 0);
1908 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)