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_SECURITY
65 PROC_TGID_ATTR_CURRENT,
68 PROC_TGID_ATTR_FSCREATE,
86 #ifdef CONFIG_SECURITY
88 PROC_TID_ATTR_CURRENT,
91 PROC_TID_ATTR_FSCREATE,
93 #ifdef CONFIG_DELAY_ACCT
97 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
107 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
109 static struct pid_entry tgid_base_stuff[] = {
110 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
111 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
112 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
113 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
114 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
115 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
116 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
117 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
118 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
119 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
120 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
121 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
122 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
123 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
124 #ifdef CONFIG_SECURITY
125 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
127 #ifdef CONFIG_DELAY_ACCT
128 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
130 #ifdef CONFIG_KALLSYMS
131 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
135 static struct pid_entry tid_base_stuff[] = {
136 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
137 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
138 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
139 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
140 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
141 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
142 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
143 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
144 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
145 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
146 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
147 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
148 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
149 #ifdef CONFIG_SECURITY
150 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
152 #ifdef CONFIG_DELAY_ACCT
153 E(PROC_TGID_DELAY_ACCT,"delay", S_IFREG|S_IRUGO),
155 #ifdef CONFIG_KALLSYMS
156 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
161 #ifdef CONFIG_SECURITY
162 static struct pid_entry tgid_attr_stuff[] = {
163 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
164 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
165 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
166 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
169 static struct pid_entry tid_attr_stuff[] = {
170 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
171 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
172 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
173 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
180 static inline struct task_struct *proc_task(struct inode *inode)
182 return PROC_I(inode)->task;
185 static inline int proc_type(struct inode *inode)
187 return PROC_I(inode)->type;
190 int proc_pid_stat(struct task_struct*,char*);
191 int proc_pid_status(struct task_struct*,char*);
192 int proc_pid_statm(struct task_struct*,char*);
193 int proc_pid_cpu(struct task_struct*,char*);
194 int proc_pid_delay(struct task_struct*,char*);
196 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
198 struct task_struct *task = proc_task(inode);
199 struct files_struct *files;
201 int fd = proc_type(inode) - PROC_TID_FD_DIR;
203 files = get_files_struct(task);
205 spin_lock(&files->file_lock);
206 file = fcheck_files(files, fd);
208 *mnt = mntget(file->f_vfsmnt);
209 *dentry = dget(file->f_dentry);
210 spin_unlock(&files->file_lock);
211 put_files_struct(files);
214 spin_unlock(&files->file_lock);
215 put_files_struct(files);
220 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
222 struct vm_area_struct * vma;
223 int result = -ENOENT;
224 struct task_struct *task = proc_task(inode);
225 struct mm_struct * mm = get_task_mm(task);
229 down_read(&mm->mmap_sem);
232 if ((vma->vm_flags & VM_EXECUTABLE) &&
234 *mnt = mntget(vma->vm_file->f_vfsmnt);
235 *dentry = dget(vma->vm_file->f_dentry);
241 up_read(&mm->mmap_sem);
247 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
249 struct fs_struct *fs;
250 int result = -ENOENT;
251 task_lock(proc_task(inode));
252 fs = proc_task(inode)->fs;
254 atomic_inc(&fs->count);
255 task_unlock(proc_task(inode));
257 read_lock(&fs->lock);
258 *mnt = mntget(fs->pwdmnt);
259 *dentry = dget(fs->pwd);
260 read_unlock(&fs->lock);
267 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
269 struct fs_struct *fs;
270 int result = -ENOENT;
271 task_lock(proc_task(inode));
272 fs = proc_task(inode)->fs;
274 atomic_inc(&fs->count);
275 task_unlock(proc_task(inode));
277 read_lock(&fs->lock);
278 *mnt = mntget(fs->rootmnt);
279 *dentry = dget(fs->root);
280 read_unlock(&fs->lock);
287 #define MAY_PTRACE(task) \
288 (task == current || \
289 (task->parent == current && \
290 (task->ptrace & PT_PTRACED) && task->state == TASK_STOPPED && \
291 security_ptrace(current,task) == 0))
293 static int may_ptrace_attach(struct task_struct *task)
301 if (((current->uid != task->euid) ||
302 (current->uid != task->suid) ||
303 (current->uid != task->uid) ||
304 (current->gid != task->egid) ||
305 (current->gid != task->sgid) ||
306 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
309 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
311 if (security_ptrace(current, task))
320 static int proc_pid_environ(struct task_struct *task, char * buffer)
323 struct mm_struct *mm = get_task_mm(task);
325 unsigned int len = mm->env_end - mm->env_start;
328 res = access_process_vm(task, mm->env_start, buffer, len, 0);
329 if (!may_ptrace_attach(task))
336 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
340 struct mm_struct *mm = get_task_mm(task);
344 len = mm->arg_end - mm->arg_start;
349 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
351 // If the nul at the end of args has been overwritten, then
352 // assume application is using setproctitle(3).
353 if (res > 0 && buffer[res-1] != '\0') {
354 len = strnlen(buffer, res);
358 len = mm->env_end - mm->env_start;
359 if (len > PAGE_SIZE - res)
360 len = PAGE_SIZE - res;
361 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
362 res = strnlen(buffer, res);
371 static int proc_pid_auxv(struct task_struct *task, char *buffer)
374 struct mm_struct *mm = get_task_mm(task);
376 unsigned int nwords = 0;
379 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
380 res = nwords * sizeof(mm->saved_auxv[0]);
383 memcpy(buffer, mm->saved_auxv, res);
390 #ifdef CONFIG_KALLSYMS
392 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
393 * Returns the resolved symbol. If that fails, simply return the address.
395 static int proc_pid_wchan(struct task_struct *task, char *buffer)
398 const char *sym_name;
399 unsigned long wchan, size, offset;
402 wchan = get_wchan(task);
404 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
406 return sprintf(buffer, "%s", sym_name);
407 return sprintf(buffer, "%lu", wchan);
409 #endif /* CONFIG_KALLSYMS */
411 /************************************************************************/
412 /* Here the fs part begins */
413 /************************************************************************/
415 /* permission checks */
417 static int proc_check_root(struct inode *inode)
419 struct dentry *de, *base, *root;
420 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
423 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
425 read_lock(¤t->fs->lock);
426 our_vfsmnt = mntget(current->fs->rootmnt);
427 base = dget(current->fs->root);
428 read_unlock(¤t->fs->lock);
430 spin_lock(&vfsmount_lock);
434 while (vfsmnt != our_vfsmnt) {
435 if (vfsmnt == vfsmnt->mnt_parent)
437 de = vfsmnt->mnt_mountpoint;
438 vfsmnt = vfsmnt->mnt_parent;
441 if (!is_subdir(de, base))
443 spin_unlock(&vfsmount_lock);
452 spin_unlock(&vfsmount_lock);
457 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
459 if (vfs_permission(inode, mask) != 0)
461 return proc_check_root(inode);
464 extern struct seq_operations proc_pid_maps_op;
465 static int maps_open(struct inode *inode, struct file *file)
467 struct task_struct *task = proc_task(inode);
468 int ret = seq_open(file, &proc_pid_maps_op);
470 struct seq_file *m = file->private_data;
476 static struct file_operations proc_maps_operations = {
480 .release = seq_release,
483 extern struct seq_operations mounts_op;
484 static int mounts_open(struct inode *inode, struct file *file)
486 struct task_struct *task = proc_task(inode);
487 int ret = seq_open(file, &mounts_op);
490 struct seq_file *m = file->private_data;
491 struct namespace *namespace;
493 namespace = task->namespace;
495 get_namespace(namespace);
499 m->private = namespace;
501 seq_release(inode, file);
508 static int mounts_release(struct inode *inode, struct file *file)
510 struct seq_file *m = file->private_data;
511 struct namespace *namespace = m->private;
512 put_namespace(namespace);
513 return seq_release(inode, file);
516 static struct file_operations proc_mounts_operations = {
520 .release = mounts_release,
523 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
525 static ssize_t proc_info_read(struct file * file, char * buf,
526 size_t count, loff_t *ppos)
528 struct inode * inode = file->f_dentry->d_inode;
532 struct task_struct *task = proc_task(inode);
534 if (count > PROC_BLOCK_SIZE)
535 count = PROC_BLOCK_SIZE;
536 if (!(page = __get_free_page(GFP_KERNEL)))
539 length = PROC_I(inode)->op.proc_read(task, (char*)page);
545 /* Static 4kB (or whatever) block capacity */
546 if (*ppos >= length) {
550 if (count + *ppos > length)
551 count = length - *ppos;
553 if (copy_to_user(buf, (char *) page + *ppos, count))
561 static struct file_operations proc_info_file_operations = {
562 .read = proc_info_read,
565 static int mem_open(struct inode* inode, struct file* file)
567 file->private_data = (void*)((long)current->self_exec_id);
571 static ssize_t mem_read(struct file * file, char * buf,
572 size_t count, loff_t *ppos)
574 struct task_struct *task = proc_task(file->f_dentry->d_inode);
576 unsigned long src = *ppos;
578 struct mm_struct *mm;
580 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
584 page = (char *)__get_free_page(GFP_USER);
590 mm = get_task_mm(task);
596 if (file->private_data != (void*)((long)current->self_exec_id))
602 int this_len, retval;
604 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
605 retval = access_process_vm(task, src, page, this_len, 0);
606 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
612 if (copy_to_user(buf, page, retval)) {
627 free_page((unsigned long) page);
632 #define mem_write NULL
635 /* This is a security hazard */
636 static ssize_t mem_write(struct file * file, const char * buf,
637 size_t count, loff_t *ppos)
641 struct task_struct *task = proc_task(file->f_dentry->d_inode);
642 unsigned long dst = *ppos;
644 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
647 page = (char *)__get_free_page(GFP_USER);
652 int this_len, retval;
654 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
655 if (copy_from_user(page, buf, this_len)) {
659 retval = access_process_vm(task, dst, page, this_len, 1);
671 free_page((unsigned long) page);
676 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
680 file->f_pos = offset;
683 file->f_pos += offset;
688 force_successful_syscall_return();
692 static struct file_operations proc_mem_operations = {
699 static struct inode_operations proc_mem_inode_operations = {
700 .permission = proc_permission,
703 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
705 struct inode *inode = dentry->d_inode;
708 /* We don't need a base pointer in the /proc filesystem */
711 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
713 error = proc_check_root(inode);
717 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
718 nd->last_type = LAST_BIND;
723 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
724 char *buffer, int buflen)
726 struct inode * inode;
727 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
733 inode = dentry->d_inode;
734 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
738 len = tmp + PAGE_SIZE - 1 - path;
742 if (copy_to_user(buffer, path, len))
745 free_page((unsigned long)tmp);
749 static int proc_pid_readlink(struct dentry * dentry, char * buffer, int buflen)
752 struct inode *inode = dentry->d_inode;
754 struct vfsmount *mnt = NULL;
758 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
760 error = proc_check_root(inode);
764 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
768 error = do_proc_readlink(de, mnt, buffer, buflen);
776 static struct inode_operations proc_pid_link_inode_operations = {
777 .readlink = proc_pid_readlink,
778 .follow_link = proc_pid_follow_link
781 static int pid_alive(struct task_struct *p)
783 BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
784 return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
789 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
791 struct inode *inode = filp->f_dentry->d_inode;
792 struct task_struct *p = proc_task(inode);
793 unsigned int fd, tid, ino;
796 struct files_struct * files;
807 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
811 ino = fake_ino(tid, PROC_TID_INO);
812 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
816 files = get_files_struct(p);
819 spin_lock(&files->file_lock);
820 for (fd = filp->f_pos-2;
822 fd++, filp->f_pos++) {
825 if (!fcheck_files(files, fd))
827 spin_unlock(&files->file_lock);
833 buf[j] = '0' + (i % 10);
837 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
838 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
839 spin_lock(&files->file_lock);
842 spin_lock(&files->file_lock);
844 spin_unlock(&files->file_lock);
845 put_files_struct(files);
851 static int proc_pident_readdir(struct file *filp,
852 void *dirent, filldir_t filldir,
853 struct pid_entry *ents, unsigned int nents)
857 struct dentry *dentry = filp->f_dentry;
858 struct inode *inode = dentry->d_inode;
864 if (!pid_alive(proc_task(inode)))
868 pid = proc_task(inode)->pid;
873 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
879 ino = parent_ino(dentry);
880 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
893 if (filldir(dirent, p->name, p->len, filp->f_pos,
894 fake_ino(pid, p->type), p->mode >> 12) < 0)
906 static int proc_tgid_base_readdir(struct file * filp,
907 void * dirent, filldir_t filldir)
909 return proc_pident_readdir(filp,dirent,filldir,
910 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
913 static int proc_tid_base_readdir(struct file * filp,
914 void * dirent, filldir_t filldir)
916 return proc_pident_readdir(filp,dirent,filldir,
917 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
920 /* building an inode */
922 static int task_dumpable(struct task_struct *task)
925 struct mm_struct *mm;
930 dumpable = mm->dumpable;
936 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
938 struct inode * inode;
939 struct proc_inode *ei;
941 /* We need a new inode */
943 inode = new_inode(sb);
950 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
951 inode->i_ino = fake_ino(task->pid, ino);
953 if (!pid_alive(task))
957 * grab the reference to task.
959 get_task_struct(task);
964 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
965 inode->i_uid = task->euid;
966 inode->i_gid = task->egid;
968 security_task_to_inode(task, inode);
982 * Exceptional case: normally we are not allowed to unhash a busy
983 * directory. In this case, however, we can do it - no aliasing problems
984 * due to the way we treat inodes.
986 * Rewrite the inode's ownerships here because the owning task may have
987 * performed a setuid(), etc.
989 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
991 struct inode *inode = dentry->d_inode;
992 struct task_struct *task = proc_task(inode);
993 if (pid_alive(task)) {
994 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
995 inode->i_uid = task->euid;
996 inode->i_gid = task->egid;
1001 security_task_to_inode(task, inode);
1008 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1010 struct inode *inode = dentry->d_inode;
1011 struct task_struct *task = proc_task(inode);
1012 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1013 struct files_struct *files;
1015 files = get_files_struct(task);
1017 spin_lock(&files->file_lock);
1018 if (fcheck_files(files, fd)) {
1019 spin_unlock(&files->file_lock);
1020 put_files_struct(files);
1021 if (task_dumpable(task)) {
1022 inode->i_uid = task->euid;
1023 inode->i_gid = task->egid;
1028 security_task_to_inode(task, inode);
1031 spin_unlock(&files->file_lock);
1032 put_files_struct(files);
1038 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1040 struct task_struct *task = proc_task(inode);
1041 spin_lock(&task->proc_lock);
1042 if (task->proc_dentry == dentry)
1043 task->proc_dentry = NULL;
1044 spin_unlock(&task->proc_lock);
1048 static int pid_delete_dentry(struct dentry * dentry)
1050 /* Is the task we represent dead?
1051 * If so, then don't put the dentry on the lru list,
1052 * kill it immediately.
1054 return !pid_alive(proc_task(dentry->d_inode));
1057 static struct dentry_operations tid_fd_dentry_operations =
1059 .d_revalidate = tid_fd_revalidate,
1060 .d_delete = pid_delete_dentry,
1063 static struct dentry_operations pid_dentry_operations =
1065 .d_revalidate = pid_revalidate,
1066 .d_delete = pid_delete_dentry,
1069 static struct dentry_operations pid_base_dentry_operations =
1071 .d_revalidate = pid_revalidate,
1072 .d_iput = pid_base_iput,
1073 .d_delete = pid_delete_dentry,
1078 static unsigned name_to_int(struct dentry *dentry)
1080 const char *name = dentry->d_name.name;
1081 int len = dentry->d_name.len;
1084 if (len > 1 && *name == '0')
1087 unsigned c = *name++ - '0';
1090 if (n >= (~0U-9)/10)
1101 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1103 struct task_struct *task = proc_task(dir);
1104 unsigned fd = name_to_int(dentry);
1106 struct files_struct * files;
1107 struct inode *inode;
1108 struct proc_inode *ei;
1112 if (!pid_alive(task))
1115 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1119 files = get_files_struct(task);
1122 inode->i_mode = S_IFLNK;
1123 spin_lock(&files->file_lock);
1124 file = fcheck_files(files, fd);
1127 if (file->f_mode & 1)
1128 inode->i_mode |= S_IRUSR | S_IXUSR;
1129 if (file->f_mode & 2)
1130 inode->i_mode |= S_IWUSR | S_IXUSR;
1131 spin_unlock(&files->file_lock);
1132 put_files_struct(files);
1133 inode->i_op = &proc_pid_link_inode_operations;
1135 ei->op.proc_get_link = proc_fd_link;
1136 dentry->d_op = &tid_fd_dentry_operations;
1137 d_add(dentry, inode);
1141 spin_unlock(&files->file_lock);
1142 put_files_struct(files);
1146 return ERR_PTR(-ENOENT);
1149 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1150 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1152 static struct file_operations proc_fd_operations = {
1153 .read = generic_read_dir,
1154 .readdir = proc_readfd,
1157 static struct file_operations proc_task_operations = {
1158 .read = generic_read_dir,
1159 .readdir = proc_task_readdir,
1163 * proc directories can do almost nothing..
1165 static struct inode_operations proc_fd_inode_operations = {
1166 .lookup = proc_lookupfd,
1167 .permission = proc_permission,
1170 static struct inode_operations proc_task_inode_operations = {
1171 .lookup = proc_task_lookup,
1172 .permission = proc_permission,
1175 #ifdef CONFIG_SECURITY
1176 static ssize_t proc_pid_attr_read(struct file * file, char * buf,
1177 size_t count, loff_t *ppos)
1179 struct inode * inode = file->f_dentry->d_inode;
1183 struct task_struct *task = proc_task(inode);
1185 if (count > PAGE_SIZE)
1187 if (!(page = __get_free_page(GFP_KERNEL)))
1190 length = security_getprocattr(task,
1191 (char*)file->f_dentry->d_name.name,
1192 (void*)page, count);
1197 /* Static 4kB (or whatever) block capacity */
1198 if (*ppos >= length) {
1202 if (count + *ppos > length)
1203 count = length - *ppos;
1204 end = count + *ppos;
1205 if (copy_to_user(buf, (char *) page + *ppos, count))
1213 static ssize_t proc_pid_attr_write(struct file * file, const char * buf,
1214 size_t count, loff_t *ppos)
1216 struct inode * inode = file->f_dentry->d_inode;
1219 struct task_struct *task = proc_task(inode);
1221 if (count > PAGE_SIZE)
1224 /* No partial writes. */
1227 page = (char*)__get_free_page(GFP_USER);
1231 if (copy_from_user(page, buf, count))
1234 length = security_setprocattr(task,
1235 (char*)file->f_dentry->d_name.name,
1236 (void*)page, count);
1238 free_page((unsigned long) page);
1242 static struct file_operations proc_pid_attr_operations = {
1243 .read = proc_pid_attr_read,
1244 .write = proc_pid_attr_write,
1247 static struct file_operations proc_tid_attr_operations;
1248 static struct inode_operations proc_tid_attr_inode_operations;
1249 static struct file_operations proc_tgid_attr_operations;
1250 static struct inode_operations proc_tgid_attr_inode_operations;
1254 static struct dentry *proc_pident_lookup(struct inode *dir,
1255 struct dentry *dentry,
1256 struct pid_entry *ents)
1258 struct inode *inode;
1260 struct task_struct *task = proc_task(dir);
1261 struct pid_entry *p;
1262 struct proc_inode *ei;
1267 if (!pid_alive(task))
1270 for (p = ents; p->name; p++) {
1271 if (p->len != dentry->d_name.len)
1273 if (!memcmp(dentry->d_name.name, p->name, p->len))
1280 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1285 inode->i_mode = p->mode;
1287 * Yes, it does not scale. And it should not. Don't add
1288 * new entries into /proc/<tgid>/ without very good reasons.
1291 case PROC_TGID_TASK:
1293 inode->i_op = &proc_task_inode_operations;
1294 inode->i_fop = &proc_task_operations;
1299 inode->i_op = &proc_fd_inode_operations;
1300 inode->i_fop = &proc_fd_operations;
1304 inode->i_op = &proc_pid_link_inode_operations;
1305 ei->op.proc_get_link = proc_exe_link;
1309 inode->i_op = &proc_pid_link_inode_operations;
1310 ei->op.proc_get_link = proc_cwd_link;
1313 case PROC_TGID_ROOT:
1314 inode->i_op = &proc_pid_link_inode_operations;
1315 ei->op.proc_get_link = proc_root_link;
1317 case PROC_TID_ENVIRON:
1318 case PROC_TGID_ENVIRON:
1319 inode->i_fop = &proc_info_file_operations;
1320 ei->op.proc_read = proc_pid_environ;
1323 case PROC_TGID_AUXV:
1324 inode->i_fop = &proc_info_file_operations;
1325 ei->op.proc_read = proc_pid_auxv;
1327 case PROC_TID_STATUS:
1328 case PROC_TGID_STATUS:
1329 inode->i_fop = &proc_info_file_operations;
1330 ei->op.proc_read = proc_pid_status;
1333 case PROC_TGID_STAT:
1334 inode->i_fop = &proc_info_file_operations;
1335 ei->op.proc_read = proc_pid_stat;
1337 case PROC_TID_CMDLINE:
1338 case PROC_TGID_CMDLINE:
1339 inode->i_fop = &proc_info_file_operations;
1340 ei->op.proc_read = proc_pid_cmdline;
1342 case PROC_TID_STATM:
1343 case PROC_TGID_STATM:
1344 inode->i_fop = &proc_info_file_operations;
1345 ei->op.proc_read = proc_pid_statm;
1348 case PROC_TGID_MAPS:
1349 inode->i_fop = &proc_maps_operations;
1353 inode->i_op = &proc_mem_inode_operations;
1354 inode->i_fop = &proc_mem_operations;
1356 case PROC_TID_MOUNTS:
1357 case PROC_TGID_MOUNTS:
1358 inode->i_fop = &proc_mounts_operations;
1360 #ifdef CONFIG_SECURITY
1363 inode->i_op = &proc_tid_attr_inode_operations;
1364 inode->i_fop = &proc_tid_attr_operations;
1366 case PROC_TGID_ATTR:
1368 inode->i_op = &proc_tgid_attr_inode_operations;
1369 inode->i_fop = &proc_tgid_attr_operations;
1371 case PROC_TID_ATTR_CURRENT:
1372 case PROC_TGID_ATTR_CURRENT:
1373 case PROC_TID_ATTR_PREV:
1374 case PROC_TGID_ATTR_PREV:
1375 case PROC_TID_ATTR_EXEC:
1376 case PROC_TGID_ATTR_EXEC:
1377 case PROC_TID_ATTR_FSCREATE:
1378 case PROC_TGID_ATTR_FSCREATE:
1379 inode->i_fop = &proc_pid_attr_operations;
1382 #ifdef CONFIG_KALLSYMS
1383 case PROC_TID_WCHAN:
1384 case PROC_TGID_WCHAN:
1385 inode->i_fop = &proc_info_file_operations;
1386 ei->op.proc_read = proc_pid_wchan;
1389 #ifdef CONFIG_DELAY_ACCT
1390 case PROC_TID_DELAY_ACCT:
1391 case PROC_TGID_DELAY_ACCT:
1392 inode->i_fop = &proc_info_file_operations;
1393 ei->op.proc_read = proc_pid_delay;
1397 printk("procfs: impossible type (%d)",p->type);
1399 return ERR_PTR(-EINVAL);
1401 dentry->d_op = &pid_dentry_operations;
1402 d_add(dentry, inode);
1406 return ERR_PTR(error);
1409 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1410 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1413 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1414 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1417 static struct file_operations proc_tgid_base_operations = {
1418 .read = generic_read_dir,
1419 .readdir = proc_tgid_base_readdir,
1422 static struct file_operations proc_tid_base_operations = {
1423 .read = generic_read_dir,
1424 .readdir = proc_tid_base_readdir,
1427 static struct inode_operations proc_tgid_base_inode_operations = {
1428 .lookup = proc_tgid_base_lookup,
1431 static struct inode_operations proc_tid_base_inode_operations = {
1432 .lookup = proc_tid_base_lookup,
1435 #ifdef CONFIG_SECURITY
1436 static int proc_tgid_attr_readdir(struct file * filp,
1437 void * dirent, filldir_t filldir)
1439 return proc_pident_readdir(filp,dirent,filldir,
1440 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1443 static int proc_tid_attr_readdir(struct file * filp,
1444 void * dirent, filldir_t filldir)
1446 return proc_pident_readdir(filp,dirent,filldir,
1447 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1450 static struct file_operations proc_tgid_attr_operations = {
1451 .read = generic_read_dir,
1452 .readdir = proc_tgid_attr_readdir,
1455 static struct file_operations proc_tid_attr_operations = {
1456 .read = generic_read_dir,
1457 .readdir = proc_tid_attr_readdir,
1460 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1461 struct dentry *dentry, struct nameidata *nd)
1463 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1466 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1467 struct dentry *dentry, struct nameidata *nd)
1469 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1472 static struct inode_operations proc_tgid_attr_inode_operations = {
1473 .lookup = proc_tgid_attr_lookup,
1476 static struct inode_operations proc_tid_attr_inode_operations = {
1477 .lookup = proc_tid_attr_lookup,
1484 static int proc_self_readlink(struct dentry *dentry, char *buffer, int buflen)
1487 sprintf(tmp, "%d", current->tgid);
1488 return vfs_readlink(dentry,buffer,buflen,tmp);
1491 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1494 sprintf(tmp, "%d", current->tgid);
1495 return vfs_follow_link(nd,tmp);
1498 static struct inode_operations proc_self_inode_operations = {
1499 .readlink = proc_self_readlink,
1500 .follow_link = proc_self_follow_link,
1504 * proc_pid_unhash - Unhash /proc/<pid> entry from the dcache.
1505 * @p: task that should be flushed.
1507 * Drops the /proc/<pid> dcache entry from the hash chains.
1509 * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1510 * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1511 * if the pid value is immediately reused. This is enforced by
1512 * - caller must acquire spin_lock(p->proc_lock)
1513 * - must be called before detach_pid()
1514 * - proc_pid_lookup acquires proc_lock, and checks that
1515 * the target is not dead by looking at the attach count
1519 struct dentry *proc_pid_unhash(struct task_struct *p)
1521 struct dentry *proc_dentry;
1523 proc_dentry = p->proc_dentry;
1524 if (proc_dentry != NULL) {
1526 spin_lock(&dcache_lock);
1527 if (!d_unhashed(proc_dentry)) {
1528 dget_locked(proc_dentry);
1529 __d_drop(proc_dentry);
1532 spin_unlock(&dcache_lock);
1538 * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1539 * @proc_entry: directoy to prune.
1541 * Shrink the /proc directory that was used by the just killed thread.
1544 void proc_pid_flush(struct dentry *proc_dentry)
1546 if(proc_dentry != NULL) {
1547 shrink_dcache_parent(proc_dentry);
1553 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1555 struct task_struct *task;
1556 struct inode *inode;
1557 struct proc_inode *ei;
1561 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1562 inode = new_inode(dir->i_sb);
1564 return ERR_PTR(-ENOMEM);
1566 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1567 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1569 inode->i_mode = S_IFLNK|S_IRWXUGO;
1570 inode->i_uid = inode->i_gid = 0;
1572 inode->i_op = &proc_self_inode_operations;
1573 d_add(dentry, inode);
1576 tgid = name_to_int(dentry);
1580 read_lock(&tasklist_lock);
1581 task = find_task_by_pid(tgid);
1583 get_task_struct(task);
1584 read_unlock(&tasklist_lock);
1588 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1592 put_task_struct(task);
1595 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1596 inode->i_op = &proc_tgid_base_inode_operations;
1597 inode->i_fop = &proc_tgid_base_operations;
1599 inode->i_flags|=S_IMMUTABLE;
1601 dentry->d_op = &pid_base_dentry_operations;
1604 d_add(dentry, inode);
1605 spin_lock(&task->proc_lock);
1606 task->proc_dentry = dentry;
1607 if (!pid_alive(task)) {
1608 dentry = proc_pid_unhash(task);
1611 spin_unlock(&task->proc_lock);
1613 put_task_struct(task);
1615 proc_pid_flush(dentry);
1620 return ERR_PTR(-ENOENT);
1624 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1626 struct task_struct *task;
1627 struct task_struct *leader = proc_task(dir);
1628 struct inode *inode;
1631 tid = name_to_int(dentry);
1635 read_lock(&tasklist_lock);
1636 task = find_task_by_pid(tid);
1638 get_task_struct(task);
1639 read_unlock(&tasklist_lock);
1642 if (leader->tgid != task->tgid)
1645 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1650 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1651 inode->i_op = &proc_tid_base_inode_operations;
1652 inode->i_fop = &proc_tid_base_operations;
1654 inode->i_flags|=S_IMMUTABLE;
1656 dentry->d_op = &pid_base_dentry_operations;
1658 d_add(dentry, inode);
1660 put_task_struct(task);
1663 put_task_struct(task);
1665 return ERR_PTR(-ENOENT);
1668 #define PROC_NUMBUF 10
1669 #define PROC_MAXPIDS 20
1672 * Get a few tgid's to return for filldir - we need to hold the
1673 * tasklist lock while doing this, and we must release it before
1674 * we actually do the filldir itself, so we use a temp buffer..
1676 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1678 struct task_struct *p;
1682 read_lock(&tasklist_lock);
1685 p = find_task_by_pid(version);
1686 if (!thread_group_leader(p))
1693 p = next_task(&init_task);
1695 for ( ; p != &init_task; p = next_task(p)) {
1701 tgids[nr_tgids] = tgid;
1703 if (nr_tgids >= PROC_MAXPIDS)
1706 read_unlock(&tasklist_lock);
1711 * Get a few tid's to return for filldir - we need to hold the
1712 * tasklist lock while doing this, and we must release it before
1713 * we actually do the filldir itself, so we use a temp buffer..
1715 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1717 struct task_struct *leader_task = proc_task(dir);
1718 struct task_struct *task = leader_task;
1722 read_lock(&tasklist_lock);
1724 * The starting point task (leader_task) might be an already
1725 * unlinked task, which cannot be used to access the task-list
1726 * via next_thread().
1728 if (pid_alive(task)) do {
1729 int tid = task->pid;
1733 tids[nr_tids] = tid;
1735 if (nr_tids >= PROC_MAXPIDS)
1737 } while ((task = next_thread(task)) != leader_task);
1738 read_unlock(&tasklist_lock);
1742 /* for the /proc/ directory itself, after non-process stuff has been done */
1743 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1745 unsigned int tgid_array[PROC_MAXPIDS];
1746 char buf[PROC_NUMBUF];
1747 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1748 unsigned int nr_tgids, i;
1751 ino_t ino = fake_ino(0,PROC_TGID_INO);
1752 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1759 * f_version caches the last tgid which was returned from readdir
1761 nr_tgids = get_tgid_list(nr, filp->f_version, tgid_array);
1763 for (i = 0; i < nr_tgids; i++) {
1764 int tgid = tgid_array[i];
1765 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1766 unsigned long j = PROC_NUMBUF;
1768 do buf[--j] = '0' + (tgid % 10); while (tgid/=10);
1770 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1771 filp->f_version = tgid;
1779 /* for the /proc/TGID/task/ directories */
1780 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1782 unsigned int tid_array[PROC_MAXPIDS];
1783 char buf[PROC_NUMBUF];
1784 unsigned int nr_tids, i;
1785 struct dentry *dentry = filp->f_dentry;
1786 struct inode *inode = dentry->d_inode;
1787 int retval = -ENOENT;
1789 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
1791 if (!pid_alive(proc_task(inode)))
1798 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1803 ino = parent_ino(dentry);
1804 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1810 nr_tids = get_tid_list(pos, tid_array, inode);
1812 for (i = 0; i < nr_tids; i++) {
1813 unsigned long j = PROC_NUMBUF;
1814 int tid = tid_array[i];
1816 ino = fake_ino(tid,PROC_TID_INO);
1819 buf[--j] = '0' + (tid % 10);
1822 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)