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