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