4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/tracehook.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/vs_context.h>
76 #include <linux/vs_network.h>
81 * Implementing inode permission operations in /proc is almost
82 * certainly an error. Permission checks need to happen during
83 * each system call not at open time. The reason is that most of
84 * what we wish to check for permissions in /proc varies at runtime.
86 * The classic example of a problem is opening file descriptors
87 * in /proc for a task before it execs a suid executable.
91 * For hysterical raisins we keep the same inumbers as in the old procfs.
92 * Feel free to change the macro below - just keep the range distinct from
93 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
94 * As soon as we'll get a separate superblock we will be able to forget
95 * about magical ranges too.
98 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
100 enum pid_directory_inos {
105 #ifdef CONFIG_SECCOMP
120 PROC_TGID_MOUNTSTATS,
125 #ifdef CONFIG_SCHEDSTATS
128 #ifdef CONFIG_CPUSETS
131 #ifdef CONFIG_SECURITY
133 PROC_TGID_ATTR_CURRENT,
136 PROC_TGID_ATTR_FSCREATE,
137 PROC_TGID_ATTR_KEYCREATE,
138 PROC_TGID_ATTR_SOCKCREATE,
142 #ifdef CONFIG_AUDITSYSCALL
146 PROC_TGID_OOM_ADJUST,
150 #ifdef CONFIG_SECCOMP
170 #ifdef CONFIG_SCHEDSTATS
173 #ifdef CONFIG_CPUSETS
176 #ifdef CONFIG_SECURITY
178 PROC_TID_ATTR_CURRENT,
181 PROC_TID_ATTR_FSCREATE,
182 PROC_TID_ATTR_KEYCREATE,
183 PROC_TID_ATTR_SOCKCREATE,
187 #ifdef CONFIG_AUDITSYSCALL
193 /* Add new entries before this */
194 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
197 /* Worst case buffer size needed for holding an integer. */
198 #define PROC_NUMBUF 10
207 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
209 static struct pid_entry tgid_base_stuff[] = {
210 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
211 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
212 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
213 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
214 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
215 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
216 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
217 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
218 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
220 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
222 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
223 #ifdef CONFIG_SECCOMP
224 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
226 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
227 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
228 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
229 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
230 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
232 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUSR),
234 #ifdef CONFIG_SECURITY
235 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
237 #ifdef CONFIG_KALLSYMS
238 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
240 #ifdef CONFIG_SCHEDSTATS
241 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
243 #ifdef CONFIG_CPUSETS
244 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
246 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
247 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
248 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
249 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
250 #ifdef CONFIG_AUDITSYSCALL
251 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
255 static struct pid_entry tid_base_stuff[] = {
256 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
257 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
258 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
259 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
260 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
261 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
262 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
263 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
265 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
267 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
268 #ifdef CONFIG_SECCOMP
269 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
271 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
272 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
273 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
274 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
276 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUSR),
278 #ifdef CONFIG_SECURITY
279 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
281 #ifdef CONFIG_KALLSYMS
282 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
284 #ifdef CONFIG_SCHEDSTATS
285 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
287 #ifdef CONFIG_CPUSETS
288 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
290 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
291 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
292 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
293 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
294 #ifdef CONFIG_AUDITSYSCALL
295 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
300 #ifdef CONFIG_SECURITY
301 static struct pid_entry tgid_attr_stuff[] = {
302 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
303 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
304 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
305 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
306 E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
307 E(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
310 static struct pid_entry tid_attr_stuff[] = {
311 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
312 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
313 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
314 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
315 E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
316 E(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO),
323 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
325 struct task_struct *task = get_proc_task(inode);
326 struct files_struct *files = NULL;
328 int fd = proc_fd(inode);
331 files = get_files_struct(task);
332 put_task_struct(task);
336 * We are not taking a ref to the file structure, so we must
339 spin_lock(&files->file_lock);
340 file = fcheck_files(files, fd);
342 *mnt = mntget(file->f_vfsmnt);
343 *dentry = dget(file->f_dentry);
344 spin_unlock(&files->file_lock);
345 put_files_struct(files);
348 spin_unlock(&files->file_lock);
349 put_files_struct(files);
354 static struct fs_struct *get_fs_struct(struct task_struct *task)
356 struct fs_struct *fs;
360 atomic_inc(&fs->count);
365 static int get_nr_threads(struct task_struct *tsk)
367 /* Must be called with the rcu_read_lock held */
371 if (lock_task_sighand(tsk, &flags)) {
372 count = atomic_read(&tsk->signal->count);
373 unlock_task_sighand(tsk, &flags);
378 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
380 struct task_struct *task = get_proc_task(inode);
381 struct fs_struct *fs = NULL;
382 int result = -ENOENT;
385 fs = get_fs_struct(task);
386 put_task_struct(task);
389 read_lock(&fs->lock);
390 *mnt = mntget(fs->pwdmnt);
391 *dentry = dget(fs->pwd);
392 read_unlock(&fs->lock);
399 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
401 struct task_struct *task = get_proc_task(inode);
402 struct fs_struct *fs = NULL;
403 int result = -ENOENT;
406 fs = get_fs_struct(task);
407 put_task_struct(task);
410 read_lock(&fs->lock);
411 *mnt = mntget(fs->rootmnt);
412 *dentry = dget(fs->root);
413 read_unlock(&fs->lock);
420 struct mm_struct *mm_for_maps(struct task_struct *task)
422 struct mm_struct *mm = get_task_mm(task);
425 down_read(&mm->mmap_sem);
429 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
435 up_read(&mm->mmap_sem);
440 static int proc_pid_environ(struct task_struct *task, char * buffer)
443 struct mm_struct *mm = get_task_mm(task);
445 unsigned int len = mm->env_end - mm->env_start;
448 res = access_process_vm(task, mm->env_start, buffer, len, 0);
449 if (!ptrace_may_attach(task))
456 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
460 struct mm_struct *mm = get_task_mm(task);
464 goto out_mm; /* Shh! No looking before we're done */
466 len = mm->arg_end - mm->arg_start;
471 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
473 // If the nul at the end of args has been overwritten, then
474 // assume application is using setproctitle(3).
475 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
476 len = strnlen(buffer, res);
480 len = mm->env_end - mm->env_start;
481 if (len > PAGE_SIZE - res)
482 len = PAGE_SIZE - res;
483 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
484 res = strnlen(buffer, res);
493 static int proc_pid_auxv(struct task_struct *task, char *buffer)
496 struct mm_struct *mm = get_task_mm(task);
498 unsigned int nwords = 0;
501 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
502 res = nwords * sizeof(mm->saved_auxv[0]);
505 memcpy(buffer, mm->saved_auxv, res);
512 #ifdef CONFIG_KALLSYMS
514 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
515 * Returns the resolved symbol. If that fails, simply return the address.
517 static int proc_pid_wchan(struct task_struct *task, char *buffer)
520 const char *sym_name;
521 unsigned long wchan, size, offset;
522 char namebuf[KSYM_NAME_LEN+1];
524 wchan = get_wchan(task);
526 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
528 return sprintf(buffer, "%s", sym_name);
529 return sprintf(buffer, "%lu", wchan);
531 #endif /* CONFIG_KALLSYMS */
533 #ifdef CONFIG_SCHEDSTATS
535 * Provides /proc/PID/schedstat
537 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
539 return sprintf(buffer, "%lu %lu %lu\n",
540 task->sched_info.cpu_time,
541 task->sched_info.run_delay,
542 task->sched_info.pcnt);
546 /* The badness from the OOM killer */
547 unsigned long badness(struct task_struct *p, unsigned long uptime);
548 static int proc_oom_score(struct task_struct *task, char *buffer)
550 unsigned long points;
551 struct timespec uptime;
553 do_posix_clock_monotonic_gettime(&uptime);
554 points = badness(task, uptime.tv_sec);
555 return sprintf(buffer, "%lu\n", points);
558 /************************************************************************/
559 /* Here the fs part begins */
560 /************************************************************************/
562 /* permission checks */
563 static int proc_fd_access_allowed(struct inode *inode)
565 struct task_struct *task;
567 /* Allow access to a task's file descriptors if it is us or we
568 * may use ptrace attach to the process and find out that
571 task = get_proc_task(inode);
573 allowed = ptrace_may_attach(task);
574 put_task_struct(task);
579 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
582 struct inode *inode = dentry->d_inode;
584 if (attr->ia_valid & ATTR_MODE)
587 error = inode_change_ok(inode, attr);
589 error = security_inode_setattr(dentry, attr);
591 error = inode_setattr(inode, attr);
596 static struct inode_operations proc_def_inode_operations = {
597 .setattr = proc_setattr,
600 extern struct seq_operations mounts_op;
606 static int mounts_open(struct inode *inode, struct file *file)
608 struct task_struct *task = get_proc_task(inode);
609 struct namespace *namespace = NULL;
610 struct proc_mounts *p;
615 namespace = task->namespace;
617 get_namespace(namespace);
619 put_task_struct(task);
624 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
626 file->private_data = &p->m;
627 ret = seq_open(file, &mounts_op);
629 p->m.private = namespace;
630 p->event = namespace->event;
635 put_namespace(namespace);
640 static int mounts_release(struct inode *inode, struct file *file)
642 struct seq_file *m = file->private_data;
643 struct namespace *namespace = m->private;
644 put_namespace(namespace);
645 return seq_release(inode, file);
648 static unsigned mounts_poll(struct file *file, poll_table *wait)
650 struct proc_mounts *p = file->private_data;
651 struct namespace *ns = p->m.private;
654 poll_wait(file, &ns->poll, wait);
656 spin_lock(&vfsmount_lock);
657 if (p->event != ns->event) {
658 p->event = ns->event;
661 spin_unlock(&vfsmount_lock);
666 static struct file_operations proc_mounts_operations = {
670 .release = mounts_release,
674 extern struct seq_operations mountstats_op;
675 static int mountstats_open(struct inode *inode, struct file *file)
677 int ret = seq_open(file, &mountstats_op);
680 struct seq_file *m = file->private_data;
681 struct namespace *namespace = NULL;
682 struct task_struct *task = get_proc_task(inode);
686 namespace = task->namespace;
688 get_namespace(namespace);
690 put_task_struct(task);
694 m->private = namespace;
696 seq_release(inode, file);
703 static struct file_operations proc_mountstats_operations = {
704 .open = mountstats_open,
707 .release = mounts_release,
710 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
712 static ssize_t proc_info_read(struct file * file, char __user * buf,
713 size_t count, loff_t *ppos)
715 struct inode * inode = file->f_dentry->d_inode;
718 struct task_struct *task = get_proc_task(inode);
724 if (count > PROC_BLOCK_SIZE)
725 count = PROC_BLOCK_SIZE;
728 if (!(page = __get_free_page(GFP_KERNEL)))
731 length = PROC_I(inode)->op.proc_read(task, (char*)page);
734 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
737 put_task_struct(task);
742 static struct file_operations proc_info_file_operations = {
743 .read = proc_info_read,
746 static int mem_open(struct inode* inode, struct file* file)
748 file->private_data = (void*)((long)current->self_exec_id);
752 static ssize_t mem_read(struct file * file, char __user * buf,
753 size_t count, loff_t *ppos)
755 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
757 unsigned long src = *ppos;
759 struct mm_struct *mm;
764 if (!tracehook_allow_access_process_vm(task)
765 || !ptrace_may_attach(task))
769 page = (char *)__get_free_page(GFP_USER);
775 mm = get_task_mm(task);
781 if (file->private_data != (void*)((long)current->self_exec_id))
787 int this_len, retval;
789 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
790 retval = access_process_vm(task, src, page, this_len, 0);
791 if (!retval || !tracehook_allow_access_process_vm(task)
792 || !ptrace_may_attach(task)) {
798 if (copy_to_user(buf, page, retval)) {
813 free_page((unsigned long) page);
815 put_task_struct(task);
820 #define mem_write NULL
823 /* This is a security hazard */
824 static ssize_t mem_write(struct file * file, const char * buf,
825 size_t count, loff_t *ppos)
829 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
830 unsigned long dst = *ppos;
836 if (!tracehook_allow_access_process_vm(task)
837 || !ptrace_may_attach(task))
841 page = (char *)__get_free_page(GFP_USER);
846 int this_len, retval;
848 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
849 if (copy_from_user(page, buf, this_len)) {
853 retval = access_process_vm(task, dst, page, this_len, 1);
865 free_page((unsigned long) page);
867 put_task_struct(task);
873 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
877 file->f_pos = offset;
880 file->f_pos += offset;
885 force_successful_syscall_return();
889 static struct file_operations proc_mem_operations = {
896 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
897 size_t count, loff_t *ppos)
899 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
900 char buffer[PROC_NUMBUF];
903 loff_t __ppos = *ppos;
907 oom_adjust = task->oomkilladj;
908 put_task_struct(task);
910 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
913 if (count > len-__ppos)
915 if (copy_to_user(buf, buffer + __ppos, count))
917 *ppos = __ppos + count;
921 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
922 size_t count, loff_t *ppos)
924 struct task_struct *task;
925 char buffer[PROC_NUMBUF], *end;
928 if (!capable(CAP_SYS_RESOURCE))
930 memset(buffer, 0, sizeof(buffer));
931 if (count > sizeof(buffer) - 1)
932 count = sizeof(buffer) - 1;
933 if (copy_from_user(buffer, buf, count))
935 oom_adjust = simple_strtol(buffer, &end, 0);
936 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
940 task = get_proc_task(file->f_dentry->d_inode);
943 task->oomkilladj = oom_adjust;
944 put_task_struct(task);
945 if (end - buffer == 0)
950 static struct file_operations proc_oom_adjust_operations = {
951 .read = oom_adjust_read,
952 .write = oom_adjust_write,
955 #ifdef CONFIG_AUDITSYSCALL
957 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
958 size_t count, loff_t *ppos)
960 struct inode * inode = file->f_dentry->d_inode;
961 struct task_struct *task = get_proc_task(inode);
963 char tmpbuf[TMPBUFLEN];
967 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
968 audit_get_loginuid(task->audit_context));
969 put_task_struct(task);
970 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
973 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
974 size_t count, loff_t *ppos)
976 struct inode * inode = file->f_dentry->d_inode;
981 if (!capable(CAP_AUDIT_CONTROL))
984 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
987 if (count >= PAGE_SIZE)
988 count = PAGE_SIZE - 1;
991 /* No partial writes. */
994 page = (char*)__get_free_page(GFP_USER);
998 if (copy_from_user(page, buf, count))
1002 loginuid = simple_strtoul(page, &tmp, 10);
1008 length = audit_set_loginuid(current, loginuid);
1009 if (likely(length == 0))
1013 free_page((unsigned long) page);
1017 static struct file_operations proc_loginuid_operations = {
1018 .read = proc_loginuid_read,
1019 .write = proc_loginuid_write,
1023 #ifdef CONFIG_SECCOMP
1024 static ssize_t seccomp_read(struct file *file, char __user *buf,
1025 size_t count, loff_t *ppos)
1027 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1029 loff_t __ppos = *ppos;
1034 /* no need to print the trailing zero, so use only len */
1035 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1036 put_task_struct(tsk);
1039 if (count > len - __ppos)
1040 count = len - __ppos;
1041 if (copy_to_user(buf, __buf + __ppos, count))
1043 *ppos = __ppos + count;
1047 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1048 size_t count, loff_t *ppos)
1050 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1051 char __buf[20], *end;
1052 unsigned int seccomp_mode;
1059 /* can set it only once to be even more secure */
1061 if (unlikely(tsk->seccomp.mode))
1065 memset(__buf, 0, sizeof(__buf));
1066 count = min(count, sizeof(__buf) - 1);
1067 if (copy_from_user(__buf, buf, count))
1070 seccomp_mode = simple_strtoul(__buf, &end, 0);
1074 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1075 tsk->seccomp.mode = seccomp_mode;
1076 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1080 if (unlikely(!(end - __buf)))
1082 result = end - __buf;
1084 put_task_struct(tsk);
1089 static struct file_operations proc_seccomp_operations = {
1090 .read = seccomp_read,
1091 .write = seccomp_write,
1093 #endif /* CONFIG_SECCOMP */
1095 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1097 struct inode *inode = dentry->d_inode;
1098 int error = -EACCES;
1100 /* We don't need a base pointer in the /proc filesystem */
1103 /* Are we allowed to snoop on the tasks file descriptors? */
1104 if (!proc_fd_access_allowed(inode))
1107 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1108 nd->last_type = LAST_BIND;
1110 return ERR_PTR(error);
1113 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1114 char __user *buffer, int buflen)
1116 struct inode * inode;
1117 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1123 inode = dentry->d_inode;
1124 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1125 len = PTR_ERR(path);
1128 len = tmp + PAGE_SIZE - 1 - path;
1132 if (copy_to_user(buffer, path, len))
1135 free_page((unsigned long)tmp);
1139 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1141 int error = -EACCES;
1142 struct inode *inode = dentry->d_inode;
1144 struct vfsmount *mnt = NULL;
1146 /* Are we allowed to snoop on the tasks file descriptors? */
1147 if (!proc_fd_access_allowed(inode))
1150 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1154 error = do_proc_readlink(de, mnt, buffer, buflen);
1161 static struct inode_operations proc_pid_link_inode_operations = {
1162 .readlink = proc_pid_readlink,
1163 .follow_link = proc_pid_follow_link,
1164 .setattr = proc_setattr,
1167 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1169 struct dentry *dentry = filp->f_dentry;
1170 struct inode *inode = dentry->d_inode;
1171 struct task_struct *p = get_proc_task(inode);
1172 unsigned int fd, tid, ino;
1174 char buf[PROC_NUMBUF];
1175 struct files_struct * files;
1176 struct fdtable *fdt;
1187 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1191 ino = parent_ino(dentry);
1192 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1196 files = get_files_struct(p);
1200 fdt = files_fdtable(files);
1201 for (fd = filp->f_pos-2;
1203 fd++, filp->f_pos++) {
1206 if (!fcheck_files(files, fd))
1214 buf[j] = '0' + (i % 10);
1218 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1219 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1226 put_files_struct(files);
1234 static int proc_pident_readdir(struct file *filp,
1235 void *dirent, filldir_t filldir,
1236 struct pid_entry *ents, unsigned int nents)
1240 struct dentry *dentry = filp->f_dentry;
1241 struct inode *inode = dentry->d_inode;
1242 struct task_struct *task = get_proc_task(inode);
1243 struct pid_entry *p;
1253 put_task_struct(task);
1258 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1264 ino = parent_ino(dentry);
1265 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1277 hide = vx_flags(VXF_INFO_HIDE, 0);
1281 case PROC_TGID_VX_INFO:
1282 case PROC_TGID_IP_INFO:
1286 if (filldir(dirent, p->name, p->len, filp->f_pos,
1287 fake_ino(pid, p->type), p->mode >> 12) < 0)
1300 static int proc_tgid_base_readdir(struct file * filp,
1301 void * dirent, filldir_t filldir)
1303 return proc_pident_readdir(filp,dirent,filldir,
1304 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1307 static int proc_tid_base_readdir(struct file * filp,
1308 void * dirent, filldir_t filldir)
1310 return proc_pident_readdir(filp,dirent,filldir,
1311 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1314 /* building an inode */
1316 static int task_dumpable(struct task_struct *task)
1319 struct mm_struct *mm;
1324 dumpable = mm->dumpable;
1332 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1334 struct inode * inode;
1335 struct proc_inode *ei;
1337 /* We need a new inode */
1339 inode = new_inode(sb);
1345 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1346 inode->i_ino = fake_ino(task->pid, ino);
1347 inode->i_op = &proc_def_inode_operations;
1350 * grab the reference to task.
1352 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1358 if (task_dumpable(task)) {
1359 inode->i_uid = task->euid;
1360 inode->i_gid = task->egid;
1362 inode->i_xid = vx_task_xid(task);
1363 security_task_to_inode(task, inode);
1376 * Exceptional case: normally we are not allowed to unhash a busy
1377 * directory. In this case, however, we can do it - no aliasing problems
1378 * due to the way we treat inodes.
1380 * Rewrite the inode's ownerships here because the owning task may have
1381 * performed a setuid(), etc.
1383 * Before the /proc/pid/status file was created the only way to read
1384 * the effective uid of a /process was to stat /proc/pid. Reading
1385 * /proc/pid/status is slow enough that procps and other packages
1386 * kept stating /proc/pid. To keep the rules in /proc simple I have
1387 * made this apply to all per process world readable and executable
1390 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1392 struct inode *inode = dentry->d_inode;
1393 struct task_struct *task = get_proc_task(inode);
1397 int pid = (inode->i_ino >> 16) & 0xFFFF;
1399 if (!vx_proc_pid_visible(task, pid))
1403 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1404 task_dumpable(task)) {
1405 inode->i_uid = task->euid;
1406 inode->i_gid = task->egid;
1411 inode->i_mode &= ~(S_ISUID | S_ISGID);
1412 security_task_to_inode(task, inode);
1419 put_task_struct(task);
1423 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1425 struct inode *inode = dentry->d_inode;
1426 struct task_struct *task;
1427 generic_fillattr(inode, stat);
1432 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1434 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1435 task_dumpable(task)) {
1436 stat->uid = task->euid;
1437 stat->gid = task->egid;
1444 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1446 struct inode *inode = dentry->d_inode;
1447 struct task_struct *task = get_proc_task(inode);
1448 int fd = proc_fd(inode);
1449 struct files_struct *files;
1452 files = get_files_struct(task);
1455 if (fcheck_files(files, fd)) {
1457 put_files_struct(files);
1458 if (task_dumpable(task)) {
1459 inode->i_uid = task->euid;
1460 inode->i_gid = task->egid;
1465 inode->i_mode &= ~(S_ISUID | S_ISGID);
1466 security_task_to_inode(task, inode);
1467 put_task_struct(task);
1471 put_files_struct(files);
1473 put_task_struct(task);
1479 static int pid_delete_dentry(struct dentry * dentry)
1481 /* Is the task we represent dead?
1482 * If so, then don't put the dentry on the lru list,
1483 * kill it immediately.
1485 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1488 static struct dentry_operations tid_fd_dentry_operations =
1490 .d_revalidate = tid_fd_revalidate,
1491 .d_delete = pid_delete_dentry,
1494 static struct dentry_operations pid_dentry_operations =
1496 .d_revalidate = pid_revalidate,
1497 .d_delete = pid_delete_dentry,
1502 static unsigned name_to_int(struct dentry *dentry)
1504 const char *name = dentry->d_name.name;
1505 int len = dentry->d_name.len;
1508 if (len > 1 && *name == '0')
1511 unsigned c = *name++ - '0';
1514 if (n >= (~0U-9)/10)
1525 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1527 struct task_struct *task = get_proc_task(dir);
1528 unsigned fd = name_to_int(dentry);
1529 struct dentry *result = ERR_PTR(-ENOENT);
1531 struct files_struct * files;
1532 struct inode *inode;
1533 struct proc_inode *ei;
1540 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1545 files = get_files_struct(task);
1548 inode->i_mode = S_IFLNK;
1551 * We are not taking a ref to the file structure, so we must
1554 spin_lock(&files->file_lock);
1555 file = fcheck_files(files, fd);
1558 if (file->f_mode & 1)
1559 inode->i_mode |= S_IRUSR | S_IXUSR;
1560 if (file->f_mode & 2)
1561 inode->i_mode |= S_IWUSR | S_IXUSR;
1562 spin_unlock(&files->file_lock);
1563 put_files_struct(files);
1564 inode->i_op = &proc_pid_link_inode_operations;
1566 ei->op.proc_get_link = proc_fd_link;
1567 dentry->d_op = &tid_fd_dentry_operations;
1568 d_add(dentry, inode);
1569 /* Close the race of the process dying before we return the dentry */
1570 if (tid_fd_revalidate(dentry, NULL))
1573 put_task_struct(task);
1578 spin_unlock(&files->file_lock);
1579 put_files_struct(files);
1585 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1586 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1587 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
1589 static struct file_operations proc_fd_operations = {
1590 .read = generic_read_dir,
1591 .readdir = proc_readfd,
1594 static struct file_operations proc_task_operations = {
1595 .read = generic_read_dir,
1596 .readdir = proc_task_readdir,
1600 * proc directories can do almost nothing..
1602 static struct inode_operations proc_fd_inode_operations = {
1603 .lookup = proc_lookupfd,
1604 .setattr = proc_setattr,
1607 static struct inode_operations proc_task_inode_operations = {
1608 .lookup = proc_task_lookup,
1609 .getattr = proc_task_getattr,
1610 .setattr = proc_setattr,
1613 #ifdef CONFIG_SECURITY
1614 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1615 size_t count, loff_t *ppos)
1617 struct inode * inode = file->f_dentry->d_inode;
1620 struct task_struct *task = get_proc_task(inode);
1626 if (count > PAGE_SIZE)
1629 if (!(page = __get_free_page(GFP_KERNEL)))
1632 length = security_getprocattr(task,
1633 (char*)file->f_dentry->d_name.name,
1634 (void*)page, count);
1636 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1639 put_task_struct(task);
1644 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1645 size_t count, loff_t *ppos)
1647 struct inode * inode = file->f_dentry->d_inode;
1650 struct task_struct *task = get_proc_task(inode);
1655 if (count > PAGE_SIZE)
1658 /* No partial writes. */
1664 page = (char*)__get_free_page(GFP_USER);
1669 if (copy_from_user(page, buf, count))
1672 length = security_setprocattr(task,
1673 (char*)file->f_dentry->d_name.name,
1674 (void*)page, count);
1676 free_page((unsigned long) page);
1678 put_task_struct(task);
1683 static struct file_operations proc_pid_attr_operations = {
1684 .read = proc_pid_attr_read,
1685 .write = proc_pid_attr_write,
1688 static struct file_operations proc_tid_attr_operations;
1689 static struct inode_operations proc_tid_attr_inode_operations;
1690 static struct file_operations proc_tgid_attr_operations;
1691 static struct inode_operations proc_tgid_attr_inode_operations;
1694 extern int proc_pid_vx_info(struct task_struct *, char *);
1695 extern int proc_pid_nx_info(struct task_struct *, char *);
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 = vx_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 = vx_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 = vx_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 (!vx_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 = vx_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 (!vx_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);