[PATCH] proc: modify proc_pident_lookup to be completely table driven
[linux-2.6.git] / fs / proc / base.c
blob9c6a809f92b69228c8ce2ea99d29438a5095b50f
1 /*
2 * linux/fs/proc/base.c
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.
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
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.
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
38 * ChangeLog:
39 * 10-Mar-2005
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>
63 #include <linux/mm.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
73 #include <linux/poll.h>
74 #include "internal.h"
76 /* NOTE:
77 * Implementing inode permission operations in /proc is almost
78 * certainly an error. Permission checks need to happen during
79 * each system call not at open time. The reason is that most of
80 * what we wish to check for permissions in /proc varies at runtime.
82 * The classic example of a problem is opening file descriptors
83 * in /proc for a task before it execs a suid executable.
87 * For hysterical raisins we keep the same inumbers as in the old procfs.
88 * Feel free to change the macro below - just keep the range distinct from
89 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
90 * As soon as we'll get a separate superblock we will be able to forget
91 * about magical ranges too.
94 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
96 enum pid_directory_inos {
97 PROC_TGID_INO = 2,
98 PROC_TGID_TASK,
99 PROC_TGID_STATUS,
100 PROC_TGID_MEM,
101 #ifdef CONFIG_SECCOMP
102 PROC_TGID_SECCOMP,
103 #endif
104 PROC_TGID_CWD,
105 PROC_TGID_ROOT,
106 PROC_TGID_EXE,
107 PROC_TGID_FD,
108 PROC_TGID_ENVIRON,
109 PROC_TGID_AUXV,
110 PROC_TGID_CMDLINE,
111 PROC_TGID_STAT,
112 PROC_TGID_STATM,
113 PROC_TGID_MAPS,
114 PROC_TGID_NUMA_MAPS,
115 PROC_TGID_MOUNTS,
116 PROC_TGID_MOUNTSTATS,
117 PROC_TGID_WCHAN,
118 #ifdef CONFIG_MMU
119 PROC_TGID_SMAPS,
120 #endif
121 #ifdef CONFIG_SCHEDSTATS
122 PROC_TGID_SCHEDSTAT,
123 #endif
124 #ifdef CONFIG_CPUSETS
125 PROC_TGID_CPUSET,
126 #endif
127 #ifdef CONFIG_SECURITY
128 PROC_TGID_ATTR,
129 PROC_TGID_ATTR_CURRENT,
130 PROC_TGID_ATTR_PREV,
131 PROC_TGID_ATTR_EXEC,
132 PROC_TGID_ATTR_FSCREATE,
133 PROC_TGID_ATTR_KEYCREATE,
134 PROC_TGID_ATTR_SOCKCREATE,
135 #endif
136 #ifdef CONFIG_AUDITSYSCALL
137 PROC_TGID_LOGINUID,
138 #endif
139 PROC_TGID_OOM_SCORE,
140 PROC_TGID_OOM_ADJUST,
141 PROC_TID_INO,
142 PROC_TID_STATUS,
143 PROC_TID_MEM,
144 #ifdef CONFIG_SECCOMP
145 PROC_TID_SECCOMP,
146 #endif
147 PROC_TID_CWD,
148 PROC_TID_ROOT,
149 PROC_TID_EXE,
150 PROC_TID_FD,
151 PROC_TID_ENVIRON,
152 PROC_TID_AUXV,
153 PROC_TID_CMDLINE,
154 PROC_TID_STAT,
155 PROC_TID_STATM,
156 PROC_TID_MAPS,
157 PROC_TID_NUMA_MAPS,
158 PROC_TID_MOUNTS,
159 PROC_TID_MOUNTSTATS,
160 PROC_TID_WCHAN,
161 #ifdef CONFIG_MMU
162 PROC_TID_SMAPS,
163 #endif
164 #ifdef CONFIG_SCHEDSTATS
165 PROC_TID_SCHEDSTAT,
166 #endif
167 #ifdef CONFIG_CPUSETS
168 PROC_TID_CPUSET,
169 #endif
170 #ifdef CONFIG_SECURITY
171 PROC_TID_ATTR,
172 PROC_TID_ATTR_CURRENT,
173 PROC_TID_ATTR_PREV,
174 PROC_TID_ATTR_EXEC,
175 PROC_TID_ATTR_FSCREATE,
176 PROC_TID_ATTR_KEYCREATE,
177 PROC_TID_ATTR_SOCKCREATE,
178 #endif
179 #ifdef CONFIG_AUDITSYSCALL
180 PROC_TID_LOGINUID,
181 #endif
182 PROC_TID_OOM_SCORE,
183 PROC_TID_OOM_ADJUST,
185 /* Add new entries before this */
186 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
189 /* Worst case buffer size needed for holding an integer. */
190 #define PROC_NUMBUF 10
192 struct pid_entry {
193 int type;
194 int len;
195 char *name;
196 mode_t mode;
197 struct inode_operations *iop;
198 struct file_operations *fop;
199 union proc_op op;
202 #define NOD(TYPE, NAME, MODE, IOP, FOP, OP) { \
203 .type = (TYPE), \
204 .len = sizeof(NAME) - 1, \
205 .name = (NAME), \
206 .mode = MODE, \
207 .iop = IOP, \
208 .fop = FOP, \
209 .op = OP, \
212 #define DIR(TYPE, NAME, MODE, OTYPE) \
213 NOD(TYPE, NAME, (S_IFDIR|(MODE)), \
214 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
215 {} )
216 #define LNK(TYPE, NAME, OTYPE) \
217 NOD(TYPE, NAME, (S_IFLNK|S_IRWXUGO), \
218 &proc_pid_link_inode_operations, NULL, \
219 { .proc_get_link = &proc_##OTYPE##_link } )
220 #define REG(TYPE, NAME, MODE, OTYPE) \
221 NOD(TYPE, NAME, (S_IFREG|(MODE)), NULL, \
222 &proc_##OTYPE##_operations, {})
223 #define INF(TYPE, NAME, MODE, OTYPE) \
224 NOD(TYPE, NAME, (S_IFREG|(MODE)), \
225 NULL, &proc_info_file_operations, \
226 { .proc_read = &proc_##OTYPE } )
228 static struct fs_struct *get_fs_struct(struct task_struct *task)
230 struct fs_struct *fs;
231 task_lock(task);
232 fs = task->fs;
233 if(fs)
234 atomic_inc(&fs->count);
235 task_unlock(task);
236 return fs;
239 static int get_nr_threads(struct task_struct *tsk)
241 /* Must be called with the rcu_read_lock held */
242 unsigned long flags;
243 int count = 0;
245 if (lock_task_sighand(tsk, &flags)) {
246 count = atomic_read(&tsk->signal->count);
247 unlock_task_sighand(tsk, &flags);
249 return count;
252 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
254 struct task_struct *task = get_proc_task(inode);
255 struct fs_struct *fs = NULL;
256 int result = -ENOENT;
258 if (task) {
259 fs = get_fs_struct(task);
260 put_task_struct(task);
262 if (fs) {
263 read_lock(&fs->lock);
264 *mnt = mntget(fs->pwdmnt);
265 *dentry = dget(fs->pwd);
266 read_unlock(&fs->lock);
267 result = 0;
268 put_fs_struct(fs);
270 return result;
273 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
275 struct task_struct *task = get_proc_task(inode);
276 struct fs_struct *fs = NULL;
277 int result = -ENOENT;
279 if (task) {
280 fs = get_fs_struct(task);
281 put_task_struct(task);
283 if (fs) {
284 read_lock(&fs->lock);
285 *mnt = mntget(fs->rootmnt);
286 *dentry = dget(fs->root);
287 read_unlock(&fs->lock);
288 result = 0;
289 put_fs_struct(fs);
291 return result;
294 #define MAY_PTRACE(task) \
295 (task == current || \
296 (task->parent == current && \
297 (task->ptrace & PT_PTRACED) && \
298 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
299 security_ptrace(current,task) == 0))
301 static int proc_pid_environ(struct task_struct *task, char * buffer)
303 int res = 0;
304 struct mm_struct *mm = get_task_mm(task);
305 if (mm) {
306 unsigned int len = mm->env_end - mm->env_start;
307 if (len > PAGE_SIZE)
308 len = PAGE_SIZE;
309 res = access_process_vm(task, mm->env_start, buffer, len, 0);
310 if (!ptrace_may_attach(task))
311 res = -ESRCH;
312 mmput(mm);
314 return res;
317 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
319 int res = 0;
320 unsigned int len;
321 struct mm_struct *mm = get_task_mm(task);
322 if (!mm)
323 goto out;
324 if (!mm->arg_end)
325 goto out_mm; /* Shh! No looking before we're done */
327 len = mm->arg_end - mm->arg_start;
329 if (len > PAGE_SIZE)
330 len = PAGE_SIZE;
332 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
334 // If the nul at the end of args has been overwritten, then
335 // assume application is using setproctitle(3).
336 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
337 len = strnlen(buffer, res);
338 if (len < res) {
339 res = len;
340 } else {
341 len = mm->env_end - mm->env_start;
342 if (len > PAGE_SIZE - res)
343 len = PAGE_SIZE - res;
344 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
345 res = strnlen(buffer, res);
348 out_mm:
349 mmput(mm);
350 out:
351 return res;
354 static int proc_pid_auxv(struct task_struct *task, char *buffer)
356 int res = 0;
357 struct mm_struct *mm = get_task_mm(task);
358 if (mm) {
359 unsigned int nwords = 0;
361 nwords += 2;
362 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
363 res = nwords * sizeof(mm->saved_auxv[0]);
364 if (res > PAGE_SIZE)
365 res = PAGE_SIZE;
366 memcpy(buffer, mm->saved_auxv, res);
367 mmput(mm);
369 return res;
373 #ifdef CONFIG_KALLSYMS
375 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
376 * Returns the resolved symbol. If that fails, simply return the address.
378 static int proc_pid_wchan(struct task_struct *task, char *buffer)
380 char *modname;
381 const char *sym_name;
382 unsigned long wchan, size, offset;
383 char namebuf[KSYM_NAME_LEN+1];
385 wchan = get_wchan(task);
387 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
388 if (sym_name)
389 return sprintf(buffer, "%s", sym_name);
390 return sprintf(buffer, "%lu", wchan);
392 #endif /* CONFIG_KALLSYMS */
394 #ifdef CONFIG_SCHEDSTATS
396 * Provides /proc/PID/schedstat
398 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
400 return sprintf(buffer, "%lu %lu %lu\n",
401 task->sched_info.cpu_time,
402 task->sched_info.run_delay,
403 task->sched_info.pcnt);
405 #endif
407 /* The badness from the OOM killer */
408 unsigned long badness(struct task_struct *p, unsigned long uptime);
409 static int proc_oom_score(struct task_struct *task, char *buffer)
411 unsigned long points;
412 struct timespec uptime;
414 do_posix_clock_monotonic_gettime(&uptime);
415 points = badness(task, uptime.tv_sec);
416 return sprintf(buffer, "%lu\n", points);
419 /************************************************************************/
420 /* Here the fs part begins */
421 /************************************************************************/
423 /* permission checks */
424 static int proc_fd_access_allowed(struct inode *inode)
426 struct task_struct *task;
427 int allowed = 0;
428 /* Allow access to a task's file descriptors if it is us or we
429 * may use ptrace attach to the process and find out that
430 * information.
432 task = get_proc_task(inode);
433 if (task) {
434 allowed = ptrace_may_attach(task);
435 put_task_struct(task);
437 return allowed;
440 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
442 int error;
443 struct inode *inode = dentry->d_inode;
445 if (attr->ia_valid & ATTR_MODE)
446 return -EPERM;
448 error = inode_change_ok(inode, attr);
449 if (!error) {
450 error = security_inode_setattr(dentry, attr);
451 if (!error)
452 error = inode_setattr(inode, attr);
454 return error;
457 static struct inode_operations proc_def_inode_operations = {
458 .setattr = proc_setattr,
461 extern struct seq_operations mounts_op;
462 struct proc_mounts {
463 struct seq_file m;
464 int event;
467 static int mounts_open(struct inode *inode, struct file *file)
469 struct task_struct *task = get_proc_task(inode);
470 struct namespace *namespace = NULL;
471 struct proc_mounts *p;
472 int ret = -EINVAL;
474 if (task) {
475 task_lock(task);
476 namespace = task->namespace;
477 if (namespace)
478 get_namespace(namespace);
479 task_unlock(task);
480 put_task_struct(task);
483 if (namespace) {
484 ret = -ENOMEM;
485 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
486 if (p) {
487 file->private_data = &p->m;
488 ret = seq_open(file, &mounts_op);
489 if (!ret) {
490 p->m.private = namespace;
491 p->event = namespace->event;
492 return 0;
494 kfree(p);
496 put_namespace(namespace);
498 return ret;
501 static int mounts_release(struct inode *inode, struct file *file)
503 struct seq_file *m = file->private_data;
504 struct namespace *namespace = m->private;
505 put_namespace(namespace);
506 return seq_release(inode, file);
509 static unsigned mounts_poll(struct file *file, poll_table *wait)
511 struct proc_mounts *p = file->private_data;
512 struct namespace *ns = p->m.private;
513 unsigned res = 0;
515 poll_wait(file, &ns->poll, wait);
517 spin_lock(&vfsmount_lock);
518 if (p->event != ns->event) {
519 p->event = ns->event;
520 res = POLLERR;
522 spin_unlock(&vfsmount_lock);
524 return res;
527 static struct file_operations proc_mounts_operations = {
528 .open = mounts_open,
529 .read = seq_read,
530 .llseek = seq_lseek,
531 .release = mounts_release,
532 .poll = mounts_poll,
535 extern struct seq_operations mountstats_op;
536 static int mountstats_open(struct inode *inode, struct file *file)
538 int ret = seq_open(file, &mountstats_op);
540 if (!ret) {
541 struct seq_file *m = file->private_data;
542 struct namespace *namespace = NULL;
543 struct task_struct *task = get_proc_task(inode);
545 if (task) {
546 task_lock(task);
547 namespace = task->namespace;
548 if (namespace)
549 get_namespace(namespace);
550 task_unlock(task);
551 put_task_struct(task);
554 if (namespace)
555 m->private = namespace;
556 else {
557 seq_release(inode, file);
558 ret = -EINVAL;
561 return ret;
564 static struct file_operations proc_mountstats_operations = {
565 .open = mountstats_open,
566 .read = seq_read,
567 .llseek = seq_lseek,
568 .release = mounts_release,
571 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
573 static ssize_t proc_info_read(struct file * file, char __user * buf,
574 size_t count, loff_t *ppos)
576 struct inode * inode = file->f_dentry->d_inode;
577 unsigned long page;
578 ssize_t length;
579 struct task_struct *task = get_proc_task(inode);
581 length = -ESRCH;
582 if (!task)
583 goto out_no_task;
585 if (count > PROC_BLOCK_SIZE)
586 count = PROC_BLOCK_SIZE;
588 length = -ENOMEM;
589 if (!(page = __get_free_page(GFP_KERNEL)))
590 goto out;
592 length = PROC_I(inode)->op.proc_read(task, (char*)page);
594 if (length >= 0)
595 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
596 free_page(page);
597 out:
598 put_task_struct(task);
599 out_no_task:
600 return length;
603 static struct file_operations proc_info_file_operations = {
604 .read = proc_info_read,
607 static int mem_open(struct inode* inode, struct file* file)
609 file->private_data = (void*)((long)current->self_exec_id);
610 return 0;
613 static ssize_t mem_read(struct file * file, char __user * buf,
614 size_t count, loff_t *ppos)
616 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
617 char *page;
618 unsigned long src = *ppos;
619 int ret = -ESRCH;
620 struct mm_struct *mm;
622 if (!task)
623 goto out_no_task;
625 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
626 goto out;
628 ret = -ENOMEM;
629 page = (char *)__get_free_page(GFP_USER);
630 if (!page)
631 goto out;
633 ret = 0;
635 mm = get_task_mm(task);
636 if (!mm)
637 goto out_free;
639 ret = -EIO;
641 if (file->private_data != (void*)((long)current->self_exec_id))
642 goto out_put;
644 ret = 0;
646 while (count > 0) {
647 int this_len, retval;
649 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
650 retval = access_process_vm(task, src, page, this_len, 0);
651 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
652 if (!ret)
653 ret = -EIO;
654 break;
657 if (copy_to_user(buf, page, retval)) {
658 ret = -EFAULT;
659 break;
662 ret += retval;
663 src += retval;
664 buf += retval;
665 count -= retval;
667 *ppos = src;
669 out_put:
670 mmput(mm);
671 out_free:
672 free_page((unsigned long) page);
673 out:
674 put_task_struct(task);
675 out_no_task:
676 return ret;
679 #define mem_write NULL
681 #ifndef mem_write
682 /* This is a security hazard */
683 static ssize_t mem_write(struct file * file, const char * buf,
684 size_t count, loff_t *ppos)
686 int copied;
687 char *page;
688 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
689 unsigned long dst = *ppos;
691 copied = -ESRCH;
692 if (!task)
693 goto out_no_task;
695 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
696 goto out;
698 copied = -ENOMEM;
699 page = (char *)__get_free_page(GFP_USER);
700 if (!page)
701 goto out;
703 copied = 0;
704 while (count > 0) {
705 int this_len, retval;
707 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
708 if (copy_from_user(page, buf, this_len)) {
709 copied = -EFAULT;
710 break;
712 retval = access_process_vm(task, dst, page, this_len, 1);
713 if (!retval) {
714 if (!copied)
715 copied = -EIO;
716 break;
718 copied += retval;
719 buf += retval;
720 dst += retval;
721 count -= retval;
723 *ppos = dst;
724 free_page((unsigned long) page);
725 out:
726 put_task_struct(task);
727 out_no_task:
728 return copied;
730 #endif
732 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
734 switch (orig) {
735 case 0:
736 file->f_pos = offset;
737 break;
738 case 1:
739 file->f_pos += offset;
740 break;
741 default:
742 return -EINVAL;
744 force_successful_syscall_return();
745 return file->f_pos;
748 static struct file_operations proc_mem_operations = {
749 .llseek = mem_lseek,
750 .read = mem_read,
751 .write = mem_write,
752 .open = mem_open,
755 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
756 size_t count, loff_t *ppos)
758 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
759 char buffer[PROC_NUMBUF];
760 size_t len;
761 int oom_adjust;
762 loff_t __ppos = *ppos;
764 if (!task)
765 return -ESRCH;
766 oom_adjust = task->oomkilladj;
767 put_task_struct(task);
769 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
770 if (__ppos >= len)
771 return 0;
772 if (count > len-__ppos)
773 count = len-__ppos;
774 if (copy_to_user(buf, buffer + __ppos, count))
775 return -EFAULT;
776 *ppos = __ppos + count;
777 return count;
780 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
781 size_t count, loff_t *ppos)
783 struct task_struct *task;
784 char buffer[PROC_NUMBUF], *end;
785 int oom_adjust;
787 if (!capable(CAP_SYS_RESOURCE))
788 return -EPERM;
789 memset(buffer, 0, sizeof(buffer));
790 if (count > sizeof(buffer) - 1)
791 count = sizeof(buffer) - 1;
792 if (copy_from_user(buffer, buf, count))
793 return -EFAULT;
794 oom_adjust = simple_strtol(buffer, &end, 0);
795 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
796 return -EINVAL;
797 if (*end == '\n')
798 end++;
799 task = get_proc_task(file->f_dentry->d_inode);
800 if (!task)
801 return -ESRCH;
802 task->oomkilladj = oom_adjust;
803 put_task_struct(task);
804 if (end - buffer == 0)
805 return -EIO;
806 return end - buffer;
809 static struct file_operations proc_oom_adjust_operations = {
810 .read = oom_adjust_read,
811 .write = oom_adjust_write,
814 #ifdef CONFIG_AUDITSYSCALL
815 #define TMPBUFLEN 21
816 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
817 size_t count, loff_t *ppos)
819 struct inode * inode = file->f_dentry->d_inode;
820 struct task_struct *task = get_proc_task(inode);
821 ssize_t length;
822 char tmpbuf[TMPBUFLEN];
824 if (!task)
825 return -ESRCH;
826 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
827 audit_get_loginuid(task->audit_context));
828 put_task_struct(task);
829 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
832 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
833 size_t count, loff_t *ppos)
835 struct inode * inode = file->f_dentry->d_inode;
836 char *page, *tmp;
837 ssize_t length;
838 uid_t loginuid;
840 if (!capable(CAP_AUDIT_CONTROL))
841 return -EPERM;
843 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
844 return -EPERM;
846 if (count >= PAGE_SIZE)
847 count = PAGE_SIZE - 1;
849 if (*ppos != 0) {
850 /* No partial writes. */
851 return -EINVAL;
853 page = (char*)__get_free_page(GFP_USER);
854 if (!page)
855 return -ENOMEM;
856 length = -EFAULT;
857 if (copy_from_user(page, buf, count))
858 goto out_free_page;
860 page[count] = '\0';
861 loginuid = simple_strtoul(page, &tmp, 10);
862 if (tmp == page) {
863 length = -EINVAL;
864 goto out_free_page;
867 length = audit_set_loginuid(current, loginuid);
868 if (likely(length == 0))
869 length = count;
871 out_free_page:
872 free_page((unsigned long) page);
873 return length;
876 static struct file_operations proc_loginuid_operations = {
877 .read = proc_loginuid_read,
878 .write = proc_loginuid_write,
880 #endif
882 #ifdef CONFIG_SECCOMP
883 static ssize_t seccomp_read(struct file *file, char __user *buf,
884 size_t count, loff_t *ppos)
886 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
887 char __buf[20];
888 loff_t __ppos = *ppos;
889 size_t len;
891 if (!tsk)
892 return -ESRCH;
893 /* no need to print the trailing zero, so use only len */
894 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
895 put_task_struct(tsk);
896 if (__ppos >= len)
897 return 0;
898 if (count > len - __ppos)
899 count = len - __ppos;
900 if (copy_to_user(buf, __buf + __ppos, count))
901 return -EFAULT;
902 *ppos = __ppos + count;
903 return count;
906 static ssize_t seccomp_write(struct file *file, const char __user *buf,
907 size_t count, loff_t *ppos)
909 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
910 char __buf[20], *end;
911 unsigned int seccomp_mode;
912 ssize_t result;
914 result = -ESRCH;
915 if (!tsk)
916 goto out_no_task;
918 /* can set it only once to be even more secure */
919 result = -EPERM;
920 if (unlikely(tsk->seccomp.mode))
921 goto out;
923 result = -EFAULT;
924 memset(__buf, 0, sizeof(__buf));
925 count = min(count, sizeof(__buf) - 1);
926 if (copy_from_user(__buf, buf, count))
927 goto out;
929 seccomp_mode = simple_strtoul(__buf, &end, 0);
930 if (*end == '\n')
931 end++;
932 result = -EINVAL;
933 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
934 tsk->seccomp.mode = seccomp_mode;
935 set_tsk_thread_flag(tsk, TIF_SECCOMP);
936 } else
937 goto out;
938 result = -EIO;
939 if (unlikely(!(end - __buf)))
940 goto out;
941 result = end - __buf;
942 out:
943 put_task_struct(tsk);
944 out_no_task:
945 return result;
948 static struct file_operations proc_seccomp_operations = {
949 .read = seccomp_read,
950 .write = seccomp_write,
952 #endif /* CONFIG_SECCOMP */
954 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
956 struct inode *inode = dentry->d_inode;
957 int error = -EACCES;
959 /* We don't need a base pointer in the /proc filesystem */
960 path_release(nd);
962 /* Are we allowed to snoop on the tasks file descriptors? */
963 if (!proc_fd_access_allowed(inode))
964 goto out;
966 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
967 nd->last_type = LAST_BIND;
968 out:
969 return ERR_PTR(error);
972 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
973 char __user *buffer, int buflen)
975 struct inode * inode;
976 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
977 int len;
979 if (!tmp)
980 return -ENOMEM;
982 inode = dentry->d_inode;
983 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
984 len = PTR_ERR(path);
985 if (IS_ERR(path))
986 goto out;
987 len = tmp + PAGE_SIZE - 1 - path;
989 if (len > buflen)
990 len = buflen;
991 if (copy_to_user(buffer, path, len))
992 len = -EFAULT;
993 out:
994 free_page((unsigned long)tmp);
995 return len;
998 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1000 int error = -EACCES;
1001 struct inode *inode = dentry->d_inode;
1002 struct dentry *de;
1003 struct vfsmount *mnt = NULL;
1005 /* Are we allowed to snoop on the tasks file descriptors? */
1006 if (!proc_fd_access_allowed(inode))
1007 goto out;
1009 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1010 if (error)
1011 goto out;
1013 error = do_proc_readlink(de, mnt, buffer, buflen);
1014 dput(de);
1015 mntput(mnt);
1016 out:
1017 return error;
1020 static struct inode_operations proc_pid_link_inode_operations = {
1021 .readlink = proc_pid_readlink,
1022 .follow_link = proc_pid_follow_link,
1023 .setattr = proc_setattr,
1027 /* building an inode */
1029 static int task_dumpable(struct task_struct *task)
1031 int dumpable = 0;
1032 struct mm_struct *mm;
1034 task_lock(task);
1035 mm = task->mm;
1036 if (mm)
1037 dumpable = mm->dumpable;
1038 task_unlock(task);
1039 if(dumpable == 1)
1040 return 1;
1041 return 0;
1045 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1047 struct inode * inode;
1048 struct proc_inode *ei;
1050 /* We need a new inode */
1052 inode = new_inode(sb);
1053 if (!inode)
1054 goto out;
1056 /* Common stuff */
1057 ei = PROC_I(inode);
1058 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1059 inode->i_ino = fake_ino(task->pid, ino);
1060 inode->i_op = &proc_def_inode_operations;
1063 * grab the reference to task.
1065 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1066 if (!ei->pid)
1067 goto out_unlock;
1069 inode->i_uid = 0;
1070 inode->i_gid = 0;
1071 if (task_dumpable(task)) {
1072 inode->i_uid = task->euid;
1073 inode->i_gid = task->egid;
1075 security_task_to_inode(task, inode);
1077 out:
1078 return inode;
1080 out_unlock:
1081 iput(inode);
1082 return NULL;
1085 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1087 struct inode *inode = dentry->d_inode;
1088 struct task_struct *task;
1089 generic_fillattr(inode, stat);
1091 rcu_read_lock();
1092 stat->uid = 0;
1093 stat->gid = 0;
1094 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1095 if (task) {
1096 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1097 task_dumpable(task)) {
1098 stat->uid = task->euid;
1099 stat->gid = task->egid;
1102 rcu_read_unlock();
1103 return 0;
1106 /* dentry stuff */
1109 * Exceptional case: normally we are not allowed to unhash a busy
1110 * directory. In this case, however, we can do it - no aliasing problems
1111 * due to the way we treat inodes.
1113 * Rewrite the inode's ownerships here because the owning task may have
1114 * performed a setuid(), etc.
1116 * Before the /proc/pid/status file was created the only way to read
1117 * the effective uid of a /process was to stat /proc/pid. Reading
1118 * /proc/pid/status is slow enough that procps and other packages
1119 * kept stating /proc/pid. To keep the rules in /proc simple I have
1120 * made this apply to all per process world readable and executable
1121 * directories.
1123 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1125 struct inode *inode = dentry->d_inode;
1126 struct task_struct *task = get_proc_task(inode);
1127 if (task) {
1128 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1129 task_dumpable(task)) {
1130 inode->i_uid = task->euid;
1131 inode->i_gid = task->egid;
1132 } else {
1133 inode->i_uid = 0;
1134 inode->i_gid = 0;
1136 inode->i_mode &= ~(S_ISUID | S_ISGID);
1137 security_task_to_inode(task, inode);
1138 put_task_struct(task);
1139 return 1;
1141 d_drop(dentry);
1142 return 0;
1145 static int pid_delete_dentry(struct dentry * dentry)
1147 /* Is the task we represent dead?
1148 * If so, then don't put the dentry on the lru list,
1149 * kill it immediately.
1151 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1154 static struct dentry_operations pid_dentry_operations =
1156 .d_revalidate = pid_revalidate,
1157 .d_delete = pid_delete_dentry,
1160 /* Lookups */
1162 static unsigned name_to_int(struct dentry *dentry)
1164 const char *name = dentry->d_name.name;
1165 int len = dentry->d_name.len;
1166 unsigned n = 0;
1168 if (len > 1 && *name == '0')
1169 goto out;
1170 while (len-- > 0) {
1171 unsigned c = *name++ - '0';
1172 if (c > 9)
1173 goto out;
1174 if (n >= (~0U-9)/10)
1175 goto out;
1176 n *= 10;
1177 n += c;
1179 return n;
1180 out:
1181 return ~0U;
1184 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1186 struct task_struct *task = get_proc_task(inode);
1187 struct files_struct *files = NULL;
1188 struct file *file;
1189 int fd = proc_fd(inode);
1191 if (task) {
1192 files = get_files_struct(task);
1193 put_task_struct(task);
1195 if (files) {
1197 * We are not taking a ref to the file structure, so we must
1198 * hold ->file_lock.
1200 spin_lock(&files->file_lock);
1201 file = fcheck_files(files, fd);
1202 if (file) {
1203 *mnt = mntget(file->f_vfsmnt);
1204 *dentry = dget(file->f_dentry);
1205 spin_unlock(&files->file_lock);
1206 put_files_struct(files);
1207 return 0;
1209 spin_unlock(&files->file_lock);
1210 put_files_struct(files);
1212 return -ENOENT;
1215 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1217 struct inode *inode = dentry->d_inode;
1218 struct task_struct *task = get_proc_task(inode);
1219 int fd = proc_fd(inode);
1220 struct files_struct *files;
1222 if (task) {
1223 files = get_files_struct(task);
1224 if (files) {
1225 rcu_read_lock();
1226 if (fcheck_files(files, fd)) {
1227 rcu_read_unlock();
1228 put_files_struct(files);
1229 if (task_dumpable(task)) {
1230 inode->i_uid = task->euid;
1231 inode->i_gid = task->egid;
1232 } else {
1233 inode->i_uid = 0;
1234 inode->i_gid = 0;
1236 inode->i_mode &= ~(S_ISUID | S_ISGID);
1237 security_task_to_inode(task, inode);
1238 put_task_struct(task);
1239 return 1;
1241 rcu_read_unlock();
1242 put_files_struct(files);
1244 put_task_struct(task);
1246 d_drop(dentry);
1247 return 0;
1250 static struct dentry_operations tid_fd_dentry_operations =
1252 .d_revalidate = tid_fd_revalidate,
1253 .d_delete = pid_delete_dentry,
1256 /* SMP-safe */
1257 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1259 struct task_struct *task = get_proc_task(dir);
1260 unsigned fd = name_to_int(dentry);
1261 struct dentry *result = ERR_PTR(-ENOENT);
1262 struct file * file;
1263 struct files_struct * files;
1264 struct inode *inode;
1265 struct proc_inode *ei;
1267 if (!task)
1268 goto out_no_task;
1269 if (fd == ~0U)
1270 goto out;
1272 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1273 if (!inode)
1274 goto out;
1275 ei = PROC_I(inode);
1276 ei->fd = fd;
1277 files = get_files_struct(task);
1278 if (!files)
1279 goto out_unlock;
1280 inode->i_mode = S_IFLNK;
1283 * We are not taking a ref to the file structure, so we must
1284 * hold ->file_lock.
1286 spin_lock(&files->file_lock);
1287 file = fcheck_files(files, fd);
1288 if (!file)
1289 goto out_unlock2;
1290 if (file->f_mode & 1)
1291 inode->i_mode |= S_IRUSR | S_IXUSR;
1292 if (file->f_mode & 2)
1293 inode->i_mode |= S_IWUSR | S_IXUSR;
1294 spin_unlock(&files->file_lock);
1295 put_files_struct(files);
1296 inode->i_op = &proc_pid_link_inode_operations;
1297 inode->i_size = 64;
1298 ei->op.proc_get_link = proc_fd_link;
1299 dentry->d_op = &tid_fd_dentry_operations;
1300 d_add(dentry, inode);
1301 /* Close the race of the process dying before we return the dentry */
1302 if (tid_fd_revalidate(dentry, NULL))
1303 result = NULL;
1304 out:
1305 put_task_struct(task);
1306 out_no_task:
1307 return result;
1309 out_unlock2:
1310 spin_unlock(&files->file_lock);
1311 put_files_struct(files);
1312 out_unlock:
1313 iput(inode);
1314 goto out;
1317 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1319 struct dentry *dentry = filp->f_dentry;
1320 struct inode *inode = dentry->d_inode;
1321 struct task_struct *p = get_proc_task(inode);
1322 unsigned int fd, tid, ino;
1323 int retval;
1324 char buf[PROC_NUMBUF];
1325 struct files_struct * files;
1326 struct fdtable *fdt;
1328 retval = -ENOENT;
1329 if (!p)
1330 goto out_no_task;
1331 retval = 0;
1332 tid = p->pid;
1334 fd = filp->f_pos;
1335 switch (fd) {
1336 case 0:
1337 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1338 goto out;
1339 filp->f_pos++;
1340 case 1:
1341 ino = parent_ino(dentry);
1342 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1343 goto out;
1344 filp->f_pos++;
1345 default:
1346 files = get_files_struct(p);
1347 if (!files)
1348 goto out;
1349 rcu_read_lock();
1350 fdt = files_fdtable(files);
1351 for (fd = filp->f_pos-2;
1352 fd < fdt->max_fds;
1353 fd++, filp->f_pos++) {
1354 unsigned int i,j;
1356 if (!fcheck_files(files, fd))
1357 continue;
1358 rcu_read_unlock();
1360 j = PROC_NUMBUF;
1361 i = fd;
1362 do {
1363 j--;
1364 buf[j] = '0' + (i % 10);
1365 i /= 10;
1366 } while (i);
1368 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1369 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1370 rcu_read_lock();
1371 break;
1373 rcu_read_lock();
1375 rcu_read_unlock();
1376 put_files_struct(files);
1378 out:
1379 put_task_struct(p);
1380 out_no_task:
1381 return retval;
1384 static struct file_operations proc_fd_operations = {
1385 .read = generic_read_dir,
1386 .readdir = proc_readfd,
1390 * proc directories can do almost nothing..
1392 static struct inode_operations proc_fd_inode_operations = {
1393 .lookup = proc_lookupfd,
1394 .setattr = proc_setattr,
1397 /* SMP-safe */
1398 static struct dentry *proc_pident_lookup(struct inode *dir,
1399 struct dentry *dentry,
1400 struct pid_entry *ents)
1402 struct inode *inode;
1403 struct dentry *error;
1404 struct task_struct *task = get_proc_task(dir);
1405 struct pid_entry *p;
1406 struct proc_inode *ei;
1408 error = ERR_PTR(-ENOENT);
1409 inode = NULL;
1411 if (!task)
1412 goto out_no_task;
1415 * Yes, it does not scale. And it should not. Don't add
1416 * new entries into /proc/<tgid>/ without very good reasons.
1418 for (p = ents; p->name; p++) {
1419 if (p->len != dentry->d_name.len)
1420 continue;
1421 if (!memcmp(dentry->d_name.name, p->name, p->len))
1422 break;
1424 if (!p->name)
1425 goto out;
1427 error = ERR_PTR(-EINVAL);
1428 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1429 if (!inode)
1430 goto out;
1432 ei = PROC_I(inode);
1433 inode->i_mode = p->mode;
1434 if (S_ISDIR(inode->i_mode))
1435 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1436 if (p->iop)
1437 inode->i_op = p->iop;
1438 if (p->fop)
1439 inode->i_fop = p->fop;
1440 ei->op = p->op;
1441 dentry->d_op = &pid_dentry_operations;
1442 d_add(dentry, inode);
1443 /* Close the race of the process dying before we return the dentry */
1444 if (pid_revalidate(dentry, NULL))
1445 error = NULL;
1446 out:
1447 put_task_struct(task);
1448 out_no_task:
1449 return error;
1452 static int proc_pident_readdir(struct file *filp,
1453 void *dirent, filldir_t filldir,
1454 struct pid_entry *ents, unsigned int nents)
1456 int i;
1457 int pid;
1458 struct dentry *dentry = filp->f_dentry;
1459 struct inode *inode = dentry->d_inode;
1460 struct task_struct *task = get_proc_task(inode);
1461 struct pid_entry *p;
1462 ino_t ino;
1463 int ret;
1465 ret = -ENOENT;
1466 if (!task)
1467 goto out;
1469 ret = 0;
1470 pid = task->pid;
1471 put_task_struct(task);
1472 i = filp->f_pos;
1473 switch (i) {
1474 case 0:
1475 ino = inode->i_ino;
1476 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1477 goto out;
1478 i++;
1479 filp->f_pos++;
1480 /* fall through */
1481 case 1:
1482 ino = parent_ino(dentry);
1483 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1484 goto out;
1485 i++;
1486 filp->f_pos++;
1487 /* fall through */
1488 default:
1489 i -= 2;
1490 if (i >= nents) {
1491 ret = 1;
1492 goto out;
1494 p = ents + i;
1495 while (p->name) {
1496 if (filldir(dirent, p->name, p->len, filp->f_pos,
1497 fake_ino(pid, p->type), p->mode >> 12) < 0)
1498 goto out;
1499 filp->f_pos++;
1500 p++;
1504 ret = 1;
1505 out:
1506 return ret;
1509 #ifdef CONFIG_SECURITY
1510 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1511 size_t count, loff_t *ppos)
1513 struct inode * inode = file->f_dentry->d_inode;
1514 unsigned long page;
1515 ssize_t length;
1516 struct task_struct *task = get_proc_task(inode);
1518 length = -ESRCH;
1519 if (!task)
1520 goto out_no_task;
1522 if (count > PAGE_SIZE)
1523 count = PAGE_SIZE;
1524 length = -ENOMEM;
1525 if (!(page = __get_free_page(GFP_KERNEL)))
1526 goto out;
1528 length = security_getprocattr(task,
1529 (char*)file->f_dentry->d_name.name,
1530 (void*)page, count);
1531 if (length >= 0)
1532 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1533 free_page(page);
1534 out:
1535 put_task_struct(task);
1536 out_no_task:
1537 return length;
1540 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1541 size_t count, loff_t *ppos)
1543 struct inode * inode = file->f_dentry->d_inode;
1544 char *page;
1545 ssize_t length;
1546 struct task_struct *task = get_proc_task(inode);
1548 length = -ESRCH;
1549 if (!task)
1550 goto out_no_task;
1551 if (count > PAGE_SIZE)
1552 count = PAGE_SIZE;
1554 /* No partial writes. */
1555 length = -EINVAL;
1556 if (*ppos != 0)
1557 goto out;
1559 length = -ENOMEM;
1560 page = (char*)__get_free_page(GFP_USER);
1561 if (!page)
1562 goto out;
1564 length = -EFAULT;
1565 if (copy_from_user(page, buf, count))
1566 goto out_free;
1568 length = security_setprocattr(task,
1569 (char*)file->f_dentry->d_name.name,
1570 (void*)page, count);
1571 out_free:
1572 free_page((unsigned long) page);
1573 out:
1574 put_task_struct(task);
1575 out_no_task:
1576 return length;
1579 static struct file_operations proc_pid_attr_operations = {
1580 .read = proc_pid_attr_read,
1581 .write = proc_pid_attr_write,
1584 static struct pid_entry tgid_attr_stuff[] = {
1585 REG(PROC_TGID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1586 REG(PROC_TGID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1587 REG(PROC_TGID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1588 REG(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1589 REG(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1590 REG(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1593 static struct pid_entry tid_attr_stuff[] = {
1594 REG(PROC_TID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1595 REG(PROC_TID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1596 REG(PROC_TID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1597 REG(PROC_TID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1598 REG(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1599 REG(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1603 static int proc_tgid_attr_readdir(struct file * filp,
1604 void * dirent, filldir_t filldir)
1606 return proc_pident_readdir(filp,dirent,filldir,
1607 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1610 static int proc_tid_attr_readdir(struct file * filp,
1611 void * dirent, filldir_t filldir)
1613 return proc_pident_readdir(filp,dirent,filldir,
1614 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1617 static struct file_operations proc_tgid_attr_operations = {
1618 .read = generic_read_dir,
1619 .readdir = proc_tgid_attr_readdir,
1622 static struct file_operations proc_tid_attr_operations = {
1623 .read = generic_read_dir,
1624 .readdir = proc_tid_attr_readdir,
1627 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1628 struct dentry *dentry, struct nameidata *nd)
1630 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1633 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1634 struct dentry *dentry, struct nameidata *nd)
1636 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1639 static struct inode_operations proc_tgid_attr_inode_operations = {
1640 .lookup = proc_tgid_attr_lookup,
1641 .getattr = pid_getattr,
1642 .setattr = proc_setattr,
1645 static struct inode_operations proc_tid_attr_inode_operations = {
1646 .lookup = proc_tid_attr_lookup,
1647 .getattr = pid_getattr,
1648 .setattr = proc_setattr,
1650 #endif
1653 * /proc/self:
1655 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1656 int buflen)
1658 char tmp[PROC_NUMBUF];
1659 sprintf(tmp, "%d", current->tgid);
1660 return vfs_readlink(dentry,buffer,buflen,tmp);
1663 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1665 char tmp[PROC_NUMBUF];
1666 sprintf(tmp, "%d", current->tgid);
1667 return ERR_PTR(vfs_follow_link(nd,tmp));
1670 static struct inode_operations proc_self_inode_operations = {
1671 .readlink = proc_self_readlink,
1672 .follow_link = proc_self_follow_link,
1676 * Thread groups
1678 static struct file_operations proc_task_operations;
1679 static struct inode_operations proc_task_inode_operations;
1681 static struct pid_entry tgid_base_stuff[] = {
1682 DIR(PROC_TGID_TASK, "task", S_IRUGO|S_IXUGO, task),
1683 DIR(PROC_TGID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1684 INF(PROC_TGID_ENVIRON, "environ", S_IRUSR, pid_environ),
1685 INF(PROC_TGID_AUXV, "auxv", S_IRUSR, pid_auxv),
1686 INF(PROC_TGID_STATUS, "status", S_IRUGO, pid_status),
1687 INF(PROC_TGID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1688 INF(PROC_TGID_STAT, "stat", S_IRUGO, tgid_stat),
1689 INF(PROC_TGID_STATM, "statm", S_IRUGO, pid_statm),
1690 REG(PROC_TGID_MAPS, "maps", S_IRUGO, maps),
1691 #ifdef CONFIG_NUMA
1692 REG(PROC_TGID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
1693 #endif
1694 REG(PROC_TGID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
1695 #ifdef CONFIG_SECCOMP
1696 REG(PROC_TGID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
1697 #endif
1698 LNK(PROC_TGID_CWD, "cwd", cwd),
1699 LNK(PROC_TGID_ROOT, "root", root),
1700 LNK(PROC_TGID_EXE, "exe", exe),
1701 REG(PROC_TGID_MOUNTS, "mounts", S_IRUGO, mounts),
1702 REG(PROC_TGID_MOUNTSTATS, "mountstats", S_IRUSR, mountstats),
1703 #ifdef CONFIG_MMU
1704 REG(PROC_TGID_SMAPS, "smaps", S_IRUGO, smaps),
1705 #endif
1706 #ifdef CONFIG_SECURITY
1707 DIR(PROC_TGID_ATTR, "attr", S_IRUGO|S_IXUGO, tgid_attr),
1708 #endif
1709 #ifdef CONFIG_KALLSYMS
1710 INF(PROC_TGID_WCHAN, "wchan", S_IRUGO, pid_wchan),
1711 #endif
1712 #ifdef CONFIG_SCHEDSTATS
1713 INF(PROC_TGID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
1714 #endif
1715 #ifdef CONFIG_CPUSETS
1716 REG(PROC_TGID_CPUSET, "cpuset", S_IRUGO, cpuset),
1717 #endif
1718 INF(PROC_TGID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1719 REG(PROC_TGID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1720 #ifdef CONFIG_AUDITSYSCALL
1721 REG(PROC_TGID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
1722 #endif
1726 static int proc_tgid_base_readdir(struct file * filp,
1727 void * dirent, filldir_t filldir)
1729 return proc_pident_readdir(filp,dirent,filldir,
1730 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1733 static struct file_operations proc_tgid_base_operations = {
1734 .read = generic_read_dir,
1735 .readdir = proc_tgid_base_readdir,
1738 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1739 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1742 static struct inode_operations proc_tgid_base_inode_operations = {
1743 .lookup = proc_tgid_base_lookup,
1744 .getattr = pid_getattr,
1745 .setattr = proc_setattr,
1749 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1751 * @task: task that should be flushed.
1753 * Looks in the dcache for
1754 * /proc/@pid
1755 * /proc/@tgid/task/@pid
1756 * if either directory is present flushes it and all of it'ts children
1757 * from the dcache.
1759 * It is safe and reasonable to cache /proc entries for a task until
1760 * that task exits. After that they just clog up the dcache with
1761 * useless entries, possibly causing useful dcache entries to be
1762 * flushed instead. This routine is proved to flush those useless
1763 * dcache entries at process exit time.
1765 * NOTE: This routine is just an optimization so it does not guarantee
1766 * that no dcache entries will exist at process exit time it
1767 * just makes it very unlikely that any will persist.
1769 void proc_flush_task(struct task_struct *task)
1771 struct dentry *dentry, *leader, *dir;
1772 char buf[PROC_NUMBUF];
1773 struct qstr name;
1775 name.name = buf;
1776 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1777 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1778 if (dentry) {
1779 shrink_dcache_parent(dentry);
1780 d_drop(dentry);
1781 dput(dentry);
1784 if (thread_group_leader(task))
1785 goto out;
1787 name.name = buf;
1788 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1789 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1790 if (!leader)
1791 goto out;
1793 name.name = "task";
1794 name.len = strlen(name.name);
1795 dir = d_hash_and_lookup(leader, &name);
1796 if (!dir)
1797 goto out_put_leader;
1799 name.name = buf;
1800 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1801 dentry = d_hash_and_lookup(dir, &name);
1802 if (dentry) {
1803 shrink_dcache_parent(dentry);
1804 d_drop(dentry);
1805 dput(dentry);
1808 dput(dir);
1809 out_put_leader:
1810 dput(leader);
1811 out:
1812 return;
1815 /* SMP-safe */
1816 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1818 struct dentry *result = ERR_PTR(-ENOENT);
1819 struct task_struct *task;
1820 struct inode *inode;
1821 struct proc_inode *ei;
1822 unsigned tgid;
1824 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1825 inode = new_inode(dir->i_sb);
1826 if (!inode)
1827 return ERR_PTR(-ENOMEM);
1828 ei = PROC_I(inode);
1829 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1830 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1831 ei->pde = NULL;
1832 inode->i_mode = S_IFLNK|S_IRWXUGO;
1833 inode->i_uid = inode->i_gid = 0;
1834 inode->i_size = 64;
1835 inode->i_op = &proc_self_inode_operations;
1836 d_add(dentry, inode);
1837 return NULL;
1839 tgid = name_to_int(dentry);
1840 if (tgid == ~0U)
1841 goto out;
1843 rcu_read_lock();
1844 task = find_task_by_pid(tgid);
1845 if (task)
1846 get_task_struct(task);
1847 rcu_read_unlock();
1848 if (!task)
1849 goto out;
1851 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1852 if (!inode)
1853 goto out_put_task;
1855 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1856 inode->i_op = &proc_tgid_base_inode_operations;
1857 inode->i_fop = &proc_tgid_base_operations;
1858 inode->i_flags|=S_IMMUTABLE;
1859 #ifdef CONFIG_SECURITY
1860 inode->i_nlink = 5;
1861 #else
1862 inode->i_nlink = 4;
1863 #endif
1865 dentry->d_op = &pid_dentry_operations;
1867 d_add(dentry, inode);
1868 /* Close the race of the process dying before we return the dentry */
1869 if (pid_revalidate(dentry, NULL))
1870 result = NULL;
1872 out_put_task:
1873 put_task_struct(task);
1874 out:
1875 return result;
1879 * Find the first task with tgid >= tgid
1882 static struct task_struct *next_tgid(unsigned int tgid)
1884 struct task_struct *task;
1885 struct pid *pid;
1887 rcu_read_lock();
1888 retry:
1889 task = NULL;
1890 pid = find_ge_pid(tgid);
1891 if (pid) {
1892 tgid = pid->nr + 1;
1893 task = pid_task(pid, PIDTYPE_PID);
1894 /* What we to know is if the pid we have find is the
1895 * pid of a thread_group_leader. Testing for task
1896 * being a thread_group_leader is the obvious thing
1897 * todo but there is a window when it fails, due to
1898 * the pid transfer logic in de_thread.
1900 * So we perform the straight forward test of seeing
1901 * if the pid we have found is the pid of a thread
1902 * group leader, and don't worry if the task we have
1903 * found doesn't happen to be a thread group leader.
1904 * As we don't care in the case of readdir.
1906 if (!task || !has_group_leader_pid(task))
1907 goto retry;
1908 get_task_struct(task);
1910 rcu_read_unlock();
1911 return task;
1914 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */))
1916 /* for the /proc/ directory itself, after non-process stuff has been done */
1917 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1919 char buf[PROC_NUMBUF];
1920 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1921 struct task_struct *task;
1922 int tgid;
1924 if (!nr) {
1925 ino_t ino = fake_ino(0,PROC_TGID_INO);
1926 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1927 return 0;
1928 filp->f_pos++;
1929 nr++;
1932 tgid = filp->f_pos - TGID_OFFSET;
1933 for (task = next_tgid(tgid);
1934 task;
1935 put_task_struct(task), task = next_tgid(tgid + 1)) {
1936 int len;
1937 ino_t ino;
1938 tgid = task->pid;
1939 filp->f_pos = tgid + TGID_OFFSET;
1940 len = snprintf(buf, sizeof(buf), "%d", tgid);
1941 ino = fake_ino(tgid, PROC_TGID_INO);
1942 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
1943 put_task_struct(task);
1944 goto out;
1947 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
1948 out:
1949 return 0;
1953 * Tasks
1955 static struct pid_entry tid_base_stuff[] = {
1956 DIR(PROC_TID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1957 INF(PROC_TID_ENVIRON, "environ", S_IRUSR, pid_environ),
1958 INF(PROC_TID_AUXV, "auxv", S_IRUSR, pid_auxv),
1959 INF(PROC_TID_STATUS, "status", S_IRUGO, pid_status),
1960 INF(PROC_TID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1961 INF(PROC_TID_STAT, "stat", S_IRUGO, tid_stat),
1962 INF(PROC_TID_STATM, "statm", S_IRUGO, pid_statm),
1963 REG(PROC_TID_MAPS, "maps", S_IRUGO, maps),
1964 #ifdef CONFIG_NUMA
1965 REG(PROC_TID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
1966 #endif
1967 REG(PROC_TID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
1968 #ifdef CONFIG_SECCOMP
1969 REG(PROC_TID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
1970 #endif
1971 LNK(PROC_TID_CWD, "cwd", cwd),
1972 LNK(PROC_TID_ROOT, "root", root),
1973 LNK(PROC_TID_EXE, "exe", exe),
1974 REG(PROC_TID_MOUNTS, "mounts", S_IRUGO, mounts),
1975 #ifdef CONFIG_MMU
1976 REG(PROC_TID_SMAPS, "smaps", S_IRUGO, smaps),
1977 #endif
1978 #ifdef CONFIG_SECURITY
1979 DIR(PROC_TID_ATTR, "attr", S_IRUGO|S_IXUGO, tid_attr),
1980 #endif
1981 #ifdef CONFIG_KALLSYMS
1982 INF(PROC_TID_WCHAN, "wchan", S_IRUGO, pid_wchan),
1983 #endif
1984 #ifdef CONFIG_SCHEDSTATS
1985 INF(PROC_TID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
1986 #endif
1987 #ifdef CONFIG_CPUSETS
1988 REG(PROC_TID_CPUSET, "cpuset", S_IRUGO, cpuset),
1989 #endif
1990 INF(PROC_TID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1991 REG(PROC_TID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1992 #ifdef CONFIG_AUDITSYSCALL
1993 REG(PROC_TID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
1994 #endif
1998 static int proc_tid_base_readdir(struct file * filp,
1999 void * dirent, filldir_t filldir)
2001 return proc_pident_readdir(filp,dirent,filldir,
2002 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2005 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2006 return proc_pident_lookup(dir, dentry, tid_base_stuff);
2009 static struct file_operations proc_tid_base_operations = {
2010 .read = generic_read_dir,
2011 .readdir = proc_tid_base_readdir,
2014 static struct inode_operations proc_tid_base_inode_operations = {
2015 .lookup = proc_tid_base_lookup,
2016 .getattr = pid_getattr,
2017 .setattr = proc_setattr,
2020 /* SMP-safe */
2021 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2023 struct dentry *result = ERR_PTR(-ENOENT);
2024 struct task_struct *task;
2025 struct task_struct *leader = get_proc_task(dir);
2026 struct inode *inode;
2027 unsigned tid;
2029 if (!leader)
2030 goto out_no_task;
2032 tid = name_to_int(dentry);
2033 if (tid == ~0U)
2034 goto out;
2036 rcu_read_lock();
2037 task = find_task_by_pid(tid);
2038 if (task)
2039 get_task_struct(task);
2040 rcu_read_unlock();
2041 if (!task)
2042 goto out;
2043 if (leader->tgid != task->tgid)
2044 goto out_drop_task;
2046 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2049 if (!inode)
2050 goto out_drop_task;
2051 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2052 inode->i_op = &proc_tid_base_inode_operations;
2053 inode->i_fop = &proc_tid_base_operations;
2054 inode->i_flags|=S_IMMUTABLE;
2055 #ifdef CONFIG_SECURITY
2056 inode->i_nlink = 4;
2057 #else
2058 inode->i_nlink = 3;
2059 #endif
2061 dentry->d_op = &pid_dentry_operations;
2063 d_add(dentry, inode);
2064 /* Close the race of the process dying before we return the dentry */
2065 if (pid_revalidate(dentry, NULL))
2066 result = NULL;
2068 out_drop_task:
2069 put_task_struct(task);
2070 out:
2071 put_task_struct(leader);
2072 out_no_task:
2073 return result;
2077 * Find the first tid of a thread group to return to user space.
2079 * Usually this is just the thread group leader, but if the users
2080 * buffer was too small or there was a seek into the middle of the
2081 * directory we have more work todo.
2083 * In the case of a short read we start with find_task_by_pid.
2085 * In the case of a seek we start with the leader and walk nr
2086 * threads past it.
2088 static struct task_struct *first_tid(struct task_struct *leader,
2089 int tid, int nr)
2091 struct task_struct *pos;
2093 rcu_read_lock();
2094 /* Attempt to start with the pid of a thread */
2095 if (tid && (nr > 0)) {
2096 pos = find_task_by_pid(tid);
2097 if (pos && (pos->group_leader == leader))
2098 goto found;
2101 /* If nr exceeds the number of threads there is nothing todo */
2102 pos = NULL;
2103 if (nr && nr >= get_nr_threads(leader))
2104 goto out;
2106 /* If we haven't found our starting place yet start
2107 * with the leader and walk nr threads forward.
2109 for (pos = leader; nr > 0; --nr) {
2110 pos = next_thread(pos);
2111 if (pos == leader) {
2112 pos = NULL;
2113 goto out;
2116 found:
2117 get_task_struct(pos);
2118 out:
2119 rcu_read_unlock();
2120 return pos;
2124 * Find the next thread in the thread list.
2125 * Return NULL if there is an error or no next thread.
2127 * The reference to the input task_struct is released.
2129 static struct task_struct *next_tid(struct task_struct *start)
2131 struct task_struct *pos = NULL;
2132 rcu_read_lock();
2133 if (pid_alive(start)) {
2134 pos = next_thread(start);
2135 if (thread_group_leader(pos))
2136 pos = NULL;
2137 else
2138 get_task_struct(pos);
2140 rcu_read_unlock();
2141 put_task_struct(start);
2142 return pos;
2145 /* for the /proc/TGID/task/ directories */
2146 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2148 char buf[PROC_NUMBUF];
2149 struct dentry *dentry = filp->f_dentry;
2150 struct inode *inode = dentry->d_inode;
2151 struct task_struct *leader = get_proc_task(inode);
2152 struct task_struct *task;
2153 int retval = -ENOENT;
2154 ino_t ino;
2155 int tid;
2156 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2158 if (!leader)
2159 goto out_no_task;
2160 retval = 0;
2162 switch (pos) {
2163 case 0:
2164 ino = inode->i_ino;
2165 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2166 goto out;
2167 pos++;
2168 /* fall through */
2169 case 1:
2170 ino = parent_ino(dentry);
2171 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2172 goto out;
2173 pos++;
2174 /* fall through */
2177 /* f_version caches the tgid value that the last readdir call couldn't
2178 * return. lseek aka telldir automagically resets f_version to 0.
2180 tid = filp->f_version;
2181 filp->f_version = 0;
2182 for (task = first_tid(leader, tid, pos - 2);
2183 task;
2184 task = next_tid(task), pos++) {
2185 int len;
2186 tid = task->pid;
2187 len = snprintf(buf, sizeof(buf), "%d", tid);
2188 ino = fake_ino(tid, PROC_TID_INO);
2189 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2190 /* returning this tgid failed, save it as the first
2191 * pid for the next readir call */
2192 filp->f_version = tid;
2193 put_task_struct(task);
2194 break;
2197 out:
2198 filp->f_pos = pos;
2199 put_task_struct(leader);
2200 out_no_task:
2201 return retval;
2204 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2206 struct inode *inode = dentry->d_inode;
2207 struct task_struct *p = get_proc_task(inode);
2208 generic_fillattr(inode, stat);
2210 if (p) {
2211 rcu_read_lock();
2212 stat->nlink += get_nr_threads(p);
2213 rcu_read_unlock();
2214 put_task_struct(p);
2217 return 0;
2220 static struct inode_operations proc_task_inode_operations = {
2221 .lookup = proc_task_lookup,
2222 .getattr = proc_task_getattr,
2223 .setattr = proc_setattr,
2226 static struct file_operations proc_task_operations = {
2227 .read = generic_read_dir,
2228 .readdir = proc_task_readdir,