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>
75 #include <linux/vs_cvirt.h>
76 #include <linux/vs_network.h>
80 * For hysterical raisins we keep the same inumbers as in the old procfs.
81 * Feel free to change the macro below - just keep the range distinct from
82 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
83 * As soon as we'll get a separate superblock we will be able to forget
84 * about magical ranges too.
87 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
89 enum pid_directory_inos {
113 #ifdef CONFIG_SCHEDSTATS
116 #ifdef CONFIG_CPUSETS
119 #ifdef CONFIG_SECURITY
121 PROC_TGID_ATTR_CURRENT,
124 PROC_TGID_ATTR_FSCREATE,
128 #ifdef CONFIG_AUDITSYSCALL
132 PROC_TGID_OOM_ADJUST,
136 #ifdef CONFIG_SECCOMP
155 #ifdef CONFIG_SCHEDSTATS
158 #ifdef CONFIG_CPUSETS
161 #ifdef CONFIG_SECURITY
163 PROC_TID_ATTR_CURRENT,
166 PROC_TID_ATTR_FSCREATE,
170 #ifdef CONFIG_AUDITSYSCALL
176 /* Add new entries before this */
177 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
187 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
189 static struct pid_entry tgid_base_stuff[] = {
190 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
191 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
192 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
193 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
194 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
195 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
196 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
197 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
198 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
200 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
202 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
203 #ifdef CONFIG_SECCOMP
204 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
206 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
207 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
208 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
209 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
211 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
213 #ifdef CONFIG_SECURITY
214 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
216 #ifdef CONFIG_KALLSYMS
217 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
219 #ifdef CONFIG_SCHEDSTATS
220 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
222 #ifdef CONFIG_CPUSETS
223 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
225 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
226 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
227 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
228 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
229 #ifdef CONFIG_AUDITSYSCALL
230 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
234 static struct pid_entry tid_base_stuff[] = {
235 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
236 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
237 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
238 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
239 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
240 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
241 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
242 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
244 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
246 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
247 #ifdef CONFIG_SECCOMP
248 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
250 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
251 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
252 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
253 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
255 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
257 #ifdef CONFIG_SECURITY
258 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
260 #ifdef CONFIG_KALLSYMS
261 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
263 #ifdef CONFIG_SCHEDSTATS
264 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
266 #ifdef CONFIG_CPUSETS
267 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
269 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
270 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
271 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
272 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
273 #ifdef CONFIG_AUDITSYSCALL
274 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
279 #ifdef CONFIG_SECURITY
280 static struct pid_entry tgid_attr_stuff[] = {
281 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
282 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
283 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
284 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
287 static struct pid_entry tid_attr_stuff[] = {
288 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
289 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
290 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
291 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
298 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
300 struct task_struct *task = proc_task(inode);
301 struct files_struct *files;
303 int fd = proc_type(inode) - PROC_TID_FD_DIR;
305 files = get_files_struct(task);
308 * We are not taking a ref to the file structure, so we must
311 spin_lock(&files->file_lock);
312 file = fcheck_files(files, fd);
314 *mnt = mntget(file->f_vfsmnt);
315 *dentry = dget(file->f_dentry);
316 spin_unlock(&files->file_lock);
317 put_files_struct(files);
320 spin_unlock(&files->file_lock);
321 put_files_struct(files);
326 static struct fs_struct *get_fs_struct(struct task_struct *task)
328 struct fs_struct *fs;
332 atomic_inc(&fs->count);
337 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
339 struct fs_struct *fs = get_fs_struct(proc_task(inode));
340 int result = -ENOENT;
342 read_lock(&fs->lock);
343 *mnt = mntget(fs->pwdmnt);
344 *dentry = dget(fs->pwd);
345 read_unlock(&fs->lock);
352 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
354 struct fs_struct *fs = get_fs_struct(proc_task(inode));
355 int result = -ENOENT;
357 read_lock(&fs->lock);
358 *mnt = mntget(fs->rootmnt);
359 *dentry = dget(fs->root);
360 read_unlock(&fs->lock);
368 /* Same as proc_root_link, but this addionally tries to get fs from other
369 * threads in the group */
370 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
371 struct vfsmount **mnt)
373 struct fs_struct *fs;
374 int result = -ENOENT;
375 struct task_struct *leader = proc_task(inode);
380 atomic_inc(&fs->count);
383 /* Try to get fs from other threads */
385 read_lock(&tasklist_lock);
386 if (pid_alive(leader)) {
387 struct task_struct *task = leader;
389 while ((task = next_thread(task)) != leader) {
393 atomic_inc(&fs->count);
400 read_unlock(&tasklist_lock);
404 read_lock(&fs->lock);
405 *mnt = mntget(fs->rootmnt);
406 *dentry = dget(fs->root);
407 read_unlock(&fs->lock);
415 #define MAY_PTRACE(task) \
416 (task == current || \
417 (task->parent == current && \
418 (task->ptrace & PT_PTRACED) && \
419 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
420 security_ptrace(current,task) == 0))
422 static int proc_pid_environ(struct task_struct *task, char * buffer)
425 struct mm_struct *mm = get_task_mm(task);
427 unsigned int len = mm->env_end - mm->env_start;
430 res = access_process_vm(task, mm->env_start, buffer, len, 0);
431 if (!ptrace_may_attach(task))
438 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
442 struct mm_struct *mm = get_task_mm(task);
446 goto out_mm; /* Shh! No looking before we're done */
448 len = mm->arg_end - mm->arg_start;
453 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
455 // If the nul at the end of args has been overwritten, then
456 // assume application is using setproctitle(3).
457 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
458 len = strnlen(buffer, res);
462 len = mm->env_end - mm->env_start;
463 if (len > PAGE_SIZE - res)
464 len = PAGE_SIZE - res;
465 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
466 res = strnlen(buffer, res);
475 static int proc_pid_auxv(struct task_struct *task, char *buffer)
478 struct mm_struct *mm = get_task_mm(task);
480 unsigned int nwords = 0;
483 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
484 res = nwords * sizeof(mm->saved_auxv[0]);
487 memcpy(buffer, mm->saved_auxv, res);
494 #ifdef CONFIG_KALLSYMS
496 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
497 * Returns the resolved symbol. If that fails, simply return the address.
499 static int proc_pid_wchan(struct task_struct *task, char *buffer)
502 const char *sym_name;
503 unsigned long wchan, size, offset;
504 char namebuf[KSYM_NAME_LEN+1];
506 wchan = get_wchan(task);
508 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
510 return sprintf(buffer, "%s", sym_name);
511 return sprintf(buffer, "%lu", wchan);
513 #endif /* CONFIG_KALLSYMS */
515 #ifdef CONFIG_SCHEDSTATS
517 * Provides /proc/PID/schedstat
519 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
521 return sprintf(buffer, "%lu %lu %lu\n",
522 task->sched_info.cpu_time,
523 task->sched_info.run_delay,
524 task->sched_info.pcnt);
528 /* The badness from the OOM killer */
529 unsigned long badness(struct task_struct *p, unsigned long uptime);
530 static int proc_oom_score(struct task_struct *task, char *buffer)
532 unsigned long points;
533 struct timespec uptime;
535 do_posix_clock_monotonic_gettime(&uptime);
536 points = badness(task, uptime.tv_sec);
537 return sprintf(buffer, "%lu\n", points);
540 /************************************************************************/
541 /* Here the fs part begins */
542 /************************************************************************/
544 /* permission checks */
546 /* If the process being read is separated by chroot from the reading process,
547 * don't let the reader access the threads.
549 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
551 struct dentry *de, *base;
552 struct vfsmount *our_vfsmnt, *mnt;
555 /* context admin override */
556 if (capable(CAP_CONTEXT))
559 read_lock(¤t->fs->lock);
560 our_vfsmnt = mntget(current->fs->rootmnt);
561 base = dget(current->fs->root);
562 read_unlock(¤t->fs->lock);
564 spin_lock(&vfsmount_lock);
568 while (mnt != our_vfsmnt) {
569 if (mnt == mnt->mnt_parent)
571 de = mnt->mnt_mountpoint;
572 mnt = mnt->mnt_parent;
575 if (!is_subdir(de, base))
577 spin_unlock(&vfsmount_lock);
587 spin_unlock(&vfsmount_lock);
592 static int proc_check_root(struct inode *inode)
595 struct vfsmount *vfsmnt;
597 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
599 return proc_check_chroot(root, vfsmnt);
602 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
604 if (generic_permission(inode, mask, NULL) != 0)
606 return proc_check_root(inode);
609 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
612 struct inode *inode = dentry->d_inode;
614 if (attr->ia_valid & ATTR_MODE)
617 error = inode_change_ok(inode, attr);
619 error = security_inode_setattr(dentry, attr);
621 error = inode_setattr(inode, attr);
626 static struct inode_operations proc_def_inode_operations = {
627 .setattr = proc_setattr,
630 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
633 struct vfsmount *vfsmnt;
635 if (generic_permission(inode, mask, NULL) != 0)
638 if (proc_task_root_link(inode, &root, &vfsmnt))
641 return proc_check_chroot(root, vfsmnt);
644 extern struct seq_operations proc_pid_maps_op;
645 static int maps_open(struct inode *inode, struct file *file)
647 struct task_struct *task = proc_task(inode);
648 int ret = seq_open(file, &proc_pid_maps_op);
650 struct seq_file *m = file->private_data;
656 static struct file_operations proc_maps_operations = {
660 .release = seq_release,
664 extern struct seq_operations proc_pid_numa_maps_op;
665 static int numa_maps_open(struct inode *inode, struct file *file)
667 struct task_struct *task = proc_task(inode);
668 int ret = seq_open(file, &proc_pid_numa_maps_op);
670 struct seq_file *m = file->private_data;
676 static struct file_operations proc_numa_maps_operations = {
677 .open = numa_maps_open,
680 .release = seq_release,
685 extern struct seq_operations proc_pid_smaps_op;
686 static int smaps_open(struct inode *inode, struct file *file)
688 struct task_struct *task = proc_task(inode);
689 int ret = seq_open(file, &proc_pid_smaps_op);
691 struct seq_file *m = file->private_data;
697 static struct file_operations proc_smaps_operations = {
701 .release = seq_release,
705 extern struct seq_operations mounts_op;
711 static int mounts_open(struct inode *inode, struct file *file)
713 struct task_struct *task = proc_task(inode);
714 struct namespace *namespace;
715 struct proc_mounts *p;
719 namespace = task->namespace;
721 get_namespace(namespace);
726 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
728 file->private_data = &p->m;
729 ret = seq_open(file, &mounts_op);
731 p->m.private = namespace;
732 p->event = namespace->event;
737 put_namespace(namespace);
742 static int mounts_release(struct inode *inode, struct file *file)
744 struct seq_file *m = file->private_data;
745 struct namespace *namespace = m->private;
746 put_namespace(namespace);
747 return seq_release(inode, file);
750 static unsigned mounts_poll(struct file *file, poll_table *wait)
752 struct proc_mounts *p = file->private_data;
753 struct namespace *ns = p->m.private;
756 poll_wait(file, &ns->poll, wait);
758 spin_lock(&vfsmount_lock);
759 if (p->event != ns->event) {
760 p->event = ns->event;
763 spin_unlock(&vfsmount_lock);
768 static struct file_operations proc_mounts_operations = {
772 .release = mounts_release,
776 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
778 static ssize_t proc_info_read(struct file * file, char __user * buf,
779 size_t count, loff_t *ppos)
781 struct inode * inode = file->f_dentry->d_inode;
784 struct task_struct *task = proc_task(inode);
786 if (count > PROC_BLOCK_SIZE)
787 count = PROC_BLOCK_SIZE;
788 if (!(page = __get_free_page(GFP_KERNEL)))
791 length = PROC_I(inode)->op.proc_read(task, (char*)page);
794 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
799 static struct file_operations proc_info_file_operations = {
800 .read = proc_info_read,
803 static int mem_open(struct inode* inode, struct file* file)
805 file->private_data = (void*)((long)current->self_exec_id);
809 static ssize_t mem_read(struct file * file, char __user * buf,
810 size_t count, loff_t *ppos)
812 struct task_struct *task = proc_task(file->f_dentry->d_inode);
814 unsigned long src = *ppos;
816 struct mm_struct *mm;
818 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
822 page = (char *)__get_free_page(GFP_USER);
828 mm = get_task_mm(task);
834 if (file->private_data != (void*)((long)current->self_exec_id))
840 int this_len, retval;
842 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
843 retval = access_process_vm(task, src, page, this_len, 0);
844 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
850 if (copy_to_user(buf, page, retval)) {
865 free_page((unsigned long) page);
870 #define mem_write NULL
873 /* This is a security hazard */
874 static ssize_t mem_write(struct file * file, const char * buf,
875 size_t count, loff_t *ppos)
879 struct task_struct *task = proc_task(file->f_dentry->d_inode);
880 unsigned long dst = *ppos;
882 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
885 page = (char *)__get_free_page(GFP_USER);
890 int this_len, retval;
892 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
893 if (copy_from_user(page, buf, this_len)) {
897 retval = access_process_vm(task, dst, page, this_len, 1);
909 free_page((unsigned long) page);
914 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
918 file->f_pos = offset;
921 file->f_pos += offset;
926 force_successful_syscall_return();
930 static struct file_operations proc_mem_operations = {
937 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
938 size_t count, loff_t *ppos)
940 struct task_struct *task = proc_task(file->f_dentry->d_inode);
943 int oom_adjust = task->oomkilladj;
944 loff_t __ppos = *ppos;
946 len = sprintf(buffer, "%i\n", oom_adjust);
949 if (count > len-__ppos)
951 if (copy_to_user(buf, buffer + __ppos, count))
953 *ppos = __ppos + count;
957 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
958 size_t count, loff_t *ppos)
960 struct task_struct *task = proc_task(file->f_dentry->d_inode);
961 char buffer[8], *end;
964 if (!capable(CAP_SYS_RESOURCE))
966 memset(buffer, 0, 8);
969 if (copy_from_user(buffer, buf, count))
971 oom_adjust = simple_strtol(buffer, &end, 0);
972 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
976 task->oomkilladj = oom_adjust;
977 if (end - buffer == 0)
982 static struct file_operations proc_oom_adjust_operations = {
983 .read = oom_adjust_read,
984 .write = oom_adjust_write,
987 static struct inode_operations proc_mem_inode_operations = {
988 .permission = proc_permission,
989 .setattr = proc_setattr,
992 #ifdef CONFIG_AUDITSYSCALL
994 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
995 size_t count, loff_t *ppos)
997 struct inode * inode = file->f_dentry->d_inode;
998 struct task_struct *task = proc_task(inode);
1000 char tmpbuf[TMPBUFLEN];
1002 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1003 audit_get_loginuid(task->audit_context));
1004 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1007 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1008 size_t count, loff_t *ppos)
1010 struct inode * inode = file->f_dentry->d_inode;
1013 struct task_struct *task = proc_task(inode);
1016 if (!capable(CAP_AUDIT_CONTROL))
1019 if (current != task)
1022 if (count > PAGE_SIZE)
1026 /* No partial writes. */
1029 page = (char*)__get_free_page(GFP_USER);
1033 if (copy_from_user(page, buf, count))
1036 loginuid = simple_strtoul(page, &tmp, 10);
1042 length = audit_set_loginuid(task, loginuid);
1043 if (likely(length == 0))
1047 free_page((unsigned long) page);
1051 static struct file_operations proc_loginuid_operations = {
1052 .read = proc_loginuid_read,
1053 .write = proc_loginuid_write,
1057 #ifdef CONFIG_SECCOMP
1058 static ssize_t seccomp_read(struct file *file, char __user *buf,
1059 size_t count, loff_t *ppos)
1061 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1063 loff_t __ppos = *ppos;
1066 /* no need to print the trailing zero, so use only len */
1067 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1070 if (count > len - __ppos)
1071 count = len - __ppos;
1072 if (copy_to_user(buf, __buf + __ppos, count))
1074 *ppos = __ppos + count;
1078 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1079 size_t count, loff_t *ppos)
1081 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1082 char __buf[20], *end;
1083 unsigned int seccomp_mode;
1085 /* can set it only once to be even more secure */
1086 if (unlikely(tsk->seccomp.mode))
1089 memset(__buf, 0, sizeof(__buf));
1090 count = min(count, sizeof(__buf) - 1);
1091 if (copy_from_user(__buf, buf, count))
1093 seccomp_mode = simple_strtoul(__buf, &end, 0);
1096 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1097 tsk->seccomp.mode = seccomp_mode;
1098 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1101 if (unlikely(!(end - __buf)))
1106 static struct file_operations proc_seccomp_operations = {
1107 .read = seccomp_read,
1108 .write = seccomp_write,
1110 #endif /* CONFIG_SECCOMP */
1112 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1114 struct inode *inode = dentry->d_inode;
1115 int error = -EACCES;
1117 /* We don't need a base pointer in the /proc filesystem */
1120 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1122 error = proc_check_root(inode);
1126 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1127 nd->last_type = LAST_BIND;
1129 return ERR_PTR(error);
1132 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1133 char __user *buffer, int buflen)
1135 struct inode * inode;
1136 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1142 inode = dentry->d_inode;
1143 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1144 len = PTR_ERR(path);
1147 len = tmp + PAGE_SIZE - 1 - path;
1151 if (copy_to_user(buffer, path, len))
1154 free_page((unsigned long)tmp);
1158 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1160 int error = -EACCES;
1161 struct inode *inode = dentry->d_inode;
1163 struct vfsmount *mnt = NULL;
1167 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1169 error = proc_check_root(inode);
1173 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1177 error = do_proc_readlink(de, mnt, buffer, buflen);
1185 static struct inode_operations proc_pid_link_inode_operations = {
1186 .readlink = proc_pid_readlink,
1187 .follow_link = proc_pid_follow_link,
1188 .setattr = proc_setattr,
1193 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1195 struct inode *inode = filp->f_dentry->d_inode;
1196 struct task_struct *p = proc_task(inode);
1197 unsigned int fd, tid, ino;
1200 struct files_struct * files;
1201 struct fdtable *fdt;
1212 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1216 ino = fake_ino(tid, PROC_TID_INO);
1217 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1221 files = get_files_struct(p);
1225 fdt = files_fdtable(files);
1226 for (fd = filp->f_pos-2;
1228 fd++, filp->f_pos++) {
1231 if (!fcheck_files(files, fd))
1239 buf[j] = '0' + (i % 10);
1243 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1244 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1251 put_files_struct(files);
1257 static int proc_pident_readdir(struct file *filp,
1258 void *dirent, filldir_t filldir,
1259 struct pid_entry *ents, unsigned int nents)
1263 struct dentry *dentry = filp->f_dentry;
1264 struct inode *inode = dentry->d_inode;
1265 struct pid_entry *p;
1270 if (!pid_alive(proc_task(inode)))
1274 pid = proc_task(inode)->pid;
1279 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1285 ino = parent_ino(dentry);
1286 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1298 hide = vx_flags(VXF_INFO_HIDE, 0);
1302 case PROC_TGID_VX_INFO:
1303 case PROC_TGID_IP_INFO:
1307 if (filldir(dirent, p->name, p->len, filp->f_pos,
1308 fake_ino(pid, p->type), p->mode >> 12) < 0)
1321 static int proc_tgid_base_readdir(struct file * filp,
1322 void * dirent, filldir_t filldir)
1324 return proc_pident_readdir(filp,dirent,filldir,
1325 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1328 static int proc_tid_base_readdir(struct file * filp,
1329 void * dirent, filldir_t filldir)
1331 return proc_pident_readdir(filp,dirent,filldir,
1332 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1335 /* building an inode */
1337 static int task_dumpable(struct task_struct *task)
1340 struct mm_struct *mm;
1345 dumpable = mm->dumpable;
1353 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1355 struct inode * inode;
1356 struct proc_inode *ei;
1358 /* We need a new inode */
1360 inode = new_inode(sb);
1367 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1368 inode->i_ino = fake_ino(task->pid, ino);
1369 inode->i_op = &proc_def_inode_operations;
1371 if (!pid_alive(task))
1375 * grab the reference to task.
1377 get_task_struct(task);
1382 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1383 inode->i_uid = task->euid;
1384 inode->i_gid = task->egid;
1386 inode->i_xid = vx_task_xid(task);
1387 security_task_to_inode(task, inode);
1401 * Exceptional case: normally we are not allowed to unhash a busy
1402 * directory. In this case, however, we can do it - no aliasing problems
1403 * due to the way we treat inodes.
1405 * Rewrite the inode's ownerships here because the owning task may have
1406 * performed a setuid(), etc.
1408 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1410 struct inode *inode = dentry->d_inode;
1411 struct task_struct *task = proc_task(inode);
1413 if (!vx_check(vx_task_xid(task), VX_IDENT))
1415 /* discard wrong fakeinit */
1417 if (pid_alive(task)) {
1418 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1419 inode->i_uid = task->euid;
1420 inode->i_gid = task->egid;
1425 inode->i_mode &= ~(S_ISUID | S_ISGID);
1426 security_task_to_inode(task, inode);
1434 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1436 struct inode *inode = dentry->d_inode;
1437 struct task_struct *task = proc_task(inode);
1438 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1439 struct files_struct *files;
1441 files = get_files_struct(task);
1444 if (fcheck_files(files, fd)) {
1446 put_files_struct(files);
1447 if (task_dumpable(task)) {
1448 inode->i_uid = task->euid;
1449 inode->i_gid = task->egid;
1454 inode->i_mode &= ~(S_ISUID | S_ISGID);
1455 security_task_to_inode(task, inode);
1459 put_files_struct(files);
1465 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1467 struct task_struct *task = proc_task(inode);
1468 spin_lock(&task->proc_lock);
1469 if (task->proc_dentry == dentry)
1470 task->proc_dentry = NULL;
1471 spin_unlock(&task->proc_lock);
1475 static int pid_delete_dentry(struct dentry * dentry)
1477 /* Is the task we represent dead?
1478 * If so, then don't put the dentry on the lru list,
1479 * kill it immediately.
1481 return !pid_alive(proc_task(dentry->d_inode));
1484 static struct dentry_operations tid_fd_dentry_operations =
1486 .d_revalidate = tid_fd_revalidate,
1487 .d_delete = pid_delete_dentry,
1490 static struct dentry_operations pid_dentry_operations =
1492 .d_revalidate = pid_revalidate,
1493 .d_delete = pid_delete_dentry,
1496 static struct dentry_operations pid_base_dentry_operations =
1498 .d_revalidate = pid_revalidate,
1499 .d_iput = pid_base_iput,
1500 .d_delete = pid_delete_dentry,
1505 static unsigned name_to_int(struct dentry *dentry)
1507 const char *name = dentry->d_name.name;
1508 int len = dentry->d_name.len;
1511 if (len > 1 && *name == '0')
1514 unsigned c = *name++ - '0';
1517 if (n >= (~0U-9)/10)
1528 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1530 struct task_struct *task = proc_task(dir);
1531 unsigned fd = name_to_int(dentry);
1533 struct files_struct * files;
1534 struct inode *inode;
1535 struct proc_inode *ei;
1539 if (!pid_alive(task))
1542 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1546 files = get_files_struct(task);
1549 inode->i_mode = S_IFLNK;
1552 * We are not taking a ref to the file structure, so we must
1555 spin_lock(&files->file_lock);
1556 file = fcheck_files(files, fd);
1559 if (file->f_mode & 1)
1560 inode->i_mode |= S_IRUSR | S_IXUSR;
1561 if (file->f_mode & 2)
1562 inode->i_mode |= S_IWUSR | S_IXUSR;
1563 spin_unlock(&files->file_lock);
1564 put_files_struct(files);
1565 inode->i_op = &proc_pid_link_inode_operations;
1567 ei->op.proc_get_link = proc_fd_link;
1568 dentry->d_op = &tid_fd_dentry_operations;
1569 d_add(dentry, inode);
1573 spin_unlock(&files->file_lock);
1574 put_files_struct(files);
1578 return ERR_PTR(-ENOENT);
1581 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1582 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1584 static struct file_operations proc_fd_operations = {
1585 .read = generic_read_dir,
1586 .readdir = proc_readfd,
1589 static struct file_operations proc_task_operations = {
1590 .read = generic_read_dir,
1591 .readdir = proc_task_readdir,
1595 * proc directories can do almost nothing..
1597 static struct inode_operations proc_fd_inode_operations = {
1598 .lookup = proc_lookupfd,
1599 .permission = proc_permission,
1600 .setattr = proc_setattr,
1603 static struct inode_operations proc_task_inode_operations = {
1604 .lookup = proc_task_lookup,
1605 .permission = proc_task_permission,
1606 .setattr = proc_setattr,
1609 #ifdef CONFIG_SECURITY
1610 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1611 size_t count, loff_t *ppos)
1613 struct inode * inode = file->f_dentry->d_inode;
1616 struct task_struct *task = proc_task(inode);
1618 if (count > PAGE_SIZE)
1620 if (!(page = __get_free_page(GFP_KERNEL)))
1623 length = security_getprocattr(task,
1624 (char*)file->f_dentry->d_name.name,
1625 (void*)page, count);
1627 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1632 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1633 size_t count, loff_t *ppos)
1635 struct inode * inode = file->f_dentry->d_inode;
1638 struct task_struct *task = proc_task(inode);
1640 if (count > PAGE_SIZE)
1643 /* No partial writes. */
1646 page = (char*)__get_free_page(GFP_USER);
1650 if (copy_from_user(page, buf, count))
1653 length = security_setprocattr(task,
1654 (char*)file->f_dentry->d_name.name,
1655 (void*)page, count);
1657 free_page((unsigned long) page);
1661 static struct file_operations proc_pid_attr_operations = {
1662 .read = proc_pid_attr_read,
1663 .write = proc_pid_attr_write,
1666 static struct file_operations proc_tid_attr_operations;
1667 static struct inode_operations proc_tid_attr_inode_operations;
1668 static struct file_operations proc_tgid_attr_operations;
1669 static struct inode_operations proc_tgid_attr_inode_operations;
1672 extern int proc_pid_vx_info(struct task_struct *, char *);
1673 extern int proc_pid_nx_info(struct task_struct *, char *);
1675 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1678 static struct dentry *proc_pident_lookup(struct inode *dir,
1679 struct dentry *dentry,
1680 struct pid_entry *ents)
1682 struct inode *inode;
1684 struct task_struct *task = proc_task(dir);
1685 struct pid_entry *p;
1686 struct proc_inode *ei;
1691 if (!pid_alive(task))
1694 for (p = ents; p->name; p++) {
1695 if (p->len != dentry->d_name.len)
1697 if (!memcmp(dentry->d_name.name, p->name, p->len))
1704 case PROC_TID_VX_INFO:
1705 case PROC_TGID_VX_INFO:
1706 case PROC_TID_IP_INFO:
1707 case PROC_TGID_IP_INFO:
1708 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
1715 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1720 inode->i_mode = p->mode;
1722 * Yes, it does not scale. And it should not. Don't add
1723 * new entries into /proc/<tgid>/ without very good reasons.
1726 case PROC_TGID_TASK:
1727 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1728 inode->i_op = &proc_task_inode_operations;
1729 inode->i_fop = &proc_task_operations;
1734 inode->i_op = &proc_fd_inode_operations;
1735 inode->i_fop = &proc_fd_operations;
1739 inode->i_op = &proc_pid_link_inode_operations;
1740 ei->op.proc_get_link = proc_exe_link;
1744 inode->i_op = &proc_pid_link_inode_operations;
1745 ei->op.proc_get_link = proc_cwd_link;
1748 case PROC_TGID_ROOT:
1749 inode->i_op = &proc_pid_link_inode_operations;
1750 ei->op.proc_get_link = proc_root_link;
1752 case PROC_TID_ENVIRON:
1753 case PROC_TGID_ENVIRON:
1754 inode->i_fop = &proc_info_file_operations;
1755 ei->op.proc_read = proc_pid_environ;
1758 case PROC_TGID_AUXV:
1759 inode->i_fop = &proc_info_file_operations;
1760 ei->op.proc_read = proc_pid_auxv;
1762 case PROC_TID_STATUS:
1763 case PROC_TGID_STATUS:
1764 inode->i_fop = &proc_info_file_operations;
1765 ei->op.proc_read = proc_pid_status;
1768 inode->i_fop = &proc_info_file_operations;
1769 ei->op.proc_read = proc_tid_stat;
1771 case PROC_TGID_STAT:
1772 inode->i_fop = &proc_info_file_operations;
1773 ei->op.proc_read = proc_tgid_stat;
1775 case PROC_TID_CMDLINE:
1776 case PROC_TGID_CMDLINE:
1777 inode->i_fop = &proc_info_file_operations;
1778 ei->op.proc_read = proc_pid_cmdline;
1780 case PROC_TID_STATM:
1781 case PROC_TGID_STATM:
1782 inode->i_fop = &proc_info_file_operations;
1783 ei->op.proc_read = proc_pid_statm;
1786 case PROC_TGID_MAPS:
1787 inode->i_fop = &proc_maps_operations;
1790 case PROC_TID_NUMA_MAPS:
1791 case PROC_TGID_NUMA_MAPS:
1792 inode->i_fop = &proc_numa_maps_operations;
1797 inode->i_op = &proc_mem_inode_operations;
1798 inode->i_fop = &proc_mem_operations;
1800 #ifdef CONFIG_SECCOMP
1801 case PROC_TID_SECCOMP:
1802 case PROC_TGID_SECCOMP:
1803 inode->i_fop = &proc_seccomp_operations;
1805 #endif /* CONFIG_SECCOMP */
1806 case PROC_TID_MOUNTS:
1807 case PROC_TGID_MOUNTS:
1808 inode->i_fop = &proc_mounts_operations;
1811 case PROC_TID_SMAPS:
1812 case PROC_TGID_SMAPS:
1813 inode->i_fop = &proc_smaps_operations;
1816 #ifdef CONFIG_SECURITY
1819 inode->i_op = &proc_tid_attr_inode_operations;
1820 inode->i_fop = &proc_tid_attr_operations;
1822 case PROC_TGID_ATTR:
1824 inode->i_op = &proc_tgid_attr_inode_operations;
1825 inode->i_fop = &proc_tgid_attr_operations;
1827 case PROC_TID_ATTR_CURRENT:
1828 case PROC_TGID_ATTR_CURRENT:
1829 case PROC_TID_ATTR_PREV:
1830 case PROC_TGID_ATTR_PREV:
1831 case PROC_TID_ATTR_EXEC:
1832 case PROC_TGID_ATTR_EXEC:
1833 case PROC_TID_ATTR_FSCREATE:
1834 case PROC_TGID_ATTR_FSCREATE:
1835 inode->i_fop = &proc_pid_attr_operations;
1838 #ifdef CONFIG_KALLSYMS
1839 case PROC_TID_WCHAN:
1840 case PROC_TGID_WCHAN:
1841 inode->i_fop = &proc_info_file_operations;
1842 ei->op.proc_read = proc_pid_wchan;
1845 #ifdef CONFIG_SCHEDSTATS
1846 case PROC_TID_SCHEDSTAT:
1847 case PROC_TGID_SCHEDSTAT:
1848 inode->i_fop = &proc_info_file_operations;
1849 ei->op.proc_read = proc_pid_schedstat;
1852 #ifdef CONFIG_CPUSETS
1853 case PROC_TID_CPUSET:
1854 case PROC_TGID_CPUSET:
1855 inode->i_fop = &proc_cpuset_operations;
1858 case PROC_TID_OOM_SCORE:
1859 case PROC_TGID_OOM_SCORE:
1860 inode->i_fop = &proc_info_file_operations;
1861 ei->op.proc_read = proc_oom_score;
1863 case PROC_TID_OOM_ADJUST:
1864 case PROC_TGID_OOM_ADJUST:
1865 inode->i_fop = &proc_oom_adjust_operations;
1867 #ifdef CONFIG_AUDITSYSCALL
1868 case PROC_TID_LOGINUID:
1869 case PROC_TGID_LOGINUID:
1870 inode->i_fop = &proc_loginuid_operations;
1873 case PROC_TID_VX_INFO:
1874 case PROC_TGID_VX_INFO:
1875 inode->i_fop = &proc_info_file_operations;
1876 ei->op.proc_read = proc_pid_vx_info;
1878 case PROC_TID_IP_INFO:
1879 case PROC_TGID_IP_INFO:
1880 inode->i_fop = &proc_info_file_operations;
1881 ei->op.proc_read = proc_pid_nx_info;
1884 printk("procfs: impossible type (%d)",p->type);
1888 dentry->d_op = &pid_dentry_operations;
1889 d_add(dentry, inode);
1895 return ERR_PTR(error);
1898 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1899 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1902 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1903 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1906 static struct file_operations proc_tgid_base_operations = {
1907 .read = generic_read_dir,
1908 .readdir = proc_tgid_base_readdir,
1911 static struct file_operations proc_tid_base_operations = {
1912 .read = generic_read_dir,
1913 .readdir = proc_tid_base_readdir,
1916 static struct inode_operations proc_tgid_base_inode_operations = {
1917 .lookup = proc_tgid_base_lookup,
1918 .setattr = proc_setattr,
1921 static struct inode_operations proc_tid_base_inode_operations = {
1922 .lookup = proc_tid_base_lookup,
1923 .setattr = proc_setattr,
1926 #ifdef CONFIG_SECURITY
1927 static int proc_tgid_attr_readdir(struct file * filp,
1928 void * dirent, filldir_t filldir)
1930 return proc_pident_readdir(filp,dirent,filldir,
1931 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1934 static int proc_tid_attr_readdir(struct file * filp,
1935 void * dirent, filldir_t filldir)
1937 return proc_pident_readdir(filp,dirent,filldir,
1938 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1941 static struct file_operations proc_tgid_attr_operations = {
1942 .read = generic_read_dir,
1943 .readdir = proc_tgid_attr_readdir,
1946 static struct file_operations proc_tid_attr_operations = {
1947 .read = generic_read_dir,
1948 .readdir = proc_tid_attr_readdir,
1951 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1952 struct dentry *dentry, struct nameidata *nd)
1954 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1957 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1958 struct dentry *dentry, struct nameidata *nd)
1960 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1963 static struct inode_operations proc_tgid_attr_inode_operations = {
1964 .lookup = proc_tgid_attr_lookup,
1965 .setattr = proc_setattr,
1968 static struct inode_operations proc_tid_attr_inode_operations = {
1969 .lookup = proc_tid_attr_lookup,
1970 .setattr = proc_setattr,
1977 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1981 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1982 return vfs_readlink(dentry,buffer,buflen,tmp);
1985 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1988 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1989 return ERR_PTR(vfs_follow_link(nd,tmp));
1992 static struct inode_operations proc_self_inode_operations = {
1993 .readlink = proc_self_readlink,
1994 .follow_link = proc_self_follow_link,
1995 .setattr = proc_setattr,
1999 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
2000 * @p: task that should be flushed.
2002 * Drops the /proc/@pid dcache entry from the hash chains.
2004 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
2005 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
2006 * if the pid value is immediately reused. This is enforced by
2007 * - caller must acquire spin_lock(p->proc_lock)
2008 * - must be called before detach_pid()
2009 * - proc_pid_lookup acquires proc_lock, and checks that
2010 * the target is not dead by looking at the attach count
2014 struct dentry *proc_pid_unhash(struct task_struct *p)
2016 struct dentry *proc_dentry;
2018 proc_dentry = p->proc_dentry;
2019 if (proc_dentry != NULL) {
2021 spin_lock(&dcache_lock);
2022 spin_lock(&proc_dentry->d_lock);
2023 if (!d_unhashed(proc_dentry)) {
2024 dget_locked(proc_dentry);
2025 __d_drop(proc_dentry);
2026 spin_unlock(&proc_dentry->d_lock);
2028 spin_unlock(&proc_dentry->d_lock);
2031 spin_unlock(&dcache_lock);
2037 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
2038 * @proc_dentry: directoy to prune.
2040 * Shrink the /proc directory that was used by the just killed thread.
2043 void proc_pid_flush(struct dentry *proc_dentry)
2046 if(proc_dentry != NULL) {
2047 shrink_dcache_parent(proc_dentry);
2052 #define VXF_FAKE_INIT (VXF_INFO_INIT|VXF_STATE_INIT)
2054 static inline int proc_pid_visible(struct task_struct *task, int pid)
2057 !vx_flags(VXF_FAKE_INIT, VXF_FAKE_INIT))
2059 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
2067 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2069 struct task_struct *task;
2070 struct inode *inode;
2071 struct proc_inode *ei;
2075 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2076 inode = new_inode(dir->i_sb);
2078 return ERR_PTR(-ENOMEM);
2080 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2081 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2083 inode->i_mode = S_IFLNK|S_IRWXUGO;
2084 inode->i_uid = inode->i_gid = 0;
2086 inode->i_op = &proc_self_inode_operations;
2087 d_add(dentry, inode);
2090 tgid = name_to_int(dentry);
2094 read_lock(&tasklist_lock);
2095 task = find_task_by_pid(tgid);
2097 get_task_struct(task);
2098 read_unlock(&tasklist_lock);
2102 /* check for context visibility */
2103 if (!proc_pid_visible(task, tgid))
2106 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2110 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2111 inode->i_op = &proc_tgid_base_inode_operations;
2112 inode->i_fop = &proc_tgid_base_operations;
2113 inode->i_flags|=S_IMMUTABLE;
2114 #ifdef CONFIG_SECURITY
2120 dentry->d_op = &pid_base_dentry_operations;
2123 d_add(dentry, inode);
2124 spin_lock(&task->proc_lock);
2125 task->proc_dentry = dentry;
2126 if (!pid_alive(task)) {
2127 dentry = proc_pid_unhash(task);
2130 spin_unlock(&task->proc_lock);
2132 put_task_struct(task);
2134 proc_pid_flush(dentry);
2139 put_task_struct(task);
2141 return ERR_PTR(-ENOENT);
2145 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2147 struct task_struct *task;
2148 struct task_struct *leader = proc_task(dir);
2149 struct inode *inode;
2152 tid = name_to_int(dentry);
2155 if (vx_current_initpid(tid))
2158 read_lock(&tasklist_lock);
2159 task = find_task_by_pid(tid);
2161 get_task_struct(task);
2162 read_unlock(&tasklist_lock);
2165 if (leader->tgid != task->tgid)
2168 /* check for context visibility */
2169 if (!proc_pid_visible(task, tid))
2172 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2176 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2177 inode->i_op = &proc_tid_base_inode_operations;
2178 inode->i_fop = &proc_tid_base_operations;
2179 inode->i_flags|=S_IMMUTABLE;
2180 #ifdef CONFIG_SECURITY
2186 dentry->d_op = &pid_base_dentry_operations;
2188 d_add(dentry, inode);
2190 put_task_struct(task);
2193 put_task_struct(task);
2195 return ERR_PTR(-ENOENT);
2198 #define PROC_NUMBUF 10
2199 #define PROC_MAXPIDS 20
2202 * Get a few tgid's to return for filldir - we need to hold the
2203 * tasklist lock while doing this, and we must release it before
2204 * we actually do the filldir itself, so we use a temp buffer..
2206 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2208 struct task_struct *p;
2212 read_lock(&tasklist_lock);
2215 p = find_task_by_real_pid(version);
2216 if (p && !thread_group_leader(p))
2223 p = next_task(&init_task);
2225 for ( ; p != &init_task; p = next_task(p)) {
2230 /* check for context visibility */
2231 if (!proc_pid_visible(p, tgid))
2235 tgids[nr_tgids] = vx_map_tgid(tgid);
2237 if (nr_tgids >= PROC_MAXPIDS)
2240 read_unlock(&tasklist_lock);
2245 * Get a few tid's to return for filldir - we need to hold the
2246 * tasklist lock while doing this, and we must release it before
2247 * we actually do the filldir itself, so we use a temp buffer..
2249 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2251 struct task_struct *leader_task = proc_task(dir);
2252 struct task_struct *task = leader_task;
2256 read_lock(&tasklist_lock);
2258 * The starting point task (leader_task) might be an already
2259 * unlinked task, which cannot be used to access the task-list
2260 * via next_thread().
2262 if (pid_alive(task)) do {
2263 int tid = task->pid;
2265 /* check for context visibility */
2266 if (!proc_pid_visible(task, tid))
2271 tids[nr_tids] = vx_map_pid(tid);
2273 if (nr_tids >= PROC_MAXPIDS)
2275 } while ((task = next_thread(task)) != leader_task);
2276 read_unlock(&tasklist_lock);
2280 /* for the /proc/ directory itself, after non-process stuff has been done */
2281 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2283 unsigned int tgid_array[PROC_MAXPIDS];
2284 char buf[PROC_NUMBUF];
2285 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2286 unsigned int nr_tgids, i;
2290 ino_t ino = fake_ino(0,PROC_TGID_INO);
2291 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2297 /* f_version caches the tgid value that the last readdir call couldn't
2298 * return. lseek aka telldir automagically resets f_version to 0.
2300 next_tgid = filp->f_version;
2301 filp->f_version = 0;
2303 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2305 /* no more entries ! */
2310 /* do not use the last found pid, reserve it for next_tgid */
2311 if (nr_tgids == PROC_MAXPIDS) {
2313 next_tgid = tgid_array[nr_tgids];
2316 for (i=0;i<nr_tgids;i++) {
2317 int tgid = tgid_array[i];
2318 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2319 unsigned long j = PROC_NUMBUF;
2322 buf[--j] = '0' + (tgid % 10);
2323 while ((tgid /= 10) != 0);
2325 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2326 /* returning this tgid failed, save it as the first
2327 * pid for the next readir call */
2328 filp->f_version = tgid_array[i];
2339 /* for the /proc/TGID/task/ directories */
2340 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2342 unsigned int tid_array[PROC_MAXPIDS];
2343 char buf[PROC_NUMBUF];
2344 unsigned int nr_tids, i;
2345 struct dentry *dentry = filp->f_dentry;
2346 struct inode *inode = dentry->d_inode;
2347 struct task_struct *task = proc_task(inode);
2348 int retval = -ENOENT;
2350 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2352 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
2354 if (!pid_alive(task))
2361 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2366 ino = parent_ino(dentry);
2367 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2373 nr_tids = get_tid_list(pos, tid_array, inode);
2374 inode->i_nlink = pos + nr_tids;
2376 for (i = 0; i < nr_tids; i++) {
2377 unsigned long j = PROC_NUMBUF;
2378 int tid = tid_array[i];
2380 ino = fake_ino(tid,PROC_TID_INO);
2383 buf[--j] = '0' + (tid % 10);
2384 while ((tid /= 10) != 0);
2386 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)