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