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