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>
74 #include <linux/vs_context.h>
75 #include <linux/vs_network.h>
80 * Implementing inode permission operations in /proc is almost
81 * certainly an error. Permission checks need to happen during
82 * each system call not at open time. The reason is that most of
83 * what we wish to check for permissions in /proc varies at runtime.
85 * The classic example of a problem is opening file descriptors
86 * in /proc for a task before it execs a suid executable.
90 * For hysterical raisins we keep the same inumbers as in the old procfs.
91 * Feel free to change the macro below - just keep the range distinct from
92 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
93 * As soon as we'll get a separate superblock we will be able to forget
94 * about magical ranges too.
97 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
99 enum pid_directory_inos {
104 #ifdef CONFIG_SECCOMP
119 PROC_TGID_MOUNTSTATS,
124 #ifdef CONFIG_SCHEDSTATS
127 #ifdef CONFIG_CPUSETS
130 #ifdef CONFIG_SECURITY
132 PROC_TGID_ATTR_CURRENT,
135 PROC_TGID_ATTR_FSCREATE,
136 PROC_TGID_ATTR_KEYCREATE,
137 PROC_TGID_ATTR_SOCKCREATE,
141 #ifdef CONFIG_AUDITSYSCALL
145 PROC_TGID_OOM_ADJUST,
149 #ifdef CONFIG_SECCOMP
169 #ifdef CONFIG_SCHEDSTATS
172 #ifdef CONFIG_CPUSETS
175 #ifdef CONFIG_SECURITY
177 PROC_TID_ATTR_CURRENT,
180 PROC_TID_ATTR_FSCREATE,
181 PROC_TID_ATTR_KEYCREATE,
182 PROC_TID_ATTR_SOCKCREATE,
186 #ifdef CONFIG_AUDITSYSCALL
192 /* Add new entries before this */
193 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
196 /* Worst case buffer size needed for holding an integer. */
197 #define PROC_NUMBUF 10
206 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
208 static struct pid_entry tgid_base_stuff[] = {
209 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
210 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
211 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
212 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
213 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
214 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
215 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
216 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
217 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
219 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
221 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
222 #ifdef CONFIG_SECCOMP
223 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
225 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
226 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
227 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
228 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
229 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
231 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUSR),
233 #ifdef CONFIG_SECURITY
234 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
236 #ifdef CONFIG_KALLSYMS
237 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
239 #ifdef CONFIG_SCHEDSTATS
240 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
242 #ifdef CONFIG_CPUSETS
243 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
245 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
246 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
247 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
248 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
249 #ifdef CONFIG_AUDITSYSCALL
250 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
254 static struct pid_entry tid_base_stuff[] = {
255 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
256 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
257 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
258 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
259 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
260 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
261 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
262 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
264 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
266 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
267 #ifdef CONFIG_SECCOMP
268 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
270 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
271 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
272 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
273 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
275 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUSR),
277 #ifdef CONFIG_SECURITY
278 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
280 #ifdef CONFIG_KALLSYMS
281 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
283 #ifdef CONFIG_SCHEDSTATS
284 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
286 #ifdef CONFIG_CPUSETS
287 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
289 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
290 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
291 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
292 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
293 #ifdef CONFIG_AUDITSYSCALL
294 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
299 #ifdef CONFIG_SECURITY
300 static struct pid_entry tgid_attr_stuff[] = {
301 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
302 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
303 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
304 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
305 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
306 E(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
309 static struct pid_entry tid_attr_stuff[] = {
310 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
311 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
312 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
313 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
314 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
315 E(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
322 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
324 struct task_struct *task = get_proc_task(inode);
325 struct files_struct *files = NULL;
327 int fd = proc_fd(inode);
330 files = get_files_struct(task);
331 put_task_struct(task);
335 * We are not taking a ref to the file structure, so we must
338 spin_lock(&files->file_lock);
339 file = fcheck_files(files, fd);
341 *mnt = mntget(file->f_vfsmnt);
342 *dentry = dget(file->f_dentry);
343 spin_unlock(&files->file_lock);
344 put_files_struct(files);
347 spin_unlock(&files->file_lock);
348 put_files_struct(files);
353 static struct fs_struct *get_fs_struct(struct task_struct *task)
355 struct fs_struct *fs;
359 atomic_inc(&fs->count);
364 static int get_nr_threads(struct task_struct *tsk)
366 /* Must be called with the rcu_read_lock held */
370 if (lock_task_sighand(tsk, &flags)) {
371 count = atomic_read(&tsk->signal->count);
372 unlock_task_sighand(tsk, &flags);
377 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
379 struct task_struct *task = get_proc_task(inode);
380 struct fs_struct *fs = NULL;
381 int result = -ENOENT;
384 fs = get_fs_struct(task);
385 put_task_struct(task);
388 read_lock(&fs->lock);
389 *mnt = mntget(fs->pwdmnt);
390 *dentry = dget(fs->pwd);
391 read_unlock(&fs->lock);
398 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
400 struct task_struct *task = get_proc_task(inode);
401 struct fs_struct *fs = NULL;
402 int result = -ENOENT;
405 fs = get_fs_struct(task);
406 put_task_struct(task);
409 read_lock(&fs->lock);
410 *mnt = mntget(fs->rootmnt);
411 *dentry = dget(fs->root);
412 read_unlock(&fs->lock);
419 #define MAY_PTRACE(task) \
420 (task == current || \
421 (task->parent == current && \
422 (task->ptrace & PT_PTRACED) && \
423 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
424 security_ptrace(current,task) == 0))
426 struct mm_struct *mm_for_maps(struct task_struct *task)
428 struct mm_struct *mm = get_task_mm(task);
431 down_read(&mm->mmap_sem);
435 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
441 up_read(&mm->mmap_sem);
446 static int proc_pid_environ(struct task_struct *task, char * buffer)
449 struct mm_struct *mm = get_task_mm(task);
451 unsigned int len = mm->env_end - mm->env_start;
454 res = access_process_vm(task, mm->env_start, buffer, len, 0);
455 if (!ptrace_may_attach(task))
462 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
466 struct mm_struct *mm = get_task_mm(task);
470 goto out_mm; /* Shh! No looking before we're done */
472 len = mm->arg_end - mm->arg_start;
477 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
479 // If the nul at the end of args has been overwritten, then
480 // assume application is using setproctitle(3).
481 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
482 len = strnlen(buffer, res);
486 len = mm->env_end - mm->env_start;
487 if (len > PAGE_SIZE - res)
488 len = PAGE_SIZE - res;
489 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
490 res = strnlen(buffer, res);
499 static int proc_pid_auxv(struct task_struct *task, char *buffer)
502 struct mm_struct *mm = get_task_mm(task);
504 unsigned int nwords = 0;
507 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
508 res = nwords * sizeof(mm->saved_auxv[0]);
511 memcpy(buffer, mm->saved_auxv, res);
518 #ifdef CONFIG_KALLSYMS
520 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
521 * Returns the resolved symbol. If that fails, simply return the address.
523 static int proc_pid_wchan(struct task_struct *task, char *buffer)
526 const char *sym_name;
527 unsigned long wchan, size, offset;
528 char namebuf[KSYM_NAME_LEN+1];
530 wchan = get_wchan(task);
532 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
534 return sprintf(buffer, "%s", sym_name);
535 return sprintf(buffer, "%lu", wchan);
537 #endif /* CONFIG_KALLSYMS */
539 #ifdef CONFIG_SCHEDSTATS
541 * Provides /proc/PID/schedstat
543 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
545 return sprintf(buffer, "%lu %lu %lu\n",
546 task->sched_info.cpu_time,
547 task->sched_info.run_delay,
548 task->sched_info.pcnt);
552 /* The badness from the OOM killer */
553 unsigned long badness(struct task_struct *p, unsigned long uptime);
554 static int proc_oom_score(struct task_struct *task, char *buffer)
556 unsigned long points;
557 struct timespec uptime;
559 do_posix_clock_monotonic_gettime(&uptime);
560 points = badness(task, uptime.tv_sec);
561 return sprintf(buffer, "%lu\n", points);
564 /************************************************************************/
565 /* Here the fs part begins */
566 /************************************************************************/
568 /* permission checks */
569 static int proc_fd_access_allowed(struct inode *inode)
571 struct task_struct *task;
573 /* Allow access to a task's file descriptors if it is us or we
574 * may use ptrace attach to the process and find out that
577 task = get_proc_task(inode);
579 allowed = ptrace_may_attach(task);
580 put_task_struct(task);
585 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
588 struct inode *inode = dentry->d_inode;
590 if (attr->ia_valid & ATTR_MODE)
593 error = inode_change_ok(inode, attr);
595 error = security_inode_setattr(dentry, attr);
597 error = inode_setattr(inode, attr);
602 static struct inode_operations proc_def_inode_operations = {
603 .setattr = proc_setattr,
606 extern struct seq_operations mounts_op;
612 static int mounts_open(struct inode *inode, struct file *file)
614 struct task_struct *task = get_proc_task(inode);
615 struct namespace *namespace = NULL;
616 struct proc_mounts *p;
621 namespace = task->namespace;
623 get_namespace(namespace);
625 put_task_struct(task);
630 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
632 file->private_data = &p->m;
633 ret = seq_open(file, &mounts_op);
635 p->m.private = namespace;
636 p->event = namespace->event;
641 put_namespace(namespace);
646 static int mounts_release(struct inode *inode, struct file *file)
648 struct seq_file *m = file->private_data;
649 struct namespace *namespace = m->private;
650 put_namespace(namespace);
651 return seq_release(inode, file);
654 static unsigned mounts_poll(struct file *file, poll_table *wait)
656 struct proc_mounts *p = file->private_data;
657 struct namespace *ns = p->m.private;
660 poll_wait(file, &ns->poll, wait);
662 spin_lock(&vfsmount_lock);
663 if (p->event != ns->event) {
664 p->event = ns->event;
667 spin_unlock(&vfsmount_lock);
672 static struct file_operations proc_mounts_operations = {
676 .release = mounts_release,
680 extern struct seq_operations mountstats_op;
681 static int mountstats_open(struct inode *inode, struct file *file)
683 int ret = seq_open(file, &mountstats_op);
686 struct seq_file *m = file->private_data;
687 struct namespace *namespace = NULL;
688 struct task_struct *task = get_proc_task(inode);
692 namespace = task->namespace;
694 get_namespace(namespace);
696 put_task_struct(task);
700 m->private = namespace;
702 seq_release(inode, file);
709 static struct file_operations proc_mountstats_operations = {
710 .open = mountstats_open,
713 .release = mounts_release,
716 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
718 static ssize_t proc_info_read(struct file * file, char __user * buf,
719 size_t count, loff_t *ppos)
721 struct inode * inode = file->f_dentry->d_inode;
724 struct task_struct *task = get_proc_task(inode);
730 if (count > PROC_BLOCK_SIZE)
731 count = PROC_BLOCK_SIZE;
734 if (!(page = __get_free_page(GFP_KERNEL)))
737 length = PROC_I(inode)->op.proc_read(task, (char*)page);
740 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
743 put_task_struct(task);
748 static struct file_operations proc_info_file_operations = {
749 .read = proc_info_read,
752 static int mem_open(struct inode* inode, struct file* file)
754 file->private_data = (void*)((long)current->self_exec_id);
758 static ssize_t mem_read(struct file * file, char __user * buf,
759 size_t count, loff_t *ppos)
761 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
763 unsigned long src = *ppos;
765 struct mm_struct *mm;
770 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
774 page = (char *)__get_free_page(GFP_USER);
780 mm = get_task_mm(task);
786 if (file->private_data != (void*)((long)current->self_exec_id))
792 int this_len, retval;
794 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
795 retval = access_process_vm(task, src, page, this_len, 0);
796 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
802 if (copy_to_user(buf, page, retval)) {
817 free_page((unsigned long) page);
819 put_task_struct(task);
824 #define mem_write NULL
827 /* This is a security hazard */
828 static ssize_t mem_write(struct file * file, const char * buf,
829 size_t count, loff_t *ppos)
833 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
834 unsigned long dst = *ppos;
840 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
844 page = (char *)__get_free_page(GFP_USER);
849 int this_len, retval;
851 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
852 if (copy_from_user(page, buf, this_len)) {
856 retval = access_process_vm(task, dst, page, this_len, 1);
868 free_page((unsigned long) page);
870 put_task_struct(task);
876 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
880 file->f_pos = offset;
883 file->f_pos += offset;
888 force_successful_syscall_return();
892 static struct file_operations proc_mem_operations = {
899 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
900 size_t count, loff_t *ppos)
902 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
903 char buffer[PROC_NUMBUF];
906 loff_t __ppos = *ppos;
910 oom_adjust = task->oomkilladj;
911 put_task_struct(task);
913 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
916 if (count > len-__ppos)
918 if (copy_to_user(buf, buffer + __ppos, count))
920 *ppos = __ppos + count;
924 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
925 size_t count, loff_t *ppos)
927 struct task_struct *task;
928 char buffer[PROC_NUMBUF], *end;
931 if (!capable(CAP_SYS_RESOURCE))
933 memset(buffer, 0, sizeof(buffer));
934 if (count > sizeof(buffer) - 1)
935 count = sizeof(buffer) - 1;
936 if (copy_from_user(buffer, buf, count))
938 oom_adjust = simple_strtol(buffer, &end, 0);
939 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
943 task = get_proc_task(file->f_dentry->d_inode);
946 task->oomkilladj = oom_adjust;
947 put_task_struct(task);
948 if (end - buffer == 0)
953 static struct file_operations proc_oom_adjust_operations = {
954 .read = oom_adjust_read,
955 .write = oom_adjust_write,
958 #ifdef CONFIG_AUDITSYSCALL
960 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
961 size_t count, loff_t *ppos)
963 struct inode * inode = file->f_dentry->d_inode;
964 struct task_struct *task = get_proc_task(inode);
966 char tmpbuf[TMPBUFLEN];
970 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
971 audit_get_loginuid(task->audit_context));
972 put_task_struct(task);
973 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
976 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
977 size_t count, loff_t *ppos)
979 struct inode * inode = file->f_dentry->d_inode;
984 if (!capable(CAP_AUDIT_CONTROL))
987 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
990 if (count >= PAGE_SIZE)
991 count = PAGE_SIZE - 1;
994 /* No partial writes. */
997 page = (char*)__get_free_page(GFP_USER);
1001 if (copy_from_user(page, buf, count))
1005 loginuid = simple_strtoul(page, &tmp, 10);
1011 length = audit_set_loginuid(current, loginuid);
1012 if (likely(length == 0))
1016 free_page((unsigned long) page);
1020 static struct file_operations proc_loginuid_operations = {
1021 .read = proc_loginuid_read,
1022 .write = proc_loginuid_write,
1026 #ifdef CONFIG_SECCOMP
1027 static ssize_t seccomp_read(struct file *file, char __user *buf,
1028 size_t count, loff_t *ppos)
1030 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1032 loff_t __ppos = *ppos;
1037 /* no need to print the trailing zero, so use only len */
1038 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1039 put_task_struct(tsk);
1042 if (count > len - __ppos)
1043 count = len - __ppos;
1044 if (copy_to_user(buf, __buf + __ppos, count))
1046 *ppos = __ppos + count;
1050 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1051 size_t count, loff_t *ppos)
1053 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1054 char __buf[20], *end;
1055 unsigned int seccomp_mode;
1062 /* can set it only once to be even more secure */
1064 if (unlikely(tsk->seccomp.mode))
1068 memset(__buf, 0, sizeof(__buf));
1069 count = min(count, sizeof(__buf) - 1);
1070 if (copy_from_user(__buf, buf, count))
1073 seccomp_mode = simple_strtoul(__buf, &end, 0);
1077 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1078 tsk->seccomp.mode = seccomp_mode;
1079 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1083 if (unlikely(!(end - __buf)))
1085 result = end - __buf;
1087 put_task_struct(tsk);
1092 static struct file_operations proc_seccomp_operations = {
1093 .read = seccomp_read,
1094 .write = seccomp_write,
1096 #endif /* CONFIG_SECCOMP */
1098 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1100 struct inode *inode = dentry->d_inode;
1101 int error = -EACCES;
1103 /* We don't need a base pointer in the /proc filesystem */
1106 /* Are we allowed to snoop on the tasks file descriptors? */
1107 if (!proc_fd_access_allowed(inode))
1110 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1111 nd->last_type = LAST_BIND;
1113 return ERR_PTR(error);
1116 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1117 char __user *buffer, int buflen)
1119 struct inode * inode;
1120 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1126 inode = dentry->d_inode;
1127 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1128 len = PTR_ERR(path);
1131 len = tmp + PAGE_SIZE - 1 - path;
1135 if (copy_to_user(buffer, path, len))
1138 free_page((unsigned long)tmp);
1142 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1144 int error = -EACCES;
1145 struct inode *inode = dentry->d_inode;
1147 struct vfsmount *mnt = NULL;
1149 /* Are we allowed to snoop on the tasks file descriptors? */
1150 if (!proc_fd_access_allowed(inode))
1153 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1157 error = do_proc_readlink(de, mnt, buffer, buflen);
1164 static struct inode_operations proc_pid_link_inode_operations = {
1165 .readlink = proc_pid_readlink,
1166 .follow_link = proc_pid_follow_link,
1167 .setattr = proc_setattr,
1170 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1172 struct dentry *dentry = filp->f_dentry;
1173 struct inode *inode = dentry->d_inode;
1174 struct task_struct *p = get_proc_task(inode);
1175 unsigned int fd, tid, ino;
1177 char buf[PROC_NUMBUF];
1178 struct files_struct * files;
1179 struct fdtable *fdt;
1190 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1194 ino = parent_ino(dentry);
1195 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1199 files = get_files_struct(p);
1203 fdt = files_fdtable(files);
1204 for (fd = filp->f_pos-2;
1206 fd++, filp->f_pos++) {
1209 if (!fcheck_files(files, fd))
1217 buf[j] = '0' + (i % 10);
1221 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1222 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1229 put_files_struct(files);
1237 static int proc_pident_readdir(struct file *filp,
1238 void *dirent, filldir_t filldir,
1239 struct pid_entry *ents, unsigned int nents)
1243 struct dentry *dentry = filp->f_dentry;
1244 struct inode *inode = dentry->d_inode;
1245 struct task_struct *task = get_proc_task(inode);
1246 struct pid_entry *p;
1256 put_task_struct(task);
1261 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1267 ino = parent_ino(dentry);
1268 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1280 hide = vx_flags(VXF_INFO_HIDE, 0);
1284 case PROC_TGID_VX_INFO:
1285 case PROC_TGID_IP_INFO:
1289 if (filldir(dirent, p->name, p->len, filp->f_pos,
1290 fake_ino(pid, p->type), p->mode >> 12) < 0)
1303 static int proc_tgid_base_readdir(struct file * filp,
1304 void * dirent, filldir_t filldir)
1306 return proc_pident_readdir(filp,dirent,filldir,
1307 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1310 static int proc_tid_base_readdir(struct file * filp,
1311 void * dirent, filldir_t filldir)
1313 return proc_pident_readdir(filp,dirent,filldir,
1314 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1317 /* building an inode */
1319 static int task_dumpable(struct task_struct *task)
1322 struct mm_struct *mm;
1327 dumpable = mm->dumpable;
1335 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1337 struct inode * inode;
1338 struct proc_inode *ei;
1340 /* We need a new inode */
1342 inode = new_inode(sb);
1348 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1349 inode->i_ino = fake_ino(task->pid, ino);
1350 inode->i_op = &proc_def_inode_operations;
1353 * grab the reference to task.
1355 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1361 if (task_dumpable(task)) {
1362 inode->i_uid = task->euid;
1363 inode->i_gid = task->egid;
1365 inode->i_xid = vx_task_xid(task);
1366 security_task_to_inode(task, inode);
1379 * Exceptional case: normally we are not allowed to unhash a busy
1380 * directory. In this case, however, we can do it - no aliasing problems
1381 * due to the way we treat inodes.
1383 * Rewrite the inode's ownerships here because the owning task may have
1384 * performed a setuid(), etc.
1386 * Before the /proc/pid/status file was created the only way to read
1387 * the effective uid of a /process was to stat /proc/pid. Reading
1388 * /proc/pid/status is slow enough that procps and other packages
1389 * kept stating /proc/pid. To keep the rules in /proc simple I have
1390 * made this apply to all per process world readable and executable
1393 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1395 struct inode *inode = dentry->d_inode;
1396 struct task_struct *task = get_proc_task(inode);
1400 int pid = (inode->i_ino >> 16) & 0xFFFF;
1402 if (!proc_pid_visible(task, pid))
1406 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1407 task_dumpable(task)) {
1408 inode->i_uid = task->euid;
1409 inode->i_gid = task->egid;
1414 inode->i_mode &= ~(S_ISUID | S_ISGID);
1415 security_task_to_inode(task, inode);
1422 put_task_struct(task);
1426 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1428 struct inode *inode = dentry->d_inode;
1429 struct task_struct *task;
1430 generic_fillattr(inode, stat);
1435 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1437 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1438 task_dumpable(task)) {
1439 stat->uid = task->euid;
1440 stat->gid = task->egid;
1447 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1449 struct inode *inode = dentry->d_inode;
1450 struct task_struct *task = get_proc_task(inode);
1451 int fd = proc_fd(inode);
1452 struct files_struct *files;
1455 files = get_files_struct(task);
1458 if (fcheck_files(files, fd)) {
1460 put_files_struct(files);
1461 if (task_dumpable(task)) {
1462 inode->i_uid = task->euid;
1463 inode->i_gid = task->egid;
1468 inode->i_mode &= ~(S_ISUID | S_ISGID);
1469 security_task_to_inode(task, inode);
1470 put_task_struct(task);
1474 put_files_struct(files);
1476 put_task_struct(task);
1482 static int pid_delete_dentry(struct dentry * dentry)
1484 /* Is the task we represent dead?
1485 * If so, then don't put the dentry on the lru list,
1486 * kill it immediately.
1488 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1491 static struct dentry_operations tid_fd_dentry_operations =
1493 .d_revalidate = tid_fd_revalidate,
1494 .d_delete = pid_delete_dentry,
1497 static struct dentry_operations pid_dentry_operations =
1499 .d_revalidate = pid_revalidate,
1500 .d_delete = pid_delete_dentry,
1505 static unsigned name_to_int(struct dentry *dentry)
1507 const char *name = dentry->d_name.name;
1508 int len = dentry->d_name.len;
1511 if (len > 1 && *name == '0')
1514 unsigned c = *name++ - '0';
1517 if (n >= (~0U-9)/10)
1528 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1530 struct task_struct *task = get_proc_task(dir);
1531 unsigned fd = name_to_int(dentry);
1532 struct dentry *result = ERR_PTR(-ENOENT);
1534 struct files_struct * files;
1535 struct inode *inode;
1536 struct proc_inode *ei;
1543 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1548 files = get_files_struct(task);
1551 inode->i_mode = S_IFLNK;
1554 * We are not taking a ref to the file structure, so we must
1557 spin_lock(&files->file_lock);
1558 file = fcheck_files(files, fd);
1561 if (file->f_mode & 1)
1562 inode->i_mode |= S_IRUSR | S_IXUSR;
1563 if (file->f_mode & 2)
1564 inode->i_mode |= S_IWUSR | S_IXUSR;
1565 spin_unlock(&files->file_lock);
1566 put_files_struct(files);
1567 inode->i_op = &proc_pid_link_inode_operations;
1569 ei->op.proc_get_link = proc_fd_link;
1570 dentry->d_op = &tid_fd_dentry_operations;
1571 d_add(dentry, inode);
1572 /* Close the race of the process dying before we return the dentry */
1573 if (tid_fd_revalidate(dentry, NULL))
1576 put_task_struct(task);
1581 spin_unlock(&files->file_lock);
1582 put_files_struct(files);
1588 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1589 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1590 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
1592 static struct file_operations proc_fd_operations = {
1593 .read = generic_read_dir,
1594 .readdir = proc_readfd,
1597 static struct file_operations proc_task_operations = {
1598 .read = generic_read_dir,
1599 .readdir = proc_task_readdir,
1603 * proc directories can do almost nothing..
1605 static struct inode_operations proc_fd_inode_operations = {
1606 .lookup = proc_lookupfd,
1607 .setattr = proc_setattr,
1610 static struct inode_operations proc_task_inode_operations = {
1611 .lookup = proc_task_lookup,
1612 .getattr = proc_task_getattr,
1613 .setattr = proc_setattr,
1616 #ifdef CONFIG_SECURITY
1617 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1618 size_t count, loff_t *ppos)
1620 struct inode * inode = file->f_dentry->d_inode;
1623 struct task_struct *task = get_proc_task(inode);
1629 if (count > PAGE_SIZE)
1632 if (!(page = __get_free_page(GFP_KERNEL)))
1635 length = security_getprocattr(task,
1636 (char*)file->f_dentry->d_name.name,
1637 (void*)page, count);
1639 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1642 put_task_struct(task);
1647 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1648 size_t count, loff_t *ppos)
1650 struct inode * inode = file->f_dentry->d_inode;
1653 struct task_struct *task = get_proc_task(inode);
1658 if (count > PAGE_SIZE)
1661 /* No partial writes. */
1667 page = (char*)__get_free_page(GFP_USER);
1672 if (copy_from_user(page, buf, count))
1675 length = security_setprocattr(task,
1676 (char*)file->f_dentry->d_name.name,
1677 (void*)page, count);
1679 free_page((unsigned long) page);
1681 put_task_struct(task);
1686 static struct file_operations proc_pid_attr_operations = {
1687 .read = proc_pid_attr_read,
1688 .write = proc_pid_attr_write,
1691 static struct file_operations proc_tid_attr_operations;
1692 static struct inode_operations proc_tid_attr_inode_operations;
1693 static struct file_operations proc_tgid_attr_operations;
1694 static struct inode_operations proc_tgid_attr_inode_operations;
1697 extern int proc_pid_vx_info(struct task_struct *, char *);
1698 extern int proc_pid_nx_info(struct task_struct *, char *);
1701 static struct dentry *proc_pident_lookup(struct inode *dir,
1702 struct dentry *dentry,
1703 struct pid_entry *ents)
1705 struct inode *inode;
1706 struct dentry *error;
1707 struct task_struct *task = get_proc_task(dir);
1708 struct pid_entry *p;
1709 struct proc_inode *ei;
1711 error = ERR_PTR(-ENOENT);
1717 for (p = ents; p->name; p++) {
1718 if (p->len != dentry->d_name.len)
1720 if (!memcmp(dentry->d_name.name, p->name, p->len))
1727 case PROC_TID_VX_INFO:
1728 case PROC_TGID_VX_INFO:
1729 case PROC_TID_IP_INFO:
1730 case PROC_TGID_IP_INFO:
1731 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
1737 error = ERR_PTR(-EINVAL);
1738 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1743 inode->i_mode = p->mode;
1745 * Yes, it does not scale. And it should not. Don't add
1746 * new entries into /proc/<tgid>/ without very good reasons.
1749 case PROC_TGID_TASK:
1751 inode->i_op = &proc_task_inode_operations;
1752 inode->i_fop = &proc_task_operations;
1757 inode->i_op = &proc_fd_inode_operations;
1758 inode->i_fop = &proc_fd_operations;
1762 inode->i_op = &proc_pid_link_inode_operations;
1763 ei->op.proc_get_link = proc_exe_link;
1767 inode->i_op = &proc_pid_link_inode_operations;
1768 ei->op.proc_get_link = proc_cwd_link;
1771 case PROC_TGID_ROOT:
1772 inode->i_op = &proc_pid_link_inode_operations;
1773 ei->op.proc_get_link = proc_root_link;
1775 case PROC_TID_ENVIRON:
1776 case PROC_TGID_ENVIRON:
1777 inode->i_fop = &proc_info_file_operations;
1778 ei->op.proc_read = proc_pid_environ;
1781 case PROC_TGID_AUXV:
1782 inode->i_fop = &proc_info_file_operations;
1783 ei->op.proc_read = proc_pid_auxv;
1785 case PROC_TID_STATUS:
1786 case PROC_TGID_STATUS:
1787 inode->i_fop = &proc_info_file_operations;
1788 ei->op.proc_read = proc_pid_status;
1791 inode->i_fop = &proc_info_file_operations;
1792 ei->op.proc_read = proc_tid_stat;
1794 case PROC_TGID_STAT:
1795 inode->i_fop = &proc_info_file_operations;
1796 ei->op.proc_read = proc_tgid_stat;
1798 case PROC_TID_CMDLINE:
1799 case PROC_TGID_CMDLINE:
1800 inode->i_fop = &proc_info_file_operations;
1801 ei->op.proc_read = proc_pid_cmdline;
1803 case PROC_TID_STATM:
1804 case PROC_TGID_STATM:
1805 inode->i_fop = &proc_info_file_operations;
1806 ei->op.proc_read = proc_pid_statm;
1809 case PROC_TGID_MAPS:
1810 inode->i_fop = &proc_maps_operations;
1813 case PROC_TID_NUMA_MAPS:
1814 case PROC_TGID_NUMA_MAPS:
1815 inode->i_fop = &proc_numa_maps_operations;
1820 inode->i_fop = &proc_mem_operations;
1822 #ifdef CONFIG_SECCOMP
1823 case PROC_TID_SECCOMP:
1824 case PROC_TGID_SECCOMP:
1825 inode->i_fop = &proc_seccomp_operations;
1827 #endif /* CONFIG_SECCOMP */
1828 case PROC_TID_MOUNTS:
1829 case PROC_TGID_MOUNTS:
1830 inode->i_fop = &proc_mounts_operations;
1833 case PROC_TID_SMAPS:
1834 case PROC_TGID_SMAPS:
1835 inode->i_fop = &proc_smaps_operations;
1838 case PROC_TID_MOUNTSTATS:
1839 case PROC_TGID_MOUNTSTATS:
1840 inode->i_fop = &proc_mountstats_operations;
1842 #ifdef CONFIG_SECURITY
1845 inode->i_op = &proc_tid_attr_inode_operations;
1846 inode->i_fop = &proc_tid_attr_operations;
1848 case PROC_TGID_ATTR:
1850 inode->i_op = &proc_tgid_attr_inode_operations;
1851 inode->i_fop = &proc_tgid_attr_operations;
1853 case PROC_TID_ATTR_CURRENT:
1854 case PROC_TGID_ATTR_CURRENT:
1855 case PROC_TID_ATTR_PREV:
1856 case PROC_TGID_ATTR_PREV:
1857 case PROC_TID_ATTR_EXEC:
1858 case PROC_TGID_ATTR_EXEC:
1859 case PROC_TID_ATTR_FSCREATE:
1860 case PROC_TGID_ATTR_FSCREATE:
1861 case PROC_TID_ATTR_KEYCREATE:
1862 case PROC_TGID_ATTR_KEYCREATE:
1863 case PROC_TID_ATTR_SOCKCREATE:
1864 case PROC_TGID_ATTR_SOCKCREATE:
1865 inode->i_fop = &proc_pid_attr_operations;
1868 #ifdef CONFIG_KALLSYMS
1869 case PROC_TID_WCHAN:
1870 case PROC_TGID_WCHAN:
1871 inode->i_fop = &proc_info_file_operations;
1872 ei->op.proc_read = proc_pid_wchan;
1875 #ifdef CONFIG_SCHEDSTATS
1876 case PROC_TID_SCHEDSTAT:
1877 case PROC_TGID_SCHEDSTAT:
1878 inode->i_fop = &proc_info_file_operations;
1879 ei->op.proc_read = proc_pid_schedstat;
1882 #ifdef CONFIG_CPUSETS
1883 case PROC_TID_CPUSET:
1884 case PROC_TGID_CPUSET:
1885 inode->i_fop = &proc_cpuset_operations;
1888 case PROC_TID_OOM_SCORE:
1889 case PROC_TGID_OOM_SCORE:
1890 inode->i_fop = &proc_info_file_operations;
1891 ei->op.proc_read = proc_oom_score;
1893 case PROC_TID_OOM_ADJUST:
1894 case PROC_TGID_OOM_ADJUST:
1895 inode->i_fop = &proc_oom_adjust_operations;
1897 #ifdef CONFIG_AUDITSYSCALL
1898 case PROC_TID_LOGINUID:
1899 case PROC_TGID_LOGINUID:
1900 inode->i_fop = &proc_loginuid_operations;
1903 case PROC_TID_VX_INFO:
1904 case PROC_TGID_VX_INFO:
1905 inode->i_fop = &proc_info_file_operations;
1906 ei->op.proc_read = proc_pid_vx_info;
1908 case PROC_TID_IP_INFO:
1909 case PROC_TGID_IP_INFO:
1910 inode->i_fop = &proc_info_file_operations;
1911 ei->op.proc_read = proc_pid_nx_info;
1914 printk("procfs: impossible type (%d)",p->type);
1916 error = ERR_PTR(-EINVAL);
1919 dentry->d_op = &pid_dentry_operations;
1920 d_add(dentry, inode);
1921 /* Close the race of the process dying before we return the dentry */
1922 if (pid_revalidate(dentry, NULL))
1925 put_task_struct(task);
1930 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1931 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1934 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1935 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1938 static struct file_operations proc_tgid_base_operations = {
1939 .read = generic_read_dir,
1940 .readdir = proc_tgid_base_readdir,
1943 static struct file_operations proc_tid_base_operations = {
1944 .read = generic_read_dir,
1945 .readdir = proc_tid_base_readdir,
1948 static struct inode_operations proc_tgid_base_inode_operations = {
1949 .lookup = proc_tgid_base_lookup,
1950 .getattr = pid_getattr,
1951 .setattr = proc_setattr,
1954 static struct inode_operations proc_tid_base_inode_operations = {
1955 .lookup = proc_tid_base_lookup,
1956 .getattr = pid_getattr,
1957 .setattr = proc_setattr,
1960 #ifdef CONFIG_SECURITY
1961 static int proc_tgid_attr_readdir(struct file * filp,
1962 void * dirent, filldir_t filldir)
1964 return proc_pident_readdir(filp,dirent,filldir,
1965 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1968 static int proc_tid_attr_readdir(struct file * filp,
1969 void * dirent, filldir_t filldir)
1971 return proc_pident_readdir(filp,dirent,filldir,
1972 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1975 static struct file_operations proc_tgid_attr_operations = {
1976 .read = generic_read_dir,
1977 .readdir = proc_tgid_attr_readdir,
1980 static struct file_operations proc_tid_attr_operations = {
1981 .read = generic_read_dir,
1982 .readdir = proc_tid_attr_readdir,
1985 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1986 struct dentry *dentry, struct nameidata *nd)
1988 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1991 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1992 struct dentry *dentry, struct nameidata *nd)
1994 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1997 static struct inode_operations proc_tgid_attr_inode_operations = {
1998 .lookup = proc_tgid_attr_lookup,
1999 .getattr = pid_getattr,
2000 .setattr = proc_setattr,
2003 static struct inode_operations proc_tid_attr_inode_operations = {
2004 .lookup = proc_tid_attr_lookup,
2005 .getattr = pid_getattr,
2006 .setattr = proc_setattr,
2013 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2016 char tmp[PROC_NUMBUF];
2017 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
2018 return vfs_readlink(dentry,buffer,buflen,tmp);
2021 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2023 char tmp[PROC_NUMBUF];
2024 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
2025 return ERR_PTR(vfs_follow_link(nd,tmp));
2028 static struct inode_operations proc_self_inode_operations = {
2029 .readlink = proc_self_readlink,
2030 .follow_link = proc_self_follow_link,
2034 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2036 * @task: task that should be flushed.
2038 * Looks in the dcache for
2040 * /proc/@tgid/task/@pid
2041 * if either directory is present flushes it and all of it'ts children
2044 * It is safe and reasonable to cache /proc entries for a task until
2045 * that task exits. After that they just clog up the dcache with
2046 * useless entries, possibly causing useful dcache entries to be
2047 * flushed instead. This routine is proved to flush those useless
2048 * dcache entries at process exit time.
2050 * NOTE: This routine is just an optimization so it does not guarantee
2051 * that no dcache entries will exist at process exit time it
2052 * just makes it very unlikely that any will persist.
2054 void proc_flush_task(struct task_struct *task)
2056 struct dentry *dentry, *leader, *dir;
2057 char buf[PROC_NUMBUF];
2061 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2062 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2064 shrink_dcache_parent(dentry);
2069 if (thread_group_leader(task))
2073 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2074 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2079 name.len = strlen(name.name);
2080 dir = d_hash_and_lookup(leader, &name);
2082 goto out_put_leader;
2085 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2086 dentry = d_hash_and_lookup(dir, &name);
2088 shrink_dcache_parent(dentry);
2101 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2103 struct dentry *result = ERR_PTR(-ENOENT);
2104 struct task_struct *task;
2105 struct inode *inode;
2106 struct proc_inode *ei;
2109 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2110 inode = new_inode(dir->i_sb);
2112 return ERR_PTR(-ENOMEM);
2114 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2115 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2117 inode->i_mode = S_IFLNK|S_IRWXUGO;
2118 inode->i_uid = inode->i_gid = 0;
2120 inode->i_op = &proc_self_inode_operations;
2121 d_add(dentry, inode);
2124 tgid = name_to_int(dentry);
2129 task = find_proc_task_by_pid(tgid);
2131 get_task_struct(task);
2136 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2140 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2141 inode->i_op = &proc_tgid_base_inode_operations;
2142 inode->i_fop = &proc_tgid_base_operations;
2143 inode->i_flags|=S_IMMUTABLE;
2144 #ifdef CONFIG_SECURITY
2150 dentry->d_op = &pid_dentry_operations;
2152 d_add(dentry, inode);
2153 /* Close the race of the process dying before we return the dentry */
2154 if (pid_revalidate(dentry, NULL))
2158 put_task_struct(task);
2164 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2166 struct dentry *result = ERR_PTR(-ENOENT);
2167 struct task_struct *task;
2168 struct task_struct *leader = get_proc_task(dir);
2169 struct inode *inode;
2175 tid = name_to_int(dentry);
2178 if (vx_current_initpid(tid))
2182 task = find_proc_task_by_pid(tid);
2184 get_task_struct(task);
2188 if (leader->tgid != task->tgid)
2191 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2196 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2197 inode->i_op = &proc_tid_base_inode_operations;
2198 inode->i_fop = &proc_tid_base_operations;
2199 inode->i_flags|=S_IMMUTABLE;
2200 #ifdef CONFIG_SECURITY
2206 dentry->d_op = &pid_dentry_operations;
2208 d_add(dentry, inode);
2209 /* Close the race of the process dying before we return the dentry */
2210 if (pid_revalidate(dentry, NULL))
2214 put_task_struct(task);
2216 put_task_struct(leader);
2222 * Find the first tgid to return to user space.
2224 * Usually this is just whatever follows &init_task, but if the users
2225 * buffer was too small to hold the full list or there was a seek into
2226 * the middle of the directory we have more work to do.
2228 * In the case of a short read we start with find_task_by_pid.
2230 * In the case of a seek we start with &init_task and walk nr
2233 static struct task_struct *first_tgid(int tgid, unsigned int nr)
2235 struct task_struct *pos;
2238 pos = find_proc_task_by_pid(tgid);
2239 if (pos && thread_group_leader(pos))
2242 /* If nr exceeds the number of processes get out quickly */
2244 if (nr && nr >= nr_processes())
2247 /* If we haven't found our starting place yet start with
2248 * the init_task and walk nr tasks forward.
2250 for (pos = next_task(&init_task); nr > 0; --nr) {
2251 pos = next_task(pos);
2252 if (pos == &init_task) {
2258 get_task_struct(pos);
2265 * Find the next task in the task list.
2266 * Return NULL if we loop or there is any error.
2268 * The reference to the input task_struct is released.
2270 static struct task_struct *next_tgid(struct task_struct *start)
2272 struct task_struct *pos;
2275 if (pid_alive(start))
2276 pos = next_task(start);
2277 if (pid_alive(pos) && (pos != &init_task)) {
2278 get_task_struct(pos);
2284 put_task_struct(start);
2288 /* for the /proc/ directory itself, after non-process stuff has been done */
2289 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2291 char buf[PROC_NUMBUF];
2292 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2293 struct task_struct *task;
2297 ino_t ino = fake_ino(0,PROC_TGID_INO);
2298 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2305 /* f_version caches the tgid value that the last readdir call couldn't
2306 * return. lseek aka telldir automagically resets f_version to 0.
2308 tgid = filp->f_version;
2309 filp->f_version = 0;
2310 for (task = first_tgid(tgid, nr);
2312 task = next_tgid(task), filp->f_pos++) {
2315 tgid = vx_map_tgid(task->pid);
2316 if (!proc_pid_visible(task, tgid))
2319 len = snprintf(buf, sizeof(buf), "%d", tgid);
2320 ino = fake_ino(tgid, PROC_TGID_INO);
2321 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2322 /* returning this tgid failed, save it as the first
2323 * pid for the next readir call */
2324 filp->f_version = tgid;
2325 put_task_struct(task);
2333 * Find the first tid of a thread group to return to user space.
2335 * Usually this is just the thread group leader, but if the users
2336 * buffer was too small or there was a seek into the middle of the
2337 * directory we have more work todo.
2339 * In the case of a short read we start with find_task_by_pid.
2341 * In the case of a seek we start with the leader and walk nr
2344 static struct task_struct *first_tid(struct task_struct *leader,
2347 struct task_struct *pos;
2350 /* Attempt to start with the pid of a thread */
2351 if (tid && (nr > 0)) {
2352 pos = find_proc_task_by_pid(tid);
2353 if (pos && (pos->group_leader == leader))
2357 /* If nr exceeds the number of threads there is nothing todo */
2359 if (nr && nr >= get_nr_threads(leader))
2362 /* If we haven't found our starting place yet start
2363 * with the leader and walk nr threads forward.
2365 for (pos = leader; nr > 0; --nr) {
2366 pos = next_thread(pos);
2367 if (pos == leader) {
2373 get_task_struct(pos);
2380 * Find the next thread in the thread list.
2381 * Return NULL if there is an error or no next thread.
2383 * The reference to the input task_struct is released.
2385 static struct task_struct *next_tid(struct task_struct *start)
2387 struct task_struct *pos = NULL;
2389 if (pid_alive(start)) {
2390 pos = next_thread(start);
2391 if (thread_group_leader(pos))
2394 get_task_struct(pos);
2397 put_task_struct(start);
2401 /* for the /proc/TGID/task/ directories */
2402 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2404 char buf[PROC_NUMBUF];
2405 struct dentry *dentry = filp->f_dentry;
2406 struct inode *inode = dentry->d_inode;
2407 struct task_struct *leader = get_proc_task(inode);
2408 struct task_struct *task;
2409 int retval = -ENOENT;
2412 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2421 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2426 ino = parent_ino(dentry);
2427 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2433 /* f_version caches the tgid value that the last readdir call couldn't
2434 * return. lseek aka telldir automagically resets f_version to 0.
2436 tid = filp->f_version;
2437 filp->f_version = 0;
2438 for (task = first_tid(leader, tid, pos - 2);
2440 task = next_tid(task), pos++) {
2442 tid = vx_map_pid(task->pid);
2443 if (!proc_pid_visible(task, tid))
2446 len = snprintf(buf, sizeof(buf), "%d", tid);
2447 ino = fake_ino(tid, PROC_TID_INO);
2448 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2449 /* returning this tgid failed, save it as the first
2450 * pid for the next readir call */
2451 filp->f_version = tid;
2452 put_task_struct(task);
2458 put_task_struct(leader);
2463 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2465 struct inode *inode = dentry->d_inode;
2466 struct task_struct *p = get_proc_task(inode);
2467 generic_fillattr(inode, stat);
2471 stat->nlink += get_nr_threads(p);