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