4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/config.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/string.h>
61 #include <linux/seq_file.h>
62 #include <linux/namei.h>
63 #include <linux/namespace.h>
65 #include <linux/smp_lock.h>
66 #include <linux/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
76 #include <linux/vs_cvirt.h>
77 #include <linux/vs_network.h>
81 * For hysterical raisins we keep the same inumbers as in the old procfs.
82 * Feel free to change the macro below - just keep the range distinct from
83 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
84 * As soon as we'll get a separate superblock we will be able to forget
85 * about magical ranges too.
88 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
90 enum pid_directory_inos {
110 PROC_TGID_MOUNTSTATS,
115 #ifdef CONFIG_SCHEDSTATS
118 #ifdef CONFIG_CPUSETS
121 #ifdef CONFIG_SECURITY
123 PROC_TGID_ATTR_CURRENT,
126 PROC_TGID_ATTR_FSCREATE,
130 #ifdef CONFIG_AUDITSYSCALL
134 PROC_TGID_OOM_ADJUST,
138 #ifdef CONFIG_SECCOMP
158 #ifdef CONFIG_SCHEDSTATS
161 #ifdef CONFIG_CPUSETS
164 #ifdef CONFIG_SECURITY
166 PROC_TID_ATTR_CURRENT,
169 PROC_TID_ATTR_FSCREATE,
173 #ifdef CONFIG_AUDITSYSCALL
179 /* Add new entries before this */
180 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
190 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
192 static struct pid_entry tgid_base_stuff[] = {
193 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
194 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
195 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
196 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
197 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
198 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
199 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
200 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
201 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUSR),
203 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
205 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
206 #ifdef CONFIG_SECCOMP
207 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
209 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
210 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
211 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
212 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
213 E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
215 E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUSR),
217 #ifdef CONFIG_SECURITY
218 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
220 #ifdef CONFIG_KALLSYMS
221 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
223 #ifdef CONFIG_SCHEDSTATS
224 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
226 #ifdef CONFIG_CPUSETS
227 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
229 E(PROC_TGID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
230 E(PROC_TGID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
231 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
232 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
233 #ifdef CONFIG_AUDITSYSCALL
234 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
238 static struct pid_entry tid_base_stuff[] = {
239 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
240 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
241 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
242 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
243 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
244 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
245 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
246 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUSR),
248 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
250 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
251 #ifdef CONFIG_SECCOMP
252 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
254 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
255 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
256 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
257 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
259 E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUSR),
261 #ifdef CONFIG_SECURITY
262 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
264 #ifdef CONFIG_KALLSYMS
265 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
267 #ifdef CONFIG_SCHEDSTATS
268 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
270 #ifdef CONFIG_CPUSETS
271 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
273 E(PROC_TID_VX_INFO, "vinfo", S_IFREG|S_IRUGO),
274 E(PROC_TID_IP_INFO, "ninfo", S_IFREG|S_IRUGO),
275 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
276 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
277 #ifdef CONFIG_AUDITSYSCALL
278 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
283 #ifdef CONFIG_SECURITY
284 static struct pid_entry tgid_attr_stuff[] = {
285 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
286 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
287 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
288 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
291 static struct pid_entry tid_attr_stuff[] = {
292 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
293 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
294 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
295 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
302 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
304 struct task_struct *task = proc_task(inode);
305 struct files_struct *files;
307 int fd = proc_type(inode) - PROC_TID_FD_DIR;
309 files = get_files_struct(task);
312 * We are not taking a ref to the file structure, so we must
315 spin_lock(&files->file_lock);
316 file = fcheck_files(files, fd);
318 *mnt = mntget(file->f_vfsmnt);
319 *dentry = dget(file->f_dentry);
320 spin_unlock(&files->file_lock);
321 put_files_struct(files);
324 spin_unlock(&files->file_lock);
325 put_files_struct(files);
330 static struct fs_struct *get_fs_struct(struct task_struct *task)
332 struct fs_struct *fs;
336 atomic_inc(&fs->count);
341 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
343 struct fs_struct *fs = get_fs_struct(proc_task(inode));
344 int result = -ENOENT;
346 read_lock(&fs->lock);
347 *mnt = mntget(fs->pwdmnt);
348 *dentry = dget(fs->pwd);
349 read_unlock(&fs->lock);
356 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
358 struct fs_struct *fs = get_fs_struct(proc_task(inode));
359 int result = -ENOENT;
361 read_lock(&fs->lock);
362 *mnt = mntget(fs->rootmnt);
363 *dentry = dget(fs->root);
364 read_unlock(&fs->lock);
372 /* Same as proc_root_link, but this addionally tries to get fs from other
373 * threads in the group */
374 static int proc_task_root_link(struct inode *inode, struct dentry **dentry,
375 struct vfsmount **mnt)
377 struct fs_struct *fs;
378 int result = -ENOENT;
379 struct task_struct *leader = proc_task(inode);
384 atomic_inc(&fs->count);
387 /* Try to get fs from other threads */
389 read_lock(&tasklist_lock);
390 if (pid_alive(leader)) {
391 struct task_struct *task = leader;
393 while ((task = next_thread(task)) != leader) {
397 atomic_inc(&fs->count);
404 read_unlock(&tasklist_lock);
408 read_lock(&fs->lock);
409 *mnt = mntget(fs->rootmnt);
410 *dentry = dget(fs->root);
411 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))
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 */
570 /* If the process being read is separated by chroot from the reading process,
571 * don't let the reader access the threads.
573 * note: this does dput(root) and mntput(vfsmnt) on exit.
575 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
577 struct dentry *de, *base;
578 struct vfsmount *our_vfsmnt, *mnt;
581 /* context admin override */
582 if (capable(CAP_CONTEXT))
585 read_lock(¤t->fs->lock);
586 our_vfsmnt = mntget(current->fs->rootmnt);
587 base = dget(current->fs->root);
588 read_unlock(¤t->fs->lock);
590 spin_lock(&vfsmount_lock);
594 while (mnt != our_vfsmnt) {
595 if (mnt == mnt->mnt_parent)
597 de = mnt->mnt_mountpoint;
598 mnt = mnt->mnt_parent;
601 if (!is_subdir(de, base))
603 spin_unlock(&vfsmount_lock);
613 spin_unlock(&vfsmount_lock);
618 static int proc_check_root(struct inode *inode)
621 struct vfsmount *vfsmnt;
623 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
625 return proc_check_chroot(root, vfsmnt);
628 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
630 if (generic_permission(inode, mask, NULL) != 0)
632 return proc_check_root(inode);
635 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
638 struct inode *inode = dentry->d_inode;
640 if (attr->ia_valid & ATTR_MODE)
643 error = inode_change_ok(inode, attr);
645 error = security_inode_setattr(dentry, attr);
647 error = inode_setattr(inode, attr);
652 static struct inode_operations proc_def_inode_operations = {
653 .setattr = proc_setattr,
656 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
659 struct vfsmount *vfsmnt;
661 if (generic_permission(inode, mask, NULL) != 0)
664 if (proc_task_root_link(inode, &root, &vfsmnt))
667 return proc_check_chroot(root, vfsmnt);
670 extern struct seq_operations proc_pid_maps_op;
671 static int maps_open(struct inode *inode, struct file *file)
673 struct task_struct *task = proc_task(inode);
674 int ret = seq_open(file, &proc_pid_maps_op);
676 struct seq_file *m = file->private_data;
682 static struct file_operations proc_maps_operations = {
686 .release = seq_release,
690 extern struct seq_operations proc_pid_numa_maps_op;
691 static int numa_maps_open(struct inode *inode, struct file *file)
693 struct task_struct *task = proc_task(inode);
694 int ret = seq_open(file, &proc_pid_numa_maps_op);
696 struct seq_file *m = file->private_data;
702 static struct file_operations proc_numa_maps_operations = {
703 .open = numa_maps_open,
706 .release = seq_release,
711 extern struct seq_operations proc_pid_smaps_op;
712 static int smaps_open(struct inode *inode, struct file *file)
714 struct task_struct *task = proc_task(inode);
715 int ret = seq_open(file, &proc_pid_smaps_op);
717 struct seq_file *m = file->private_data;
723 static struct file_operations proc_smaps_operations = {
727 .release = seq_release,
731 extern struct seq_operations mounts_op;
737 static int mounts_open(struct inode *inode, struct file *file)
739 struct task_struct *task = proc_task(inode);
740 struct namespace *namespace;
741 struct proc_mounts *p;
745 namespace = task->namespace;
747 get_namespace(namespace);
752 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
754 file->private_data = &p->m;
755 ret = seq_open(file, &mounts_op);
757 p->m.private = namespace;
758 p->event = namespace->event;
763 put_namespace(namespace);
768 static int mounts_release(struct inode *inode, struct file *file)
770 struct seq_file *m = file->private_data;
771 struct namespace *namespace = m->private;
772 put_namespace(namespace);
773 return seq_release(inode, file);
776 static unsigned mounts_poll(struct file *file, poll_table *wait)
778 struct proc_mounts *p = file->private_data;
779 struct namespace *ns = p->m.private;
782 poll_wait(file, &ns->poll, wait);
784 spin_lock(&vfsmount_lock);
785 if (p->event != ns->event) {
786 p->event = ns->event;
789 spin_unlock(&vfsmount_lock);
794 static struct file_operations proc_mounts_operations = {
798 .release = mounts_release,
802 extern struct seq_operations mountstats_op;
803 static int mountstats_open(struct inode *inode, struct file *file)
805 struct task_struct *task = proc_task(inode);
806 int ret = seq_open(file, &mountstats_op);
809 struct seq_file *m = file->private_data;
810 struct namespace *namespace;
812 namespace = task->namespace;
814 get_namespace(namespace);
818 m->private = namespace;
820 seq_release(inode, file);
827 static struct file_operations proc_mountstats_operations = {
828 .open = mountstats_open,
831 .release = mounts_release,
834 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
836 static ssize_t proc_info_read(struct file * file, char __user * buf,
837 size_t count, loff_t *ppos)
839 struct inode * inode = file->f_dentry->d_inode;
842 struct task_struct *task = proc_task(inode);
844 if (count > PROC_BLOCK_SIZE)
845 count = PROC_BLOCK_SIZE;
846 if (!(page = __get_free_page(GFP_KERNEL)))
849 length = PROC_I(inode)->op.proc_read(task, (char*)page);
852 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
857 static struct file_operations proc_info_file_operations = {
858 .read = proc_info_read,
861 static int mem_open(struct inode* inode, struct file* file)
863 file->private_data = (void*)((long)current->self_exec_id);
867 static ssize_t mem_read(struct file * file, char __user * buf,
868 size_t count, loff_t *ppos)
870 struct task_struct *task = proc_task(file->f_dentry->d_inode);
872 unsigned long src = *ppos;
874 struct mm_struct *mm;
876 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
880 page = (char *)__get_free_page(GFP_USER);
886 mm = get_task_mm(task);
892 if (file->private_data != (void*)((long)current->self_exec_id))
898 int this_len, retval;
900 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
901 retval = access_process_vm(task, src, page, this_len, 0);
902 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
908 if (copy_to_user(buf, page, retval)) {
923 free_page((unsigned long) page);
928 #define mem_write NULL
931 /* This is a security hazard */
932 static ssize_t mem_write(struct file * file, const char * buf,
933 size_t count, loff_t *ppos)
937 struct task_struct *task = proc_task(file->f_dentry->d_inode);
938 unsigned long dst = *ppos;
940 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
943 page = (char *)__get_free_page(GFP_USER);
948 int this_len, retval;
950 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
951 if (copy_from_user(page, buf, this_len)) {
955 retval = access_process_vm(task, dst, page, this_len, 1);
967 free_page((unsigned long) page);
972 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
976 file->f_pos = offset;
979 file->f_pos += offset;
984 force_successful_syscall_return();
988 static struct file_operations proc_mem_operations = {
995 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
996 size_t count, loff_t *ppos)
998 struct task_struct *task = proc_task(file->f_dentry->d_inode);
1001 int oom_adjust = task->oomkilladj;
1002 loff_t __ppos = *ppos;
1004 len = sprintf(buffer, "%i\n", oom_adjust);
1007 if (count > len-__ppos)
1009 if (copy_to_user(buf, buffer + __ppos, count))
1011 *ppos = __ppos + count;
1015 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1016 size_t count, loff_t *ppos)
1018 struct task_struct *task = proc_task(file->f_dentry->d_inode);
1019 char buffer[8], *end;
1022 if (!capable(CAP_SYS_RESOURCE))
1024 memset(buffer, 0, 8);
1027 if (copy_from_user(buffer, buf, count))
1029 oom_adjust = simple_strtol(buffer, &end, 0);
1030 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
1034 task->oomkilladj = oom_adjust;
1035 if (end - buffer == 0)
1037 return end - buffer;
1040 static struct file_operations proc_oom_adjust_operations = {
1041 .read = oom_adjust_read,
1042 .write = oom_adjust_write,
1045 static struct inode_operations proc_mem_inode_operations = {
1046 .permission = proc_permission,
1047 .setattr = proc_setattr,
1050 #ifdef CONFIG_AUDITSYSCALL
1051 #define TMPBUFLEN 21
1052 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1053 size_t count, loff_t *ppos)
1055 struct inode * inode = file->f_dentry->d_inode;
1056 struct task_struct *task = proc_task(inode);
1058 char tmpbuf[TMPBUFLEN];
1060 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1061 audit_get_loginuid(task->audit_context));
1062 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1065 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1066 size_t count, loff_t *ppos)
1068 struct inode * inode = file->f_dentry->d_inode;
1071 struct task_struct *task = proc_task(inode);
1074 if (!capable(CAP_AUDIT_CONTROL))
1077 if (current != task)
1080 if (count > PAGE_SIZE)
1084 /* No partial writes. */
1087 page = (char*)__get_free_page(GFP_USER);
1091 if (copy_from_user(page, buf, count))
1094 loginuid = simple_strtoul(page, &tmp, 10);
1100 length = audit_set_loginuid(task, loginuid);
1101 if (likely(length == 0))
1105 free_page((unsigned long) page);
1109 static struct file_operations proc_loginuid_operations = {
1110 .read = proc_loginuid_read,
1111 .write = proc_loginuid_write,
1115 #ifdef CONFIG_SECCOMP
1116 static ssize_t seccomp_read(struct file *file, char __user *buf,
1117 size_t count, loff_t *ppos)
1119 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1121 loff_t __ppos = *ppos;
1124 /* no need to print the trailing zero, so use only len */
1125 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1128 if (count > len - __ppos)
1129 count = len - __ppos;
1130 if (copy_to_user(buf, __buf + __ppos, count))
1132 *ppos = __ppos + count;
1136 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1137 size_t count, loff_t *ppos)
1139 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1140 char __buf[20], *end;
1141 unsigned int seccomp_mode;
1143 /* can set it only once to be even more secure */
1144 if (unlikely(tsk->seccomp.mode))
1147 memset(__buf, 0, sizeof(__buf));
1148 count = min(count, sizeof(__buf) - 1);
1149 if (copy_from_user(__buf, buf, count))
1151 seccomp_mode = simple_strtoul(__buf, &end, 0);
1154 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1155 tsk->seccomp.mode = seccomp_mode;
1156 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1159 if (unlikely(!(end - __buf)))
1164 static struct file_operations proc_seccomp_operations = {
1165 .read = seccomp_read,
1166 .write = seccomp_write,
1168 #endif /* CONFIG_SECCOMP */
1170 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1172 struct inode *inode = dentry->d_inode;
1173 int error = -EACCES;
1175 /* We don't need a base pointer in the /proc filesystem */
1178 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1180 error = proc_check_root(inode);
1184 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1185 nd->last_type = LAST_BIND;
1187 return ERR_PTR(error);
1190 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1191 char __user *buffer, int buflen)
1193 struct inode * inode;
1194 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1200 inode = dentry->d_inode;
1201 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1202 len = PTR_ERR(path);
1205 len = tmp + PAGE_SIZE - 1 - path;
1209 if (copy_to_user(buffer, path, len))
1212 free_page((unsigned long)tmp);
1216 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1218 int error = -EACCES;
1219 struct inode *inode = dentry->d_inode;
1221 struct vfsmount *mnt = NULL;
1225 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1227 error = proc_check_root(inode);
1231 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1235 error = do_proc_readlink(de, mnt, buffer, buflen);
1243 static struct inode_operations proc_pid_link_inode_operations = {
1244 .readlink = proc_pid_readlink,
1245 .follow_link = proc_pid_follow_link,
1246 .setattr = proc_setattr,
1251 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1253 struct inode *inode = filp->f_dentry->d_inode;
1254 struct task_struct *p = proc_task(inode);
1255 unsigned int fd, tid, ino;
1258 struct files_struct * files;
1259 struct fdtable *fdt;
1270 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1274 ino = fake_ino(tid, PROC_TID_INO);
1275 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1279 files = get_files_struct(p);
1283 fdt = files_fdtable(files);
1284 for (fd = filp->f_pos-2;
1286 fd++, filp->f_pos++) {
1289 if (!fcheck_files(files, fd))
1297 buf[j] = '0' + (i % 10);
1301 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1302 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1309 put_files_struct(files);
1315 static int proc_pident_readdir(struct file *filp,
1316 void *dirent, filldir_t filldir,
1317 struct pid_entry *ents, unsigned int nents)
1321 struct dentry *dentry = filp->f_dentry;
1322 struct inode *inode = dentry->d_inode;
1323 struct pid_entry *p;
1328 if (!pid_alive(proc_task(inode)))
1332 pid = proc_task(inode)->pid;
1337 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1343 ino = parent_ino(dentry);
1344 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1356 hide = vx_flags(VXF_INFO_HIDE, 0);
1360 case PROC_TGID_VX_INFO:
1361 case PROC_TGID_IP_INFO:
1365 if (filldir(dirent, p->name, p->len, filp->f_pos,
1366 fake_ino(pid, p->type), p->mode >> 12) < 0)
1379 static int proc_tgid_base_readdir(struct file * filp,
1380 void * dirent, filldir_t filldir)
1382 return proc_pident_readdir(filp,dirent,filldir,
1383 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1386 static int proc_tid_base_readdir(struct file * filp,
1387 void * dirent, filldir_t filldir)
1389 return proc_pident_readdir(filp,dirent,filldir,
1390 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1393 /* building an inode */
1395 static int task_dumpable(struct task_struct *task)
1398 struct mm_struct *mm;
1403 dumpable = mm->dumpable;
1411 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1413 struct inode * inode;
1414 struct proc_inode *ei;
1416 /* We need a new inode */
1418 inode = new_inode(sb);
1425 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1426 inode->i_ino = fake_ino(task->pid, ino);
1427 inode->i_op = &proc_def_inode_operations;
1429 if (!pid_alive(task))
1433 * grab the reference to task.
1435 get_task_struct(task);
1440 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1441 inode->i_uid = task->euid;
1442 inode->i_gid = task->egid;
1444 inode->i_xid = vx_task_xid(task);
1445 security_task_to_inode(task, inode);
1459 * Exceptional case: normally we are not allowed to unhash a busy
1460 * directory. In this case, however, we can do it - no aliasing problems
1461 * due to the way we treat inodes.
1463 * Rewrite the inode's ownerships here because the owning task may have
1464 * performed a setuid(), etc.
1466 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1468 struct inode *inode = dentry->d_inode;
1469 struct task_struct *task = proc_task(inode);
1471 if (!vx_check(vx_task_xid(task), VX_IDENT))
1473 /* discard wrong fakeinit */
1475 if (pid_alive(task)) {
1476 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1477 inode->i_uid = task->euid;
1478 inode->i_gid = task->egid;
1483 inode->i_mode &= ~(S_ISUID | S_ISGID);
1484 security_task_to_inode(task, inode);
1492 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1494 struct inode *inode = dentry->d_inode;
1495 struct task_struct *task = proc_task(inode);
1496 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1497 struct files_struct *files;
1499 files = get_files_struct(task);
1502 if (fcheck_files(files, fd)) {
1504 put_files_struct(files);
1505 if (task_dumpable(task)) {
1506 inode->i_uid = task->euid;
1507 inode->i_gid = task->egid;
1512 inode->i_mode &= ~(S_ISUID | S_ISGID);
1513 security_task_to_inode(task, inode);
1517 put_files_struct(files);
1523 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1525 struct task_struct *task = proc_task(inode);
1526 spin_lock(&task->proc_lock);
1527 if (task->proc_dentry == dentry)
1528 task->proc_dentry = NULL;
1529 spin_unlock(&task->proc_lock);
1533 static int pid_delete_dentry(struct dentry * dentry)
1535 /* Is the task we represent dead?
1536 * If so, then don't put the dentry on the lru list,
1537 * kill it immediately.
1539 return !pid_alive(proc_task(dentry->d_inode));
1542 static struct dentry_operations tid_fd_dentry_operations =
1544 .d_revalidate = tid_fd_revalidate,
1545 .d_delete = pid_delete_dentry,
1548 static struct dentry_operations pid_dentry_operations =
1550 .d_revalidate = pid_revalidate,
1551 .d_delete = pid_delete_dentry,
1554 static struct dentry_operations pid_base_dentry_operations =
1556 .d_revalidate = pid_revalidate,
1557 .d_iput = pid_base_iput,
1558 .d_delete = pid_delete_dentry,
1563 static unsigned name_to_int(struct dentry *dentry)
1565 const char *name = dentry->d_name.name;
1566 int len = dentry->d_name.len;
1569 if (len > 1 && *name == '0')
1572 unsigned c = *name++ - '0';
1575 if (n >= (~0U-9)/10)
1586 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1588 struct task_struct *task = proc_task(dir);
1589 unsigned fd = name_to_int(dentry);
1591 struct files_struct * files;
1592 struct inode *inode;
1593 struct proc_inode *ei;
1597 if (!pid_alive(task))
1600 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1604 files = get_files_struct(task);
1607 inode->i_mode = S_IFLNK;
1610 * We are not taking a ref to the file structure, so we must
1613 spin_lock(&files->file_lock);
1614 file = fcheck_files(files, fd);
1617 if (file->f_mode & 1)
1618 inode->i_mode |= S_IRUSR | S_IXUSR;
1619 if (file->f_mode & 2)
1620 inode->i_mode |= S_IWUSR | S_IXUSR;
1621 spin_unlock(&files->file_lock);
1622 put_files_struct(files);
1623 inode->i_op = &proc_pid_link_inode_operations;
1625 ei->op.proc_get_link = proc_fd_link;
1626 dentry->d_op = &tid_fd_dentry_operations;
1627 d_add(dentry, inode);
1631 spin_unlock(&files->file_lock);
1632 put_files_struct(files);
1636 return ERR_PTR(-ENOENT);
1639 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1640 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1642 static struct file_operations proc_fd_operations = {
1643 .read = generic_read_dir,
1644 .readdir = proc_readfd,
1647 static struct file_operations proc_task_operations = {
1648 .read = generic_read_dir,
1649 .readdir = proc_task_readdir,
1653 * proc directories can do almost nothing..
1655 static struct inode_operations proc_fd_inode_operations = {
1656 .lookup = proc_lookupfd,
1657 .permission = proc_permission,
1658 .setattr = proc_setattr,
1661 static struct inode_operations proc_task_inode_operations = {
1662 .lookup = proc_task_lookup,
1663 .permission = proc_task_permission,
1664 .setattr = proc_setattr,
1667 #ifdef CONFIG_SECURITY
1668 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1669 size_t count, loff_t *ppos)
1671 struct inode * inode = file->f_dentry->d_inode;
1674 struct task_struct *task = proc_task(inode);
1676 if (count > PAGE_SIZE)
1678 if (!(page = __get_free_page(GFP_KERNEL)))
1681 length = security_getprocattr(task,
1682 (char*)file->f_dentry->d_name.name,
1683 (void*)page, count);
1685 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1690 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1691 size_t count, loff_t *ppos)
1693 struct inode * inode = file->f_dentry->d_inode;
1696 struct task_struct *task = proc_task(inode);
1698 if (count > PAGE_SIZE)
1701 /* No partial writes. */
1704 page = (char*)__get_free_page(GFP_USER);
1708 if (copy_from_user(page, buf, count))
1711 length = security_setprocattr(task,
1712 (char*)file->f_dentry->d_name.name,
1713 (void*)page, count);
1715 free_page((unsigned long) page);
1719 static struct file_operations proc_pid_attr_operations = {
1720 .read = proc_pid_attr_read,
1721 .write = proc_pid_attr_write,
1724 static struct file_operations proc_tid_attr_operations;
1725 static struct inode_operations proc_tid_attr_inode_operations;
1726 static struct file_operations proc_tgid_attr_operations;
1727 static struct inode_operations proc_tgid_attr_inode_operations;
1730 extern int proc_pid_vx_info(struct task_struct *, char *);
1731 extern int proc_pid_nx_info(struct task_struct *, char *);
1733 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1736 static struct dentry *proc_pident_lookup(struct inode *dir,
1737 struct dentry *dentry,
1738 struct pid_entry *ents)
1740 struct inode *inode;
1742 struct task_struct *task = proc_task(dir);
1743 struct pid_entry *p;
1744 struct proc_inode *ei;
1749 if (!pid_alive(task))
1752 for (p = ents; p->name; p++) {
1753 if (p->len != dentry->d_name.len)
1755 if (!memcmp(dentry->d_name.name, p->name, p->len))
1762 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1767 inode->i_mode = p->mode;
1769 * Yes, it does not scale. And it should not. Don't add
1770 * new entries into /proc/<tgid>/ without very good reasons.
1773 case PROC_TGID_TASK:
1774 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1775 inode->i_op = &proc_task_inode_operations;
1776 inode->i_fop = &proc_task_operations;
1781 inode->i_op = &proc_fd_inode_operations;
1782 inode->i_fop = &proc_fd_operations;
1786 inode->i_op = &proc_pid_link_inode_operations;
1787 ei->op.proc_get_link = proc_exe_link;
1791 inode->i_op = &proc_pid_link_inode_operations;
1792 ei->op.proc_get_link = proc_cwd_link;
1795 case PROC_TGID_ROOT:
1796 inode->i_op = &proc_pid_link_inode_operations;
1797 ei->op.proc_get_link = proc_root_link;
1799 case PROC_TID_ENVIRON:
1800 case PROC_TGID_ENVIRON:
1801 inode->i_fop = &proc_info_file_operations;
1802 ei->op.proc_read = proc_pid_environ;
1805 case PROC_TGID_AUXV:
1806 inode->i_fop = &proc_info_file_operations;
1807 ei->op.proc_read = proc_pid_auxv;
1809 case PROC_TID_STATUS:
1810 case PROC_TGID_STATUS:
1811 inode->i_fop = &proc_info_file_operations;
1812 ei->op.proc_read = proc_pid_status;
1815 inode->i_fop = &proc_info_file_operations;
1816 ei->op.proc_read = proc_tid_stat;
1818 case PROC_TGID_STAT:
1819 inode->i_fop = &proc_info_file_operations;
1820 ei->op.proc_read = proc_tgid_stat;
1822 case PROC_TID_CMDLINE:
1823 case PROC_TGID_CMDLINE:
1824 inode->i_fop = &proc_info_file_operations;
1825 ei->op.proc_read = proc_pid_cmdline;
1827 case PROC_TID_STATM:
1828 case PROC_TGID_STATM:
1829 inode->i_fop = &proc_info_file_operations;
1830 ei->op.proc_read = proc_pid_statm;
1833 case PROC_TGID_MAPS:
1834 inode->i_fop = &proc_maps_operations;
1837 case PROC_TID_NUMA_MAPS:
1838 case PROC_TGID_NUMA_MAPS:
1839 inode->i_fop = &proc_numa_maps_operations;
1844 inode->i_op = &proc_mem_inode_operations;
1845 inode->i_fop = &proc_mem_operations;
1847 #ifdef CONFIG_SECCOMP
1848 case PROC_TID_SECCOMP:
1849 case PROC_TGID_SECCOMP:
1850 inode->i_fop = &proc_seccomp_operations;
1852 #endif /* CONFIG_SECCOMP */
1853 case PROC_TID_MOUNTS:
1854 case PROC_TGID_MOUNTS:
1855 inode->i_fop = &proc_mounts_operations;
1858 case PROC_TID_SMAPS:
1859 case PROC_TGID_SMAPS:
1860 inode->i_fop = &proc_smaps_operations;
1863 case PROC_TID_MOUNTSTATS:
1864 case PROC_TGID_MOUNTSTATS:
1865 inode->i_fop = &proc_mountstats_operations;
1867 #ifdef CONFIG_SECURITY
1870 inode->i_op = &proc_tid_attr_inode_operations;
1871 inode->i_fop = &proc_tid_attr_operations;
1873 case PROC_TGID_ATTR:
1875 inode->i_op = &proc_tgid_attr_inode_operations;
1876 inode->i_fop = &proc_tgid_attr_operations;
1878 case PROC_TID_ATTR_CURRENT:
1879 case PROC_TGID_ATTR_CURRENT:
1880 case PROC_TID_ATTR_PREV:
1881 case PROC_TGID_ATTR_PREV:
1882 case PROC_TID_ATTR_EXEC:
1883 case PROC_TGID_ATTR_EXEC:
1884 case PROC_TID_ATTR_FSCREATE:
1885 case PROC_TGID_ATTR_FSCREATE:
1886 inode->i_fop = &proc_pid_attr_operations;
1889 #ifdef CONFIG_KALLSYMS
1890 case PROC_TID_WCHAN:
1891 case PROC_TGID_WCHAN:
1892 inode->i_fop = &proc_info_file_operations;
1893 ei->op.proc_read = proc_pid_wchan;
1896 #ifdef CONFIG_SCHEDSTATS
1897 case PROC_TID_SCHEDSTAT:
1898 case PROC_TGID_SCHEDSTAT:
1899 inode->i_fop = &proc_info_file_operations;
1900 ei->op.proc_read = proc_pid_schedstat;
1903 #ifdef CONFIG_CPUSETS
1904 case PROC_TID_CPUSET:
1905 case PROC_TGID_CPUSET:
1906 inode->i_fop = &proc_cpuset_operations;
1909 case PROC_TID_OOM_SCORE:
1910 case PROC_TGID_OOM_SCORE:
1911 inode->i_fop = &proc_info_file_operations;
1912 ei->op.proc_read = proc_oom_score;
1914 case PROC_TID_OOM_ADJUST:
1915 case PROC_TGID_OOM_ADJUST:
1916 inode->i_fop = &proc_oom_adjust_operations;
1918 #ifdef CONFIG_AUDITSYSCALL
1919 case PROC_TID_LOGINUID:
1920 case PROC_TGID_LOGINUID:
1921 inode->i_fop = &proc_loginuid_operations;
1924 case PROC_TID_VX_INFO:
1925 case PROC_TGID_VX_INFO:
1926 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
1928 inode->i_fop = &proc_info_file_operations;
1929 ei->op.proc_read = proc_pid_vx_info;
1931 case PROC_TID_IP_INFO:
1932 case PROC_TGID_IP_INFO:
1933 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
1935 inode->i_fop = &proc_info_file_operations;
1936 ei->op.proc_read = proc_pid_nx_info;
1939 printk("procfs: impossible type (%d)",p->type);
1943 dentry->d_op = &pid_dentry_operations;
1944 d_add(dentry, inode);
1952 return ERR_PTR(error);
1955 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1956 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1959 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1960 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1963 static struct file_operations proc_tgid_base_operations = {
1964 .read = generic_read_dir,
1965 .readdir = proc_tgid_base_readdir,
1968 static struct file_operations proc_tid_base_operations = {
1969 .read = generic_read_dir,
1970 .readdir = proc_tid_base_readdir,
1973 static struct inode_operations proc_tgid_base_inode_operations = {
1974 .lookup = proc_tgid_base_lookup,
1975 .setattr = proc_setattr,
1978 static struct inode_operations proc_tid_base_inode_operations = {
1979 .lookup = proc_tid_base_lookup,
1980 .setattr = proc_setattr,
1983 #ifdef CONFIG_SECURITY
1984 static int proc_tgid_attr_readdir(struct file * filp,
1985 void * dirent, filldir_t filldir)
1987 return proc_pident_readdir(filp,dirent,filldir,
1988 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1991 static int proc_tid_attr_readdir(struct file * filp,
1992 void * dirent, filldir_t filldir)
1994 return proc_pident_readdir(filp,dirent,filldir,
1995 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1998 static struct file_operations proc_tgid_attr_operations = {
1999 .read = generic_read_dir,
2000 .readdir = proc_tgid_attr_readdir,
2003 static struct file_operations proc_tid_attr_operations = {
2004 .read = generic_read_dir,
2005 .readdir = proc_tid_attr_readdir,
2008 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
2009 struct dentry *dentry, struct nameidata *nd)
2011 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
2014 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
2015 struct dentry *dentry, struct nameidata *nd)
2017 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
2020 static struct inode_operations proc_tgid_attr_inode_operations = {
2021 .lookup = proc_tgid_attr_lookup,
2022 .setattr = proc_setattr,
2025 static struct inode_operations proc_tid_attr_inode_operations = {
2026 .lookup = proc_tid_attr_lookup,
2027 .setattr = proc_setattr,
2034 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2038 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
2039 return vfs_readlink(dentry,buffer,buflen,tmp);
2042 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2045 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
2046 return ERR_PTR(vfs_follow_link(nd,tmp));
2049 static struct inode_operations proc_self_inode_operations = {
2050 .readlink = proc_self_readlink,
2051 .follow_link = proc_self_follow_link,
2052 .setattr = proc_setattr,
2056 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
2057 * @p: task that should be flushed.
2059 * Drops the /proc/@pid dcache entry from the hash chains.
2061 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
2062 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
2063 * if the pid value is immediately reused. This is enforced by
2064 * - caller must acquire spin_lock(p->proc_lock)
2065 * - must be called before detach_pid()
2066 * - proc_pid_lookup acquires proc_lock, and checks that
2067 * the target is not dead by looking at the attach count
2071 struct dentry *proc_pid_unhash(struct task_struct *p)
2073 struct dentry *proc_dentry;
2075 proc_dentry = p->proc_dentry;
2076 if (proc_dentry != NULL) {
2078 spin_lock(&dcache_lock);
2079 spin_lock(&proc_dentry->d_lock);
2080 if (!d_unhashed(proc_dentry)) {
2081 dget_locked(proc_dentry);
2082 __d_drop(proc_dentry);
2083 spin_unlock(&proc_dentry->d_lock);
2085 spin_unlock(&proc_dentry->d_lock);
2088 spin_unlock(&dcache_lock);
2094 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
2095 * @proc_dentry: directoy to prune.
2097 * Shrink the /proc directory that was used by the just killed thread.
2100 void proc_pid_flush(struct dentry *proc_dentry)
2103 if(proc_dentry != NULL) {
2104 shrink_dcache_parent(proc_dentry);
2109 #define VXF_FAKE_INIT (VXF_INFO_INIT|VXF_STATE_INIT)
2111 static inline int proc_pid_visible(struct task_struct *task, int pid)
2114 !vx_flags(VXF_FAKE_INIT, VXF_FAKE_INIT))
2116 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
2124 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2126 struct task_struct *task;
2127 struct inode *inode;
2128 struct proc_inode *ei;
2132 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2133 inode = new_inode(dir->i_sb);
2135 return ERR_PTR(-ENOMEM);
2137 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2138 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2140 inode->i_mode = S_IFLNK|S_IRWXUGO;
2141 inode->i_uid = inode->i_gid = 0;
2143 inode->i_op = &proc_self_inode_operations;
2144 d_add(dentry, inode);
2147 tgid = name_to_int(dentry);
2151 read_lock(&tasklist_lock);
2152 task = find_task_by_pid(tgid);
2154 get_task_struct(task);
2155 read_unlock(&tasklist_lock);
2159 /* check for context visibility */
2160 if (!proc_pid_visible(task, tgid))
2163 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2167 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2168 inode->i_op = &proc_tgid_base_inode_operations;
2169 inode->i_fop = &proc_tgid_base_operations;
2170 inode->i_flags|=S_IMMUTABLE;
2171 #ifdef CONFIG_SECURITY
2177 dentry->d_op = &pid_base_dentry_operations;
2180 d_add(dentry, inode);
2181 spin_lock(&task->proc_lock);
2182 task->proc_dentry = dentry;
2183 if (!pid_alive(task)) {
2184 dentry = proc_pid_unhash(task);
2187 spin_unlock(&task->proc_lock);
2189 put_task_struct(task);
2191 proc_pid_flush(dentry);
2196 put_task_struct(task);
2198 return ERR_PTR(-ENOENT);
2202 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2204 struct task_struct *task;
2205 struct task_struct *leader = proc_task(dir);
2206 struct inode *inode;
2209 tid = name_to_int(dentry);
2212 if (vx_current_initpid(tid))
2215 read_lock(&tasklist_lock);
2216 task = find_task_by_pid(tid);
2218 get_task_struct(task);
2219 read_unlock(&tasklist_lock);
2222 if (leader->tgid != task->tgid)
2225 /* check for context visibility */
2226 if (!proc_pid_visible(task, tid))
2229 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2233 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2234 inode->i_op = &proc_tid_base_inode_operations;
2235 inode->i_fop = &proc_tid_base_operations;
2236 inode->i_flags|=S_IMMUTABLE;
2237 #ifdef CONFIG_SECURITY
2243 dentry->d_op = &pid_base_dentry_operations;
2245 d_add(dentry, inode);
2247 put_task_struct(task);
2250 put_task_struct(task);
2252 return ERR_PTR(-ENOENT);
2255 #define PROC_NUMBUF 10
2256 #define PROC_MAXPIDS 20
2259 * Get a few tgid's to return for filldir - we need to hold the
2260 * tasklist lock while doing this, and we must release it before
2261 * we actually do the filldir itself, so we use a temp buffer..
2263 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2265 struct task_struct *p;
2269 read_lock(&tasklist_lock);
2272 p = find_task_by_real_pid(version);
2273 if (p && !thread_group_leader(p))
2280 p = next_task(&init_task);
2282 for ( ; p != &init_task; p = next_task(p)) {
2287 /* check for context visibility */
2288 if (!proc_pid_visible(p, tgid))
2292 tgids[nr_tgids] = vx_map_tgid(tgid);
2294 if (nr_tgids >= PROC_MAXPIDS)
2297 read_unlock(&tasklist_lock);
2302 * Get a few tid's to return for filldir - we need to hold the
2303 * tasklist lock while doing this, and we must release it before
2304 * we actually do the filldir itself, so we use a temp buffer..
2306 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2308 struct task_struct *leader_task = proc_task(dir);
2309 struct task_struct *task = leader_task;
2313 read_lock(&tasklist_lock);
2315 * The starting point task (leader_task) might be an already
2316 * unlinked task, which cannot be used to access the task-list
2317 * via next_thread().
2319 if (pid_alive(task)) do {
2320 int tid = task->pid;
2322 /* check for context visibility */
2323 if (!proc_pid_visible(task, tid))
2328 tids[nr_tids] = vx_map_pid(tid);
2330 if (nr_tids >= PROC_MAXPIDS)
2332 } while ((task = next_thread(task)) != leader_task);
2333 read_unlock(&tasklist_lock);
2337 /* for the /proc/ directory itself, after non-process stuff has been done */
2338 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2340 unsigned int tgid_array[PROC_MAXPIDS];
2341 char buf[PROC_NUMBUF];
2342 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2343 unsigned int nr_tgids, i;
2347 ino_t ino = fake_ino(0,PROC_TGID_INO);
2348 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2354 /* f_version caches the tgid value that the last readdir call couldn't
2355 * return. lseek aka telldir automagically resets f_version to 0.
2357 next_tgid = filp->f_version;
2358 filp->f_version = 0;
2360 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2362 /* no more entries ! */
2367 /* do not use the last found pid, reserve it for next_tgid */
2368 if (nr_tgids == PROC_MAXPIDS) {
2370 next_tgid = tgid_array[nr_tgids];
2373 for (i=0;i<nr_tgids;i++) {
2374 int tgid = tgid_array[i];
2375 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2376 unsigned long j = PROC_NUMBUF;
2379 buf[--j] = '0' + (tgid % 10);
2380 while ((tgid /= 10) != 0);
2382 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2383 /* returning this tgid failed, save it as the first
2384 * pid for the next readir call */
2385 filp->f_version = tgid_array[i];
2396 /* for the /proc/TGID/task/ directories */
2397 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2399 unsigned int tid_array[PROC_MAXPIDS];
2400 char buf[PROC_NUMBUF];
2401 unsigned int nr_tids, i;
2402 struct dentry *dentry = filp->f_dentry;
2403 struct inode *inode = dentry->d_inode;
2404 struct task_struct *task = proc_task(inode);
2405 int retval = -ENOENT;
2407 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2409 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
2411 if (!pid_alive(task))
2418 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2423 ino = parent_ino(dentry);
2424 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2430 nr_tids = get_tid_list(pos, tid_array, inode);
2431 inode->i_nlink = pos + nr_tids;
2433 for (i = 0; i < nr_tids; i++) {
2434 unsigned long j = PROC_NUMBUF;
2435 int tid = tid_array[i];
2437 ino = fake_ino(tid,PROC_TID_INO);
2440 buf[--j] = '0' + (tid % 10);
2441 while ((tid /= 10) != 0);
2443 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)