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.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/config.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/string.h>
61 #include <linux/seq_file.h>
62 #include <linux/namei.h>
63 #include <linux/namespace.h>
65 #include <linux/smp_lock.h>
66 #include <linux/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
76 #include <linux/vs_cvirt.h>
77 #include <linux/vs_network.h>
81 * For hysterical raisins we keep the same inumbers as in the old procfs.
82 * Feel free to change the macro below - just keep the range distinct from
83 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
84 * As soon as we'll get a separate superblock we will be able to forget
85 * about magical ranges too.
88 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
90 enum pid_directory_inos {
114 #ifdef CONFIG_SCHEDSTATS
117 #ifdef CONFIG_CPUSETS
120 #ifdef CONFIG_SECURITY
122 PROC_TGID_ATTR_CURRENT,
125 PROC_TGID_ATTR_FSCREATE,
129 #ifdef CONFIG_AUDITSYSCALL
133 PROC_TGID_OOM_ADJUST,
137 #ifdef CONFIG_SECCOMP
156 #ifdef CONFIG_SCHEDSTATS
159 #ifdef CONFIG_CPUSETS
162 #ifdef CONFIG_SECURITY
164 PROC_TID_ATTR_CURRENT,
167 PROC_TID_ATTR_FSCREATE,
171 #ifdef CONFIG_AUDITSYSCALL
177 /* Add new entries before this */
178 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
188 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
190 static struct pid_entry tgid_base_stuff[] = {
191 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
192 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
193 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
194 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
195 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
196 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
197 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
198 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
199 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
201 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
203 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
204 #ifdef CONFIG_SECCOMP
205 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
207 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
208 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
209 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
210 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
212 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
214 #ifdef CONFIG_SECURITY
215 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
217 #ifdef CONFIG_KALLSYMS
218 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
220 #ifdef CONFIG_SCHEDSTATS
221 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
223 #ifdef CONFIG_CPUSETS
224 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
226 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
227 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
228 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
229 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
230 #ifdef CONFIG_AUDITSYSCALL
231 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
235 static struct pid_entry tid_base_stuff[] = {
236 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
237 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
238 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
239 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
240 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
241 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
242 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
243 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
245 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
247 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
248 #ifdef CONFIG_SECCOMP
249 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
251 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
252 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
253 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
254 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
256 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
258 #ifdef CONFIG_SECURITY
259 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
261 #ifdef CONFIG_KALLSYMS
262 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
264 #ifdef CONFIG_SCHEDSTATS
265 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
267 #ifdef CONFIG_CPUSETS
268 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
270 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
271 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
272 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
273 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
274 #ifdef CONFIG_AUDITSYSCALL
275 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
280 #ifdef CONFIG_SECURITY
281 static struct pid_entry tgid_attr_stuff[] = {
282 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
283 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
284 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
285 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
288 static struct pid_entry tid_attr_stuff[] = {
289 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
290 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
291 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
292 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
299 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
301 struct task_struct *task = proc_task(inode);
302 struct files_struct *files;
304 int fd = proc_type(inode) - PROC_TID_FD_DIR;
306 files = get_files_struct(task);
309 * We are not taking a ref to the file structure, so we must
312 spin_lock(&files->file_lock);
313 file = fcheck_files(files, fd);
315 *mnt = mntget(file->f_vfsmnt);
316 *dentry = dget(file->f_dentry);
317 spin_unlock(&files->file_lock);
318 put_files_struct(files);
321 spin_unlock(&files->file_lock);
322 put_files_struct(files);
327 static struct fs_struct *get_fs_struct(struct task_struct *task)
329 struct fs_struct *fs;
333 atomic_inc(&fs->count);
338 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
340 struct fs_struct *fs = get_fs_struct(proc_task(inode));
341 int result = -ENOENT;
343 read_lock(&fs->lock);
344 *mnt = mntget(fs->pwdmnt);
345 *dentry = dget(fs->pwd);
346 read_unlock(&fs->lock);
353 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
355 struct fs_struct *fs = get_fs_struct(proc_task(inode));
356 int result = -ENOENT;
358 read_lock(&fs->lock);
359 *mnt = mntget(fs->rootmnt);
360 *dentry = dget(fs->root);
361 read_unlock(&fs->lock);
369 /* Same as proc_root_link, but this addionally tries to get fs from other
370 * threads in the group */
371 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
372 struct vfsmount **mnt)
374 struct fs_struct *fs;
375 int result = -ENOENT;
376 struct task_struct *leader = proc_task(inode);
381 atomic_inc(&fs->count);
384 /* Try to get fs from other threads */
386 read_lock(&tasklist_lock);
387 if (pid_alive(leader)) {
388 struct task_struct *task = leader;
390 while ((task = next_thread(task)) != leader) {
394 atomic_inc(&fs->count);
401 read_unlock(&tasklist_lock);
405 read_lock(&fs->lock);
406 *mnt = mntget(fs->rootmnt);
407 *dentry = dget(fs->root);
408 read_unlock(&fs->lock);
416 #define MAY_PTRACE(task) \
417 (task == current || \
418 (task->parent == current && \
419 (task->ptrace & PT_PTRACED) && \
420 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
421 security_ptrace(current,task) == 0))
423 static int proc_pid_environ(struct task_struct *task, char * buffer)
426 struct mm_struct *mm = get_task_mm(task);
428 unsigned int len = mm->env_end - mm->env_start;
431 res = access_process_vm(task, mm->env_start, buffer, len, 0);
432 if (!ptrace_may_attach(task))
439 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
443 struct mm_struct *mm = get_task_mm(task);
447 goto out_mm; /* Shh! No looking before we're done */
449 len = mm->arg_end - mm->arg_start;
454 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
456 // If the nul at the end of args has been overwritten, then
457 // assume application is using setproctitle(3).
458 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
459 len = strnlen(buffer, res);
463 len = mm->env_end - mm->env_start;
464 if (len > PAGE_SIZE - res)
465 len = PAGE_SIZE - res;
466 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
467 res = strnlen(buffer, res);
476 static int proc_pid_auxv(struct task_struct *task, char *buffer)
479 struct mm_struct *mm = get_task_mm(task);
481 unsigned int nwords = 0;
484 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
485 res = nwords * sizeof(mm->saved_auxv[0]);
488 memcpy(buffer, mm->saved_auxv, res);
495 #ifdef CONFIG_KALLSYMS
497 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
498 * Returns the resolved symbol. If that fails, simply return the address.
500 static int proc_pid_wchan(struct task_struct *task, char *buffer)
503 const char *sym_name;
504 unsigned long wchan, size, offset;
505 char namebuf[KSYM_NAME_LEN+1];
507 wchan = get_wchan(task);
509 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
511 return sprintf(buffer, "%s", sym_name);
512 return sprintf(buffer, "%lu", wchan);
514 #endif /* CONFIG_KALLSYMS */
516 #ifdef CONFIG_SCHEDSTATS
518 * Provides /proc/PID/schedstat
520 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
522 return sprintf(buffer, "%lu %lu %lu\n",
523 task->sched_info.cpu_time,
524 task->sched_info.run_delay,
525 task->sched_info.pcnt);
529 /* The badness from the OOM killer */
530 unsigned long badness(struct task_struct *p, unsigned long uptime);
531 static int proc_oom_score(struct task_struct *task, char *buffer)
533 unsigned long points;
534 struct timespec uptime;
536 do_posix_clock_monotonic_gettime(&uptime);
537 points = badness(task, uptime.tv_sec);
538 return sprintf(buffer, "%lu\n", points);
541 /************************************************************************/
542 /* Here the fs part begins */
543 /************************************************************************/
545 /* permission checks */
547 /* If the process being read is separated by chroot from the reading process,
548 * don't let the reader access the threads.
550 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
552 struct dentry *de, *base;
553 struct vfsmount *our_vfsmnt, *mnt;
556 /* context admin override */
557 if (capable(CAP_CONTEXT))
560 read_lock(¤t->fs->lock);
561 our_vfsmnt = mntget(current->fs->rootmnt);
562 base = dget(current->fs->root);
563 read_unlock(¤t->fs->lock);
565 spin_lock(&vfsmount_lock);
569 while (mnt != our_vfsmnt) {
570 if (mnt == mnt->mnt_parent)
572 de = mnt->mnt_mountpoint;
573 mnt = mnt->mnt_parent;
576 if (!is_subdir(de, base))
578 spin_unlock(&vfsmount_lock);
588 spin_unlock(&vfsmount_lock);
593 static int proc_check_root(struct inode *inode)
596 struct vfsmount *vfsmnt;
598 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
600 return proc_check_chroot(root, vfsmnt);
603 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
605 if (generic_permission(inode, mask, NULL) != 0)
607 return proc_check_root(inode);
610 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
613 struct inode *inode = dentry->d_inode;
615 if (attr->ia_valid & ATTR_MODE)
618 error = inode_change_ok(inode, attr);
620 error = security_inode_setattr(dentry, attr);
622 error = inode_setattr(inode, attr);
627 static struct inode_operations proc_def_inode_operations = {
628 .setattr = proc_setattr,
631 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
634 struct vfsmount *vfsmnt;
636 if (generic_permission(inode, mask, NULL) != 0)
639 if (proc_task_root_link(inode, &root, &vfsmnt))
642 return proc_check_chroot(root, vfsmnt);
645 extern struct seq_operations proc_pid_maps_op;
646 static int maps_open(struct inode *inode, struct file *file)
648 struct task_struct *task = proc_task(inode);
649 int ret = seq_open(file, &proc_pid_maps_op);
651 struct seq_file *m = file->private_data;
657 static struct file_operations proc_maps_operations = {
661 .release = seq_release,
665 extern struct seq_operations proc_pid_numa_maps_op;
666 static int numa_maps_open(struct inode *inode, struct file *file)
668 struct task_struct *task = proc_task(inode);
669 int ret = seq_open(file, &proc_pid_numa_maps_op);
671 struct seq_file *m = file->private_data;
677 static struct file_operations proc_numa_maps_operations = {
678 .open = numa_maps_open,
681 .release = seq_release,
686 extern struct seq_operations proc_pid_smaps_op;
687 static int smaps_open(struct inode *inode, struct file *file)
689 struct task_struct *task = proc_task(inode);
690 int ret = seq_open(file, &proc_pid_smaps_op);
692 struct seq_file *m = file->private_data;
698 static struct file_operations proc_smaps_operations = {
702 .release = seq_release,
706 extern struct seq_operations mounts_op;
712 static int mounts_open(struct inode *inode, struct file *file)
714 struct task_struct *task = proc_task(inode);
715 struct namespace *namespace;
716 struct proc_mounts *p;
720 namespace = task->namespace;
722 get_namespace(namespace);
727 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
729 file->private_data = &p->m;
730 ret = seq_open(file, &mounts_op);
732 p->m.private = namespace;
733 p->event = namespace->event;
738 put_namespace(namespace);
743 static int mounts_release(struct inode *inode, struct file *file)
745 struct seq_file *m = file->private_data;
746 struct namespace *namespace = m->private;
747 put_namespace(namespace);
748 return seq_release(inode, file);
751 static unsigned mounts_poll(struct file *file, poll_table *wait)
753 struct proc_mounts *p = file->private_data;
754 struct namespace *ns = p->m.private;
757 poll_wait(file, &ns->poll, wait);
759 spin_lock(&vfsmount_lock);
760 if (p->event != ns->event) {
761 p->event = ns->event;
764 spin_unlock(&vfsmount_lock);
769 static struct file_operations proc_mounts_operations = {
773 .release = mounts_release,
777 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
779 static ssize_t proc_info_read(struct file * file, char __user * buf,
780 size_t count, loff_t *ppos)
782 struct inode * inode = file->f_dentry->d_inode;
785 struct task_struct *task = proc_task(inode);
787 if (count > PROC_BLOCK_SIZE)
788 count = PROC_BLOCK_SIZE;
789 if (!(page = __get_free_page(GFP_KERNEL)))
792 length = PROC_I(inode)->op.proc_read(task, (char*)page);
795 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
800 static struct file_operations proc_info_file_operations = {
801 .read = proc_info_read,
804 static int mem_open(struct inode* inode, struct file* file)
806 file->private_data = (void*)((long)current->self_exec_id);
810 static ssize_t mem_read(struct file * file, char __user * buf,
811 size_t count, loff_t *ppos)
813 struct task_struct *task = proc_task(file->f_dentry->d_inode);
815 unsigned long src = *ppos;
817 struct mm_struct *mm;
819 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
823 page = (char *)__get_free_page(GFP_USER);
829 mm = get_task_mm(task);
835 if (file->private_data != (void*)((long)current->self_exec_id))
841 int this_len, retval;
843 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
844 retval = access_process_vm(task, src, page, this_len, 0);
845 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
851 if (copy_to_user(buf, page, retval)) {
866 free_page((unsigned long) page);
871 #define mem_write NULL
874 /* This is a security hazard */
875 static ssize_t mem_write(struct file * file, const char * buf,
876 size_t count, loff_t *ppos)
880 struct task_struct *task = proc_task(file->f_dentry->d_inode);
881 unsigned long dst = *ppos;
883 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
886 page = (char *)__get_free_page(GFP_USER);
891 int this_len, retval;
893 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
894 if (copy_from_user(page, buf, this_len)) {
898 retval = access_process_vm(task, dst, page, this_len, 1);
910 free_page((unsigned long) page);
915 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
919 file->f_pos = offset;
922 file->f_pos += offset;
927 force_successful_syscall_return();
931 static struct file_operations proc_mem_operations = {
938 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
939 size_t count, loff_t *ppos)
941 struct task_struct *task = proc_task(file->f_dentry->d_inode);
944 int oom_adjust = task->oomkilladj;
945 loff_t __ppos = *ppos;
947 len = sprintf(buffer, "%i\n", oom_adjust);
950 if (count > len-__ppos)
952 if (copy_to_user(buf, buffer + __ppos, count))
954 *ppos = __ppos + count;
958 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
959 size_t count, loff_t *ppos)
961 struct task_struct *task = proc_task(file->f_dentry->d_inode);
962 char buffer[8], *end;
965 if (!capable(CAP_SYS_RESOURCE))
967 memset(buffer, 0, 8);
970 if (copy_from_user(buffer, buf, count))
972 oom_adjust = simple_strtol(buffer, &end, 0);
973 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
977 task->oomkilladj = oom_adjust;
978 if (end - buffer == 0)
983 static struct file_operations proc_oom_adjust_operations = {
984 .read = oom_adjust_read,
985 .write = oom_adjust_write,
988 static struct inode_operations proc_mem_inode_operations = {
989 .permission = proc_permission,
990 .setattr = proc_setattr,
993 #ifdef CONFIG_AUDITSYSCALL
995 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
996 size_t count, loff_t *ppos)
998 struct inode * inode = file->f_dentry->d_inode;
999 struct task_struct *task = proc_task(inode);
1001 char tmpbuf[TMPBUFLEN];
1003 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1004 audit_get_loginuid(task->audit_context));
1005 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1008 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1009 size_t count, loff_t *ppos)
1011 struct inode * inode = file->f_dentry->d_inode;
1014 struct task_struct *task = proc_task(inode);
1017 if (!capable(CAP_AUDIT_CONTROL))
1020 if (current != task)
1023 if (count > PAGE_SIZE)
1027 /* No partial writes. */
1030 page = (char*)__get_free_page(GFP_USER);
1034 if (copy_from_user(page, buf, count))
1037 loginuid = simple_strtoul(page, &tmp, 10);
1043 length = audit_set_loginuid(task, loginuid);
1044 if (likely(length == 0))
1048 free_page((unsigned long) page);
1052 static struct file_operations proc_loginuid_operations = {
1053 .read = proc_loginuid_read,
1054 .write = proc_loginuid_write,
1058 #ifdef CONFIG_SECCOMP
1059 static ssize_t seccomp_read(struct file *file, char __user *buf,
1060 size_t count, loff_t *ppos)
1062 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1064 loff_t __ppos = *ppos;
1067 /* no need to print the trailing zero, so use only len */
1068 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1071 if (count > len - __ppos)
1072 count = len - __ppos;
1073 if (copy_to_user(buf, __buf + __ppos, count))
1075 *ppos = __ppos + count;
1079 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1080 size_t count, loff_t *ppos)
1082 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1083 char __buf[20], *end;
1084 unsigned int seccomp_mode;
1086 /* can set it only once to be even more secure */
1087 if (unlikely(tsk->seccomp.mode))
1090 memset(__buf, 0, sizeof(__buf));
1091 count = min(count, sizeof(__buf) - 1);
1092 if (copy_from_user(__buf, buf, count))
1094 seccomp_mode = simple_strtoul(__buf, &end, 0);
1097 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1098 tsk->seccomp.mode = seccomp_mode;
1099 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1102 if (unlikely(!(end - __buf)))
1107 static struct file_operations proc_seccomp_operations = {
1108 .read = seccomp_read,
1109 .write = seccomp_write,
1111 #endif /* CONFIG_SECCOMP */
1113 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1115 struct inode *inode = dentry->d_inode;
1116 int error = -EACCES;
1118 /* We don't need a base pointer in the /proc filesystem */
1121 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1123 error = proc_check_root(inode);
1127 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1128 nd->last_type = LAST_BIND;
1130 return ERR_PTR(error);
1133 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1134 char __user *buffer, int buflen)
1136 struct inode * inode;
1137 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1143 inode = dentry->d_inode;
1144 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1145 len = PTR_ERR(path);
1148 len = tmp + PAGE_SIZE - 1 - path;
1152 if (copy_to_user(buffer, path, len))
1155 free_page((unsigned long)tmp);
1159 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1161 int error = -EACCES;
1162 struct inode *inode = dentry->d_inode;
1164 struct vfsmount *mnt = NULL;
1168 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1170 error = proc_check_root(inode);
1174 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1178 error = do_proc_readlink(de, mnt, buffer, buflen);
1186 static struct inode_operations proc_pid_link_inode_operations = {
1187 .readlink = proc_pid_readlink,
1188 .follow_link = proc_pid_follow_link,
1189 .setattr = proc_setattr,
1194 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1196 struct inode *inode = filp->f_dentry->d_inode;
1197 struct task_struct *p = proc_task(inode);
1198 unsigned int fd, tid, ino;
1201 struct files_struct * files;
1202 struct fdtable *fdt;
1213 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1217 ino = fake_ino(tid, PROC_TID_INO);
1218 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1222 files = get_files_struct(p);
1226 fdt = files_fdtable(files);
1227 for (fd = filp->f_pos-2;
1229 fd++, filp->f_pos++) {
1232 if (!fcheck_files(files, fd))
1240 buf[j] = '0' + (i % 10);
1244 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1245 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1252 put_files_struct(files);
1258 static int proc_pident_readdir(struct file *filp,
1259 void *dirent, filldir_t filldir,
1260 struct pid_entry *ents, unsigned int nents)
1264 struct dentry *dentry = filp->f_dentry;
1265 struct inode *inode = dentry->d_inode;
1266 struct pid_entry *p;
1271 if (!pid_alive(proc_task(inode)))
1275 pid = proc_task(inode)->pid;
1280 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1286 ino = parent_ino(dentry);
1287 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1299 hide = vx_flags(VXF_INFO_HIDE, 0);
1303 case PROC_TGID_VX_INFO:
1304 case PROC_TGID_IP_INFO:
1308 if (filldir(dirent, p->name, p->len, filp->f_pos,
1309 fake_ino(pid, p->type), p->mode >> 12) < 0)
1322 static int proc_tgid_base_readdir(struct file * filp,
1323 void * dirent, filldir_t filldir)
1325 return proc_pident_readdir(filp,dirent,filldir,
1326 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1329 static int proc_tid_base_readdir(struct file * filp,
1330 void * dirent, filldir_t filldir)
1332 return proc_pident_readdir(filp,dirent,filldir,
1333 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1336 /* building an inode */
1338 static int task_dumpable(struct task_struct *task)
1341 struct mm_struct *mm;
1346 dumpable = mm->dumpable;
1354 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1356 struct inode * inode;
1357 struct proc_inode *ei;
1359 /* We need a new inode */
1361 inode = new_inode(sb);
1368 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1369 inode->i_ino = fake_ino(task->pid, ino);
1370 inode->i_op = &proc_def_inode_operations;
1372 if (!pid_alive(task))
1376 * grab the reference to task.
1378 get_task_struct(task);
1383 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1384 inode->i_uid = task->euid;
1385 inode->i_gid = task->egid;
1387 inode->i_xid = vx_task_xid(task);
1388 security_task_to_inode(task, inode);
1402 * Exceptional case: normally we are not allowed to unhash a busy
1403 * directory. In this case, however, we can do it - no aliasing problems
1404 * due to the way we treat inodes.
1406 * Rewrite the inode's ownerships here because the owning task may have
1407 * performed a setuid(), etc.
1409 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1411 struct inode *inode = dentry->d_inode;
1412 struct task_struct *task = proc_task(inode);
1414 if (!vx_check(vx_task_xid(task), VX_IDENT))
1416 /* discard wrong fakeinit */
1418 if (pid_alive(task)) {
1419 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1420 inode->i_uid = task->euid;
1421 inode->i_gid = task->egid;
1426 inode->i_mode &= ~(S_ISUID | S_ISGID);
1427 security_task_to_inode(task, inode);
1435 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1437 struct inode *inode = dentry->d_inode;
1438 struct task_struct *task = proc_task(inode);
1439 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1440 struct files_struct *files;
1442 files = get_files_struct(task);
1445 if (fcheck_files(files, fd)) {
1447 put_files_struct(files);
1448 if (task_dumpable(task)) {
1449 inode->i_uid = task->euid;
1450 inode->i_gid = task->egid;
1455 inode->i_mode &= ~(S_ISUID | S_ISGID);
1456 security_task_to_inode(task, inode);
1460 put_files_struct(files);
1466 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1468 struct task_struct *task = proc_task(inode);
1469 spin_lock(&task->proc_lock);
1470 if (task->proc_dentry == dentry)
1471 task->proc_dentry = NULL;
1472 spin_unlock(&task->proc_lock);
1476 static int pid_delete_dentry(struct dentry * dentry)
1478 /* Is the task we represent dead?
1479 * If so, then don't put the dentry on the lru list,
1480 * kill it immediately.
1482 return !pid_alive(proc_task(dentry->d_inode));
1485 static struct dentry_operations tid_fd_dentry_operations =
1487 .d_revalidate = tid_fd_revalidate,
1488 .d_delete = pid_delete_dentry,
1491 static struct dentry_operations pid_dentry_operations =
1493 .d_revalidate = pid_revalidate,
1494 .d_delete = pid_delete_dentry,
1497 static struct dentry_operations pid_base_dentry_operations =
1499 .d_revalidate = pid_revalidate,
1500 .d_iput = pid_base_iput,
1501 .d_delete = pid_delete_dentry,
1506 static unsigned name_to_int(struct dentry *dentry)
1508 const char *name = dentry->d_name.name;
1509 int len = dentry->d_name.len;
1512 if (len > 1 && *name == '0')
1515 unsigned c = *name++ - '0';
1518 if (n >= (~0U-9)/10)
1529 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1531 struct task_struct *task = proc_task(dir);
1532 unsigned fd = name_to_int(dentry);
1534 struct files_struct * files;
1535 struct inode *inode;
1536 struct proc_inode *ei;
1540 if (!pid_alive(task))
1543 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1547 files = get_files_struct(task);
1550 inode->i_mode = S_IFLNK;
1553 * We are not taking a ref to the file structure, so we must
1556 spin_lock(&files->file_lock);
1557 file = fcheck_files(files, fd);
1560 if (file->f_mode & 1)
1561 inode->i_mode |= S_IRUSR | S_IXUSR;
1562 if (file->f_mode & 2)
1563 inode->i_mode |= S_IWUSR | S_IXUSR;
1564 spin_unlock(&files->file_lock);
1565 put_files_struct(files);
1566 inode->i_op = &proc_pid_link_inode_operations;
1568 ei->op.proc_get_link = proc_fd_link;
1569 dentry->d_op = &tid_fd_dentry_operations;
1570 d_add(dentry, inode);
1574 spin_unlock(&files->file_lock);
1575 put_files_struct(files);
1579 return ERR_PTR(-ENOENT);
1582 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1583 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1585 static struct file_operations proc_fd_operations = {
1586 .read = generic_read_dir,
1587 .readdir = proc_readfd,
1590 static struct file_operations proc_task_operations = {
1591 .read = generic_read_dir,
1592 .readdir = proc_task_readdir,
1596 * proc directories can do almost nothing..
1598 static struct inode_operations proc_fd_inode_operations = {
1599 .lookup = proc_lookupfd,
1600 .permission = proc_permission,
1601 .setattr = proc_setattr,
1604 static struct inode_operations proc_task_inode_operations = {
1605 .lookup = proc_task_lookup,
1606 .permission = proc_task_permission,
1607 .setattr = proc_setattr,
1610 #ifdef CONFIG_SECURITY
1611 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1612 size_t count, loff_t *ppos)
1614 struct inode * inode = file->f_dentry->d_inode;
1617 struct task_struct *task = proc_task(inode);
1619 if (count > PAGE_SIZE)
1621 if (!(page = __get_free_page(GFP_KERNEL)))
1624 length = security_getprocattr(task,
1625 (char*)file->f_dentry->d_name.name,
1626 (void*)page, count);
1628 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1633 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1634 size_t count, loff_t *ppos)
1636 struct inode * inode = file->f_dentry->d_inode;
1639 struct task_struct *task = proc_task(inode);
1641 if (count > PAGE_SIZE)
1644 /* No partial writes. */
1647 page = (char*)__get_free_page(GFP_USER);
1651 if (copy_from_user(page, buf, count))
1654 length = security_setprocattr(task,
1655 (char*)file->f_dentry->d_name.name,
1656 (void*)page, count);
1658 free_page((unsigned long) page);
1662 static struct file_operations proc_pid_attr_operations = {
1663 .read = proc_pid_attr_read,
1664 .write = proc_pid_attr_write,
1667 static struct file_operations proc_tid_attr_operations;
1668 static struct inode_operations proc_tid_attr_inode_operations;
1669 static struct file_operations proc_tgid_attr_operations;
1670 static struct inode_operations proc_tgid_attr_inode_operations;
1673 extern int proc_pid_vx_info(struct task_struct *, char *);
1674 extern int proc_pid_nx_info(struct task_struct *, char *);
1676 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1679 static struct dentry *proc_pident_lookup(struct inode *dir,
1680 struct dentry *dentry,
1681 struct pid_entry *ents)
1683 struct inode *inode;
1685 struct task_struct *task = proc_task(dir);
1686 struct pid_entry *p;
1687 struct proc_inode *ei;
1692 if (!pid_alive(task))
1695 for (p = ents; p->name; p++) {
1696 if (p->len != dentry->d_name.len)
1698 if (!memcmp(dentry->d_name.name, p->name, p->len))
1705 case PROC_TID_VX_INFO:
1706 case PROC_TGID_VX_INFO:
1707 case PROC_TID_IP_INFO:
1708 case PROC_TGID_IP_INFO:
1709 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
1716 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1721 inode->i_mode = p->mode;
1723 * Yes, it does not scale. And it should not. Don't add
1724 * new entries into /proc/<tgid>/ without very good reasons.
1727 case PROC_TGID_TASK:
1728 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1729 inode->i_op = &proc_task_inode_operations;
1730 inode->i_fop = &proc_task_operations;
1735 inode->i_op = &proc_fd_inode_operations;
1736 inode->i_fop = &proc_fd_operations;
1740 inode->i_op = &proc_pid_link_inode_operations;
1741 ei->op.proc_get_link = proc_exe_link;
1745 inode->i_op = &proc_pid_link_inode_operations;
1746 ei->op.proc_get_link = proc_cwd_link;
1749 case PROC_TGID_ROOT:
1750 inode->i_op = &proc_pid_link_inode_operations;
1751 ei->op.proc_get_link = proc_root_link;
1753 case PROC_TID_ENVIRON:
1754 case PROC_TGID_ENVIRON:
1755 inode->i_fop = &proc_info_file_operations;
1756 ei->op.proc_read = proc_pid_environ;
1759 case PROC_TGID_AUXV:
1760 inode->i_fop = &proc_info_file_operations;
1761 ei->op.proc_read = proc_pid_auxv;
1763 case PROC_TID_STATUS:
1764 case PROC_TGID_STATUS:
1765 inode->i_fop = &proc_info_file_operations;
1766 ei->op.proc_read = proc_pid_status;
1769 inode->i_fop = &proc_info_file_operations;
1770 ei->op.proc_read = proc_tid_stat;
1772 case PROC_TGID_STAT:
1773 inode->i_fop = &proc_info_file_operations;
1774 ei->op.proc_read = proc_tgid_stat;
1776 case PROC_TID_CMDLINE:
1777 case PROC_TGID_CMDLINE:
1778 inode->i_fop = &proc_info_file_operations;
1779 ei->op.proc_read = proc_pid_cmdline;
1781 case PROC_TID_STATM:
1782 case PROC_TGID_STATM:
1783 inode->i_fop = &proc_info_file_operations;
1784 ei->op.proc_read = proc_pid_statm;
1787 case PROC_TGID_MAPS:
1788 inode->i_fop = &proc_maps_operations;
1791 case PROC_TID_NUMA_MAPS:
1792 case PROC_TGID_NUMA_MAPS:
1793 inode->i_fop = &proc_numa_maps_operations;
1798 inode->i_op = &proc_mem_inode_operations;
1799 inode->i_fop = &proc_mem_operations;
1801 #ifdef CONFIG_SECCOMP
1802 case PROC_TID_SECCOMP:
1803 case PROC_TGID_SECCOMP:
1804 inode->i_fop = &proc_seccomp_operations;
1806 #endif /* CONFIG_SECCOMP */
1807 case PROC_TID_MOUNTS:
1808 case PROC_TGID_MOUNTS:
1809 inode->i_fop = &proc_mounts_operations;
1812 case PROC_TID_SMAPS:
1813 case PROC_TGID_SMAPS:
1814 inode->i_fop = &proc_smaps_operations;
1817 #ifdef CONFIG_SECURITY
1820 inode->i_op = &proc_tid_attr_inode_operations;
1821 inode->i_fop = &proc_tid_attr_operations;
1823 case PROC_TGID_ATTR:
1825 inode->i_op = &proc_tgid_attr_inode_operations;
1826 inode->i_fop = &proc_tgid_attr_operations;
1828 case PROC_TID_ATTR_CURRENT:
1829 case PROC_TGID_ATTR_CURRENT:
1830 case PROC_TID_ATTR_PREV:
1831 case PROC_TGID_ATTR_PREV:
1832 case PROC_TID_ATTR_EXEC:
1833 case PROC_TGID_ATTR_EXEC:
1834 case PROC_TID_ATTR_FSCREATE:
1835 case PROC_TGID_ATTR_FSCREATE:
1836 inode->i_fop = &proc_pid_attr_operations;
1839 #ifdef CONFIG_KALLSYMS
1840 case PROC_TID_WCHAN:
1841 case PROC_TGID_WCHAN:
1842 inode->i_fop = &proc_info_file_operations;
1843 ei->op.proc_read = proc_pid_wchan;
1846 #ifdef CONFIG_SCHEDSTATS
1847 case PROC_TID_SCHEDSTAT:
1848 case PROC_TGID_SCHEDSTAT:
1849 inode->i_fop = &proc_info_file_operations;
1850 ei->op.proc_read = proc_pid_schedstat;
1853 #ifdef CONFIG_CPUSETS
1854 case PROC_TID_CPUSET:
1855 case PROC_TGID_CPUSET:
1856 inode->i_fop = &proc_cpuset_operations;
1859 case PROC_TID_OOM_SCORE:
1860 case PROC_TGID_OOM_SCORE:
1861 inode->i_fop = &proc_info_file_operations;
1862 ei->op.proc_read = proc_oom_score;
1864 case PROC_TID_OOM_ADJUST:
1865 case PROC_TGID_OOM_ADJUST:
1866 inode->i_fop = &proc_oom_adjust_operations;
1868 #ifdef CONFIG_AUDITSYSCALL
1869 case PROC_TID_LOGINUID:
1870 case PROC_TGID_LOGINUID:
1871 inode->i_fop = &proc_loginuid_operations;
1874 case PROC_TID_VX_INFO:
1875 case PROC_TGID_VX_INFO:
1876 inode->i_fop = &proc_info_file_operations;
1877 ei->op.proc_read = proc_pid_vx_info;
1879 case PROC_TID_IP_INFO:
1880 case PROC_TGID_IP_INFO:
1881 inode->i_fop = &proc_info_file_operations;
1882 ei->op.proc_read = proc_pid_nx_info;
1885 printk("procfs: impossible type (%d)",p->type);
1889 dentry->d_op = &pid_dentry_operations;
1890 d_add(dentry, inode);
1896 return ERR_PTR(error);
1899 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1900 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1903 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1904 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1907 static struct file_operations proc_tgid_base_operations = {
1908 .read = generic_read_dir,
1909 .readdir = proc_tgid_base_readdir,
1912 static struct file_operations proc_tid_base_operations = {
1913 .read = generic_read_dir,
1914 .readdir = proc_tid_base_readdir,
1917 static struct inode_operations proc_tgid_base_inode_operations = {
1918 .lookup = proc_tgid_base_lookup,
1919 .setattr = proc_setattr,
1922 static struct inode_operations proc_tid_base_inode_operations = {
1923 .lookup = proc_tid_base_lookup,
1924 .setattr = proc_setattr,
1927 #ifdef CONFIG_SECURITY
1928 static int proc_tgid_attr_readdir(struct file * filp,
1929 void * dirent, filldir_t filldir)
1931 return proc_pident_readdir(filp,dirent,filldir,
1932 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1935 static int proc_tid_attr_readdir(struct file * filp,
1936 void * dirent, filldir_t filldir)
1938 return proc_pident_readdir(filp,dirent,filldir,
1939 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1942 static struct file_operations proc_tgid_attr_operations = {
1943 .read = generic_read_dir,
1944 .readdir = proc_tgid_attr_readdir,
1947 static struct file_operations proc_tid_attr_operations = {
1948 .read = generic_read_dir,
1949 .readdir = proc_tid_attr_readdir,
1952 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1953 struct dentry *dentry, struct nameidata *nd)
1955 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1958 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1959 struct dentry *dentry, struct nameidata *nd)
1961 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1964 static struct inode_operations proc_tgid_attr_inode_operations = {
1965 .lookup = proc_tgid_attr_lookup,
1966 .setattr = proc_setattr,
1969 static struct inode_operations proc_tid_attr_inode_operations = {
1970 .lookup = proc_tid_attr_lookup,
1971 .setattr = proc_setattr,
1978 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1982 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1983 return vfs_readlink(dentry,buffer,buflen,tmp);
1986 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1989 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1990 return ERR_PTR(vfs_follow_link(nd,tmp));
1993 static struct inode_operations proc_self_inode_operations = {
1994 .readlink = proc_self_readlink,
1995 .follow_link = proc_self_follow_link,
1996 .setattr = proc_setattr,
2000 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
2001 * @p: task that should be flushed.
2003 * Drops the /proc/@pid dcache entry from the hash chains.
2005 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
2006 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
2007 * if the pid value is immediately reused. This is enforced by
2008 * - caller must acquire spin_lock(p->proc_lock)
2009 * - must be called before detach_pid()
2010 * - proc_pid_lookup acquires proc_lock, and checks that
2011 * the target is not dead by looking at the attach count
2015 struct dentry *proc_pid_unhash(struct task_struct *p)
2017 struct dentry *proc_dentry;
2019 proc_dentry = p->proc_dentry;
2020 if (proc_dentry != NULL) {
2022 spin_lock(&dcache_lock);
2023 spin_lock(&proc_dentry->d_lock);
2024 if (!d_unhashed(proc_dentry)) {
2025 dget_locked(proc_dentry);
2026 __d_drop(proc_dentry);
2027 spin_unlock(&proc_dentry->d_lock);
2029 spin_unlock(&proc_dentry->d_lock);
2032 spin_unlock(&dcache_lock);
2038 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
2039 * @proc_dentry: directoy to prune.
2041 * Shrink the /proc directory that was used by the just killed thread.
2044 void proc_pid_flush(struct dentry *proc_dentry)
2047 if(proc_dentry != NULL) {
2048 shrink_dcache_parent(proc_dentry);
2053 #define VXF_FAKE_INIT (VXF_INFO_INIT|VXF_STATE_INIT)
2055 static inline int proc_pid_visible(struct task_struct *task, int pid)
2058 !vx_flags(VXF_FAKE_INIT, VXF_FAKE_INIT))
2060 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
2068 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2070 struct task_struct *task;
2071 struct inode *inode;
2072 struct proc_inode *ei;
2076 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2077 inode = new_inode(dir->i_sb);
2079 return ERR_PTR(-ENOMEM);
2081 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2082 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2084 inode->i_mode = S_IFLNK|S_IRWXUGO;
2085 inode->i_uid = inode->i_gid = 0;
2087 inode->i_op = &proc_self_inode_operations;
2088 d_add(dentry, inode);
2091 tgid = name_to_int(dentry);
2095 read_lock(&tasklist_lock);
2096 task = find_task_by_pid(tgid);
2098 get_task_struct(task);
2099 read_unlock(&tasklist_lock);
2103 /* check for context visibility */
2104 if (!proc_pid_visible(task, tgid))
2107 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2111 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2112 inode->i_op = &proc_tgid_base_inode_operations;
2113 inode->i_fop = &proc_tgid_base_operations;
2114 inode->i_flags|=S_IMMUTABLE;
2115 #ifdef CONFIG_SECURITY
2121 dentry->d_op = &pid_base_dentry_operations;
2124 d_add(dentry, inode);
2125 spin_lock(&task->proc_lock);
2126 task->proc_dentry = dentry;
2127 if (!pid_alive(task)) {
2128 dentry = proc_pid_unhash(task);
2131 spin_unlock(&task->proc_lock);
2133 put_task_struct(task);
2135 proc_pid_flush(dentry);
2140 put_task_struct(task);
2142 return ERR_PTR(-ENOENT);
2146 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2148 struct task_struct *task;
2149 struct task_struct *leader = proc_task(dir);
2150 struct inode *inode;
2153 tid = name_to_int(dentry);
2156 if (vx_current_initpid(tid))
2159 read_lock(&tasklist_lock);
2160 task = find_task_by_pid(tid);
2162 get_task_struct(task);
2163 read_unlock(&tasklist_lock);
2166 if (leader->tgid != task->tgid)
2169 /* check for context visibility */
2170 if (!proc_pid_visible(task, tid))
2173 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2177 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2178 inode->i_op = &proc_tid_base_inode_operations;
2179 inode->i_fop = &proc_tid_base_operations;
2180 inode->i_flags|=S_IMMUTABLE;
2181 #ifdef CONFIG_SECURITY
2187 dentry->d_op = &pid_base_dentry_operations;
2189 d_add(dentry, inode);
2191 put_task_struct(task);
2194 put_task_struct(task);
2196 return ERR_PTR(-ENOENT);
2199 #define PROC_NUMBUF 10
2200 #define PROC_MAXPIDS 20
2203 * Get a few tgid's to return for filldir - we need to hold the
2204 * tasklist lock while doing this, and we must release it before
2205 * we actually do the filldir itself, so we use a temp buffer..
2207 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2209 struct task_struct *p;
2213 read_lock(&tasklist_lock);
2216 p = find_task_by_real_pid(version);
2217 if (p && !thread_group_leader(p))
2224 p = next_task(&init_task);
2226 for ( ; p != &init_task; p = next_task(p)) {
2231 /* check for context visibility */
2232 if (!proc_pid_visible(p, tgid))
2236 tgids[nr_tgids] = vx_map_tgid(tgid);
2238 if (nr_tgids >= PROC_MAXPIDS)
2241 read_unlock(&tasklist_lock);
2246 * Get a few tid's to return for filldir - we need to hold the
2247 * tasklist lock while doing this, and we must release it before
2248 * we actually do the filldir itself, so we use a temp buffer..
2250 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2252 struct task_struct *leader_task = proc_task(dir);
2253 struct task_struct *task = leader_task;
2257 read_lock(&tasklist_lock);
2259 * The starting point task (leader_task) might be an already
2260 * unlinked task, which cannot be used to access the task-list
2261 * via next_thread().
2263 if (pid_alive(task)) do {
2264 int tid = task->pid;
2266 /* check for context visibility */
2267 if (!proc_pid_visible(task, tid))
2272 tids[nr_tids] = vx_map_pid(tid);
2274 if (nr_tids >= PROC_MAXPIDS)
2276 } while ((task = next_thread(task)) != leader_task);
2277 read_unlock(&tasklist_lock);
2281 /* for the /proc/ directory itself, after non-process stuff has been done */
2282 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2284 unsigned int tgid_array[PROC_MAXPIDS];
2285 char buf[PROC_NUMBUF];
2286 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2287 unsigned int nr_tgids, i;
2291 ino_t ino = fake_ino(0,PROC_TGID_INO);
2292 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2298 /* f_version caches the tgid value that the last readdir call couldn't
2299 * return. lseek aka telldir automagically resets f_version to 0.
2301 next_tgid = filp->f_version;
2302 filp->f_version = 0;
2304 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2306 /* no more entries ! */
2311 /* do not use the last found pid, reserve it for next_tgid */
2312 if (nr_tgids == PROC_MAXPIDS) {
2314 next_tgid = tgid_array[nr_tgids];
2317 for (i=0;i<nr_tgids;i++) {
2318 int tgid = tgid_array[i];
2319 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2320 unsigned long j = PROC_NUMBUF;
2323 buf[--j] = '0' + (tgid % 10);
2324 while ((tgid /= 10) != 0);
2326 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2327 /* returning this tgid failed, save it as the first
2328 * pid for the next readir call */
2329 filp->f_version = tgid_array[i];
2340 /* for the /proc/TGID/task/ directories */
2341 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2343 unsigned int tid_array[PROC_MAXPIDS];
2344 char buf[PROC_NUMBUF];
2345 unsigned int nr_tids, i;
2346 struct dentry *dentry = filp->f_dentry;
2347 struct inode *inode = dentry->d_inode;
2348 struct task_struct *task = proc_task(inode);
2349 int retval = -ENOENT;
2351 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2353 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
2355 if (!pid_alive(task))
2362 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2367 ino = parent_ino(dentry);
2368 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2374 nr_tids = get_tid_list(pos, tid_array, inode);
2375 inode->i_nlink = pos + nr_tids;
2377 for (i = 0; i < nr_tids; i++) {
2378 unsigned long j = PROC_NUMBUF;
2379 int tid = tid_array[i];
2381 ino = fake_ino(tid,PROC_TID_INO);
2384 buf[--j] = '0' + (tid % 10);
2385 while ((tid /= 10) != 0);
2387 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)