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/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/tracehook.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
79 * Implementing inode permission operations in /proc is almost
80 * certainly an error. Permission checks need to happen during
81 * each system call not at open time. The reason is that most of
82 * what we wish to check for permissions in /proc varies at runtime.
84 * The classic example of a problem is opening file descriptors
85 * in /proc for a task before it execs a suid executable.
89 * For hysterical raisins we keep the same inumbers as in the old procfs.
90 * Feel free to change the macro below - just keep the range distinct from
91 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
92 * As soon as we'll get a separate superblock we will be able to forget
93 * about magical ranges too.
96 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
98 enum pid_directory_inos {
103 #ifdef CONFIG_SECCOMP
118 PROC_TGID_MOUNTSTATS,
123 #ifdef CONFIG_SCHEDSTATS
126 #ifdef CONFIG_CPUSETS
129 #ifdef CONFIG_SECURITY
131 PROC_TGID_ATTR_CURRENT,
134 PROC_TGID_ATTR_FSCREATE,
135 PROC_TGID_ATTR_KEYCREATE,
136 PROC_TGID_ATTR_SOCKCREATE,
140 #ifdef CONFIG_AUDITSYSCALL
144 PROC_TGID_OOM_ADJUST,
148 #ifdef CONFIG_SECCOMP
168 #ifdef CONFIG_SCHEDSTATS
171 #ifdef CONFIG_CPUSETS
174 #ifdef CONFIG_SECURITY
176 PROC_TID_ATTR_CURRENT,
179 PROC_TID_ATTR_FSCREATE,
180 PROC_TID_ATTR_KEYCREATE,
181 PROC_TID_ATTR_SOCKCREATE,
185 #ifdef CONFIG_AUDITSYSCALL
191 /* Add new entries before this */
192 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
195 /* Worst case buffer size needed for holding an integer. */
196 #define PROC_NUMBUF 10
205 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
207 static struct pid_entry tgid_base_stuff[] = {
208 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
209 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
210 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
211 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
212 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
213 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
214 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
215 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
216 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
218 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
220 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
221 #ifdef CONFIG_SECCOMP
222 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
224 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
225 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
226 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
227 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
228 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
230 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUSR),
232 #ifdef CONFIG_SECURITY
233 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
235 #ifdef CONFIG_KALLSYMS
236 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
238 #ifdef CONFIG_SCHEDSTATS
239 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
241 #ifdef CONFIG_CPUSETS
242 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
244 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
245 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
246 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
247 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
248 #ifdef CONFIG_AUDITSYSCALL
249 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
253 static struct pid_entry tid_base_stuff[] = {
254 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
255 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
256 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
257 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
258 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
259 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
260 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
261 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
263 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
265 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
266 #ifdef CONFIG_SECCOMP
267 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
269 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
270 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
271 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
272 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
274 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUSR),
276 #ifdef CONFIG_SECURITY
277 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
279 #ifdef CONFIG_KALLSYMS
280 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
282 #ifdef CONFIG_SCHEDSTATS
283 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
285 #ifdef CONFIG_CPUSETS
286 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
288 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
289 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
290 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
291 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
292 #ifdef CONFIG_AUDITSYSCALL
293 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
298 #ifdef CONFIG_SECURITY
299 static struct pid_entry tgid_attr_stuff[] = {
300 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
301 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
302 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
303 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
304 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
305 E(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
308 static struct pid_entry tid_attr_stuff[] = {
309 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
310 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
311 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
312 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
313 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
314 E(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
321 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
323 struct task_struct *task = get_proc_task(inode);
324 struct files_struct *files = NULL;
326 int fd = proc_fd(inode);
329 files = get_files_struct(task);
330 put_task_struct(task);
334 * We are not taking a ref to the file structure, so we must
337 spin_lock(&files->file_lock);
338 file = fcheck_files(files, fd);
340 *mnt = mntget(file->f_vfsmnt);
341 *dentry = dget(file->f_dentry);
342 spin_unlock(&files->file_lock);
343 put_files_struct(files);
346 spin_unlock(&files->file_lock);
347 put_files_struct(files);
352 static struct fs_struct *get_fs_struct(struct task_struct *task)
354 struct fs_struct *fs;
358 atomic_inc(&fs->count);
363 static int get_nr_threads(struct task_struct *tsk)
365 /* Must be called with the rcu_read_lock held */
369 if (lock_task_sighand(tsk, &flags)) {
370 count = atomic_read(&tsk->signal->count);
371 unlock_task_sighand(tsk, &flags);
376 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
378 struct task_struct *task = get_proc_task(inode);
379 struct fs_struct *fs = NULL;
380 int result = -ENOENT;
383 fs = get_fs_struct(task);
384 put_task_struct(task);
387 read_lock(&fs->lock);
388 *mnt = mntget(fs->pwdmnt);
389 *dentry = dget(fs->pwd);
390 read_unlock(&fs->lock);
397 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
399 struct task_struct *task = get_proc_task(inode);
400 struct fs_struct *fs = NULL;
401 int result = -ENOENT;
404 fs = get_fs_struct(task);
405 put_task_struct(task);
408 read_lock(&fs->lock);
409 *mnt = mntget(fs->rootmnt);
410 *dentry = dget(fs->root);
411 read_unlock(&fs->lock);
418 struct mm_struct *mm_for_maps(struct task_struct *task)
420 struct mm_struct *mm = get_task_mm(task);
423 down_read(&mm->mmap_sem);
427 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
433 up_read(&mm->mmap_sem);
438 static int proc_pid_environ(struct task_struct *task, char * buffer)
441 struct mm_struct *mm = get_task_mm(task);
443 unsigned int len = mm->env_end - mm->env_start;
446 res = access_process_vm(task, mm->env_start, buffer, len, 0);
447 if (!ptrace_may_attach(task))
454 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
458 struct mm_struct *mm = get_task_mm(task);
462 goto out_mm; /* Shh! No looking before we're done */
464 len = mm->arg_end - mm->arg_start;
469 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
471 // If the nul at the end of args has been overwritten, then
472 // assume application is using setproctitle(3).
473 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
474 len = strnlen(buffer, res);
478 len = mm->env_end - mm->env_start;
479 if (len > PAGE_SIZE - res)
480 len = PAGE_SIZE - res;
481 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
482 res = strnlen(buffer, res);
491 static int proc_pid_auxv(struct task_struct *task, char *buffer)
494 struct mm_struct *mm = get_task_mm(task);
496 unsigned int nwords = 0;
499 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
500 res = nwords * sizeof(mm->saved_auxv[0]);
503 memcpy(buffer, mm->saved_auxv, res);
510 #ifdef CONFIG_KALLSYMS
512 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
513 * Returns the resolved symbol. If that fails, simply return the address.
515 static int proc_pid_wchan(struct task_struct *task, char *buffer)
518 const char *sym_name;
519 unsigned long wchan, size, offset;
520 char namebuf[KSYM_NAME_LEN+1];
522 wchan = get_wchan(task);
524 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
526 return sprintf(buffer, "%s", sym_name);
527 return sprintf(buffer, "%lu", wchan);
529 #endif /* CONFIG_KALLSYMS */
531 #ifdef CONFIG_SCHEDSTATS
533 * Provides /proc/PID/schedstat
535 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
537 return sprintf(buffer, "%lu %lu %lu\n",
538 task->sched_info.cpu_time,
539 task->sched_info.run_delay,
540 task->sched_info.pcnt);
544 /* The badness from the OOM killer */
545 unsigned long badness(struct task_struct *p, unsigned long uptime);
546 static int proc_oom_score(struct task_struct *task, char *buffer)
548 unsigned long points;
549 struct timespec uptime;
551 do_posix_clock_monotonic_gettime(&uptime);
552 points = badness(task, uptime.tv_sec);
553 return sprintf(buffer, "%lu\n", points);
556 /************************************************************************/
557 /* Here the fs part begins */
558 /************************************************************************/
560 /* permission checks */
561 static int proc_fd_access_allowed(struct inode *inode)
563 struct task_struct *task;
565 /* Allow access to a task's file descriptors if it is us or we
566 * may use ptrace attach to the process and find out that
569 task = get_proc_task(inode);
571 allowed = ptrace_may_attach(task);
572 put_task_struct(task);
577 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
580 struct inode *inode = dentry->d_inode;
582 if (attr->ia_valid & ATTR_MODE)
585 error = inode_change_ok(inode, attr);
587 error = security_inode_setattr(dentry, attr);
589 error = inode_setattr(inode, attr);
594 static struct inode_operations proc_def_inode_operations = {
595 .setattr = proc_setattr,
598 extern struct seq_operations mounts_op;
604 static int mounts_open(struct inode *inode, struct file *file)
606 struct task_struct *task = get_proc_task(inode);
607 struct namespace *namespace = NULL;
608 struct proc_mounts *p;
613 namespace = task->namespace;
615 get_namespace(namespace);
617 put_task_struct(task);
622 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
624 file->private_data = &p->m;
625 ret = seq_open(file, &mounts_op);
627 p->m.private = namespace;
628 p->event = namespace->event;
633 put_namespace(namespace);
638 static int mounts_release(struct inode *inode, struct file *file)
640 struct seq_file *m = file->private_data;
641 struct namespace *namespace = m->private;
642 put_namespace(namespace);
643 return seq_release(inode, file);
646 static unsigned mounts_poll(struct file *file, poll_table *wait)
648 struct proc_mounts *p = file->private_data;
649 struct namespace *ns = p->m.private;
652 poll_wait(file, &ns->poll, wait);
654 spin_lock(&vfsmount_lock);
655 if (p->event != ns->event) {
656 p->event = ns->event;
659 spin_unlock(&vfsmount_lock);
664 static struct file_operations proc_mounts_operations = {
668 .release = mounts_release,
672 extern struct seq_operations mountstats_op;
673 static int mountstats_open(struct inode *inode, struct file *file)
675 int ret = seq_open(file, &mountstats_op);
678 struct seq_file *m = file->private_data;
679 struct namespace *namespace = NULL;
680 struct task_struct *task = get_proc_task(inode);
684 namespace = task->namespace;
686 get_namespace(namespace);
688 put_task_struct(task);
692 m->private = namespace;
694 seq_release(inode, file);
701 static struct file_operations proc_mountstats_operations = {
702 .open = mountstats_open,
705 .release = mounts_release,
708 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
710 static ssize_t proc_info_read(struct file * file, char __user * buf,
711 size_t count, loff_t *ppos)
713 struct inode * inode = file->f_dentry->d_inode;
716 struct task_struct *task = get_proc_task(inode);
722 if (count > PROC_BLOCK_SIZE)
723 count = PROC_BLOCK_SIZE;
726 if (!(page = __get_free_page(GFP_KERNEL)))
729 length = PROC_I(inode)->op.proc_read(task, (char*)page);
732 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
735 put_task_struct(task);
740 static struct file_operations proc_info_file_operations = {
741 .read = proc_info_read,
744 static int mem_open(struct inode* inode, struct file* file)
746 file->private_data = (void*)((long)current->self_exec_id);
750 static ssize_t mem_read(struct file * file, char __user * buf,
751 size_t count, loff_t *ppos)
753 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
755 unsigned long src = *ppos;
757 struct mm_struct *mm;
762 if (!tracehook_allow_access_process_vm(task)
763 || !ptrace_may_attach(task))
767 page = (char *)__get_free_page(GFP_USER);
773 mm = get_task_mm(task);
779 if (file->private_data != (void*)((long)current->self_exec_id))
785 int this_len, retval;
787 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
788 retval = access_process_vm(task, src, page, this_len, 0);
789 if (!retval || !tracehook_allow_access_process_vm(task)
790 || !ptrace_may_attach(task)) {
796 if (copy_to_user(buf, page, retval)) {
811 free_page((unsigned long) page);
813 put_task_struct(task);
818 #define mem_write NULL
821 /* This is a security hazard */
822 static ssize_t mem_write(struct file * file, const char * buf,
823 size_t count, loff_t *ppos)
827 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
828 unsigned long dst = *ppos;
834 if (!tracehook_allow_access_process_vm(task)
835 || !ptrace_may_attach(task))
839 page = (char *)__get_free_page(GFP_USER);
844 int this_len, retval;
846 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
847 if (copy_from_user(page, buf, this_len)) {
851 retval = access_process_vm(task, dst, page, this_len, 1);
863 free_page((unsigned long) page);
865 put_task_struct(task);
871 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
875 file->f_pos = offset;
878 file->f_pos += offset;
883 force_successful_syscall_return();
887 static struct file_operations proc_mem_operations = {
894 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
895 size_t count, loff_t *ppos)
897 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
898 char buffer[PROC_NUMBUF];
901 loff_t __ppos = *ppos;
905 oom_adjust = task->oomkilladj;
906 put_task_struct(task);
908 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
911 if (count > len-__ppos)
913 if (copy_to_user(buf, buffer + __ppos, count))
915 *ppos = __ppos + count;
919 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
920 size_t count, loff_t *ppos)
922 struct task_struct *task;
923 char buffer[PROC_NUMBUF], *end;
926 if (!capable(CAP_SYS_RESOURCE))
928 memset(buffer, 0, sizeof(buffer));
929 if (count > sizeof(buffer) - 1)
930 count = sizeof(buffer) - 1;
931 if (copy_from_user(buffer, buf, count))
933 oom_adjust = simple_strtol(buffer, &end, 0);
934 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
938 task = get_proc_task(file->f_dentry->d_inode);
941 task->oomkilladj = oom_adjust;
942 put_task_struct(task);
943 if (end - buffer == 0)
948 static struct file_operations proc_oom_adjust_operations = {
949 .read = oom_adjust_read,
950 .write = oom_adjust_write,
953 #ifdef CONFIG_AUDITSYSCALL
955 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
956 size_t count, loff_t *ppos)
958 struct inode * inode = file->f_dentry->d_inode;
959 struct task_struct *task = get_proc_task(inode);
961 char tmpbuf[TMPBUFLEN];
965 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
966 audit_get_loginuid(task->audit_context));
967 put_task_struct(task);
968 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
971 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
972 size_t count, loff_t *ppos)
974 struct inode * inode = file->f_dentry->d_inode;
979 if (!capable(CAP_AUDIT_CONTROL))
982 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
985 if (count >= PAGE_SIZE)
986 count = PAGE_SIZE - 1;
989 /* No partial writes. */
992 page = (char*)__get_free_page(GFP_USER);
996 if (copy_from_user(page, buf, count))
1000 loginuid = simple_strtoul(page, &tmp, 10);
1006 length = audit_set_loginuid(current, loginuid);
1007 if (likely(length == 0))
1011 free_page((unsigned long) page);
1015 static struct file_operations proc_loginuid_operations = {
1016 .read = proc_loginuid_read,
1017 .write = proc_loginuid_write,
1021 #ifdef CONFIG_SECCOMP
1022 static ssize_t seccomp_read(struct file *file, char __user *buf,
1023 size_t count, loff_t *ppos)
1025 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1027 loff_t __ppos = *ppos;
1032 /* no need to print the trailing zero, so use only len */
1033 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1034 put_task_struct(tsk);
1037 if (count > len - __ppos)
1038 count = len - __ppos;
1039 if (copy_to_user(buf, __buf + __ppos, count))
1041 *ppos = __ppos + count;
1045 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1046 size_t count, loff_t *ppos)
1048 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1049 char __buf[20], *end;
1050 unsigned int seccomp_mode;
1057 /* can set it only once to be even more secure */
1059 if (unlikely(tsk->seccomp.mode))
1063 memset(__buf, 0, sizeof(__buf));
1064 count = min(count, sizeof(__buf) - 1);
1065 if (copy_from_user(__buf, buf, count))
1068 seccomp_mode = simple_strtoul(__buf, &end, 0);
1072 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1073 tsk->seccomp.mode = seccomp_mode;
1074 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1078 if (unlikely(!(end - __buf)))
1080 result = end - __buf;
1082 put_task_struct(tsk);
1087 static struct file_operations proc_seccomp_operations = {
1088 .read = seccomp_read,
1089 .write = seccomp_write,
1091 #endif /* CONFIG_SECCOMP */
1093 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1095 struct inode *inode = dentry->d_inode;
1096 int error = -EACCES;
1098 /* We don't need a base pointer in the /proc filesystem */
1101 /* Are we allowed to snoop on the tasks file descriptors? */
1102 if (!proc_fd_access_allowed(inode))
1105 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1106 nd->last_type = LAST_BIND;
1108 return ERR_PTR(error);
1111 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1112 char __user *buffer, int buflen)
1114 struct inode * inode;
1115 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1121 inode = dentry->d_inode;
1122 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1123 len = PTR_ERR(path);
1126 len = tmp + PAGE_SIZE - 1 - path;
1130 if (copy_to_user(buffer, path, len))
1133 free_page((unsigned long)tmp);
1137 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1139 int error = -EACCES;
1140 struct inode *inode = dentry->d_inode;
1142 struct vfsmount *mnt = NULL;
1144 /* Are we allowed to snoop on the tasks file descriptors? */
1145 if (!proc_fd_access_allowed(inode))
1148 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1152 error = do_proc_readlink(de, mnt, buffer, buflen);
1159 static struct inode_operations proc_pid_link_inode_operations = {
1160 .readlink = proc_pid_readlink,
1161 .follow_link = proc_pid_follow_link,
1162 .setattr = proc_setattr,
1165 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1167 struct dentry *dentry = filp->f_dentry;
1168 struct inode *inode = dentry->d_inode;
1169 struct task_struct *p = get_proc_task(inode);
1170 unsigned int fd, tid, ino;
1172 char buf[PROC_NUMBUF];
1173 struct files_struct * files;
1174 struct fdtable *fdt;
1185 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1189 ino = parent_ino(dentry);
1190 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1194 files = get_files_struct(p);
1198 fdt = files_fdtable(files);
1199 for (fd = filp->f_pos-2;
1201 fd++, filp->f_pos++) {
1204 if (!fcheck_files(files, fd))
1212 buf[j] = '0' + (i % 10);
1216 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1217 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1224 put_files_struct(files);
1232 static int proc_pident_readdir(struct file *filp,
1233 void *dirent, filldir_t filldir,
1234 struct pid_entry *ents, unsigned int nents)
1238 struct dentry *dentry = filp->f_dentry;
1239 struct inode *inode = dentry->d_inode;
1240 struct task_struct *task = get_proc_task(inode);
1241 struct pid_entry *p;
1251 put_task_struct(task);
1256 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1262 ino = parent_ino(dentry);
1263 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1275 hide = vx_flags(VXF_INFO_HIDE, 0);
1279 case PROC_TGID_VX_INFO:
1280 case PROC_TGID_IP_INFO:
1284 if (filldir(dirent, p->name, p->len, filp->f_pos,
1285 fake_ino(pid, p->type), p->mode >> 12) < 0)
1298 static int proc_tgid_base_readdir(struct file * filp,
1299 void * dirent, filldir_t filldir)
1301 return proc_pident_readdir(filp,dirent,filldir,
1302 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1305 static int proc_tid_base_readdir(struct file * filp,
1306 void * dirent, filldir_t filldir)
1308 return proc_pident_readdir(filp,dirent,filldir,
1309 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1312 /* building an inode */
1314 static int task_dumpable(struct task_struct *task)
1317 struct mm_struct *mm;
1322 dumpable = mm->dumpable;
1330 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1332 struct inode * inode;
1333 struct proc_inode *ei;
1335 /* We need a new inode */
1337 inode = new_inode(sb);
1343 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1344 inode->i_ino = fake_ino(task->pid, ino);
1345 inode->i_op = &proc_def_inode_operations;
1348 * grab the reference to task.
1350 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1356 if (task_dumpable(task)) {
1357 inode->i_uid = task->euid;
1358 inode->i_gid = task->egid;
1360 inode->i_xid = vx_task_xid(task);
1361 security_task_to_inode(task, inode);
1374 * Exceptional case: normally we are not allowed to unhash a busy
1375 * directory. In this case, however, we can do it - no aliasing problems
1376 * due to the way we treat inodes.
1378 * Rewrite the inode's ownerships here because the owning task may have
1379 * performed a setuid(), etc.
1381 * Before the /proc/pid/status file was created the only way to read
1382 * the effective uid of a /process was to stat /proc/pid. Reading
1383 * /proc/pid/status is slow enough that procps and other packages
1384 * kept stating /proc/pid. To keep the rules in /proc simple I have
1385 * made this apply to all per process world readable and executable
1388 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1390 struct inode *inode = dentry->d_inode;
1391 struct task_struct *task = get_proc_task(inode);
1395 int pid = (inode->i_ino >> 16) & 0xFFFF;
1397 if (!vx_proc_pid_visible(task, pid))
1401 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1402 task_dumpable(task)) {
1403 inode->i_uid = task->euid;
1404 inode->i_gid = task->egid;
1409 inode->i_mode &= ~(S_ISUID | S_ISGID);
1410 security_task_to_inode(task, inode);
1417 put_task_struct(task);
1421 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1423 struct inode *inode = dentry->d_inode;
1424 struct task_struct *task;
1425 generic_fillattr(inode, stat);
1430 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1432 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1433 task_dumpable(task)) {
1434 stat->uid = task->euid;
1435 stat->gid = task->egid;
1442 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1444 struct inode *inode = dentry->d_inode;
1445 struct task_struct *task = get_proc_task(inode);
1446 int fd = proc_fd(inode);
1447 struct files_struct *files;
1450 files = get_files_struct(task);
1453 if (fcheck_files(files, fd)) {
1455 put_files_struct(files);
1456 if (task_dumpable(task)) {
1457 inode->i_uid = task->euid;
1458 inode->i_gid = task->egid;
1463 inode->i_mode &= ~(S_ISUID | S_ISGID);
1464 security_task_to_inode(task, inode);
1465 put_task_struct(task);
1469 put_files_struct(files);
1471 put_task_struct(task);
1477 static int pid_delete_dentry(struct dentry * dentry)
1479 /* Is the task we represent dead?
1480 * If so, then don't put the dentry on the lru list,
1481 * kill it immediately.
1483 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1486 static struct dentry_operations tid_fd_dentry_operations =
1488 .d_revalidate = tid_fd_revalidate,
1489 .d_delete = pid_delete_dentry,
1492 static struct dentry_operations pid_dentry_operations =
1494 .d_revalidate = pid_revalidate,
1495 .d_delete = pid_delete_dentry,
1500 static unsigned name_to_int(struct dentry *dentry)
1502 const char *name = dentry->d_name.name;
1503 int len = dentry->d_name.len;
1506 if (len > 1 && *name == '0')
1509 unsigned c = *name++ - '0';
1512 if (n >= (~0U-9)/10)
1523 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1525 struct task_struct *task = get_proc_task(dir);
1526 unsigned fd = name_to_int(dentry);
1527 struct dentry *result = ERR_PTR(-ENOENT);
1529 struct files_struct * files;
1530 struct inode *inode;
1531 struct proc_inode *ei;
1538 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1543 files = get_files_struct(task);
1546 inode->i_mode = S_IFLNK;
1549 * We are not taking a ref to the file structure, so we must
1552 spin_lock(&files->file_lock);
1553 file = fcheck_files(files, fd);
1556 if (file->f_mode & 1)
1557 inode->i_mode |= S_IRUSR | S_IXUSR;
1558 if (file->f_mode & 2)
1559 inode->i_mode |= S_IWUSR | S_IXUSR;
1560 spin_unlock(&files->file_lock);
1561 put_files_struct(files);
1562 inode->i_op = &proc_pid_link_inode_operations;
1564 ei->op.proc_get_link = proc_fd_link;
1565 dentry->d_op = &tid_fd_dentry_operations;
1566 d_add(dentry, inode);
1567 /* Close the race of the process dying before we return the dentry */
1568 if (tid_fd_revalidate(dentry, NULL))
1571 put_task_struct(task);
1576 spin_unlock(&files->file_lock);
1577 put_files_struct(files);
1583 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1584 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1585 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
1587 static struct file_operations proc_fd_operations = {
1588 .read = generic_read_dir,
1589 .readdir = proc_readfd,
1592 static struct file_operations proc_task_operations = {
1593 .read = generic_read_dir,
1594 .readdir = proc_task_readdir,
1598 * proc directories can do almost nothing..
1600 static struct inode_operations proc_fd_inode_operations = {
1601 .lookup = proc_lookupfd,
1602 .setattr = proc_setattr,
1605 static struct inode_operations proc_task_inode_operations = {
1606 .lookup = proc_task_lookup,
1607 .getattr = proc_task_getattr,
1608 .setattr = proc_setattr,
1611 #ifdef CONFIG_SECURITY
1612 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1613 size_t count, loff_t *ppos)
1615 struct inode * inode = file->f_dentry->d_inode;
1618 struct task_struct *task = get_proc_task(inode);
1624 if (count > PAGE_SIZE)
1627 if (!(page = __get_free_page(GFP_KERNEL)))
1630 length = security_getprocattr(task,
1631 (char*)file->f_dentry->d_name.name,
1632 (void*)page, count);
1634 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1637 put_task_struct(task);
1642 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1643 size_t count, loff_t *ppos)
1645 struct inode * inode = file->f_dentry->d_inode;
1648 struct task_struct *task = get_proc_task(inode);
1653 if (count > PAGE_SIZE)
1656 /* No partial writes. */
1662 page = (char*)__get_free_page(GFP_USER);
1667 if (copy_from_user(page, buf, count))
1670 length = security_setprocattr(task,
1671 (char*)file->f_dentry->d_name.name,
1672 (void*)page, count);
1674 free_page((unsigned long) page);
1676 put_task_struct(task);
1681 static struct file_operations proc_pid_attr_operations = {
1682 .read = proc_pid_attr_read,
1683 .write = proc_pid_attr_write,
1686 static struct file_operations proc_tid_attr_operations;
1687 static struct inode_operations proc_tid_attr_inode_operations;
1688 static struct file_operations proc_tgid_attr_operations;
1689 static struct inode_operations proc_tgid_attr_inode_operations;
1692 extern int proc_pid_vx_info(struct task_struct *, char *);
1693 extern int proc_pid_nx_info(struct task_struct *, char *);
1695 extern int proc_pid_vx_info(struct task_struct *, char *);
1696 extern int proc_pid_nx_info(struct task_struct *, char *);
1699 static struct dentry *proc_pident_lookup(struct inode *dir,
1700 struct dentry *dentry,
1701 struct pid_entry *ents)
1703 struct inode *inode;
1704 struct dentry *error;
1705 struct task_struct *task = get_proc_task(dir);
1706 struct pid_entry *p;
1707 struct proc_inode *ei;
1709 error = ERR_PTR(-ENOENT);
1715 for (p = ents; p->name; p++) {
1716 if (p->len != dentry->d_name.len)
1718 if (!memcmp(dentry->d_name.name, p->name, p->len))
1725 case PROC_TID_VX_INFO:
1726 case PROC_TGID_VX_INFO:
1727 case PROC_TID_IP_INFO:
1728 case PROC_TGID_IP_INFO:
1729 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
1735 error = ERR_PTR(-EINVAL);
1736 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1741 inode->i_mode = p->mode;
1743 * Yes, it does not scale. And it should not. Don't add
1744 * new entries into /proc/<tgid>/ without very good reasons.
1747 case PROC_TGID_TASK:
1749 inode->i_op = &proc_task_inode_operations;
1750 inode->i_fop = &proc_task_operations;
1755 inode->i_op = &proc_fd_inode_operations;
1756 inode->i_fop = &proc_fd_operations;
1760 inode->i_op = &proc_pid_link_inode_operations;
1761 ei->op.proc_get_link = proc_exe_link;
1765 inode->i_op = &proc_pid_link_inode_operations;
1766 ei->op.proc_get_link = proc_cwd_link;
1769 case PROC_TGID_ROOT:
1770 inode->i_op = &proc_pid_link_inode_operations;
1771 ei->op.proc_get_link = proc_root_link;
1773 case PROC_TID_ENVIRON:
1774 case PROC_TGID_ENVIRON:
1775 inode->i_fop = &proc_info_file_operations;
1776 ei->op.proc_read = proc_pid_environ;
1779 case PROC_TGID_AUXV:
1780 inode->i_fop = &proc_info_file_operations;
1781 ei->op.proc_read = proc_pid_auxv;
1783 case PROC_TID_STATUS:
1784 case PROC_TGID_STATUS:
1785 inode->i_fop = &proc_info_file_operations;
1786 ei->op.proc_read = proc_pid_status;
1789 inode->i_fop = &proc_info_file_operations;
1790 ei->op.proc_read = proc_tid_stat;
1792 case PROC_TGID_STAT:
1793 inode->i_fop = &proc_info_file_operations;
1794 ei->op.proc_read = proc_tgid_stat;
1796 case PROC_TID_CMDLINE:
1797 case PROC_TGID_CMDLINE:
1798 inode->i_fop = &proc_info_file_operations;
1799 ei->op.proc_read = proc_pid_cmdline;
1801 case PROC_TID_STATM:
1802 case PROC_TGID_STATM:
1803 inode->i_fop = &proc_info_file_operations;
1804 ei->op.proc_read = proc_pid_statm;
1807 case PROC_TGID_MAPS:
1808 inode->i_fop = &proc_maps_operations;
1811 case PROC_TID_NUMA_MAPS:
1812 case PROC_TGID_NUMA_MAPS:
1813 inode->i_fop = &proc_numa_maps_operations;
1818 inode->i_fop = &proc_mem_operations;
1820 #ifdef CONFIG_SECCOMP
1821 case PROC_TID_SECCOMP:
1822 case PROC_TGID_SECCOMP:
1823 inode->i_fop = &proc_seccomp_operations;
1825 #endif /* CONFIG_SECCOMP */
1826 case PROC_TID_MOUNTS:
1827 case PROC_TGID_MOUNTS:
1828 inode->i_fop = &proc_mounts_operations;
1831 case PROC_TID_SMAPS:
1832 case PROC_TGID_SMAPS:
1833 inode->i_fop = &proc_smaps_operations;
1836 case PROC_TID_MOUNTSTATS:
1837 case PROC_TGID_MOUNTSTATS:
1838 inode->i_fop = &proc_mountstats_operations;
1840 #ifdef CONFIG_SECURITY
1843 inode->i_op = &proc_tid_attr_inode_operations;
1844 inode->i_fop = &proc_tid_attr_operations;
1846 case PROC_TGID_ATTR:
1848 inode->i_op = &proc_tgid_attr_inode_operations;
1849 inode->i_fop = &proc_tgid_attr_operations;
1851 case PROC_TID_ATTR_CURRENT:
1852 case PROC_TGID_ATTR_CURRENT:
1853 case PROC_TID_ATTR_PREV:
1854 case PROC_TGID_ATTR_PREV:
1855 case PROC_TID_ATTR_EXEC:
1856 case PROC_TGID_ATTR_EXEC:
1857 case PROC_TID_ATTR_FSCREATE:
1858 case PROC_TGID_ATTR_FSCREATE:
1859 case PROC_TID_ATTR_KEYCREATE:
1860 case PROC_TGID_ATTR_KEYCREATE:
1861 case PROC_TID_ATTR_SOCKCREATE:
1862 case PROC_TGID_ATTR_SOCKCREATE:
1863 inode->i_fop = &proc_pid_attr_operations;
1866 #ifdef CONFIG_KALLSYMS
1867 case PROC_TID_WCHAN:
1868 case PROC_TGID_WCHAN:
1869 inode->i_fop = &proc_info_file_operations;
1870 ei->op.proc_read = proc_pid_wchan;
1873 #ifdef CONFIG_SCHEDSTATS
1874 case PROC_TID_SCHEDSTAT:
1875 case PROC_TGID_SCHEDSTAT:
1876 inode->i_fop = &proc_info_file_operations;
1877 ei->op.proc_read = proc_pid_schedstat;
1880 #ifdef CONFIG_CPUSETS
1881 case PROC_TID_CPUSET:
1882 case PROC_TGID_CPUSET:
1883 inode->i_fop = &proc_cpuset_operations;
1886 case PROC_TID_OOM_SCORE:
1887 case PROC_TGID_OOM_SCORE:
1888 inode->i_fop = &proc_info_file_operations;
1889 ei->op.proc_read = proc_oom_score;
1891 case PROC_TID_OOM_ADJUST:
1892 case PROC_TGID_OOM_ADJUST:
1893 inode->i_fop = &proc_oom_adjust_operations;
1895 #ifdef CONFIG_AUDITSYSCALL
1896 case PROC_TID_LOGINUID:
1897 case PROC_TGID_LOGINUID:
1898 inode->i_fop = &proc_loginuid_operations;
1901 case PROC_TID_VX_INFO:
1902 case PROC_TGID_VX_INFO:
1903 inode->i_fop = &proc_info_file_operations;
1904 ei->op.proc_read = proc_pid_vx_info;
1906 case PROC_TID_IP_INFO:
1907 case PROC_TGID_IP_INFO:
1908 inode->i_fop = &proc_info_file_operations;
1909 ei->op.proc_read = proc_pid_nx_info;
1912 printk("procfs: impossible type (%d)",p->type);
1914 error = ERR_PTR(-EINVAL);
1917 dentry->d_op = &pid_dentry_operations;
1918 d_add(dentry, inode);
1919 /* Close the race of the process dying before we return the dentry */
1920 if (pid_revalidate(dentry, NULL))
1923 put_task_struct(task);
1928 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1929 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1932 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1933 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1936 static struct file_operations proc_tgid_base_operations = {
1937 .read = generic_read_dir,
1938 .readdir = proc_tgid_base_readdir,
1941 static struct file_operations proc_tid_base_operations = {
1942 .read = generic_read_dir,
1943 .readdir = proc_tid_base_readdir,
1946 static struct inode_operations proc_tgid_base_inode_operations = {
1947 .lookup = proc_tgid_base_lookup,
1948 .getattr = pid_getattr,
1949 .setattr = proc_setattr,
1952 static struct inode_operations proc_tid_base_inode_operations = {
1953 .lookup = proc_tid_base_lookup,
1954 .getattr = pid_getattr,
1955 .setattr = proc_setattr,
1958 #ifdef CONFIG_SECURITY
1959 static int proc_tgid_attr_readdir(struct file * filp,
1960 void * dirent, filldir_t filldir)
1962 return proc_pident_readdir(filp,dirent,filldir,
1963 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1966 static int proc_tid_attr_readdir(struct file * filp,
1967 void * dirent, filldir_t filldir)
1969 return proc_pident_readdir(filp,dirent,filldir,
1970 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1973 static struct file_operations proc_tgid_attr_operations = {
1974 .read = generic_read_dir,
1975 .readdir = proc_tgid_attr_readdir,
1978 static struct file_operations proc_tid_attr_operations = {
1979 .read = generic_read_dir,
1980 .readdir = proc_tid_attr_readdir,
1983 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1984 struct dentry *dentry, struct nameidata *nd)
1986 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1989 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1990 struct dentry *dentry, struct nameidata *nd)
1992 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1995 static struct inode_operations proc_tgid_attr_inode_operations = {
1996 .lookup = proc_tgid_attr_lookup,
1997 .getattr = pid_getattr,
1998 .setattr = proc_setattr,
2001 static struct inode_operations proc_tid_attr_inode_operations = {
2002 .lookup = proc_tid_attr_lookup,
2003 .getattr = pid_getattr,
2004 .setattr = proc_setattr,
2011 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2014 char tmp[PROC_NUMBUF];
2015 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
2016 return vfs_readlink(dentry,buffer,buflen,tmp);
2019 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2021 char tmp[PROC_NUMBUF];
2022 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
2023 return ERR_PTR(vfs_follow_link(nd,tmp));
2026 static struct inode_operations proc_self_inode_operations = {
2027 .readlink = proc_self_readlink,
2028 .follow_link = proc_self_follow_link,
2032 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2034 * @task: task that should be flushed.
2036 * Looks in the dcache for
2038 * /proc/@tgid/task/@pid
2039 * if either directory is present flushes it and all of it'ts children
2042 * It is safe and reasonable to cache /proc entries for a task until
2043 * that task exits. After that they just clog up the dcache with
2044 * useless entries, possibly causing useful dcache entries to be
2045 * flushed instead. This routine is proved to flush those useless
2046 * dcache entries at process exit time.
2048 * NOTE: This routine is just an optimization so it does not guarantee
2049 * that no dcache entries will exist at process exit time it
2050 * just makes it very unlikely that any will persist.
2052 void proc_flush_task(struct task_struct *task)
2054 struct dentry *dentry, *leader, *dir;
2055 char buf[PROC_NUMBUF];
2059 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2060 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2062 shrink_dcache_parent(dentry);
2067 if (thread_group_leader(task))
2071 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2072 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2077 name.len = strlen(name.name);
2078 dir = d_hash_and_lookup(leader, &name);
2080 goto out_put_leader;
2083 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2084 dentry = d_hash_and_lookup(dir, &name);
2086 shrink_dcache_parent(dentry);
2099 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2101 struct dentry *result = ERR_PTR(-ENOENT);
2102 struct task_struct *task;
2103 struct inode *inode;
2104 struct proc_inode *ei;
2107 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2108 inode = new_inode(dir->i_sb);
2110 return ERR_PTR(-ENOMEM);
2112 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2113 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2115 inode->i_mode = S_IFLNK|S_IRWXUGO;
2116 inode->i_uid = inode->i_gid = 0;
2118 inode->i_op = &proc_self_inode_operations;
2119 d_add(dentry, inode);
2122 tgid = name_to_int(dentry);
2127 task = vx_find_proc_task_by_pid(tgid);
2129 get_task_struct(task);
2134 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2138 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2139 inode->i_op = &proc_tgid_base_inode_operations;
2140 inode->i_fop = &proc_tgid_base_operations;
2141 inode->i_flags|=S_IMMUTABLE;
2142 #ifdef CONFIG_SECURITY
2148 dentry->d_op = &pid_dentry_operations;
2150 d_add(dentry, inode);
2151 /* Close the race of the process dying before we return the dentry */
2152 if (pid_revalidate(dentry, NULL))
2156 put_task_struct(task);
2162 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2164 struct dentry *result = ERR_PTR(-ENOENT);
2165 struct task_struct *task;
2166 struct task_struct *leader = get_proc_task(dir);
2167 struct inode *inode;
2173 tid = name_to_int(dentry);
2176 if (vx_current_initpid(tid))
2180 task = vx_find_proc_task_by_pid(tid);
2182 get_task_struct(task);
2186 if (leader->tgid != task->tgid)
2189 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2194 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2195 inode->i_op = &proc_tid_base_inode_operations;
2196 inode->i_fop = &proc_tid_base_operations;
2197 inode->i_flags|=S_IMMUTABLE;
2198 #ifdef CONFIG_SECURITY
2204 dentry->d_op = &pid_dentry_operations;
2206 d_add(dentry, inode);
2207 /* Close the race of the process dying before we return the dentry */
2208 if (pid_revalidate(dentry, NULL))
2212 put_task_struct(task);
2214 put_task_struct(leader);
2220 * Find the first tgid to return to user space.
2222 * Usually this is just whatever follows &init_task, but if the users
2223 * buffer was too small to hold the full list or there was a seek into
2224 * the middle of the directory we have more work to do.
2226 * In the case of a short read we start with find_task_by_pid.
2228 * In the case of a seek we start with &init_task and walk nr
2231 static struct task_struct *first_tgid(int tgid, unsigned int nr)
2233 struct task_struct *pos;
2236 pos = vx_find_proc_task_by_pid(tgid);
2237 if (pos && thread_group_leader(pos))
2240 /* If nr exceeds the number of processes get out quickly */
2242 if (nr && nr >= nr_processes())
2245 /* If we haven't found our starting place yet start with
2246 * the init_task and walk nr tasks forward.
2248 for (pos = next_task(&init_task); nr > 0; --nr) {
2249 pos = next_task(pos);
2250 if (pos == &init_task) {
2256 get_task_struct(pos);
2263 * Find the next task in the task list.
2264 * Return NULL if we loop or there is any error.
2266 * The reference to the input task_struct is released.
2268 static struct task_struct *next_tgid(struct task_struct *start)
2270 struct task_struct *pos;
2273 if (pid_alive(start))
2274 pos = next_task(start);
2275 if (pid_alive(pos) && (pos != &init_task)) {
2276 get_task_struct(pos);
2282 put_task_struct(start);
2286 /* for the /proc/ directory itself, after non-process stuff has been done */
2287 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2289 char buf[PROC_NUMBUF];
2290 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2291 struct task_struct *task;
2295 ino_t ino = fake_ino(0,PROC_TGID_INO);
2296 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2303 /* f_version caches the tgid value that the last readdir call couldn't
2304 * return. lseek aka telldir automagically resets f_version to 0.
2306 tgid = filp->f_version;
2307 filp->f_version = 0;
2308 for (task = first_tgid(tgid, nr);
2310 task = next_tgid(task), filp->f_pos++) {
2313 tgid = vx_map_tgid(task->pid);
2314 if (!vx_proc_pid_visible(task, tgid))
2317 len = snprintf(buf, sizeof(buf), "%d", tgid);
2318 ino = fake_ino(tgid, PROC_TGID_INO);
2319 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2320 /* returning this tgid failed, save it as the first
2321 * pid for the next readir call */
2322 filp->f_version = tgid;
2323 put_task_struct(task);
2331 * Find the first tid of a thread group to return to user space.
2333 * Usually this is just the thread group leader, but if the users
2334 * buffer was too small or there was a seek into the middle of the
2335 * directory we have more work todo.
2337 * In the case of a short read we start with find_task_by_pid.
2339 * In the case of a seek we start with the leader and walk nr
2342 static struct task_struct *first_tid(struct task_struct *leader,
2345 struct task_struct *pos;
2348 /* Attempt to start with the pid of a thread */
2349 if (tid && (nr > 0)) {
2350 pos = vx_find_proc_task_by_pid(tid);
2351 if (pos && (pos->group_leader == leader))
2355 /* If nr exceeds the number of threads there is nothing todo */
2357 if (nr && nr >= get_nr_threads(leader))
2360 /* If we haven't found our starting place yet start
2361 * with the leader and walk nr threads forward.
2363 for (pos = leader; nr > 0; --nr) {
2364 pos = next_thread(pos);
2365 if (pos == leader) {
2371 get_task_struct(pos);
2378 * Find the next thread in the thread list.
2379 * Return NULL if there is an error or no next thread.
2381 * The reference to the input task_struct is released.
2383 static struct task_struct *next_tid(struct task_struct *start)
2385 struct task_struct *pos = NULL;
2387 if (pid_alive(start)) {
2388 pos = next_thread(start);
2389 if (thread_group_leader(pos))
2392 get_task_struct(pos);
2395 put_task_struct(start);
2399 /* for the /proc/TGID/task/ directories */
2400 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2402 char buf[PROC_NUMBUF];
2403 struct dentry *dentry = filp->f_dentry;
2404 struct inode *inode = dentry->d_inode;
2405 struct task_struct *leader = get_proc_task(inode);
2406 struct task_struct *task;
2407 int retval = -ENOENT;
2410 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2419 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2424 ino = parent_ino(dentry);
2425 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2431 /* f_version caches the tgid value that the last readdir call couldn't
2432 * return. lseek aka telldir automagically resets f_version to 0.
2434 tid = filp->f_version;
2435 filp->f_version = 0;
2436 for (task = first_tid(leader, tid, pos - 2);
2438 task = next_tid(task), pos++) {
2440 tid = vx_map_pid(task->pid);
2441 if (!vx_proc_pid_visible(task, tid))
2444 len = snprintf(buf, sizeof(buf), "%d", tid);
2445 ino = fake_ino(tid, PROC_TID_INO);
2446 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2447 /* returning this tgid failed, save it as the first
2448 * pid for the next readir call */
2449 filp->f_version = tid;
2450 put_task_struct(task);
2456 put_task_struct(leader);
2461 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2463 struct inode *inode = dentry->d_inode;
2464 struct task_struct *p = get_proc_task(inode);
2465 generic_fillattr(inode, stat);
2469 stat->nlink += get_nr_threads(p);