4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
16 #include <asm/uaccess.h>
18 #include <linux/config.h>
19 #include <linux/errno.h>
20 #include <linux/time.h>
21 #include <linux/proc_fs.h>
22 #include <linux/stat.h>
23 #include <linux/init.h>
24 #include <linux/file.h>
25 #include <linux/string.h>
26 #include <linux/seq_file.h>
27 #include <linux/namei.h>
28 #include <linux/namespace.h>
30 #include <linux/smp_lock.h>
31 #include <linux/kallsyms.h>
32 #include <linux/mount.h>
33 #include <linux/security.h>
34 #include <linux/ptrace.h>
37 * For hysterical raisins we keep the same inumbers as in the old procfs.
38 * Feel free to change the macro below - just keep the range distinct from
39 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
40 * As soon as we'll get a separate superblock we will be able to forget
41 * about magical ranges too.
44 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
46 enum pid_directory_inos {
63 #ifdef CONFIG_SCHEDSTATS
66 #ifdef CONFIG_SECURITY
68 PROC_TGID_ATTR_CURRENT,
71 PROC_TGID_ATTR_FSCREATE,
89 #ifdef CONFIG_SCHEDSTATS
92 #ifdef CONFIG_SECURITY
94 PROC_TID_ATTR_CURRENT,
97 PROC_TID_ATTR_FSCREATE,
99 #ifdef CONFIG_DELAY_ACCT
101 PROC_TGID_DELAY_ACCT,
103 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
113 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
115 static struct pid_entry tgid_base_stuff[] = {
116 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
117 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
118 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
119 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
120 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
121 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
122 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
123 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
124 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
125 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
126 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
127 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
128 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
129 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
130 #ifdef CONFIG_SECURITY
131 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
133 #ifdef CONFIG_DELAY_ACCT
134 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
136 #ifdef CONFIG_KALLSYMS
137 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
139 #ifdef CONFIG_SCHEDSTATS
140 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
144 static struct pid_entry tid_base_stuff[] = {
145 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
146 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
147 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
148 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
149 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
150 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
151 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
152 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
153 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
154 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
155 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
156 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
157 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
158 #ifdef CONFIG_SECURITY
159 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
161 #ifdef CONFIG_DELAY_ACCT
162 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
164 #ifdef CONFIG_KALLSYMS
165 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
167 #ifdef CONFIG_SCHEDSTATS
168 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
173 #ifdef CONFIG_SECURITY
174 static struct pid_entry tgid_attr_stuff[] = {
175 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
176 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
177 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
178 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
181 static struct pid_entry tid_attr_stuff[] = {
182 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
183 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
184 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
185 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
192 static inline struct task_struct *proc_task(struct inode *inode)
194 return PROC_I(inode)->task;
197 static inline int proc_type(struct inode *inode)
199 return PROC_I(inode)->type;
202 int proc_tid_stat(struct task_struct*,char*);
203 int proc_tgid_stat(struct task_struct*,char*);
204 int proc_pid_status(struct task_struct*,char*);
205 int proc_pid_statm(struct task_struct*,char*);
206 int proc_pid_delay(struct task_struct*,char*);
208 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
210 struct task_struct *task = proc_task(inode);
211 struct files_struct *files;
213 int fd = proc_type(inode) - PROC_TID_FD_DIR;
215 files = get_files_struct(task);
217 spin_lock(&files->file_lock);
218 file = fcheck_files(files, fd);
220 *mnt = mntget(file->f_vfsmnt);
221 *dentry = dget(file->f_dentry);
222 spin_unlock(&files->file_lock);
223 put_files_struct(files);
226 spin_unlock(&files->file_lock);
227 put_files_struct(files);
232 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
234 struct vm_area_struct * vma;
235 int result = -ENOENT;
236 struct task_struct *task = proc_task(inode);
237 struct mm_struct * mm = get_task_mm(task);
241 down_read(&mm->mmap_sem);
244 if ((vma->vm_flags & VM_EXECUTABLE) &&
246 *mnt = mntget(vma->vm_file->f_vfsmnt);
247 *dentry = dget(vma->vm_file->f_dentry);
253 up_read(&mm->mmap_sem);
259 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
261 struct fs_struct *fs;
262 int result = -ENOENT;
263 task_lock(proc_task(inode));
264 fs = proc_task(inode)->fs;
266 atomic_inc(&fs->count);
267 task_unlock(proc_task(inode));
269 read_lock(&fs->lock);
270 *mnt = mntget(fs->pwdmnt);
271 *dentry = dget(fs->pwd);
272 read_unlock(&fs->lock);
279 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
281 struct fs_struct *fs;
282 int result = -ENOENT;
283 task_lock(proc_task(inode));
284 fs = proc_task(inode)->fs;
286 atomic_inc(&fs->count);
287 task_unlock(proc_task(inode));
289 read_lock(&fs->lock);
290 *mnt = mntget(fs->rootmnt);
291 *dentry = dget(fs->root);
292 read_unlock(&fs->lock);
299 #define MAY_PTRACE(task) \
300 (task == current || \
301 (task->parent == current && \
302 (task->ptrace & PT_PTRACED) && \
303 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
304 security_ptrace(current,task) == 0))
306 static int may_ptrace_attach(struct task_struct *task)
314 if (((current->uid != task->euid) ||
315 (current->uid != task->suid) ||
316 (current->uid != task->uid) ||
317 (current->gid != task->egid) ||
318 (current->gid != task->sgid) ||
319 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
322 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
324 if (security_ptrace(current, task))
333 static int proc_pid_environ(struct task_struct *task, char * buffer)
336 struct mm_struct *mm = get_task_mm(task);
338 unsigned int len = mm->env_end - mm->env_start;
341 res = access_process_vm(task, mm->env_start, buffer, len, 0);
342 if (!may_ptrace_attach(task))
349 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
353 struct mm_struct *mm = get_task_mm(task);
357 goto out_mm; /* Shh! No looking before we're done */
359 len = mm->arg_end - mm->arg_start;
364 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
366 // If the nul at the end of args has been overwritten, then
367 // assume application is using setproctitle(3).
368 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
369 len = strnlen(buffer, res);
373 len = mm->env_end - mm->env_start;
374 if (len > PAGE_SIZE - res)
375 len = PAGE_SIZE - res;
376 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
377 res = strnlen(buffer, res);
386 static int proc_pid_auxv(struct task_struct *task, char *buffer)
389 struct mm_struct *mm = get_task_mm(task);
391 unsigned int nwords = 0;
394 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
395 res = nwords * sizeof(mm->saved_auxv[0]);
398 memcpy(buffer, mm->saved_auxv, res);
405 #ifdef CONFIG_KALLSYMS
407 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
408 * Returns the resolved symbol. If that fails, simply return the address.
410 static int proc_pid_wchan(struct task_struct *task, char *buffer)
413 const char *sym_name;
414 unsigned long wchan, size, offset;
417 wchan = get_wchan(task);
419 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
421 return sprintf(buffer, "%s", sym_name);
422 return sprintf(buffer, "%lu", wchan);
424 #endif /* CONFIG_KALLSYMS */
426 #ifdef CONFIG_SCHEDSTATS
428 * Provides /proc/PID/schedstat
430 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
432 return sprintf(buffer, "%lu %lu %lu\n",
433 task->sched_info.cpu_time,
434 task->sched_info.run_delay,
435 task->sched_info.pcnt);
439 /************************************************************************/
440 /* Here the fs part begins */
441 /************************************************************************/
443 /* permission checks */
445 static int proc_check_root(struct inode *inode)
447 struct dentry *de, *base, *root;
448 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
451 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
453 read_lock(¤t->fs->lock);
454 our_vfsmnt = mntget(current->fs->rootmnt);
455 base = dget(current->fs->root);
456 read_unlock(¤t->fs->lock);
458 spin_lock(&vfsmount_lock);
462 while (vfsmnt != our_vfsmnt) {
463 if (vfsmnt == vfsmnt->mnt_parent)
465 de = vfsmnt->mnt_mountpoint;
466 vfsmnt = vfsmnt->mnt_parent;
469 if (!is_subdir(de, base))
471 spin_unlock(&vfsmount_lock);
480 spin_unlock(&vfsmount_lock);
485 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
487 if (generic_permission(inode, mask, NULL) != 0)
489 return proc_check_root(inode);
492 extern struct seq_operations proc_pid_maps_op;
493 static int maps_open(struct inode *inode, struct file *file)
495 struct task_struct *task = proc_task(inode);
496 int ret = seq_open(file, &proc_pid_maps_op);
498 struct seq_file *m = file->private_data;
504 static struct file_operations proc_maps_operations = {
508 .release = seq_release,
511 extern struct seq_operations mounts_op;
512 static int mounts_open(struct inode *inode, struct file *file)
514 struct task_struct *task = proc_task(inode);
515 int ret = seq_open(file, &mounts_op);
518 struct seq_file *m = file->private_data;
519 struct namespace *namespace;
521 namespace = task->namespace;
523 get_namespace(namespace);
527 m->private = namespace;
529 seq_release(inode, file);
536 static int mounts_release(struct inode *inode, struct file *file)
538 struct seq_file *m = file->private_data;
539 struct namespace *namespace = m->private;
540 put_namespace(namespace);
541 return seq_release(inode, file);
544 static struct file_operations proc_mounts_operations = {
548 .release = mounts_release,
551 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
553 static ssize_t proc_info_read(struct file * file, char __user * buf,
554 size_t count, loff_t *ppos)
556 struct inode * inode = file->f_dentry->d_inode;
559 struct task_struct *task = proc_task(inode);
561 if (count > PROC_BLOCK_SIZE)
562 count = PROC_BLOCK_SIZE;
563 if (!(page = __get_free_page(GFP_KERNEL)))
566 length = PROC_I(inode)->op.proc_read(task, (char*)page);
569 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
574 static struct file_operations proc_info_file_operations = {
575 .read = proc_info_read,
578 static int mem_open(struct inode* inode, struct file* file)
580 file->private_data = (void*)((long)current->self_exec_id);
584 static ssize_t mem_read(struct file * file, char __user * buf,
585 size_t count, loff_t *ppos)
587 struct task_struct *task = proc_task(file->f_dentry->d_inode);
589 unsigned long src = *ppos;
591 struct mm_struct *mm;
593 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
597 page = (char *)__get_free_page(GFP_USER);
603 mm = get_task_mm(task);
609 if (file->private_data != (void*)((long)current->self_exec_id))
615 int this_len, retval;
617 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
618 retval = access_process_vm(task, src, page, this_len, 0);
619 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
625 if (copy_to_user(buf, page, retval)) {
640 free_page((unsigned long) page);
645 #define mem_write NULL
648 /* This is a security hazard */
649 static ssize_t mem_write(struct file * file, const char * buf,
650 size_t count, loff_t *ppos)
654 struct task_struct *task = proc_task(file->f_dentry->d_inode);
655 unsigned long dst = *ppos;
657 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
660 page = (char *)__get_free_page(GFP_USER);
665 int this_len, retval;
667 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
668 if (copy_from_user(page, buf, this_len)) {
672 retval = access_process_vm(task, dst, page, this_len, 1);
684 free_page((unsigned long) page);
689 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
693 file->f_pos = offset;
696 file->f_pos += offset;
701 force_successful_syscall_return();
705 static struct file_operations proc_mem_operations = {
712 static struct inode_operations proc_mem_inode_operations = {
713 .permission = proc_permission,
716 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
718 struct inode *inode = dentry->d_inode;
721 /* We don't need a base pointer in the /proc filesystem */
724 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
726 error = proc_check_root(inode);
730 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
731 nd->last_type = LAST_BIND;
736 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
737 char __user *buffer, int buflen)
739 struct inode * inode;
740 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
746 inode = dentry->d_inode;
747 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
751 len = tmp + PAGE_SIZE - 1 - path;
755 if (copy_to_user(buffer, path, len))
758 free_page((unsigned long)tmp);
762 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
765 struct inode *inode = dentry->d_inode;
767 struct vfsmount *mnt = NULL;
771 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
773 error = proc_check_root(inode);
777 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
781 error = do_proc_readlink(de, mnt, buffer, buflen);
789 static struct inode_operations proc_pid_link_inode_operations = {
790 .readlink = proc_pid_readlink,
791 .follow_link = proc_pid_follow_link
796 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
798 struct inode *inode = filp->f_dentry->d_inode;
799 struct task_struct *p = proc_task(inode);
800 unsigned int fd, tid, ino;
803 struct files_struct * files;
814 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
818 ino = fake_ino(tid, PROC_TID_INO);
819 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
823 files = get_files_struct(p);
826 spin_lock(&files->file_lock);
827 for (fd = filp->f_pos-2;
829 fd++, filp->f_pos++) {
832 if (!fcheck_files(files, fd))
834 spin_unlock(&files->file_lock);
840 buf[j] = '0' + (i % 10);
844 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
845 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
846 spin_lock(&files->file_lock);
849 spin_lock(&files->file_lock);
851 spin_unlock(&files->file_lock);
852 put_files_struct(files);
858 static int proc_pident_readdir(struct file *filp,
859 void *dirent, filldir_t filldir,
860 struct pid_entry *ents, unsigned int nents)
864 struct dentry *dentry = filp->f_dentry;
865 struct inode *inode = dentry->d_inode;
871 if (!pid_alive(proc_task(inode)))
875 pid = proc_task(inode)->pid;
880 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
886 ino = parent_ino(dentry);
887 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
900 if (filldir(dirent, p->name, p->len, filp->f_pos,
901 fake_ino(pid, p->type), p->mode >> 12) < 0)
913 static int proc_tgid_base_readdir(struct file * filp,
914 void * dirent, filldir_t filldir)
916 return proc_pident_readdir(filp,dirent,filldir,
917 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
920 static int proc_tid_base_readdir(struct file * filp,
921 void * dirent, filldir_t filldir)
923 return proc_pident_readdir(filp,dirent,filldir,
924 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
927 /* building an inode */
929 static int task_dumpable(struct task_struct *task)
932 struct mm_struct *mm;
937 dumpable = mm->dumpable;
943 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
945 struct inode * inode;
946 struct proc_inode *ei;
948 /* We need a new inode */
950 inode = new_inode(sb);
957 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
958 inode->i_ino = fake_ino(task->pid, ino);
960 if (!pid_alive(task))
964 * grab the reference to task.
966 get_task_struct(task);
971 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
972 inode->i_uid = task->euid;
973 inode->i_gid = task->egid;
975 security_task_to_inode(task, inode);
989 * Exceptional case: normally we are not allowed to unhash a busy
990 * directory. In this case, however, we can do it - no aliasing problems
991 * due to the way we treat inodes.
993 * Rewrite the inode's ownerships here because the owning task may have
994 * performed a setuid(), etc.
996 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
998 struct inode *inode = dentry->d_inode;
999 struct task_struct *task = proc_task(inode);
1000 if (pid_alive(task)) {
1001 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1002 inode->i_uid = task->euid;
1003 inode->i_gid = task->egid;
1008 security_task_to_inode(task, inode);
1015 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1017 struct inode *inode = dentry->d_inode;
1018 struct task_struct *task = proc_task(inode);
1019 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1020 struct files_struct *files;
1022 files = get_files_struct(task);
1024 spin_lock(&files->file_lock);
1025 if (fcheck_files(files, fd)) {
1026 spin_unlock(&files->file_lock);
1027 put_files_struct(files);
1028 if (task_dumpable(task)) {
1029 inode->i_uid = task->euid;
1030 inode->i_gid = task->egid;
1035 security_task_to_inode(task, inode);
1038 spin_unlock(&files->file_lock);
1039 put_files_struct(files);
1045 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1047 struct task_struct *task = proc_task(inode);
1048 spin_lock(&task->proc_lock);
1049 if (task->proc_dentry == dentry)
1050 task->proc_dentry = NULL;
1051 spin_unlock(&task->proc_lock);
1055 static int pid_delete_dentry(struct dentry * dentry)
1057 /* Is the task we represent dead?
1058 * If so, then don't put the dentry on the lru list,
1059 * kill it immediately.
1061 return !pid_alive(proc_task(dentry->d_inode));
1064 static struct dentry_operations tid_fd_dentry_operations =
1066 .d_revalidate = tid_fd_revalidate,
1067 .d_delete = pid_delete_dentry,
1070 static struct dentry_operations pid_dentry_operations =
1072 .d_revalidate = pid_revalidate,
1073 .d_delete = pid_delete_dentry,
1076 static struct dentry_operations pid_base_dentry_operations =
1078 .d_revalidate = pid_revalidate,
1079 .d_iput = pid_base_iput,
1080 .d_delete = pid_delete_dentry,
1085 static unsigned name_to_int(struct dentry *dentry)
1087 const char *name = dentry->d_name.name;
1088 int len = dentry->d_name.len;
1091 if (len > 1 && *name == '0')
1094 unsigned c = *name++ - '0';
1097 if (n >= (~0U-9)/10)
1108 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1110 struct task_struct *task = proc_task(dir);
1111 unsigned fd = name_to_int(dentry);
1113 struct files_struct * files;
1114 struct inode *inode;
1115 struct proc_inode *ei;
1119 if (!pid_alive(task))
1122 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1126 files = get_files_struct(task);
1129 inode->i_mode = S_IFLNK;
1130 spin_lock(&files->file_lock);
1131 file = fcheck_files(files, fd);
1134 if (file->f_mode & 1)
1135 inode->i_mode |= S_IRUSR | S_IXUSR;
1136 if (file->f_mode & 2)
1137 inode->i_mode |= S_IWUSR | S_IXUSR;
1138 spin_unlock(&files->file_lock);
1139 put_files_struct(files);
1140 inode->i_op = &proc_pid_link_inode_operations;
1142 ei->op.proc_get_link = proc_fd_link;
1143 dentry->d_op = &tid_fd_dentry_operations;
1144 d_add(dentry, inode);
1148 spin_unlock(&files->file_lock);
1149 put_files_struct(files);
1153 return ERR_PTR(-ENOENT);
1156 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1157 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1159 static struct file_operations proc_fd_operations = {
1160 .read = generic_read_dir,
1161 .readdir = proc_readfd,
1164 static struct file_operations proc_task_operations = {
1165 .read = generic_read_dir,
1166 .readdir = proc_task_readdir,
1170 * proc directories can do almost nothing..
1172 static struct inode_operations proc_fd_inode_operations = {
1173 .lookup = proc_lookupfd,
1174 .permission = proc_permission,
1177 static struct inode_operations proc_task_inode_operations = {
1178 .lookup = proc_task_lookup,
1179 .permission = proc_permission,
1182 #ifdef CONFIG_SECURITY
1183 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1184 size_t count, loff_t *ppos)
1186 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);
1200 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1205 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1206 size_t count, loff_t *ppos)
1208 struct inode * inode = file->f_dentry->d_inode;
1211 struct task_struct *task = proc_task(inode);
1213 if (count > PAGE_SIZE)
1216 /* No partial writes. */
1219 page = (char*)__get_free_page(GFP_USER);
1223 if (copy_from_user(page, buf, count))
1226 length = security_setprocattr(task,
1227 (char*)file->f_dentry->d_name.name,
1228 (void*)page, count);
1230 free_page((unsigned long) page);
1234 static struct file_operations proc_pid_attr_operations = {
1235 .read = proc_pid_attr_read,
1236 .write = proc_pid_attr_write,
1239 static struct file_operations proc_tid_attr_operations;
1240 static struct inode_operations proc_tid_attr_inode_operations;
1241 static struct file_operations proc_tgid_attr_operations;
1242 static struct inode_operations proc_tgid_attr_inode_operations;
1246 static struct dentry *proc_pident_lookup(struct inode *dir,
1247 struct dentry *dentry,
1248 struct pid_entry *ents)
1250 struct inode *inode;
1252 struct task_struct *task = proc_task(dir);
1253 struct pid_entry *p;
1254 struct proc_inode *ei;
1259 if (!pid_alive(task))
1262 for (p = ents; p->name; p++) {
1263 if (p->len != dentry->d_name.len)
1265 if (!memcmp(dentry->d_name.name, p->name, p->len))
1272 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1277 inode->i_mode = p->mode;
1279 * Yes, it does not scale. And it should not. Don't add
1280 * new entries into /proc/<tgid>/ without very good reasons.
1283 case PROC_TGID_TASK:
1285 inode->i_op = &proc_task_inode_operations;
1286 inode->i_fop = &proc_task_operations;
1291 inode->i_op = &proc_fd_inode_operations;
1292 inode->i_fop = &proc_fd_operations;
1296 inode->i_op = &proc_pid_link_inode_operations;
1297 ei->op.proc_get_link = proc_exe_link;
1301 inode->i_op = &proc_pid_link_inode_operations;
1302 ei->op.proc_get_link = proc_cwd_link;
1305 case PROC_TGID_ROOT:
1306 inode->i_op = &proc_pid_link_inode_operations;
1307 ei->op.proc_get_link = proc_root_link;
1309 case PROC_TID_ENVIRON:
1310 case PROC_TGID_ENVIRON:
1311 inode->i_fop = &proc_info_file_operations;
1312 ei->op.proc_read = proc_pid_environ;
1315 case PROC_TGID_AUXV:
1316 inode->i_fop = &proc_info_file_operations;
1317 ei->op.proc_read = proc_pid_auxv;
1319 case PROC_TID_STATUS:
1320 case PROC_TGID_STATUS:
1321 inode->i_fop = &proc_info_file_operations;
1322 ei->op.proc_read = proc_pid_status;
1325 inode->i_fop = &proc_info_file_operations;
1326 ei->op.proc_read = proc_tid_stat;
1328 case PROC_TGID_STAT:
1329 inode->i_fop = &proc_info_file_operations;
1330 ei->op.proc_read = proc_tgid_stat;
1332 case PROC_TID_CMDLINE:
1333 case PROC_TGID_CMDLINE:
1334 inode->i_fop = &proc_info_file_operations;
1335 ei->op.proc_read = proc_pid_cmdline;
1337 case PROC_TID_STATM:
1338 case PROC_TGID_STATM:
1339 inode->i_fop = &proc_info_file_operations;
1340 ei->op.proc_read = proc_pid_statm;
1343 case PROC_TGID_MAPS:
1344 inode->i_fop = &proc_maps_operations;
1348 inode->i_op = &proc_mem_inode_operations;
1349 inode->i_fop = &proc_mem_operations;
1351 case PROC_TID_MOUNTS:
1352 case PROC_TGID_MOUNTS:
1353 inode->i_fop = &proc_mounts_operations;
1355 #ifdef CONFIG_SECURITY
1358 inode->i_op = &proc_tid_attr_inode_operations;
1359 inode->i_fop = &proc_tid_attr_operations;
1361 case PROC_TGID_ATTR:
1363 inode->i_op = &proc_tgid_attr_inode_operations;
1364 inode->i_fop = &proc_tgid_attr_operations;
1366 case PROC_TID_ATTR_CURRENT:
1367 case PROC_TGID_ATTR_CURRENT:
1368 case PROC_TID_ATTR_PREV:
1369 case PROC_TGID_ATTR_PREV:
1370 case PROC_TID_ATTR_EXEC:
1371 case PROC_TGID_ATTR_EXEC:
1372 case PROC_TID_ATTR_FSCREATE:
1373 case PROC_TGID_ATTR_FSCREATE:
1374 inode->i_fop = &proc_pid_attr_operations;
1377 #ifdef CONFIG_KALLSYMS
1378 case PROC_TID_WCHAN:
1379 case PROC_TGID_WCHAN:
1380 inode->i_fop = &proc_info_file_operations;
1381 ei->op.proc_read = proc_pid_wchan;
1384 #ifdef CONFIG_DELAY_ACCT
1385 case PROC_TID_DELAY_ACCT:
1386 case PROC_TGID_DELAY_ACCT:
1387 inode->i_fop = &proc_info_file_operations;
1388 ei->op.proc_read = proc_pid_delay;
1391 #ifdef CONFIG_SCHEDSTATS
1392 case PROC_TID_SCHEDSTAT:
1393 case PROC_TGID_SCHEDSTAT:
1394 inode->i_fop = &proc_info_file_operations;
1395 ei->op.proc_read = proc_pid_schedstat;
1399 printk("procfs: impossible type (%d)",p->type);
1401 return ERR_PTR(-EINVAL);
1403 dentry->d_op = &pid_dentry_operations;
1404 d_add(dentry, inode);
1408 return ERR_PTR(error);
1411 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1412 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1415 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1416 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1419 static struct file_operations proc_tgid_base_operations = {
1420 .read = generic_read_dir,
1421 .readdir = proc_tgid_base_readdir,
1424 static struct file_operations proc_tid_base_operations = {
1425 .read = generic_read_dir,
1426 .readdir = proc_tid_base_readdir,
1429 static struct inode_operations proc_tgid_base_inode_operations = {
1430 .lookup = proc_tgid_base_lookup,
1433 static struct inode_operations proc_tid_base_inode_operations = {
1434 .lookup = proc_tid_base_lookup,
1437 #ifdef CONFIG_SECURITY
1438 static int proc_tgid_attr_readdir(struct file * filp,
1439 void * dirent, filldir_t filldir)
1441 return proc_pident_readdir(filp,dirent,filldir,
1442 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1445 static int proc_tid_attr_readdir(struct file * filp,
1446 void * dirent, filldir_t filldir)
1448 return proc_pident_readdir(filp,dirent,filldir,
1449 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1452 static struct file_operations proc_tgid_attr_operations = {
1453 .read = generic_read_dir,
1454 .readdir = proc_tgid_attr_readdir,
1457 static struct file_operations proc_tid_attr_operations = {
1458 .read = generic_read_dir,
1459 .readdir = proc_tid_attr_readdir,
1462 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1463 struct dentry *dentry, struct nameidata *nd)
1465 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1468 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1469 struct dentry *dentry, struct nameidata *nd)
1471 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1474 static struct inode_operations proc_tgid_attr_inode_operations = {
1475 .lookup = proc_tgid_attr_lookup,
1478 static struct inode_operations proc_tid_attr_inode_operations = {
1479 .lookup = proc_tid_attr_lookup,
1486 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1490 sprintf(tmp, "%d", current->tgid);
1491 return vfs_readlink(dentry,buffer,buflen,tmp);
1494 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1497 sprintf(tmp, "%d", current->tgid);
1498 return vfs_follow_link(nd,tmp);
1501 static struct inode_operations proc_self_inode_operations = {
1502 .readlink = proc_self_readlink,
1503 .follow_link = proc_self_follow_link,
1507 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1508 * @p: task that should be flushed.
1510 * Drops the /proc/<pid> dcache entry from the hash chains.
1512 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1513 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1514 * if the pid value is immediately reused. This is enforced by
1515 * - caller must acquire spin_lock(p->proc_lock)
1516 * - must be called before detach_pid()
1517 * - proc_pid_lookup acquires proc_lock, and checks that
1518 * the target is not dead by looking at the attach count
1522 struct dentry *proc_pid_unhash(struct task_struct *p)
1524 struct dentry *proc_dentry;
1526 proc_dentry = p->proc_dentry;
1527 if (proc_dentry != NULL) {
1529 spin_lock(&dcache_lock);
1530 if (!d_unhashed(proc_dentry)) {
1531 dget_locked(proc_dentry);
1532 __d_drop(proc_dentry);
1535 spin_unlock(&dcache_lock);
1541 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1542 * @proc_entry: directoy to prune.
1544 * Shrink the /proc directory that was used by the just killed thread.
1547 void proc_pid_flush(struct dentry *proc_dentry)
1550 if(proc_dentry != NULL) {
1551 shrink_dcache_parent(proc_dentry);
1557 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1559 struct task_struct *task;
1560 struct inode *inode;
1561 struct proc_inode *ei;
1565 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1566 inode = new_inode(dir->i_sb);
1568 return ERR_PTR(-ENOMEM);
1570 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1571 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1573 inode->i_mode = S_IFLNK|S_IRWXUGO;
1574 inode->i_uid = inode->i_gid = 0;
1576 inode->i_op = &proc_self_inode_operations;
1577 d_add(dentry, inode);
1580 tgid = name_to_int(dentry);
1584 read_lock(&tasklist_lock);
1585 task = find_task_by_pid(tgid);
1587 get_task_struct(task);
1588 read_unlock(&tasklist_lock);
1592 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1596 put_task_struct(task);
1599 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1600 inode->i_op = &proc_tgid_base_inode_operations;
1601 inode->i_fop = &proc_tgid_base_operations;
1603 inode->i_flags|=S_IMMUTABLE;
1605 dentry->d_op = &pid_base_dentry_operations;
1608 d_add(dentry, inode);
1609 spin_lock(&task->proc_lock);
1610 task->proc_dentry = dentry;
1611 if (!pid_alive(task)) {
1612 dentry = proc_pid_unhash(task);
1615 spin_unlock(&task->proc_lock);
1617 put_task_struct(task);
1619 proc_pid_flush(dentry);
1624 return ERR_PTR(-ENOENT);
1628 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1630 struct task_struct *task;
1631 struct task_struct *leader = proc_task(dir);
1632 struct inode *inode;
1635 tid = name_to_int(dentry);
1639 read_lock(&tasklist_lock);
1640 task = find_task_by_pid(tid);
1642 get_task_struct(task);
1643 read_unlock(&tasklist_lock);
1646 if (leader->tgid != task->tgid)
1649 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1654 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1655 inode->i_op = &proc_tid_base_inode_operations;
1656 inode->i_fop = &proc_tid_base_operations;
1658 inode->i_flags|=S_IMMUTABLE;
1660 dentry->d_op = &pid_base_dentry_operations;
1662 d_add(dentry, inode);
1664 put_task_struct(task);
1667 put_task_struct(task);
1669 return ERR_PTR(-ENOENT);
1672 #define PROC_NUMBUF 10
1673 #define PROC_MAXPIDS 20
1676 * Get a few tgid's to return for filldir - we need to hold the
1677 * tasklist lock while doing this, and we must release it before
1678 * we actually do the filldir itself, so we use a temp buffer..
1680 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1682 struct task_struct *p;
1686 read_lock(&tasklist_lock);
1689 p = find_task_by_pid(version);
1690 if (p && !thread_group_leader(p))
1697 p = next_task(&init_task);
1699 for ( ; p != &init_task; p = next_task(p)) {
1705 tgids[nr_tgids] = tgid;
1707 if (nr_tgids >= PROC_MAXPIDS)
1710 read_unlock(&tasklist_lock);
1715 * Get a few tid's to return for filldir - we need to hold the
1716 * tasklist lock while doing this, and we must release it before
1717 * we actually do the filldir itself, so we use a temp buffer..
1719 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1721 struct task_struct *leader_task = proc_task(dir);
1722 struct task_struct *task = leader_task;
1726 read_lock(&tasklist_lock);
1728 * The starting point task (leader_task) might be an already
1729 * unlinked task, which cannot be used to access the task-list
1730 * via next_thread().
1732 if (pid_alive(task)) do {
1733 int tid = task->pid;
1737 tids[nr_tids] = tid;
1739 if (nr_tids >= PROC_MAXPIDS)
1741 } while ((task = next_thread(task)) != leader_task);
1742 read_unlock(&tasklist_lock);
1746 /* for the /proc/ directory itself, after non-process stuff has been done */
1747 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1749 unsigned int tgid_array[PROC_MAXPIDS];
1750 char buf[PROC_NUMBUF];
1751 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1752 unsigned int nr_tgids, i;
1756 ino_t ino = fake_ino(0,PROC_TGID_INO);
1757 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1763 /* f_version caches the tgid value that the last readdir call couldn't
1764 * return. lseek aka telldir automagically resets f_version to 0.
1766 next_tgid = filp->f_version;
1767 filp->f_version = 0;
1769 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
1771 /* no more entries ! */
1776 /* do not use the last found pid, reserve it for next_tgid */
1777 if (nr_tgids == PROC_MAXPIDS) {
1779 next_tgid = tgid_array[nr_tgids];
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 /* returning this tgid failed, save it as the first
1793 * pid for the next readir call */
1794 filp->f_version = tgid_array[i];
1805 /* for the /proc/TGID/task/ directories */
1806 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1808 unsigned int tid_array[PROC_MAXPIDS];
1809 char buf[PROC_NUMBUF];
1810 unsigned int nr_tids, i;
1811 struct dentry *dentry = filp->f_dentry;
1812 struct inode *inode = dentry->d_inode;
1813 int retval = -ENOENT;
1815 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1817 if (!pid_alive(proc_task(inode)))
1824 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1829 ino = parent_ino(dentry);
1830 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1836 nr_tids = get_tid_list(pos, tid_array, inode);
1838 for (i = 0; i < nr_tids; i++) {
1839 unsigned long j = PROC_NUMBUF;
1840 int tid = tid_array[i];
1842 ino = fake_ino(tid,PROC_TID_INO);
1845 buf[--j] = '0' + (tid % 10);
1846 while ((tid /= 10) != 0);
1848 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)