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