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