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;
1272 static struct dentry *proc_pident_lookup(struct inode *dir,
1273 struct dentry *dentry,
1274 struct pid_entry *ents)
1276 struct inode *inode;
1278 struct task_struct *task = proc_task(dir);
1279 struct pid_entry *p;
1280 struct proc_inode *ei;
1285 if (!pid_alive(task))
1288 for (p = ents; p->name; p++) {
1289 if (p->len != dentry->d_name.len)
1291 if (!memcmp(dentry->d_name.name, p->name, p->len))
1298 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1303 inode->i_mode = p->mode;
1305 * Yes, it does not scale. And it should not. Don't add
1306 * new entries into /proc/<tgid>/ without very good reasons.
1309 case PROC_TGID_TASK:
1311 inode->i_op = &proc_task_inode_operations;
1312 inode->i_fop = &proc_task_operations;
1317 inode->i_op = &proc_fd_inode_operations;
1318 inode->i_fop = &proc_fd_operations;
1322 inode->i_op = &proc_pid_link_inode_operations;
1323 ei->op.proc_get_link = proc_exe_link;
1327 inode->i_op = &proc_pid_link_inode_operations;
1328 ei->op.proc_get_link = proc_cwd_link;
1331 case PROC_TGID_ROOT:
1332 inode->i_op = &proc_pid_link_inode_operations;
1333 ei->op.proc_get_link = proc_root_link;
1335 case PROC_TID_ENVIRON:
1336 case PROC_TGID_ENVIRON:
1337 inode->i_fop = &proc_info_file_operations;
1338 ei->op.proc_read = proc_pid_environ;
1341 case PROC_TGID_AUXV:
1342 inode->i_fop = &proc_info_file_operations;
1343 ei->op.proc_read = proc_pid_auxv;
1345 case PROC_TID_STATUS:
1346 case PROC_TGID_STATUS:
1347 inode->i_fop = &proc_info_file_operations;
1348 ei->op.proc_read = proc_pid_status;
1351 inode->i_fop = &proc_info_file_operations;
1352 ei->op.proc_read = proc_tid_stat;
1354 case PROC_TGID_STAT:
1355 inode->i_fop = &proc_info_file_operations;
1356 ei->op.proc_read = proc_tgid_stat;
1358 case PROC_TID_CMDLINE:
1359 case PROC_TGID_CMDLINE:
1360 inode->i_fop = &proc_info_file_operations;
1361 ei->op.proc_read = proc_pid_cmdline;
1363 case PROC_TID_STATM:
1364 case PROC_TGID_STATM:
1365 inode->i_fop = &proc_info_file_operations;
1366 ei->op.proc_read = proc_pid_statm;
1369 case PROC_TGID_MAPS:
1370 inode->i_fop = &proc_maps_operations;
1374 inode->i_op = &proc_mem_inode_operations;
1375 inode->i_fop = &proc_mem_operations;
1377 case PROC_TID_MOUNTS:
1378 case PROC_TGID_MOUNTS:
1379 inode->i_fop = &proc_mounts_operations;
1381 #ifdef CONFIG_SECURITY
1384 inode->i_op = &proc_tid_attr_inode_operations;
1385 inode->i_fop = &proc_tid_attr_operations;
1387 case PROC_TGID_ATTR:
1389 inode->i_op = &proc_tgid_attr_inode_operations;
1390 inode->i_fop = &proc_tgid_attr_operations;
1392 case PROC_TID_ATTR_CURRENT:
1393 case PROC_TGID_ATTR_CURRENT:
1394 case PROC_TID_ATTR_PREV:
1395 case PROC_TGID_ATTR_PREV:
1396 case PROC_TID_ATTR_EXEC:
1397 case PROC_TGID_ATTR_EXEC:
1398 case PROC_TID_ATTR_FSCREATE:
1399 case PROC_TGID_ATTR_FSCREATE:
1400 inode->i_fop = &proc_pid_attr_operations;
1403 #ifdef CONFIG_KALLSYMS
1404 case PROC_TID_WCHAN:
1405 case PROC_TGID_WCHAN:
1406 inode->i_fop = &proc_info_file_operations;
1407 ei->op.proc_read = proc_pid_wchan;
1410 #ifdef CONFIG_SCHEDSTATS
1411 case PROC_TID_SCHEDSTAT:
1412 case PROC_TGID_SCHEDSTAT:
1413 inode->i_fop = &proc_info_file_operations;
1414 ei->op.proc_read = proc_pid_schedstat;
1417 case PROC_TID_VX_INFO:
1418 case PROC_TGID_VX_INFO:
1419 inode->i_fop = &proc_info_file_operations;
1420 ei->op.proc_read = proc_pid_vx_info;
1422 case PROC_TID_IP_INFO:
1423 case PROC_TGID_IP_INFO:
1424 inode->i_fop = &proc_info_file_operations;
1425 ei->op.proc_read = proc_pid_nx_info;
1427 #ifdef CONFIG_DELAY_ACCT
1428 case PROC_TID_DELAY_ACCT:
1429 case PROC_TGID_DELAY_ACCT:
1430 inode->i_fop = &proc_info_file_operations;
1431 ei->op.proc_read = proc_pid_delay;
1434 #ifdef CONFIG_SCHEDSTATS
1435 case PROC_TID_SCHEDSTAT:
1436 case PROC_TGID_SCHEDSTAT:
1437 inode->i_fop = &proc_info_file_operations;
1438 ei->op.proc_read = proc_pid_schedstat;
1442 printk("procfs: impossible type (%d)",p->type);
1444 return ERR_PTR(-EINVAL);
1446 dentry->d_op = &pid_dentry_operations;
1447 d_add(dentry, inode);
1451 return ERR_PTR(error);
1454 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1455 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1458 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1459 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1462 static struct file_operations proc_tgid_base_operations = {
1463 .read = generic_read_dir,
1464 .readdir = proc_tgid_base_readdir,
1467 static struct file_operations proc_tid_base_operations = {
1468 .read = generic_read_dir,
1469 .readdir = proc_tid_base_readdir,
1472 static struct inode_operations proc_tgid_base_inode_operations = {
1473 .lookup = proc_tgid_base_lookup,
1476 static struct inode_operations proc_tid_base_inode_operations = {
1477 .lookup = proc_tid_base_lookup,
1480 #ifdef CONFIG_SECURITY
1481 static int proc_tgid_attr_readdir(struct file * filp,
1482 void * dirent, filldir_t filldir)
1484 return proc_pident_readdir(filp,dirent,filldir,
1485 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1488 static int proc_tid_attr_readdir(struct file * filp,
1489 void * dirent, filldir_t filldir)
1491 return proc_pident_readdir(filp,dirent,filldir,
1492 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1495 static struct file_operations proc_tgid_attr_operations = {
1496 .read = generic_read_dir,
1497 .readdir = proc_tgid_attr_readdir,
1500 static struct file_operations proc_tid_attr_operations = {
1501 .read = generic_read_dir,
1502 .readdir = proc_tid_attr_readdir,
1505 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1506 struct dentry *dentry, struct nameidata *nd)
1508 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1511 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1512 struct dentry *dentry, struct nameidata *nd)
1514 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1517 static struct inode_operations proc_tgid_attr_inode_operations = {
1518 .lookup = proc_tgid_attr_lookup,
1521 static struct inode_operations proc_tid_attr_inode_operations = {
1522 .lookup = proc_tid_attr_lookup,
1529 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1533 sprintf(tmp, "%d", vx_map_pid(current->tgid));
1534 return vfs_readlink(dentry,buffer,buflen,tmp);
1537 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1540 sprintf(tmp, "%d", vx_map_pid(current->tgid));
1541 return vfs_follow_link(nd,tmp);
1544 static struct inode_operations proc_self_inode_operations = {
1545 .readlink = proc_self_readlink,
1546 .follow_link = proc_self_follow_link,
1550 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1551 * @p: task that should be flushed.
1553 * Drops the /proc/<pid> dcache entry from the hash chains.
1555 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1556 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1557 * if the pid value is immediately reused. This is enforced by
1558 * - caller must acquire spin_lock(p->proc_lock)
1559 * - must be called before detach_pid()
1560 * - proc_pid_lookup acquires proc_lock, and checks that
1561 * the target is not dead by looking at the attach count
1565 struct dentry *proc_pid_unhash(struct task_struct *p)
1567 struct dentry *proc_dentry;
1569 proc_dentry = p->proc_dentry;
1570 if (proc_dentry != NULL) {
1572 spin_lock(&dcache_lock);
1573 if (!d_unhashed(proc_dentry)) {
1574 dget_locked(proc_dentry);
1575 __d_drop(proc_dentry);
1578 spin_unlock(&dcache_lock);
1584 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1585 * @proc_entry: directoy to prune.
1587 * Shrink the /proc directory that was used by the just killed thread.
1590 void proc_pid_flush(struct dentry *proc_dentry)
1593 if(proc_dentry != NULL) {
1594 shrink_dcache_parent(proc_dentry);
1600 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1602 struct task_struct *task;
1603 struct inode *inode;
1604 struct proc_inode *ei;
1608 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1609 inode = new_inode(dir->i_sb);
1611 return ERR_PTR(-ENOMEM);
1613 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1614 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1616 inode->i_mode = S_IFLNK|S_IRWXUGO;
1617 inode->i_uid = inode->i_gid = 0;
1619 inode->i_op = &proc_self_inode_operations;
1620 d_add(dentry, inode);
1623 tgid = name_to_int(dentry);
1627 read_lock(&tasklist_lock);
1628 task = find_task_by_pid(tgid);
1630 get_task_struct(task);
1631 read_unlock(&tasklist_lock);
1635 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1638 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1642 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1643 inode->i_op = &proc_tgid_base_inode_operations;
1644 inode->i_fop = &proc_tgid_base_operations;
1646 inode->i_flags|=S_IMMUTABLE;
1648 dentry->d_op = &pid_base_dentry_operations;
1651 d_add(dentry, inode);
1652 spin_lock(&task->proc_lock);
1653 task->proc_dentry = dentry;
1654 if (!pid_alive(task)) {
1655 dentry = proc_pid_unhash(task);
1658 spin_unlock(&task->proc_lock);
1660 put_task_struct(task);
1662 proc_pid_flush(dentry);
1667 put_task_struct(task);
1669 return ERR_PTR(-ENOENT);
1673 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1675 struct task_struct *task;
1676 struct task_struct *leader = proc_task(dir);
1677 struct inode *inode;
1680 tid = name_to_int(dentry);
1683 if (vx_current_initpid(tid))
1686 read_lock(&tasklist_lock);
1687 task = find_task_by_pid(tid);
1689 get_task_struct(task);
1690 read_unlock(&tasklist_lock);
1693 if (leader->tgid != task->tgid)
1696 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1699 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1703 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1704 inode->i_op = &proc_tid_base_inode_operations;
1705 inode->i_fop = &proc_tid_base_operations;
1707 inode->i_flags|=S_IMMUTABLE;
1709 dentry->d_op = &pid_base_dentry_operations;
1711 d_add(dentry, inode);
1713 put_task_struct(task);
1716 put_task_struct(task);
1718 return ERR_PTR(-ENOENT);
1721 #define PROC_NUMBUF 10
1722 #define PROC_MAXPIDS 20
1725 * Get a few tgid's to return for filldir - we need to hold the
1726 * tasklist lock while doing this, and we must release it before
1727 * we actually do the filldir itself, so we use a temp buffer..
1729 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1731 struct task_struct *p;
1735 read_lock(&tasklist_lock);
1738 p = find_task_by_real_pid(version);
1739 if (p && !thread_group_leader(p))
1746 p = next_task(&init_task);
1748 for ( ; p != &init_task; p = next_task(p)) {
1753 if (!vx_check(vx_task_xid(p), VX_WATCH|VX_IDENT))
1757 tgids[nr_tgids] = vx_map_tgid(tgid);
1759 if (nr_tgids >= PROC_MAXPIDS)
1762 read_unlock(&tasklist_lock);
1767 * Get a few tid's to return for filldir - we need to hold the
1768 * tasklist lock while doing this, and we must release it before
1769 * we actually do the filldir itself, so we use a temp buffer..
1771 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1773 struct task_struct *leader_task = proc_task(dir);
1774 struct task_struct *task = leader_task;
1778 read_lock(&tasklist_lock);
1780 * The starting point task (leader_task) might be an already
1781 * unlinked task, which cannot be used to access the task-list
1782 * via next_thread().
1784 if (pid_alive(task)) do {
1785 int tid = task->pid;
1787 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1791 tids[nr_tids] = vx_map_pid(tid);
1793 if (nr_tids >= PROC_MAXPIDS)
1795 } while ((task = next_thread(task)) != leader_task);
1796 read_unlock(&tasklist_lock);
1800 /* for the /proc/ directory itself, after non-process stuff has been done */
1801 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1803 unsigned int tgid_array[PROC_MAXPIDS];
1804 char buf[PROC_NUMBUF];
1805 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1806 unsigned int nr_tgids, i;
1810 ino_t ino = fake_ino(0,PROC_TGID_INO);
1811 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1817 /* f_version caches the tgid value that the last readdir call couldn't
1818 * return. lseek aka telldir automagically resets f_version to 0.
1820 next_tgid = filp->f_version;
1821 filp->f_version = 0;
1823 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
1825 /* no more entries ! */
1830 /* do not use the last found pid, reserve it for next_tgid */
1831 if (nr_tgids == PROC_MAXPIDS) {
1833 next_tgid = tgid_array[nr_tgids];
1836 for (i=0;i<nr_tgids;i++) {
1837 int tgid = tgid_array[i];
1838 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1839 unsigned long j = PROC_NUMBUF;
1842 buf[--j] = '0' + (tgid % 10);
1843 while ((tgid /= 10) != 0);
1845 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1846 /* returning this tgid failed, save it as the first
1847 * pid for the next readir call */
1848 filp->f_version = tgid_array[i];
1859 /* for the /proc/TGID/task/ directories */
1860 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1862 unsigned int tid_array[PROC_MAXPIDS];
1863 char buf[PROC_NUMBUF];
1864 unsigned int nr_tids, i;
1865 struct dentry *dentry = filp->f_dentry;
1866 struct inode *inode = dentry->d_inode;
1867 struct task_struct *task = proc_task(inode);
1868 int retval = -ENOENT;
1870 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1872 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1874 if (!pid_alive(task))
1881 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1886 ino = parent_ino(dentry);
1887 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1893 nr_tids = get_tid_list(pos, tid_array, inode);
1895 for (i = 0; i < nr_tids; i++) {
1896 unsigned long j = PROC_NUMBUF;
1897 int tid = tid_array[i];
1899 ino = fake_ino(tid,PROC_TID_INO);
1902 buf[--j] = '0' + (tid % 10);
1903 while ((tid /= 10) != 0);
1905 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)