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