]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - fs/proc/base.c
Task Control Groups: add procfs interface
[mirror_ubuntu-hirsute-kernel.git] / fs / proc / base.c
1 /*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
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.
14 *
15 *
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>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
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.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
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.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
48 */
49
50 #include <asm/uaccess.h>
51
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/cgroup.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 <linux/elf.h>
77 #include "internal.h"
78
79 /* NOTE:
80 * Implementing inode permission operations in /proc is almost
81 * certainly an error. Permission checks need to happen during
82 * each system call not at open time. The reason is that most of
83 * what we wish to check for permissions in /proc varies at runtime.
84 *
85 * The classic example of a problem is opening file descriptors
86 * in /proc for a task before it execs a suid executable.
87 */
88
89
90 /* Worst case buffer size needed for holding an integer. */
91 #define PROC_NUMBUF 13
92
93 struct pid_entry {
94 char *name;
95 int len;
96 mode_t mode;
97 const struct inode_operations *iop;
98 const struct file_operations *fop;
99 union proc_op op;
100 };
101
102 #define NOD(NAME, MODE, IOP, FOP, OP) { \
103 .name = (NAME), \
104 .len = sizeof(NAME) - 1, \
105 .mode = MODE, \
106 .iop = IOP, \
107 .fop = FOP, \
108 .op = OP, \
109 }
110
111 #define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 {} )
115 #define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
119 #define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
121 &proc_##OTYPE##_operations, {})
122 #define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
126
127 int maps_protect;
128 EXPORT_SYMBOL(maps_protect);
129
130 static struct fs_struct *get_fs_struct(struct task_struct *task)
131 {
132 struct fs_struct *fs;
133 task_lock(task);
134 fs = task->fs;
135 if(fs)
136 atomic_inc(&fs->count);
137 task_unlock(task);
138 return fs;
139 }
140
141 static int get_nr_threads(struct task_struct *tsk)
142 {
143 /* Must be called with the rcu_read_lock held */
144 unsigned long flags;
145 int count = 0;
146
147 if (lock_task_sighand(tsk, &flags)) {
148 count = atomic_read(&tsk->signal->count);
149 unlock_task_sighand(tsk, &flags);
150 }
151 return count;
152 }
153
154 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
155 {
156 struct task_struct *task = get_proc_task(inode);
157 struct fs_struct *fs = NULL;
158 int result = -ENOENT;
159
160 if (task) {
161 fs = get_fs_struct(task);
162 put_task_struct(task);
163 }
164 if (fs) {
165 read_lock(&fs->lock);
166 *mnt = mntget(fs->pwdmnt);
167 *dentry = dget(fs->pwd);
168 read_unlock(&fs->lock);
169 result = 0;
170 put_fs_struct(fs);
171 }
172 return result;
173 }
174
175 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
176 {
177 struct task_struct *task = get_proc_task(inode);
178 struct fs_struct *fs = NULL;
179 int result = -ENOENT;
180
181 if (task) {
182 fs = get_fs_struct(task);
183 put_task_struct(task);
184 }
185 if (fs) {
186 read_lock(&fs->lock);
187 *mnt = mntget(fs->rootmnt);
188 *dentry = dget(fs->root);
189 read_unlock(&fs->lock);
190 result = 0;
191 put_fs_struct(fs);
192 }
193 return result;
194 }
195
196 #define MAY_PTRACE(task) \
197 (task == current || \
198 (task->parent == current && \
199 (task->ptrace & PT_PTRACED) && \
200 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
201 security_ptrace(current,task) == 0))
202
203 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
204 {
205 int res = 0;
206 unsigned int len;
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 goto out;
210 if (!mm->arg_end)
211 goto out_mm; /* Shh! No looking before we're done */
212
213 len = mm->arg_end - mm->arg_start;
214
215 if (len > PAGE_SIZE)
216 len = PAGE_SIZE;
217
218 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
219
220 // If the nul at the end of args has been overwritten, then
221 // assume application is using setproctitle(3).
222 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
223 len = strnlen(buffer, res);
224 if (len < res) {
225 res = len;
226 } else {
227 len = mm->env_end - mm->env_start;
228 if (len > PAGE_SIZE - res)
229 len = PAGE_SIZE - res;
230 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
231 res = strnlen(buffer, res);
232 }
233 }
234 out_mm:
235 mmput(mm);
236 out:
237 return res;
238 }
239
240 static int proc_pid_auxv(struct task_struct *task, char *buffer)
241 {
242 int res = 0;
243 struct mm_struct *mm = get_task_mm(task);
244 if (mm) {
245 unsigned int nwords = 0;
246 do
247 nwords += 2;
248 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
249 res = nwords * sizeof(mm->saved_auxv[0]);
250 if (res > PAGE_SIZE)
251 res = PAGE_SIZE;
252 memcpy(buffer, mm->saved_auxv, res);
253 mmput(mm);
254 }
255 return res;
256 }
257
258
259 #ifdef CONFIG_KALLSYMS
260 /*
261 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
262 * Returns the resolved symbol. If that fails, simply return the address.
263 */
264 static int proc_pid_wchan(struct task_struct *task, char *buffer)
265 {
266 unsigned long wchan;
267 char symname[KSYM_NAME_LEN];
268
269 wchan = get_wchan(task);
270
271 if (lookup_symbol_name(wchan, symname) < 0)
272 return sprintf(buffer, "%lu", wchan);
273 else
274 return sprintf(buffer, "%s", symname);
275 }
276 #endif /* CONFIG_KALLSYMS */
277
278 #ifdef CONFIG_SCHEDSTATS
279 /*
280 * Provides /proc/PID/schedstat
281 */
282 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
283 {
284 return sprintf(buffer, "%llu %llu %lu\n",
285 task->sched_info.cpu_time,
286 task->sched_info.run_delay,
287 task->sched_info.pcount);
288 }
289 #endif
290
291 /* The badness from the OOM killer */
292 unsigned long badness(struct task_struct *p, unsigned long uptime);
293 static int proc_oom_score(struct task_struct *task, char *buffer)
294 {
295 unsigned long points;
296 struct timespec uptime;
297
298 do_posix_clock_monotonic_gettime(&uptime);
299 read_lock(&tasklist_lock);
300 points = badness(task, uptime.tv_sec);
301 read_unlock(&tasklist_lock);
302 return sprintf(buffer, "%lu\n", points);
303 }
304
305 /************************************************************************/
306 /* Here the fs part begins */
307 /************************************************************************/
308
309 /* permission checks */
310 static int proc_fd_access_allowed(struct inode *inode)
311 {
312 struct task_struct *task;
313 int allowed = 0;
314 /* Allow access to a task's file descriptors if it is us or we
315 * may use ptrace attach to the process and find out that
316 * information.
317 */
318 task = get_proc_task(inode);
319 if (task) {
320 allowed = ptrace_may_attach(task);
321 put_task_struct(task);
322 }
323 return allowed;
324 }
325
326 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
327 {
328 int error;
329 struct inode *inode = dentry->d_inode;
330
331 if (attr->ia_valid & ATTR_MODE)
332 return -EPERM;
333
334 error = inode_change_ok(inode, attr);
335 if (!error)
336 error = inode_setattr(inode, attr);
337 return error;
338 }
339
340 static const struct inode_operations proc_def_inode_operations = {
341 .setattr = proc_setattr,
342 };
343
344 extern struct seq_operations mounts_op;
345 struct proc_mounts {
346 struct seq_file m;
347 int event;
348 };
349
350 static int mounts_open(struct inode *inode, struct file *file)
351 {
352 struct task_struct *task = get_proc_task(inode);
353 struct mnt_namespace *ns = NULL;
354 struct proc_mounts *p;
355 int ret = -EINVAL;
356
357 if (task) {
358 task_lock(task);
359 if (task->nsproxy) {
360 ns = task->nsproxy->mnt_ns;
361 if (ns)
362 get_mnt_ns(ns);
363 }
364 task_unlock(task);
365 put_task_struct(task);
366 }
367
368 if (ns) {
369 ret = -ENOMEM;
370 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
371 if (p) {
372 file->private_data = &p->m;
373 ret = seq_open(file, &mounts_op);
374 if (!ret) {
375 p->m.private = ns;
376 p->event = ns->event;
377 return 0;
378 }
379 kfree(p);
380 }
381 put_mnt_ns(ns);
382 }
383 return ret;
384 }
385
386 static int mounts_release(struct inode *inode, struct file *file)
387 {
388 struct seq_file *m = file->private_data;
389 struct mnt_namespace *ns = m->private;
390 put_mnt_ns(ns);
391 return seq_release(inode, file);
392 }
393
394 static unsigned mounts_poll(struct file *file, poll_table *wait)
395 {
396 struct proc_mounts *p = file->private_data;
397 struct mnt_namespace *ns = p->m.private;
398 unsigned res = 0;
399
400 poll_wait(file, &ns->poll, wait);
401
402 spin_lock(&vfsmount_lock);
403 if (p->event != ns->event) {
404 p->event = ns->event;
405 res = POLLERR;
406 }
407 spin_unlock(&vfsmount_lock);
408
409 return res;
410 }
411
412 static const struct file_operations proc_mounts_operations = {
413 .open = mounts_open,
414 .read = seq_read,
415 .llseek = seq_lseek,
416 .release = mounts_release,
417 .poll = mounts_poll,
418 };
419
420 extern struct seq_operations mountstats_op;
421 static int mountstats_open(struct inode *inode, struct file *file)
422 {
423 int ret = seq_open(file, &mountstats_op);
424
425 if (!ret) {
426 struct seq_file *m = file->private_data;
427 struct mnt_namespace *mnt_ns = NULL;
428 struct task_struct *task = get_proc_task(inode);
429
430 if (task) {
431 task_lock(task);
432 if (task->nsproxy)
433 mnt_ns = task->nsproxy->mnt_ns;
434 if (mnt_ns)
435 get_mnt_ns(mnt_ns);
436 task_unlock(task);
437 put_task_struct(task);
438 }
439
440 if (mnt_ns)
441 m->private = mnt_ns;
442 else {
443 seq_release(inode, file);
444 ret = -EINVAL;
445 }
446 }
447 return ret;
448 }
449
450 static const struct file_operations proc_mountstats_operations = {
451 .open = mountstats_open,
452 .read = seq_read,
453 .llseek = seq_lseek,
454 .release = mounts_release,
455 };
456
457 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
458
459 static ssize_t proc_info_read(struct file * file, char __user * buf,
460 size_t count, loff_t *ppos)
461 {
462 struct inode * inode = file->f_path.dentry->d_inode;
463 unsigned long page;
464 ssize_t length;
465 struct task_struct *task = get_proc_task(inode);
466
467 length = -ESRCH;
468 if (!task)
469 goto out_no_task;
470
471 if (count > PROC_BLOCK_SIZE)
472 count = PROC_BLOCK_SIZE;
473
474 length = -ENOMEM;
475 if (!(page = __get_free_page(GFP_TEMPORARY)))
476 goto out;
477
478 length = PROC_I(inode)->op.proc_read(task, (char*)page);
479
480 if (length >= 0)
481 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
482 free_page(page);
483 out:
484 put_task_struct(task);
485 out_no_task:
486 return length;
487 }
488
489 static const struct file_operations proc_info_file_operations = {
490 .read = proc_info_read,
491 };
492
493 static int mem_open(struct inode* inode, struct file* file)
494 {
495 file->private_data = (void*)((long)current->self_exec_id);
496 return 0;
497 }
498
499 static ssize_t mem_read(struct file * file, char __user * buf,
500 size_t count, loff_t *ppos)
501 {
502 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
503 char *page;
504 unsigned long src = *ppos;
505 int ret = -ESRCH;
506 struct mm_struct *mm;
507
508 if (!task)
509 goto out_no_task;
510
511 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
512 goto out;
513
514 ret = -ENOMEM;
515 page = (char *)__get_free_page(GFP_TEMPORARY);
516 if (!page)
517 goto out;
518
519 ret = 0;
520
521 mm = get_task_mm(task);
522 if (!mm)
523 goto out_free;
524
525 ret = -EIO;
526
527 if (file->private_data != (void*)((long)current->self_exec_id))
528 goto out_put;
529
530 ret = 0;
531
532 while (count > 0) {
533 int this_len, retval;
534
535 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
536 retval = access_process_vm(task, src, page, this_len, 0);
537 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
538 if (!ret)
539 ret = -EIO;
540 break;
541 }
542
543 if (copy_to_user(buf, page, retval)) {
544 ret = -EFAULT;
545 break;
546 }
547
548 ret += retval;
549 src += retval;
550 buf += retval;
551 count -= retval;
552 }
553 *ppos = src;
554
555 out_put:
556 mmput(mm);
557 out_free:
558 free_page((unsigned long) page);
559 out:
560 put_task_struct(task);
561 out_no_task:
562 return ret;
563 }
564
565 #define mem_write NULL
566
567 #ifndef mem_write
568 /* This is a security hazard */
569 static ssize_t mem_write(struct file * file, const char __user *buf,
570 size_t count, loff_t *ppos)
571 {
572 int copied;
573 char *page;
574 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
575 unsigned long dst = *ppos;
576
577 copied = -ESRCH;
578 if (!task)
579 goto out_no_task;
580
581 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
582 goto out;
583
584 copied = -ENOMEM;
585 page = (char *)__get_free_page(GFP_TEMPORARY);
586 if (!page)
587 goto out;
588
589 copied = 0;
590 while (count > 0) {
591 int this_len, retval;
592
593 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
594 if (copy_from_user(page, buf, this_len)) {
595 copied = -EFAULT;
596 break;
597 }
598 retval = access_process_vm(task, dst, page, this_len, 1);
599 if (!retval) {
600 if (!copied)
601 copied = -EIO;
602 break;
603 }
604 copied += retval;
605 buf += retval;
606 dst += retval;
607 count -= retval;
608 }
609 *ppos = dst;
610 free_page((unsigned long) page);
611 out:
612 put_task_struct(task);
613 out_no_task:
614 return copied;
615 }
616 #endif
617
618 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
619 {
620 switch (orig) {
621 case 0:
622 file->f_pos = offset;
623 break;
624 case 1:
625 file->f_pos += offset;
626 break;
627 default:
628 return -EINVAL;
629 }
630 force_successful_syscall_return();
631 return file->f_pos;
632 }
633
634 static const struct file_operations proc_mem_operations = {
635 .llseek = mem_lseek,
636 .read = mem_read,
637 .write = mem_write,
638 .open = mem_open,
639 };
640
641 static ssize_t environ_read(struct file *file, char __user *buf,
642 size_t count, loff_t *ppos)
643 {
644 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
645 char *page;
646 unsigned long src = *ppos;
647 int ret = -ESRCH;
648 struct mm_struct *mm;
649
650 if (!task)
651 goto out_no_task;
652
653 if (!ptrace_may_attach(task))
654 goto out;
655
656 ret = -ENOMEM;
657 page = (char *)__get_free_page(GFP_TEMPORARY);
658 if (!page)
659 goto out;
660
661 ret = 0;
662
663 mm = get_task_mm(task);
664 if (!mm)
665 goto out_free;
666
667 while (count > 0) {
668 int this_len, retval, max_len;
669
670 this_len = mm->env_end - (mm->env_start + src);
671
672 if (this_len <= 0)
673 break;
674
675 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
676 this_len = (this_len > max_len) ? max_len : this_len;
677
678 retval = access_process_vm(task, (mm->env_start + src),
679 page, this_len, 0);
680
681 if (retval <= 0) {
682 ret = retval;
683 break;
684 }
685
686 if (copy_to_user(buf, page, retval)) {
687 ret = -EFAULT;
688 break;
689 }
690
691 ret += retval;
692 src += retval;
693 buf += retval;
694 count -= retval;
695 }
696 *ppos = src;
697
698 mmput(mm);
699 out_free:
700 free_page((unsigned long) page);
701 out:
702 put_task_struct(task);
703 out_no_task:
704 return ret;
705 }
706
707 static const struct file_operations proc_environ_operations = {
708 .read = environ_read,
709 };
710
711 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
712 size_t count, loff_t *ppos)
713 {
714 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
715 char buffer[PROC_NUMBUF];
716 size_t len;
717 int oom_adjust;
718
719 if (!task)
720 return -ESRCH;
721 oom_adjust = task->oomkilladj;
722 put_task_struct(task);
723
724 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
725
726 return simple_read_from_buffer(buf, count, ppos, buffer, len);
727 }
728
729 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
730 size_t count, loff_t *ppos)
731 {
732 struct task_struct *task;
733 char buffer[PROC_NUMBUF], *end;
734 int oom_adjust;
735
736 memset(buffer, 0, sizeof(buffer));
737 if (count > sizeof(buffer) - 1)
738 count = sizeof(buffer) - 1;
739 if (copy_from_user(buffer, buf, count))
740 return -EFAULT;
741 oom_adjust = simple_strtol(buffer, &end, 0);
742 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
743 oom_adjust != OOM_DISABLE)
744 return -EINVAL;
745 if (*end == '\n')
746 end++;
747 task = get_proc_task(file->f_path.dentry->d_inode);
748 if (!task)
749 return -ESRCH;
750 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
751 put_task_struct(task);
752 return -EACCES;
753 }
754 task->oomkilladj = oom_adjust;
755 put_task_struct(task);
756 if (end - buffer == 0)
757 return -EIO;
758 return end - buffer;
759 }
760
761 static const struct file_operations proc_oom_adjust_operations = {
762 .read = oom_adjust_read,
763 .write = oom_adjust_write,
764 };
765
766 #ifdef CONFIG_MMU
767 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
768 size_t count, loff_t *ppos)
769 {
770 struct task_struct *task;
771 char buffer[PROC_NUMBUF], *end;
772 struct mm_struct *mm;
773
774 memset(buffer, 0, sizeof(buffer));
775 if (count > sizeof(buffer) - 1)
776 count = sizeof(buffer) - 1;
777 if (copy_from_user(buffer, buf, count))
778 return -EFAULT;
779 if (!simple_strtol(buffer, &end, 0))
780 return -EINVAL;
781 if (*end == '\n')
782 end++;
783 task = get_proc_task(file->f_path.dentry->d_inode);
784 if (!task)
785 return -ESRCH;
786 mm = get_task_mm(task);
787 if (mm) {
788 clear_refs_smap(mm);
789 mmput(mm);
790 }
791 put_task_struct(task);
792 if (end - buffer == 0)
793 return -EIO;
794 return end - buffer;
795 }
796
797 static struct file_operations proc_clear_refs_operations = {
798 .write = clear_refs_write,
799 };
800 #endif
801
802 #ifdef CONFIG_AUDITSYSCALL
803 #define TMPBUFLEN 21
804 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
805 size_t count, loff_t *ppos)
806 {
807 struct inode * inode = file->f_path.dentry->d_inode;
808 struct task_struct *task = get_proc_task(inode);
809 ssize_t length;
810 char tmpbuf[TMPBUFLEN];
811
812 if (!task)
813 return -ESRCH;
814 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
815 audit_get_loginuid(task->audit_context));
816 put_task_struct(task);
817 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
818 }
819
820 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
821 size_t count, loff_t *ppos)
822 {
823 struct inode * inode = file->f_path.dentry->d_inode;
824 char *page, *tmp;
825 ssize_t length;
826 uid_t loginuid;
827
828 if (!capable(CAP_AUDIT_CONTROL))
829 return -EPERM;
830
831 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
832 return -EPERM;
833
834 if (count >= PAGE_SIZE)
835 count = PAGE_SIZE - 1;
836
837 if (*ppos != 0) {
838 /* No partial writes. */
839 return -EINVAL;
840 }
841 page = (char*)__get_free_page(GFP_TEMPORARY);
842 if (!page)
843 return -ENOMEM;
844 length = -EFAULT;
845 if (copy_from_user(page, buf, count))
846 goto out_free_page;
847
848 page[count] = '\0';
849 loginuid = simple_strtoul(page, &tmp, 10);
850 if (tmp == page) {
851 length = -EINVAL;
852 goto out_free_page;
853
854 }
855 length = audit_set_loginuid(current, loginuid);
856 if (likely(length == 0))
857 length = count;
858
859 out_free_page:
860 free_page((unsigned long) page);
861 return length;
862 }
863
864 static const struct file_operations proc_loginuid_operations = {
865 .read = proc_loginuid_read,
866 .write = proc_loginuid_write,
867 };
868 #endif
869
870 #ifdef CONFIG_FAULT_INJECTION
871 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
872 size_t count, loff_t *ppos)
873 {
874 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
875 char buffer[PROC_NUMBUF];
876 size_t len;
877 int make_it_fail;
878
879 if (!task)
880 return -ESRCH;
881 make_it_fail = task->make_it_fail;
882 put_task_struct(task);
883
884 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
885
886 return simple_read_from_buffer(buf, count, ppos, buffer, len);
887 }
888
889 static ssize_t proc_fault_inject_write(struct file * file,
890 const char __user * buf, size_t count, loff_t *ppos)
891 {
892 struct task_struct *task;
893 char buffer[PROC_NUMBUF], *end;
894 int make_it_fail;
895
896 if (!capable(CAP_SYS_RESOURCE))
897 return -EPERM;
898 memset(buffer, 0, sizeof(buffer));
899 if (count > sizeof(buffer) - 1)
900 count = sizeof(buffer) - 1;
901 if (copy_from_user(buffer, buf, count))
902 return -EFAULT;
903 make_it_fail = simple_strtol(buffer, &end, 0);
904 if (*end == '\n')
905 end++;
906 task = get_proc_task(file->f_dentry->d_inode);
907 if (!task)
908 return -ESRCH;
909 task->make_it_fail = make_it_fail;
910 put_task_struct(task);
911 if (end - buffer == 0)
912 return -EIO;
913 return end - buffer;
914 }
915
916 static const struct file_operations proc_fault_inject_operations = {
917 .read = proc_fault_inject_read,
918 .write = proc_fault_inject_write,
919 };
920 #endif
921
922 #ifdef CONFIG_SCHED_DEBUG
923 /*
924 * Print out various scheduling related per-task fields:
925 */
926 static int sched_show(struct seq_file *m, void *v)
927 {
928 struct inode *inode = m->private;
929 struct task_struct *p;
930
931 WARN_ON(!inode);
932
933 p = get_proc_task(inode);
934 if (!p)
935 return -ESRCH;
936 proc_sched_show_task(p, m);
937
938 put_task_struct(p);
939
940 return 0;
941 }
942
943 static ssize_t
944 sched_write(struct file *file, const char __user *buf,
945 size_t count, loff_t *offset)
946 {
947 struct inode *inode = file->f_path.dentry->d_inode;
948 struct task_struct *p;
949
950 WARN_ON(!inode);
951
952 p = get_proc_task(inode);
953 if (!p)
954 return -ESRCH;
955 proc_sched_set_task(p);
956
957 put_task_struct(p);
958
959 return count;
960 }
961
962 static int sched_open(struct inode *inode, struct file *filp)
963 {
964 int ret;
965
966 ret = single_open(filp, sched_show, NULL);
967 if (!ret) {
968 struct seq_file *m = filp->private_data;
969
970 m->private = inode;
971 }
972 return ret;
973 }
974
975 static const struct file_operations proc_pid_sched_operations = {
976 .open = sched_open,
977 .read = seq_read,
978 .write = sched_write,
979 .llseek = seq_lseek,
980 .release = single_release,
981 };
982
983 #endif
984
985 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
986 {
987 struct inode *inode = dentry->d_inode;
988 int error = -EACCES;
989
990 /* We don't need a base pointer in the /proc filesystem */
991 path_release(nd);
992
993 /* Are we allowed to snoop on the tasks file descriptors? */
994 if (!proc_fd_access_allowed(inode))
995 goto out;
996
997 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
998 nd->last_type = LAST_BIND;
999 out:
1000 return ERR_PTR(error);
1001 }
1002
1003 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1004 char __user *buffer, int buflen)
1005 {
1006 struct inode * inode;
1007 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1008 char *path;
1009 int len;
1010
1011 if (!tmp)
1012 return -ENOMEM;
1013
1014 inode = dentry->d_inode;
1015 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1016 len = PTR_ERR(path);
1017 if (IS_ERR(path))
1018 goto out;
1019 len = tmp + PAGE_SIZE - 1 - path;
1020
1021 if (len > buflen)
1022 len = buflen;
1023 if (copy_to_user(buffer, path, len))
1024 len = -EFAULT;
1025 out:
1026 free_page((unsigned long)tmp);
1027 return len;
1028 }
1029
1030 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1031 {
1032 int error = -EACCES;
1033 struct inode *inode = dentry->d_inode;
1034 struct dentry *de;
1035 struct vfsmount *mnt = NULL;
1036
1037 /* Are we allowed to snoop on the tasks file descriptors? */
1038 if (!proc_fd_access_allowed(inode))
1039 goto out;
1040
1041 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1042 if (error)
1043 goto out;
1044
1045 error = do_proc_readlink(de, mnt, buffer, buflen);
1046 dput(de);
1047 mntput(mnt);
1048 out:
1049 return error;
1050 }
1051
1052 static const struct inode_operations proc_pid_link_inode_operations = {
1053 .readlink = proc_pid_readlink,
1054 .follow_link = proc_pid_follow_link,
1055 .setattr = proc_setattr,
1056 };
1057
1058
1059 /* building an inode */
1060
1061 static int task_dumpable(struct task_struct *task)
1062 {
1063 int dumpable = 0;
1064 struct mm_struct *mm;
1065
1066 task_lock(task);
1067 mm = task->mm;
1068 if (mm)
1069 dumpable = get_dumpable(mm);
1070 task_unlock(task);
1071 if(dumpable == 1)
1072 return 1;
1073 return 0;
1074 }
1075
1076
1077 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1078 {
1079 struct inode * inode;
1080 struct proc_inode *ei;
1081
1082 /* We need a new inode */
1083
1084 inode = new_inode(sb);
1085 if (!inode)
1086 goto out;
1087
1088 /* Common stuff */
1089 ei = PROC_I(inode);
1090 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1091 inode->i_op = &proc_def_inode_operations;
1092
1093 /*
1094 * grab the reference to task.
1095 */
1096 ei->pid = get_task_pid(task, PIDTYPE_PID);
1097 if (!ei->pid)
1098 goto out_unlock;
1099
1100 inode->i_uid = 0;
1101 inode->i_gid = 0;
1102 if (task_dumpable(task)) {
1103 inode->i_uid = task->euid;
1104 inode->i_gid = task->egid;
1105 }
1106 security_task_to_inode(task, inode);
1107
1108 out:
1109 return inode;
1110
1111 out_unlock:
1112 iput(inode);
1113 return NULL;
1114 }
1115
1116 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1117 {
1118 struct inode *inode = dentry->d_inode;
1119 struct task_struct *task;
1120 generic_fillattr(inode, stat);
1121
1122 rcu_read_lock();
1123 stat->uid = 0;
1124 stat->gid = 0;
1125 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1126 if (task) {
1127 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1128 task_dumpable(task)) {
1129 stat->uid = task->euid;
1130 stat->gid = task->egid;
1131 }
1132 }
1133 rcu_read_unlock();
1134 return 0;
1135 }
1136
1137 /* dentry stuff */
1138
1139 /*
1140 * Exceptional case: normally we are not allowed to unhash a busy
1141 * directory. In this case, however, we can do it - no aliasing problems
1142 * due to the way we treat inodes.
1143 *
1144 * Rewrite the inode's ownerships here because the owning task may have
1145 * performed a setuid(), etc.
1146 *
1147 * Before the /proc/pid/status file was created the only way to read
1148 * the effective uid of a /process was to stat /proc/pid. Reading
1149 * /proc/pid/status is slow enough that procps and other packages
1150 * kept stating /proc/pid. To keep the rules in /proc simple I have
1151 * made this apply to all per process world readable and executable
1152 * directories.
1153 */
1154 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1155 {
1156 struct inode *inode = dentry->d_inode;
1157 struct task_struct *task = get_proc_task(inode);
1158 if (task) {
1159 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1160 task_dumpable(task)) {
1161 inode->i_uid = task->euid;
1162 inode->i_gid = task->egid;
1163 } else {
1164 inode->i_uid = 0;
1165 inode->i_gid = 0;
1166 }
1167 inode->i_mode &= ~(S_ISUID | S_ISGID);
1168 security_task_to_inode(task, inode);
1169 put_task_struct(task);
1170 return 1;
1171 }
1172 d_drop(dentry);
1173 return 0;
1174 }
1175
1176 static int pid_delete_dentry(struct dentry * dentry)
1177 {
1178 /* Is the task we represent dead?
1179 * If so, then don't put the dentry on the lru list,
1180 * kill it immediately.
1181 */
1182 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1183 }
1184
1185 static struct dentry_operations pid_dentry_operations =
1186 {
1187 .d_revalidate = pid_revalidate,
1188 .d_delete = pid_delete_dentry,
1189 };
1190
1191 /* Lookups */
1192
1193 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1194 struct task_struct *, const void *);
1195
1196 /*
1197 * Fill a directory entry.
1198 *
1199 * If possible create the dcache entry and derive our inode number and
1200 * file type from dcache entry.
1201 *
1202 * Since all of the proc inode numbers are dynamically generated, the inode
1203 * numbers do not exist until the inode is cache. This means creating the
1204 * the dcache entry in readdir is necessary to keep the inode numbers
1205 * reported by readdir in sync with the inode numbers reported
1206 * by stat.
1207 */
1208 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1209 char *name, int len,
1210 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1211 {
1212 struct dentry *child, *dir = filp->f_path.dentry;
1213 struct inode *inode;
1214 struct qstr qname;
1215 ino_t ino = 0;
1216 unsigned type = DT_UNKNOWN;
1217
1218 qname.name = name;
1219 qname.len = len;
1220 qname.hash = full_name_hash(name, len);
1221
1222 child = d_lookup(dir, &qname);
1223 if (!child) {
1224 struct dentry *new;
1225 new = d_alloc(dir, &qname);
1226 if (new) {
1227 child = instantiate(dir->d_inode, new, task, ptr);
1228 if (child)
1229 dput(new);
1230 else
1231 child = new;
1232 }
1233 }
1234 if (!child || IS_ERR(child) || !child->d_inode)
1235 goto end_instantiate;
1236 inode = child->d_inode;
1237 if (inode) {
1238 ino = inode->i_ino;
1239 type = inode->i_mode >> 12;
1240 }
1241 dput(child);
1242 end_instantiate:
1243 if (!ino)
1244 ino = find_inode_number(dir, &qname);
1245 if (!ino)
1246 ino = 1;
1247 return filldir(dirent, name, len, filp->f_pos, ino, type);
1248 }
1249
1250 static unsigned name_to_int(struct dentry *dentry)
1251 {
1252 const char *name = dentry->d_name.name;
1253 int len = dentry->d_name.len;
1254 unsigned n = 0;
1255
1256 if (len > 1 && *name == '0')
1257 goto out;
1258 while (len-- > 0) {
1259 unsigned c = *name++ - '0';
1260 if (c > 9)
1261 goto out;
1262 if (n >= (~0U-9)/10)
1263 goto out;
1264 n *= 10;
1265 n += c;
1266 }
1267 return n;
1268 out:
1269 return ~0U;
1270 }
1271
1272 #define PROC_FDINFO_MAX 64
1273
1274 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1275 struct vfsmount **mnt, char *info)
1276 {
1277 struct task_struct *task = get_proc_task(inode);
1278 struct files_struct *files = NULL;
1279 struct file *file;
1280 int fd = proc_fd(inode);
1281
1282 if (task) {
1283 files = get_files_struct(task);
1284 put_task_struct(task);
1285 }
1286 if (files) {
1287 /*
1288 * We are not taking a ref to the file structure, so we must
1289 * hold ->file_lock.
1290 */
1291 spin_lock(&files->file_lock);
1292 file = fcheck_files(files, fd);
1293 if (file) {
1294 if (mnt)
1295 *mnt = mntget(file->f_path.mnt);
1296 if (dentry)
1297 *dentry = dget(file->f_path.dentry);
1298 if (info)
1299 snprintf(info, PROC_FDINFO_MAX,
1300 "pos:\t%lli\n"
1301 "flags:\t0%o\n",
1302 (long long) file->f_pos,
1303 file->f_flags);
1304 spin_unlock(&files->file_lock);
1305 put_files_struct(files);
1306 return 0;
1307 }
1308 spin_unlock(&files->file_lock);
1309 put_files_struct(files);
1310 }
1311 return -ENOENT;
1312 }
1313
1314 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1315 struct vfsmount **mnt)
1316 {
1317 return proc_fd_info(inode, dentry, mnt, NULL);
1318 }
1319
1320 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1321 {
1322 struct inode *inode = dentry->d_inode;
1323 struct task_struct *task = get_proc_task(inode);
1324 int fd = proc_fd(inode);
1325 struct files_struct *files;
1326
1327 if (task) {
1328 files = get_files_struct(task);
1329 if (files) {
1330 rcu_read_lock();
1331 if (fcheck_files(files, fd)) {
1332 rcu_read_unlock();
1333 put_files_struct(files);
1334 if (task_dumpable(task)) {
1335 inode->i_uid = task->euid;
1336 inode->i_gid = task->egid;
1337 } else {
1338 inode->i_uid = 0;
1339 inode->i_gid = 0;
1340 }
1341 inode->i_mode &= ~(S_ISUID | S_ISGID);
1342 security_task_to_inode(task, inode);
1343 put_task_struct(task);
1344 return 1;
1345 }
1346 rcu_read_unlock();
1347 put_files_struct(files);
1348 }
1349 put_task_struct(task);
1350 }
1351 d_drop(dentry);
1352 return 0;
1353 }
1354
1355 static struct dentry_operations tid_fd_dentry_operations =
1356 {
1357 .d_revalidate = tid_fd_revalidate,
1358 .d_delete = pid_delete_dentry,
1359 };
1360
1361 static struct dentry *proc_fd_instantiate(struct inode *dir,
1362 struct dentry *dentry, struct task_struct *task, const void *ptr)
1363 {
1364 unsigned fd = *(const unsigned *)ptr;
1365 struct file *file;
1366 struct files_struct *files;
1367 struct inode *inode;
1368 struct proc_inode *ei;
1369 struct dentry *error = ERR_PTR(-ENOENT);
1370
1371 inode = proc_pid_make_inode(dir->i_sb, task);
1372 if (!inode)
1373 goto out;
1374 ei = PROC_I(inode);
1375 ei->fd = fd;
1376 files = get_files_struct(task);
1377 if (!files)
1378 goto out_iput;
1379 inode->i_mode = S_IFLNK;
1380
1381 /*
1382 * We are not taking a ref to the file structure, so we must
1383 * hold ->file_lock.
1384 */
1385 spin_lock(&files->file_lock);
1386 file = fcheck_files(files, fd);
1387 if (!file)
1388 goto out_unlock;
1389 if (file->f_mode & 1)
1390 inode->i_mode |= S_IRUSR | S_IXUSR;
1391 if (file->f_mode & 2)
1392 inode->i_mode |= S_IWUSR | S_IXUSR;
1393 spin_unlock(&files->file_lock);
1394 put_files_struct(files);
1395
1396 inode->i_op = &proc_pid_link_inode_operations;
1397 inode->i_size = 64;
1398 ei->op.proc_get_link = proc_fd_link;
1399 dentry->d_op = &tid_fd_dentry_operations;
1400 d_add(dentry, inode);
1401 /* Close the race of the process dying before we return the dentry */
1402 if (tid_fd_revalidate(dentry, NULL))
1403 error = NULL;
1404
1405 out:
1406 return error;
1407 out_unlock:
1408 spin_unlock(&files->file_lock);
1409 put_files_struct(files);
1410 out_iput:
1411 iput(inode);
1412 goto out;
1413 }
1414
1415 static struct dentry *proc_lookupfd_common(struct inode *dir,
1416 struct dentry *dentry,
1417 instantiate_t instantiate)
1418 {
1419 struct task_struct *task = get_proc_task(dir);
1420 unsigned fd = name_to_int(dentry);
1421 struct dentry *result = ERR_PTR(-ENOENT);
1422
1423 if (!task)
1424 goto out_no_task;
1425 if (fd == ~0U)
1426 goto out;
1427
1428 result = instantiate(dir, dentry, task, &fd);
1429 out:
1430 put_task_struct(task);
1431 out_no_task:
1432 return result;
1433 }
1434
1435 static int proc_readfd_common(struct file * filp, void * dirent,
1436 filldir_t filldir, instantiate_t instantiate)
1437 {
1438 struct dentry *dentry = filp->f_path.dentry;
1439 struct inode *inode = dentry->d_inode;
1440 struct task_struct *p = get_proc_task(inode);
1441 unsigned int fd, tid, ino;
1442 int retval;
1443 struct files_struct * files;
1444 struct fdtable *fdt;
1445
1446 retval = -ENOENT;
1447 if (!p)
1448 goto out_no_task;
1449 retval = 0;
1450 tid = p->pid;
1451
1452 fd = filp->f_pos;
1453 switch (fd) {
1454 case 0:
1455 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1456 goto out;
1457 filp->f_pos++;
1458 case 1:
1459 ino = parent_ino(dentry);
1460 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1461 goto out;
1462 filp->f_pos++;
1463 default:
1464 files = get_files_struct(p);
1465 if (!files)
1466 goto out;
1467 rcu_read_lock();
1468 fdt = files_fdtable(files);
1469 for (fd = filp->f_pos-2;
1470 fd < fdt->max_fds;
1471 fd++, filp->f_pos++) {
1472 char name[PROC_NUMBUF];
1473 int len;
1474
1475 if (!fcheck_files(files, fd))
1476 continue;
1477 rcu_read_unlock();
1478
1479 len = snprintf(name, sizeof(name), "%d", fd);
1480 if (proc_fill_cache(filp, dirent, filldir,
1481 name, len, instantiate,
1482 p, &fd) < 0) {
1483 rcu_read_lock();
1484 break;
1485 }
1486 rcu_read_lock();
1487 }
1488 rcu_read_unlock();
1489 put_files_struct(files);
1490 }
1491 out:
1492 put_task_struct(p);
1493 out_no_task:
1494 return retval;
1495 }
1496
1497 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1498 struct nameidata *nd)
1499 {
1500 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1501 }
1502
1503 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1504 {
1505 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1506 }
1507
1508 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1509 size_t len, loff_t *ppos)
1510 {
1511 char tmp[PROC_FDINFO_MAX];
1512 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1513 if (!err)
1514 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1515 return err;
1516 }
1517
1518 static const struct file_operations proc_fdinfo_file_operations = {
1519 .open = nonseekable_open,
1520 .read = proc_fdinfo_read,
1521 };
1522
1523 static const struct file_operations proc_fd_operations = {
1524 .read = generic_read_dir,
1525 .readdir = proc_readfd,
1526 };
1527
1528 /*
1529 * /proc/pid/fd needs a special permission handler so that a process can still
1530 * access /proc/self/fd after it has executed a setuid().
1531 */
1532 static int proc_fd_permission(struct inode *inode, int mask,
1533 struct nameidata *nd)
1534 {
1535 int rv;
1536
1537 rv = generic_permission(inode, mask, NULL);
1538 if (rv == 0)
1539 return 0;
1540 if (task_pid(current) == proc_pid(inode))
1541 rv = 0;
1542 return rv;
1543 }
1544
1545 /*
1546 * proc directories can do almost nothing..
1547 */
1548 static const struct inode_operations proc_fd_inode_operations = {
1549 .lookup = proc_lookupfd,
1550 .permission = proc_fd_permission,
1551 .setattr = proc_setattr,
1552 };
1553
1554 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1555 struct dentry *dentry, struct task_struct *task, const void *ptr)
1556 {
1557 unsigned fd = *(unsigned *)ptr;
1558 struct inode *inode;
1559 struct proc_inode *ei;
1560 struct dentry *error = ERR_PTR(-ENOENT);
1561
1562 inode = proc_pid_make_inode(dir->i_sb, task);
1563 if (!inode)
1564 goto out;
1565 ei = PROC_I(inode);
1566 ei->fd = fd;
1567 inode->i_mode = S_IFREG | S_IRUSR;
1568 inode->i_fop = &proc_fdinfo_file_operations;
1569 dentry->d_op = &tid_fd_dentry_operations;
1570 d_add(dentry, inode);
1571 /* Close the race of the process dying before we return the dentry */
1572 if (tid_fd_revalidate(dentry, NULL))
1573 error = NULL;
1574
1575 out:
1576 return error;
1577 }
1578
1579 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1580 struct dentry *dentry,
1581 struct nameidata *nd)
1582 {
1583 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1584 }
1585
1586 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1587 {
1588 return proc_readfd_common(filp, dirent, filldir,
1589 proc_fdinfo_instantiate);
1590 }
1591
1592 static const struct file_operations proc_fdinfo_operations = {
1593 .read = generic_read_dir,
1594 .readdir = proc_readfdinfo,
1595 };
1596
1597 /*
1598 * proc directories can do almost nothing..
1599 */
1600 static const struct inode_operations proc_fdinfo_inode_operations = {
1601 .lookup = proc_lookupfdinfo,
1602 .setattr = proc_setattr,
1603 };
1604
1605
1606 static struct dentry *proc_pident_instantiate(struct inode *dir,
1607 struct dentry *dentry, struct task_struct *task, const void *ptr)
1608 {
1609 const struct pid_entry *p = ptr;
1610 struct inode *inode;
1611 struct proc_inode *ei;
1612 struct dentry *error = ERR_PTR(-EINVAL);
1613
1614 inode = proc_pid_make_inode(dir->i_sb, task);
1615 if (!inode)
1616 goto out;
1617
1618 ei = PROC_I(inode);
1619 inode->i_mode = p->mode;
1620 if (S_ISDIR(inode->i_mode))
1621 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1622 if (p->iop)
1623 inode->i_op = p->iop;
1624 if (p->fop)
1625 inode->i_fop = p->fop;
1626 ei->op = p->op;
1627 dentry->d_op = &pid_dentry_operations;
1628 d_add(dentry, inode);
1629 /* Close the race of the process dying before we return the dentry */
1630 if (pid_revalidate(dentry, NULL))
1631 error = NULL;
1632 out:
1633 return error;
1634 }
1635
1636 static struct dentry *proc_pident_lookup(struct inode *dir,
1637 struct dentry *dentry,
1638 const struct pid_entry *ents,
1639 unsigned int nents)
1640 {
1641 struct inode *inode;
1642 struct dentry *error;
1643 struct task_struct *task = get_proc_task(dir);
1644 const struct pid_entry *p, *last;
1645
1646 error = ERR_PTR(-ENOENT);
1647 inode = NULL;
1648
1649 if (!task)
1650 goto out_no_task;
1651
1652 /*
1653 * Yes, it does not scale. And it should not. Don't add
1654 * new entries into /proc/<tgid>/ without very good reasons.
1655 */
1656 last = &ents[nents - 1];
1657 for (p = ents; p <= last; p++) {
1658 if (p->len != dentry->d_name.len)
1659 continue;
1660 if (!memcmp(dentry->d_name.name, p->name, p->len))
1661 break;
1662 }
1663 if (p > last)
1664 goto out;
1665
1666 error = proc_pident_instantiate(dir, dentry, task, p);
1667 out:
1668 put_task_struct(task);
1669 out_no_task:
1670 return error;
1671 }
1672
1673 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1674 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1675 {
1676 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1677 proc_pident_instantiate, task, p);
1678 }
1679
1680 static int proc_pident_readdir(struct file *filp,
1681 void *dirent, filldir_t filldir,
1682 const struct pid_entry *ents, unsigned int nents)
1683 {
1684 int i;
1685 int pid;
1686 struct dentry *dentry = filp->f_path.dentry;
1687 struct inode *inode = dentry->d_inode;
1688 struct task_struct *task = get_proc_task(inode);
1689 const struct pid_entry *p, *last;
1690 ino_t ino;
1691 int ret;
1692
1693 ret = -ENOENT;
1694 if (!task)
1695 goto out_no_task;
1696
1697 ret = 0;
1698 pid = task->pid;
1699 i = filp->f_pos;
1700 switch (i) {
1701 case 0:
1702 ino = inode->i_ino;
1703 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1704 goto out;
1705 i++;
1706 filp->f_pos++;
1707 /* fall through */
1708 case 1:
1709 ino = parent_ino(dentry);
1710 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1711 goto out;
1712 i++;
1713 filp->f_pos++;
1714 /* fall through */
1715 default:
1716 i -= 2;
1717 if (i >= nents) {
1718 ret = 1;
1719 goto out;
1720 }
1721 p = ents + i;
1722 last = &ents[nents - 1];
1723 while (p <= last) {
1724 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1725 goto out;
1726 filp->f_pos++;
1727 p++;
1728 }
1729 }
1730
1731 ret = 1;
1732 out:
1733 put_task_struct(task);
1734 out_no_task:
1735 return ret;
1736 }
1737
1738 #ifdef CONFIG_SECURITY
1739 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1740 size_t count, loff_t *ppos)
1741 {
1742 struct inode * inode = file->f_path.dentry->d_inode;
1743 char *p = NULL;
1744 ssize_t length;
1745 struct task_struct *task = get_proc_task(inode);
1746
1747 if (!task)
1748 return -ESRCH;
1749
1750 length = security_getprocattr(task,
1751 (char*)file->f_path.dentry->d_name.name,
1752 &p);
1753 put_task_struct(task);
1754 if (length > 0)
1755 length = simple_read_from_buffer(buf, count, ppos, p, length);
1756 kfree(p);
1757 return length;
1758 }
1759
1760 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1761 size_t count, loff_t *ppos)
1762 {
1763 struct inode * inode = file->f_path.dentry->d_inode;
1764 char *page;
1765 ssize_t length;
1766 struct task_struct *task = get_proc_task(inode);
1767
1768 length = -ESRCH;
1769 if (!task)
1770 goto out_no_task;
1771 if (count > PAGE_SIZE)
1772 count = PAGE_SIZE;
1773
1774 /* No partial writes. */
1775 length = -EINVAL;
1776 if (*ppos != 0)
1777 goto out;
1778
1779 length = -ENOMEM;
1780 page = (char*)__get_free_page(GFP_TEMPORARY);
1781 if (!page)
1782 goto out;
1783
1784 length = -EFAULT;
1785 if (copy_from_user(page, buf, count))
1786 goto out_free;
1787
1788 length = security_setprocattr(task,
1789 (char*)file->f_path.dentry->d_name.name,
1790 (void*)page, count);
1791 out_free:
1792 free_page((unsigned long) page);
1793 out:
1794 put_task_struct(task);
1795 out_no_task:
1796 return length;
1797 }
1798
1799 static const struct file_operations proc_pid_attr_operations = {
1800 .read = proc_pid_attr_read,
1801 .write = proc_pid_attr_write,
1802 };
1803
1804 static const struct pid_entry attr_dir_stuff[] = {
1805 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1806 REG("prev", S_IRUGO, pid_attr),
1807 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1808 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1809 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1810 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1811 };
1812
1813 static int proc_attr_dir_readdir(struct file * filp,
1814 void * dirent, filldir_t filldir)
1815 {
1816 return proc_pident_readdir(filp,dirent,filldir,
1817 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1818 }
1819
1820 static const struct file_operations proc_attr_dir_operations = {
1821 .read = generic_read_dir,
1822 .readdir = proc_attr_dir_readdir,
1823 };
1824
1825 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1826 struct dentry *dentry, struct nameidata *nd)
1827 {
1828 return proc_pident_lookup(dir, dentry,
1829 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1830 }
1831
1832 static const struct inode_operations proc_attr_dir_inode_operations = {
1833 .lookup = proc_attr_dir_lookup,
1834 .getattr = pid_getattr,
1835 .setattr = proc_setattr,
1836 };
1837
1838 #endif
1839
1840 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1841 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
1842 size_t count, loff_t *ppos)
1843 {
1844 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1845 struct mm_struct *mm;
1846 char buffer[PROC_NUMBUF];
1847 size_t len;
1848 int ret;
1849
1850 if (!task)
1851 return -ESRCH;
1852
1853 ret = 0;
1854 mm = get_task_mm(task);
1855 if (mm) {
1856 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
1857 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
1858 MMF_DUMP_FILTER_SHIFT));
1859 mmput(mm);
1860 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
1861 }
1862
1863 put_task_struct(task);
1864
1865 return ret;
1866 }
1867
1868 static ssize_t proc_coredump_filter_write(struct file *file,
1869 const char __user *buf,
1870 size_t count,
1871 loff_t *ppos)
1872 {
1873 struct task_struct *task;
1874 struct mm_struct *mm;
1875 char buffer[PROC_NUMBUF], *end;
1876 unsigned int val;
1877 int ret;
1878 int i;
1879 unsigned long mask;
1880
1881 ret = -EFAULT;
1882 memset(buffer, 0, sizeof(buffer));
1883 if (count > sizeof(buffer) - 1)
1884 count = sizeof(buffer) - 1;
1885 if (copy_from_user(buffer, buf, count))
1886 goto out_no_task;
1887
1888 ret = -EINVAL;
1889 val = (unsigned int)simple_strtoul(buffer, &end, 0);
1890 if (*end == '\n')
1891 end++;
1892 if (end - buffer == 0)
1893 goto out_no_task;
1894
1895 ret = -ESRCH;
1896 task = get_proc_task(file->f_dentry->d_inode);
1897 if (!task)
1898 goto out_no_task;
1899
1900 ret = end - buffer;
1901 mm = get_task_mm(task);
1902 if (!mm)
1903 goto out_no_mm;
1904
1905 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
1906 if (val & mask)
1907 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1908 else
1909 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1910 }
1911
1912 mmput(mm);
1913 out_no_mm:
1914 put_task_struct(task);
1915 out_no_task:
1916 return ret;
1917 }
1918
1919 static const struct file_operations proc_coredump_filter_operations = {
1920 .read = proc_coredump_filter_read,
1921 .write = proc_coredump_filter_write,
1922 };
1923 #endif
1924
1925 /*
1926 * /proc/self:
1927 */
1928 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1929 int buflen)
1930 {
1931 char tmp[PROC_NUMBUF];
1932 sprintf(tmp, "%d", current->tgid);
1933 return vfs_readlink(dentry,buffer,buflen,tmp);
1934 }
1935
1936 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1937 {
1938 char tmp[PROC_NUMBUF];
1939 sprintf(tmp, "%d", current->tgid);
1940 return ERR_PTR(vfs_follow_link(nd,tmp));
1941 }
1942
1943 static const struct inode_operations proc_self_inode_operations = {
1944 .readlink = proc_self_readlink,
1945 .follow_link = proc_self_follow_link,
1946 };
1947
1948 /*
1949 * proc base
1950 *
1951 * These are the directory entries in the root directory of /proc
1952 * that properly belong to the /proc filesystem, as they describe
1953 * describe something that is process related.
1954 */
1955 static const struct pid_entry proc_base_stuff[] = {
1956 NOD("self", S_IFLNK|S_IRWXUGO,
1957 &proc_self_inode_operations, NULL, {}),
1958 };
1959
1960 /*
1961 * Exceptional case: normally we are not allowed to unhash a busy
1962 * directory. In this case, however, we can do it - no aliasing problems
1963 * due to the way we treat inodes.
1964 */
1965 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1966 {
1967 struct inode *inode = dentry->d_inode;
1968 struct task_struct *task = get_proc_task(inode);
1969 if (task) {
1970 put_task_struct(task);
1971 return 1;
1972 }
1973 d_drop(dentry);
1974 return 0;
1975 }
1976
1977 static struct dentry_operations proc_base_dentry_operations =
1978 {
1979 .d_revalidate = proc_base_revalidate,
1980 .d_delete = pid_delete_dentry,
1981 };
1982
1983 static struct dentry *proc_base_instantiate(struct inode *dir,
1984 struct dentry *dentry, struct task_struct *task, const void *ptr)
1985 {
1986 const struct pid_entry *p = ptr;
1987 struct inode *inode;
1988 struct proc_inode *ei;
1989 struct dentry *error = ERR_PTR(-EINVAL);
1990
1991 /* Allocate the inode */
1992 error = ERR_PTR(-ENOMEM);
1993 inode = new_inode(dir->i_sb);
1994 if (!inode)
1995 goto out;
1996
1997 /* Initialize the inode */
1998 ei = PROC_I(inode);
1999 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2000
2001 /*
2002 * grab the reference to the task.
2003 */
2004 ei->pid = get_task_pid(task, PIDTYPE_PID);
2005 if (!ei->pid)
2006 goto out_iput;
2007
2008 inode->i_uid = 0;
2009 inode->i_gid = 0;
2010 inode->i_mode = p->mode;
2011 if (S_ISDIR(inode->i_mode))
2012 inode->i_nlink = 2;
2013 if (S_ISLNK(inode->i_mode))
2014 inode->i_size = 64;
2015 if (p->iop)
2016 inode->i_op = p->iop;
2017 if (p->fop)
2018 inode->i_fop = p->fop;
2019 ei->op = p->op;
2020 dentry->d_op = &proc_base_dentry_operations;
2021 d_add(dentry, inode);
2022 error = NULL;
2023 out:
2024 return error;
2025 out_iput:
2026 iput(inode);
2027 goto out;
2028 }
2029
2030 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2031 {
2032 struct dentry *error;
2033 struct task_struct *task = get_proc_task(dir);
2034 const struct pid_entry *p, *last;
2035
2036 error = ERR_PTR(-ENOENT);
2037
2038 if (!task)
2039 goto out_no_task;
2040
2041 /* Lookup the directory entry */
2042 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2043 for (p = proc_base_stuff; p <= last; p++) {
2044 if (p->len != dentry->d_name.len)
2045 continue;
2046 if (!memcmp(dentry->d_name.name, p->name, p->len))
2047 break;
2048 }
2049 if (p > last)
2050 goto out;
2051
2052 error = proc_base_instantiate(dir, dentry, task, p);
2053
2054 out:
2055 put_task_struct(task);
2056 out_no_task:
2057 return error;
2058 }
2059
2060 static int proc_base_fill_cache(struct file *filp, void *dirent,
2061 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2062 {
2063 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2064 proc_base_instantiate, task, p);
2065 }
2066
2067 #ifdef CONFIG_TASK_IO_ACCOUNTING
2068 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2069 {
2070 return sprintf(buffer,
2071 #ifdef CONFIG_TASK_XACCT
2072 "rchar: %llu\n"
2073 "wchar: %llu\n"
2074 "syscr: %llu\n"
2075 "syscw: %llu\n"
2076 #endif
2077 "read_bytes: %llu\n"
2078 "write_bytes: %llu\n"
2079 "cancelled_write_bytes: %llu\n",
2080 #ifdef CONFIG_TASK_XACCT
2081 (unsigned long long)task->rchar,
2082 (unsigned long long)task->wchar,
2083 (unsigned long long)task->syscr,
2084 (unsigned long long)task->syscw,
2085 #endif
2086 (unsigned long long)task->ioac.read_bytes,
2087 (unsigned long long)task->ioac.write_bytes,
2088 (unsigned long long)task->ioac.cancelled_write_bytes);
2089 }
2090 #endif
2091
2092 /*
2093 * Thread groups
2094 */
2095 static const struct file_operations proc_task_operations;
2096 static const struct inode_operations proc_task_inode_operations;
2097
2098 static const struct pid_entry tgid_base_stuff[] = {
2099 DIR("task", S_IRUGO|S_IXUGO, task),
2100 DIR("fd", S_IRUSR|S_IXUSR, fd),
2101 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2102 REG("environ", S_IRUSR, environ),
2103 INF("auxv", S_IRUSR, pid_auxv),
2104 INF("status", S_IRUGO, pid_status),
2105 #ifdef CONFIG_SCHED_DEBUG
2106 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2107 #endif
2108 INF("cmdline", S_IRUGO, pid_cmdline),
2109 INF("stat", S_IRUGO, tgid_stat),
2110 INF("statm", S_IRUGO, pid_statm),
2111 REG("maps", S_IRUGO, maps),
2112 #ifdef CONFIG_NUMA
2113 REG("numa_maps", S_IRUGO, numa_maps),
2114 #endif
2115 REG("mem", S_IRUSR|S_IWUSR, mem),
2116 LNK("cwd", cwd),
2117 LNK("root", root),
2118 LNK("exe", exe),
2119 REG("mounts", S_IRUGO, mounts),
2120 REG("mountstats", S_IRUSR, mountstats),
2121 #ifdef CONFIG_MMU
2122 REG("clear_refs", S_IWUSR, clear_refs),
2123 REG("smaps", S_IRUGO, smaps),
2124 #endif
2125 #ifdef CONFIG_SECURITY
2126 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2127 #endif
2128 #ifdef CONFIG_KALLSYMS
2129 INF("wchan", S_IRUGO, pid_wchan),
2130 #endif
2131 #ifdef CONFIG_SCHEDSTATS
2132 INF("schedstat", S_IRUGO, pid_schedstat),
2133 #endif
2134 #ifdef CONFIG_CPUSETS
2135 REG("cpuset", S_IRUGO, cpuset),
2136 #endif
2137 #ifdef CONFIG_CGROUPS
2138 REG("cgroup", S_IRUGO, cgroup),
2139 #endif
2140 INF("oom_score", S_IRUGO, oom_score),
2141 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2142 #ifdef CONFIG_AUDITSYSCALL
2143 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2144 #endif
2145 #ifdef CONFIG_FAULT_INJECTION
2146 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2147 #endif
2148 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2149 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2150 #endif
2151 #ifdef CONFIG_TASK_IO_ACCOUNTING
2152 INF("io", S_IRUGO, pid_io_accounting),
2153 #endif
2154 };
2155
2156 static int proc_tgid_base_readdir(struct file * filp,
2157 void * dirent, filldir_t filldir)
2158 {
2159 return proc_pident_readdir(filp,dirent,filldir,
2160 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2161 }
2162
2163 static const struct file_operations proc_tgid_base_operations = {
2164 .read = generic_read_dir,
2165 .readdir = proc_tgid_base_readdir,
2166 };
2167
2168 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2169 return proc_pident_lookup(dir, dentry,
2170 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2171 }
2172
2173 static const struct inode_operations proc_tgid_base_inode_operations = {
2174 .lookup = proc_tgid_base_lookup,
2175 .getattr = pid_getattr,
2176 .setattr = proc_setattr,
2177 };
2178
2179 /**
2180 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2181 *
2182 * @task: task that should be flushed.
2183 *
2184 * Looks in the dcache for
2185 * /proc/@pid
2186 * /proc/@tgid/task/@pid
2187 * if either directory is present flushes it and all of it'ts children
2188 * from the dcache.
2189 *
2190 * It is safe and reasonable to cache /proc entries for a task until
2191 * that task exits. After that they just clog up the dcache with
2192 * useless entries, possibly causing useful dcache entries to be
2193 * flushed instead. This routine is proved to flush those useless
2194 * dcache entries at process exit time.
2195 *
2196 * NOTE: This routine is just an optimization so it does not guarantee
2197 * that no dcache entries will exist at process exit time it
2198 * just makes it very unlikely that any will persist.
2199 */
2200 void proc_flush_task(struct task_struct *task)
2201 {
2202 struct dentry *dentry, *leader, *dir;
2203 char buf[PROC_NUMBUF];
2204 struct qstr name;
2205
2206 name.name = buf;
2207 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2208 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2209 if (dentry) {
2210 shrink_dcache_parent(dentry);
2211 d_drop(dentry);
2212 dput(dentry);
2213 }
2214
2215 if (thread_group_leader(task))
2216 goto out;
2217
2218 name.name = buf;
2219 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2220 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2221 if (!leader)
2222 goto out;
2223
2224 name.name = "task";
2225 name.len = strlen(name.name);
2226 dir = d_hash_and_lookup(leader, &name);
2227 if (!dir)
2228 goto out_put_leader;
2229
2230 name.name = buf;
2231 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2232 dentry = d_hash_and_lookup(dir, &name);
2233 if (dentry) {
2234 shrink_dcache_parent(dentry);
2235 d_drop(dentry);
2236 dput(dentry);
2237 }
2238
2239 dput(dir);
2240 out_put_leader:
2241 dput(leader);
2242 out:
2243 return;
2244 }
2245
2246 static struct dentry *proc_pid_instantiate(struct inode *dir,
2247 struct dentry * dentry,
2248 struct task_struct *task, const void *ptr)
2249 {
2250 struct dentry *error = ERR_PTR(-ENOENT);
2251 struct inode *inode;
2252
2253 inode = proc_pid_make_inode(dir->i_sb, task);
2254 if (!inode)
2255 goto out;
2256
2257 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2258 inode->i_op = &proc_tgid_base_inode_operations;
2259 inode->i_fop = &proc_tgid_base_operations;
2260 inode->i_flags|=S_IMMUTABLE;
2261 inode->i_nlink = 5;
2262 #ifdef CONFIG_SECURITY
2263 inode->i_nlink += 1;
2264 #endif
2265
2266 dentry->d_op = &pid_dentry_operations;
2267
2268 d_add(dentry, inode);
2269 /* Close the race of the process dying before we return the dentry */
2270 if (pid_revalidate(dentry, NULL))
2271 error = NULL;
2272 out:
2273 return error;
2274 }
2275
2276 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2277 {
2278 struct dentry *result = ERR_PTR(-ENOENT);
2279 struct task_struct *task;
2280 unsigned tgid;
2281
2282 result = proc_base_lookup(dir, dentry);
2283 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2284 goto out;
2285
2286 tgid = name_to_int(dentry);
2287 if (tgid == ~0U)
2288 goto out;
2289
2290 rcu_read_lock();
2291 task = find_task_by_pid(tgid);
2292 if (task)
2293 get_task_struct(task);
2294 rcu_read_unlock();
2295 if (!task)
2296 goto out;
2297
2298 result = proc_pid_instantiate(dir, dentry, task, NULL);
2299 put_task_struct(task);
2300 out:
2301 return result;
2302 }
2303
2304 /*
2305 * Find the first task with tgid >= tgid
2306 *
2307 */
2308 static struct task_struct *next_tgid(unsigned int tgid)
2309 {
2310 struct task_struct *task;
2311 struct pid *pid;
2312
2313 rcu_read_lock();
2314 retry:
2315 task = NULL;
2316 pid = find_ge_pid(tgid);
2317 if (pid) {
2318 tgid = pid->nr + 1;
2319 task = pid_task(pid, PIDTYPE_PID);
2320 /* What we to know is if the pid we have find is the
2321 * pid of a thread_group_leader. Testing for task
2322 * being a thread_group_leader is the obvious thing
2323 * todo but there is a window when it fails, due to
2324 * the pid transfer logic in de_thread.
2325 *
2326 * So we perform the straight forward test of seeing
2327 * if the pid we have found is the pid of a thread
2328 * group leader, and don't worry if the task we have
2329 * found doesn't happen to be a thread group leader.
2330 * As we don't care in the case of readdir.
2331 */
2332 if (!task || !has_group_leader_pid(task))
2333 goto retry;
2334 get_task_struct(task);
2335 }
2336 rcu_read_unlock();
2337 return task;
2338 }
2339
2340 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2341
2342 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2343 struct task_struct *task, int tgid)
2344 {
2345 char name[PROC_NUMBUF];
2346 int len = snprintf(name, sizeof(name), "%d", tgid);
2347 return proc_fill_cache(filp, dirent, filldir, name, len,
2348 proc_pid_instantiate, task, NULL);
2349 }
2350
2351 /* for the /proc/ directory itself, after non-process stuff has been done */
2352 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2353 {
2354 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2355 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2356 struct task_struct *task;
2357 int tgid;
2358
2359 if (!reaper)
2360 goto out_no_task;
2361
2362 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2363 const struct pid_entry *p = &proc_base_stuff[nr];
2364 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2365 goto out;
2366 }
2367
2368 tgid = filp->f_pos - TGID_OFFSET;
2369 for (task = next_tgid(tgid);
2370 task;
2371 put_task_struct(task), task = next_tgid(tgid + 1)) {
2372 tgid = task->pid;
2373 filp->f_pos = tgid + TGID_OFFSET;
2374 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2375 put_task_struct(task);
2376 goto out;
2377 }
2378 }
2379 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2380 out:
2381 put_task_struct(reaper);
2382 out_no_task:
2383 return 0;
2384 }
2385
2386 /*
2387 * Tasks
2388 */
2389 static const struct pid_entry tid_base_stuff[] = {
2390 DIR("fd", S_IRUSR|S_IXUSR, fd),
2391 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2392 REG("environ", S_IRUSR, environ),
2393 INF("auxv", S_IRUSR, pid_auxv),
2394 INF("status", S_IRUGO, pid_status),
2395 #ifdef CONFIG_SCHED_DEBUG
2396 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2397 #endif
2398 INF("cmdline", S_IRUGO, pid_cmdline),
2399 INF("stat", S_IRUGO, tid_stat),
2400 INF("statm", S_IRUGO, pid_statm),
2401 REG("maps", S_IRUGO, maps),
2402 #ifdef CONFIG_NUMA
2403 REG("numa_maps", S_IRUGO, numa_maps),
2404 #endif
2405 REG("mem", S_IRUSR|S_IWUSR, mem),
2406 LNK("cwd", cwd),
2407 LNK("root", root),
2408 LNK("exe", exe),
2409 REG("mounts", S_IRUGO, mounts),
2410 #ifdef CONFIG_MMU
2411 REG("clear_refs", S_IWUSR, clear_refs),
2412 REG("smaps", S_IRUGO, smaps),
2413 #endif
2414 #ifdef CONFIG_SECURITY
2415 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2416 #endif
2417 #ifdef CONFIG_KALLSYMS
2418 INF("wchan", S_IRUGO, pid_wchan),
2419 #endif
2420 #ifdef CONFIG_SCHEDSTATS
2421 INF("schedstat", S_IRUGO, pid_schedstat),
2422 #endif
2423 #ifdef CONFIG_CPUSETS
2424 REG("cpuset", S_IRUGO, cpuset),
2425 #endif
2426 #ifdef CONFIG_CGROUPS
2427 REG("cgroup", S_IRUGO, cgroup),
2428 #endif
2429 INF("oom_score", S_IRUGO, oom_score),
2430 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2431 #ifdef CONFIG_AUDITSYSCALL
2432 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2433 #endif
2434 #ifdef CONFIG_FAULT_INJECTION
2435 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2436 #endif
2437 };
2438
2439 static int proc_tid_base_readdir(struct file * filp,
2440 void * dirent, filldir_t filldir)
2441 {
2442 return proc_pident_readdir(filp,dirent,filldir,
2443 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2444 }
2445
2446 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2447 return proc_pident_lookup(dir, dentry,
2448 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2449 }
2450
2451 static const struct file_operations proc_tid_base_operations = {
2452 .read = generic_read_dir,
2453 .readdir = proc_tid_base_readdir,
2454 };
2455
2456 static const struct inode_operations proc_tid_base_inode_operations = {
2457 .lookup = proc_tid_base_lookup,
2458 .getattr = pid_getattr,
2459 .setattr = proc_setattr,
2460 };
2461
2462 static struct dentry *proc_task_instantiate(struct inode *dir,
2463 struct dentry *dentry, struct task_struct *task, const void *ptr)
2464 {
2465 struct dentry *error = ERR_PTR(-ENOENT);
2466 struct inode *inode;
2467 inode = proc_pid_make_inode(dir->i_sb, task);
2468
2469 if (!inode)
2470 goto out;
2471 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2472 inode->i_op = &proc_tid_base_inode_operations;
2473 inode->i_fop = &proc_tid_base_operations;
2474 inode->i_flags|=S_IMMUTABLE;
2475 inode->i_nlink = 4;
2476 #ifdef CONFIG_SECURITY
2477 inode->i_nlink += 1;
2478 #endif
2479
2480 dentry->d_op = &pid_dentry_operations;
2481
2482 d_add(dentry, inode);
2483 /* Close the race of the process dying before we return the dentry */
2484 if (pid_revalidate(dentry, NULL))
2485 error = NULL;
2486 out:
2487 return error;
2488 }
2489
2490 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2491 {
2492 struct dentry *result = ERR_PTR(-ENOENT);
2493 struct task_struct *task;
2494 struct task_struct *leader = get_proc_task(dir);
2495 unsigned tid;
2496
2497 if (!leader)
2498 goto out_no_task;
2499
2500 tid = name_to_int(dentry);
2501 if (tid == ~0U)
2502 goto out;
2503
2504 rcu_read_lock();
2505 task = find_task_by_pid(tid);
2506 if (task)
2507 get_task_struct(task);
2508 rcu_read_unlock();
2509 if (!task)
2510 goto out;
2511 if (leader->tgid != task->tgid)
2512 goto out_drop_task;
2513
2514 result = proc_task_instantiate(dir, dentry, task, NULL);
2515 out_drop_task:
2516 put_task_struct(task);
2517 out:
2518 put_task_struct(leader);
2519 out_no_task:
2520 return result;
2521 }
2522
2523 /*
2524 * Find the first tid of a thread group to return to user space.
2525 *
2526 * Usually this is just the thread group leader, but if the users
2527 * buffer was too small or there was a seek into the middle of the
2528 * directory we have more work todo.
2529 *
2530 * In the case of a short read we start with find_task_by_pid.
2531 *
2532 * In the case of a seek we start with the leader and walk nr
2533 * threads past it.
2534 */
2535 static struct task_struct *first_tid(struct task_struct *leader,
2536 int tid, int nr)
2537 {
2538 struct task_struct *pos;
2539
2540 rcu_read_lock();
2541 /* Attempt to start with the pid of a thread */
2542 if (tid && (nr > 0)) {
2543 pos = find_task_by_pid(tid);
2544 if (pos && (pos->group_leader == leader))
2545 goto found;
2546 }
2547
2548 /* If nr exceeds the number of threads there is nothing todo */
2549 pos = NULL;
2550 if (nr && nr >= get_nr_threads(leader))
2551 goto out;
2552
2553 /* If we haven't found our starting place yet start
2554 * with the leader and walk nr threads forward.
2555 */
2556 for (pos = leader; nr > 0; --nr) {
2557 pos = next_thread(pos);
2558 if (pos == leader) {
2559 pos = NULL;
2560 goto out;
2561 }
2562 }
2563 found:
2564 get_task_struct(pos);
2565 out:
2566 rcu_read_unlock();
2567 return pos;
2568 }
2569
2570 /*
2571 * Find the next thread in the thread list.
2572 * Return NULL if there is an error or no next thread.
2573 *
2574 * The reference to the input task_struct is released.
2575 */
2576 static struct task_struct *next_tid(struct task_struct *start)
2577 {
2578 struct task_struct *pos = NULL;
2579 rcu_read_lock();
2580 if (pid_alive(start)) {
2581 pos = next_thread(start);
2582 if (thread_group_leader(pos))
2583 pos = NULL;
2584 else
2585 get_task_struct(pos);
2586 }
2587 rcu_read_unlock();
2588 put_task_struct(start);
2589 return pos;
2590 }
2591
2592 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2593 struct task_struct *task, int tid)
2594 {
2595 char name[PROC_NUMBUF];
2596 int len = snprintf(name, sizeof(name), "%d", tid);
2597 return proc_fill_cache(filp, dirent, filldir, name, len,
2598 proc_task_instantiate, task, NULL);
2599 }
2600
2601 /* for the /proc/TGID/task/ directories */
2602 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2603 {
2604 struct dentry *dentry = filp->f_path.dentry;
2605 struct inode *inode = dentry->d_inode;
2606 struct task_struct *leader = NULL;
2607 struct task_struct *task;
2608 int retval = -ENOENT;
2609 ino_t ino;
2610 int tid;
2611 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2612
2613 task = get_proc_task(inode);
2614 if (!task)
2615 goto out_no_task;
2616 rcu_read_lock();
2617 if (pid_alive(task)) {
2618 leader = task->group_leader;
2619 get_task_struct(leader);
2620 }
2621 rcu_read_unlock();
2622 put_task_struct(task);
2623 if (!leader)
2624 goto out_no_task;
2625 retval = 0;
2626
2627 switch (pos) {
2628 case 0:
2629 ino = inode->i_ino;
2630 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2631 goto out;
2632 pos++;
2633 /* fall through */
2634 case 1:
2635 ino = parent_ino(dentry);
2636 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2637 goto out;
2638 pos++;
2639 /* fall through */
2640 }
2641
2642 /* f_version caches the tgid value that the last readdir call couldn't
2643 * return. lseek aka telldir automagically resets f_version to 0.
2644 */
2645 tid = (int)filp->f_version;
2646 filp->f_version = 0;
2647 for (task = first_tid(leader, tid, pos - 2);
2648 task;
2649 task = next_tid(task), pos++) {
2650 tid = task->pid;
2651 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2652 /* returning this tgid failed, save it as the first
2653 * pid for the next readir call */
2654 filp->f_version = (u64)tid;
2655 put_task_struct(task);
2656 break;
2657 }
2658 }
2659 out:
2660 filp->f_pos = pos;
2661 put_task_struct(leader);
2662 out_no_task:
2663 return retval;
2664 }
2665
2666 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2667 {
2668 struct inode *inode = dentry->d_inode;
2669 struct task_struct *p = get_proc_task(inode);
2670 generic_fillattr(inode, stat);
2671
2672 if (p) {
2673 rcu_read_lock();
2674 stat->nlink += get_nr_threads(p);
2675 rcu_read_unlock();
2676 put_task_struct(p);
2677 }
2678
2679 return 0;
2680 }
2681
2682 static const struct inode_operations proc_task_inode_operations = {
2683 .lookup = proc_task_lookup,
2684 .getattr = proc_task_getattr,
2685 .setattr = proc_setattr,
2686 };
2687
2688 static const struct file_operations proc_task_operations = {
2689 .read = generic_read_dir,
2690 .readdir = proc_task_readdir,
2691 };