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