Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / proc / base.c
blob5d216bcd11d860bdbcf9f694aa65111c102a3480
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/resource.h>
67 #include <linux/module.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/cgroup.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
77 #include <linux/elf.h>
78 #include <linux/pid_namespace.h>
79 #include "internal.h"
81 /* NOTE:
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
91 struct pid_entry {
92 char *name;
93 int len;
94 mode_t mode;
95 const struct inode_operations *iop;
96 const struct file_operations *fop;
97 union proc_op op;
100 #define NOD(NAME, MODE, IOP, FOP, OP) { \
101 .name = (NAME), \
102 .len = sizeof(NAME) - 1, \
103 .mode = MODE, \
104 .iop = IOP, \
105 .fop = FOP, \
106 .op = OP, \
109 #define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
112 {} )
113 #define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
117 #define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
119 &proc_##OTYPE##_operations, {})
120 #define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
124 #define ONE(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
127 { .proc_show = &proc_##OTYPE } )
129 int maps_protect;
130 EXPORT_SYMBOL(maps_protect);
132 static struct fs_struct *get_fs_struct(struct task_struct *task)
134 struct fs_struct *fs;
135 task_lock(task);
136 fs = task->fs;
137 if(fs)
138 atomic_inc(&fs->count);
139 task_unlock(task);
140 return fs;
143 static int get_nr_threads(struct task_struct *tsk)
145 /* Must be called with the rcu_read_lock held */
146 unsigned long flags;
147 int count = 0;
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
153 return count;
156 static int proc_cwd_link(struct inode *inode, struct path *path)
158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
160 int result = -ENOENT;
162 if (task) {
163 fs = get_fs_struct(task);
164 put_task_struct(task);
166 if (fs) {
167 read_lock(&fs->lock);
168 *path = fs->pwd;
169 path_get(&fs->pwd);
170 read_unlock(&fs->lock);
171 result = 0;
172 put_fs_struct(fs);
174 return result;
177 static int proc_root_link(struct inode *inode, struct path *path)
179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
181 int result = -ENOENT;
183 if (task) {
184 fs = get_fs_struct(task);
185 put_task_struct(task);
187 if (fs) {
188 read_lock(&fs->lock);
189 *path = fs->root;
190 path_get(&fs->root);
191 read_unlock(&fs->lock);
192 result = 0;
193 put_fs_struct(fs);
195 return result;
198 #define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
202 (task_is_stopped_or_traced(task)) && \
203 security_ptrace(current,task) == 0))
205 struct mm_struct *mm_for_maps(struct task_struct *task)
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 return NULL;
210 down_read(&mm->mmap_sem);
211 task_lock(task);
212 if (task->mm != mm)
213 goto out;
214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
215 goto out;
216 task_unlock(task);
217 return mm;
218 out:
219 task_unlock(task);
220 up_read(&mm->mmap_sem);
221 mmput(mm);
222 return NULL;
225 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
227 int res = 0;
228 unsigned int len;
229 struct mm_struct *mm = get_task_mm(task);
230 if (!mm)
231 goto out;
232 if (!mm->arg_end)
233 goto out_mm; /* Shh! No looking before we're done */
235 len = mm->arg_end - mm->arg_start;
237 if (len > PAGE_SIZE)
238 len = PAGE_SIZE;
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
246 if (len < res) {
247 res = len;
248 } else {
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
256 out_mm:
257 mmput(mm);
258 out:
259 return res;
262 static int proc_pid_auxv(struct task_struct *task, char *buffer)
264 int res = 0;
265 struct mm_struct *mm = get_task_mm(task);
266 if (mm) {
267 unsigned int nwords = 0;
269 nwords += 2;
270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
272 if (res > PAGE_SIZE)
273 res = PAGE_SIZE;
274 memcpy(buffer, mm->saved_auxv, res);
275 mmput(mm);
277 return res;
281 #ifdef CONFIG_KALLSYMS
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
286 static int proc_pid_wchan(struct task_struct *task, char *buffer)
288 unsigned long wchan;
289 char symname[KSYM_NAME_LEN];
291 wchan = get_wchan(task);
293 if (lookup_symbol_name(wchan, symname) < 0)
294 return sprintf(buffer, "%lu", wchan);
295 else
296 return sprintf(buffer, "%s", symname);
298 #endif /* CONFIG_KALLSYMS */
300 #ifdef CONFIG_SCHEDSTATS
302 * Provides /proc/PID/schedstat
304 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
306 return sprintf(buffer, "%llu %llu %lu\n",
307 task->sched_info.cpu_time,
308 task->sched_info.run_delay,
309 task->sched_info.pcount);
311 #endif
313 #ifdef CONFIG_LATENCYTOP
314 static int lstats_show_proc(struct seq_file *m, void *v)
316 int i;
317 <<<<<<< HEAD:fs/proc/base.c
318 struct task_struct *task = m->private;
319 seq_puts(m, "Latency Top version : v0.1\n");
320 =======
321 struct inode *inode = m->private;
322 struct task_struct *task = get_proc_task(inode);
323 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
325 <<<<<<< HEAD:fs/proc/base.c
326 =======
327 if (!task)
328 return -ESRCH;
329 seq_puts(m, "Latency Top version : v0.1\n");
330 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
331 for (i = 0; i < 32; i++) {
332 if (task->latency_record[i].backtrace[0]) {
333 int q;
334 seq_printf(m, "%i %li %li ",
335 task->latency_record[i].count,
336 task->latency_record[i].time,
337 task->latency_record[i].max);
338 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
339 char sym[KSYM_NAME_LEN];
340 char *c;
341 if (!task->latency_record[i].backtrace[q])
342 break;
343 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
344 break;
345 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
346 c = strchr(sym, '+');
347 if (c)
348 *c = 0;
349 seq_printf(m, "%s ", sym);
351 seq_printf(m, "\n");
355 <<<<<<< HEAD:fs/proc/base.c
356 =======
357 put_task_struct(task);
358 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
359 return 0;
362 static int lstats_open(struct inode *inode, struct file *file)
364 <<<<<<< HEAD:fs/proc/base.c
365 int ret;
366 struct seq_file *m;
367 struct task_struct *task = get_proc_task(inode);
369 ret = single_open(file, lstats_show_proc, NULL);
370 if (!ret) {
371 m = file->private_data;
372 m->private = task;
374 return ret;
375 =======
376 return single_open(file, lstats_show_proc, inode);
377 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
380 static ssize_t lstats_write(struct file *file, const char __user *buf,
381 size_t count, loff_t *offs)
383 <<<<<<< HEAD:fs/proc/base.c
384 struct seq_file *m;
385 struct task_struct *task;
386 =======
387 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
388 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
390 <<<<<<< HEAD:fs/proc/base.c
391 m = file->private_data;
392 task = m->private;
393 =======
394 if (!task)
395 return -ESRCH;
396 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
397 clear_all_latency_tracing(task);
398 <<<<<<< HEAD:fs/proc/base.c
399 =======
400 put_task_struct(task);
401 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
403 return count;
406 static const struct file_operations proc_lstats_operations = {
407 .open = lstats_open,
408 .read = seq_read,
409 .write = lstats_write,
410 .llseek = seq_lseek,
411 .release = single_release,
414 #endif
416 /* The badness from the OOM killer */
417 unsigned long badness(struct task_struct *p, unsigned long uptime);
418 static int proc_oom_score(struct task_struct *task, char *buffer)
420 unsigned long points;
421 struct timespec uptime;
423 do_posix_clock_monotonic_gettime(&uptime);
424 read_lock(&tasklist_lock);
425 points = badness(task, uptime.tv_sec);
426 read_unlock(&tasklist_lock);
427 return sprintf(buffer, "%lu\n", points);
430 struct limit_names {
431 char *name;
432 char *unit;
435 static const struct limit_names lnames[RLIM_NLIMITS] = {
436 [RLIMIT_CPU] = {"Max cpu time", "ms"},
437 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
438 [RLIMIT_DATA] = {"Max data size", "bytes"},
439 [RLIMIT_STACK] = {"Max stack size", "bytes"},
440 [RLIMIT_CORE] = {"Max core file size", "bytes"},
441 [RLIMIT_RSS] = {"Max resident set", "bytes"},
442 [RLIMIT_NPROC] = {"Max processes", "processes"},
443 [RLIMIT_NOFILE] = {"Max open files", "files"},
444 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
445 [RLIMIT_AS] = {"Max address space", "bytes"},
446 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
447 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
448 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
449 [RLIMIT_NICE] = {"Max nice priority", NULL},
450 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
451 <<<<<<< HEAD:fs/proc/base.c
452 =======
453 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
454 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
457 /* Display limits for a process */
458 static int proc_pid_limits(struct task_struct *task, char *buffer)
460 unsigned int i;
461 int count = 0;
462 unsigned long flags;
463 char *bufptr = buffer;
465 struct rlimit rlim[RLIM_NLIMITS];
467 rcu_read_lock();
468 if (!lock_task_sighand(task,&flags)) {
469 rcu_read_unlock();
470 return 0;
472 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
473 unlock_task_sighand(task, &flags);
474 rcu_read_unlock();
477 * print the file header
479 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
480 "Limit", "Soft Limit", "Hard Limit", "Units");
482 for (i = 0; i < RLIM_NLIMITS; i++) {
483 if (rlim[i].rlim_cur == RLIM_INFINITY)
484 count += sprintf(&bufptr[count], "%-25s %-20s ",
485 lnames[i].name, "unlimited");
486 else
487 count += sprintf(&bufptr[count], "%-25s %-20lu ",
488 lnames[i].name, rlim[i].rlim_cur);
490 if (rlim[i].rlim_max == RLIM_INFINITY)
491 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
492 else
493 count += sprintf(&bufptr[count], "%-20lu ",
494 rlim[i].rlim_max);
496 if (lnames[i].unit)
497 count += sprintf(&bufptr[count], "%-10s\n",
498 lnames[i].unit);
499 else
500 count += sprintf(&bufptr[count], "\n");
503 return count;
506 /************************************************************************/
507 /* Here the fs part begins */
508 /************************************************************************/
510 /* permission checks */
511 static int proc_fd_access_allowed(struct inode *inode)
513 struct task_struct *task;
514 int allowed = 0;
515 /* Allow access to a task's file descriptors if it is us or we
516 * may use ptrace attach to the process and find out that
517 * information.
519 task = get_proc_task(inode);
520 if (task) {
521 allowed = ptrace_may_attach(task);
522 put_task_struct(task);
524 return allowed;
527 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
529 int error;
530 struct inode *inode = dentry->d_inode;
532 if (attr->ia_valid & ATTR_MODE)
533 return -EPERM;
535 error = inode_change_ok(inode, attr);
536 if (!error)
537 error = inode_setattr(inode, attr);
538 return error;
541 static const struct inode_operations proc_def_inode_operations = {
542 .setattr = proc_setattr,
545 extern const struct seq_operations mounts_op;
546 struct proc_mounts {
547 struct seq_file m;
548 int event;
551 static int mounts_open(struct inode *inode, struct file *file)
553 struct task_struct *task = get_proc_task(inode);
554 struct nsproxy *nsp;
555 struct mnt_namespace *ns = NULL;
556 struct proc_mounts *p;
557 int ret = -EINVAL;
559 if (task) {
560 rcu_read_lock();
561 nsp = task_nsproxy(task);
562 if (nsp) {
563 ns = nsp->mnt_ns;
564 if (ns)
565 get_mnt_ns(ns);
567 rcu_read_unlock();
569 put_task_struct(task);
572 if (ns) {
573 ret = -ENOMEM;
574 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
575 if (p) {
576 file->private_data = &p->m;
577 ret = seq_open(file, &mounts_op);
578 if (!ret) {
579 p->m.private = ns;
580 p->event = ns->event;
581 return 0;
583 kfree(p);
585 put_mnt_ns(ns);
587 return ret;
590 static int mounts_release(struct inode *inode, struct file *file)
592 struct seq_file *m = file->private_data;
593 struct mnt_namespace *ns = m->private;
594 put_mnt_ns(ns);
595 return seq_release(inode, file);
598 static unsigned mounts_poll(struct file *file, poll_table *wait)
600 struct proc_mounts *p = file->private_data;
601 struct mnt_namespace *ns = p->m.private;
602 unsigned res = 0;
604 poll_wait(file, &ns->poll, wait);
606 spin_lock(&vfsmount_lock);
607 if (p->event != ns->event) {
608 p->event = ns->event;
609 res = POLLERR;
611 spin_unlock(&vfsmount_lock);
613 return res;
616 static const struct file_operations proc_mounts_operations = {
617 .open = mounts_open,
618 .read = seq_read,
619 .llseek = seq_lseek,
620 .release = mounts_release,
621 .poll = mounts_poll,
624 extern const struct seq_operations mountstats_op;
625 static int mountstats_open(struct inode *inode, struct file *file)
627 int ret = seq_open(file, &mountstats_op);
629 if (!ret) {
630 struct seq_file *m = file->private_data;
631 struct nsproxy *nsp;
632 struct mnt_namespace *mnt_ns = NULL;
633 struct task_struct *task = get_proc_task(inode);
635 if (task) {
636 rcu_read_lock();
637 nsp = task_nsproxy(task);
638 if (nsp) {
639 mnt_ns = nsp->mnt_ns;
640 if (mnt_ns)
641 get_mnt_ns(mnt_ns);
643 rcu_read_unlock();
645 put_task_struct(task);
648 if (mnt_ns)
649 m->private = mnt_ns;
650 else {
651 seq_release(inode, file);
652 ret = -EINVAL;
655 return ret;
658 static const struct file_operations proc_mountstats_operations = {
659 .open = mountstats_open,
660 .read = seq_read,
661 .llseek = seq_lseek,
662 .release = mounts_release,
665 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
667 static ssize_t proc_info_read(struct file * file, char __user * buf,
668 size_t count, loff_t *ppos)
670 struct inode * inode = file->f_path.dentry->d_inode;
671 unsigned long page;
672 ssize_t length;
673 struct task_struct *task = get_proc_task(inode);
675 length = -ESRCH;
676 if (!task)
677 goto out_no_task;
679 if (count > PROC_BLOCK_SIZE)
680 count = PROC_BLOCK_SIZE;
682 length = -ENOMEM;
683 if (!(page = __get_free_page(GFP_TEMPORARY)))
684 goto out;
686 length = PROC_I(inode)->op.proc_read(task, (char*)page);
688 if (length >= 0)
689 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
690 free_page(page);
691 out:
692 put_task_struct(task);
693 out_no_task:
694 return length;
697 static const struct file_operations proc_info_file_operations = {
698 .read = proc_info_read,
701 static int proc_single_show(struct seq_file *m, void *v)
703 struct inode *inode = m->private;
704 struct pid_namespace *ns;
705 struct pid *pid;
706 struct task_struct *task;
707 int ret;
709 ns = inode->i_sb->s_fs_info;
710 pid = proc_pid(inode);
711 task = get_pid_task(pid, PIDTYPE_PID);
712 if (!task)
713 return -ESRCH;
715 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
717 put_task_struct(task);
718 return ret;
721 static int proc_single_open(struct inode *inode, struct file *filp)
723 int ret;
724 ret = single_open(filp, proc_single_show, NULL);
725 if (!ret) {
726 struct seq_file *m = filp->private_data;
728 m->private = inode;
730 return ret;
733 static const struct file_operations proc_single_file_operations = {
734 .open = proc_single_open,
735 .read = seq_read,
736 .llseek = seq_lseek,
737 .release = single_release,
740 static int mem_open(struct inode* inode, struct file* file)
742 file->private_data = (void*)((long)current->self_exec_id);
743 return 0;
746 static ssize_t mem_read(struct file * file, char __user * buf,
747 size_t count, loff_t *ppos)
749 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
750 char *page;
751 unsigned long src = *ppos;
752 int ret = -ESRCH;
753 struct mm_struct *mm;
755 if (!task)
756 goto out_no_task;
758 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
759 goto out;
761 ret = -ENOMEM;
762 page = (char *)__get_free_page(GFP_TEMPORARY);
763 if (!page)
764 goto out;
766 ret = 0;
768 mm = get_task_mm(task);
769 if (!mm)
770 goto out_free;
772 ret = -EIO;
774 if (file->private_data != (void*)((long)current->self_exec_id))
775 goto out_put;
777 ret = 0;
779 while (count > 0) {
780 int this_len, retval;
782 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
783 retval = access_process_vm(task, src, page, this_len, 0);
784 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
785 if (!ret)
786 ret = -EIO;
787 break;
790 if (copy_to_user(buf, page, retval)) {
791 ret = -EFAULT;
792 break;
795 ret += retval;
796 src += retval;
797 buf += retval;
798 count -= retval;
800 *ppos = src;
802 out_put:
803 mmput(mm);
804 out_free:
805 free_page((unsigned long) page);
806 out:
807 put_task_struct(task);
808 out_no_task:
809 return ret;
812 #define mem_write NULL
814 #ifndef mem_write
815 /* This is a security hazard */
816 static ssize_t mem_write(struct file * file, const char __user *buf,
817 size_t count, loff_t *ppos)
819 int copied;
820 char *page;
821 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
822 unsigned long dst = *ppos;
824 copied = -ESRCH;
825 if (!task)
826 goto out_no_task;
828 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
829 goto out;
831 copied = -ENOMEM;
832 page = (char *)__get_free_page(GFP_TEMPORARY);
833 if (!page)
834 goto out;
836 copied = 0;
837 while (count > 0) {
838 int this_len, retval;
840 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
841 if (copy_from_user(page, buf, this_len)) {
842 copied = -EFAULT;
843 break;
845 retval = access_process_vm(task, dst, page, this_len, 1);
846 if (!retval) {
847 if (!copied)
848 copied = -EIO;
849 break;
851 copied += retval;
852 buf += retval;
853 dst += retval;
854 count -= retval;
856 *ppos = dst;
857 free_page((unsigned long) page);
858 out:
859 put_task_struct(task);
860 out_no_task:
861 return copied;
863 #endif
865 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
867 switch (orig) {
868 case 0:
869 file->f_pos = offset;
870 break;
871 case 1:
872 file->f_pos += offset;
873 break;
874 default:
875 return -EINVAL;
877 force_successful_syscall_return();
878 return file->f_pos;
881 static const struct file_operations proc_mem_operations = {
882 .llseek = mem_lseek,
883 .read = mem_read,
884 .write = mem_write,
885 .open = mem_open,
888 static ssize_t environ_read(struct file *file, char __user *buf,
889 size_t count, loff_t *ppos)
891 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
892 char *page;
893 unsigned long src = *ppos;
894 int ret = -ESRCH;
895 struct mm_struct *mm;
897 if (!task)
898 goto out_no_task;
900 if (!ptrace_may_attach(task))
901 goto out;
903 ret = -ENOMEM;
904 page = (char *)__get_free_page(GFP_TEMPORARY);
905 if (!page)
906 goto out;
908 ret = 0;
910 mm = get_task_mm(task);
911 if (!mm)
912 goto out_free;
914 while (count > 0) {
915 int this_len, retval, max_len;
917 this_len = mm->env_end - (mm->env_start + src);
919 if (this_len <= 0)
920 break;
922 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
923 this_len = (this_len > max_len) ? max_len : this_len;
925 retval = access_process_vm(task, (mm->env_start + src),
926 page, this_len, 0);
928 if (retval <= 0) {
929 ret = retval;
930 break;
933 if (copy_to_user(buf, page, retval)) {
934 ret = -EFAULT;
935 break;
938 ret += retval;
939 src += retval;
940 buf += retval;
941 count -= retval;
943 *ppos = src;
945 mmput(mm);
946 out_free:
947 free_page((unsigned long) page);
948 out:
949 put_task_struct(task);
950 out_no_task:
951 return ret;
954 static const struct file_operations proc_environ_operations = {
955 .read = environ_read,
958 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
959 size_t count, loff_t *ppos)
961 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
962 char buffer[PROC_NUMBUF];
963 size_t len;
964 int oom_adjust;
966 if (!task)
967 return -ESRCH;
968 oom_adjust = task->oomkilladj;
969 put_task_struct(task);
971 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
973 return simple_read_from_buffer(buf, count, ppos, buffer, len);
976 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
977 size_t count, loff_t *ppos)
979 struct task_struct *task;
980 char buffer[PROC_NUMBUF], *end;
981 int oom_adjust;
983 memset(buffer, 0, sizeof(buffer));
984 if (count > sizeof(buffer) - 1)
985 count = sizeof(buffer) - 1;
986 if (copy_from_user(buffer, buf, count))
987 return -EFAULT;
988 oom_adjust = simple_strtol(buffer, &end, 0);
989 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
990 oom_adjust != OOM_DISABLE)
991 return -EINVAL;
992 if (*end == '\n')
993 end++;
994 task = get_proc_task(file->f_path.dentry->d_inode);
995 if (!task)
996 return -ESRCH;
997 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
998 put_task_struct(task);
999 return -EACCES;
1001 task->oomkilladj = oom_adjust;
1002 put_task_struct(task);
1003 if (end - buffer == 0)
1004 return -EIO;
1005 return end - buffer;
1008 static const struct file_operations proc_oom_adjust_operations = {
1009 .read = oom_adjust_read,
1010 .write = oom_adjust_write,
1013 #ifdef CONFIG_AUDITSYSCALL
1014 #define TMPBUFLEN 21
1015 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1016 size_t count, loff_t *ppos)
1018 struct inode * inode = file->f_path.dentry->d_inode;
1019 struct task_struct *task = get_proc_task(inode);
1020 ssize_t length;
1021 char tmpbuf[TMPBUFLEN];
1023 if (!task)
1024 return -ESRCH;
1025 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1026 audit_get_loginuid(task));
1027 put_task_struct(task);
1028 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1031 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1032 size_t count, loff_t *ppos)
1034 struct inode * inode = file->f_path.dentry->d_inode;
1035 char *page, *tmp;
1036 ssize_t length;
1037 uid_t loginuid;
1039 if (!capable(CAP_AUDIT_CONTROL))
1040 return -EPERM;
1042 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1043 return -EPERM;
1045 if (count >= PAGE_SIZE)
1046 count = PAGE_SIZE - 1;
1048 if (*ppos != 0) {
1049 /* No partial writes. */
1050 return -EINVAL;
1052 page = (char*)__get_free_page(GFP_TEMPORARY);
1053 if (!page)
1054 return -ENOMEM;
1055 length = -EFAULT;
1056 if (copy_from_user(page, buf, count))
1057 goto out_free_page;
1059 page[count] = '\0';
1060 loginuid = simple_strtoul(page, &tmp, 10);
1061 if (tmp == page) {
1062 length = -EINVAL;
1063 goto out_free_page;
1066 length = audit_set_loginuid(current, loginuid);
1067 if (likely(length == 0))
1068 length = count;
1070 out_free_page:
1071 free_page((unsigned long) page);
1072 return length;
1075 static const struct file_operations proc_loginuid_operations = {
1076 .read = proc_loginuid_read,
1077 .write = proc_loginuid_write,
1079 <<<<<<< HEAD:fs/proc/base.c
1080 =======
1082 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1083 size_t count, loff_t *ppos)
1085 struct inode * inode = file->f_path.dentry->d_inode;
1086 struct task_struct *task = get_proc_task(inode);
1087 ssize_t length;
1088 char tmpbuf[TMPBUFLEN];
1090 if (!task)
1091 return -ESRCH;
1092 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1093 audit_get_sessionid(task));
1094 put_task_struct(task);
1095 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1098 static const struct file_operations proc_sessionid_operations = {
1099 .read = proc_sessionid_read,
1101 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
1102 #endif
1104 #ifdef CONFIG_FAULT_INJECTION
1105 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1106 size_t count, loff_t *ppos)
1108 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1109 char buffer[PROC_NUMBUF];
1110 size_t len;
1111 int make_it_fail;
1113 if (!task)
1114 return -ESRCH;
1115 make_it_fail = task->make_it_fail;
1116 put_task_struct(task);
1118 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1120 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1123 static ssize_t proc_fault_inject_write(struct file * file,
1124 const char __user * buf, size_t count, loff_t *ppos)
1126 struct task_struct *task;
1127 char buffer[PROC_NUMBUF], *end;
1128 int make_it_fail;
1130 if (!capable(CAP_SYS_RESOURCE))
1131 return -EPERM;
1132 memset(buffer, 0, sizeof(buffer));
1133 if (count > sizeof(buffer) - 1)
1134 count = sizeof(buffer) - 1;
1135 if (copy_from_user(buffer, buf, count))
1136 return -EFAULT;
1137 make_it_fail = simple_strtol(buffer, &end, 0);
1138 if (*end == '\n')
1139 end++;
1140 task = get_proc_task(file->f_dentry->d_inode);
1141 if (!task)
1142 return -ESRCH;
1143 task->make_it_fail = make_it_fail;
1144 put_task_struct(task);
1145 if (end - buffer == 0)
1146 return -EIO;
1147 return end - buffer;
1150 static const struct file_operations proc_fault_inject_operations = {
1151 .read = proc_fault_inject_read,
1152 .write = proc_fault_inject_write,
1154 #endif
1157 #ifdef CONFIG_SCHED_DEBUG
1159 * Print out various scheduling related per-task fields:
1161 static int sched_show(struct seq_file *m, void *v)
1163 struct inode *inode = m->private;
1164 struct task_struct *p;
1166 WARN_ON(!inode);
1168 p = get_proc_task(inode);
1169 if (!p)
1170 return -ESRCH;
1171 proc_sched_show_task(p, m);
1173 put_task_struct(p);
1175 return 0;
1178 static ssize_t
1179 sched_write(struct file *file, const char __user *buf,
1180 size_t count, loff_t *offset)
1182 struct inode *inode = file->f_path.dentry->d_inode;
1183 struct task_struct *p;
1185 WARN_ON(!inode);
1187 p = get_proc_task(inode);
1188 if (!p)
1189 return -ESRCH;
1190 proc_sched_set_task(p);
1192 put_task_struct(p);
1194 return count;
1197 static int sched_open(struct inode *inode, struct file *filp)
1199 int ret;
1201 ret = single_open(filp, sched_show, NULL);
1202 if (!ret) {
1203 struct seq_file *m = filp->private_data;
1205 m->private = inode;
1207 return ret;
1210 static const struct file_operations proc_pid_sched_operations = {
1211 .open = sched_open,
1212 .read = seq_read,
1213 .write = sched_write,
1214 .llseek = seq_lseek,
1215 .release = single_release,
1218 #endif
1220 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1222 struct inode *inode = dentry->d_inode;
1223 int error = -EACCES;
1225 /* We don't need a base pointer in the /proc filesystem */
1226 path_put(&nd->path);
1228 /* Are we allowed to snoop on the tasks file descriptors? */
1229 if (!proc_fd_access_allowed(inode))
1230 goto out;
1232 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1233 nd->last_type = LAST_BIND;
1234 out:
1235 return ERR_PTR(error);
1238 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1240 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1241 char *pathname;
1242 int len;
1244 if (!tmp)
1245 return -ENOMEM;
1247 pathname = d_path(path, tmp, PAGE_SIZE);
1248 len = PTR_ERR(pathname);
1249 if (IS_ERR(pathname))
1250 goto out;
1251 len = tmp + PAGE_SIZE - 1 - pathname;
1253 if (len > buflen)
1254 len = buflen;
1255 if (copy_to_user(buffer, pathname, len))
1256 len = -EFAULT;
1257 out:
1258 free_page((unsigned long)tmp);
1259 return len;
1262 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1264 int error = -EACCES;
1265 struct inode *inode = dentry->d_inode;
1266 struct path path;
1268 /* Are we allowed to snoop on the tasks file descriptors? */
1269 if (!proc_fd_access_allowed(inode))
1270 goto out;
1272 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1273 if (error)
1274 goto out;
1276 error = do_proc_readlink(&path, buffer, buflen);
1277 path_put(&path);
1278 out:
1279 return error;
1282 static const struct inode_operations proc_pid_link_inode_operations = {
1283 .readlink = proc_pid_readlink,
1284 .follow_link = proc_pid_follow_link,
1285 .setattr = proc_setattr,
1289 /* building an inode */
1291 static int task_dumpable(struct task_struct *task)
1293 int dumpable = 0;
1294 struct mm_struct *mm;
1296 task_lock(task);
1297 mm = task->mm;
1298 if (mm)
1299 dumpable = get_dumpable(mm);
1300 task_unlock(task);
1301 if(dumpable == 1)
1302 return 1;
1303 return 0;
1307 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1309 struct inode * inode;
1310 struct proc_inode *ei;
1312 /* We need a new inode */
1314 inode = new_inode(sb);
1315 if (!inode)
1316 goto out;
1318 /* Common stuff */
1319 ei = PROC_I(inode);
1320 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1321 inode->i_op = &proc_def_inode_operations;
1324 * grab the reference to task.
1326 ei->pid = get_task_pid(task, PIDTYPE_PID);
1327 if (!ei->pid)
1328 goto out_unlock;
1330 inode->i_uid = 0;
1331 inode->i_gid = 0;
1332 if (task_dumpable(task)) {
1333 inode->i_uid = task->euid;
1334 inode->i_gid = task->egid;
1336 security_task_to_inode(task, inode);
1338 out:
1339 return inode;
1341 out_unlock:
1342 iput(inode);
1343 return NULL;
1346 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1348 struct inode *inode = dentry->d_inode;
1349 struct task_struct *task;
1350 generic_fillattr(inode, stat);
1352 rcu_read_lock();
1353 stat->uid = 0;
1354 stat->gid = 0;
1355 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1356 if (task) {
1357 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1358 task_dumpable(task)) {
1359 stat->uid = task->euid;
1360 stat->gid = task->egid;
1363 rcu_read_unlock();
1364 return 0;
1367 /* dentry stuff */
1370 * Exceptional case: normally we are not allowed to unhash a busy
1371 * directory. In this case, however, we can do it - no aliasing problems
1372 * due to the way we treat inodes.
1374 * Rewrite the inode's ownerships here because the owning task may have
1375 * performed a setuid(), etc.
1377 * Before the /proc/pid/status file was created the only way to read
1378 * the effective uid of a /process was to stat /proc/pid. Reading
1379 * /proc/pid/status is slow enough that procps and other packages
1380 * kept stating /proc/pid. To keep the rules in /proc simple I have
1381 * made this apply to all per process world readable and executable
1382 * directories.
1384 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1386 struct inode *inode = dentry->d_inode;
1387 struct task_struct *task = get_proc_task(inode);
1388 if (task) {
1389 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1390 task_dumpable(task)) {
1391 inode->i_uid = task->euid;
1392 inode->i_gid = task->egid;
1393 } else {
1394 inode->i_uid = 0;
1395 inode->i_gid = 0;
1397 inode->i_mode &= ~(S_ISUID | S_ISGID);
1398 security_task_to_inode(task, inode);
1399 put_task_struct(task);
1400 return 1;
1402 d_drop(dentry);
1403 return 0;
1406 static int pid_delete_dentry(struct dentry * dentry)
1408 /* Is the task we represent dead?
1409 * If so, then don't put the dentry on the lru list,
1410 * kill it immediately.
1412 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1415 static struct dentry_operations pid_dentry_operations =
1417 .d_revalidate = pid_revalidate,
1418 .d_delete = pid_delete_dentry,
1421 /* Lookups */
1423 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1424 struct task_struct *, const void *);
1427 * Fill a directory entry.
1429 * If possible create the dcache entry and derive our inode number and
1430 * file type from dcache entry.
1432 * Since all of the proc inode numbers are dynamically generated, the inode
1433 * numbers do not exist until the inode is cache. This means creating the
1434 * the dcache entry in readdir is necessary to keep the inode numbers
1435 * reported by readdir in sync with the inode numbers reported
1436 * by stat.
1438 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1439 char *name, int len,
1440 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1442 struct dentry *child, *dir = filp->f_path.dentry;
1443 struct inode *inode;
1444 struct qstr qname;
1445 ino_t ino = 0;
1446 unsigned type = DT_UNKNOWN;
1448 qname.name = name;
1449 qname.len = len;
1450 qname.hash = full_name_hash(name, len);
1452 child = d_lookup(dir, &qname);
1453 if (!child) {
1454 struct dentry *new;
1455 new = d_alloc(dir, &qname);
1456 if (new) {
1457 child = instantiate(dir->d_inode, new, task, ptr);
1458 if (child)
1459 dput(new);
1460 else
1461 child = new;
1464 if (!child || IS_ERR(child) || !child->d_inode)
1465 goto end_instantiate;
1466 inode = child->d_inode;
1467 if (inode) {
1468 ino = inode->i_ino;
1469 type = inode->i_mode >> 12;
1471 dput(child);
1472 end_instantiate:
1473 if (!ino)
1474 ino = find_inode_number(dir, &qname);
1475 if (!ino)
1476 ino = 1;
1477 return filldir(dirent, name, len, filp->f_pos, ino, type);
1480 static unsigned name_to_int(struct dentry *dentry)
1482 const char *name = dentry->d_name.name;
1483 int len = dentry->d_name.len;
1484 unsigned n = 0;
1486 if (len > 1 && *name == '0')
1487 goto out;
1488 while (len-- > 0) {
1489 unsigned c = *name++ - '0';
1490 if (c > 9)
1491 goto out;
1492 if (n >= (~0U-9)/10)
1493 goto out;
1494 n *= 10;
1495 n += c;
1497 return n;
1498 out:
1499 return ~0U;
1502 #define PROC_FDINFO_MAX 64
1504 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1506 struct task_struct *task = get_proc_task(inode);
1507 struct files_struct *files = NULL;
1508 struct file *file;
1509 int fd = proc_fd(inode);
1511 if (task) {
1512 files = get_files_struct(task);
1513 put_task_struct(task);
1515 if (files) {
1517 * We are not taking a ref to the file structure, so we must
1518 * hold ->file_lock.
1520 spin_lock(&files->file_lock);
1521 file = fcheck_files(files, fd);
1522 if (file) {
1523 if (path) {
1524 *path = file->f_path;
1525 path_get(&file->f_path);
1527 if (info)
1528 snprintf(info, PROC_FDINFO_MAX,
1529 "pos:\t%lli\n"
1530 "flags:\t0%o\n",
1531 (long long) file->f_pos,
1532 file->f_flags);
1533 spin_unlock(&files->file_lock);
1534 put_files_struct(files);
1535 return 0;
1537 spin_unlock(&files->file_lock);
1538 put_files_struct(files);
1540 return -ENOENT;
1543 static int proc_fd_link(struct inode *inode, struct path *path)
1545 return proc_fd_info(inode, path, NULL);
1548 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1550 struct inode *inode = dentry->d_inode;
1551 struct task_struct *task = get_proc_task(inode);
1552 int fd = proc_fd(inode);
1553 struct files_struct *files;
1555 if (task) {
1556 files = get_files_struct(task);
1557 if (files) {
1558 rcu_read_lock();
1559 if (fcheck_files(files, fd)) {
1560 rcu_read_unlock();
1561 put_files_struct(files);
1562 if (task_dumpable(task)) {
1563 inode->i_uid = task->euid;
1564 inode->i_gid = task->egid;
1565 } else {
1566 inode->i_uid = 0;
1567 inode->i_gid = 0;
1569 inode->i_mode &= ~(S_ISUID | S_ISGID);
1570 security_task_to_inode(task, inode);
1571 put_task_struct(task);
1572 return 1;
1574 rcu_read_unlock();
1575 put_files_struct(files);
1577 put_task_struct(task);
1579 d_drop(dentry);
1580 return 0;
1583 static struct dentry_operations tid_fd_dentry_operations =
1585 .d_revalidate = tid_fd_revalidate,
1586 .d_delete = pid_delete_dentry,
1589 static struct dentry *proc_fd_instantiate(struct inode *dir,
1590 struct dentry *dentry, struct task_struct *task, const void *ptr)
1592 unsigned fd = *(const unsigned *)ptr;
1593 struct file *file;
1594 struct files_struct *files;
1595 struct inode *inode;
1596 struct proc_inode *ei;
1597 struct dentry *error = ERR_PTR(-ENOENT);
1599 inode = proc_pid_make_inode(dir->i_sb, task);
1600 if (!inode)
1601 goto out;
1602 ei = PROC_I(inode);
1603 ei->fd = fd;
1604 files = get_files_struct(task);
1605 if (!files)
1606 goto out_iput;
1607 inode->i_mode = S_IFLNK;
1610 * We are not taking a ref to the file structure, so we must
1611 * hold ->file_lock.
1613 spin_lock(&files->file_lock);
1614 file = fcheck_files(files, fd);
1615 if (!file)
1616 goto out_unlock;
1617 if (file->f_mode & 1)
1618 inode->i_mode |= S_IRUSR | S_IXUSR;
1619 if (file->f_mode & 2)
1620 inode->i_mode |= S_IWUSR | S_IXUSR;
1621 spin_unlock(&files->file_lock);
1622 put_files_struct(files);
1624 inode->i_op = &proc_pid_link_inode_operations;
1625 inode->i_size = 64;
1626 ei->op.proc_get_link = proc_fd_link;
1627 dentry->d_op = &tid_fd_dentry_operations;
1628 d_add(dentry, inode);
1629 /* Close the race of the process dying before we return the dentry */
1630 if (tid_fd_revalidate(dentry, NULL))
1631 error = NULL;
1633 out:
1634 return error;
1635 out_unlock:
1636 spin_unlock(&files->file_lock);
1637 put_files_struct(files);
1638 out_iput:
1639 iput(inode);
1640 goto out;
1643 static struct dentry *proc_lookupfd_common(struct inode *dir,
1644 struct dentry *dentry,
1645 instantiate_t instantiate)
1647 struct task_struct *task = get_proc_task(dir);
1648 unsigned fd = name_to_int(dentry);
1649 struct dentry *result = ERR_PTR(-ENOENT);
1651 if (!task)
1652 goto out_no_task;
1653 if (fd == ~0U)
1654 goto out;
1656 result = instantiate(dir, dentry, task, &fd);
1657 out:
1658 put_task_struct(task);
1659 out_no_task:
1660 return result;
1663 static int proc_readfd_common(struct file * filp, void * dirent,
1664 filldir_t filldir, instantiate_t instantiate)
1666 struct dentry *dentry = filp->f_path.dentry;
1667 struct inode *inode = dentry->d_inode;
1668 struct task_struct *p = get_proc_task(inode);
1669 unsigned int fd, ino;
1670 int retval;
1671 struct files_struct * files;
1672 struct fdtable *fdt;
1674 retval = -ENOENT;
1675 if (!p)
1676 goto out_no_task;
1677 retval = 0;
1679 fd = filp->f_pos;
1680 switch (fd) {
1681 case 0:
1682 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1683 goto out;
1684 filp->f_pos++;
1685 case 1:
1686 ino = parent_ino(dentry);
1687 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1688 goto out;
1689 filp->f_pos++;
1690 default:
1691 files = get_files_struct(p);
1692 if (!files)
1693 goto out;
1694 rcu_read_lock();
1695 fdt = files_fdtable(files);
1696 for (fd = filp->f_pos-2;
1697 fd < fdt->max_fds;
1698 fd++, filp->f_pos++) {
1699 char name[PROC_NUMBUF];
1700 int len;
1702 if (!fcheck_files(files, fd))
1703 continue;
1704 rcu_read_unlock();
1706 len = snprintf(name, sizeof(name), "%d", fd);
1707 if (proc_fill_cache(filp, dirent, filldir,
1708 name, len, instantiate,
1709 p, &fd) < 0) {
1710 rcu_read_lock();
1711 break;
1713 rcu_read_lock();
1715 rcu_read_unlock();
1716 put_files_struct(files);
1718 out:
1719 put_task_struct(p);
1720 out_no_task:
1721 return retval;
1724 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1725 struct nameidata *nd)
1727 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1730 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1732 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1735 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1736 size_t len, loff_t *ppos)
1738 char tmp[PROC_FDINFO_MAX];
1739 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1740 if (!err)
1741 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1742 return err;
1745 static const struct file_operations proc_fdinfo_file_operations = {
1746 .open = nonseekable_open,
1747 .read = proc_fdinfo_read,
1750 static const struct file_operations proc_fd_operations = {
1751 .read = generic_read_dir,
1752 .readdir = proc_readfd,
1756 * /proc/pid/fd needs a special permission handler so that a process can still
1757 * access /proc/self/fd after it has executed a setuid().
1759 static int proc_fd_permission(struct inode *inode, int mask,
1760 struct nameidata *nd)
1762 int rv;
1764 rv = generic_permission(inode, mask, NULL);
1765 if (rv == 0)
1766 return 0;
1767 if (task_pid(current) == proc_pid(inode))
1768 rv = 0;
1769 return rv;
1773 * proc directories can do almost nothing..
1775 static const struct inode_operations proc_fd_inode_operations = {
1776 .lookup = proc_lookupfd,
1777 .permission = proc_fd_permission,
1778 .setattr = proc_setattr,
1781 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1782 struct dentry *dentry, struct task_struct *task, const void *ptr)
1784 unsigned fd = *(unsigned *)ptr;
1785 struct inode *inode;
1786 struct proc_inode *ei;
1787 struct dentry *error = ERR_PTR(-ENOENT);
1789 inode = proc_pid_make_inode(dir->i_sb, task);
1790 if (!inode)
1791 goto out;
1792 ei = PROC_I(inode);
1793 ei->fd = fd;
1794 inode->i_mode = S_IFREG | S_IRUSR;
1795 inode->i_fop = &proc_fdinfo_file_operations;
1796 dentry->d_op = &tid_fd_dentry_operations;
1797 d_add(dentry, inode);
1798 /* Close the race of the process dying before we return the dentry */
1799 if (tid_fd_revalidate(dentry, NULL))
1800 error = NULL;
1802 out:
1803 return error;
1806 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1807 struct dentry *dentry,
1808 struct nameidata *nd)
1810 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1813 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1815 return proc_readfd_common(filp, dirent, filldir,
1816 proc_fdinfo_instantiate);
1819 static const struct file_operations proc_fdinfo_operations = {
1820 .read = generic_read_dir,
1821 .readdir = proc_readfdinfo,
1825 * proc directories can do almost nothing..
1827 static const struct inode_operations proc_fdinfo_inode_operations = {
1828 .lookup = proc_lookupfdinfo,
1829 .setattr = proc_setattr,
1833 static struct dentry *proc_pident_instantiate(struct inode *dir,
1834 struct dentry *dentry, struct task_struct *task, const void *ptr)
1836 const struct pid_entry *p = ptr;
1837 struct inode *inode;
1838 struct proc_inode *ei;
1839 struct dentry *error = ERR_PTR(-EINVAL);
1841 inode = proc_pid_make_inode(dir->i_sb, task);
1842 if (!inode)
1843 goto out;
1845 ei = PROC_I(inode);
1846 inode->i_mode = p->mode;
1847 if (S_ISDIR(inode->i_mode))
1848 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1849 if (p->iop)
1850 inode->i_op = p->iop;
1851 if (p->fop)
1852 inode->i_fop = p->fop;
1853 ei->op = p->op;
1854 dentry->d_op = &pid_dentry_operations;
1855 d_add(dentry, inode);
1856 /* Close the race of the process dying before we return the dentry */
1857 if (pid_revalidate(dentry, NULL))
1858 error = NULL;
1859 out:
1860 return error;
1863 static struct dentry *proc_pident_lookup(struct inode *dir,
1864 struct dentry *dentry,
1865 const struct pid_entry *ents,
1866 unsigned int nents)
1868 struct inode *inode;
1869 struct dentry *error;
1870 struct task_struct *task = get_proc_task(dir);
1871 const struct pid_entry *p, *last;
1873 error = ERR_PTR(-ENOENT);
1874 inode = NULL;
1876 if (!task)
1877 goto out_no_task;
1880 * Yes, it does not scale. And it should not. Don't add
1881 * new entries into /proc/<tgid>/ without very good reasons.
1883 last = &ents[nents - 1];
1884 for (p = ents; p <= last; p++) {
1885 if (p->len != dentry->d_name.len)
1886 continue;
1887 if (!memcmp(dentry->d_name.name, p->name, p->len))
1888 break;
1890 if (p > last)
1891 goto out;
1893 error = proc_pident_instantiate(dir, dentry, task, p);
1894 out:
1895 put_task_struct(task);
1896 out_no_task:
1897 return error;
1900 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1901 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1903 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1904 proc_pident_instantiate, task, p);
1907 static int proc_pident_readdir(struct file *filp,
1908 void *dirent, filldir_t filldir,
1909 const struct pid_entry *ents, unsigned int nents)
1911 int i;
1912 struct dentry *dentry = filp->f_path.dentry;
1913 struct inode *inode = dentry->d_inode;
1914 struct task_struct *task = get_proc_task(inode);
1915 const struct pid_entry *p, *last;
1916 ino_t ino;
1917 int ret;
1919 ret = -ENOENT;
1920 if (!task)
1921 goto out_no_task;
1923 ret = 0;
1924 i = filp->f_pos;
1925 switch (i) {
1926 case 0:
1927 ino = inode->i_ino;
1928 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1929 goto out;
1930 i++;
1931 filp->f_pos++;
1932 /* fall through */
1933 case 1:
1934 ino = parent_ino(dentry);
1935 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1936 goto out;
1937 i++;
1938 filp->f_pos++;
1939 /* fall through */
1940 default:
1941 i -= 2;
1942 if (i >= nents) {
1943 ret = 1;
1944 goto out;
1946 p = ents + i;
1947 last = &ents[nents - 1];
1948 while (p <= last) {
1949 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1950 goto out;
1951 filp->f_pos++;
1952 p++;
1956 ret = 1;
1957 out:
1958 put_task_struct(task);
1959 out_no_task:
1960 return ret;
1963 #ifdef CONFIG_SECURITY
1964 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1965 size_t count, loff_t *ppos)
1967 struct inode * inode = file->f_path.dentry->d_inode;
1968 char *p = NULL;
1969 ssize_t length;
1970 struct task_struct *task = get_proc_task(inode);
1972 if (!task)
1973 return -ESRCH;
1975 length = security_getprocattr(task,
1976 (char*)file->f_path.dentry->d_name.name,
1977 &p);
1978 put_task_struct(task);
1979 if (length > 0)
1980 length = simple_read_from_buffer(buf, count, ppos, p, length);
1981 kfree(p);
1982 return length;
1985 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1986 size_t count, loff_t *ppos)
1988 struct inode * inode = file->f_path.dentry->d_inode;
1989 char *page;
1990 ssize_t length;
1991 struct task_struct *task = get_proc_task(inode);
1993 length = -ESRCH;
1994 if (!task)
1995 goto out_no_task;
1996 if (count > PAGE_SIZE)
1997 count = PAGE_SIZE;
1999 /* No partial writes. */
2000 length = -EINVAL;
2001 if (*ppos != 0)
2002 goto out;
2004 length = -ENOMEM;
2005 page = (char*)__get_free_page(GFP_TEMPORARY);
2006 if (!page)
2007 goto out;
2009 length = -EFAULT;
2010 if (copy_from_user(page, buf, count))
2011 goto out_free;
2013 length = security_setprocattr(task,
2014 (char*)file->f_path.dentry->d_name.name,
2015 (void*)page, count);
2016 out_free:
2017 free_page((unsigned long) page);
2018 out:
2019 put_task_struct(task);
2020 out_no_task:
2021 return length;
2024 static const struct file_operations proc_pid_attr_operations = {
2025 .read = proc_pid_attr_read,
2026 .write = proc_pid_attr_write,
2029 static const struct pid_entry attr_dir_stuff[] = {
2030 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2031 REG("prev", S_IRUGO, pid_attr),
2032 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2033 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2034 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2035 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
2038 static int proc_attr_dir_readdir(struct file * filp,
2039 void * dirent, filldir_t filldir)
2041 return proc_pident_readdir(filp,dirent,filldir,
2042 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2045 static const struct file_operations proc_attr_dir_operations = {
2046 .read = generic_read_dir,
2047 .readdir = proc_attr_dir_readdir,
2050 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2051 struct dentry *dentry, struct nameidata *nd)
2053 return proc_pident_lookup(dir, dentry,
2054 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2057 static const struct inode_operations proc_attr_dir_inode_operations = {
2058 .lookup = proc_attr_dir_lookup,
2059 .getattr = pid_getattr,
2060 .setattr = proc_setattr,
2063 #endif
2065 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2066 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2067 size_t count, loff_t *ppos)
2069 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2070 struct mm_struct *mm;
2071 char buffer[PROC_NUMBUF];
2072 size_t len;
2073 int ret;
2075 if (!task)
2076 return -ESRCH;
2078 ret = 0;
2079 mm = get_task_mm(task);
2080 if (mm) {
2081 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2082 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2083 MMF_DUMP_FILTER_SHIFT));
2084 mmput(mm);
2085 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2088 put_task_struct(task);
2090 return ret;
2093 static ssize_t proc_coredump_filter_write(struct file *file,
2094 const char __user *buf,
2095 size_t count,
2096 loff_t *ppos)
2098 struct task_struct *task;
2099 struct mm_struct *mm;
2100 char buffer[PROC_NUMBUF], *end;
2101 unsigned int val;
2102 int ret;
2103 int i;
2104 unsigned long mask;
2106 ret = -EFAULT;
2107 memset(buffer, 0, sizeof(buffer));
2108 if (count > sizeof(buffer) - 1)
2109 count = sizeof(buffer) - 1;
2110 if (copy_from_user(buffer, buf, count))
2111 goto out_no_task;
2113 ret = -EINVAL;
2114 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2115 if (*end == '\n')
2116 end++;
2117 if (end - buffer == 0)
2118 goto out_no_task;
2120 ret = -ESRCH;
2121 task = get_proc_task(file->f_dentry->d_inode);
2122 if (!task)
2123 goto out_no_task;
2125 ret = end - buffer;
2126 mm = get_task_mm(task);
2127 if (!mm)
2128 goto out_no_mm;
2130 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2131 if (val & mask)
2132 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2133 else
2134 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2137 mmput(mm);
2138 out_no_mm:
2139 put_task_struct(task);
2140 out_no_task:
2141 return ret;
2144 static const struct file_operations proc_coredump_filter_operations = {
2145 .read = proc_coredump_filter_read,
2146 .write = proc_coredump_filter_write,
2148 #endif
2151 * /proc/self:
2153 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2154 int buflen)
2156 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2157 pid_t tgid = task_tgid_nr_ns(current, ns);
2158 char tmp[PROC_NUMBUF];
2159 if (!tgid)
2160 return -ENOENT;
2161 sprintf(tmp, "%d", tgid);
2162 return vfs_readlink(dentry,buffer,buflen,tmp);
2165 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2167 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2168 pid_t tgid = task_tgid_nr_ns(current, ns);
2169 char tmp[PROC_NUMBUF];
2170 if (!tgid)
2171 return ERR_PTR(-ENOENT);
2172 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2173 return ERR_PTR(vfs_follow_link(nd,tmp));
2176 static const struct inode_operations proc_self_inode_operations = {
2177 .readlink = proc_self_readlink,
2178 .follow_link = proc_self_follow_link,
2182 * proc base
2184 * These are the directory entries in the root directory of /proc
2185 * that properly belong to the /proc filesystem, as they describe
2186 * describe something that is process related.
2188 static const struct pid_entry proc_base_stuff[] = {
2189 NOD("self", S_IFLNK|S_IRWXUGO,
2190 &proc_self_inode_operations, NULL, {}),
2194 * Exceptional case: normally we are not allowed to unhash a busy
2195 * directory. In this case, however, we can do it - no aliasing problems
2196 * due to the way we treat inodes.
2198 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2200 struct inode *inode = dentry->d_inode;
2201 struct task_struct *task = get_proc_task(inode);
2202 if (task) {
2203 put_task_struct(task);
2204 return 1;
2206 d_drop(dentry);
2207 return 0;
2210 static struct dentry_operations proc_base_dentry_operations =
2212 .d_revalidate = proc_base_revalidate,
2213 .d_delete = pid_delete_dentry,
2216 static struct dentry *proc_base_instantiate(struct inode *dir,
2217 struct dentry *dentry, struct task_struct *task, const void *ptr)
2219 const struct pid_entry *p = ptr;
2220 struct inode *inode;
2221 struct proc_inode *ei;
2222 struct dentry *error = ERR_PTR(-EINVAL);
2224 /* Allocate the inode */
2225 error = ERR_PTR(-ENOMEM);
2226 inode = new_inode(dir->i_sb);
2227 if (!inode)
2228 goto out;
2230 /* Initialize the inode */
2231 ei = PROC_I(inode);
2232 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2235 * grab the reference to the task.
2237 ei->pid = get_task_pid(task, PIDTYPE_PID);
2238 if (!ei->pid)
2239 goto out_iput;
2241 inode->i_uid = 0;
2242 inode->i_gid = 0;
2243 inode->i_mode = p->mode;
2244 if (S_ISDIR(inode->i_mode))
2245 inode->i_nlink = 2;
2246 if (S_ISLNK(inode->i_mode))
2247 inode->i_size = 64;
2248 if (p->iop)
2249 inode->i_op = p->iop;
2250 if (p->fop)
2251 inode->i_fop = p->fop;
2252 ei->op = p->op;
2253 dentry->d_op = &proc_base_dentry_operations;
2254 d_add(dentry, inode);
2255 error = NULL;
2256 out:
2257 return error;
2258 out_iput:
2259 iput(inode);
2260 goto out;
2263 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2265 struct dentry *error;
2266 struct task_struct *task = get_proc_task(dir);
2267 const struct pid_entry *p, *last;
2269 error = ERR_PTR(-ENOENT);
2271 if (!task)
2272 goto out_no_task;
2274 /* Lookup the directory entry */
2275 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2276 for (p = proc_base_stuff; p <= last; p++) {
2277 if (p->len != dentry->d_name.len)
2278 continue;
2279 if (!memcmp(dentry->d_name.name, p->name, p->len))
2280 break;
2282 if (p > last)
2283 goto out;
2285 error = proc_base_instantiate(dir, dentry, task, p);
2287 out:
2288 put_task_struct(task);
2289 out_no_task:
2290 return error;
2293 static int proc_base_fill_cache(struct file *filp, void *dirent,
2294 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2296 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2297 proc_base_instantiate, task, p);
2300 #ifdef CONFIG_TASK_IO_ACCOUNTING
2301 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2303 return sprintf(buffer,
2304 #ifdef CONFIG_TASK_XACCT
2305 "rchar: %llu\n"
2306 "wchar: %llu\n"
2307 "syscr: %llu\n"
2308 "syscw: %llu\n"
2309 #endif
2310 "read_bytes: %llu\n"
2311 "write_bytes: %llu\n"
2312 "cancelled_write_bytes: %llu\n",
2313 #ifdef CONFIG_TASK_XACCT
2314 (unsigned long long)task->rchar,
2315 (unsigned long long)task->wchar,
2316 (unsigned long long)task->syscr,
2317 (unsigned long long)task->syscw,
2318 #endif
2319 (unsigned long long)task->ioac.read_bytes,
2320 (unsigned long long)task->ioac.write_bytes,
2321 (unsigned long long)task->ioac.cancelled_write_bytes);
2323 #endif
2326 * Thread groups
2328 static const struct file_operations proc_task_operations;
2329 static const struct inode_operations proc_task_inode_operations;
2331 static const struct pid_entry tgid_base_stuff[] = {
2332 DIR("task", S_IRUGO|S_IXUGO, task),
2333 DIR("fd", S_IRUSR|S_IXUSR, fd),
2334 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2335 <<<<<<< HEAD:fs/proc/base.c
2336 =======
2337 #ifdef CONFIG_NET
2338 DIR("net", S_IRUGO|S_IXUSR, net),
2339 #endif
2340 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
2341 REG("environ", S_IRUSR, environ),
2342 INF("auxv", S_IRUSR, pid_auxv),
2343 ONE("status", S_IRUGO, pid_status),
2344 INF("limits", S_IRUSR, pid_limits),
2345 #ifdef CONFIG_SCHED_DEBUG
2346 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2347 #endif
2348 INF("cmdline", S_IRUGO, pid_cmdline),
2349 ONE("stat", S_IRUGO, tgid_stat),
2350 ONE("statm", S_IRUGO, pid_statm),
2351 REG("maps", S_IRUGO, maps),
2352 #ifdef CONFIG_NUMA
2353 REG("numa_maps", S_IRUGO, numa_maps),
2354 #endif
2355 REG("mem", S_IRUSR|S_IWUSR, mem),
2356 LNK("cwd", cwd),
2357 LNK("root", root),
2358 LNK("exe", exe),
2359 REG("mounts", S_IRUGO, mounts),
2360 REG("mountstats", S_IRUSR, mountstats),
2361 #ifdef CONFIG_PROC_PAGE_MONITOR
2362 REG("clear_refs", S_IWUSR, clear_refs),
2363 REG("smaps", S_IRUGO, smaps),
2364 REG("pagemap", S_IRUSR, pagemap),
2365 #endif
2366 #ifdef CONFIG_SECURITY
2367 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2368 #endif
2369 #ifdef CONFIG_KALLSYMS
2370 INF("wchan", S_IRUGO, pid_wchan),
2371 #endif
2372 #ifdef CONFIG_SCHEDSTATS
2373 INF("schedstat", S_IRUGO, pid_schedstat),
2374 #endif
2375 #ifdef CONFIG_LATENCYTOP
2376 REG("latency", S_IRUGO, lstats),
2377 #endif
2378 #ifdef CONFIG_PROC_PID_CPUSET
2379 REG("cpuset", S_IRUGO, cpuset),
2380 #endif
2381 #ifdef CONFIG_CGROUPS
2382 REG("cgroup", S_IRUGO, cgroup),
2383 #endif
2384 INF("oom_score", S_IRUGO, oom_score),
2385 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2386 #ifdef CONFIG_AUDITSYSCALL
2387 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2388 <<<<<<< HEAD:fs/proc/base.c
2389 =======
2390 REG("sessionid", S_IRUSR, sessionid),
2391 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
2392 #endif
2393 #ifdef CONFIG_FAULT_INJECTION
2394 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2395 #endif
2396 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2397 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2398 #endif
2399 #ifdef CONFIG_TASK_IO_ACCOUNTING
2400 INF("io", S_IRUGO, pid_io_accounting),
2401 #endif
2404 static int proc_tgid_base_readdir(struct file * filp,
2405 void * dirent, filldir_t filldir)
2407 return proc_pident_readdir(filp,dirent,filldir,
2408 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2411 static const struct file_operations proc_tgid_base_operations = {
2412 .read = generic_read_dir,
2413 .readdir = proc_tgid_base_readdir,
2416 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2417 return proc_pident_lookup(dir, dentry,
2418 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2421 static const struct inode_operations proc_tgid_base_inode_operations = {
2422 .lookup = proc_tgid_base_lookup,
2423 .getattr = pid_getattr,
2424 .setattr = proc_setattr,
2427 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2429 struct dentry *dentry, *leader, *dir;
2430 char buf[PROC_NUMBUF];
2431 struct qstr name;
2433 name.name = buf;
2434 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2435 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2436 if (dentry) {
2437 if (!(current->flags & PF_EXITING))
2438 shrink_dcache_parent(dentry);
2439 d_drop(dentry);
2440 dput(dentry);
2443 if (tgid == 0)
2444 goto out;
2446 name.name = buf;
2447 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2448 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2449 if (!leader)
2450 goto out;
2452 name.name = "task";
2453 name.len = strlen(name.name);
2454 dir = d_hash_and_lookup(leader, &name);
2455 if (!dir)
2456 goto out_put_leader;
2458 name.name = buf;
2459 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2460 dentry = d_hash_and_lookup(dir, &name);
2461 if (dentry) {
2462 shrink_dcache_parent(dentry);
2463 d_drop(dentry);
2464 dput(dentry);
2467 dput(dir);
2468 out_put_leader:
2469 dput(leader);
2470 out:
2471 return;
2475 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2476 * @task: task that should be flushed.
2478 * When flushing dentries from proc, one needs to flush them from global
2479 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2480 * in. This call is supposed to do all of this job.
2482 * Looks in the dcache for
2483 * /proc/@pid
2484 * /proc/@tgid/task/@pid
2485 * if either directory is present flushes it and all of it'ts children
2486 * from the dcache.
2488 * It is safe and reasonable to cache /proc entries for a task until
2489 * that task exits. After that they just clog up the dcache with
2490 * useless entries, possibly causing useful dcache entries to be
2491 * flushed instead. This routine is proved to flush those useless
2492 * dcache entries at process exit time.
2494 * NOTE: This routine is just an optimization so it does not guarantee
2495 * that no dcache entries will exist at process exit time it
2496 * just makes it very unlikely that any will persist.
2499 void proc_flush_task(struct task_struct *task)
2501 int i;
2502 struct pid *pid, *tgid = NULL;
2503 struct upid *upid;
2505 pid = task_pid(task);
2506 if (thread_group_leader(task))
2507 tgid = task_tgid(task);
2509 for (i = 0; i <= pid->level; i++) {
2510 upid = &pid->numbers[i];
2511 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2512 tgid ? tgid->numbers[i].nr : 0);
2515 upid = &pid->numbers[pid->level];
2516 if (upid->nr == 1)
2517 pid_ns_release_proc(upid->ns);
2520 static struct dentry *proc_pid_instantiate(struct inode *dir,
2521 struct dentry * dentry,
2522 struct task_struct *task, const void *ptr)
2524 struct dentry *error = ERR_PTR(-ENOENT);
2525 struct inode *inode;
2527 inode = proc_pid_make_inode(dir->i_sb, task);
2528 if (!inode)
2529 goto out;
2531 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2532 inode->i_op = &proc_tgid_base_inode_operations;
2533 inode->i_fop = &proc_tgid_base_operations;
2534 inode->i_flags|=S_IMMUTABLE;
2535 inode->i_nlink = 5;
2536 #ifdef CONFIG_SECURITY
2537 inode->i_nlink += 1;
2538 #endif
2540 dentry->d_op = &pid_dentry_operations;
2542 d_add(dentry, inode);
2543 /* Close the race of the process dying before we return the dentry */
2544 if (pid_revalidate(dentry, NULL))
2545 error = NULL;
2546 out:
2547 return error;
2550 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2552 struct dentry *result = ERR_PTR(-ENOENT);
2553 struct task_struct *task;
2554 unsigned tgid;
2555 struct pid_namespace *ns;
2557 result = proc_base_lookup(dir, dentry);
2558 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2559 goto out;
2561 tgid = name_to_int(dentry);
2562 if (tgid == ~0U)
2563 goto out;
2565 ns = dentry->d_sb->s_fs_info;
2566 rcu_read_lock();
2567 task = find_task_by_pid_ns(tgid, ns);
2568 if (task)
2569 get_task_struct(task);
2570 rcu_read_unlock();
2571 if (!task)
2572 goto out;
2574 result = proc_pid_instantiate(dir, dentry, task, NULL);
2575 put_task_struct(task);
2576 out:
2577 return result;
2581 * Find the first task with tgid >= tgid
2584 struct tgid_iter {
2585 unsigned int tgid;
2586 struct task_struct *task;
2588 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2590 struct pid *pid;
2592 if (iter.task)
2593 put_task_struct(iter.task);
2594 rcu_read_lock();
2595 retry:
2596 iter.task = NULL;
2597 pid = find_ge_pid(iter.tgid, ns);
2598 if (pid) {
2599 iter.tgid = pid_nr_ns(pid, ns);
2600 iter.task = pid_task(pid, PIDTYPE_PID);
2601 /* What we to know is if the pid we have find is the
2602 * pid of a thread_group_leader. Testing for task
2603 * being a thread_group_leader is the obvious thing
2604 * todo but there is a window when it fails, due to
2605 * the pid transfer logic in de_thread.
2607 * So we perform the straight forward test of seeing
2608 * if the pid we have found is the pid of a thread
2609 * group leader, and don't worry if the task we have
2610 * found doesn't happen to be a thread group leader.
2611 * As we don't care in the case of readdir.
2613 if (!iter.task || !has_group_leader_pid(iter.task)) {
2614 iter.tgid += 1;
2615 goto retry;
2617 get_task_struct(iter.task);
2619 rcu_read_unlock();
2620 return iter;
2623 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2625 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2626 struct tgid_iter iter)
2628 char name[PROC_NUMBUF];
2629 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2630 return proc_fill_cache(filp, dirent, filldir, name, len,
2631 proc_pid_instantiate, iter.task, NULL);
2634 /* for the /proc/ directory itself, after non-process stuff has been done */
2635 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2637 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2638 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2639 struct tgid_iter iter;
2640 struct pid_namespace *ns;
2642 if (!reaper)
2643 goto out_no_task;
2645 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2646 const struct pid_entry *p = &proc_base_stuff[nr];
2647 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2648 goto out;
2651 ns = filp->f_dentry->d_sb->s_fs_info;
2652 iter.task = NULL;
2653 iter.tgid = filp->f_pos - TGID_OFFSET;
2654 for (iter = next_tgid(ns, iter);
2655 iter.task;
2656 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2657 filp->f_pos = iter.tgid + TGID_OFFSET;
2658 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2659 put_task_struct(iter.task);
2660 goto out;
2663 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2664 out:
2665 put_task_struct(reaper);
2666 out_no_task:
2667 return 0;
2671 * Tasks
2673 static const struct pid_entry tid_base_stuff[] = {
2674 DIR("fd", S_IRUSR|S_IXUSR, fd),
2675 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2676 REG("environ", S_IRUSR, environ),
2677 INF("auxv", S_IRUSR, pid_auxv),
2678 ONE("status", S_IRUGO, pid_status),
2679 INF("limits", S_IRUSR, pid_limits),
2680 #ifdef CONFIG_SCHED_DEBUG
2681 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2682 #endif
2683 INF("cmdline", S_IRUGO, pid_cmdline),
2684 ONE("stat", S_IRUGO, tid_stat),
2685 ONE("statm", S_IRUGO, pid_statm),
2686 REG("maps", S_IRUGO, maps),
2687 #ifdef CONFIG_NUMA
2688 REG("numa_maps", S_IRUGO, numa_maps),
2689 #endif
2690 REG("mem", S_IRUSR|S_IWUSR, mem),
2691 LNK("cwd", cwd),
2692 LNK("root", root),
2693 LNK("exe", exe),
2694 REG("mounts", S_IRUGO, mounts),
2695 #ifdef CONFIG_PROC_PAGE_MONITOR
2696 REG("clear_refs", S_IWUSR, clear_refs),
2697 REG("smaps", S_IRUGO, smaps),
2698 REG("pagemap", S_IRUSR, pagemap),
2699 #endif
2700 #ifdef CONFIG_SECURITY
2701 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2702 #endif
2703 #ifdef CONFIG_KALLSYMS
2704 INF("wchan", S_IRUGO, pid_wchan),
2705 #endif
2706 #ifdef CONFIG_SCHEDSTATS
2707 INF("schedstat", S_IRUGO, pid_schedstat),
2708 #endif
2709 #ifdef CONFIG_LATENCYTOP
2710 REG("latency", S_IRUGO, lstats),
2711 #endif
2712 #ifdef CONFIG_PROC_PID_CPUSET
2713 REG("cpuset", S_IRUGO, cpuset),
2714 #endif
2715 #ifdef CONFIG_CGROUPS
2716 REG("cgroup", S_IRUGO, cgroup),
2717 #endif
2718 INF("oom_score", S_IRUGO, oom_score),
2719 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2720 #ifdef CONFIG_AUDITSYSCALL
2721 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2722 <<<<<<< HEAD:fs/proc/base.c
2723 =======
2724 REG("sessionid", S_IRUSR, sessionid),
2725 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/proc/base.c
2726 #endif
2727 #ifdef CONFIG_FAULT_INJECTION
2728 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2729 #endif
2732 static int proc_tid_base_readdir(struct file * filp,
2733 void * dirent, filldir_t filldir)
2735 return proc_pident_readdir(filp,dirent,filldir,
2736 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2739 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2740 return proc_pident_lookup(dir, dentry,
2741 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2744 static const struct file_operations proc_tid_base_operations = {
2745 .read = generic_read_dir,
2746 .readdir = proc_tid_base_readdir,
2749 static const struct inode_operations proc_tid_base_inode_operations = {
2750 .lookup = proc_tid_base_lookup,
2751 .getattr = pid_getattr,
2752 .setattr = proc_setattr,
2755 static struct dentry *proc_task_instantiate(struct inode *dir,
2756 struct dentry *dentry, struct task_struct *task, const void *ptr)
2758 struct dentry *error = ERR_PTR(-ENOENT);
2759 struct inode *inode;
2760 inode = proc_pid_make_inode(dir->i_sb, task);
2762 if (!inode)
2763 goto out;
2764 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2765 inode->i_op = &proc_tid_base_inode_operations;
2766 inode->i_fop = &proc_tid_base_operations;
2767 inode->i_flags|=S_IMMUTABLE;
2768 inode->i_nlink = 4;
2769 #ifdef CONFIG_SECURITY
2770 inode->i_nlink += 1;
2771 #endif
2773 dentry->d_op = &pid_dentry_operations;
2775 d_add(dentry, inode);
2776 /* Close the race of the process dying before we return the dentry */
2777 if (pid_revalidate(dentry, NULL))
2778 error = NULL;
2779 out:
2780 return error;
2783 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2785 struct dentry *result = ERR_PTR(-ENOENT);
2786 struct task_struct *task;
2787 struct task_struct *leader = get_proc_task(dir);
2788 unsigned tid;
2789 struct pid_namespace *ns;
2791 if (!leader)
2792 goto out_no_task;
2794 tid = name_to_int(dentry);
2795 if (tid == ~0U)
2796 goto out;
2798 ns = dentry->d_sb->s_fs_info;
2799 rcu_read_lock();
2800 task = find_task_by_pid_ns(tid, ns);
2801 if (task)
2802 get_task_struct(task);
2803 rcu_read_unlock();
2804 if (!task)
2805 goto out;
2806 if (!same_thread_group(leader, task))
2807 goto out_drop_task;
2809 result = proc_task_instantiate(dir, dentry, task, NULL);
2810 out_drop_task:
2811 put_task_struct(task);
2812 out:
2813 put_task_struct(leader);
2814 out_no_task:
2815 return result;
2819 * Find the first tid of a thread group to return to user space.
2821 * Usually this is just the thread group leader, but if the users
2822 * buffer was too small or there was a seek into the middle of the
2823 * directory we have more work todo.
2825 * In the case of a short read we start with find_task_by_pid.
2827 * In the case of a seek we start with the leader and walk nr
2828 * threads past it.
2830 static struct task_struct *first_tid(struct task_struct *leader,
2831 int tid, int nr, struct pid_namespace *ns)
2833 struct task_struct *pos;
2835 rcu_read_lock();
2836 /* Attempt to start with the pid of a thread */
2837 if (tid && (nr > 0)) {
2838 pos = find_task_by_pid_ns(tid, ns);
2839 if (pos && (pos->group_leader == leader))
2840 goto found;
2843 /* If nr exceeds the number of threads there is nothing todo */
2844 pos = NULL;
2845 if (nr && nr >= get_nr_threads(leader))
2846 goto out;
2848 /* If we haven't found our starting place yet start
2849 * with the leader and walk nr threads forward.
2851 for (pos = leader; nr > 0; --nr) {
2852 pos = next_thread(pos);
2853 if (pos == leader) {
2854 pos = NULL;
2855 goto out;
2858 found:
2859 get_task_struct(pos);
2860 out:
2861 rcu_read_unlock();
2862 return pos;
2866 * Find the next thread in the thread list.
2867 * Return NULL if there is an error or no next thread.
2869 * The reference to the input task_struct is released.
2871 static struct task_struct *next_tid(struct task_struct *start)
2873 struct task_struct *pos = NULL;
2874 rcu_read_lock();
2875 if (pid_alive(start)) {
2876 pos = next_thread(start);
2877 if (thread_group_leader(pos))
2878 pos = NULL;
2879 else
2880 get_task_struct(pos);
2882 rcu_read_unlock();
2883 put_task_struct(start);
2884 return pos;
2887 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2888 struct task_struct *task, int tid)
2890 char name[PROC_NUMBUF];
2891 int len = snprintf(name, sizeof(name), "%d", tid);
2892 return proc_fill_cache(filp, dirent, filldir, name, len,
2893 proc_task_instantiate, task, NULL);
2896 /* for the /proc/TGID/task/ directories */
2897 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2899 struct dentry *dentry = filp->f_path.dentry;
2900 struct inode *inode = dentry->d_inode;
2901 struct task_struct *leader = NULL;
2902 struct task_struct *task;
2903 int retval = -ENOENT;
2904 ino_t ino;
2905 int tid;
2906 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2907 struct pid_namespace *ns;
2909 task = get_proc_task(inode);
2910 if (!task)
2911 goto out_no_task;
2912 rcu_read_lock();
2913 if (pid_alive(task)) {
2914 leader = task->group_leader;
2915 get_task_struct(leader);
2917 rcu_read_unlock();
2918 put_task_struct(task);
2919 if (!leader)
2920 goto out_no_task;
2921 retval = 0;
2923 switch (pos) {
2924 case 0:
2925 ino = inode->i_ino;
2926 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2927 goto out;
2928 pos++;
2929 /* fall through */
2930 case 1:
2931 ino = parent_ino(dentry);
2932 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2933 goto out;
2934 pos++;
2935 /* fall through */
2938 /* f_version caches the tgid value that the last readdir call couldn't
2939 * return. lseek aka telldir automagically resets f_version to 0.
2941 ns = filp->f_dentry->d_sb->s_fs_info;
2942 tid = (int)filp->f_version;
2943 filp->f_version = 0;
2944 for (task = first_tid(leader, tid, pos - 2, ns);
2945 task;
2946 task = next_tid(task), pos++) {
2947 tid = task_pid_nr_ns(task, ns);
2948 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2949 /* returning this tgid failed, save it as the first
2950 * pid for the next readir call */
2951 filp->f_version = (u64)tid;
2952 put_task_struct(task);
2953 break;
2956 out:
2957 filp->f_pos = pos;
2958 put_task_struct(leader);
2959 out_no_task:
2960 return retval;
2963 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2965 struct inode *inode = dentry->d_inode;
2966 struct task_struct *p = get_proc_task(inode);
2967 generic_fillattr(inode, stat);
2969 if (p) {
2970 rcu_read_lock();
2971 stat->nlink += get_nr_threads(p);
2972 rcu_read_unlock();
2973 put_task_struct(p);
2976 return 0;
2979 static const struct inode_operations proc_task_inode_operations = {
2980 .lookup = proc_task_lookup,
2981 .getattr = proc_task_getattr,
2982 .setattr = proc_setattr,
2985 static const struct file_operations proc_task_operations = {
2986 .read = generic_read_dir,
2987 .readdir = proc_task_readdir,