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/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
73 #include <linux/poll.h>
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
88 * For hysterical raisins we keep the same inumbers as in the old procfs.
89 * Feel free to change the macro below - just keep the range distinct from
90 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
91 * As soon as we'll get a separate superblock we will be able to forget
92 * about magical ranges too.
95 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
97 enum pid_directory_inos {
102 #ifdef CONFIG_SECCOMP
117 PROC_TGID_MOUNTSTATS,
122 #ifdef CONFIG_SCHEDSTATS
125 #ifdef CONFIG_CPUSETS
128 #ifdef CONFIG_SECURITY
130 PROC_TGID_ATTR_CURRENT,
133 PROC_TGID_ATTR_FSCREATE,
134 PROC_TGID_ATTR_KEYCREATE,
135 PROC_TGID_ATTR_SOCKCREATE,
139 #ifdef CONFIG_AUDITSYSCALL
143 PROC_TGID_OOM_ADJUST,
147 #ifdef CONFIG_SECCOMP
167 #ifdef CONFIG_SCHEDSTATS
170 #ifdef CONFIG_CPUSETS
173 #ifdef CONFIG_SECURITY
175 PROC_TID_ATTR_CURRENT,
178 PROC_TID_ATTR_FSCREATE,
179 PROC_TID_ATTR_KEYCREATE,
180 PROC_TID_ATTR_SOCKCREATE,
184 #ifdef CONFIG_AUDITSYSCALL
190 /* Add new entries before this */
191 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
194 /* Worst case buffer size needed for holding an integer. */
195 #define PROC_NUMBUF 10
204 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
206 static struct pid_entry tgid_base_stuff[] = {
207 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
208 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
209 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
210 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
211 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
212 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
213 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
214 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
215 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
217 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
219 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
220 #ifdef CONFIG_SECCOMP
221 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
223 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
224 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
225 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
226 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
227 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
229 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUSR),
231 #ifdef CONFIG_SECURITY
232 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
234 #ifdef CONFIG_KALLSYMS
235 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
237 #ifdef CONFIG_SCHEDSTATS
238 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
240 #ifdef CONFIG_CPUSETS
241 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
243 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
244 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
245 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
246 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
247 #ifdef CONFIG_AUDITSYSCALL
248 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
252 static struct pid_entry tid_base_stuff[] = {
253 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
254 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
255 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
256 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
257 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
258 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
259 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
260 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
262 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
264 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
265 #ifdef CONFIG_SECCOMP
266 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
268 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
269 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
270 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
271 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
273 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUSR),
275 #ifdef CONFIG_SECURITY
276 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
278 #ifdef CONFIG_KALLSYMS
279 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
281 #ifdef CONFIG_SCHEDSTATS
282 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
284 #ifdef CONFIG_CPUSETS
285 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
287 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
288 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
289 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
290 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
291 #ifdef CONFIG_AUDITSYSCALL
292 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
297 #ifdef CONFIG_SECURITY
298 static struct pid_entry tgid_attr_stuff[] = {
299 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
300 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
301 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
302 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
303 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
304 E(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
307 static struct pid_entry tid_attr_stuff[] = {
308 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
309 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
310 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
311 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
312 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
313 E(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
320 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
322 struct task_struct *task = get_proc_task(inode);
323 struct files_struct *files = NULL;
325 int fd = proc_fd(inode);
328 files = get_files_struct(task);
329 put_task_struct(task);
333 * We are not taking a ref to the file structure, so we must
336 spin_lock(&files->file_lock);
337 file = fcheck_files(files, fd);
339 *mnt = mntget(file->f_vfsmnt);
340 *dentry = dget(file->f_dentry);
341 spin_unlock(&files->file_lock);
342 put_files_struct(files);
345 spin_unlock(&files->file_lock);
346 put_files_struct(files);
351 static struct fs_struct *get_fs_struct(struct task_struct *task)
353 struct fs_struct *fs;
357 atomic_inc(&fs->count);
362 static int get_nr_threads(struct task_struct *tsk)
364 /* Must be called with the rcu_read_lock held */
368 if (lock_task_sighand(tsk, &flags)) {
369 count = atomic_read(&tsk->signal->count);
370 unlock_task_sighand(tsk, &flags);
375 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
377 struct task_struct *task = get_proc_task(inode);
378 struct fs_struct *fs = NULL;
379 int result = -ENOENT;
382 fs = get_fs_struct(task);
383 put_task_struct(task);
386 read_lock(&fs->lock);
387 *mnt = mntget(fs->pwdmnt);
388 *dentry = dget(fs->pwd);
389 read_unlock(&fs->lock);
396 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
398 struct task_struct *task = get_proc_task(inode);
399 struct fs_struct *fs = NULL;
400 int result = -ENOENT;
403 fs = get_fs_struct(task);
404 put_task_struct(task);
407 read_lock(&fs->lock);
408 *mnt = mntget(fs->rootmnt);
409 *dentry = dget(fs->root);
410 read_unlock(&fs->lock);
417 #define MAY_PTRACE(task) \
418 (task == current || \
419 (task->parent == current && \
420 (task->ptrace & PT_PTRACED) && \
421 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
422 security_ptrace(current,task) == 0))
424 struct mm_struct *mm_for_maps(struct task_struct *task)
426 struct mm_struct *mm = get_task_mm(task);
429 down_read(&mm->mmap_sem);
433 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
439 up_read(&mm->mmap_sem);
444 static int proc_pid_environ(struct task_struct *task, char * buffer)
447 struct mm_struct *mm = get_task_mm(task);
449 unsigned int len = mm->env_end - mm->env_start;
452 res = access_process_vm(task, mm->env_start, buffer, len, 0);
453 if (!ptrace_may_attach(task))
460 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
464 struct mm_struct *mm = get_task_mm(task);
468 goto out_mm; /* Shh! No looking before we're done */
470 len = mm->arg_end - mm->arg_start;
475 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
477 // If the nul at the end of args has been overwritten, then
478 // assume application is using setproctitle(3).
479 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
480 len = strnlen(buffer, res);
484 len = mm->env_end - mm->env_start;
485 if (len > PAGE_SIZE - res)
486 len = PAGE_SIZE - res;
487 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
488 res = strnlen(buffer, res);
497 static int proc_pid_auxv(struct task_struct *task, char *buffer)
500 struct mm_struct *mm = get_task_mm(task);
502 unsigned int nwords = 0;
505 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
506 res = nwords * sizeof(mm->saved_auxv[0]);
509 memcpy(buffer, mm->saved_auxv, res);
516 #ifdef CONFIG_KALLSYMS
518 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
519 * Returns the resolved symbol. If that fails, simply return the address.
521 static int proc_pid_wchan(struct task_struct *task, char *buffer)
524 const char *sym_name;
525 unsigned long wchan, size, offset;
526 char namebuf[KSYM_NAME_LEN+1];
528 wchan = get_wchan(task);
530 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
532 return sprintf(buffer, "%s", sym_name);
533 return sprintf(buffer, "%lu", wchan);
535 #endif /* CONFIG_KALLSYMS */
537 #ifdef CONFIG_SCHEDSTATS
539 * Provides /proc/PID/schedstat
541 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
543 return sprintf(buffer, "%lu %lu %lu\n",
544 task->sched_info.cpu_time,
545 task->sched_info.run_delay,
546 task->sched_info.pcnt);
550 /* The badness from the OOM killer */
551 unsigned long badness(struct task_struct *p, unsigned long uptime);
552 static int proc_oom_score(struct task_struct *task, char *buffer)
554 unsigned long points;
555 struct timespec uptime;
557 do_posix_clock_monotonic_gettime(&uptime);
558 points = badness(task, uptime.tv_sec);
559 return sprintf(buffer, "%lu\n", points);
562 /************************************************************************/
563 /* Here the fs part begins */
564 /************************************************************************/
566 /* permission checks */
567 static int proc_fd_access_allowed(struct inode *inode)
569 struct task_struct *task;
571 /* Allow access to a task's file descriptors if it is us or we
572 * may use ptrace attach to the process and find out that
575 task = get_proc_task(inode);
577 allowed = ptrace_may_attach(task);
578 put_task_struct(task);
583 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
586 struct inode *inode = dentry->d_inode;
588 if (attr->ia_valid & ATTR_MODE)
591 error = inode_change_ok(inode, attr);
593 error = security_inode_setattr(dentry, attr);
595 error = inode_setattr(inode, attr);
600 static struct inode_operations proc_def_inode_operations = {
601 .setattr = proc_setattr,
604 extern struct seq_operations mounts_op;
610 static int mounts_open(struct inode *inode, struct file *file)
612 struct task_struct *task = get_proc_task(inode);
613 struct namespace *namespace = NULL;
614 struct proc_mounts *p;
619 namespace = task->namespace;
621 get_namespace(namespace);
623 put_task_struct(task);
628 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
630 file->private_data = &p->m;
631 ret = seq_open(file, &mounts_op);
633 p->m.private = namespace;
634 p->event = namespace->event;
639 put_namespace(namespace);
644 static int mounts_release(struct inode *inode, struct file *file)
646 struct seq_file *m = file->private_data;
647 struct namespace *namespace = m->private;
648 put_namespace(namespace);
649 return seq_release(inode, file);
652 static unsigned mounts_poll(struct file *file, poll_table *wait)
654 struct proc_mounts *p = file->private_data;
655 struct namespace *ns = p->m.private;
658 poll_wait(file, &ns->poll, wait);
660 spin_lock(&vfsmount_lock);
661 if (p->event != ns->event) {
662 p->event = ns->event;
665 spin_unlock(&vfsmount_lock);
670 static struct file_operations proc_mounts_operations = {
674 .release = mounts_release,
678 extern struct seq_operations mountstats_op;
679 static int mountstats_open(struct inode *inode, struct file *file)
681 int ret = seq_open(file, &mountstats_op);
684 struct seq_file *m = file->private_data;
685 struct namespace *namespace = NULL;
686 struct task_struct *task = get_proc_task(inode);
690 namespace = task->namespace;
692 get_namespace(namespace);
694 put_task_struct(task);
698 m->private = namespace;
700 seq_release(inode, file);
707 static struct file_operations proc_mountstats_operations = {
708 .open = mountstats_open,
711 .release = mounts_release,
714 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
716 static ssize_t proc_info_read(struct file * file, char __user * buf,
717 size_t count, loff_t *ppos)
719 struct inode * inode = file->f_dentry->d_inode;
722 struct task_struct *task = get_proc_task(inode);
728 if (count > PROC_BLOCK_SIZE)
729 count = PROC_BLOCK_SIZE;
732 if (!(page = __get_free_page(GFP_KERNEL)))
735 length = PROC_I(inode)->op.proc_read(task, (char*)page);
738 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
741 put_task_struct(task);
746 static struct file_operations proc_info_file_operations = {
747 .read = proc_info_read,
750 static int mem_open(struct inode* inode, struct file* file)
752 file->private_data = (void*)((long)current->self_exec_id);
756 static ssize_t mem_read(struct file * file, char __user * buf,
757 size_t count, loff_t *ppos)
759 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
761 unsigned long src = *ppos;
763 struct mm_struct *mm;
768 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
772 page = (char *)__get_free_page(GFP_USER);
778 mm = get_task_mm(task);
784 if (file->private_data != (void*)((long)current->self_exec_id))
790 int this_len, retval;
792 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
793 retval = access_process_vm(task, src, page, this_len, 0);
794 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
800 if (copy_to_user(buf, page, retval)) {
815 free_page((unsigned long) page);
817 put_task_struct(task);
822 #define mem_write NULL
825 /* This is a security hazard */
826 static ssize_t mem_write(struct file * file, const char * buf,
827 size_t count, loff_t *ppos)
831 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
832 unsigned long dst = *ppos;
838 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
842 page = (char *)__get_free_page(GFP_USER);
847 int this_len, retval;
849 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
850 if (copy_from_user(page, buf, this_len)) {
854 retval = access_process_vm(task, dst, page, this_len, 1);
866 free_page((unsigned long) page);
868 put_task_struct(task);
874 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
878 file->f_pos = offset;
881 file->f_pos += offset;
886 force_successful_syscall_return();
890 static struct file_operations proc_mem_operations = {
897 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
898 size_t count, loff_t *ppos)
900 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
901 char buffer[PROC_NUMBUF];
904 loff_t __ppos = *ppos;
908 oom_adjust = task->oomkilladj;
909 put_task_struct(task);
911 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
914 if (count > len-__ppos)
916 if (copy_to_user(buf, buffer + __ppos, count))
918 *ppos = __ppos + count;
922 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
923 size_t count, loff_t *ppos)
925 struct task_struct *task;
926 char buffer[PROC_NUMBUF], *end;
929 if (!capable(CAP_SYS_RESOURCE))
931 memset(buffer, 0, sizeof(buffer));
932 if (count > sizeof(buffer) - 1)
933 count = sizeof(buffer) - 1;
934 if (copy_from_user(buffer, buf, count))
936 oom_adjust = simple_strtol(buffer, &end, 0);
937 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
941 task = get_proc_task(file->f_dentry->d_inode);
944 task->oomkilladj = oom_adjust;
945 put_task_struct(task);
946 if (end - buffer == 0)
951 static struct file_operations proc_oom_adjust_operations = {
952 .read = oom_adjust_read,
953 .write = oom_adjust_write,
956 #ifdef CONFIG_AUDITSYSCALL
958 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
959 size_t count, loff_t *ppos)
961 struct inode * inode = file->f_dentry->d_inode;
962 struct task_struct *task = get_proc_task(inode);
964 char tmpbuf[TMPBUFLEN];
968 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
969 audit_get_loginuid(task->audit_context));
970 put_task_struct(task);
971 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
974 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
975 size_t count, loff_t *ppos)
977 struct inode * inode = file->f_dentry->d_inode;
982 if (!capable(CAP_AUDIT_CONTROL))
985 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
988 if (count >= PAGE_SIZE)
989 count = PAGE_SIZE - 1;
992 /* No partial writes. */
995 page = (char*)__get_free_page(GFP_USER);
999 if (copy_from_user(page, buf, count))
1003 loginuid = simple_strtoul(page, &tmp, 10);
1009 length = audit_set_loginuid(current, loginuid);
1010 if (likely(length == 0))
1014 free_page((unsigned long) page);
1018 static struct file_operations proc_loginuid_operations = {
1019 .read = proc_loginuid_read,
1020 .write = proc_loginuid_write,
1024 #ifdef CONFIG_SECCOMP
1025 static ssize_t seccomp_read(struct file *file, char __user *buf,
1026 size_t count, loff_t *ppos)
1028 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1030 loff_t __ppos = *ppos;
1035 /* no need to print the trailing zero, so use only len */
1036 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1037 put_task_struct(tsk);
1040 if (count > len - __ppos)
1041 count = len - __ppos;
1042 if (copy_to_user(buf, __buf + __ppos, count))
1044 *ppos = __ppos + count;
1048 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1049 size_t count, loff_t *ppos)
1051 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1052 char __buf[20], *end;
1053 unsigned int seccomp_mode;
1060 /* can set it only once to be even more secure */
1062 if (unlikely(tsk->seccomp.mode))
1066 memset(__buf, 0, sizeof(__buf));
1067 count = min(count, sizeof(__buf) - 1);
1068 if (copy_from_user(__buf, buf, count))
1071 seccomp_mode = simple_strtoul(__buf, &end, 0);
1075 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1076 tsk->seccomp.mode = seccomp_mode;
1077 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1081 if (unlikely(!(end - __buf)))
1083 result = end - __buf;
1085 put_task_struct(tsk);
1090 static struct file_operations proc_seccomp_operations = {
1091 .read = seccomp_read,
1092 .write = seccomp_write,
1094 #endif /* CONFIG_SECCOMP */
1096 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1098 struct inode *inode = dentry->d_inode;
1099 int error = -EACCES;
1101 /* We don't need a base pointer in the /proc filesystem */
1104 /* Are we allowed to snoop on the tasks file descriptors? */
1105 if (!proc_fd_access_allowed(inode))
1108 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1109 nd->last_type = LAST_BIND;
1111 return ERR_PTR(error);
1114 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1115 char __user *buffer, int buflen)
1117 struct inode * inode;
1118 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1124 inode = dentry->d_inode;
1125 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1126 len = PTR_ERR(path);
1129 len = tmp + PAGE_SIZE - 1 - path;
1133 if (copy_to_user(buffer, path, len))
1136 free_page((unsigned long)tmp);
1140 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1142 int error = -EACCES;
1143 struct inode *inode = dentry->d_inode;
1145 struct vfsmount *mnt = NULL;
1147 /* Are we allowed to snoop on the tasks file descriptors? */
1148 if (!proc_fd_access_allowed(inode))
1151 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1155 error = do_proc_readlink(de, mnt, buffer, buflen);
1162 static struct inode_operations proc_pid_link_inode_operations = {
1163 .readlink = proc_pid_readlink,
1164 .follow_link = proc_pid_follow_link,
1165 .setattr = proc_setattr,
1168 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1170 struct dentry *dentry = filp->f_dentry;
1171 struct inode *inode = dentry->d_inode;
1172 struct task_struct *p = get_proc_task(inode);
1173 unsigned int fd, tid, ino;
1175 char buf[PROC_NUMBUF];
1176 struct files_struct * files;
1177 struct fdtable *fdt;
1188 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1192 ino = parent_ino(dentry);
1193 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1197 files = get_files_struct(p);
1201 fdt = files_fdtable(files);
1202 for (fd = filp->f_pos-2;
1204 fd++, filp->f_pos++) {
1207 if (!fcheck_files(files, fd))
1215 buf[j] = '0' + (i % 10);
1219 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1220 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1227 put_files_struct(files);
1235 static int proc_pident_readdir(struct file *filp,
1236 void *dirent, filldir_t filldir,
1237 struct pid_entry *ents, unsigned int nents)
1241 struct dentry *dentry = filp->f_dentry;
1242 struct inode *inode = dentry->d_inode;
1243 struct task_struct *task = get_proc_task(inode);
1244 struct pid_entry *p;
1254 put_task_struct(task);
1259 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1265 ino = parent_ino(dentry);
1266 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1278 hide = vx_flags(VXF_INFO_HIDE, 0);
1282 case PROC_TGID_VX_INFO:
1283 case PROC_TGID_IP_INFO:
1287 if (filldir(dirent, p->name, p->len, filp->f_pos,
1288 fake_ino(pid, p->type), p->mode >> 12) < 0)
1301 static int proc_tgid_base_readdir(struct file * filp,
1302 void * dirent, filldir_t filldir)
1304 return proc_pident_readdir(filp,dirent,filldir,
1305 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1308 static int proc_tid_base_readdir(struct file * filp,
1309 void * dirent, filldir_t filldir)
1311 return proc_pident_readdir(filp,dirent,filldir,
1312 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1315 /* building an inode */
1317 static int task_dumpable(struct task_struct *task)
1320 struct mm_struct *mm;
1325 dumpable = mm->dumpable;
1333 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1335 struct inode * inode;
1336 struct proc_inode *ei;
1338 /* We need a new inode */
1340 inode = new_inode(sb);
1346 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1347 inode->i_ino = fake_ino(task->pid, ino);
1348 inode->i_op = &proc_def_inode_operations;
1351 * grab the reference to task.
1353 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1359 if (task_dumpable(task)) {
1360 inode->i_uid = task->euid;
1361 inode->i_gid = task->egid;
1363 inode->i_xid = vx_task_xid(task);
1364 security_task_to_inode(task, inode);
1377 * Exceptional case: normally we are not allowed to unhash a busy
1378 * directory. In this case, however, we can do it - no aliasing problems
1379 * due to the way we treat inodes.
1381 * Rewrite the inode's ownerships here because the owning task may have
1382 * performed a setuid(), etc.
1384 * Before the /proc/pid/status file was created the only way to read
1385 * the effective uid of a /process was to stat /proc/pid. Reading
1386 * /proc/pid/status is slow enough that procps and other packages
1387 * kept stating /proc/pid. To keep the rules in /proc simple I have
1388 * made this apply to all per process world readable and executable
1391 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1393 struct inode *inode = dentry->d_inode;
1394 struct task_struct *task = get_proc_task(inode);
1398 int pid = (inode->i_ino >> 16) & 0xFFFF;
1400 if (!vx_proc_pid_visible(task, pid))
1404 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1405 task_dumpable(task)) {
1406 inode->i_uid = task->euid;
1407 inode->i_gid = task->egid;
1412 inode->i_mode &= ~(S_ISUID | S_ISGID);
1413 security_task_to_inode(task, inode);
1420 put_task_struct(task);
1424 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1426 struct inode *inode = dentry->d_inode;
1427 struct task_struct *task;
1428 generic_fillattr(inode, stat);
1433 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1435 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1436 task_dumpable(task)) {
1437 stat->uid = task->euid;
1438 stat->gid = task->egid;
1445 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1447 struct inode *inode = dentry->d_inode;
1448 struct task_struct *task = get_proc_task(inode);
1449 int fd = proc_fd(inode);
1450 struct files_struct *files;
1453 files = get_files_struct(task);
1456 if (fcheck_files(files, fd)) {
1458 put_files_struct(files);
1459 if (task_dumpable(task)) {
1460 inode->i_uid = task->euid;
1461 inode->i_gid = task->egid;
1466 inode->i_mode &= ~(S_ISUID | S_ISGID);
1467 security_task_to_inode(task, inode);
1468 put_task_struct(task);
1472 put_files_struct(files);
1474 put_task_struct(task);
1480 static int pid_delete_dentry(struct dentry * dentry)
1482 /* Is the task we represent dead?
1483 * If so, then don't put the dentry on the lru list,
1484 * kill it immediately.
1486 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1489 static struct dentry_operations tid_fd_dentry_operations =
1491 .d_revalidate = tid_fd_revalidate,
1492 .d_delete = pid_delete_dentry,
1495 static struct dentry_operations pid_dentry_operations =
1497 .d_revalidate = pid_revalidate,
1498 .d_delete = pid_delete_dentry,
1503 static unsigned name_to_int(struct dentry *dentry)
1505 const char *name = dentry->d_name.name;
1506 int len = dentry->d_name.len;
1509 if (len > 1 && *name == '0')
1512 unsigned c = *name++ - '0';
1515 if (n >= (~0U-9)/10)
1526 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1528 struct task_struct *task = get_proc_task(dir);
1529 unsigned fd = name_to_int(dentry);
1530 struct dentry *result = ERR_PTR(-ENOENT);
1532 struct files_struct * files;
1533 struct inode *inode;
1534 struct proc_inode *ei;
1541 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);
1570 /* Close the race of the process dying before we return the dentry */
1571 if (tid_fd_revalidate(dentry, NULL))
1574 put_task_struct(task);
1579 spin_unlock(&files->file_lock);
1580 put_files_struct(files);
1586 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1587 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1588 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
1590 static struct file_operations proc_fd_operations = {
1591 .read = generic_read_dir,
1592 .readdir = proc_readfd,
1595 static struct file_operations proc_task_operations = {
1596 .read = generic_read_dir,
1597 .readdir = proc_task_readdir,
1601 * proc directories can do almost nothing..
1603 static struct inode_operations proc_fd_inode_operations = {
1604 .lookup = proc_lookupfd,
1605 .setattr = proc_setattr,
1608 static struct inode_operations proc_task_inode_operations = {
1609 .lookup = proc_task_lookup,
1610 .getattr = proc_task_getattr,
1611 .setattr = proc_setattr,
1614 #ifdef CONFIG_SECURITY
1615 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1616 size_t count, loff_t *ppos)
1618 struct inode * inode = file->f_dentry->d_inode;
1621 struct task_struct *task = get_proc_task(inode);
1627 if (count > PAGE_SIZE)
1630 if (!(page = __get_free_page(GFP_KERNEL)))
1633 length = security_getprocattr(task,
1634 (char*)file->f_dentry->d_name.name,
1635 (void*)page, count);
1637 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1640 put_task_struct(task);
1645 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1646 size_t count, loff_t *ppos)
1648 struct inode * inode = file->f_dentry->d_inode;
1651 struct task_struct *task = get_proc_task(inode);
1656 if (count > PAGE_SIZE)
1659 /* No partial writes. */
1665 page = (char*)__get_free_page(GFP_USER);
1670 if (copy_from_user(page, buf, count))
1673 length = security_setprocattr(task,
1674 (char*)file->f_dentry->d_name.name,
1675 (void*)page, count);
1677 free_page((unsigned long) page);
1679 put_task_struct(task);
1684 static struct file_operations proc_pid_attr_operations = {
1685 .read = proc_pid_attr_read,
1686 .write = proc_pid_attr_write,
1689 static struct file_operations proc_tid_attr_operations;
1690 static struct inode_operations proc_tid_attr_inode_operations;
1691 static struct file_operations proc_tgid_attr_operations;
1692 static struct inode_operations proc_tgid_attr_inode_operations;
1695 extern int proc_pid_vx_info(struct task_struct *, char *);
1696 extern int proc_pid_nx_info(struct task_struct *, char *);
1698 extern int proc_pid_vx_info(struct task_struct *, char *);
1699 extern int proc_pid_nx_info(struct task_struct *, char *);
1702 static struct dentry *proc_pident_lookup(struct inode *dir,
1703 struct dentry *dentry,
1704 struct pid_entry *ents)
1706 struct inode *inode;
1707 struct dentry *error;
1708 struct task_struct *task = get_proc_task(dir);
1709 struct pid_entry *p;
1710 struct proc_inode *ei;
1712 error = ERR_PTR(-ENOENT);
1718 for (p = ents; p->name; p++) {
1719 if (p->len != dentry->d_name.len)
1721 if (!memcmp(dentry->d_name.name, p->name, p->len))
1728 case PROC_TID_VX_INFO:
1729 case PROC_TGID_VX_INFO:
1730 case PROC_TID_IP_INFO:
1731 case PROC_TGID_IP_INFO:
1732 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
1738 error = ERR_PTR(-EINVAL);
1739 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1744 inode->i_mode = p->mode;
1746 * Yes, it does not scale. And it should not. Don't add
1747 * new entries into /proc/<tgid>/ without very good reasons.
1750 case PROC_TGID_TASK:
1752 inode->i_op = &proc_task_inode_operations;
1753 inode->i_fop = &proc_task_operations;
1758 inode->i_op = &proc_fd_inode_operations;
1759 inode->i_fop = &proc_fd_operations;
1763 inode->i_op = &proc_pid_link_inode_operations;
1764 ei->op.proc_get_link = proc_exe_link;
1768 inode->i_op = &proc_pid_link_inode_operations;
1769 ei->op.proc_get_link = proc_cwd_link;
1772 case PROC_TGID_ROOT:
1773 inode->i_op = &proc_pid_link_inode_operations;
1774 ei->op.proc_get_link = proc_root_link;
1776 case PROC_TID_ENVIRON:
1777 case PROC_TGID_ENVIRON:
1778 inode->i_fop = &proc_info_file_operations;
1779 ei->op.proc_read = proc_pid_environ;
1782 case PROC_TGID_AUXV:
1783 inode->i_fop = &proc_info_file_operations;
1784 ei->op.proc_read = proc_pid_auxv;
1786 case PROC_TID_STATUS:
1787 case PROC_TGID_STATUS:
1788 inode->i_fop = &proc_info_file_operations;
1789 ei->op.proc_read = proc_pid_status;
1792 inode->i_fop = &proc_info_file_operations;
1793 ei->op.proc_read = proc_tid_stat;
1795 case PROC_TGID_STAT:
1796 inode->i_fop = &proc_info_file_operations;
1797 ei->op.proc_read = proc_tgid_stat;
1799 case PROC_TID_CMDLINE:
1800 case PROC_TGID_CMDLINE:
1801 inode->i_fop = &proc_info_file_operations;
1802 ei->op.proc_read = proc_pid_cmdline;
1804 case PROC_TID_STATM:
1805 case PROC_TGID_STATM:
1806 inode->i_fop = &proc_info_file_operations;
1807 ei->op.proc_read = proc_pid_statm;
1810 case PROC_TGID_MAPS:
1811 inode->i_fop = &proc_maps_operations;
1814 case PROC_TID_NUMA_MAPS:
1815 case PROC_TGID_NUMA_MAPS:
1816 inode->i_fop = &proc_numa_maps_operations;
1821 inode->i_fop = &proc_mem_operations;
1823 #ifdef CONFIG_SECCOMP
1824 case PROC_TID_SECCOMP:
1825 case PROC_TGID_SECCOMP:
1826 inode->i_fop = &proc_seccomp_operations;
1828 #endif /* CONFIG_SECCOMP */
1829 case PROC_TID_MOUNTS:
1830 case PROC_TGID_MOUNTS:
1831 inode->i_fop = &proc_mounts_operations;
1834 case PROC_TID_SMAPS:
1835 case PROC_TGID_SMAPS:
1836 inode->i_fop = &proc_smaps_operations;
1839 case PROC_TID_MOUNTSTATS:
1840 case PROC_TGID_MOUNTSTATS:
1841 inode->i_fop = &proc_mountstats_operations;
1843 #ifdef CONFIG_SECURITY
1846 inode->i_op = &proc_tid_attr_inode_operations;
1847 inode->i_fop = &proc_tid_attr_operations;
1849 case PROC_TGID_ATTR:
1851 inode->i_op = &proc_tgid_attr_inode_operations;
1852 inode->i_fop = &proc_tgid_attr_operations;
1854 case PROC_TID_ATTR_CURRENT:
1855 case PROC_TGID_ATTR_CURRENT:
1856 case PROC_TID_ATTR_PREV:
1857 case PROC_TGID_ATTR_PREV:
1858 case PROC_TID_ATTR_EXEC:
1859 case PROC_TGID_ATTR_EXEC:
1860 case PROC_TID_ATTR_FSCREATE:
1861 case PROC_TGID_ATTR_FSCREATE:
1862 case PROC_TID_ATTR_KEYCREATE:
1863 case PROC_TGID_ATTR_KEYCREATE:
1864 case PROC_TID_ATTR_SOCKCREATE:
1865 case PROC_TGID_ATTR_SOCKCREATE:
1866 inode->i_fop = &proc_pid_attr_operations;
1869 #ifdef CONFIG_KALLSYMS
1870 case PROC_TID_WCHAN:
1871 case PROC_TGID_WCHAN:
1872 inode->i_fop = &proc_info_file_operations;
1873 ei->op.proc_read = proc_pid_wchan;
1876 #ifdef CONFIG_SCHEDSTATS
1877 case PROC_TID_SCHEDSTAT:
1878 case PROC_TGID_SCHEDSTAT:
1879 inode->i_fop = &proc_info_file_operations;
1880 ei->op.proc_read = proc_pid_schedstat;
1883 #ifdef CONFIG_CPUSETS
1884 case PROC_TID_CPUSET:
1885 case PROC_TGID_CPUSET:
1886 inode->i_fop = &proc_cpuset_operations;
1889 case PROC_TID_OOM_SCORE:
1890 case PROC_TGID_OOM_SCORE:
1891 inode->i_fop = &proc_info_file_operations;
1892 ei->op.proc_read = proc_oom_score;
1894 case PROC_TID_OOM_ADJUST:
1895 case PROC_TGID_OOM_ADJUST:
1896 inode->i_fop = &proc_oom_adjust_operations;
1898 #ifdef CONFIG_AUDITSYSCALL
1899 case PROC_TID_LOGINUID:
1900 case PROC_TGID_LOGINUID:
1901 inode->i_fop = &proc_loginuid_operations;
1904 case PROC_TID_VX_INFO:
1905 case PROC_TGID_VX_INFO:
1906 inode->i_fop = &proc_info_file_operations;
1907 ei->op.proc_read = proc_pid_vx_info;
1909 case PROC_TID_IP_INFO:
1910 case PROC_TGID_IP_INFO:
1911 inode->i_fop = &proc_info_file_operations;
1912 ei->op.proc_read = proc_pid_nx_info;
1915 printk("procfs: impossible type (%d)",p->type);
1917 error = ERR_PTR(-EINVAL);
1920 dentry->d_op = &pid_dentry_operations;
1921 d_add(dentry, inode);
1922 /* Close the race of the process dying before we return the dentry */
1923 if (pid_revalidate(dentry, NULL))
1926 put_task_struct(task);
1931 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1932 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1935 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1936 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1939 static struct file_operations proc_tgid_base_operations = {
1940 .read = generic_read_dir,
1941 .readdir = proc_tgid_base_readdir,
1944 static struct file_operations proc_tid_base_operations = {
1945 .read = generic_read_dir,
1946 .readdir = proc_tid_base_readdir,
1949 static struct inode_operations proc_tgid_base_inode_operations = {
1950 .lookup = proc_tgid_base_lookup,
1951 .getattr = pid_getattr,
1952 .setattr = proc_setattr,
1955 static struct inode_operations proc_tid_base_inode_operations = {
1956 .lookup = proc_tid_base_lookup,
1957 .getattr = pid_getattr,
1958 .setattr = proc_setattr,
1961 #ifdef CONFIG_SECURITY
1962 static int proc_tgid_attr_readdir(struct file * filp,
1963 void * dirent, filldir_t filldir)
1965 return proc_pident_readdir(filp,dirent,filldir,
1966 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1969 static int proc_tid_attr_readdir(struct file * filp,
1970 void * dirent, filldir_t filldir)
1972 return proc_pident_readdir(filp,dirent,filldir,
1973 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1976 static struct file_operations proc_tgid_attr_operations = {
1977 .read = generic_read_dir,
1978 .readdir = proc_tgid_attr_readdir,
1981 static struct file_operations proc_tid_attr_operations = {
1982 .read = generic_read_dir,
1983 .readdir = proc_tid_attr_readdir,
1986 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1987 struct dentry *dentry, struct nameidata *nd)
1989 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1992 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1993 struct dentry *dentry, struct nameidata *nd)
1995 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1998 static struct inode_operations proc_tgid_attr_inode_operations = {
1999 .lookup = proc_tgid_attr_lookup,
2000 .getattr = pid_getattr,
2001 .setattr = proc_setattr,
2004 static struct inode_operations proc_tid_attr_inode_operations = {
2005 .lookup = proc_tid_attr_lookup,
2006 .getattr = pid_getattr,
2007 .setattr = proc_setattr,
2014 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2017 char tmp[PROC_NUMBUF];
2018 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
2019 return vfs_readlink(dentry,buffer,buflen,tmp);
2022 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2024 char tmp[PROC_NUMBUF];
2025 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
2026 return ERR_PTR(vfs_follow_link(nd,tmp));
2029 static struct inode_operations proc_self_inode_operations = {
2030 .readlink = proc_self_readlink,
2031 .follow_link = proc_self_follow_link,
2035 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2037 * @task: task that should be flushed.
2039 * Looks in the dcache for
2041 * /proc/@tgid/task/@pid
2042 * if either directory is present flushes it and all of it'ts children
2045 * It is safe and reasonable to cache /proc entries for a task until
2046 * that task exits. After that they just clog up the dcache with
2047 * useless entries, possibly causing useful dcache entries to be
2048 * flushed instead. This routine is proved to flush those useless
2049 * dcache entries at process exit time.
2051 * NOTE: This routine is just an optimization so it does not guarantee
2052 * that no dcache entries will exist at process exit time it
2053 * just makes it very unlikely that any will persist.
2055 void proc_flush_task(struct task_struct *task)
2057 struct dentry *dentry, *leader, *dir;
2058 char buf[PROC_NUMBUF];
2062 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2063 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2065 shrink_dcache_parent(dentry);
2070 if (thread_group_leader(task))
2074 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2075 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2080 name.len = strlen(name.name);
2081 dir = d_hash_and_lookup(leader, &name);
2083 goto out_put_leader;
2086 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2087 dentry = d_hash_and_lookup(dir, &name);
2089 shrink_dcache_parent(dentry);
2102 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2104 struct dentry *result = ERR_PTR(-ENOENT);
2105 struct task_struct *task;
2106 struct inode *inode;
2107 struct proc_inode *ei;
2110 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2111 inode = new_inode(dir->i_sb);
2113 return ERR_PTR(-ENOMEM);
2115 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2116 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2118 inode->i_mode = S_IFLNK|S_IRWXUGO;
2119 inode->i_uid = inode->i_gid = 0;
2121 inode->i_op = &proc_self_inode_operations;
2122 d_add(dentry, inode);
2125 tgid = name_to_int(dentry);
2130 task = vx_find_proc_task_by_pid(tgid);
2132 get_task_struct(task);
2137 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2141 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2142 inode->i_op = &proc_tgid_base_inode_operations;
2143 inode->i_fop = &proc_tgid_base_operations;
2144 inode->i_flags|=S_IMMUTABLE;
2145 #ifdef CONFIG_SECURITY
2151 dentry->d_op = &pid_dentry_operations;
2153 d_add(dentry, inode);
2154 /* Close the race of the process dying before we return the dentry */
2155 if (pid_revalidate(dentry, NULL))
2159 put_task_struct(task);
2165 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2167 struct dentry *result = ERR_PTR(-ENOENT);
2168 struct task_struct *task;
2169 struct task_struct *leader = get_proc_task(dir);
2170 struct inode *inode;
2176 tid = name_to_int(dentry);
2179 if (vx_current_initpid(tid))
2183 task = vx_find_proc_task_by_pid(tid);
2185 get_task_struct(task);
2189 if (leader->tgid != task->tgid)
2192 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2197 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2198 inode->i_op = &proc_tid_base_inode_operations;
2199 inode->i_fop = &proc_tid_base_operations;
2200 inode->i_flags|=S_IMMUTABLE;
2201 #ifdef CONFIG_SECURITY
2207 dentry->d_op = &pid_dentry_operations;
2209 d_add(dentry, inode);
2210 /* Close the race of the process dying before we return the dentry */
2211 if (pid_revalidate(dentry, NULL))
2215 put_task_struct(task);
2217 put_task_struct(leader);
2223 * Find the first tgid to return to user space.
2225 * Usually this is just whatever follows &init_task, but if the users
2226 * buffer was too small to hold the full list or there was a seek into
2227 * the middle of the directory we have more work to do.
2229 * In the case of a short read we start with find_task_by_pid.
2231 * In the case of a seek we start with &init_task and walk nr
2234 static struct task_struct *first_tgid(int tgid, unsigned int nr)
2236 struct task_struct *pos;
2239 pos = vx_find_proc_task_by_pid(tgid);
2240 if (pos && thread_group_leader(pos))
2243 /* If nr exceeds the number of processes get out quickly */
2245 if (nr && nr >= nr_processes())
2248 /* If we haven't found our starting place yet start with
2249 * the init_task and walk nr tasks forward.
2251 for (pos = next_task(&init_task); nr > 0; --nr) {
2252 pos = next_task(pos);
2253 if (pos == &init_task) {
2259 get_task_struct(pos);
2266 * Find the next task in the task list.
2267 * Return NULL if we loop or there is any error.
2269 * The reference to the input task_struct is released.
2271 static struct task_struct *next_tgid(struct task_struct *start)
2273 struct task_struct *pos;
2276 if (pid_alive(start))
2277 pos = next_task(start);
2278 if (pid_alive(pos) && (pos != &init_task)) {
2279 get_task_struct(pos);
2285 put_task_struct(start);
2289 /* for the /proc/ directory itself, after non-process stuff has been done */
2290 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2292 char buf[PROC_NUMBUF];
2293 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2294 struct task_struct *task;
2298 ino_t ino = fake_ino(0,PROC_TGID_INO);
2299 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2306 /* f_version caches the tgid value that the last readdir call couldn't
2307 * return. lseek aka telldir automagically resets f_version to 0.
2309 tgid = filp->f_version;
2310 filp->f_version = 0;
2311 for (task = first_tgid(tgid, nr);
2313 task = next_tgid(task), filp->f_pos++) {
2316 tgid = vx_map_tgid(task->pid);
2317 if (!vx_proc_pid_visible(task, tgid))
2320 len = snprintf(buf, sizeof(buf), "%d", tgid);
2321 ino = fake_ino(tgid, PROC_TGID_INO);
2322 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2323 /* returning this tgid failed, save it as the first
2324 * pid for the next readir call */
2325 filp->f_version = tgid;
2326 put_task_struct(task);
2334 * Find the first tid of a thread group to return to user space.
2336 * Usually this is just the thread group leader, but if the users
2337 * buffer was too small or there was a seek into the middle of the
2338 * directory we have more work todo.
2340 * In the case of a short read we start with find_task_by_pid.
2342 * In the case of a seek we start with the leader and walk nr
2345 static struct task_struct *first_tid(struct task_struct *leader,
2348 struct task_struct *pos;
2351 /* Attempt to start with the pid of a thread */
2352 if (tid && (nr > 0)) {
2353 pos = vx_find_proc_task_by_pid(tid);
2354 if (pos && (pos->group_leader == leader))
2358 /* If nr exceeds the number of threads there is nothing todo */
2360 if (nr && nr >= get_nr_threads(leader))
2363 /* If we haven't found our starting place yet start
2364 * with the leader and walk nr threads forward.
2366 for (pos = leader; nr > 0; --nr) {
2367 pos = next_thread(pos);
2368 if (pos == leader) {
2374 get_task_struct(pos);
2381 * Find the next thread in the thread list.
2382 * Return NULL if there is an error or no next thread.
2384 * The reference to the input task_struct is released.
2386 static struct task_struct *next_tid(struct task_struct *start)
2388 struct task_struct *pos = NULL;
2390 if (pid_alive(start)) {
2391 pos = next_thread(start);
2392 if (thread_group_leader(pos))
2395 get_task_struct(pos);
2398 put_task_struct(start);
2402 /* for the /proc/TGID/task/ directories */
2403 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2405 char buf[PROC_NUMBUF];
2406 struct dentry *dentry = filp->f_dentry;
2407 struct inode *inode = dentry->d_inode;
2408 struct task_struct *leader = get_proc_task(inode);
2409 struct task_struct *task;
2410 int retval = -ENOENT;
2413 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2422 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2427 ino = parent_ino(dentry);
2428 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2434 /* f_version caches the tgid value that the last readdir call couldn't
2435 * return. lseek aka telldir automagically resets f_version to 0.
2437 tid = filp->f_version;
2438 filp->f_version = 0;
2439 for (task = first_tid(leader, tid, pos - 2);
2441 task = next_tid(task), pos++) {
2443 tid = vx_map_pid(task->pid);
2444 if (!vx_proc_pid_visible(task, tid))
2447 len = snprintf(buf, sizeof(buf), "%d", tid);
2448 ino = fake_ino(tid, PROC_TID_INO);
2449 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2450 /* returning this tgid failed, save it as the first
2451 * pid for the next readir call */
2452 filp->f_version = tid;
2453 put_task_struct(task);
2459 put_task_struct(leader);
2464 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2466 struct inode *inode = dentry->d_inode;
2467 struct task_struct *p = get_proc_task(inode);
2468 generic_fillattr(inode, stat);
2472 stat->nlink += get_nr_threads(p);