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