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