RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / proc / base.c
blob30033f3de822963d5577fa6745eb4825f96d76ae
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/mnt_namespace.h>
63 #include <linux/mm.h>
64 #include <linux/rcupdate.h>
65 #include <linux/kallsyms.h>
66 #include <linux/module.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
73 #include <linux/poll.h>
74 #include <linux/nsproxy.h>
75 #include <linux/oom.h>
76 #include "internal.h"
78 /* NOTE:
79 * Implementing inode permission operations in /proc is almost
80 * certainly an error. Permission checks need to happen during
81 * each system call not at open time. The reason is that most of
82 * what we wish to check for permissions in /proc varies at runtime.
84 * The classic example of a problem is opening file descriptors
85 * in /proc for a task before it execs a suid executable.
89 /* Worst case buffer size needed for holding an integer. */
90 #define PROC_NUMBUF 13
92 struct pid_entry {
93 char *name;
94 int len;
95 mode_t mode;
96 const struct inode_operations *iop;
97 const struct file_operations *fop;
98 union proc_op op;
101 #define NOD(NAME, MODE, IOP, FOP, OP) { \
102 .name = (NAME), \
103 .len = sizeof(NAME) - 1, \
104 .mode = MODE, \
105 .iop = IOP, \
106 .fop = FOP, \
107 .op = OP, \
110 #define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 {} )
114 #define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
118 #define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
120 &proc_##OTYPE##_operations, {})
121 #define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
126 int maps_protect;
127 EXPORT_SYMBOL(maps_protect);
129 static struct fs_struct *get_fs_struct(struct task_struct *task)
131 struct fs_struct *fs;
132 task_lock(task);
133 fs = task->fs;
134 if(fs)
135 atomic_inc(&fs->count);
136 task_unlock(task);
137 return fs;
140 static int get_nr_threads(struct task_struct *tsk)
142 /* Must be called with the rcu_read_lock held */
143 unsigned long flags;
144 int count = 0;
146 if (lock_task_sighand(tsk, &flags)) {
147 count = atomic_read(&tsk->signal->count);
148 unlock_task_sighand(tsk, &flags);
150 return count;
153 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
155 struct task_struct *task = get_proc_task(inode);
156 struct fs_struct *fs = NULL;
157 int result = -ENOENT;
159 if (task) {
160 fs = get_fs_struct(task);
161 put_task_struct(task);
163 if (fs) {
164 read_lock(&fs->lock);
165 *mnt = mntget(fs->pwdmnt);
166 *dentry = dget(fs->pwd);
167 read_unlock(&fs->lock);
168 result = 0;
169 put_fs_struct(fs);
171 return result;
174 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
176 struct task_struct *task = get_proc_task(inode);
177 struct fs_struct *fs = NULL;
178 int result = -ENOENT;
180 if (task) {
181 fs = get_fs_struct(task);
182 put_task_struct(task);
184 if (fs) {
185 read_lock(&fs->lock);
186 *mnt = mntget(fs->rootmnt);
187 *dentry = dget(fs->root);
188 read_unlock(&fs->lock);
189 result = 0;
190 put_fs_struct(fs);
192 return result;
195 #define MAY_PTRACE(task) \
196 (task == current || \
197 (task->parent == current && \
198 (task->ptrace & PT_PTRACED) && \
199 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
200 security_ptrace(current,task) == 0))
202 static int proc_pid_environ(struct task_struct *task, char * buffer)
204 int res = 0;
205 struct mm_struct *mm = get_task_mm(task);
206 if (mm) {
207 unsigned int len = mm->env_end - mm->env_start;
208 if (len > PAGE_SIZE)
209 len = PAGE_SIZE;
210 res = access_process_vm(task, mm->env_start, buffer, len, 0);
211 if (!ptrace_may_attach(task))
212 res = -ESRCH;
213 mmput(mm);
215 return res;
218 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
220 int res = 0;
221 unsigned int len;
222 struct mm_struct *mm = get_task_mm(task);
223 if (!mm)
224 goto out;
225 if (!mm->arg_end)
226 goto out_mm; /* Shh! No looking before we're done */
228 len = mm->arg_end - mm->arg_start;
230 if (len > PAGE_SIZE)
231 len = PAGE_SIZE;
233 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
235 // If the nul at the end of args has been overwritten, then
236 // assume application is using setproctitle(3).
237 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
238 len = strnlen(buffer, res);
239 if (len < res) {
240 res = len;
241 } else {
242 len = mm->env_end - mm->env_start;
243 if (len > PAGE_SIZE - res)
244 len = PAGE_SIZE - res;
245 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
246 res = strnlen(buffer, res);
249 out_mm:
250 mmput(mm);
251 out:
252 return res;
255 static int proc_pid_auxv(struct task_struct *task, char *buffer)
257 int res = 0;
258 struct mm_struct *mm = get_task_mm(task);
259 if (mm) {
260 unsigned int nwords = 0;
262 nwords += 2;
263 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
264 res = nwords * sizeof(mm->saved_auxv[0]);
265 if (res > PAGE_SIZE)
266 res = PAGE_SIZE;
267 memcpy(buffer, mm->saved_auxv, res);
268 mmput(mm);
270 return res;
274 #ifdef CONFIG_KALLSYMS
276 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
277 * Returns the resolved symbol. If that fails, simply return the address.
279 static int proc_pid_wchan(struct task_struct *task, char *buffer)
281 unsigned long wchan;
282 char symname[KSYM_NAME_LEN];
284 wchan = get_wchan(task);
286 if (lookup_symbol_name(wchan, symname) < 0)
287 return sprintf(buffer, "%lu", wchan);
288 else
289 return sprintf(buffer, "%s", symname);
291 #endif /* CONFIG_KALLSYMS */
293 #ifdef CONFIG_SCHEDSTATS
295 * Provides /proc/PID/schedstat
297 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
299 return sprintf(buffer, "%lu %lu %lu\n",
300 task->sched_info.cpu_time,
301 task->sched_info.run_delay,
302 task->sched_info.pcnt);
304 #endif
306 /* The badness from the OOM killer */
307 unsigned long badness(struct task_struct *p, unsigned long uptime);
308 static int proc_oom_score(struct task_struct *task, char *buffer)
310 unsigned long points = 0;
311 struct timespec uptime;
313 do_posix_clock_monotonic_gettime(&uptime);
314 read_lock(&tasklist_lock);
315 if (pid_alive(task))
316 points = badness(task, uptime.tv_sec);
317 read_unlock(&tasklist_lock);
318 return sprintf(buffer, "%lu\n", points);
321 /************************************************************************/
322 /* Here the fs part begins */
323 /************************************************************************/
325 /* permission checks */
326 static int proc_fd_access_allowed(struct inode *inode)
328 struct task_struct *task;
329 int allowed = 0;
330 /* Allow access to a task's file descriptors if it is us or we
331 * may use ptrace attach to the process and find out that
332 * information.
334 task = get_proc_task(inode);
335 if (task) {
336 allowed = ptrace_may_attach(task);
337 put_task_struct(task);
339 return allowed;
342 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
344 int error;
345 struct inode *inode = dentry->d_inode;
347 if (attr->ia_valid & ATTR_MODE)
348 return -EPERM;
350 error = inode_change_ok(inode, attr);
351 if (!error)
352 error = inode_setattr(inode, attr);
353 return error;
356 static const struct inode_operations proc_def_inode_operations = {
357 .setattr = proc_setattr,
360 extern struct seq_operations mounts_op;
361 struct proc_mounts {
362 struct seq_file m;
363 int event;
366 static int mounts_open(struct inode *inode, struct file *file)
368 struct task_struct *task = get_proc_task(inode);
369 struct mnt_namespace *ns = NULL;
370 struct proc_mounts *p;
371 int ret = -EINVAL;
373 if (task) {
374 task_lock(task);
375 if (task->nsproxy) {
376 ns = task->nsproxy->mnt_ns;
377 if (ns)
378 get_mnt_ns(ns);
380 task_unlock(task);
381 put_task_struct(task);
384 if (ns) {
385 ret = -ENOMEM;
386 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
387 if (p) {
388 file->private_data = &p->m;
389 ret = seq_open(file, &mounts_op);
390 if (!ret) {
391 p->m.private = ns;
392 p->event = ns->event;
393 return 0;
395 kfree(p);
397 put_mnt_ns(ns);
399 return ret;
402 static int mounts_release(struct inode *inode, struct file *file)
404 struct seq_file *m = file->private_data;
405 struct mnt_namespace *ns = m->private;
406 put_mnt_ns(ns);
407 return seq_release(inode, file);
410 static unsigned mounts_poll(struct file *file, poll_table *wait)
412 struct proc_mounts *p = file->private_data;
413 struct mnt_namespace *ns = p->m.private;
414 unsigned res = 0;
416 poll_wait(file, &ns->poll, wait);
418 spin_lock(&vfsmount_lock);
419 if (p->event != ns->event) {
420 p->event = ns->event;
421 res = POLLERR;
423 spin_unlock(&vfsmount_lock);
425 return res;
428 static const struct file_operations proc_mounts_operations = {
429 .open = mounts_open,
430 .read = seq_read,
431 .llseek = seq_lseek,
432 .release = mounts_release,
433 .poll = mounts_poll,
436 extern struct seq_operations mountstats_op;
437 static int mountstats_open(struct inode *inode, struct file *file)
439 int ret = seq_open(file, &mountstats_op);
441 if (!ret) {
442 struct seq_file *m = file->private_data;
443 struct mnt_namespace *mnt_ns = NULL;
444 struct task_struct *task = get_proc_task(inode);
446 if (task) {
447 task_lock(task);
448 if (task->nsproxy)
449 mnt_ns = task->nsproxy->mnt_ns;
450 if (mnt_ns)
451 get_mnt_ns(mnt_ns);
452 task_unlock(task);
453 put_task_struct(task);
456 if (mnt_ns)
457 m->private = mnt_ns;
458 else {
459 seq_release(inode, file);
460 ret = -EINVAL;
463 return ret;
466 static const struct file_operations proc_mountstats_operations = {
467 .open = mountstats_open,
468 .read = seq_read,
469 .llseek = seq_lseek,
470 .release = mounts_release,
473 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
475 static ssize_t proc_info_read(struct file * file, char __user * buf,
476 size_t count, loff_t *ppos)
478 struct inode * inode = file->f_path.dentry->d_inode;
479 unsigned long page;
480 ssize_t length;
481 struct task_struct *task = get_proc_task(inode);
483 length = -ESRCH;
484 if (!task)
485 goto out_no_task;
487 if (count > PROC_BLOCK_SIZE)
488 count = PROC_BLOCK_SIZE;
490 length = -ENOMEM;
491 if (!(page = __get_free_page(GFP_KERNEL)))
492 goto out;
494 length = PROC_I(inode)->op.proc_read(task, (char*)page);
496 if (length >= 0)
497 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
498 free_page(page);
499 out:
500 put_task_struct(task);
501 out_no_task:
502 return length;
505 static const struct file_operations proc_info_file_operations = {
506 .read = proc_info_read,
509 static int mem_open(struct inode* inode, struct file* file)
511 file->private_data = (void*)((long)current->self_exec_id);
512 return 0;
515 static ssize_t mem_read(struct file * file, char __user * buf,
516 size_t count, loff_t *ppos)
518 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
519 char *page;
520 unsigned long src = *ppos;
521 int ret = -ESRCH;
522 struct mm_struct *mm;
524 if (!task)
525 goto out_no_task;
527 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
528 goto out;
530 ret = -ENOMEM;
531 page = (char *)__get_free_page(GFP_USER);
532 if (!page)
533 goto out;
535 ret = 0;
537 mm = get_task_mm(task);
538 if (!mm)
539 goto out_free;
541 ret = -EIO;
543 if (file->private_data != (void*)((long)current->self_exec_id))
544 goto out_put;
546 ret = 0;
548 while (count > 0) {
549 int this_len, retval;
551 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
552 retval = access_process_vm(task, src, page, this_len, 0);
553 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
554 if (!ret)
555 ret = -EIO;
556 break;
559 if (copy_to_user(buf, page, retval)) {
560 ret = -EFAULT;
561 break;
564 ret += retval;
565 src += retval;
566 buf += retval;
567 count -= retval;
569 *ppos = src;
571 out_put:
572 mmput(mm);
573 out_free:
574 free_page((unsigned long) page);
575 out:
576 put_task_struct(task);
577 out_no_task:
578 return ret;
581 #define mem_write NULL
583 #ifndef mem_write
584 /* This is a security hazard */
585 static ssize_t mem_write(struct file * file, const char __user *buf,
586 size_t count, loff_t *ppos)
588 int copied;
589 char *page;
590 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
591 unsigned long dst = *ppos;
593 copied = -ESRCH;
594 if (!task)
595 goto out_no_task;
597 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
598 goto out;
600 copied = -ENOMEM;
601 page = (char *)__get_free_page(GFP_USER);
602 if (!page)
603 goto out;
605 copied = 0;
606 while (count > 0) {
607 int this_len, retval;
609 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
610 if (copy_from_user(page, buf, this_len)) {
611 copied = -EFAULT;
612 break;
614 retval = access_process_vm(task, dst, page, this_len, 1);
615 if (!retval) {
616 if (!copied)
617 copied = -EIO;
618 break;
620 copied += retval;
621 buf += retval;
622 dst += retval;
623 count -= retval;
625 *ppos = dst;
626 free_page((unsigned long) page);
627 out:
628 put_task_struct(task);
629 out_no_task:
630 return copied;
632 #endif
634 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
636 switch (orig) {
637 case 0:
638 file->f_pos = offset;
639 break;
640 case 1:
641 file->f_pos += offset;
642 break;
643 default:
644 return -EINVAL;
646 force_successful_syscall_return();
647 return file->f_pos;
650 static const struct file_operations proc_mem_operations = {
651 .llseek = mem_lseek,
652 .read = mem_read,
653 .write = mem_write,
654 .open = mem_open,
657 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
658 size_t count, loff_t *ppos)
660 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
661 char buffer[PROC_NUMBUF];
662 size_t len;
663 int oom_adjust;
665 if (!task)
666 return -ESRCH;
667 oom_adjust = task->oomkilladj;
668 put_task_struct(task);
670 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
672 return simple_read_from_buffer(buf, count, ppos, buffer, len);
675 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
676 size_t count, loff_t *ppos)
678 struct task_struct *task;
679 char buffer[PROC_NUMBUF], *end;
680 int oom_adjust;
682 memset(buffer, 0, sizeof(buffer));
683 if (count > sizeof(buffer) - 1)
684 count = sizeof(buffer) - 1;
685 if (copy_from_user(buffer, buf, count))
686 return -EFAULT;
687 oom_adjust = simple_strtol(buffer, &end, 0);
688 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
689 oom_adjust != OOM_DISABLE)
690 return -EINVAL;
691 if (*end == '\n')
692 end++;
693 task = get_proc_task(file->f_path.dentry->d_inode);
694 if (!task)
695 return -ESRCH;
696 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
697 put_task_struct(task);
698 return -EACCES;
700 task->oomkilladj = oom_adjust;
701 put_task_struct(task);
702 if (end - buffer == 0)
703 return -EIO;
704 return end - buffer;
707 static const struct file_operations proc_oom_adjust_operations = {
708 .read = oom_adjust_read,
709 .write = oom_adjust_write,
712 #ifdef CONFIG_MMU
713 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
714 size_t count, loff_t *ppos)
716 struct task_struct *task;
717 char buffer[PROC_NUMBUF], *end;
718 struct mm_struct *mm;
720 memset(buffer, 0, sizeof(buffer));
721 if (count > sizeof(buffer) - 1)
722 count = sizeof(buffer) - 1;
723 if (copy_from_user(buffer, buf, count))
724 return -EFAULT;
725 if (!simple_strtol(buffer, &end, 0))
726 return -EINVAL;
727 if (*end == '\n')
728 end++;
729 task = get_proc_task(file->f_path.dentry->d_inode);
730 if (!task)
731 return -ESRCH;
732 mm = get_task_mm(task);
733 if (mm) {
734 clear_refs_smap(mm);
735 mmput(mm);
737 put_task_struct(task);
738 if (end - buffer == 0)
739 return -EIO;
740 return end - buffer;
743 static struct file_operations proc_clear_refs_operations = {
744 .write = clear_refs_write,
746 #endif
748 #ifdef CONFIG_AUDITSYSCALL
749 #define TMPBUFLEN 21
750 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
751 size_t count, loff_t *ppos)
753 struct inode * inode = file->f_path.dentry->d_inode;
754 struct task_struct *task = get_proc_task(inode);
755 ssize_t length;
756 char tmpbuf[TMPBUFLEN];
758 if (!task)
759 return -ESRCH;
760 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
761 audit_get_loginuid(task->audit_context));
762 put_task_struct(task);
763 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
766 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
767 size_t count, loff_t *ppos)
769 struct inode * inode = file->f_path.dentry->d_inode;
770 char *page, *tmp;
771 ssize_t length;
772 uid_t loginuid;
774 if (!capable(CAP_AUDIT_CONTROL))
775 return -EPERM;
777 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
778 return -EPERM;
780 if (count >= PAGE_SIZE)
781 count = PAGE_SIZE - 1;
783 if (*ppos != 0) {
784 /* No partial writes. */
785 return -EINVAL;
787 page = (char*)__get_free_page(GFP_USER);
788 if (!page)
789 return -ENOMEM;
790 length = -EFAULT;
791 if (copy_from_user(page, buf, count))
792 goto out_free_page;
794 page[count] = '\0';
795 loginuid = simple_strtoul(page, &tmp, 10);
796 if (tmp == page) {
797 length = -EINVAL;
798 goto out_free_page;
801 length = audit_set_loginuid(current, loginuid);
802 if (likely(length == 0))
803 length = count;
805 out_free_page:
806 free_page((unsigned long) page);
807 return length;
810 static const struct file_operations proc_loginuid_operations = {
811 .read = proc_loginuid_read,
812 .write = proc_loginuid_write,
814 #endif
816 #ifdef CONFIG_SECCOMP
817 static ssize_t seccomp_read(struct file *file, char __user *buf,
818 size_t count, loff_t *ppos)
820 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
821 char __buf[20];
822 size_t len;
824 if (!tsk)
825 return -ESRCH;
826 /* no need to print the trailing zero, so use only len */
827 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
828 put_task_struct(tsk);
830 return simple_read_from_buffer(buf, count, ppos, __buf, len);
833 static ssize_t seccomp_write(struct file *file, const char __user *buf,
834 size_t count, loff_t *ppos)
836 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
837 char __buf[20], *end;
838 unsigned int seccomp_mode;
839 ssize_t result;
841 result = -ESRCH;
842 if (!tsk)
843 goto out_no_task;
845 /* can set it only once to be even more secure */
846 result = -EPERM;
847 if (unlikely(tsk->seccomp.mode))
848 goto out;
850 result = -EFAULT;
851 memset(__buf, 0, sizeof(__buf));
852 count = min(count, sizeof(__buf) - 1);
853 if (copy_from_user(__buf, buf, count))
854 goto out;
856 seccomp_mode = simple_strtoul(__buf, &end, 0);
857 if (*end == '\n')
858 end++;
859 result = -EINVAL;
860 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
861 tsk->seccomp.mode = seccomp_mode;
862 set_tsk_thread_flag(tsk, TIF_SECCOMP);
863 } else
864 goto out;
865 result = -EIO;
866 if (unlikely(!(end - __buf)))
867 goto out;
868 result = end - __buf;
869 out:
870 put_task_struct(tsk);
871 out_no_task:
872 return result;
875 static const struct file_operations proc_seccomp_operations = {
876 .read = seccomp_read,
877 .write = seccomp_write,
879 #endif /* CONFIG_SECCOMP */
881 #ifdef CONFIG_FAULT_INJECTION
882 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
883 size_t count, loff_t *ppos)
885 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
886 char buffer[PROC_NUMBUF];
887 size_t len;
888 int make_it_fail;
890 if (!task)
891 return -ESRCH;
892 make_it_fail = task->make_it_fail;
893 put_task_struct(task);
895 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
897 return simple_read_from_buffer(buf, count, ppos, buffer, len);
900 static ssize_t proc_fault_inject_write(struct file * file,
901 const char __user * buf, size_t count, loff_t *ppos)
903 struct task_struct *task;
904 char buffer[PROC_NUMBUF], *end;
905 int make_it_fail;
907 if (!capable(CAP_SYS_RESOURCE))
908 return -EPERM;
909 memset(buffer, 0, sizeof(buffer));
910 if (count > sizeof(buffer) - 1)
911 count = sizeof(buffer) - 1;
912 if (copy_from_user(buffer, buf, count))
913 return -EFAULT;
914 make_it_fail = simple_strtol(buffer, &end, 0);
915 if (*end == '\n')
916 end++;
917 task = get_proc_task(file->f_dentry->d_inode);
918 if (!task)
919 return -ESRCH;
920 task->make_it_fail = make_it_fail;
921 put_task_struct(task);
922 if (end - buffer == 0)
923 return -EIO;
924 return end - buffer;
927 static const struct file_operations proc_fault_inject_operations = {
928 .read = proc_fault_inject_read,
929 .write = proc_fault_inject_write,
931 #endif
933 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
935 struct inode *inode = dentry->d_inode;
936 int error = -EACCES;
938 /* We don't need a base pointer in the /proc filesystem */
939 path_release(nd);
941 /* Are we allowed to snoop on the tasks file descriptors? */
942 if (!proc_fd_access_allowed(inode))
943 goto out;
945 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
946 nd->last_type = LAST_BIND;
947 out:
948 return ERR_PTR(error);
951 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
952 char __user *buffer, int buflen)
954 struct inode * inode;
955 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
956 int len;
958 if (!tmp)
959 return -ENOMEM;
961 inode = dentry->d_inode;
962 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
963 len = PTR_ERR(path);
964 if (IS_ERR(path))
965 goto out;
966 len = tmp + PAGE_SIZE - 1 - path;
968 if (len > buflen)
969 len = buflen;
970 if (copy_to_user(buffer, path, len))
971 len = -EFAULT;
972 out:
973 free_page((unsigned long)tmp);
974 return len;
977 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
979 int error = -EACCES;
980 struct inode *inode = dentry->d_inode;
981 struct dentry *de;
982 struct vfsmount *mnt = NULL;
984 /* Are we allowed to snoop on the tasks file descriptors? */
985 if (!proc_fd_access_allowed(inode))
986 goto out;
988 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
989 if (error)
990 goto out;
992 error = do_proc_readlink(de, mnt, buffer, buflen);
993 dput(de);
994 mntput(mnt);
995 out:
996 return error;
999 static const struct inode_operations proc_pid_link_inode_operations = {
1000 .readlink = proc_pid_readlink,
1001 .follow_link = proc_pid_follow_link,
1002 .setattr = proc_setattr,
1006 /* building an inode */
1008 static int task_dumpable(struct task_struct *task)
1010 int dumpable = 0;
1011 struct mm_struct *mm;
1013 task_lock(task);
1014 mm = task->mm;
1015 if (mm)
1016 dumpable = mm->dumpable;
1017 task_unlock(task);
1018 if(dumpable == 1)
1019 return 1;
1020 return 0;
1024 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1026 struct inode * inode;
1027 struct proc_inode *ei;
1029 /* We need a new inode */
1031 inode = new_inode(sb);
1032 if (!inode)
1033 goto out;
1035 /* Common stuff */
1036 ei = PROC_I(inode);
1037 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1038 inode->i_op = &proc_def_inode_operations;
1041 * grab the reference to task.
1043 ei->pid = get_task_pid(task, PIDTYPE_PID);
1044 if (!ei->pid)
1045 goto out_unlock;
1047 inode->i_uid = 0;
1048 inode->i_gid = 0;
1049 if (task_dumpable(task)) {
1050 inode->i_uid = task->euid;
1051 inode->i_gid = task->egid;
1053 security_task_to_inode(task, inode);
1055 out:
1056 return inode;
1058 out_unlock:
1059 iput(inode);
1060 return NULL;
1063 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1065 struct inode *inode = dentry->d_inode;
1066 struct task_struct *task;
1067 generic_fillattr(inode, stat);
1069 rcu_read_lock();
1070 stat->uid = 0;
1071 stat->gid = 0;
1072 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1073 if (task) {
1074 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1075 task_dumpable(task)) {
1076 stat->uid = task->euid;
1077 stat->gid = task->egid;
1080 rcu_read_unlock();
1081 return 0;
1084 /* dentry stuff */
1087 * Exceptional case: normally we are not allowed to unhash a busy
1088 * directory. In this case, however, we can do it - no aliasing problems
1089 * due to the way we treat inodes.
1091 * Rewrite the inode's ownerships here because the owning task may have
1092 * performed a setuid(), etc.
1094 * Before the /proc/pid/status file was created the only way to read
1095 * the effective uid of a /process was to stat /proc/pid. Reading
1096 * /proc/pid/status is slow enough that procps and other packages
1097 * kept stating /proc/pid. To keep the rules in /proc simple I have
1098 * made this apply to all per process world readable and executable
1099 * directories.
1101 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1103 struct inode *inode = dentry->d_inode;
1104 struct task_struct *task = get_proc_task(inode);
1105 if (task) {
1106 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1107 task_dumpable(task)) {
1108 inode->i_uid = task->euid;
1109 inode->i_gid = task->egid;
1110 } else {
1111 inode->i_uid = 0;
1112 inode->i_gid = 0;
1114 inode->i_mode &= ~(S_ISUID | S_ISGID);
1115 security_task_to_inode(task, inode);
1116 put_task_struct(task);
1117 return 1;
1119 d_drop(dentry);
1120 return 0;
1123 static int pid_delete_dentry(struct dentry * dentry)
1125 /* Is the task we represent dead?
1126 * If so, then don't put the dentry on the lru list,
1127 * kill it immediately.
1129 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1132 static struct dentry_operations pid_dentry_operations =
1134 .d_revalidate = pid_revalidate,
1135 .d_delete = pid_delete_dentry,
1138 /* Lookups */
1140 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1141 struct task_struct *, const void *);
1144 * Fill a directory entry.
1146 * If possible create the dcache entry and derive our inode number and
1147 * file type from dcache entry.
1149 * Since all of the proc inode numbers are dynamically generated, the inode
1150 * numbers do not exist until the inode is cache. This means creating the
1151 * the dcache entry in readdir is necessary to keep the inode numbers
1152 * reported by readdir in sync with the inode numbers reported
1153 * by stat.
1155 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1156 char *name, int len,
1157 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1159 struct dentry *child, *dir = filp->f_path.dentry;
1160 struct inode *inode;
1161 struct qstr qname;
1162 ino_t ino = 0;
1163 unsigned type = DT_UNKNOWN;
1165 qname.name = name;
1166 qname.len = len;
1167 qname.hash = full_name_hash(name, len);
1169 child = d_lookup(dir, &qname);
1170 if (!child) {
1171 struct dentry *new;
1172 new = d_alloc(dir, &qname);
1173 if (new) {
1174 child = instantiate(dir->d_inode, new, task, ptr);
1175 if (child)
1176 dput(new);
1177 else
1178 child = new;
1181 if (!child || IS_ERR(child) || !child->d_inode)
1182 goto end_instantiate;
1183 inode = child->d_inode;
1184 if (inode) {
1185 ino = inode->i_ino;
1186 type = inode->i_mode >> 12;
1188 dput(child);
1189 end_instantiate:
1190 if (!ino)
1191 ino = find_inode_number(dir, &qname);
1192 if (!ino)
1193 ino = 1;
1194 return filldir(dirent, name, len, filp->f_pos, ino, type);
1197 static unsigned name_to_int(struct dentry *dentry)
1199 const char *name = dentry->d_name.name;
1200 int len = dentry->d_name.len;
1201 unsigned n = 0;
1203 if (len > 1 && *name == '0')
1204 goto out;
1205 while (len-- > 0) {
1206 unsigned c = *name++ - '0';
1207 if (c > 9)
1208 goto out;
1209 if (n >= (~0U-9)/10)
1210 goto out;
1211 n *= 10;
1212 n += c;
1214 return n;
1215 out:
1216 return ~0U;
1219 #define PROC_FDINFO_MAX 64
1221 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1222 struct vfsmount **mnt, char *info)
1224 struct task_struct *task = get_proc_task(inode);
1225 struct files_struct *files = NULL;
1226 struct file *file;
1227 int fd = proc_fd(inode);
1229 if (task) {
1230 files = get_files_struct(task);
1231 put_task_struct(task);
1233 if (files) {
1235 * We are not taking a ref to the file structure, so we must
1236 * hold ->file_lock.
1238 spin_lock(&files->file_lock);
1239 file = fcheck_files(files, fd);
1240 if (file) {
1241 unsigned int f_flags;
1242 struct fdtable *fdt;
1244 fdt = files_fdtable(files);
1245 f_flags = file->f_flags & ~O_CLOEXEC;
1246 if (FD_ISSET(fd, fdt->close_on_exec))
1247 f_flags |= O_CLOEXEC;
1249 if (mnt)
1250 *mnt = mntget(file->f_path.mnt);
1251 if (dentry)
1252 *dentry = dget(file->f_path.dentry);
1253 if (info)
1254 snprintf(info, PROC_FDINFO_MAX,
1255 "pos:\t%lli\n"
1256 "flags:\t0%o\n",
1257 (long long) file->f_pos,
1258 f_flags);
1259 spin_unlock(&files->file_lock);
1260 put_files_struct(files);
1261 return 0;
1263 spin_unlock(&files->file_lock);
1264 put_files_struct(files);
1266 return -ENOENT;
1269 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1270 struct vfsmount **mnt)
1272 return proc_fd_info(inode, dentry, mnt, NULL);
1275 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1277 struct inode *inode = dentry->d_inode;
1278 struct task_struct *task = get_proc_task(inode);
1279 int fd = proc_fd(inode);
1280 struct files_struct *files;
1282 if (task) {
1283 files = get_files_struct(task);
1284 if (files) {
1285 rcu_read_lock();
1286 if (fcheck_files(files, fd)) {
1287 rcu_read_unlock();
1288 put_files_struct(files);
1289 if (task_dumpable(task)) {
1290 inode->i_uid = task->euid;
1291 inode->i_gid = task->egid;
1292 } else {
1293 inode->i_uid = 0;
1294 inode->i_gid = 0;
1296 inode->i_mode &= ~(S_ISUID | S_ISGID);
1297 security_task_to_inode(task, inode);
1298 put_task_struct(task);
1299 return 1;
1301 rcu_read_unlock();
1302 put_files_struct(files);
1304 put_task_struct(task);
1306 d_drop(dentry);
1307 return 0;
1310 static struct dentry_operations tid_fd_dentry_operations =
1312 .d_revalidate = tid_fd_revalidate,
1313 .d_delete = pid_delete_dentry,
1316 static struct dentry *proc_fd_instantiate(struct inode *dir,
1317 struct dentry *dentry, struct task_struct *task, const void *ptr)
1319 unsigned fd = *(const unsigned *)ptr;
1320 struct file *file;
1321 struct files_struct *files;
1322 struct inode *inode;
1323 struct proc_inode *ei;
1324 struct dentry *error = ERR_PTR(-ENOENT);
1326 inode = proc_pid_make_inode(dir->i_sb, task);
1327 if (!inode)
1328 goto out;
1329 ei = PROC_I(inode);
1330 ei->fd = fd;
1331 files = get_files_struct(task);
1332 if (!files)
1333 goto out_iput;
1334 inode->i_mode = S_IFLNK;
1337 * We are not taking a ref to the file structure, so we must
1338 * hold ->file_lock.
1340 spin_lock(&files->file_lock);
1341 file = fcheck_files(files, fd);
1342 if (!file)
1343 goto out_unlock;
1344 if (file->f_mode & 1)
1345 inode->i_mode |= S_IRUSR | S_IXUSR;
1346 if (file->f_mode & 2)
1347 inode->i_mode |= S_IWUSR | S_IXUSR;
1348 spin_unlock(&files->file_lock);
1349 put_files_struct(files);
1351 inode->i_op = &proc_pid_link_inode_operations;
1352 inode->i_size = 64;
1353 ei->op.proc_get_link = proc_fd_link;
1354 dentry->d_op = &tid_fd_dentry_operations;
1355 d_add(dentry, inode);
1356 /* Close the race of the process dying before we return the dentry */
1357 if (tid_fd_revalidate(dentry, NULL))
1358 error = NULL;
1360 out:
1361 return error;
1362 out_unlock:
1363 spin_unlock(&files->file_lock);
1364 put_files_struct(files);
1365 out_iput:
1366 iput(inode);
1367 goto out;
1370 static struct dentry *proc_lookupfd_common(struct inode *dir,
1371 struct dentry *dentry,
1372 instantiate_t instantiate)
1374 struct task_struct *task = get_proc_task(dir);
1375 unsigned fd = name_to_int(dentry);
1376 struct dentry *result = ERR_PTR(-ENOENT);
1378 if (!task)
1379 goto out_no_task;
1380 if (fd == ~0U)
1381 goto out;
1383 result = instantiate(dir, dentry, task, &fd);
1384 out:
1385 put_task_struct(task);
1386 out_no_task:
1387 return result;
1390 static int proc_readfd_common(struct file * filp, void * dirent,
1391 filldir_t filldir, instantiate_t instantiate)
1393 struct dentry *dentry = filp->f_path.dentry;
1394 struct inode *inode = dentry->d_inode;
1395 struct task_struct *p = get_proc_task(inode);
1396 unsigned int fd, tid, ino;
1397 int retval;
1398 struct files_struct * files;
1399 struct fdtable *fdt;
1401 retval = -ENOENT;
1402 if (!p)
1403 goto out_no_task;
1404 retval = 0;
1405 tid = p->pid;
1407 fd = filp->f_pos;
1408 switch (fd) {
1409 case 0:
1410 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1411 goto out;
1412 filp->f_pos++;
1413 case 1:
1414 ino = parent_ino(dentry);
1415 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1416 goto out;
1417 filp->f_pos++;
1418 default:
1419 files = get_files_struct(p);
1420 if (!files)
1421 goto out;
1422 rcu_read_lock();
1423 fdt = files_fdtable(files);
1424 for (fd = filp->f_pos-2;
1425 fd < fdt->max_fds;
1426 fd++, filp->f_pos++) {
1427 char name[PROC_NUMBUF];
1428 int len;
1430 if (!fcheck_files(files, fd))
1431 continue;
1432 rcu_read_unlock();
1434 len = snprintf(name, sizeof(name), "%d", fd);
1435 if (proc_fill_cache(filp, dirent, filldir,
1436 name, len, instantiate,
1437 p, &fd) < 0) {
1438 rcu_read_lock();
1439 break;
1441 rcu_read_lock();
1443 rcu_read_unlock();
1444 put_files_struct(files);
1446 out:
1447 put_task_struct(p);
1448 out_no_task:
1449 return retval;
1452 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1453 struct nameidata *nd)
1455 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1458 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1460 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1463 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1464 size_t len, loff_t *ppos)
1466 char tmp[PROC_FDINFO_MAX];
1467 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1468 if (!err)
1469 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1470 return err;
1473 static const struct file_operations proc_fdinfo_file_operations = {
1474 .open = nonseekable_open,
1475 .read = proc_fdinfo_read,
1478 static const struct file_operations proc_fd_operations = {
1479 .read = generic_read_dir,
1480 .readdir = proc_readfd,
1484 * /proc/pid/fd needs a special permission handler so that a process can still
1485 * access /proc/self/fd after it has executed a setuid().
1487 static int proc_fd_permission(struct inode *inode, int mask,
1488 struct nameidata *nd)
1490 int rv;
1492 rv = generic_permission(inode, mask, NULL);
1493 if (rv == 0)
1494 return 0;
1495 if (task_pid(current) == proc_pid(inode))
1496 rv = 0;
1497 return rv;
1501 * proc directories can do almost nothing..
1503 static const struct inode_operations proc_fd_inode_operations = {
1504 .lookup = proc_lookupfd,
1505 .permission = proc_fd_permission,
1506 .setattr = proc_setattr,
1509 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1510 struct dentry *dentry, struct task_struct *task, const void *ptr)
1512 unsigned fd = *(unsigned *)ptr;
1513 struct inode *inode;
1514 struct proc_inode *ei;
1515 struct dentry *error = ERR_PTR(-ENOENT);
1517 inode = proc_pid_make_inode(dir->i_sb, task);
1518 if (!inode)
1519 goto out;
1520 ei = PROC_I(inode);
1521 ei->fd = fd;
1522 inode->i_mode = S_IFREG | S_IRUSR;
1523 inode->i_fop = &proc_fdinfo_file_operations;
1524 dentry->d_op = &tid_fd_dentry_operations;
1525 d_add(dentry, inode);
1526 /* Close the race of the process dying before we return the dentry */
1527 if (tid_fd_revalidate(dentry, NULL))
1528 error = NULL;
1530 out:
1531 return error;
1534 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1535 struct dentry *dentry,
1536 struct nameidata *nd)
1538 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1541 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1543 return proc_readfd_common(filp, dirent, filldir,
1544 proc_fdinfo_instantiate);
1547 static const struct file_operations proc_fdinfo_operations = {
1548 .read = generic_read_dir,
1549 .readdir = proc_readfdinfo,
1553 * proc directories can do almost nothing..
1555 static const struct inode_operations proc_fdinfo_inode_operations = {
1556 .lookup = proc_lookupfdinfo,
1557 .setattr = proc_setattr,
1561 static struct dentry *proc_pident_instantiate(struct inode *dir,
1562 struct dentry *dentry, struct task_struct *task, const void *ptr)
1564 const struct pid_entry *p = ptr;
1565 struct inode *inode;
1566 struct proc_inode *ei;
1567 struct dentry *error = ERR_PTR(-EINVAL);
1569 inode = proc_pid_make_inode(dir->i_sb, task);
1570 if (!inode)
1571 goto out;
1573 ei = PROC_I(inode);
1574 inode->i_mode = p->mode;
1575 if (S_ISDIR(inode->i_mode))
1576 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1577 if (p->iop)
1578 inode->i_op = p->iop;
1579 if (p->fop)
1580 inode->i_fop = p->fop;
1581 ei->op = p->op;
1582 dentry->d_op = &pid_dentry_operations;
1583 d_add(dentry, inode);
1584 /* Close the race of the process dying before we return the dentry */
1585 if (pid_revalidate(dentry, NULL))
1586 error = NULL;
1587 out:
1588 return error;
1591 static struct dentry *proc_pident_lookup(struct inode *dir,
1592 struct dentry *dentry,
1593 const struct pid_entry *ents,
1594 unsigned int nents)
1596 struct inode *inode;
1597 struct dentry *error;
1598 struct task_struct *task = get_proc_task(dir);
1599 const struct pid_entry *p, *last;
1601 error = ERR_PTR(-ENOENT);
1602 inode = NULL;
1604 if (!task)
1605 goto out_no_task;
1608 * Yes, it does not scale. And it should not. Don't add
1609 * new entries into /proc/<tgid>/ without very good reasons.
1611 last = &ents[nents - 1];
1612 for (p = ents; p <= last; p++) {
1613 if (p->len != dentry->d_name.len)
1614 continue;
1615 if (!memcmp(dentry->d_name.name, p->name, p->len))
1616 break;
1618 if (p > last)
1619 goto out;
1621 error = proc_pident_instantiate(dir, dentry, task, p);
1622 out:
1623 put_task_struct(task);
1624 out_no_task:
1625 return error;
1628 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1629 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1631 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1632 proc_pident_instantiate, task, p);
1635 static int proc_pident_readdir(struct file *filp,
1636 void *dirent, filldir_t filldir,
1637 const struct pid_entry *ents, unsigned int nents)
1639 int i;
1640 int pid;
1641 struct dentry *dentry = filp->f_path.dentry;
1642 struct inode *inode = dentry->d_inode;
1643 struct task_struct *task = get_proc_task(inode);
1644 const struct pid_entry *p, *last;
1645 ino_t ino;
1646 int ret;
1648 ret = -ENOENT;
1649 if (!task)
1650 goto out_no_task;
1652 ret = 0;
1653 pid = task->pid;
1654 i = filp->f_pos;
1655 switch (i) {
1656 case 0:
1657 ino = inode->i_ino;
1658 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1659 goto out;
1660 i++;
1661 filp->f_pos++;
1662 /* fall through */
1663 case 1:
1664 ino = parent_ino(dentry);
1665 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1666 goto out;
1667 i++;
1668 filp->f_pos++;
1669 /* fall through */
1670 default:
1671 i -= 2;
1672 if (i >= nents) {
1673 ret = 1;
1674 goto out;
1676 p = ents + i;
1677 last = &ents[nents - 1];
1678 while (p <= last) {
1679 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1680 goto out;
1681 filp->f_pos++;
1682 p++;
1686 ret = 1;
1687 out:
1688 put_task_struct(task);
1689 out_no_task:
1690 return ret;
1693 #ifdef CONFIG_SECURITY
1694 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1695 size_t count, loff_t *ppos)
1697 struct inode * inode = file->f_path.dentry->d_inode;
1698 char *p = NULL;
1699 ssize_t length;
1700 struct task_struct *task = get_proc_task(inode);
1702 if (!task)
1703 return -ESRCH;
1705 length = security_getprocattr(task,
1706 (char*)file->f_path.dentry->d_name.name,
1707 &p);
1708 put_task_struct(task);
1709 if (length > 0)
1710 length = simple_read_from_buffer(buf, count, ppos, p, length);
1711 kfree(p);
1712 return length;
1715 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1716 size_t count, loff_t *ppos)
1718 struct inode * inode = file->f_path.dentry->d_inode;
1719 char *page;
1720 ssize_t length;
1721 struct task_struct *task = get_proc_task(inode);
1723 length = -ESRCH;
1724 if (!task)
1725 goto out_no_task;
1726 if (count > PAGE_SIZE)
1727 count = PAGE_SIZE;
1729 /* No partial writes. */
1730 length = -EINVAL;
1731 if (*ppos != 0)
1732 goto out;
1734 length = -ENOMEM;
1735 page = (char*)__get_free_page(GFP_USER);
1736 if (!page)
1737 goto out;
1739 length = -EFAULT;
1740 if (copy_from_user(page, buf, count))
1741 goto out_free;
1743 length = security_setprocattr(task,
1744 (char*)file->f_path.dentry->d_name.name,
1745 (void*)page, count);
1746 out_free:
1747 free_page((unsigned long) page);
1748 out:
1749 put_task_struct(task);
1750 out_no_task:
1751 return length;
1754 static const struct file_operations proc_pid_attr_operations = {
1755 .read = proc_pid_attr_read,
1756 .write = proc_pid_attr_write,
1759 static const struct pid_entry attr_dir_stuff[] = {
1760 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1761 REG("prev", S_IRUGO, pid_attr),
1762 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1763 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1764 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1765 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1768 static int proc_attr_dir_readdir(struct file * filp,
1769 void * dirent, filldir_t filldir)
1771 return proc_pident_readdir(filp,dirent,filldir,
1772 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1775 static const struct file_operations proc_attr_dir_operations = {
1776 .read = generic_read_dir,
1777 .readdir = proc_attr_dir_readdir,
1780 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1781 struct dentry *dentry, struct nameidata *nd)
1783 return proc_pident_lookup(dir, dentry,
1784 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1787 static const struct inode_operations proc_attr_dir_inode_operations = {
1788 .lookup = proc_attr_dir_lookup,
1789 .getattr = pid_getattr,
1790 .setattr = proc_setattr,
1793 #endif
1796 * /proc/self:
1798 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1799 int buflen)
1801 char tmp[PROC_NUMBUF];
1802 sprintf(tmp, "%d", current->tgid);
1803 return vfs_readlink(dentry,buffer,buflen,tmp);
1806 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1808 char tmp[PROC_NUMBUF];
1809 sprintf(tmp, "%d", current->tgid);
1810 return ERR_PTR(vfs_follow_link(nd,tmp));
1813 static const struct inode_operations proc_self_inode_operations = {
1814 .readlink = proc_self_readlink,
1815 .follow_link = proc_self_follow_link,
1819 * proc base
1821 * These are the directory entries in the root directory of /proc
1822 * that properly belong to the /proc filesystem, as they describe
1823 * describe something that is process related.
1825 static const struct pid_entry proc_base_stuff[] = {
1826 NOD("self", S_IFLNK|S_IRWXUGO,
1827 &proc_self_inode_operations, NULL, {}),
1831 * Exceptional case: normally we are not allowed to unhash a busy
1832 * directory. In this case, however, we can do it - no aliasing problems
1833 * due to the way we treat inodes.
1835 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1837 struct inode *inode = dentry->d_inode;
1838 struct task_struct *task = get_proc_task(inode);
1839 if (task) {
1840 put_task_struct(task);
1841 return 1;
1843 d_drop(dentry);
1844 return 0;
1847 static struct dentry_operations proc_base_dentry_operations =
1849 .d_revalidate = proc_base_revalidate,
1850 .d_delete = pid_delete_dentry,
1853 static struct dentry *proc_base_instantiate(struct inode *dir,
1854 struct dentry *dentry, struct task_struct *task, const void *ptr)
1856 const struct pid_entry *p = ptr;
1857 struct inode *inode;
1858 struct proc_inode *ei;
1859 struct dentry *error = ERR_PTR(-EINVAL);
1861 /* Allocate the inode */
1862 error = ERR_PTR(-ENOMEM);
1863 inode = new_inode(dir->i_sb);
1864 if (!inode)
1865 goto out;
1867 /* Initialize the inode */
1868 ei = PROC_I(inode);
1869 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1872 * grab the reference to the task.
1874 ei->pid = get_task_pid(task, PIDTYPE_PID);
1875 if (!ei->pid)
1876 goto out_iput;
1878 inode->i_uid = 0;
1879 inode->i_gid = 0;
1880 inode->i_mode = p->mode;
1881 if (S_ISDIR(inode->i_mode))
1882 inode->i_nlink = 2;
1883 if (S_ISLNK(inode->i_mode))
1884 inode->i_size = 64;
1885 if (p->iop)
1886 inode->i_op = p->iop;
1887 if (p->fop)
1888 inode->i_fop = p->fop;
1889 ei->op = p->op;
1890 dentry->d_op = &proc_base_dentry_operations;
1891 d_add(dentry, inode);
1892 error = NULL;
1893 out:
1894 return error;
1895 out_iput:
1896 iput(inode);
1897 goto out;
1900 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1902 struct dentry *error;
1903 struct task_struct *task = get_proc_task(dir);
1904 const struct pid_entry *p, *last;
1906 error = ERR_PTR(-ENOENT);
1908 if (!task)
1909 goto out_no_task;
1911 /* Lookup the directory entry */
1912 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1913 for (p = proc_base_stuff; p <= last; p++) {
1914 if (p->len != dentry->d_name.len)
1915 continue;
1916 if (!memcmp(dentry->d_name.name, p->name, p->len))
1917 break;
1919 if (p > last)
1920 goto out;
1922 error = proc_base_instantiate(dir, dentry, task, p);
1924 out:
1925 put_task_struct(task);
1926 out_no_task:
1927 return error;
1930 static int proc_base_fill_cache(struct file *filp, void *dirent,
1931 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1933 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1934 proc_base_instantiate, task, p);
1937 #ifdef CONFIG_TASK_IO_ACCOUNTING
1938 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1940 return sprintf(buffer,
1941 #ifdef CONFIG_TASK_XACCT
1942 "rchar: %llu\n"
1943 "wchar: %llu\n"
1944 "syscr: %llu\n"
1945 "syscw: %llu\n"
1946 #endif
1947 "read_bytes: %llu\n"
1948 "write_bytes: %llu\n"
1949 "cancelled_write_bytes: %llu\n",
1950 #ifdef CONFIG_TASK_XACCT
1951 (unsigned long long)task->rchar,
1952 (unsigned long long)task->wchar,
1953 (unsigned long long)task->syscr,
1954 (unsigned long long)task->syscw,
1955 #endif
1956 (unsigned long long)task->ioac.read_bytes,
1957 (unsigned long long)task->ioac.write_bytes,
1958 (unsigned long long)task->ioac.cancelled_write_bytes);
1960 #endif
1963 * Thread groups
1965 static const struct file_operations proc_task_operations;
1966 static const struct inode_operations proc_task_inode_operations;
1968 static const struct pid_entry tgid_base_stuff[] = {
1969 DIR("task", S_IRUGO|S_IXUGO, task),
1970 DIR("fd", S_IRUSR|S_IXUSR, fd),
1971 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
1972 INF("environ", S_IRUSR, pid_environ),
1973 INF("auxv", S_IRUSR, pid_auxv),
1974 INF("status", S_IRUGO, pid_status),
1975 INF("cmdline", S_IRUGO, pid_cmdline),
1976 INF("stat", S_IRUGO, tgid_stat),
1977 INF("statm", S_IRUGO, pid_statm),
1978 REG("maps", S_IRUGO, maps),
1979 #ifdef CONFIG_NUMA
1980 REG("numa_maps", S_IRUGO, numa_maps),
1981 #endif
1982 REG("mem", S_IRUSR|S_IWUSR, mem),
1983 #ifdef CONFIG_SECCOMP
1984 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
1985 #endif
1986 LNK("cwd", cwd),
1987 LNK("root", root),
1988 LNK("exe", exe),
1989 REG("mounts", S_IRUGO, mounts),
1990 REG("mountstats", S_IRUSR, mountstats),
1991 #ifdef CONFIG_MMU
1992 REG("clear_refs", S_IWUSR, clear_refs),
1993 REG("smaps", S_IRUGO, smaps),
1994 #endif
1995 #ifdef CONFIG_SECURITY
1996 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
1997 #endif
1998 #ifdef CONFIG_KALLSYMS
1999 INF("wchan", S_IRUGO, pid_wchan),
2000 #endif
2001 #ifdef CONFIG_SCHEDSTATS
2002 INF("schedstat", S_IRUGO, pid_schedstat),
2003 #endif
2004 #ifdef CONFIG_CPUSETS
2005 REG("cpuset", S_IRUGO, cpuset),
2006 #endif
2007 INF("oom_score", S_IRUGO, oom_score),
2008 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2009 #ifdef CONFIG_AUDITSYSCALL
2010 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2011 #endif
2012 #ifdef CONFIG_FAULT_INJECTION
2013 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2014 #endif
2015 #ifdef CONFIG_TASK_IO_ACCOUNTING
2016 INF("io", S_IRUGO, pid_io_accounting),
2017 #endif
2020 static int proc_tgid_base_readdir(struct file * filp,
2021 void * dirent, filldir_t filldir)
2023 return proc_pident_readdir(filp,dirent,filldir,
2024 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2027 static const struct file_operations proc_tgid_base_operations = {
2028 .read = generic_read_dir,
2029 .readdir = proc_tgid_base_readdir,
2032 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2033 return proc_pident_lookup(dir, dentry,
2034 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2037 static const struct inode_operations proc_tgid_base_inode_operations = {
2038 .lookup = proc_tgid_base_lookup,
2039 .getattr = pid_getattr,
2040 .setattr = proc_setattr,
2044 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2046 * @task: task that should be flushed.
2048 * Looks in the dcache for
2049 * /proc/@pid
2050 * /proc/@tgid/task/@pid
2051 * if either directory is present flushes it and all of it'ts children
2052 * from the dcache.
2054 * It is safe and reasonable to cache /proc entries for a task until
2055 * that task exits. After that they just clog up the dcache with
2056 * useless entries, possibly causing useful dcache entries to be
2057 * flushed instead. This routine is proved to flush those useless
2058 * dcache entries at process exit time.
2060 * NOTE: This routine is just an optimization so it does not guarantee
2061 * that no dcache entries will exist at process exit time it
2062 * just makes it very unlikely that any will persist.
2064 void proc_flush_task(struct task_struct *task)
2066 struct dentry *dentry, *leader, *dir;
2067 char buf[PROC_NUMBUF];
2068 struct qstr name;
2070 name.name = buf;
2071 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2072 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2073 if (dentry) {
2074 shrink_dcache_parent(dentry);
2075 d_drop(dentry);
2076 dput(dentry);
2079 if (thread_group_leader(task))
2080 goto out;
2082 name.name = buf;
2083 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2084 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2085 if (!leader)
2086 goto out;
2088 name.name = "task";
2089 name.len = strlen(name.name);
2090 dir = d_hash_and_lookup(leader, &name);
2091 if (!dir)
2092 goto out_put_leader;
2094 name.name = buf;
2095 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2096 dentry = d_hash_and_lookup(dir, &name);
2097 if (dentry) {
2098 shrink_dcache_parent(dentry);
2099 d_drop(dentry);
2100 dput(dentry);
2103 dput(dir);
2104 out_put_leader:
2105 dput(leader);
2106 out:
2107 return;
2110 static struct dentry *proc_pid_instantiate(struct inode *dir,
2111 struct dentry * dentry,
2112 struct task_struct *task, const void *ptr)
2114 struct dentry *error = ERR_PTR(-ENOENT);
2115 struct inode *inode;
2117 inode = proc_pid_make_inode(dir->i_sb, task);
2118 if (!inode)
2119 goto out;
2121 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2122 inode->i_op = &proc_tgid_base_inode_operations;
2123 inode->i_fop = &proc_tgid_base_operations;
2124 inode->i_flags|=S_IMMUTABLE;
2125 inode->i_nlink = 5;
2126 #ifdef CONFIG_SECURITY
2127 inode->i_nlink += 1;
2128 #endif
2130 dentry->d_op = &pid_dentry_operations;
2132 d_add(dentry, inode);
2133 /* Close the race of the process dying before we return the dentry */
2134 if (pid_revalidate(dentry, NULL))
2135 error = NULL;
2136 out:
2137 return error;
2140 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2142 struct dentry *result = ERR_PTR(-ENOENT);
2143 struct task_struct *task;
2144 unsigned tgid;
2146 result = proc_base_lookup(dir, dentry);
2147 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2148 goto out;
2150 tgid = name_to_int(dentry);
2151 if (tgid == ~0U)
2152 goto out;
2154 rcu_read_lock();
2155 task = find_task_by_pid(tgid);
2156 if (task)
2157 get_task_struct(task);
2158 rcu_read_unlock();
2159 if (!task)
2160 goto out;
2162 result = proc_pid_instantiate(dir, dentry, task, NULL);
2163 put_task_struct(task);
2164 out:
2165 return result;
2169 * Find the first task with tgid >= tgid
2172 static struct task_struct *next_tgid(unsigned int tgid)
2174 struct task_struct *task;
2175 struct pid *pid;
2177 rcu_read_lock();
2178 retry:
2179 task = NULL;
2180 pid = find_ge_pid(tgid);
2181 if (pid) {
2182 tgid = pid->nr + 1;
2183 task = pid_task(pid, PIDTYPE_PID);
2184 /* What we to know is if the pid we have find is the
2185 * pid of a thread_group_leader. Testing for task
2186 * being a thread_group_leader is the obvious thing
2187 * todo but there is a window when it fails, due to
2188 * the pid transfer logic in de_thread.
2190 * So we perform the straight forward test of seeing
2191 * if the pid we have found is the pid of a thread
2192 * group leader, and don't worry if the task we have
2193 * found doesn't happen to be a thread group leader.
2194 * As we don't care in the case of readdir.
2196 if (!task || !has_group_leader_pid(task))
2197 goto retry;
2198 get_task_struct(task);
2200 rcu_read_unlock();
2201 return task;
2204 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2206 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2207 struct task_struct *task, int tgid)
2209 char name[PROC_NUMBUF];
2210 int len = snprintf(name, sizeof(name), "%d", tgid);
2211 return proc_fill_cache(filp, dirent, filldir, name, len,
2212 proc_pid_instantiate, task, NULL);
2215 /* for the /proc/ directory itself, after non-process stuff has been done */
2216 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2218 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2219 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2220 struct task_struct *task;
2221 int tgid;
2223 if (!reaper)
2224 goto out_no_task;
2226 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2227 const struct pid_entry *p = &proc_base_stuff[nr];
2228 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2229 goto out;
2232 tgid = filp->f_pos - TGID_OFFSET;
2233 for (task = next_tgid(tgid);
2234 task;
2235 put_task_struct(task), task = next_tgid(tgid + 1)) {
2236 tgid = task->pid;
2237 filp->f_pos = tgid + TGID_OFFSET;
2238 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2239 put_task_struct(task);
2240 goto out;
2243 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2244 out:
2245 put_task_struct(reaper);
2246 out_no_task:
2247 return 0;
2251 * Tasks
2253 static const struct pid_entry tid_base_stuff[] = {
2254 DIR("fd", S_IRUSR|S_IXUSR, fd),
2255 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2256 INF("environ", S_IRUSR, pid_environ),
2257 INF("auxv", S_IRUSR, pid_auxv),
2258 INF("status", S_IRUGO, pid_status),
2259 INF("cmdline", S_IRUGO, pid_cmdline),
2260 INF("stat", S_IRUGO, tid_stat),
2261 INF("statm", S_IRUGO, pid_statm),
2262 REG("maps", S_IRUGO, maps),
2263 #ifdef CONFIG_NUMA
2264 REG("numa_maps", S_IRUGO, numa_maps),
2265 #endif
2266 REG("mem", S_IRUSR|S_IWUSR, mem),
2267 #ifdef CONFIG_SECCOMP
2268 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2269 #endif
2270 LNK("cwd", cwd),
2271 LNK("root", root),
2272 LNK("exe", exe),
2273 REG("mounts", S_IRUGO, mounts),
2274 #ifdef CONFIG_MMU
2275 REG("clear_refs", S_IWUSR, clear_refs),
2276 REG("smaps", S_IRUGO, smaps),
2277 #endif
2278 #ifdef CONFIG_SECURITY
2279 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2280 #endif
2281 #ifdef CONFIG_KALLSYMS
2282 INF("wchan", S_IRUGO, pid_wchan),
2283 #endif
2284 #ifdef CONFIG_SCHEDSTATS
2285 INF("schedstat", S_IRUGO, pid_schedstat),
2286 #endif
2287 #ifdef CONFIG_CPUSETS
2288 REG("cpuset", S_IRUGO, cpuset),
2289 #endif
2290 INF("oom_score", S_IRUGO, oom_score),
2291 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2292 #ifdef CONFIG_AUDITSYSCALL
2293 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2294 #endif
2295 #ifdef CONFIG_FAULT_INJECTION
2296 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2297 #endif
2300 static int proc_tid_base_readdir(struct file * filp,
2301 void * dirent, filldir_t filldir)
2303 return proc_pident_readdir(filp,dirent,filldir,
2304 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2307 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2308 return proc_pident_lookup(dir, dentry,
2309 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2312 static const struct file_operations proc_tid_base_operations = {
2313 .read = generic_read_dir,
2314 .readdir = proc_tid_base_readdir,
2317 static const struct inode_operations proc_tid_base_inode_operations = {
2318 .lookup = proc_tid_base_lookup,
2319 .getattr = pid_getattr,
2320 .setattr = proc_setattr,
2323 static struct dentry *proc_task_instantiate(struct inode *dir,
2324 struct dentry *dentry, struct task_struct *task, const void *ptr)
2326 struct dentry *error = ERR_PTR(-ENOENT);
2327 struct inode *inode;
2328 inode = proc_pid_make_inode(dir->i_sb, task);
2330 if (!inode)
2331 goto out;
2332 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2333 inode->i_op = &proc_tid_base_inode_operations;
2334 inode->i_fop = &proc_tid_base_operations;
2335 inode->i_flags|=S_IMMUTABLE;
2336 inode->i_nlink = 4;
2337 #ifdef CONFIG_SECURITY
2338 inode->i_nlink += 1;
2339 #endif
2341 dentry->d_op = &pid_dentry_operations;
2343 d_add(dentry, inode);
2344 /* Close the race of the process dying before we return the dentry */
2345 if (pid_revalidate(dentry, NULL))
2346 error = NULL;
2347 out:
2348 return error;
2351 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2353 struct dentry *result = ERR_PTR(-ENOENT);
2354 struct task_struct *task;
2355 struct task_struct *leader = get_proc_task(dir);
2356 unsigned tid;
2358 if (!leader)
2359 goto out_no_task;
2361 tid = name_to_int(dentry);
2362 if (tid == ~0U)
2363 goto out;
2365 rcu_read_lock();
2366 task = find_task_by_pid(tid);
2367 if (task)
2368 get_task_struct(task);
2369 rcu_read_unlock();
2370 if (!task)
2371 goto out;
2372 if (leader->tgid != task->tgid)
2373 goto out_drop_task;
2375 result = proc_task_instantiate(dir, dentry, task, NULL);
2376 out_drop_task:
2377 put_task_struct(task);
2378 out:
2379 put_task_struct(leader);
2380 out_no_task:
2381 return result;
2385 * Find the first tid of a thread group to return to user space.
2387 * Usually this is just the thread group leader, but if the users
2388 * buffer was too small or there was a seek into the middle of the
2389 * directory we have more work todo.
2391 * In the case of a short read we start with find_task_by_pid.
2393 * In the case of a seek we start with the leader and walk nr
2394 * threads past it.
2396 static struct task_struct *first_tid(struct task_struct *leader,
2397 int tid, int nr)
2399 struct task_struct *pos;
2401 rcu_read_lock();
2402 /* Attempt to start with the pid of a thread */
2403 if (tid && (nr > 0)) {
2404 pos = find_task_by_pid(tid);
2405 if (pos && (pos->group_leader == leader))
2406 goto found;
2409 /* If nr exceeds the number of threads there is nothing todo */
2410 pos = NULL;
2411 if (nr && nr >= get_nr_threads(leader))
2412 goto out;
2414 /* If we haven't found our starting place yet start
2415 * with the leader and walk nr threads forward.
2417 for (pos = leader; nr > 0; --nr) {
2418 pos = next_thread(pos);
2419 if (pos == leader) {
2420 pos = NULL;
2421 goto out;
2424 found:
2425 get_task_struct(pos);
2426 out:
2427 rcu_read_unlock();
2428 return pos;
2432 * Find the next thread in the thread list.
2433 * Return NULL if there is an error or no next thread.
2435 * The reference to the input task_struct is released.
2437 static struct task_struct *next_tid(struct task_struct *start)
2439 struct task_struct *pos = NULL;
2440 rcu_read_lock();
2441 if (pid_alive(start)) {
2442 pos = next_thread(start);
2443 if (thread_group_leader(pos))
2444 pos = NULL;
2445 else
2446 get_task_struct(pos);
2448 rcu_read_unlock();
2449 put_task_struct(start);
2450 return pos;
2453 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2454 struct task_struct *task, int tid)
2456 char name[PROC_NUMBUF];
2457 int len = snprintf(name, sizeof(name), "%d", tid);
2458 return proc_fill_cache(filp, dirent, filldir, name, len,
2459 proc_task_instantiate, task, NULL);
2462 /* for the /proc/TGID/task/ directories */
2463 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2465 struct dentry *dentry = filp->f_path.dentry;
2466 struct inode *inode = dentry->d_inode;
2467 struct task_struct *leader = NULL;
2468 struct task_struct *task;
2469 int retval = -ENOENT;
2470 ino_t ino;
2471 int tid;
2472 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2474 task = get_proc_task(inode);
2475 if (!task)
2476 goto out_no_task;
2477 rcu_read_lock();
2478 if (pid_alive(task)) {
2479 leader = task->group_leader;
2480 get_task_struct(leader);
2482 rcu_read_unlock();
2483 put_task_struct(task);
2484 if (!leader)
2485 goto out_no_task;
2486 retval = 0;
2488 switch (pos) {
2489 case 0:
2490 ino = inode->i_ino;
2491 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2492 goto out;
2493 pos++;
2494 /* fall through */
2495 case 1:
2496 ino = parent_ino(dentry);
2497 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2498 goto out;
2499 pos++;
2500 /* fall through */
2503 /* f_version caches the tgid value that the last readdir call couldn't
2504 * return. lseek aka telldir automagically resets f_version to 0.
2506 tid = filp->f_version;
2507 filp->f_version = 0;
2508 for (task = first_tid(leader, tid, pos - 2);
2509 task;
2510 task = next_tid(task), pos++) {
2511 tid = task->pid;
2512 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2513 /* returning this tgid failed, save it as the first
2514 * pid for the next readir call */
2515 filp->f_version = tid;
2516 put_task_struct(task);
2517 break;
2520 out:
2521 filp->f_pos = pos;
2522 put_task_struct(leader);
2523 out_no_task:
2524 return retval;
2527 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2529 struct inode *inode = dentry->d_inode;
2530 struct task_struct *p = get_proc_task(inode);
2531 generic_fillattr(inode, stat);
2533 if (p) {
2534 rcu_read_lock();
2535 stat->nlink += get_nr_threads(p);
2536 rcu_read_unlock();
2537 put_task_struct(p);
2540 return 0;
2543 static const struct inode_operations proc_task_inode_operations = {
2544 .lookup = proc_task_lookup,
2545 .getattr = proc_task_getattr,
2546 .setattr = proc_setattr,
2549 static const struct file_operations proc_task_operations = {
2550 .read = generic_read_dir,
2551 .readdir = proc_task_readdir,